blob: a34d9c35020e31e3d796f804f5fcdf3455c946bd [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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 android.provider;
18
19import android.content.ContentResolver;
Jeff Hamilton6f42edc2010-08-18 11:12:05 -050020import android.content.ContentUris;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.ContentValues;
22import android.content.Context;
23import android.content.Intent;
24import android.database.Cursor;
25import android.database.DatabaseUtils;
Jeff Hamilton31d95db2010-07-29 16:00:55 -050026import android.graphics.BitmapFactory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.net.Uri;
Jeff Hamilton6f42edc2010-08-18 11:12:05 -050028import android.provider.BrowserContract.Bookmarks;
John Reckba5c4122010-12-22 15:42:34 -080029import android.provider.BrowserContract.Combined;
Jeff Hamilton6f42edc2010-08-18 11:12:05 -050030import android.provider.BrowserContract.History;
Jeff Hamilton88697b52010-08-23 10:16:43 -050031import android.provider.BrowserContract.Searches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.util.Log;
33import android.webkit.WebIconDatabase;
34
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035public class Browser {
36 private static final String LOGTAG = "browser";
Jeff Hamilton31d95db2010-07-29 16:00:55 -050037
38 /**
39 * A table containing both bookmarks and history items. The columns of the table are defined in
40 * {@link BookmarkColumns}. Reading this table requires the
41 * {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} permission and writing to it
42 * requires the {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS} permission.
43 */
44 public static final Uri BOOKMARKS_URI = Uri.parse("content://browser/bookmarks");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
46 /**
47 * The name of extra data when starting Browser with ACTION_VIEW or
48 * ACTION_SEARCH intent.
49 * <p>
50 * The value should be an integer between 0 and 1000. If not set or set to
51 * 0, the Browser will use default. If set to 100, the Browser will start
52 * with 100%.
53 */
54 public static final String INITIAL_ZOOM_LEVEL = "browser.initialZoomLevel";
55
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070056 /**
57 * The name of the extra data when starting the Browser from another
58 * application.
59 * <p>
60 * The value is a unique identification string that will be used to
Jeff Hamilton31d95db2010-07-29 16:00:55 -050061 * identify the calling application. The Browser will attempt to reuse the
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070062 * same window each time the application launches the Browser with the same
63 * identifier.
64 */
Jeff Hamilton31d95db2010-07-29 16:00:55 -050065 public static final String EXTRA_APPLICATION_ID = "com.android.browser.application_id";
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -070066
Mitsuru Oshima8eb241b2009-06-10 15:18:08 -070067 /**
Grace Kloba67909182010-01-27 14:11:13 -080068 * The name of the extra data in the VIEW intent. The data are key/value
69 * pairs in the format of Bundle. They will be sent in the HTTP request
Grace Klobad0d9bc22010-01-26 18:08:28 -080070 * headers for the provided url. The keys can't be the standard HTTP headers
71 * as they are set by the WebView. The url's schema must be http(s).
72 * <p>
73 */
Grace Kloba67909182010-01-27 14:11:13 -080074 public static final String EXTRA_HEADERS = "com.android.browser.headers";
Grace Klobad0d9bc22010-01-26 18:08:28 -080075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 /* if you change column order you must also change indices
77 below */
78 public static final String[] HISTORY_PROJECTION = new String[] {
Jeff Hamilton31d95db2010-07-29 16:00:55 -050079 BookmarkColumns._ID, // 0
80 BookmarkColumns.URL, // 1
81 BookmarkColumns.VISITS, // 2
82 BookmarkColumns.DATE, // 3
83 BookmarkColumns.BOOKMARK, // 4
84 BookmarkColumns.TITLE, // 5
85 BookmarkColumns.FAVICON, // 6
86 BookmarkColumns.THUMBNAIL, // 7
87 BookmarkColumns.TOUCH_ICON, // 8
88 BookmarkColumns.USER_ENTERED, // 9
89 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91 /* these indices dependent on HISTORY_PROJECTION */
92 public static final int HISTORY_PROJECTION_ID_INDEX = 0;
93 public static final int HISTORY_PROJECTION_URL_INDEX = 1;
94 public static final int HISTORY_PROJECTION_VISITS_INDEX = 2;
95 public static final int HISTORY_PROJECTION_DATE_INDEX = 3;
96 public static final int HISTORY_PROJECTION_BOOKMARK_INDEX = 4;
97 public static final int HISTORY_PROJECTION_TITLE_INDEX = 5;
98 public static final int HISTORY_PROJECTION_FAVICON_INDEX = 6;
Leon Scroggins9f53fca2009-06-18 18:12:10 -040099 /**
Leon Scroggins7e2ff1a2009-06-18 17:45:14 -0400100 * @hide
101 */
Leon Scroggins908baed2009-06-18 13:39:38 -0400102 public static final int HISTORY_PROJECTION_THUMBNAIL_INDEX = 7;
Patrick Scott2ba12622009-08-04 13:20:05 -0400103 /**
104 * @hide
105 */
106 public static final int HISTORY_PROJECTION_TOUCH_ICON_INDEX = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108 /* columns needed to determine whether to truncate history */
109 public static final String[] TRUNCATE_HISTORY_PROJECTION = new String[] {
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500110 BookmarkColumns._ID,
111 BookmarkColumns.DATE,
112 };
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 public static final int TRUNCATE_HISTORY_PROJECTION_ID_INDEX = 0;
115
116 /* truncate this many history items at a time */
117 public static final int TRUNCATE_N_OLDEST = 5;
118
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500119 /**
120 * A table containing a log of browser searches. The columns of the table are defined in
121 * {@link SearchColumns}. Reading this table requires the
122 * {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} permission and writing to it
123 * requires the {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS} permission.
124 */
125 public static final Uri SEARCHES_URI = Uri.parse("content://browser/searches");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500127 /**
128 * A projection of {@link #SEARCHES_URI} that contains {@link SearchColumns#_ID},
129 * {@link SearchColumns#SEARCH}, and {@link SearchColumns#DATE}.
130 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 public static final String[] SEARCHES_PROJECTION = new String[] {
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500132 // if you change column order you must also change indices below
133 SearchColumns._ID, // 0
134 SearchColumns.SEARCH, // 1
135 SearchColumns.DATE, // 2
136 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
138 /* these indices dependent on SEARCHES_PROJECTION */
139 public static final int SEARCHES_PROJECTION_SEARCH_INDEX = 1;
140 public static final int SEARCHES_PROJECTION_DATE_INDEX = 2;
141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 /* Set a cap on the count of history items in the history/bookmark
143 table, to prevent db and layout operations from dragging to a
144 crawl. Revisit this cap when/if db/layout performance
145 improvements are made. Note: this does not affect bookmark
146 entries -- if the user wants more bookmarks than the cap, they
147 get them. */
148 private static final int MAX_HISTORY_COUNT = 250;
149
150 /**
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500151 * Open an activity to save a bookmark. Launch with a title
152 * and/or a url, both of which can be edited by the user before saving.
153 *
154 * @param c Context used to launch the activity to add a bookmark.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 * @param title Title for the bookmark. Can be null or empty string.
156 * @param url Url for the bookmark. Can be null or empty string.
157 */
158 public static final void saveBookmark(Context c,
159 String title,
160 String url) {
161 Intent i = new Intent(Intent.ACTION_INSERT, Browser.BOOKMARKS_URI);
162 i.putExtra("title", title);
163 i.putExtra("url", url);
164 c.startActivity(i);
165 }
166
Leon Scrogginsdcf19a82009-12-10 12:37:09 -0500167 /**
Leon Scroggins31cb0be2011-02-25 10:53:50 -0500168 * Boolean extra passed along with an Intent to a browser, specifying that
169 * a new tab be created. Overrides EXTRA_APPLICATION_ID; if both are set,
170 * a new tab will be used, rather than using the same one.
Leon Scrogginsaf5b4062011-02-22 16:35:13 -0500171 */
172 public static final String EXTRA_CREATE_NEW_TAB = "create_new_tab";
173
174 /**
Leon Scrogginsdcf19a82009-12-10 12:37:09 -0500175 * Stores a Bitmap extra in an {@link Intent} representing the screenshot of
176 * a page to share. When receiving an {@link Intent#ACTION_SEND} from the
177 * Browser, use this to access the screenshot.
178 * @hide
179 */
180 public final static String EXTRA_SHARE_SCREENSHOT = "share_screenshot";
181
182 /**
183 * Stores a Bitmap extra in an {@link Intent} representing the favicon of a
184 * page to share. When receiving an {@link Intent#ACTION_SEND} from the
185 * Browser, use this to access the favicon.
186 * @hide
187 */
188 public final static String EXTRA_SHARE_FAVICON = "share_favicon";
189
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500190 /**
191 * Sends the given string using an Intent with {@link Intent#ACTION_SEND} and a mime type
192 * of text/plain. The string is put into {@link Intent#EXTRA_TEXT}.
193 *
194 * @param context the context used to start the activity
195 * @param string the string to send
196 */
197 public static final void sendString(Context context, String string) {
198 sendString(context, string, context.getString(com.android.internal.R.string.sendText));
Andrei Popescu354eb662009-09-24 13:45:08 +0100199 }
200
201 /**
202 * Find an application to handle the given string and, if found, invoke
203 * it with the given string as a parameter.
204 * @param c Context used to launch the new activity.
205 * @param stringToSend The string to be handled.
206 * @param chooserDialogTitle The title of the dialog that allows the user
207 * to select between multiple applications that are all capable of handling
208 * the string.
209 * @hide pending API council approval
210 */
211 public static final void sendString(Context c,
212 String stringToSend,
213 String chooserDialogTitle) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 Intent send = new Intent(Intent.ACTION_SEND);
215 send.setType("text/plain");
Andrei Popescu354eb662009-09-24 13:45:08 +0100216 send.putExtra(Intent.EXTRA_TEXT, stringToSend);
217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 try {
Leon Scrogginsfe026bd2010-08-24 14:16:09 -0400219 Intent i = Intent.createChooser(send, chooserDialogTitle);
220 // In case this is called from outside an Activity
221 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
222 c.startActivity(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 } catch(android.content.ActivityNotFoundException ex) {
224 // if no app handles it, do nothing
225 }
226 }
227
228 /**
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500229 * Return a cursor pointing to a list of all the bookmarks. The cursor will have a single
230 * column, {@link BookmarkColumns#URL}.
231 * <p>
Leon Scroggins9ce4c6c2009-06-19 14:13:08 -0400232 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS}
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500233 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 * @param cr The ContentResolver used to access the database.
235 */
236 public static final Cursor getAllBookmarks(ContentResolver cr) throws
237 IllegalStateException {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500238 return cr.query(Bookmarks.CONTENT_URI,
239 new String[] { Bookmarks.URL },
240 Bookmarks.IS_FOLDER + " = 0", null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 }
242
243 /**
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500244 * Return a cursor pointing to a list of all visited site urls. The cursor will
245 * have a single column, {@link BookmarkColumns#URL}.
246 * <p>
Leon Scroggins9ce4c6c2009-06-19 14:13:08 -0400247 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS}
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500248 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 * @param cr The ContentResolver used to access the database.
250 */
251 public static final Cursor getAllVisitedUrls(ContentResolver cr) throws
252 IllegalStateException {
John Reckba5c4122010-12-22 15:42:34 -0800253 return cr.query(Combined.CONTENT_URI,
254 new String[] { Combined.URL }, null, null,
255 Combined.DATE_CREATED + " ASC");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 }
257
Leon Scrogginsd2334cb2010-03-02 15:58:40 -0500258 private static final void addOrUrlEquals(StringBuilder sb) {
259 sb.append(" OR " + BookmarkColumns.URL + " = ");
260 }
261
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500262 private static final Cursor getVisitedLike(ContentResolver cr, String url) {
Leon Scrogginsd2334cb2010-03-02 15:58:40 -0500263 boolean secure = false;
264 String compareString = url;
265 if (compareString.startsWith("http://")) {
266 compareString = compareString.substring(7);
267 } else if (compareString.startsWith("https://")) {
268 compareString = compareString.substring(8);
269 secure = true;
270 }
271 if (compareString.startsWith("www.")) {
272 compareString = compareString.substring(4);
273 }
274 StringBuilder whereClause = null;
275 if (secure) {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500276 whereClause = new StringBuilder(Bookmarks.URL + " = ");
Leon Scrogginsd2334cb2010-03-02 15:58:40 -0500277 DatabaseUtils.appendEscapedSQLString(whereClause,
278 "https://" + compareString);
279 addOrUrlEquals(whereClause);
280 DatabaseUtils.appendEscapedSQLString(whereClause,
281 "https://www." + compareString);
282 } else {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500283 whereClause = new StringBuilder(Bookmarks.URL + " = ");
Leon Scrogginsd2334cb2010-03-02 15:58:40 -0500284 DatabaseUtils.appendEscapedSQLString(whereClause,
285 compareString);
286 addOrUrlEquals(whereClause);
287 String wwwString = "www." + compareString;
288 DatabaseUtils.appendEscapedSQLString(whereClause,
289 wwwString);
290 addOrUrlEquals(whereClause);
291 DatabaseUtils.appendEscapedSQLString(whereClause,
292 "http://" + compareString);
293 addOrUrlEquals(whereClause);
294 DatabaseUtils.appendEscapedSQLString(whereClause,
295 "http://" + wwwString);
296 }
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500297 return cr.query(History.CONTENT_URI, new String[] { History._ID, History.VISITS },
Leon Scrogginsd2334cb2010-03-02 15:58:40 -0500298 whereClause.toString(), null, null);
299 }
300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 /**
302 * Update the visited history to acknowledge that a site has been
303 * visited.
Leon Scroggins9ce4c6c2009-06-19 14:13:08 -0400304 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS}
305 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 * @param cr The ContentResolver used to access the database.
307 * @param url The site being visited.
Leon Scroggins331c7782009-11-25 12:43:11 -0500308 * @param real If true, this is an actual visit, and should add to the
309 * number of visits. If false, the user entered it manually.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 */
311 public static final void updateVisitedHistory(ContentResolver cr,
312 String url, boolean real) {
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500313 long now = System.currentTimeMillis();
Leon Scrogginse77852c2010-03-24 14:44:04 -0400314 Cursor c = null;
315 try {
316 c = getVisitedLike(cr, url);
317 /* We should only get one answer that is exactly the same. */
318 if (c.moveToFirst()) {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500319 ContentValues values = new ContentValues();
Leon Scrogginse77852c2010-03-24 14:44:04 -0400320 if (real) {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500321 values.put(History.VISITS, c.getInt(1) + 1);
Leon Scroggins331c7782009-11-25 12:43:11 -0500322 } else {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500323 values.put(History.USER_ENTERED, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 }
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500325 values.put(History.DATE_LAST_VISITED, now);
326 cr.update(ContentUris.withAppendedId(History.CONTENT_URI, c.getLong(0)),
327 values, null, null);
Leon Scrogginse77852c2010-03-24 14:44:04 -0400328 } else {
329 truncateHistory(cr);
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500330 ContentValues values = new ContentValues();
Leon Scrogginse77852c2010-03-24 14:44:04 -0400331 int visits;
332 int user_entered;
333 if (real) {
334 visits = 1;
335 user_entered = 0;
336 } else {
337 visits = 0;
338 user_entered = 1;
339 }
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500340 values.put(History.URL, url);
341 values.put(History.VISITS, visits);
342 values.put(History.DATE_LAST_VISITED, now);
343 values.put(History.TITLE, url);
344 values.put(History.DATE_CREATED, 0);
345 values.put(History.USER_ENTERED, user_entered);
346 cr.insert(History.CONTENT_URI, values);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 }
Leon Scrogginse77852c2010-03-24 14:44:04 -0400348 } catch (IllegalStateException e) {
349 Log.e(LOGTAG, "updateVisitedHistory", e);
350 } finally {
351 if (c != null) c.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 }
353 }
354
355 /**
Leon Clarke9c8d8862009-09-24 15:20:10 +0100356 * Returns all the URLs in the history.
357 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS}
358 * @param cr The ContentResolver used to access the database.
359 * @hide pending API council approval
360 */
361 public static final String[] getVisitedHistory(ContentResolver cr) {
Leon Scrogginsfe1143b2010-03-12 17:42:21 -0500362 Cursor c = null;
363 String[] str = null;
Grace Klobabf54f02b2009-09-24 14:14:11 -0700364 try {
365 String[] projection = new String[] {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500366 History.URL,
Grace Klobabf54f02b2009-09-24 14:14:11 -0700367 };
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500368 c = cr.query(History.CONTENT_URI, projection, History.VISITS + " > 0", null, null);
369 if (c == null) return new String[0];
Leon Scrogginsfe1143b2010-03-12 17:42:21 -0500370 str = new String[c.getCount()];
Grace Klobabf54f02b2009-09-24 14:14:11 -0700371 int i = 0;
372 while (c.moveToNext()) {
373 str[i] = c.getString(0);
374 i++;
375 }
Grace Klobabf54f02b2009-09-24 14:14:11 -0700376 } catch (IllegalStateException e) {
Leon Scrogginsfe1143b2010-03-12 17:42:21 -0500377 Log.e(LOGTAG, "getVisitedHistory", e);
378 str = new String[0];
379 } finally {
380 if (c != null) c.close();
Grace Klobabf54f02b2009-09-24 14:14:11 -0700381 }
Leon Scrogginsfe1143b2010-03-12 17:42:21 -0500382 return str;
Leon Clarke9c8d8862009-09-24 15:20:10 +0100383 }
384
385 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 * If there are more than MAX_HISTORY_COUNT non-bookmark history
387 * items in the bookmark/history table, delete TRUNCATE_N_OLDEST
388 * of them. This is used to keep our history table to a
389 * reasonable size. Note: it does not prune bookmarks. If the
390 * user wants 1000 bookmarks, the user gets 1000 bookmarks.
Leon Scroggins9ce4c6c2009-06-19 14:13:08 -0400391 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS}
392 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 *
394 * @param cr The ContentResolver used to access the database.
395 */
396 public static final void truncateHistory(ContentResolver cr) {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500397 // TODO make a single request to the provider to do this in a single transaction
398 Cursor cursor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 try {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 // Select non-bookmark history, ordered by date
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500402 cursor = cr.query(History.CONTENT_URI,
403 new String[] { History._ID, History.URL, History.DATE_LAST_VISITED },
404 null, null, History.DATE_LAST_VISITED + " ASC");
405
406 if (cursor.moveToFirst() && cursor.getCount() >= MAX_HISTORY_COUNT) {
407 final WebIconDatabase iconDb = WebIconDatabase.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 /* eliminate oldest history items */
409 for (int i = 0; i < TRUNCATE_N_OLDEST; i++) {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500410 cr.delete(ContentUris.withAppendedId(History.CONTENT_URI, cursor.getLong(0)),
411 null, null);
412 iconDb.releaseIconForPageUrl(cursor.getString(1));
413 if (!cursor.moveToNext()) break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 }
415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 } catch (IllegalStateException e) {
417 Log.e(LOGTAG, "truncateHistory", e);
Leon Scrogginsfe1143b2010-03-12 17:42:21 -0500418 } finally {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500419 if (cursor != null) cursor.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 }
421 }
422
423 /**
424 * Returns whether there is any history to clear.
Leon Scroggins9ce4c6c2009-06-19 14:13:08 -0400425 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 * @param cr The ContentResolver used to access the database.
427 * @return boolean True if the history can be cleared.
428 */
429 public static final boolean canClearHistory(ContentResolver cr) {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500430 Cursor cursor = null;
Leon Scrogginsfe1143b2010-03-12 17:42:21 -0500431 boolean ret = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 try {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500433 cursor = cr.query(History.CONTENT_URI,
434 new String [] { History._ID, History.VISITS },
435 null, null, null);
436 ret = cursor.getCount() > 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 } catch (IllegalStateException e) {
Leon Scrogginsfe1143b2010-03-12 17:42:21 -0500438 Log.e(LOGTAG, "canClearHistory", e);
439 } finally {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500440 if (cursor != null) cursor.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
Leon Scrogginsfe1143b2010-03-12 17:42:21 -0500442 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 }
444
445 /**
446 * Delete all entries from the bookmarks/history table which are
447 * not bookmarks. Also set all visited bookmarks to unvisited.
Leon Scroggins9ce4c6c2009-06-19 14:13:08 -0400448 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 * @param cr The ContentResolver used to access the database.
450 */
451 public static final void clearHistory(ContentResolver cr) {
452 deleteHistoryWhere(cr, null);
453 }
454
455 /**
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500456 * Helper function to delete all history items and release the icons for them in the
457 * {@link WebIconDatabase}.
458 *
459 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS}
460 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}
461 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 * @param cr The ContentResolver used to access the database.
463 * @param whereClause String to limit the items affected.
464 * null means all items.
465 */
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500466 private static final void deleteHistoryWhere(ContentResolver cr, String whereClause) {
467 Cursor cursor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 try {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500469 cursor = cr.query(History.CONTENT_URI, new String[] { History.URL }, whereClause,
470 null, null);
471 if (cursor.moveToFirst()) {
Leon Scrogginsfe1143b2010-03-12 17:42:21 -0500472 final WebIconDatabase iconDb = WebIconDatabase.getInstance();
Leon Scrogginsfe1143b2010-03-12 17:42:21 -0500473 do {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500474 // Delete favicons
475 // TODO don't release if the URL is bookmarked
476 iconDb.releaseIconForPageUrl(cursor.getString(0));
477 } while (cursor.moveToNext());
Leon Scrogginsfe1143b2010-03-12 17:42:21 -0500478
Jeff Hamilton5360eab2010-09-16 00:46:48 -0500479 cr.delete(History.CONTENT_URI, whereClause, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 } catch (IllegalStateException e) {
Leon Scrogginsfe1143b2010-03-12 17:42:21 -0500482 Log.e(LOGTAG, "deleteHistoryWhere", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 return;
Leon Scrogginsfe1143b2010-03-12 17:42:21 -0500484 } finally {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500485 if (cursor != null) cursor.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 }
487 }
488
489 /**
490 * Delete all history items from begin to end.
Leon Scroggins9ce4c6c2009-06-19 14:13:08 -0400491 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 * @param cr The ContentResolver used to access the database.
493 * @param begin First date to remove. If -1, all dates before end.
494 * Inclusive.
495 * @param end Last date to remove. If -1, all dates after begin.
496 * Non-inclusive.
497 */
498 public static final void deleteHistoryTimeFrame(ContentResolver cr,
499 long begin, long end) {
500 String whereClause;
501 String date = BookmarkColumns.DATE;
502 if (-1 == begin) {
503 if (-1 == end) {
504 clearHistory(cr);
505 return;
506 }
507 whereClause = date + " < " + Long.toString(end);
508 } else if (-1 == end) {
509 whereClause = date + " >= " + Long.toString(begin);
510 } else {
511 whereClause = date + " >= " + Long.toString(begin) + " AND " + date
512 + " < " + Long.toString(end);
513 }
514 deleteHistoryWhere(cr, whereClause);
515 }
516
517 /**
518 * Remove a specific url from the history database.
Leon Scroggins9ce4c6c2009-06-19 14:13:08 -0400519 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 * @param cr The ContentResolver used to access the database.
521 * @param url url to remove.
522 */
523 public static final void deleteFromHistory(ContentResolver cr,
524 String url) {
Jeff Hamilton6f42edc2010-08-18 11:12:05 -0500525 cr.delete(History.CONTENT_URI, History.URL + "=?", new String[] { url });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 }
527
528 /**
529 * Add a search string to the searches database.
Leon Scroggins9ce4c6c2009-06-19 14:13:08 -0400530 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS}
531 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 * @param cr The ContentResolver used to access the database.
533 * @param search The string to add to the searches database.
534 */
535 public static final void addSearchUrl(ContentResolver cr, String search) {
Jeff Hamilton88697b52010-08-23 10:16:43 -0500536 // The content provider will take care of updating existing searches instead of duplicating
537 ContentValues values = new ContentValues();
538 values.put(Searches.SEARCH, search);
539 values.put(Searches.DATE, System.currentTimeMillis());
540 cr.insert(Searches.CONTENT_URI, values);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 }
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 /**
544 * Remove all searches from the search database.
Leon Scroggins9ce4c6c2009-06-19 14:13:08 -0400545 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 * @param cr The ContentResolver used to access the database.
547 */
548 public static final void clearSearches(ContentResolver cr) {
549 // FIXME: Should this clear the urls to which these searches lead?
550 // (i.e. remove google.com/query= blah blah blah)
551 try {
Jeff Hamilton88697b52010-08-23 10:16:43 -0500552 cr.delete(Searches.CONTENT_URI, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 } catch (IllegalStateException e) {
554 Log.e(LOGTAG, "clearSearches", e);
555 }
556 }
557
558 /**
Patrick Scott36acfbc2010-04-09 12:42:03 -0400559 * Request all icons from the database. This call must either be called
560 * in the main thread or have had Looper.prepare() invoked in the calling
561 * thread.
Leon Scroggins9ce4c6c2009-06-19 14:13:08 -0400562 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 * @param cr The ContentResolver used to access the database.
564 * @param where Clause to be used to limit the query from the database.
565 * Must be an allowable string to be passed into a database query.
566 * @param listener IconListener that gets the icons once they are
567 * retrieved.
568 */
569 public static final void requestAllIcons(ContentResolver cr, String where,
570 WebIconDatabase.IconListener listener) {
Jeff Hamilton88697b52010-08-23 10:16:43 -0500571 WebIconDatabase.getInstance().bulkRequestIconForPageUrl(cr, where, listener);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
573
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500574 /**
575 * Column definitions for the mixed bookmark and history items available
576 * at {@link #BOOKMARKS_URI}.
577 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 public static class BookmarkColumns implements BaseColumns {
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500579 /**
580 * The URL of the bookmark or history item.
581 * <p>Type: TEXT (URL)</p>
582 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 public static final String URL = "url";
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500584
585 /**
586 * The number of time the item has been visited.
587 * <p>Type: NUMBER</p>
588 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 public static final String VISITS = "visits";
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500590
591 /**
592 * The date the item was last visited, in milliseconds since the epoch.
593 * <p>Type: NUMBER (date in milliseconds since January 1, 1970)</p>
594 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 public static final String DATE = "date";
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500596
597 /**
598 * Flag indicating that an item is a bookmark. A value of 1 indicates a bookmark, a value
599 * of 0 indicates a history item.
600 * <p>Type: INTEGER (boolean)</p>
601 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 public static final String BOOKMARK = "bookmark";
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500603
604 /**
605 * The user visible title of the bookmark or history item.
606 * <p>Type: TEXT</p>
607 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 public static final String TITLE = "title";
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500609
610 /**
611 * The date the item created, in milliseconds since the epoch.
612 * <p>Type: NUMBER (date in milliseconds since January 1, 1970)</p>
613 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 public static final String CREATED = "created";
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500615
616 /**
617 * The favicon of the bookmark. Must decode via {@link BitmapFactory#decodeByteArray}.
618 * <p>Type: BLOB (image)</p>
619 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 public static final String FAVICON = "favicon";
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500621
Leon Scroggins9f53fca2009-06-18 18:12:10 -0400622 /**
Leon Scroggins7e2ff1a2009-06-18 17:45:14 -0400623 * @hide
624 */
Leon Scroggins908baed2009-06-18 13:39:38 -0400625 public static final String THUMBNAIL = "thumbnail";
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500626
Patrick Scott2ba12622009-08-04 13:20:05 -0400627 /**
628 * @hide
629 */
630 public static final String TOUCH_ICON = "touch_icon";
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500631
Leon Scroggins331c7782009-11-25 12:43:11 -0500632 /**
633 * @hide
634 */
635 public static final String USER_ENTERED = "user_entered";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 }
637
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500638 /**
639 * Column definitions for the search history table, available at {@link #SEARCHES_URI}.
640 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 public static class SearchColumns implements BaseColumns {
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500642 /**
Kristian Monsenb5503c12010-08-31 14:02:38 +0100643 * @deprecated Not used.
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500644 */
645 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 public static final String URL = "url";
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500647
648 /**
649 * The user entered search term.
650 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 public static final String SEARCH = "search";
Jeff Hamilton31d95db2010-07-29 16:00:55 -0500652
653 /**
654 * The date the search was performed, in milliseconds since the epoch.
655 * <p>Type: NUMBER (date in milliseconds since January 1, 1970)</p>
656 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 public static final String DATE = "date";
658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659}