/WebVox/src/com/marvin/webvox/Bookmarks.java

http://eyes-free.googlecode.com/ · Java · 219 lines · 141 code · 10 blank · 68 comment · 25 complexity · 1f91aed97fa41dafa0728264899368e0 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.marvin.webvox;
  17. import com.marvin.webvox.R;
  18. import android.content.ContentResolver;
  19. import android.content.ContentUris;
  20. import android.content.ContentValues;
  21. import android.content.Context;
  22. import android.database.Cursor;
  23. import android.graphics.Bitmap;
  24. import android.net.Uri;
  25. import android.provider.Browser;
  26. import android.util.Log;
  27. import android.webkit.WebIconDatabase;
  28. import android.widget.Toast;
  29. import java.io.ByteArrayOutputStream;
  30. import java.util.Date;
  31. /**
  32. * This class is purely to have a common place for adding/deleting bookmarks.
  33. */
  34. /* package */ class Bookmarks {
  35. private static final String WHERE_CLAUSE
  36. = "url = ? OR url = ? OR url = ? OR url = ?";
  37. private static final String WHERE_CLAUSE_SECURE = "url = ? OR url = ?";
  38. private static String[] SELECTION_ARGS;
  39. /**
  40. * Add a bookmark to the database.
  41. * @param context Context of the calling Activity. This is used to make
  42. * Toast confirming that the bookmark has been added. If the
  43. * caller provides null, the Toast will not be shown.
  44. * @param cr The ContentResolver being used to add the bookmark to the db.
  45. * @param url URL of the website to be bookmarked.
  46. * @param name Provided name for the bookmark.
  47. * @param thumbnail A thumbnail for the bookmark.
  48. * @param retainIcon Whether to retain the page's icon in the icon database.
  49. * This will usually be <code>true</code> except when bookmarks are
  50. * added by a settings restore agent.
  51. */
  52. /* package */ static void addBookmark(Context context,
  53. ContentResolver cr, String url, String name,
  54. Bitmap thumbnail, boolean retainIcon) {
  55. // Want to append to the beginning of the list
  56. long creationTime = new Date().getTime();
  57. // First we check to see if the user has already visited this
  58. // site. They may have bookmarked it in a different way from
  59. // how it's stored in the database, so allow different combos
  60. // to map to the same url.
  61. boolean secure = false;
  62. String compareString = url;
  63. if (compareString.startsWith("http://")) {
  64. compareString = compareString.substring(7);
  65. } else if (compareString.startsWith("https://")) {
  66. compareString = compareString.substring(8);
  67. secure = true;
  68. }
  69. if (compareString.startsWith("www.")) {
  70. compareString = compareString.substring(4);
  71. }
  72. if (secure) {
  73. SELECTION_ARGS = new String[2];
  74. SELECTION_ARGS[0] = "https://" + compareString;
  75. SELECTION_ARGS[1] = "https://www." + compareString;
  76. } else {
  77. SELECTION_ARGS = new String[4];
  78. SELECTION_ARGS[0] = compareString;
  79. SELECTION_ARGS[1] = "www." + compareString;
  80. SELECTION_ARGS[2] = "http://" + compareString;
  81. SELECTION_ARGS[3] = "http://" + SELECTION_ARGS[1];
  82. }
  83. Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
  84. Browser.HISTORY_PROJECTION,
  85. secure ? WHERE_CLAUSE_SECURE : WHERE_CLAUSE,
  86. SELECTION_ARGS,
  87. null);
  88. ContentValues map = new ContentValues();
  89. if (cursor.moveToFirst() && cursor.getInt(
  90. Browser.HISTORY_PROJECTION_BOOKMARK_INDEX) == 0) {
  91. // This means we have been to this site but not bookmarked
  92. // it, so convert the history item to a bookmark
  93. map.put(Browser.BookmarkColumns.CREATED, creationTime);
  94. map.put(Browser.BookmarkColumns.TITLE, name);
  95. map.put(Browser.BookmarkColumns.BOOKMARK, 1);
  96. // map.put(Browser.BookmarkColumns.THUMBNAIL, bitmapToBytes(thumbnail));
  97. cr.update(Browser.BOOKMARKS_URI, map,
  98. "_id = " + cursor.getInt(0), null);
  99. } else {
  100. int count = cursor.getCount();
  101. boolean matchedTitle = false;
  102. for (int i = 0; i < count; i++) {
  103. // One or more bookmarks already exist for this site.
  104. // Check the names of each
  105. cursor.moveToPosition(i);
  106. if (cursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX)
  107. .equals(name)) {
  108. // The old bookmark has the same name.
  109. // Update its creation time.
  110. map.put(Browser.BookmarkColumns.CREATED,
  111. creationTime);
  112. cr.update(Browser.BOOKMARKS_URI, map,
  113. "_id = " + cursor.getInt(0), null);
  114. matchedTitle = true;
  115. break;
  116. }
  117. }
  118. if (!matchedTitle) {
  119. // Adding a bookmark for a site the user has visited,
  120. // or a new bookmark (with a different name) for a site
  121. // the user has visited
  122. map.put(Browser.BookmarkColumns.TITLE, name);
  123. map.put(Browser.BookmarkColumns.URL, url);
  124. map.put(Browser.BookmarkColumns.CREATED, creationTime);
  125. map.put(Browser.BookmarkColumns.BOOKMARK, 1);
  126. map.put(Browser.BookmarkColumns.DATE, 0);
  127. // map.put(Browser.BookmarkColumns.THUMBNAIL, bitmapToBytes(thumbnail));
  128. int visits = 0;
  129. if (count > 0) {
  130. // The user has already bookmarked, and possibly
  131. // visited this site. However, they are creating
  132. // a new bookmark with the same url but a different
  133. // name. The new bookmark should have the same
  134. // number of visits as the already created bookmark.
  135. visits = cursor.getInt(
  136. Browser.HISTORY_PROJECTION_VISITS_INDEX);
  137. }
  138. // Bookmark starts with 3 extra visits so that it will
  139. // bubble up in the most visited and goto search box
  140. map.put(Browser.BookmarkColumns.VISITS, visits + 3);
  141. cr.insert(Browser.BOOKMARKS_URI, map);
  142. }
  143. }
  144. if (retainIcon) {
  145. WebIconDatabase.getInstance().retainIconForPageUrl(url);
  146. }
  147. cursor.deactivate();
  148. if (context != null) {
  149. Toast.makeText(context, R.string.added_to_bookmarks,
  150. Toast.LENGTH_LONG).show();
  151. }
  152. }
  153. /**
  154. * Remove a bookmark from the database. If the url is a visited site, it
  155. * will remain in the database, but only as a history item, and not as a
  156. * bookmarked site.
  157. * @param context Context of the calling Activity. This is used to make
  158. * Toast confirming that the bookmark has been removed. If the
  159. * caller provides null, the Toast will not be shown.
  160. * @param cr The ContentResolver being used to remove the bookmark.
  161. * @param url URL of the website to be removed.
  162. */
  163. /* package */ static void removeFromBookmarks(Context context,
  164. ContentResolver cr, String url, String title) {
  165. Cursor cursor = cr.query(
  166. Browser.BOOKMARKS_URI,
  167. Browser.HISTORY_PROJECTION,
  168. "url = ? AND title = ?",
  169. new String[] { url, title },
  170. null);
  171. boolean first = cursor.moveToFirst();
  172. // Should be in the database no matter what
  173. if (!first) {
  174. throw new AssertionError("URL is not in the database! " + url + " " + title);
  175. }
  176. // Remove from bookmarks
  177. WebIconDatabase.getInstance().releaseIconForPageUrl(url);
  178. Uri uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
  179. cursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX));
  180. int numVisits = cursor.getInt(
  181. Browser.HISTORY_PROJECTION_VISITS_INDEX);
  182. if (0 == numVisits) {
  183. cr.delete(uri, null, null);
  184. } else {
  185. // It is no longer a bookmark, but it is still a visited
  186. // site.
  187. ContentValues values = new ContentValues();
  188. values.put(Browser.BookmarkColumns.BOOKMARK, 0);
  189. try {
  190. cr.update(uri, values, null, null);
  191. } catch (IllegalStateException e) {
  192. Log.e("removeFromBookmarks", "no database!");
  193. }
  194. }
  195. if (context != null) {
  196. Toast.makeText(context, R.string.removed_from_bookmarks,
  197. Toast.LENGTH_LONG).show();
  198. }
  199. cursor.deactivate();
  200. }
  201. private static byte[] bitmapToBytes(Bitmap bm) {
  202. if (bm == null) {
  203. return null;
  204. }
  205. final ByteArrayOutputStream os = new ByteArrayOutputStream();
  206. bm.compress(Bitmap.CompressFormat.PNG, 100, os);
  207. return os.toByteArray();
  208. }
  209. }