diff options
| author | Leon Scroggins <scroggo@google.com> | 2010-03-12 18:09:39 -0500 |
|---|---|---|
| committer | Leon Scroggins <scroggo@google.com> | 2010-03-15 10:42:53 -0400 |
| commit | 2c0f611fab8c0a24365c0dd5cbf0a90f70f314c8 (patch) | |
| tree | 724688157cb7f665c8c1743ace0580a42579e902 /src/com/android/browser/Bookmarks.java | |
| parent | 98e1137fe2221b6370a7e19ded46313d7bc5e4a6 (diff) | |
| download | packages_apps_Browser-2c0f611fab8c0a24365c0dd5cbf0a90f70f314c8.tar.gz packages_apps_Browser-2c0f611fab8c0a24365c0dd5cbf0a90f70f314c8.tar.bz2 packages_apps_Browser-2c0f611fab8c0a24365c0dd5cbf0a90f70f314c8.zip | |
Close Cursors when finished with them.
Change-Id: Idf284f59b05b3f5b3565f7374899927a70f3ca07
Diffstat (limited to 'src/com/android/browser/Bookmarks.java')
| -rw-r--r-- | src/com/android/browser/Bookmarks.java | 186 |
1 files changed, 101 insertions, 85 deletions
diff --git a/src/com/android/browser/Bookmarks.java b/src/com/android/browser/Bookmarks.java index 2cbd85122..5ada9dcb8 100644 --- a/src/com/android/browser/Bookmarks.java +++ b/src/com/android/browser/Bookmarks.java @@ -47,6 +47,7 @@ import java.util.Date; "content:" }; + private final static String LOGTAG = "Bookmarks"; /** * Add a bookmark to the database. * @param context Context of the calling Activity. This is used to make @@ -66,66 +67,74 @@ import java.util.Date; // Want to append to the beginning of the list long creationTime = new Date().getTime(); ContentValues map = new ContentValues(); - Cursor cursor = Browser.getVisitedLike(cr, url); - if (cursor.moveToFirst() && cursor.getInt( - Browser.HISTORY_PROJECTION_BOOKMARK_INDEX) == 0) { - // This means we have been to this site but not bookmarked - // it, so convert the history item to a bookmark - map.put(Browser.BookmarkColumns.CREATED, creationTime); - map.put(Browser.BookmarkColumns.TITLE, name); - map.put(Browser.BookmarkColumns.BOOKMARK, 1); - map.put(Browser.BookmarkColumns.THUMBNAIL, bitmapToBytes(thumbnail)); - cr.update(Browser.BOOKMARKS_URI, map, - "_id = " + cursor.getInt(0), null); - } else { - int count = cursor.getCount(); - boolean matchedTitle = false; - for (int i = 0; i < count; i++) { - // One or more bookmarks already exist for this site. - // Check the names of each - cursor.moveToPosition(i); - if (cursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX) - .equals(name)) { - // The old bookmark has the same name. - // Update its creation time. - map.put(Browser.BookmarkColumns.CREATED, - creationTime); - cr.update(Browser.BOOKMARKS_URI, map, - "_id = " + cursor.getInt(0), null); - matchedTitle = true; - break; - } - } - if (!matchedTitle) { - // Adding a bookmark for a site the user has visited, - // or a new bookmark (with a different name) for a site - // the user has visited - map.put(Browser.BookmarkColumns.TITLE, name); - map.put(Browser.BookmarkColumns.URL, url); + Cursor cursor = null; + try { + cursor = Browser.getVisitedLike(cr, url); + if (cursor.moveToFirst() && cursor.getInt( + Browser.HISTORY_PROJECTION_BOOKMARK_INDEX) == 0) { + // This means we have been to this site but not bookmarked + // it, so convert the history item to a bookmark map.put(Browser.BookmarkColumns.CREATED, creationTime); + map.put(Browser.BookmarkColumns.TITLE, name); map.put(Browser.BookmarkColumns.BOOKMARK, 1); - map.put(Browser.BookmarkColumns.DATE, 0); - map.put(Browser.BookmarkColumns.THUMBNAIL, bitmapToBytes(thumbnail)); - int visits = 0; - if (count > 0) { - // The user has already bookmarked, and possibly - // visited this site. However, they are creating - // a new bookmark with the same url but a different - // name. The new bookmark should have the same - // number of visits as the already created bookmark. - visits = cursor.getInt( - Browser.HISTORY_PROJECTION_VISITS_INDEX); + map.put(Browser.BookmarkColumns.THUMBNAIL, + bitmapToBytes(thumbnail)); + cr.update(Browser.BOOKMARKS_URI, map, + "_id = " + cursor.getInt(0), null); + } else { + int count = cursor.getCount(); + boolean matchedTitle = false; + for (int i = 0; i < count; i++) { + // One or more bookmarks already exist for this site. + // Check the names of each + cursor.moveToPosition(i); + if (cursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX) + .equals(name)) { + // The old bookmark has the same name. + // Update its creation time. + map.put(Browser.BookmarkColumns.CREATED, + creationTime); + cr.update(Browser.BOOKMARKS_URI, map, + "_id = " + cursor.getInt(0), null); + matchedTitle = true; + break; + } + } + if (!matchedTitle) { + // Adding a bookmark for a site the user has visited, + // or a new bookmark (with a different name) for a site + // the user has visited + map.put(Browser.BookmarkColumns.TITLE, name); + map.put(Browser.BookmarkColumns.URL, url); + map.put(Browser.BookmarkColumns.CREATED, creationTime); + map.put(Browser.BookmarkColumns.BOOKMARK, 1); + map.put(Browser.BookmarkColumns.DATE, 0); + map.put(Browser.BookmarkColumns.THUMBNAIL, + bitmapToBytes(thumbnail)); + int visits = 0; + if (count > 0) { + // The user has already bookmarked, and possibly + // visited this site. However, they are creating + // a new bookmark with the same url but a different + // name. The new bookmark should have the same + // number of visits as the already created bookmark. + visits = cursor.getInt( + Browser.HISTORY_PROJECTION_VISITS_INDEX); + } + // Bookmark starts with 3 extra visits so that it will + // bubble up in the most visited and goto search box + map.put(Browser.BookmarkColumns.VISITS, visits + 3); + cr.insert(Browser.BOOKMARKS_URI, map); } - // Bookmark starts with 3 extra visits so that it will - // bubble up in the most visited and goto search box - map.put(Browser.BookmarkColumns.VISITS, visits + 3); - cr.insert(Browser.BOOKMARKS_URI, map); } + } catch (IllegalStateException e) { + Log.e(LOGTAG, "addBookmark", e); + } finally { + if (cursor != null) cursor.close(); } if (retainIcon) { WebIconDatabase.getInstance().retainIconForPageUrl(url); } - cursor.deactivate(); if (context != null) { Toast.makeText(context, R.string.added_to_bookmarks, Toast.LENGTH_LONG).show(); @@ -144,41 +153,48 @@ import java.util.Date; */ /* package */ static void removeFromBookmarks(Context context, ContentResolver cr, String url, String title) { - Cursor cursor = cr.query( - Browser.BOOKMARKS_URI, - Browser.HISTORY_PROJECTION, - "url = ? AND title = ?", - new String[] { url, title }, - null); - boolean first = cursor.moveToFirst(); - // Should be in the database no matter what - if (!first) { - throw new AssertionError("URL is not in the database! " + url + " " + title); - } - // Remove from bookmarks - WebIconDatabase.getInstance().releaseIconForPageUrl(url); - Uri uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI, - cursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX)); - int numVisits = cursor.getInt( - Browser.HISTORY_PROJECTION_VISITS_INDEX); - if (0 == numVisits) { - cr.delete(uri, null, null); - } else { - // It is no longer a bookmark, but it is still a visited - // site. - ContentValues values = new ContentValues(); - values.put(Browser.BookmarkColumns.BOOKMARK, 0); - try { - cr.update(uri, values, null, null); - } catch (IllegalStateException e) { - Log.e("removeFromBookmarks", "no database!"); + Cursor cursor = null; + try { + cursor = cr.query( + Browser.BOOKMARKS_URI, + Browser.HISTORY_PROJECTION, + "url = ? AND title = ?", + new String[] { url, title }, + null); + boolean first = cursor.moveToFirst(); + // Should be in the database no matter what + if (!first) { + throw new AssertionError("URL is not in the database! " + url + + " " + title); } + // Remove from bookmarks + WebIconDatabase.getInstance().releaseIconForPageUrl(url); + Uri uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI, + cursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX)); + int numVisits = cursor.getInt( + Browser.HISTORY_PROJECTION_VISITS_INDEX); + if (0 == numVisits) { + cr.delete(uri, null, null); + } else { + // It is no longer a bookmark, but it is still a visited + // site. + ContentValues values = new ContentValues(); + values.put(Browser.BookmarkColumns.BOOKMARK, 0); + try { + cr.update(uri, values, null, null); + } catch (IllegalStateException e) { + Log.e("removeFromBookmarks", "no database!"); + } + } + if (context != null) { + Toast.makeText(context, R.string.removed_from_bookmarks, + Toast.LENGTH_LONG).show(); + } + } catch (IllegalStateException e) { + Log.e(LOGTAG, "removeFromBookmarks", e); + } finally { + if (cursor != null) cursor.close(); } - if (context != null) { - Toast.makeText(context, R.string.removed_from_bookmarks, - Toast.LENGTH_LONG).show(); - } - cursor.deactivate(); } private static byte[] bitmapToBytes(Bitmap bm) { |
