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