Browser.java revision 2187f30d147fddafe1304af45bb43e8fedf06cea
1/* 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; 20import android.content.ContentValues; 21import android.content.Context; 22import android.content.Intent; 23import android.database.Cursor; 24import android.database.DatabaseUtils; 25import android.net.Uri; 26import android.util.Log; 27import android.webkit.WebIconDatabase; 28 29import java.util.Date; 30 31public class Browser { 32 private static final String LOGTAG = "browser"; 33 public static final Uri BOOKMARKS_URI = 34 Uri.parse("content://browser/bookmarks"); 35 36 /** 37 * The inline scheme to show embedded content in a browser. 38 * @hide 39 */ 40 public static final Uri INLINE_URI = Uri.parse("inline:"); 41 42 /** 43 * The name of extra data when starting Browser with ACTION_VIEW or 44 * ACTION_SEARCH intent. 45 * <p> 46 * The value should be an integer between 0 and 1000. If not set or set to 47 * 0, the Browser will use default. If set to 100, the Browser will start 48 * with 100%. 49 */ 50 public static final String INITIAL_ZOOM_LEVEL = "browser.initialZoomLevel"; 51 52 /** 53 * The name of the extra data when starting the Browser from another 54 * application. 55 * <p> 56 * The value is a unique identification string that will be used to 57 * indentify the calling application. The Browser will attempt to reuse the 58 * same window each time the application launches the Browser with the same 59 * identifier. 60 */ 61 public static final String EXTRA_APPLICATION_ID = 62 "com.android.browser.application_id"; 63 64 /** 65 * The content to be rendered when url's scheme is inline. 66 * @hide 67 */ 68 public static final String EXTRA_INLINE_CONTENT ="com.android.browser.inline.content"; 69 70 /** 71 * The encoding of the inlined content for inline scheme. 72 * @hide 73 */ 74 public static final String EXTRA_INLINE_ENCODING ="com.android.browser.inline.encoding"; 75 76 /** 77 * The url used when the inline content is falied to render. 78 * @hide 79 */ 80 public static final String EXTRA_INLINE_FAILURL ="com.android.browser.inline.failurl"; 81 82 /** 83 * The name of the extra data in the VIEW intent. The data is in boolean. 84 * <p> 85 * If the Browser is handling the intent and the setting for 86 * USE_LOCATION_FOR_SERVICES is allow, the Browser will send the location in 87 * the POST data if this extra data is presented and it is true. 88 * <p> 89 * pending api approval 90 * @hide 91 */ 92 public static final String EXTRA_APPEND_LOCATION = "com.android.browser.append_location"; 93 94 /** 95 * The name of the extra data in the VIEW intent. The data is in the format of 96 * a byte array. 97 * <p> 98 * Any value sent here will be passed in the http request to the provided url as post data. 99 * <p> 100 * pending api approval 101 * @hide 102 */ 103 public static final String EXTRA_POST_DATA = "com.android.browser.post_data"; 104 105 /* if you change column order you must also change indices 106 below */ 107 public static final String[] HISTORY_PROJECTION = new String[] { 108 BookmarkColumns._ID, BookmarkColumns.URL, BookmarkColumns.VISITS, 109 BookmarkColumns.DATE, BookmarkColumns.BOOKMARK, BookmarkColumns.TITLE, 110 BookmarkColumns.FAVICON, BookmarkColumns.THUMBNAIL }; 111 112 /* these indices dependent on HISTORY_PROJECTION */ 113 public static final int HISTORY_PROJECTION_ID_INDEX = 0; 114 public static final int HISTORY_PROJECTION_URL_INDEX = 1; 115 public static final int HISTORY_PROJECTION_VISITS_INDEX = 2; 116 public static final int HISTORY_PROJECTION_DATE_INDEX = 3; 117 public static final int HISTORY_PROJECTION_BOOKMARK_INDEX = 4; 118 public static final int HISTORY_PROJECTION_TITLE_INDEX = 5; 119 public static final int HISTORY_PROJECTION_FAVICON_INDEX = 6; 120 /** 121 * @hide 122 */ 123 public static final int HISTORY_PROJECTION_THUMBNAIL_INDEX = 7; 124 125 /* columns needed to determine whether to truncate history */ 126 public static final String[] TRUNCATE_HISTORY_PROJECTION = new String[] { 127 BookmarkColumns._ID, BookmarkColumns.DATE, }; 128 public static final int TRUNCATE_HISTORY_PROJECTION_ID_INDEX = 0; 129 130 /* truncate this many history items at a time */ 131 public static final int TRUNCATE_N_OLDEST = 5; 132 133 public static final Uri SEARCHES_URI = 134 Uri.parse("content://browser/searches"); 135 136 /* if you change column order you must also change indices 137 below */ 138 public static final String[] SEARCHES_PROJECTION = new String[] { 139 SearchColumns._ID, SearchColumns.SEARCH, SearchColumns.DATE }; 140 141 /* these indices dependent on SEARCHES_PROJECTION */ 142 public static final int SEARCHES_PROJECTION_SEARCH_INDEX = 1; 143 public static final int SEARCHES_PROJECTION_DATE_INDEX = 2; 144 145 private static final String SEARCHES_WHERE_CLAUSE = "search = ?"; 146 147 /* Set a cap on the count of history items in the history/bookmark 148 table, to prevent db and layout operations from dragging to a 149 crawl. Revisit this cap when/if db/layout performance 150 improvements are made. Note: this does not affect bookmark 151 entries -- if the user wants more bookmarks than the cap, they 152 get them. */ 153 private static final int MAX_HISTORY_COUNT = 250; 154 155 /** 156 * Open the AddBookmark activity to save a bookmark. Launch with 157 * and/or url, which can be edited by the user before saving. 158 * @param c Context used to launch the AddBookmark activity. 159 * @param title Title for the bookmark. Can be null or empty string. 160 * @param url Url for the bookmark. Can be null or empty string. 161 */ 162 public static final void saveBookmark(Context c, 163 String title, 164 String url) { 165 Intent i = new Intent(Intent.ACTION_INSERT, Browser.BOOKMARKS_URI); 166 i.putExtra("title", title); 167 i.putExtra("url", url); 168 c.startActivity(i); 169 } 170 171 public static final void sendString(Context c, String s) { 172 Intent send = new Intent(Intent.ACTION_SEND); 173 send.setType("text/plain"); 174 send.putExtra(Intent.EXTRA_TEXT, s); 175 176 try { 177 c.startActivity(Intent.createChooser(send, 178 c.getText(com.android.internal.R.string.sendText))); 179 } catch(android.content.ActivityNotFoundException ex) { 180 // if no app handles it, do nothing 181 } 182 } 183 184 /** 185 * Return a cursor pointing to a list of all the bookmarks. 186 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} 187 * @param cr The ContentResolver used to access the database. 188 */ 189 public static final Cursor getAllBookmarks(ContentResolver cr) throws 190 IllegalStateException { 191 return cr.query(BOOKMARKS_URI, 192 new String[] { BookmarkColumns.URL }, 193 "bookmark = 1", null, null); 194 } 195 196 /** 197 * Return a cursor pointing to a list of all visited site urls. 198 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} 199 * @param cr The ContentResolver used to access the database. 200 */ 201 public static final Cursor getAllVisitedUrls(ContentResolver cr) throws 202 IllegalStateException { 203 return cr.query(BOOKMARKS_URI, 204 new String[] { BookmarkColumns.URL }, null, null, null); 205 } 206 207 /** 208 * Update the visited history to acknowledge that a site has been 209 * visited. 210 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} 211 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS} 212 * @param cr The ContentResolver used to access the database. 213 * @param url The site being visited. 214 * @param real Whether this is an actual visit, and should be added to the 215 * number of visits. 216 */ 217 public static final void updateVisitedHistory(ContentResolver cr, 218 String url, boolean real) { 219 long now = new Date().getTime(); 220 try { 221 StringBuilder sb = new StringBuilder(BookmarkColumns.URL + " = "); 222 DatabaseUtils.appendEscapedSQLString(sb, url); 223 Cursor c = cr.query( 224 BOOKMARKS_URI, 225 HISTORY_PROJECTION, 226 sb.toString(), 227 null, 228 null); 229 /* We should only get one answer that is exactly the same. */ 230 if (c.moveToFirst()) { 231 ContentValues map = new ContentValues(); 232 if (real) { 233 map.put(BookmarkColumns.VISITS, c 234 .getInt(HISTORY_PROJECTION_VISITS_INDEX) + 1); 235 } 236 map.put(BookmarkColumns.DATE, now); 237 cr.update(BOOKMARKS_URI, map, "_id = " + c.getInt(0), null); 238 } else { 239 truncateHistory(cr); 240 ContentValues map = new ContentValues(); 241 map.put(BookmarkColumns.URL, url); 242 map.put(BookmarkColumns.VISITS, real ? 1 : 0); 243 map.put(BookmarkColumns.DATE, now); 244 map.put(BookmarkColumns.BOOKMARK, 0); 245 map.put(BookmarkColumns.TITLE, url); 246 map.put(BookmarkColumns.CREATED, 0); 247 cr.insert(BOOKMARKS_URI, map); 248 } 249 c.deactivate(); 250 } catch (IllegalStateException e) { 251 return; 252 } 253 } 254 255 /** 256 * If there are more than MAX_HISTORY_COUNT non-bookmark history 257 * items in the bookmark/history table, delete TRUNCATE_N_OLDEST 258 * of them. This is used to keep our history table to a 259 * reasonable size. Note: it does not prune bookmarks. If the 260 * user wants 1000 bookmarks, the user gets 1000 bookmarks. 261 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} 262 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS} 263 * 264 * @param cr The ContentResolver used to access the database. 265 */ 266 public static final void truncateHistory(ContentResolver cr) { 267 try { 268 // Select non-bookmark history, ordered by date 269 Cursor c = cr.query( 270 BOOKMARKS_URI, 271 TRUNCATE_HISTORY_PROJECTION, 272 "bookmark = 0", 273 null, 274 BookmarkColumns.DATE); 275 // Log.v(LOGTAG, "history count " + c.count()); 276 if (c.moveToFirst() && c.getCount() >= MAX_HISTORY_COUNT) { 277 /* eliminate oldest history items */ 278 for (int i = 0; i < TRUNCATE_N_OLDEST; i++) { 279 // Log.v(LOGTAG, "truncate history " + 280 // c.getInt(TRUNCATE_HISTORY_PROJECTION_ID_INDEX)); 281 deleteHistoryWhere( 282 cr, "_id = " + 283 c.getInt(TRUNCATE_HISTORY_PROJECTION_ID_INDEX)); 284 if (!c.moveToNext()) break; 285 } 286 } 287 c.deactivate(); 288 } catch (IllegalStateException e) { 289 Log.e(LOGTAG, "truncateHistory", e); 290 return; 291 } 292 } 293 294 /** 295 * Returns whether there is any history to clear. 296 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} 297 * @param cr The ContentResolver used to access the database. 298 * @return boolean True if the history can be cleared. 299 */ 300 public static final boolean canClearHistory(ContentResolver cr) { 301 try { 302 Cursor c = cr.query( 303 BOOKMARKS_URI, 304 new String [] { BookmarkColumns._ID, 305 BookmarkColumns.BOOKMARK, 306 BookmarkColumns.VISITS }, 307 "bookmark = 0 OR visits > 0", 308 null, 309 null 310 ); 311 boolean ret = c.moveToFirst(); 312 c.deactivate(); 313 return ret; 314 } catch (IllegalStateException e) { 315 return false; 316 } 317 } 318 319 /** 320 * Delete all entries from the bookmarks/history table which are 321 * not bookmarks. Also set all visited bookmarks to unvisited. 322 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS} 323 * @param cr The ContentResolver used to access the database. 324 */ 325 public static final void clearHistory(ContentResolver cr) { 326 deleteHistoryWhere(cr, null); 327 } 328 329 /** 330 * Helper function to delete all history items and revert all 331 * bookmarks to zero visits which meet the criteria provided. 332 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} 333 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS} 334 * @param cr The ContentResolver used to access the database. 335 * @param whereClause String to limit the items affected. 336 * null means all items. 337 */ 338 private static final void deleteHistoryWhere(ContentResolver cr, 339 String whereClause) { 340 try { 341 Cursor c = cr.query(BOOKMARKS_URI, 342 HISTORY_PROJECTION, 343 whereClause, 344 null, 345 null); 346 if (!c.moveToFirst()) { 347 c.deactivate(); 348 return; 349 } 350 351 final WebIconDatabase iconDb = WebIconDatabase.getInstance(); 352 /* Delete favicons, and revert bookmarks which have been visited 353 * to simply bookmarks. 354 */ 355 StringBuffer sb = new StringBuffer(); 356 boolean firstTime = true; 357 do { 358 String url = c.getString(HISTORY_PROJECTION_URL_INDEX); 359 boolean isBookmark = 360 c.getInt(HISTORY_PROJECTION_BOOKMARK_INDEX) == 1; 361 if (isBookmark) { 362 if (firstTime) { 363 firstTime = false; 364 } else { 365 sb.append(" OR "); 366 } 367 sb.append("( _id = "); 368 sb.append(c.getInt(0)); 369 sb.append(" )"); 370 } else { 371 iconDb.releaseIconForPageUrl(url); 372 } 373 } while (c.moveToNext()); 374 c.deactivate(); 375 376 if (!firstTime) { 377 ContentValues map = new ContentValues(); 378 map.put(BookmarkColumns.VISITS, 0); 379 map.put(BookmarkColumns.DATE, 0); 380 /* FIXME: Should I also remove the title? */ 381 cr.update(BOOKMARKS_URI, map, sb.toString(), null); 382 } 383 384 String deleteWhereClause = BookmarkColumns.BOOKMARK + " = 0"; 385 if (whereClause != null) { 386 deleteWhereClause += " AND " + whereClause; 387 } 388 cr.delete(BOOKMARKS_URI, deleteWhereClause, null); 389 } catch (IllegalStateException e) { 390 return; 391 } 392 } 393 394 /** 395 * Delete all history items from begin to end. 396 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS} 397 * @param cr The ContentResolver used to access the database. 398 * @param begin First date to remove. If -1, all dates before end. 399 * Inclusive. 400 * @param end Last date to remove. If -1, all dates after begin. 401 * Non-inclusive. 402 */ 403 public static final void deleteHistoryTimeFrame(ContentResolver cr, 404 long begin, long end) { 405 String whereClause; 406 String date = BookmarkColumns.DATE; 407 if (-1 == begin) { 408 if (-1 == end) { 409 clearHistory(cr); 410 return; 411 } 412 whereClause = date + " < " + Long.toString(end); 413 } else if (-1 == end) { 414 whereClause = date + " >= " + Long.toString(begin); 415 } else { 416 whereClause = date + " >= " + Long.toString(begin) + " AND " + date 417 + " < " + Long.toString(end); 418 } 419 deleteHistoryWhere(cr, whereClause); 420 } 421 422 /** 423 * Remove a specific url from the history database. 424 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS} 425 * @param cr The ContentResolver used to access the database. 426 * @param url url to remove. 427 */ 428 public static final void deleteFromHistory(ContentResolver cr, 429 String url) { 430 StringBuilder sb = new StringBuilder(BookmarkColumns.URL + " = "); 431 DatabaseUtils.appendEscapedSQLString(sb, url); 432 String matchesUrl = sb.toString(); 433 deleteHistoryWhere(cr, matchesUrl); 434 } 435 436 /** 437 * Add a search string to the searches database. 438 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} 439 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS} 440 * @param cr The ContentResolver used to access the database. 441 * @param search The string to add to the searches database. 442 */ 443 public static final void addSearchUrl(ContentResolver cr, String search) { 444 long now = new Date().getTime(); 445 try { 446 Cursor c = cr.query( 447 SEARCHES_URI, 448 SEARCHES_PROJECTION, 449 SEARCHES_WHERE_CLAUSE, 450 new String [] { search }, 451 null); 452 ContentValues map = new ContentValues(); 453 map.put(SearchColumns.SEARCH, search); 454 map.put(SearchColumns.DATE, now); 455 /* We should only get one answer that is exactly the same. */ 456 if (c.moveToFirst()) { 457 cr.update(SEARCHES_URI, map, "_id = " + c.getInt(0), null); 458 } else { 459 cr.insert(SEARCHES_URI, map); 460 } 461 c.deactivate(); 462 } catch (IllegalStateException e) { 463 Log.e(LOGTAG, "addSearchUrl", e); 464 return; 465 } 466 } 467 /** 468 * Remove all searches from the search database. 469 * Requires {@link android.Manifest.permission#WRITE_HISTORY_BOOKMARKS} 470 * @param cr The ContentResolver used to access the database. 471 */ 472 public static final void clearSearches(ContentResolver cr) { 473 // FIXME: Should this clear the urls to which these searches lead? 474 // (i.e. remove google.com/query= blah blah blah) 475 try { 476 cr.delete(SEARCHES_URI, null, null); 477 } catch (IllegalStateException e) { 478 Log.e(LOGTAG, "clearSearches", e); 479 } 480 } 481 482 /** 483 * Request all icons from the database. 484 * Requires {@link android.Manifest.permission#READ_HISTORY_BOOKMARKS} 485 * @param cr The ContentResolver used to access the database. 486 * @param where Clause to be used to limit the query from the database. 487 * Must be an allowable string to be passed into a database query. 488 * @param listener IconListener that gets the icons once they are 489 * retrieved. 490 */ 491 public static final void requestAllIcons(ContentResolver cr, String where, 492 WebIconDatabase.IconListener listener) { 493 try { 494 final Cursor c = cr.query( 495 BOOKMARKS_URI, 496 HISTORY_PROJECTION, 497 where, null, null); 498 if (c.moveToFirst()) { 499 final WebIconDatabase db = WebIconDatabase.getInstance(); 500 do { 501 db.requestIconForPageUrl( 502 c.getString(HISTORY_PROJECTION_URL_INDEX), 503 listener); 504 } while (c.moveToNext()); 505 } 506 c.deactivate(); 507 } catch (IllegalStateException e) { 508 Log.e(LOGTAG, "requestAllIcons", e); 509 } 510 } 511 512 public static class BookmarkColumns implements BaseColumns { 513 public static final String URL = "url"; 514 public static final String VISITS = "visits"; 515 public static final String DATE = "date"; 516 public static final String BOOKMARK = "bookmark"; 517 public static final String TITLE = "title"; 518 public static final String CREATED = "created"; 519 public static final String FAVICON = "favicon"; 520 /** 521 * @hide 522 */ 523 public static final String THUMBNAIL = "thumbnail"; 524 } 525 526 public static class SearchColumns implements BaseColumns { 527 public static final String URL = "url"; 528 public static final String SEARCH = "search"; 529 public static final String DATE = "date"; 530 } 531} 532