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