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