blob: fef634f52bdfb8604d326f9276d36bb6222ab4a4 [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.
Leon Scrogginse372c022009-06-12 17:07:29 -040060 * @param url URL of the website to be bookmarked.
61 * @param name Provided name for the bookmark.
Ben Murdochaac7aa62009-09-17 16:57:40 +010062 * @param thumbnail A thumbnail for the bookmark.
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070063 * @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 III052ce662010-09-13 14:44:16 -040066 * @param parent ID of the parent folder.
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,
Leon Scroggins III052ce662010-09-13 14:44:16 -040069 String name, Bitmap thumbnail, boolean retainIcon, long parent) {
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 try {
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050073 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 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 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070088 if (retainIcon) {
89 WebIconDatabase.getInstance().retainIconForPageUrl(url);
90 }
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050091 if (showToast) {
Leon Scrogginse372c022009-06-12 17:07:29 -040092 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 Popescuc9526192009-09-23 15:52:16 +0100108 ContentResolver cr, String url, String title) {
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500109 Cursor cursor = null;
110 try {
John Reck4b59db82010-11-16 15:40:34 -0800111 Uri uri = BookmarkUtils.getBookmarksUri(context);
112 cursor = cr.query(uri,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500113 new String[] { BrowserContract.Bookmarks._ID },
114 BrowserContract.Bookmarks.URL + " = ? AND " +
115 BrowserContract.Bookmarks.TITLE + " = ?",
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500116 new String[] { url, title },
117 null);
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500118
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500119 if (!cursor.moveToFirst()) {
John Reck4b59db82010-11-16 15:40:34 -0800120 return;
Leon Scrogginse372c022009-06-12 17:07:29 -0400121 }
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500122
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500123 // Remove from bookmarks
124 WebIconDatabase.getInstance().releaseIconForPageUrl(url);
John Reck4b59db82010-11-16 15:40:34 -0800125 uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500126 cursor.getLong(0));
127 cr.delete(uri, null, null);
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500128 if (context != null) {
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500129 Toast.makeText(context, R.string.removed_from_bookmarks, Toast.LENGTH_LONG).show();
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500130 }
131 } catch (IllegalStateException e) {
132 Log.e(LOGTAG, "removeFromBookmarks", e);
133 } finally {
134 if (cursor != null) cursor.close();
Leon Scrogginse372c022009-06-12 17:07:29 -0400135 }
Leon Scrogginse372c022009-06-12 17:07:29 -0400136 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100137
138 private static byte[] bitmapToBytes(Bitmap bm) {
139 if (bm == null) {
140 return null;
141 }
142
143 final ByteArrayOutputStream os = new ByteArrayOutputStream();
144 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
145 return os.toByteArray();
146 }
Ben Murdochde353622009-10-12 10:29:00 +0100147
148 /* package */ static boolean urlHasAcceptableScheme(String url) {
149 if (url == null) {
150 return false;
151 }
152
153 for (int i = 0; i < acceptableBookmarkSchemes.length; i++) {
154 if (url.startsWith(acceptableBookmarkSchemes[i])) {
155 return true;
156 }
157 }
158 return false;
159 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500160
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500161 static final String QUERY_BOOKMARKS_WHERE =
Jeff Hamilton1a805652010-09-07 12:36:30 -0700162 Combined.URL + " == ? OR " +
163 Combined.URL + " == ? OR " +
164 Combined.URL + " LIKE ? || '%' OR " +
165 Combined.URL + " LIKE ? || '%'";
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500166
Jeff Hamilton1a805652010-09-07 12:36:30 -0700167 /* package */ static Cursor queryCombinedForUrl(ContentResolver cr,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500168 String originalUrl, String url) {
Jeff Hamilton84029622010-08-05 14:29:28 -0500169 if (cr == null || url == null) {
170 return null;
171 }
172
173 // If originalUrl is null, just set it to url.
174 if (originalUrl == null) {
175 originalUrl = url;
176 }
177
178 // Look for both the original url and the actual url. This takes in to
179 // account redirects.
Jeff Hamilton1a805652010-09-07 12:36:30 -0700180 String originalUrlNoQuery = removeQuery(originalUrl);
181 String urlNoQuery = removeQuery(url);
Jeff Hamilton84029622010-08-05 14:29:28 -0500182 originalUrl = originalUrlNoQuery + '?';
183 url = urlNoQuery + '?';
184
185 // Use NoQuery to search for the base url (i.e. if the url is
186 // http://www.yahoo.com/?rs=1, search for http://www.yahoo.com)
187 // Use url to match the base url with other queries (i.e. if the url is
188 // http://www.google.com/m, search for
189 // http://www.google.com/m?some_query)
Jeff Hamilton1a805652010-09-07 12:36:30 -0700190 final String[] selArgs = new String[] { originalUrlNoQuery, urlNoQuery, originalUrl, url };
191 final String[] projection = new String[] { Combined.URL };
192 return cr.query(Combined.CONTENT_URI, projection, QUERY_BOOKMARKS_WHERE, selArgs, null);
Jeff Hamilton84029622010-08-05 14:29:28 -0500193 }
194
195 // Strip the query from the given url.
196 static String removeQuery(String url) {
197 if (url == null) {
198 return null;
199 }
200 int query = url.indexOf('?');
201 String noQuery = url;
202 if (query != -1) {
203 noQuery = url.substring(0, query);
204 }
205 return noQuery;
206 }
207
208 /**
209 * Update the bookmark's favicon. This is a convenience method for updating
210 * a bookmark favicon for the originalUrl and url of the passed in WebView.
211 * @param cr The ContentResolver to use.
212 * @param originalUrl The original url before any redirects.
213 * @param url The current url.
214 * @param favicon The favicon bitmap to write to the db.
215 */
Jeff Hamilton1a805652010-09-07 12:36:30 -0700216 /* package */ static void updateFavicon(final ContentResolver cr,
Jeff Hamilton84029622010-08-05 14:29:28 -0500217 final String originalUrl, final String url, final Bitmap favicon) {
218 new AsyncTask<Void, Void, Void>() {
219 @Override
220 protected Void doInBackground(Void... unused) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700221 Cursor cursor = queryCombinedForUrl(cr, originalUrl, url);
222 try {
223 if (cursor.moveToFirst()) {
224 final ByteArrayOutputStream os = new ByteArrayOutputStream();
225 favicon.compress(Bitmap.CompressFormat.PNG, 100, os);
226
227 ContentValues values = new ContentValues();
228 values.put(Images.FAVICON, os.toByteArray());
229 values.put(Images.URL, cursor.getString(0));
230
231 do {
232 cr.update(Images.CONTENT_URI, values, null, null);
233 } while (cursor.moveToNext());
234 }
235 } finally {
236 if (cursor != null) cursor.close();
Jeff Hamilton84029622010-08-05 14:29:28 -0500237 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500238 return null;
239 }
240 }.execute();
241 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100242}