blob: 9bd84f8f855bd1737bd741a6deb18379502a228e [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;
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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080029
Bijan Amirzada41242f22014-03-21 12:12:18 -070030import com.android.browser.R;
31import com.android.browser.platformsupport.BrowserContract;
32import com.android.browser.platformsupport.BrowserContract.Combined;
33import com.android.browser.platformsupport.BrowserContract.Images;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080034
John Reck568467e2010-12-21 14:15:50 -080035import android.text.TextUtils;
Leon Scrogginse372c022009-06-12 17:07:29 -040036import android.util.Log;
Leon Scrogginse372c022009-06-12 17:07:29 -040037import android.widget.Toast;
38
Ben Murdochaac7aa62009-09-17 16:57:40 +010039import java.io.ByteArrayOutputStream;
Leon Scrogginse372c022009-06-12 17:07:29 -040040
41/**
42 * This class is purely to have a common place for adding/deleting bookmarks.
43 */
John Reck57928452011-02-17 12:44:07 -080044public class Bookmarks {
Ben Murdochde353622009-10-12 10:29:00 +010045 // We only want the user to be able to bookmark content that
46 // the browser can handle directly.
47 private static final String acceptableBookmarkSchemes[] = {
48 "http:",
49 "https:",
50 "about:",
51 "data:",
52 "javascript:",
53 "file:",
54 "content:"
55 };
56
Leon Scroggins2c0f6112010-03-12 18:09:39 -050057 private final static String LOGTAG = "Bookmarks";
Leon Scrogginse372c022009-06-12 17:07:29 -040058 /**
59 * Add a bookmark to the database.
60 * @param context Context of the calling Activity. This is used to make
61 * Toast confirming that the bookmark has been added. If the
62 * caller provides null, the Toast will not be shown.
Leon Scrogginse372c022009-06-12 17:07:29 -040063 * @param url URL of the website to be bookmarked.
64 * @param name Provided name for the bookmark.
Ben Murdochaac7aa62009-09-17 16:57:40 +010065 * @param thumbnail A thumbnail for the bookmark.
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070066 * @param retainIcon Whether to retain the page's icon in the icon database.
67 * This will usually be <code>true</code> except when bookmarks are
68 * added by a settings restore agent.
Leon Scroggins III052ce662010-09-13 14:44:16 -040069 * @param parent ID of the parent folder.
Leon Scrogginse372c022009-06-12 17:07:29 -040070 */
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050071 /* package */ static void addBookmark(Context context, boolean showToast, String url,
John Reckaf262e72011-07-25 13:55:44 -070072 String name, Bitmap thumbnail, long parent) {
Leon Scrogginse372c022009-06-12 17:07:29 -040073 // Want to append to the beginning of the list
Jeff Hamilton84029622010-08-05 14:29:28 -050074 ContentValues values = new ContentValues();
Leon Scroggins2c0f6112010-03-12 18:09:39 -050075 try {
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050076 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Jeff Hamilton84029622010-08-05 14:29:28 -050077 values.put(BrowserContract.Bookmarks.TITLE, name);
78 values.put(BrowserContract.Bookmarks.URL, url);
79 values.put(BrowserContract.Bookmarks.IS_FOLDER, 0);
80 values.put(BrowserContract.Bookmarks.THUMBNAIL,
81 bitmapToBytes(thumbnail));
Leon Scroggins III052ce662010-09-13 14:44:16 -040082 values.put(BrowserContract.Bookmarks.PARENT, parent);
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050083 context.getContentResolver().insert(BrowserContract.Bookmarks.CONTENT_URI, values);
Leon Scroggins2c0f6112010-03-12 18:09:39 -050084 } catch (IllegalStateException e) {
85 Log.e(LOGTAG, "addBookmark", e);
Leon Scrogginse372c022009-06-12 17:07:29 -040086 }
Jeff Hamilton7f6cf3e2010-09-17 17:22:21 -050087 if (showToast) {
Leon Scrogginse372c022009-06-12 17:07:29 -040088 Toast.makeText(context, R.string.added_to_bookmarks,
89 Toast.LENGTH_LONG).show();
90 }
91 }
92
93 /**
94 * Remove a bookmark from the database. If the url is a visited site, it
95 * will remain in the database, but only as a history item, and not as a
96 * bookmarked site.
97 * @param context Context of the calling Activity. This is used to make
John Reck03b20542011-01-06 16:22:48 -080098 * Toast confirming that the bookmark has been removed and to
99 * lookup the correct content uri. It must not be null.
Leon Scrogginse372c022009-06-12 17:07:29 -0400100 * @param cr The ContentResolver being used to remove the bookmark.
101 * @param url URL of the website to be removed.
102 */
103 /* package */ static void removeFromBookmarks(Context context,
Andrei Popescuc9526192009-09-23 15:52:16 +0100104 ContentResolver cr, String url, String title) {
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500105 Cursor cursor = null;
106 try {
John Reck4b59db82010-11-16 15:40:34 -0800107 Uri uri = BookmarkUtils.getBookmarksUri(context);
108 cursor = cr.query(uri,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500109 new String[] { BrowserContract.Bookmarks._ID },
110 BrowserContract.Bookmarks.URL + " = ? AND " +
111 BrowserContract.Bookmarks.TITLE + " = ?",
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500112 new String[] { url, title },
113 null);
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500114
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500115 if (!cursor.moveToFirst()) {
John Reck4b59db82010-11-16 15:40:34 -0800116 return;
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
John Reck4b59db82010-11-16 15:40:34 -0800120 uri = ContentUris.withAppendedId(BrowserContract.Bookmarks.CONTENT_URI,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500121 cursor.getLong(0));
122 cr.delete(uri, null, null);
Leon Scroggins2c0f6112010-03-12 18:09:39 -0500123 if (context != null) {
John Reck03b20542011-01-06 16:22:48 -0800124 Toast.makeText(context, R.string.removed_from_bookmarks,
125 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 " +
John Reck57928452011-02-17 12:44:07 -0800159 Combined.URL + " == ?";
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500160
John Reck57928452011-02-17 12:44:07 -0800161 public static Cursor queryCombinedForUrl(ContentResolver cr,
Jeff Hamilton8ce956c2010-08-17 11:13:53 -0500162 String originalUrl, String url) {
Jeff Hamilton84029622010-08-05 14:29:28 -0500163 if (cr == null || url == null) {
164 return null;
165 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800166
Jeff Hamilton84029622010-08-05 14:29:28 -0500167 // If originalUrl is null, just set it to url.
168 if (originalUrl == null) {
169 originalUrl = url;
170 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800171
Jeff Hamilton84029622010-08-05 14:29:28 -0500172 // Look for both the original url and the actual url. This takes in to
173 // account redirects.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800174
John Reck57928452011-02-17 12:44:07 -0800175 final String[] selArgs = new String[] { originalUrl, url };
Jeff Hamilton1a805652010-09-07 12:36:30 -0700176 final String[] projection = new String[] { Combined.URL };
177 return cr.query(Combined.CONTENT_URI, projection, QUERY_BOOKMARKS_WHERE, selArgs, null);
Jeff Hamilton84029622010-08-05 14:29:28 -0500178 }
179
180 // Strip the query from the given url.
181 static String removeQuery(String url) {
182 if (url == null) {
183 return null;
184 }
185 int query = url.indexOf('?');
186 String noQuery = url;
187 if (query != -1) {
188 noQuery = url.substring(0, query);
189 }
190 return noQuery;
191 }
192
193 /**
194 * Update the bookmark's favicon. This is a convenience method for updating
195 * a bookmark favicon for the originalUrl and url of the passed in WebView.
196 * @param cr The ContentResolver to use.
197 * @param originalUrl The original url before any redirects.
198 * @param url The current url.
199 * @param favicon The favicon bitmap to write to the db.
200 */
Jeff Hamilton1a805652010-09-07 12:36:30 -0700201 /* package */ static void updateFavicon(final ContentResolver cr,
Jeff Hamilton84029622010-08-05 14:29:28 -0500202 final String originalUrl, final String url, final Bitmap favicon) {
203 new AsyncTask<Void, Void, Void>() {
204 @Override
205 protected Void doInBackground(Void... unused) {
John Reck568467e2010-12-21 14:15:50 -0800206 final ByteArrayOutputStream os = new ByteArrayOutputStream();
207 favicon.compress(Bitmap.CompressFormat.PNG, 100, os);
Jeff Hamilton1a805652010-09-07 12:36:30 -0700208
John Reck568467e2010-12-21 14:15:50 -0800209 // The Images update will insert if it doesn't exist
210 ContentValues values = new ContentValues();
211 values.put(Images.FAVICON, os.toByteArray());
212 updateImages(cr, originalUrl, values);
213 updateImages(cr, url, values);
Jeff Hamilton84029622010-08-05 14:29:28 -0500214 return null;
215 }
John Reck568467e2010-12-21 14:15:50 -0800216
217 private void updateImages(final ContentResolver cr,
218 final String url, ContentValues values) {
219 String iurl = removeQuery(url);
220 if (!TextUtils.isEmpty(iurl)) {
221 values.put(Images.URL, iurl);
222 cr.update(BrowserContract.Images.CONTENT_URI, values, null, null);
223 }
224 }
Jeff Hamilton84029622010-08-05 14:29:28 -0500225 }.execute();
226 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100227}