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; |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 26 | import android.os.AsyncTask; |
Jeff Hamilton | 69bd707 | 2010-08-17 12:38:22 -0500 | [diff] [blame] | 27 | import android.provider.BrowserContract; |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | import android.webkit.WebIconDatabase; |
| 30 | import android.widget.Toast; |
| 31 | |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 32 | import java.io.ByteArrayOutputStream; |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 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 | 2c0f611 | 2010-03-12 18:09:39 -0500 | [diff] [blame] | 50 | private final static String LOGTAG = "Bookmarks"; |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 51 | /** |
| 52 | * Add a bookmark to the database. |
| 53 | * @param context Context of the calling Activity. This is used to make |
| 54 | * Toast confirming that the bookmark has been added. If the |
| 55 | * caller provides null, the Toast will not be shown. |
| 56 | * @param cr The ContentResolver being used to add the bookmark to the db. |
| 57 | * @param url URL of the website to be bookmarked. |
| 58 | * @param name Provided name for the bookmark. |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 59 | * @param thumbnail A thumbnail for the bookmark. |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 60 | * @param retainIcon Whether to retain the page's icon in the icon database. |
| 61 | * This will usually be <code>true</code> except when bookmarks are |
| 62 | * added by a settings restore agent. |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 63 | */ |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 64 | /* package */ static void addBookmark(Context context, ContentResolver cr, String url, |
| 65 | String name, 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 |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 67 | ContentValues values = new ContentValues(); |
Leon Scroggins | 2c0f611 | 2010-03-12 18:09:39 -0500 | [diff] [blame] | 68 | Cursor cursor = null; |
| 69 | try { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 70 | values.put(BrowserContract.Bookmarks.TITLE, name); |
| 71 | values.put(BrowserContract.Bookmarks.URL, url); |
| 72 | values.put(BrowserContract.Bookmarks.IS_FOLDER, 0); |
| 73 | values.put(BrowserContract.Bookmarks.THUMBNAIL, |
| 74 | bitmapToBytes(thumbnail)); |
| 75 | cr.insert(BrowserContract.Bookmarks.CONTENT_URI, values); |
Leon Scroggins | 2c0f611 | 2010-03-12 18:09:39 -0500 | [diff] [blame] | 76 | } catch (IllegalStateException e) { |
| 77 | Log.e(LOGTAG, "addBookmark", e); |
| 78 | } finally { |
| 79 | if (cursor != null) cursor.close(); |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 80 | } |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 81 | if (retainIcon) { |
| 82 | WebIconDatabase.getInstance().retainIconForPageUrl(url); |
| 83 | } |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 84 | if (context != null) { |
| 85 | Toast.makeText(context, R.string.added_to_bookmarks, |
| 86 | Toast.LENGTH_LONG).show(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Remove a bookmark from the database. If the url is a visited site, it |
| 92 | * will remain in the database, but only as a history item, and not as a |
| 93 | * bookmarked site. |
| 94 | * @param context Context of the calling Activity. This is used to make |
| 95 | * Toast confirming that the bookmark has been removed. If the |
| 96 | * caller provides null, the Toast will not be shown. |
| 97 | * @param cr The ContentResolver being used to remove the bookmark. |
| 98 | * @param url URL of the website to be removed. |
| 99 | */ |
| 100 | /* package */ static void removeFromBookmarks(Context context, |
Andrei Popescu | c952619 | 2009-09-23 15:52:16 +0100 | [diff] [blame] | 101 | ContentResolver cr, String url, String title) { |
Leon Scroggins | 2c0f611 | 2010-03-12 18:09:39 -0500 | [diff] [blame] | 102 | Cursor cursor = null; |
| 103 | try { |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 104 | cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI, |
| 105 | new String[] { BrowserContract.Bookmarks._ID }, |
| 106 | BrowserContract.Bookmarks.URL + " = ? AND " + |
| 107 | BrowserContract.Bookmarks.TITLE + " = ?", |
Leon Scroggins | 2c0f611 | 2010-03-12 18:09:39 -0500 | [diff] [blame] | 108 | new String[] { url, title }, |
| 109 | null); |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 110 | |
Leon Scroggins | 2c0f611 | 2010-03-12 18:09:39 -0500 | [diff] [blame] | 111 | // Should be in the database no matter what |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 112 | if (!cursor.moveToFirst()) { |
Leon Scroggins | 2c0f611 | 2010-03-12 18:09:39 -0500 | [diff] [blame] | 113 | throw new AssertionError("URL is not in the database! " + url |
| 114 | + " " + title); |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 115 | } |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 116 | |
Leon Scroggins | 2c0f611 | 2010-03-12 18:09:39 -0500 | [diff] [blame] | 117 | // Remove from bookmarks |
| 118 | WebIconDatabase.getInstance().releaseIconForPageUrl(url); |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 119 | Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI, |
| 120 | cursor.getLong(0)); |
| 121 | cr.delete(uri, null, null); |
Leon Scroggins | 2c0f611 | 2010-03-12 18:09:39 -0500 | [diff] [blame] | 122 | if (context != null) { |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 123 | Toast.makeText(context, R.string.removed_from_bookmarks, Toast.LENGTH_LONG).show(); |
Leon Scroggins | 2c0f611 | 2010-03-12 18:09:39 -0500 | [diff] [blame] | 124 | } |
| 125 | } catch (IllegalStateException e) { |
| 126 | Log.e(LOGTAG, "removeFromBookmarks", e); |
| 127 | } finally { |
| 128 | if (cursor != null) cursor.close(); |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 129 | } |
Leon Scroggins | e372c02 | 2009-06-12 17:07:29 -0400 | [diff] [blame] | 130 | } |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 131 | |
| 132 | private static byte[] bitmapToBytes(Bitmap bm) { |
| 133 | if (bm == null) { |
| 134 | return null; |
| 135 | } |
| 136 | |
| 137 | final ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 138 | bm.compress(Bitmap.CompressFormat.PNG, 100, os); |
| 139 | return os.toByteArray(); |
| 140 | } |
Ben Murdoch | de35362 | 2009-10-12 10:29:00 +0100 | [diff] [blame] | 141 | |
| 142 | /* package */ static boolean urlHasAcceptableScheme(String url) { |
| 143 | if (url == null) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | for (int i = 0; i < acceptableBookmarkSchemes.length; i++) { |
| 148 | if (url.startsWith(acceptableBookmarkSchemes[i])) { |
| 149 | return true; |
| 150 | } |
| 151 | } |
| 152 | return false; |
| 153 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 154 | |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 155 | static final String QUERY_BOOKMARKS_WHERE = |
| 156 | BrowserContract.Bookmarks.IS_FOLDER + " == 0 AND (" + |
| 157 | BrowserContract.Bookmarks.URL + " == ? OR " + |
| 158 | BrowserContract.Bookmarks.URL + " == ? OR " + |
| 159 | BrowserContract.Bookmarks.URL + " LIKE ? || '%' OR " + |
| 160 | BrowserContract.Bookmarks.URL + " LIKE ? || '%')"; |
| 161 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 162 | /* package */ static Cursor queryBookmarksForUrl(ContentResolver cr, |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 163 | String originalUrl, String url) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 164 | if (cr == null || url == null) { |
| 165 | return null; |
| 166 | } |
| 167 | |
| 168 | // If originalUrl is null, just set it to url. |
| 169 | if (originalUrl == null) { |
| 170 | originalUrl = url; |
| 171 | } |
| 172 | |
| 173 | // Look for both the original url and the actual url. This takes in to |
| 174 | // account redirects. |
| 175 | String originalUrlNoQuery = Bookmarks.removeQuery(originalUrl); |
| 176 | String urlNoQuery = Bookmarks.removeQuery(url); |
| 177 | originalUrl = originalUrlNoQuery + '?'; |
| 178 | url = urlNoQuery + '?'; |
| 179 | |
| 180 | // Use NoQuery to search for the base url (i.e. if the url is |
| 181 | // http://www.yahoo.com/?rs=1, search for http://www.yahoo.com) |
| 182 | // Use url to match the base url with other queries (i.e. if the url is |
| 183 | // http://www.google.com/m, search for |
| 184 | // http://www.google.com/m?some_query) |
| 185 | final String[] selArgs = new String[] { |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 186 | originalUrlNoQuery, urlNoQuery, originalUrl, url }; |
| 187 | final String[] projection = new String[] { BrowserContract.Bookmarks._ID }; |
| 188 | return cr.query(BrowserContract.Bookmarks.CONTENT_URI, projection, QUERY_BOOKMARKS_WHERE, |
| 189 | selArgs, null); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // Strip the query from the given url. |
| 193 | static String removeQuery(String url) { |
| 194 | if (url == null) { |
| 195 | return null; |
| 196 | } |
| 197 | int query = url.indexOf('?'); |
| 198 | String noQuery = url; |
| 199 | if (query != -1) { |
| 200 | noQuery = url.substring(0, query); |
| 201 | } |
| 202 | return noQuery; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Update the bookmark's favicon. This is a convenience method for updating |
| 207 | * a bookmark favicon for the originalUrl and url of the passed in WebView. |
| 208 | * @param cr The ContentResolver to use. |
| 209 | * @param originalUrl The original url before any redirects. |
| 210 | * @param url The current url. |
| 211 | * @param favicon The favicon bitmap to write to the db. |
| 212 | */ |
| 213 | /* package */ static void updateBookmarkFavicon(final ContentResolver cr, |
| 214 | final String originalUrl, final String url, final Bitmap favicon) { |
| 215 | new AsyncTask<Void, Void, Void>() { |
| 216 | @Override |
| 217 | protected Void doInBackground(Void... unused) { |
| 218 | final Cursor c = |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 219 | Bookmarks.queryBookmarksForUrl(cr, originalUrl, url); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 220 | if (c == null) { |
| 221 | return null; |
| 222 | } |
| 223 | if (c.moveToFirst()) { |
| 224 | ContentValues values = new ContentValues(); |
| 225 | final ByteArrayOutputStream os = |
| 226 | new ByteArrayOutputStream(); |
| 227 | favicon.compress(Bitmap.CompressFormat.PNG, 100, os); |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 228 | values.put(BrowserContract.Bookmarks.FAVICON, |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 229 | os.toByteArray()); |
| 230 | do { |
| 231 | cr.update(ContentUris.withAppendedId( |
Jeff Hamilton | 8ce956c | 2010-08-17 11:13:53 -0500 | [diff] [blame] | 232 | BrowserContract.Bookmarks.CONTENT_URI, c.getLong(0)), |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 233 | values, null, null); |
| 234 | } while (c.moveToNext()); |
| 235 | } |
| 236 | c.close(); |
| 237 | return null; |
| 238 | } |
| 239 | }.execute(); |
| 240 | } |
Ben Murdoch | aac7aa6 | 2009-09-17 16:57:40 +0100 | [diff] [blame] | 241 | } |