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