blob: beea489ec0e36ec69d3f1fbee08105c1e78b5d7d [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;
John Reck568467e2010-12-21 14:15:50 -080032import android.text.TextUtils;
Leon Scrogginse372c022009-06-12 17:07:29 -040033import android.util.Log;
34import android.webkit.WebIconDatabase;
35import android.widget.Toast;
36
Ben Murdochaac7aa62009-09-17 16:57:40 +010037import java.io.ByteArrayOutputStream;
Leon Scrogginse372c022009-06-12 17:07:29 -040038
39/**
40 * This class is purely to have a common place for adding/deleting bookmarks.
41 */
42/* package */ class Bookmarks {
Ben Murdochde353622009-10-12 10:29:00 +010043 // 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 Scroggins2c0f6112010-03-12 18:09:39 -050055 private final static String LOGTAG = "Bookmarks";
Leon Scrogginse372c022009-06-12 17:07:29 -040056 /**
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 Scrogginse372c022009-06-12 17:07:29 -040061 * @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 Scroggins III052ce662010-09-13 14:44:16 -040067 * @param parent ID of the parent folder.
Leon Scrogginse372c022009-06-12 17:07:29 -040068 */
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050069 /* package */ static void addBookmark(Context context, boolean showToast, String url,
Leon Scroggins III052ce662010-09-13 14:44:16 -040070 String name, Bitmap thumbnail, boolean retainIcon, long parent) {
Leon Scrogginse372c022009-06-12 17:07:29 -040071 // Want to append to the beginning of the list
Jeff Hamilton84029622010-08-05 14:29:28 -050072 ContentValues values = new ContentValues();
Leon Scroggins2c0f6112010-03-12 18:09:39 -050073 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));
Leon Scroggins III052ce662010-09-13 14:44:16 -040084 values.put(BrowserContract.Bookmarks.PARENT, parent);
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050085 context.getContentResolver().insert(BrowserContract.Bookmarks.CONTENT_URI, values);
Leon Scroggins2c0f6112010-03-12 18:09:39 -050086 } catch (IllegalStateException e) {
87 Log.e(LOGTAG, "addBookmark", e);
Leon Scrogginse372c022009-06-12 17:07:29 -040088 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070089 if (retainIcon) {
90 WebIconDatabase.getInstance().retainIconForPageUrl(url);
91 }
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050092 if (showToast) {
Leon Scrogginse372c022009-06-12 17:07:29 -040093 Toast.makeText(context, R.string.added_to_bookmarks,
94 Toast.LENGTH_LONG).show();
95 }
96 }
97
98 /**
99 * Remove a bookmark from the database. If the url is a visited site, it
100 * will remain in the database, but only as a history item, and not as a
101 * bookmarked site.
102 * @param context Context of the calling Activity. This is used to make
John Reck6c22ce62011-01-06 16:22:48 -0800103 * Toast confirming that the bookmark has been removed and to
104 * lookup the correct content uri. It must not be null.
Leon Scrogginse372c022009-06-12 17:07:29 -0400105 * @param cr The ContentResolver being used to remove the bookmark.
106 * @param url URL of the website to be removed.
107 */
108 /* package */ static void removeFromBookmarks(Context context,
Andrei Popescuc9526192009-09-23 15:52:16 +0100109 ContentResolver cr, String url, String title) {
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500110 Cursor cursor = null;
111 try {
John Reck4b59db82010-11-16 15:40:34 -0800112 Uri uri = BookmarkUtils.getBookmarksUri(context);
113 cursor = cr.query(uri,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500114 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
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500120 if (!cursor.moveToFirst()) {
John Reck4b59db82010-11-16 15:40:34 -0800121 return;
Leon Scrogginse372c022009-06-12 17:07:29 -0400122 }
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500123
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500124 // Remove from bookmarks
125 WebIconDatabase.getInstance().releaseIconForPageUrl(url);
John Reck4b59db82010-11-16 15:40:34 -0800126 uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500127 cursor.getLong(0));
128 cr.delete(uri, null, null);
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500129 if (context != null) {
John Reck6c22ce62011-01-06 16:22:48 -0800130 Toast.makeText(context, R.string.removed_from_bookmarks,
131 Toast.LENGTH_LONG).show();
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500132 }
133 } catch (IllegalStateException e) {
134 Log.e(LOGTAG, "removeFromBookmarks", e);
135 } finally {
136 if (cursor != null) cursor.close();
Leon Scrogginse372c022009-06-12 17:07:29 -0400137 }
Leon Scrogginse372c022009-06-12 17:07:29 -0400138 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100139
140 private static byte[] bitmapToBytes(Bitmap bm) {
141 if (bm == null) {
142 return null;
143 }
144
145 final ByteArrayOutputStream os = new ByteArrayOutputStream();
146 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
147 return os.toByteArray();
148 }
Ben Murdochde353622009-10-12 10:29:00 +0100149
150 /* package */ static boolean urlHasAcceptableScheme(String url) {
151 if (url == null) {
152 return false;
153 }
154
155 for (int i = 0; i < acceptableBookmarkSchemes.length; i++) {
156 if (url.startsWith(acceptableBookmarkSchemes[i])) {
157 return true;
158 }
159 }
160 return false;
161 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500162
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500163 static final String QUERY_BOOKMARKS_WHERE =
Jeff Hamilton1a805652010-09-07 12:36:30 -0700164 Combined.URL + " == ? OR " +
165 Combined.URL + " == ? OR " +
166 Combined.URL + " LIKE ? || '%' OR " +
167 Combined.URL + " LIKE ? || '%'";
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500168
Jeff Hamilton1a805652010-09-07 12:36:30 -0700169 /* package */ static Cursor queryCombinedForUrl(ContentResolver cr,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500170 String originalUrl, String url) {
Jeff Hamilton84029622010-08-05 14:29:28 -0500171 if (cr == null || url == null) {
172 return null;
173 }
174
175 // If originalUrl is null, just set it to url.
176 if (originalUrl == null) {
177 originalUrl = url;
178 }
179
180 // Look for both the original url and the actual url. This takes in to
181 // account redirects.
Jeff Hamilton1a805652010-09-07 12:36:30 -0700182 String originalUrlNoQuery = removeQuery(originalUrl);
183 String urlNoQuery = removeQuery(url);
Jeff Hamilton84029622010-08-05 14:29:28 -0500184 originalUrl = originalUrlNoQuery + '?';
185 url = urlNoQuery + '?';
186
187 // Use NoQuery to search for the base url (i.e. if the url is
188 // http://www.yahoo.com/?rs=1, search for http://www.yahoo.com)
189 // Use url to match the base url with other queries (i.e. if the url is
190 // http://www.google.com/m, search for
191 // http://www.google.com/m?some_query)
Jeff Hamilton1a805652010-09-07 12:36:30 -0700192 final String[] selArgs = new String[] { originalUrlNoQuery, urlNoQuery, originalUrl, url };
193 final String[] projection = new String[] { Combined.URL };
194 return cr.query(Combined.CONTENT_URI, projection, QUERY_BOOKMARKS_WHERE, selArgs, null);
Jeff Hamilton84029622010-08-05 14:29:28 -0500195 }
196
197 // Strip the query from the given url.
198 static String removeQuery(String url) {
199 if (url == null) {
200 return null;
201 }
202 int query = url.indexOf('?');
203 String noQuery = url;
204 if (query != -1) {
205 noQuery = url.substring(0, query);
206 }
207 return noQuery;
208 }
209
210 /**
211 * Update the bookmark's favicon. This is a convenience method for updating
212 * a bookmark favicon for the originalUrl and url of the passed in WebView.
213 * @param cr The ContentResolver to use.
214 * @param originalUrl The original url before any redirects.
215 * @param url The current url.
216 * @param favicon The favicon bitmap to write to the db.
217 */
Jeff Hamilton1a805652010-09-07 12:36:30 -0700218 /* package */ static void updateFavicon(final ContentResolver cr,
Jeff Hamilton84029622010-08-05 14:29:28 -0500219 final String originalUrl, final String url, final Bitmap favicon) {
220 new AsyncTask<Void, Void, Void>() {
221 @Override
222 protected Void doInBackground(Void... unused) {
John Reck568467e2010-12-21 14:15:50 -0800223 final ByteArrayOutputStream os = new ByteArrayOutputStream();
224 favicon.compress(Bitmap.CompressFormat.PNG, 100, os);
Jeff Hamilton1a805652010-09-07 12:36:30 -0700225
John Reck568467e2010-12-21 14:15:50 -0800226 // The Images update will insert if it doesn't exist
227 ContentValues values = new ContentValues();
228 values.put(Images.FAVICON, os.toByteArray());
229 updateImages(cr, originalUrl, values);
230 updateImages(cr, url, values);
Jeff Hamilton84029622010-08-05 14:29:28 -0500231 return null;
232 }
John Reck568467e2010-12-21 14:15:50 -0800233
234 private void updateImages(final ContentResolver cr,
235 final String url, ContentValues values) {
236 String iurl = removeQuery(url);
237 if (!TextUtils.isEmpty(iurl)) {
238 values.put(Images.URL, iurl);
239 cr.update(BrowserContract.Images.CONTENT_URI, values, null, null);
240 }
241 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500242 }.execute();
243 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100244}