blob: afc99c37e7452e3a69d641d40fba9a4ff1830b7b [file] [log] [blame]
Leon Scrogginse372c022009-06-12 17:07:29 -04001/*
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 Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Leon Scrogginse372c022009-06-12 17:07:29 -040018
19import android.content.ContentResolver;
20import android.content.ContentUris;
21import android.content.ContentValues;
22import android.content.Context;
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050023import android.content.SharedPreferences;
Leon Scrogginse372c022009-06-12 17:07:29 -040024import android.database.Cursor;
Pankaj Garg6bedeba2015-06-23 15:47:37 -070025import android.database.sqlite.SQLiteException;
Ben Murdochaac7aa62009-09-17 16:57:40 +010026import android.graphics.Bitmap;
Leon Scrogginse372c022009-06-12 17:07:29 -040027import android.net.Uri;
Jeff Hamilton84029622010-08-05 14:29:28 -050028import android.os.AsyncTask;
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050029import android.preference.PreferenceManager;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080030
Bijan Amirzada41242f22014-03-21 12:12:18 -070031import com.android.browser.R;
32import com.android.browser.platformsupport.BrowserContract;
33import com.android.browser.platformsupport.BrowserContract.Combined;
34import com.android.browser.platformsupport.BrowserContract.Images;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080035
John Reck568467e2010-12-21 14:15:50 -080036import android.text.TextUtils;
Leon Scrogginse372c022009-06-12 17:07:29 -040037import android.util.Log;
Leon Scrogginse372c022009-06-12 17:07:29 -040038import android.widget.Toast;
39
Ben Murdochaac7aa62009-09-17 16:57:40 +010040import java.io.ByteArrayOutputStream;
Leon Scrogginse372c022009-06-12 17:07:29 -040041
42/**
43 * This class is purely to have a common place for adding/deleting bookmarks.
44 */
John Reck57928452011-02-17 12:44:07 -080045public class Bookmarks {
Ben Murdochde353622009-10-12 10:29:00 +010046 // We only want the user to be able to bookmark content that
47 // the browser can handle directly.
48 private static final String acceptableBookmarkSchemes[] = {
49 "http:",
50 "https:",
51 "about:",
52 "data:",
53 "javascript:",
54 "file:",
55 "content:"
56 };
57
Leon Scroggins2c0f6112010-03-12 18:09:39 -050058 private final static String LOGTAG = "Bookmarks";
Leon Scrogginse372c022009-06-12 17:07:29 -040059 /**
60 * Add a bookmark to the database.
61 * @param context Context of the calling Activity. This is used to make
62 * Toast confirming that the bookmark has been added. If the
63 * caller provides null, the Toast will not be shown.
Leon Scrogginse372c022009-06-12 17:07:29 -040064 * @param url URL of the website to be bookmarked.
65 * @param name Provided name for the bookmark.
Ben Murdochaac7aa62009-09-17 16:57:40 +010066 * @param thumbnail A thumbnail for the bookmark.
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070067 * @param retainIcon Whether to retain the page's icon in the icon database.
68 * This will usually be <code>true</code> except when bookmarks are
69 * added by a settings restore agent.
Leon Scroggins III052ce662010-09-13 14:44:16 -040070 * @param parent ID of the parent folder.
Leon Scrogginse372c022009-06-12 17:07:29 -040071 */
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050072 /* package */ static void addBookmark(Context context, boolean showToast, String url,
John Reckaf262e72011-07-25 13:55:44 -070073 String name, Bitmap thumbnail, long parent) {
Leon Scrogginse372c022009-06-12 17:07:29 -040074 // Want to append to the beginning of the list
Jeff Hamilton84029622010-08-05 14:29:28 -050075 ContentValues values = new ContentValues();
Leon Scroggins2c0f6112010-03-12 18:09:39 -050076 try {
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050077 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Jeff Hamilton84029622010-08-05 14:29:28 -050078 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 III052ce662010-09-13 14:44:16 -040083 values.put(BrowserContract.Bookmarks.PARENT, parent);
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050084 context.getContentResolver().insert(BrowserContract.Bookmarks.CONTENT_URI, values);
Leon Scroggins2c0f6112010-03-12 18:09:39 -050085 } catch (IllegalStateException e) {
86 Log.e(LOGTAG, "addBookmark", e);
Leon Scrogginse372c022009-06-12 17:07:29 -040087 }
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050088 if (showToast) {
Leon Scrogginse372c022009-06-12 17:07:29 -040089 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 Reck03b20542011-01-06 16:22:48 -080099 * Toast confirming that the bookmark has been removed and to
100 * lookup the correct content uri. It must not be null.
Leon Scrogginse372c022009-06-12 17:07:29 -0400101 * @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 Popescuc9526192009-09-23 15:52:16 +0100105 ContentResolver cr, String url, String title) {
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500106 Cursor cursor = null;
107 try {
John Reck4b59db82010-11-16 15:40:34 -0800108 Uri uri = BookmarkUtils.getBookmarksUri(context);
109 cursor = cr.query(uri,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500110 new String[] { BrowserContract.Bookmarks._ID },
111 BrowserContract.Bookmarks.URL + " = ? AND " +
112 BrowserContract.Bookmarks.TITLE + " = ?",
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500113 new String[] { url, title },
114 null);
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500115
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500116 if (!cursor.moveToFirst()) {
John Reck4b59db82010-11-16 15:40:34 -0800117 return;
Leon Scrogginse372c022009-06-12 17:07:29 -0400118 }
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500119
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500120 // Remove from bookmarks
John Reck4b59db82010-11-16 15:40:34 -0800121 uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500122 cursor.getLong(0));
123 cr.delete(uri, null, null);
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500124 if (context != null) {
John Reck03b20542011-01-06 16:22:48 -0800125 Toast.makeText(context, R.string.removed_from_bookmarks,
126 Toast.LENGTH_LONG).show();
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500127 }
128 } catch (IllegalStateException e) {
129 Log.e(LOGTAG, "removeFromBookmarks", e);
130 } finally {
131 if (cursor != null) cursor.close();
Leon Scrogginse372c022009-06-12 17:07:29 -0400132 }
Leon Scrogginse372c022009-06-12 17:07:29 -0400133 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100134
135 private static byte[] bitmapToBytes(Bitmap bm) {
136 if (bm == null) {
137 return null;
138 }
139
140 final ByteArrayOutputStream os = new ByteArrayOutputStream();
141 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
142 return os.toByteArray();
143 }
Ben Murdochde353622009-10-12 10:29:00 +0100144
145 /* package */ static boolean urlHasAcceptableScheme(String url) {
146 if (url == null) {
147 return false;
148 }
149
150 for (int i = 0; i < acceptableBookmarkSchemes.length; i++) {
151 if (url.startsWith(acceptableBookmarkSchemes[i])) {
152 return true;
153 }
154 }
155 return false;
156 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500157
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500158 static final String QUERY_BOOKMARKS_WHERE =
Jeff Hamilton1a805652010-09-07 12:36:30 -0700159 Combined.URL + " == ? OR " +
John Reck57928452011-02-17 12:44:07 -0800160 Combined.URL + " == ?";
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500161
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700162 private static String eatTrailingSlash(String input) {
163 if (TextUtils.isEmpty(input)) {
164 return input;
165 }
166
167 if (input.charAt(input.length() - 1) == '/') {
168 return input.substring(0, input.length() - 1);
169 }
170
171 return input;
172 }
173
John Reck57928452011-02-17 12:44:07 -0800174 public static Cursor queryCombinedForUrl(ContentResolver cr,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500175 String originalUrl, String url) {
Jeff Hamilton84029622010-08-05 14:29:28 -0500176 if (cr == null || url == null) {
177 return null;
178 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800179
Jeff Hamilton84029622010-08-05 14:29:28 -0500180 // If originalUrl is null, just set it to url.
181 if (originalUrl == null) {
182 originalUrl = url;
183 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800184
Jeff Hamilton84029622010-08-05 14:29:28 -0500185 // Look for both the original url and the actual url. This takes in to
186 // account redirects.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800187
John Reck57928452011-02-17 12:44:07 -0800188 final String[] selArgs = new String[] { originalUrl, url };
Jeff Hamilton1a805652010-09-07 12:36:30 -0700189 final String[] projection = new String[] { Combined.URL };
190 return cr.query(Combined.CONTENT_URI, projection, QUERY_BOOKMARKS_WHERE, selArgs, null);
Jeff Hamilton84029622010-08-05 14:29:28 -0500191 }
192
193 // Strip the query from the given url.
194 static String removeQuery(String url) {
195 if (url == null) {
196 return null;
197 }
198 int query = url.indexOf('?');
199 String noQuery = url;
200 if (query != -1) {
201 noQuery = url.substring(0, query);
202 }
203 return noQuery;
204 }
205
206 /**
207 * Update the bookmark's favicon. This is a convenience method for updating
208 * a bookmark favicon for the originalUrl and url of the passed in WebView.
209 * @param cr The ContentResolver to use.
210 * @param originalUrl The original url before any redirects.
211 * @param url The current url.
212 * @param favicon The favicon bitmap to write to the db.
213 */
Jeff Hamilton1a805652010-09-07 12:36:30 -0700214 /* package */ static void updateFavicon(final ContentResolver cr,
Jeff Hamilton84029622010-08-05 14:29:28 -0500215 final String originalUrl, final String url, final Bitmap favicon) {
216 new AsyncTask<Void, Void, Void>() {
217 @Override
218 protected Void doInBackground(Void... unused) {
John Reck568467e2010-12-21 14:15:50 -0800219 final ByteArrayOutputStream os = new ByteArrayOutputStream();
220 favicon.compress(Bitmap.CompressFormat.PNG, 100, os);
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700221 byte[] image = os.toByteArray();
Jeff Hamilton1a805652010-09-07 12:36:30 -0700222
John Reck568467e2010-12-21 14:15:50 -0800223 // The Images update will insert if it doesn't exist
224 ContentValues values = new ContentValues();
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700225 values.put(Images.FAVICON, image);
226 values.put(Images.THUMBNAIL, image);
227
228 updateImages(cr, removeQuery(originalUrl), values);
229 updateImages(cr, removeQuery(url), values);
230
231 Cursor cursor = null;
232 try {
233 cursor = queryCombinedForUrl(cr, originalUrl, url);
234 if (cursor != null && cursor.moveToFirst()) {
235 do {
236 updateImages(cr, cursor.getString(0), values);
237 } while (cursor.moveToNext());
238 }
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700239
240 cursor = queryCombinedForUrl(cr, eatTrailingSlash(originalUrl),
241 eatTrailingSlash(url));
242 if (cursor != null && cursor.moveToFirst()) {
243 do {
244 updateImages(cr, cursor.getString(0), values);
245 } while (cursor.moveToNext());
246 }
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700247 } catch (IllegalStateException e) {
248 // Ignore
249 } catch (SQLiteException s) {
250 // Ignore
251 } finally {
252 if (cursor != null) cursor.close();
253 }
254
Jeff Hamilton84029622010-08-05 14:29:28 -0500255 return null;
256 }
John Reck568467e2010-12-21 14:15:50 -0800257
258 private void updateImages(final ContentResolver cr,
Pankaj Garg6bedeba2015-06-23 15:47:37 -0700259 final String url, ContentValues values) {
260 if (!TextUtils.isEmpty(url)) {
261 values.put(Images.URL, url);
John Reck568467e2010-12-21 14:15:50 -0800262 cr.update(BrowserContract.Images.CONTENT_URI, values, null, null);
263 }
264 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500265 }.execute();
266 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100267}