blob: 532d7c0e9dac5c2e2c8c9c7e4fa3957c9bf11bf1 [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;
23import android.database.Cursor;
Ben Murdochaac7aa62009-09-17 16:57:40 +010024import android.graphics.Bitmap;
Leon Scrogginse372c022009-06-12 17:07:29 -040025import android.net.Uri;
Jeff Hamilton84029622010-08-05 14:29:28 -050026import android.os.AsyncTask;
Jeff Hamilton69bd7072010-08-17 12:38:22 -050027import android.provider.BrowserContract;
Jeff Hamilton1a805652010-09-07 12:36:30 -070028import android.provider.BrowserContract.Combined;
29import android.provider.BrowserContract.Images;
Leon Scrogginse372c022009-06-12 17:07:29 -040030import android.util.Log;
31import android.webkit.WebIconDatabase;
32import android.widget.Toast;
33
Ben Murdochaac7aa62009-09-17 16:57:40 +010034import java.io.ByteArrayOutputStream;
Leon Scrogginse372c022009-06-12 17:07:29 -040035
36/**
37 * This class is purely to have a common place for adding/deleting bookmarks.
38 */
39/* package */ class Bookmarks {
Ben Murdochde353622009-10-12 10:29:00 +010040 // We only want the user to be able to bookmark content that
41 // the browser can handle directly.
42 private static final String acceptableBookmarkSchemes[] = {
43 "http:",
44 "https:",
45 "about:",
46 "data:",
47 "javascript:",
48 "file:",
49 "content:"
50 };
51
Leon Scroggins2c0f6112010-03-12 18:09:39 -050052 private final static String LOGTAG = "Bookmarks";
Leon Scrogginse372c022009-06-12 17:07:29 -040053 /**
54 * Add a bookmark to the database.
55 * @param context Context of the calling Activity. This is used to make
56 * Toast confirming that the bookmark has been added. If the
57 * caller provides null, the Toast will not be shown.
58 * @param cr The ContentResolver being used to add the bookmark to the db.
59 * @param url URL of the website to be bookmarked.
60 * @param name Provided name for the bookmark.
Ben Murdochaac7aa62009-09-17 16:57:40 +010061 * @param thumbnail A thumbnail for the bookmark.
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070062 * @param retainIcon Whether to retain the page's icon in the icon database.
63 * This will usually be <code>true</code> except when bookmarks are
64 * added by a settings restore agent.
Leon Scrogginse372c022009-06-12 17:07:29 -040065 */
Jeff Hamilton84029622010-08-05 14:29:28 -050066 /* package */ static void addBookmark(Context context, ContentResolver cr, String url,
67 String name, Bitmap thumbnail, boolean retainIcon) {
Leon Scrogginse372c022009-06-12 17:07:29 -040068 // Want to append to the beginning of the list
Jeff Hamilton84029622010-08-05 14:29:28 -050069 ContentValues values = new ContentValues();
Leon Scroggins2c0f6112010-03-12 18:09:39 -050070 Cursor cursor = null;
71 try {
Jeff Hamilton84029622010-08-05 14:29:28 -050072 values.put(BrowserContract.Bookmarks.TITLE, name);
73 values.put(BrowserContract.Bookmarks.URL, url);
74 values.put(BrowserContract.Bookmarks.IS_FOLDER, 0);
75 values.put(BrowserContract.Bookmarks.THUMBNAIL,
76 bitmapToBytes(thumbnail));
77 cr.insert(BrowserContract.Bookmarks.CONTENT_URI, values);
Leon Scroggins2c0f6112010-03-12 18:09:39 -050078 } catch (IllegalStateException e) {
79 Log.e(LOGTAG, "addBookmark", e);
80 } finally {
81 if (cursor != null) cursor.close();
Leon Scrogginse372c022009-06-12 17:07:29 -040082 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070083 if (retainIcon) {
84 WebIconDatabase.getInstance().retainIconForPageUrl(url);
85 }
Leon Scrogginse372c022009-06-12 17:07:29 -040086 if (context != null) {
87 Toast.makeText(context, R.string.added_to_bookmarks,
88 Toast.LENGTH_LONG).show();
89 }
90 }
91
92 /**
93 * Remove a bookmark from the database. If the url is a visited site, it
94 * will remain in the database, but only as a history item, and not as a
95 * bookmarked site.
96 * @param context Context of the calling Activity. This is used to make
97 * Toast confirming that the bookmark has been removed. If the
98 * caller provides null, the Toast will not be shown.
99 * @param cr The ContentResolver being used to remove the bookmark.
100 * @param url URL of the website to be removed.
101 */
102 /* package */ static void removeFromBookmarks(Context context,
Andrei Popescuc9526192009-09-23 15:52:16 +0100103 ContentResolver cr, String url, String title) {
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500104 Cursor cursor = null;
105 try {
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500106 cursor = cr.query(BrowserContract.Bookmarks.CONTENT_URI,
107 new String[] { BrowserContract.Bookmarks._ID },
108 BrowserContract.Bookmarks.URL + " = ? AND " +
109 BrowserContract.Bookmarks.TITLE + " = ?",
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500110 new String[] { url, title },
111 null);
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500112
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500113 // Should be in the database no matter what
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500114 if (!cursor.moveToFirst()) {
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500115 throw new AssertionError("URL is not in the database! " + url
116 + " " + title);
Leon Scrogginse372c022009-06-12 17:07:29 -0400117 }
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500118
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500119 // Remove from bookmarks
120 WebIconDatabase.getInstance().releaseIconForPageUrl(url);
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500121 Uri uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI,
122 cursor.getLong(0));
123 cr.delete(uri, null, null);
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500124 if (context != null) {
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500125 Toast.makeText(context, R.string.removed_from_bookmarks, Toast.LENGTH_LONG).show();
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500126 }
127 } catch (IllegalStateException e) {
128 Log.e(LOGTAG, "removeFromBookmarks", e);
129 } finally {
130 if (cursor != null) cursor.close();
Leon Scrogginse372c022009-06-12 17:07:29 -0400131 }
Leon Scrogginse372c022009-06-12 17:07:29 -0400132 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100133
134 private static byte[] bitmapToBytes(Bitmap bm) {
135 if (bm == null) {
136 return null;
137 }
138
139 final ByteArrayOutputStream os = new ByteArrayOutputStream();
140 bm.compress(Bitmap.CompressFormat.PNG, 100, os);
141 return os.toByteArray();
142 }
Ben Murdochde353622009-10-12 10:29:00 +0100143
144 /* package */ static boolean urlHasAcceptableScheme(String url) {
145 if (url == null) {
146 return false;
147 }
148
149 for (int i = 0; i < acceptableBookmarkSchemes.length; i++) {
150 if (url.startsWith(acceptableBookmarkSchemes[i])) {
151 return true;
152 }
153 }
154 return false;
155 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500156
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500157 static final String QUERY_BOOKMARKS_WHERE =
Jeff Hamilton1a805652010-09-07 12:36:30 -0700158 Combined.URL + " == ? OR " +
159 Combined.URL + " == ? OR " +
160 Combined.URL + " LIKE ? || '%' OR " +
161 Combined.URL + " LIKE ? || '%'";
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500162
Jeff Hamilton1a805652010-09-07 12:36:30 -0700163 /* package */ static Cursor queryCombinedForUrl(ContentResolver cr,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500164 String originalUrl, String url) {
Jeff Hamilton84029622010-08-05 14:29:28 -0500165 if (cr == null || url == null) {
166 return null;
167 }
168
169 // If originalUrl is null, just set it to url.
170 if (originalUrl == null) {
171 originalUrl = url;
172 }
173
174 // Look for both the original url and the actual url. This takes in to
175 // account redirects.
Jeff Hamilton1a805652010-09-07 12:36:30 -0700176 String originalUrlNoQuery = removeQuery(originalUrl);
177 String urlNoQuery = removeQuery(url);
Jeff Hamilton84029622010-08-05 14:29:28 -0500178 originalUrl = originalUrlNoQuery + '?';
179 url = urlNoQuery + '?';
180
181 // Use NoQuery to search for the base url (i.e. if the url is
182 // http://www.yahoo.com/?rs=1, search for http://www.yahoo.com)
183 // Use url to match the base url with other queries (i.e. if the url is
184 // http://www.google.com/m, search for
185 // http://www.google.com/m?some_query)
Jeff Hamilton1a805652010-09-07 12:36:30 -0700186 final String[] selArgs = new String[] { originalUrlNoQuery, urlNoQuery, originalUrl, url };
187 final String[] projection = new String[] { Combined.URL };
188 return cr.query(Combined.CONTENT_URI, projection, QUERY_BOOKMARKS_WHERE, selArgs, null);
Jeff Hamilton84029622010-08-05 14:29:28 -0500189 }
190
191 // Strip the query from the given url.
192 static String removeQuery(String url) {
193 if (url == null) {
194 return null;
195 }
196 int query = url.indexOf('?');
197 String noQuery = url;
198 if (query != -1) {
199 noQuery = url.substring(0, query);
200 }
201 return noQuery;
202 }
203
204 /**
205 * Update the bookmark's favicon. This is a convenience method for updating
206 * a bookmark favicon for the originalUrl and url of the passed in WebView.
207 * @param cr The ContentResolver to use.
208 * @param originalUrl The original url before any redirects.
209 * @param url The current url.
210 * @param favicon The favicon bitmap to write to the db.
211 */
Jeff Hamilton1a805652010-09-07 12:36:30 -0700212 /* package */ static void updateFavicon(final ContentResolver cr,
Jeff Hamilton84029622010-08-05 14:29:28 -0500213 final String originalUrl, final String url, final Bitmap favicon) {
214 new AsyncTask<Void, Void, Void>() {
215 @Override
216 protected Void doInBackground(Void... unused) {
Jeff Hamilton1a805652010-09-07 12:36:30 -0700217 Cursor cursor = queryCombinedForUrl(cr, originalUrl, url);
218 try {
219 if (cursor.moveToFirst()) {
220 final ByteArrayOutputStream os = new ByteArrayOutputStream();
221 favicon.compress(Bitmap.CompressFormat.PNG, 100, os);
222
223 ContentValues values = new ContentValues();
224 values.put(Images.FAVICON, os.toByteArray());
225 values.put(Images.URL, cursor.getString(0));
226
227 do {
228 cr.update(Images.CONTENT_URI, values, null, null);
229 } while (cursor.moveToNext());
230 }
231 } finally {
232 if (cursor != null) cursor.close();
Jeff Hamilton84029622010-08-05 14:29:28 -0500233 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500234 return null;
235 }
236 }.execute();
237 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100238}