Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.content.ContentResolver; |
| 20 | import android.content.ContentUris; |
| 21 | import android.content.ContentValues; |
| 22 | import android.content.Context; |
| 23 | import android.database.Cursor; |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 25 | import android.net.Uri; |
| 26 | import android.provider.Browser; |
| 27 | import android.util.Log; |
| 28 | import android.webkit.WebIconDatabase; |
| 29 | import android.widget.Toast; |
| 30 | |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 31 | import java.io.ByteArrayOutputStream; |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 32 | import java.util.Date; |
| 33 | |
| 34 | /** |
| 35 | * This class is purely to have a common place for adding/deleting bookmarks. |
| 36 | */ |
| 37 | /* package */ class Bookmarks { |
Ben Murdoch | de35362 | 2009-10-12 10:29:00 +0100 | [diff] [blame] | 38 | // We only want the user to be able to bookmark content that |
| 39 | // the browser can handle directly. |
| 40 | private static final String acceptableBookmarkSchemes[] = { |
| 41 | "http:", |
| 42 | "https:", |
| 43 | "about:", |
| 44 | "data:", |
| 45 | "javascript:", |
| 46 | "file:", |
| 47 | "content:" |
| 48 | }; |
| 49 | |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 50 | /** |
| 51 | * Add a bookmark to the database. |
| 52 | * @param context Context of the calling Activity. This is used to make |
| 53 | * Toast confirming that the bookmark has been added. If the |
| 54 | * caller provides null, the Toast will not be shown. |
| 55 | * @param cr The ContentResolver being used to add the bookmark to the db. |
| 56 | * @param url URL of the website to be bookmarked. |
| 57 | * @param name Provided name for the bookmark. |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 58 | * @param thumbnail A thumbnail for the bookmark. |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 59 | * @param retainIcon Whether to retain the page's icon in the icon database. |
| 60 | * This will usually be <code>true</code> except when bookmarks are |
| 61 | * added by a settings restore agent. |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 62 | */ |
| 63 | /* package */ static void addBookmark(Context context, |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 64 | ContentResolver cr, String url, String name, |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 65 | Bitmap thumbnail, boolean retainIcon) { |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 66 | // Want to append to the beginning of the list |
| 67 | long creationTime = new Date().getTime(); |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 68 | ContentValues map = new ContentValues(); |
Leon Scroggins | da3e030 | 2010-03-02 16:01:49 -0500 | [diff] [blame] | 69 | Cursor cursor = Browser.getVisitedLike(cr, url); |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 70 | if (cursor.moveToFirst() && cursor.getInt( |
| 71 | Browser.HISTORY_PROJECTION_BOOKMARK_INDEX) == 0) { |
| 72 | // This means we have been to this site but not bookmarked |
| 73 | // it, so convert the history item to a bookmark |
| 74 | map.put(Browser.BookmarkColumns.CREATED, creationTime); |
| 75 | map.put(Browser.BookmarkColumns.TITLE, name); |
| 76 | map.put(Browser.BookmarkColumns.BOOKMARK, 1); |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 77 | map.put(Browser.BookmarkColumns.THUMBNAIL, bitmapToBytes(thumbnail)); |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 78 | cr.update(Browser.BOOKMARKS_URI, map, |
| 79 | "_id = " + cursor.getInt(0), null); |
| 80 | } else { |
| 81 | int count = cursor.getCount(); |
| 82 | boolean matchedTitle = false; |
| 83 | for (int i = 0; i < count; i++) { |
| 84 | // One or more bookmarks already exist for this site. |
| 85 | // Check the names of each |
| 86 | cursor.moveToPosition(i); |
| 87 | if (cursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX) |
| 88 | .equals(name)) { |
| 89 | // The old bookmark has the same name. |
| 90 | // Update its creation time. |
| 91 | map.put(Browser.BookmarkColumns.CREATED, |
| 92 | creationTime); |
| 93 | cr.update(Browser.BOOKMARKS_URI, map, |
| 94 | "_id = " + cursor.getInt(0), null); |
| 95 | matchedTitle = true; |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | if (!matchedTitle) { |
| 100 | // Adding a bookmark for a site the user has visited, |
| 101 | // or a new bookmark (with a different name) for a site |
| 102 | // the user has visited |
| 103 | map.put(Browser.BookmarkColumns.TITLE, name); |
| 104 | map.put(Browser.BookmarkColumns.URL, url); |
| 105 | map.put(Browser.BookmarkColumns.CREATED, creationTime); |
| 106 | map.put(Browser.BookmarkColumns.BOOKMARK, 1); |
| 107 | map.put(Browser.BookmarkColumns.DATE, 0); |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 108 | map.put(Browser.BookmarkColumns.THUMBNAIL, bitmapToBytes(thumbnail)); |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 109 | int visits = 0; |
| 110 | if (count > 0) { |
| 111 | // The user has already bookmarked, and possibly |
| 112 | // visited this site. However, they are creating |
| 113 | // a new bookmark with the same url but a different |
| 114 | // name. The new bookmark should have the same |
| 115 | // number of visits as the already created bookmark. |
| 116 | visits = cursor.getInt( |
| 117 | Browser.HISTORY_PROJECTION_VISITS_INDEX); |
| 118 | } |
| 119 | // Bookmark starts with 3 extra visits so that it will |
| 120 | // bubble up in the most visited and goto search box |
| 121 | map.put(Browser.BookmarkColumns.VISITS, visits + 3); |
| 122 | cr.insert(Browser.BOOKMARKS_URI, map); |
| 123 | } |
| 124 | } |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 125 | if (retainIcon) { |
| 126 | WebIconDatabase.getInstance().retainIconForPageUrl(url); |
| 127 | } |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 128 | cursor.deactivate(); |
| 129 | if (context != null) { |
| 130 | Toast.makeText(context, R.string.added_to_bookmarks, |
| 131 | Toast.LENGTH_LONG).show(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Remove a bookmark from the database. If the url is a visited site, it |
| 137 | * will remain in the database, but only as a history item, and not as a |
| 138 | * bookmarked site. |
| 139 | * @param context Context of the calling Activity. This is used to make |
| 140 | * Toast confirming that the bookmark has been removed. If the |
| 141 | * caller provides null, the Toast will not be shown. |
| 142 | * @param cr The ContentResolver being used to remove the bookmark. |
| 143 | * @param url URL of the website to be removed. |
| 144 | */ |
| 145 | /* package */ static void removeFromBookmarks(Context context, |
Andrei Popescu | c952619 | 2009-09-23 15:52:16 +0100 | [diff] [blame] | 146 | ContentResolver cr, String url, String title) { |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 147 | Cursor cursor = cr.query( |
| 148 | Browser.BOOKMARKS_URI, |
| 149 | Browser.HISTORY_PROJECTION, |
Andrei Popescu | c952619 | 2009-09-23 15:52:16 +0100 | [diff] [blame] | 150 | "url = ? AND title = ?", |
| 151 | new String[] { url, title }, |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 152 | null); |
| 153 | boolean first = cursor.moveToFirst(); |
| 154 | // Should be in the database no matter what |
| 155 | if (!first) { |
Andrei Popescu | c952619 | 2009-09-23 15:52:16 +0100 | [diff] [blame] | 156 | throw new AssertionError("URL is not in the database! " + url + " " + title); |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 157 | } |
| 158 | // Remove from bookmarks |
| 159 | WebIconDatabase.getInstance().releaseIconForPageUrl(url); |
| 160 | Uri uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI, |
| 161 | cursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX)); |
| 162 | int numVisits = cursor.getInt( |
| 163 | Browser.HISTORY_PROJECTION_VISITS_INDEX); |
| 164 | if (0 == numVisits) { |
| 165 | cr.delete(uri, null, null); |
| 166 | } else { |
| 167 | // It is no longer a bookmark, but it is still a visited |
| 168 | // site. |
| 169 | ContentValues values = new ContentValues(); |
| 170 | values.put(Browser.BookmarkColumns.BOOKMARK, 0); |
| 171 | try { |
| 172 | cr.update(uri, values, null, null); |
| 173 | } catch (IllegalStateException e) { |
| 174 | Log.e("removeFromBookmarks", "no database!"); |
| 175 | } |
| 176 | } |
| 177 | if (context != null) { |
| 178 | Toast.makeText(context, R.string.removed_from_bookmarks, |
| 179 | Toast.LENGTH_LONG).show(); |
| 180 | } |
| 181 | cursor.deactivate(); |
| 182 | } |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 183 | |
| 184 | private static byte[] bitmapToBytes(Bitmap bm) { |
| 185 | if (bm == null) { |
| 186 | return null; |
| 187 | } |
| 188 | |
| 189 | final ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 190 | bm.compress(Bitmap.CompressFormat.PNG, 100, os); |
| 191 | return os.toByteArray(); |
| 192 | } |
Ben Murdoch | de35362 | 2009-10-12 10:29:00 +0100 | [diff] [blame] | 193 | |
| 194 | /* package */ static boolean urlHasAcceptableScheme(String url) { |
| 195 | if (url == null) { |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | for (int i = 0; i < acceptableBookmarkSchemes.length; i++) { |
| 200 | if (url.startsWith(acceptableBookmarkSchemes[i])) { |
| 201 | return true; |
| 202 | } |
| 203 | } |
| 204 | return false; |
| 205 | } |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 206 | } |