blob: 0bccbed97c6d1edd7657a65f671473e8043e3030 [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
17package com.android.browser;
18
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;
Ben Murdochaac7aa62009-09-17 16:57:40 +010025import android.graphics.Bitmap;
Leon Scrogginse372c022009-06-12 17:07:29 -040026import android.net.Uri;
Jeff Hamilton84029622010-08-05 14:29:28 -050027import android.os.AsyncTask;
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050028import android.preference.PreferenceManager;
Jeff Hamilton69bd7072010-08-17 12:38:22 -050029import android.provider.BrowserContract;
Jeff Hamilton1a805652010-09-07 12:36:30 -070030import android.provider.BrowserContract.Combined;
31import android.provider.BrowserContract.Images;
Leon Scrogginse372c022009-06-12 17:07:29 -040032import android.util.Log;
33import android.webkit.WebIconDatabase;
34import android.widget.Toast;
35
Ben Murdochaac7aa62009-09-17 16:57:40 +010036import java.io.ByteArrayOutputStream;
Leon Scrogginse372c022009-06-12 17:07:29 -040037
38/**
39 * This class is purely to have a common place for adding/deleting bookmarks.
40 */
41/* package */ class Bookmarks {
Ben Murdochde353622009-10-12 10:29:00 +010042 // 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 Scroggins2c0f6112010-03-12 18:09:39 -050054 private final static String LOGTAG = "Bookmarks";
Leon Scrogginse372c022009-06-12 17:07:29 -040055 /**
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.
60 * @param cr The ContentResolver being used to add the bookmark to the db.
61 * @param url URL of the website to be bookmarked.
62 * @param name Provided name for the bookmark.
Ben Murdochaac7aa62009-09-17 16:57:40 +010063 * @param thumbnail A thumbnail for the bookmark.
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070064 * @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 Scrogginse372c022009-06-12 17:07:29 -040067 */
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050068 /* package */ static void addBookmark(Context context, boolean showToast, String url,
Jeff Hamilton84029622010-08-05 14:29:28 -050069 String name, Bitmap thumbnail, boolean retainIcon) {
Leon Scrogginse372c022009-06-12 17:07:29 -040070 // Want to append to the beginning of the list
Jeff Hamilton84029622010-08-05 14:29:28 -050071 ContentValues values = new ContentValues();
Leon Scroggins2c0f6112010-03-12 18:09:39 -050072 Cursor cursor = null;
73 try {
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050074 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
75 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
76 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
77 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountType);
78 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountName);
Jeff Hamilton84029622010-08-05 14:29:28 -050079 values.put(BrowserContract.Bookmarks.TITLE, name);
80 values.put(BrowserContract.Bookmarks.URL, url);
81 values.put(BrowserContract.Bookmarks.IS_FOLDER, 0);
82 values.put(BrowserContract.Bookmarks.THUMBNAIL,
83 bitmapToBytes(thumbnail));
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);
87 } finally {
88 if (cursor != null) cursor.close();
Leon Scrogginse372c022009-06-12 17:07:29 -040089 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070090 if (retainIcon) {
91 WebIconDatabase.getInstance().retainIconForPageUrl(url);
92 }
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050093 if (showToast) {
Leon Scrogginse372c022009-06-12 17:07:29 -040094 Toast.makeText(context, R.string.added_to_bookmarks,
95 Toast.LENGTH_LONG).show();
96 }
97 }
98
99 /**
100 * Remove a bookmark from the database. If the url is a visited site, it
101 * will remain in the database, but only as a history item, and not as a
102 * bookmarked site.
103 * @param context Context of the calling Activity. This is used to make
104 * Toast confirming that the bookmark has been removed. If the
105 * caller provides null, the Toast will not be shown.
106 * @param cr The ContentResolver being used to remove the bookmark.
107 * @param url URL of the website to be removed.
108 */
109 /* package */ static void removeFromBookmarks(Context context,
Andrei Popescuc9526192009-09-23 15:52:16 +0100110 ContentResolver cr, String url, String title) {
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500111 Cursor cursor = null;
112 try {
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500113 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
114 new String[] { BrowserContract.Bookmarks._ID },
115 BrowserContract.Bookmarks.URL + " = ? AND " +
116 BrowserContract.Bookmarks.TITLE + " = ?",
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500117 new String[] { url, title },
118 null);
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500119
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500120 // Should be in the database no matter what
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500121 if (!cursor.moveToFirst()) {
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500122 throw new AssertionError("URL is not in the database! " + url
123 + " " + title);
Leon Scrogginse372c022009-06-12 17:07:29 -0400124 }
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500125
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500126 // Remove from bookmarks
127 WebIconDatabase.getInstance().releaseIconForPageUrl(url);
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500128 Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI,
129 cursor.getLong(0));
130 cr.delete(uri, null, null);
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500131 if (context != null) {
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500132 Toast.makeText(context, R.string.removed_from_bookmarks, Toast.LENGTH_LONG).show();
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500133 }
134 } catch (IllegalStateException e) {
135 Log.e(LOGTAG, "removeFromBookmarks", e);
136 } finally {
137 if (cursor != null) cursor.close();
Leon Scrogginse372c022009-06-12 17:07:29 -0400138 }
Leon Scrogginse372c022009-06-12 17:07:29 -0400139 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100140
141 private static byte[] bitmapToBytes(Bitmap bm) {
142 if (bm == null) {
143 return null;
144 }
145
146 final ByteArrayOutputStream os = new ByteArrayOutputStream();
147 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
148 return os.toByteArray();
149 }
Ben Murdochde353622009-10-12 10:29:00 +0100150
151 /* package */ static boolean urlHasAcceptableScheme(String url) {
152 if (url == null) {
153 return false;
154 }
155
156 for (int i = 0; i < acceptableBookmarkSchemes.length; i++) {
157 if (url.startsWith(acceptableBookmarkSchemes[i])) {
158 return true;
159 }
160 }
161 return false;
162 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500163
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500164 static final String QUERY_BOOKMARKS_WHERE =
Jeff Hamilton1a805652010-09-07 12:36:30 -0700165 Combined.URL + " == ? OR " +
166 Combined.URL + " == ? OR " +
167 Combined.URL + " LIKE ? || '%' OR " +
168 Combined.URL + " LIKE ? || '%'";
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500169
Jeff Hamilton1a805652010-09-07 12:36:30 -0700170 /* package */ static Cursor queryCombinedForUrl(ContentResolver cr,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500171 String originalUrl, String url) {
Jeff Hamilton84029622010-08-05 14:29:28 -0500172 if (cr == null || url == null) {
173 return null;
174 }
175
176 // If originalUrl is null, just set it to url.
177 if (originalUrl == null) {
178 originalUrl = url;
179 }
180
181 // Look for both the original url and the actual url. This takes in to
182 // account redirects.
Jeff Hamilton1a805652010-09-07 12:36:30 -0700183 String originalUrlNoQuery = removeQuery(originalUrl);
184 String urlNoQuery = removeQuery(url);
Jeff Hamilton84029622010-08-05 14:29:28 -0500185 originalUrl = originalUrlNoQuery + '?';
186 url = urlNoQuery + '?';
187
188 // Use NoQuery to search for the base url (i.e. if the url is
189 // http://www.yahoo.com/?rs=1, search for http://www.yahoo.com)
190 // Use url to match the base url with other queries (i.e. if the url is
191 // http://www.google.com/m, search for
192 // http://www.google.com/m?some_query)
Jeff Hamilton1a805652010-09-07 12:36:30 -0700193 final String[] selArgs = new String[] { originalUrlNoQuery, urlNoQuery, originalUrl, url };
194 final String[] projection = new String[] { Combined.URL };
195 return cr.query(Combined.CONTENT_URI, projection, QUERY_BOOKMARKS_WHERE, selArgs, null);
Jeff Hamilton84029622010-08-05 14:29:28 -0500196 }
197
198 // Strip the query from the given url.
199 static String removeQuery(String url) {
200 if (url == null) {
201 return null;
202 }
203 int query = url.indexOf('?');
204 String noQuery = url;
205 if (query != -1) {
206 noQuery = url.substring(0, query);
207 }
208 return noQuery;
209 }
210
211 /**
212 * Update the bookmark's favicon. This is a convenience method for updating
213 * a bookmark favicon for the originalUrl and url of the passed in WebView.
214 * @param cr The ContentResolver to use.
215 * @param originalUrl The original url before any redirects.
216 * @param url The current url.
217 * @param favicon The favicon bitmap to write to the db.
218 */
Jeff Hamilton1a805652010-09-07 12:36:30 -0700219 /* package */ static void updateFavicon(final ContentResolver cr,
Jeff Hamilton84029622010-08-05 14:29:28 -0500220 final String originalUrl, final String url, final Bitmap favicon) {
221 new AsyncTask<Void, Void, Void>() {
222 @Override
223 protected Void doInBackground(Void... unused) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700224 Cursor cursor = queryCombinedForUrl(cr, originalUrl, url);
225 try {
226 if (cursor.moveToFirst()) {
227 final ByteArrayOutputStream os = new ByteArrayOutputStream();
228 favicon.compress(Bitmap.CompressFormat.PNG, 100, os);
229
230 ContentValues values = new ContentValues();
231 values.put(Images.FAVICON, os.toByteArray());
232 values.put(Images.URL, cursor.getString(0));
233
234 do {
235 cr.update(Images.CONTENT_URI, values, null, null);
236 } while (cursor.moveToNext());
237 }
238 } finally {
239 if (cursor != null) cursor.close();
Jeff Hamilton84029622010-08-05 14:29:28 -0500240 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500241 return null;
242 }
243 }.execute();
244 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100245}