2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.browser;
19 import com.google.android.providers.GoogleSettings.Partner;
21 import android.app.SearchManager;
22 import android.content.ComponentName;
23 import android.content.ContentProvider;
24 import android.content.ContentUris;
25 import android.content.ContentValues;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.SharedPreferences;
29 import android.content.UriMatcher;
30 import android.content.SharedPreferences.Editor;
31 import android.content.pm.PackageManager;
32 import android.content.pm.ResolveInfo;
33 import android.database.AbstractCursor;
34 import android.database.ContentObserver;
35 import android.database.Cursor;
36 import android.database.sqlite.SQLiteDatabase;
37 import android.database.sqlite.SQLiteOpenHelper;
38 import android.net.Uri;
39 import android.os.Handler;
40 import android.preference.PreferenceManager;
41 import android.provider.Browser;
42 import android.provider.Settings;
43 import android.server.search.SearchableInfo;
44 import android.text.TextUtils;
45 import android.text.util.Regex;
46 import android.util.Log;
47 import android.util.TypedValue;
49 import java.util.Date;
50 import java.util.regex.Matcher;
51 import java.util.regex.Pattern;
54 public class BrowserProvider extends ContentProvider {
56 private SQLiteOpenHelper mOpenHelper;
57 private static final String sDatabaseName = "browser.db";
58 private static final String TAG = "BrowserProvider";
59 private static final String ORDER_BY = "visits DESC, date DESC";
61 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
62 "viewer?source=androidclient";
64 private static final String[] TABLE_NAMES = new String[] {
65 "bookmarks", "searches"
67 private static final String[] SUGGEST_PROJECTION = new String[] {
68 "_id", "url", "title", "bookmark"
70 private static final String SUGGEST_SELECTION =
71 "url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?"
73 private String[] SUGGEST_ARGS = new String[5];
75 // shared suggestion array index, make sure to match COLUMNS
76 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
77 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
78 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
79 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
80 private static final int SUGGEST_COLUMN_ICON_1_ID = 5;
81 private static final int SUGGEST_COLUMN_ICON_2_ID = 6;
82 private static final int SUGGEST_COLUMN_QUERY_ID = 7;
83 private static final int SUGGEST_COLUMN_FORMAT = 8;
85 // shared suggestion columns
86 private static final String[] COLUMNS = new String[] {
88 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
89 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
90 SearchManager.SUGGEST_COLUMN_TEXT_1,
91 SearchManager.SUGGEST_COLUMN_TEXT_2,
92 SearchManager.SUGGEST_COLUMN_ICON_1,
93 SearchManager.SUGGEST_COLUMN_ICON_2,
94 SearchManager.SUGGEST_COLUMN_QUERY,
95 SearchManager.SUGGEST_COLUMN_FORMAT};
97 private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3;
98 private static final int MAX_SUGGESTION_LONG_ENTRIES = 6;
99 private static final String MAX_SUGGESTION_LONG_ENTRIES_STRING =
100 Integer.valueOf(MAX_SUGGESTION_LONG_ENTRIES).toString();
102 // make sure that these match the index of TABLE_NAMES
103 private static final int URI_MATCH_BOOKMARKS = 0;
104 private static final int URI_MATCH_SEARCHES = 1;
105 // (id % 10) should match the table name index
106 private static final int URI_MATCH_BOOKMARKS_ID = 10;
107 private static final int URI_MATCH_SEARCHES_ID = 11;
109 private static final int URI_MATCH_SUGGEST = 20;
110 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
112 private static final UriMatcher URI_MATCHER;
115 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
116 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
117 URI_MATCH_BOOKMARKS);
118 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
119 URI_MATCH_BOOKMARKS_ID);
120 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
122 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
123 URI_MATCH_SEARCHES_ID);
124 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
126 URI_MATCHER.addURI("browser",
127 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
128 URI_MATCH_BOOKMARKS_SUGGEST);
131 // 1 -> 2 add cache table
132 // 2 -> 3 update history table
133 // 3 -> 4 add passwords table
134 // 4 -> 5 add settings table
137 // 7 -> 8 drop proxy table
138 // 8 -> 9 drop settings table
139 // 9 -> 10 add form_urls and form_data
140 // 10 -> 11 add searches table
141 // 11 -> 12 modify cache table
142 // 12 -> 13 modify cache table
143 // 13 -> 14 correspond with Google Bookmarks schema
144 // 14 -> 15 move couple of tables to either browser private database or webview database
145 // 15 -> 17 Set it up for the SearchManager
146 // 17 -> 18 Added favicon in bookmarks table for Home shortcuts
147 // 18 -> 19 Remove labels table
148 // 19 -> 20 Added thumbnail
149 private static final int DATABASE_VERSION = 20;
151 // Regular expression which matches http://, followed by some stuff, followed by
152 // optionally a trailing slash, all matched as separate groups.
153 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
155 private SearchManager mSearchManager;
157 // The ID of the ColorStateList to be applied to urls of website suggestions, as derived from
158 // the current theme. This is not set until/unless beautifyUrl is called, at which point
159 // this variable caches the color value.
160 private static String mSearchUrlColorId;
162 public BrowserProvider() {
166 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
167 StringBuffer sb = new StringBuffer();
170 final String client_id = Partner.getString(context.getContentResolver(),
171 Partner.CLIENT_ID, "android-google");
173 for (int i = 0; i < srcString.length(); ++i) {
174 char c = srcString.charAt(i);
176 sb.append(srcString.subSequence(lastCharLoc, i));
179 for (int j = i; j < srcString.length(); ++j) {
180 char k = srcString.charAt(j);
182 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
183 if (propertyKeyValue.equals("CLIENT_ID")) {
184 sb.append(client_id);
186 sb.append("unknown");
195 if (srcString.length() - lastCharLoc > 0) {
196 // Put on the tail, if there is one
197 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
202 private static class DatabaseHelper extends SQLiteOpenHelper {
203 private Context mContext;
205 public DatabaseHelper(Context context) {
206 super(context, sDatabaseName, null, DATABASE_VERSION);
211 public void onCreate(SQLiteDatabase db) {
212 db.execSQL("CREATE TABLE bookmarks (" +
213 "_id INTEGER PRIMARY KEY," +
219 "description TEXT," +
220 "bookmark INTEGER," +
221 "favicon BLOB DEFAULT NULL," +
222 "thumbnail BLOB DEFAULT NULL" +
225 final CharSequence[] bookmarks = mContext.getResources()
226 .getTextArray(R.array.bookmarks);
227 int size = bookmarks.length;
229 for (int i = 0; i < size; i = i + 2) {
230 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
231 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
232 "date, created, bookmark)" + " VALUES('" +
233 bookmarks[i] + "', '" + bookmarkDestination +
236 } catch (ArrayIndexOutOfBoundsException e) {
239 db.execSQL("CREATE TABLE searches (" +
240 "_id INTEGER PRIMARY KEY," +
247 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
248 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
250 if (oldVersion == 18) {
251 db.execSQL("DROP TABLE IF EXISTS labels");
253 if (oldVersion <= 19) {
254 db.execSQL("ALTER TABLE bookmarks ADD COLUMN thumbnail BLOB DEFAULT NULL;");
256 db.execSQL("DROP TABLE IF EXISTS bookmarks");
257 db.execSQL("DROP TABLE IF EXISTS searches");
264 public boolean onCreate() {
265 final Context context = getContext();
266 mOpenHelper = new DatabaseHelper(context);
267 // we added "picasa web album" into default bookmarks for version 19.
268 // To avoid erasing the bookmark table, we added it explicitly for
269 // version 18 and 19 as in the other cases, we will erase the table.
270 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
271 SharedPreferences p = PreferenceManager
272 .getDefaultSharedPreferences(context);
273 boolean fix = p.getBoolean("fix_picasa", true);
276 Editor ed = p.edit();
277 ed.putBoolean("fix_picasa", false);
281 mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
282 mShowWebSuggestionsSettingChangeObserver
283 = new ShowWebSuggestionsSettingChangeObserver();
284 context.getContentResolver().registerContentObserver(
285 Settings.System.getUriFor(
286 Settings.System.SHOW_WEB_SUGGESTIONS),
287 true, mShowWebSuggestionsSettingChangeObserver);
288 updateShowWebSuggestions();
293 * This Observer will ensure that if the user changes the system
294 * setting of whether to display web suggestions, we will
295 * change accordingly.
297 /* package */ class ShowWebSuggestionsSettingChangeObserver
298 extends ContentObserver {
299 public ShowWebSuggestionsSettingChangeObserver() {
300 super(new Handler());
304 public void onChange(boolean selfChange) {
305 updateShowWebSuggestions();
309 private ShowWebSuggestionsSettingChangeObserver
310 mShowWebSuggestionsSettingChangeObserver;
312 // If non-null, then the system is set to show web suggestions,
313 // and this is the SearchableInfo to use to get them.
314 private SearchableInfo mSearchableInfo;
317 * Check the system settings to see whether web suggestions are
318 * allowed. If so, store the SearchableInfo to grab suggestions
319 * while the user is typing.
321 private void updateShowWebSuggestions() {
322 mSearchableInfo = null;
323 Context context = getContext();
324 if (Settings.System.getInt(context.getContentResolver(),
325 Settings.System.SHOW_WEB_SUGGESTIONS,
326 1 /* default on */) == 1) {
327 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
328 intent.addCategory(Intent.CATEGORY_DEFAULT);
329 ResolveInfo info = context.getPackageManager().resolveActivity(
330 intent, PackageManager.MATCH_DEFAULT_ONLY);
332 ComponentName googleSearchComponent =
333 new ComponentName(info.activityInfo.packageName,
334 info.activityInfo.name);
335 mSearchableInfo = mSearchManager.getSearchableInfo(
336 googleSearchComponent, false);
341 private void fixPicasaBookmark() {
342 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
343 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
344 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
346 if (!cursor.moveToFirst()) {
347 // set "created" so that it will be on the top of the list
348 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
349 "date, created, bookmark)" + " VALUES('" +
350 getContext().getString(R.string.picasa) + "', '"
351 + PICASA_URL + "', 0, 0, " + new Date().getTime()
355 if (cursor != null) {
362 * Subclass AbstractCursor so we can combine multiple Cursors and add
364 * Here are the rules.
365 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
367 * 2. If bookmark/history entries are less than
368 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
370 private class MySuggestionCursor extends AbstractCursor {
371 private Cursor mHistoryCursor;
372 private Cursor mSuggestCursor;
373 private int mHistoryCount;
374 private int mSuggestionCount;
375 private boolean mBeyondCursor;
376 private String mString;
377 private int mSuggestText1Id;
378 private int mSuggestText2Id;
379 private int mSuggestQueryId;
381 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
384 mHistoryCount = hc.getCount();
385 mSuggestionCount = sc != null ? sc.getCount() : 0;
386 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
387 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
390 mBeyondCursor = false;
392 // Some web suggest providers only give suggestions and have no description string for
393 // items. The order of the result columns may be different as well. So retrieve the
394 // column indices for the fields we need now and check before using below.
395 if (mSuggestCursor == null) {
396 mSuggestText1Id = -1;
397 mSuggestText2Id = -1;
398 mSuggestQueryId = -1;
400 mSuggestText1Id = mSuggestCursor.getColumnIndex(
401 SearchManager.SUGGEST_COLUMN_TEXT_1);
402 mSuggestText2Id = mSuggestCursor.getColumnIndex(
403 SearchManager.SUGGEST_COLUMN_TEXT_2);
404 mSuggestQueryId = mSuggestCursor.getColumnIndex(
405 SearchManager.SUGGEST_COLUMN_QUERY);
410 public boolean onMove(int oldPosition, int newPosition) {
411 if (mHistoryCursor == null) {
414 if (mHistoryCount > newPosition) {
415 mHistoryCursor.moveToPosition(newPosition);
416 mBeyondCursor = false;
417 } else if (mHistoryCount + mSuggestionCount > newPosition) {
418 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
419 mBeyondCursor = false;
421 mBeyondCursor = true;
427 public int getCount() {
428 if (mString.length() > 0) {
429 return mHistoryCount + mSuggestionCount + 1;
431 return mHistoryCount + mSuggestionCount;
436 public String[] getColumnNames() {
441 public String getString(int columnIndex) {
442 if ((mPos != -1 && mHistoryCursor != null)) {
443 switch(columnIndex) {
444 case SUGGEST_COLUMN_INTENT_ACTION_ID:
445 if (mHistoryCount > mPos) {
446 return Intent.ACTION_VIEW;
448 return Intent.ACTION_SEARCH;
451 case SUGGEST_COLUMN_INTENT_DATA_ID:
452 if (mHistoryCount > mPos) {
453 return mHistoryCursor.getString(1);
458 case SUGGEST_COLUMN_TEXT_1_ID:
459 if (mHistoryCount > mPos) {
460 return getHistoryTitle();
461 } else if (!mBeyondCursor) {
462 if (mSuggestText1Id == -1) return null;
463 return mSuggestCursor.getString(mSuggestText1Id);
468 case SUGGEST_COLUMN_TEXT_2_ID:
469 if (mHistoryCount > mPos) {
470 return getHistorySubtitle();
471 } else if (!mBeyondCursor) {
472 if (mSuggestText2Id == -1) return null;
473 return mSuggestCursor.getString(mSuggestText2Id);
475 return getContext().getString(R.string.search_the_web);
478 case SUGGEST_COLUMN_ICON_1_ID:
479 if (mHistoryCount > mPos) {
480 if (mHistoryCursor.getInt(3) == 1) {
481 return Integer.valueOf(
482 R.drawable.ic_search_category_bookmark)
485 return Integer.valueOf(
486 R.drawable.ic_search_category_history)
490 return Integer.valueOf(
491 R.drawable.ic_search_category_suggest)
495 case SUGGEST_COLUMN_ICON_2_ID:
498 case SUGGEST_COLUMN_QUERY_ID:
499 if (mHistoryCount > mPos) {
500 // Return the url in the intent query column. This is ignored
501 // within the browser because our searchable is set to
502 // android:searchMode="queryRewriteFromData", but it is used by
503 // global search for query rewriting.
504 return mHistoryCursor.getString(1);
505 } else if (!mBeyondCursor) {
506 if (mSuggestQueryId == -1) return null;
507 return mSuggestCursor.getString(mSuggestQueryId);
512 case SUGGEST_COLUMN_FORMAT:
520 public double getDouble(int column) {
521 throw new UnsupportedOperationException();
525 public float getFloat(int column) {
526 throw new UnsupportedOperationException();
530 public int getInt(int column) {
531 throw new UnsupportedOperationException();
535 public long getLong(int column) {
536 if ((mPos != -1) && column == 0) {
537 return mPos; // use row# as the _Id
539 throw new UnsupportedOperationException();
543 public short getShort(int column) {
544 throw new UnsupportedOperationException();
548 public boolean isNull(int column) {
549 throw new UnsupportedOperationException();
552 // TODO Temporary change, finalize after jq's changes go in
553 public void deactivate() {
554 if (mHistoryCursor != null) {
555 mHistoryCursor.deactivate();
557 if (mSuggestCursor != null) {
558 mSuggestCursor.deactivate();
563 public boolean requery() {
564 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
565 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
568 // TODO Temporary change, finalize after jq's changes go in
569 public void close() {
571 if (mHistoryCursor != null) {
572 mHistoryCursor.close();
573 mHistoryCursor = null;
575 if (mSuggestCursor != null) {
576 mSuggestCursor.close();
577 mSuggestCursor = null;
582 * Provides the title (text line 1) for a browser suggestion, which should be the
583 * webpage title. If the webpage title is empty, returns the stripped url instead.
585 * @return the title string to use
587 private String getHistoryTitle() {
588 String title = mHistoryCursor.getString(2 /* webpage title */);
589 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
590 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
596 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
597 * webpage url. If the webpage title is empty, then the url should go in the title
598 * instead, and the subtitle should be empty, so this would return null.
600 * @return the subtitle string to use, or null if none
602 private String getHistorySubtitle() {
603 String title = mHistoryCursor.getString(2 /* webpage title */);
604 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
607 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
612 * Strips "http://" from the beginning of a url and "/" from the end,
613 * and adds html formatting to make it green.
615 private String beautifyUrl(String url) {
616 if (mSearchUrlColorId == null) {
617 // Get the color used for this purpose from the current theme.
618 TypedValue colorValue = new TypedValue();
619 getContext().getTheme().resolveAttribute(
620 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
621 mSearchUrlColorId = Integer.toString(colorValue.resourceId);
624 return "<font color=\"@" + mSearchUrlColorId + "\">" + stripUrl(url) + "</font>";
629 public Cursor query(Uri url, String[] projectionIn, String selection,
630 String[] selectionArgs, String sortOrder)
631 throws IllegalStateException {
632 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
634 int match = URI_MATCHER.match(url);
636 throw new IllegalArgumentException("Unknown URL");
639 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
640 String suggestSelection;
642 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
643 suggestSelection = null;
646 String like = selectionArgs[0] + "%";
647 if (selectionArgs[0].startsWith("http")
648 || selectionArgs[0].startsWith("file")) {
649 myArgs = new String[1];
651 suggestSelection = selection;
653 SUGGEST_ARGS[0] = "http://" + like;
654 SUGGEST_ARGS[1] = "http://www." + like;
655 SUGGEST_ARGS[2] = "https://" + like;
656 SUGGEST_ARGS[3] = "https://www." + like;
657 // To match against titles.
658 SUGGEST_ARGS[4] = like;
659 myArgs = SUGGEST_ARGS;
660 suggestSelection = SUGGEST_SELECTION;
664 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
665 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
666 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
668 if (match == URI_MATCH_BOOKMARKS_SUGGEST
669 || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
670 return new MySuggestionCursor(c, null, "");
672 // get Google suggest if there is still space in the list
673 if (myArgs != null && myArgs.length > 1
674 && mSearchableInfo != null
675 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
676 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
677 return new MySuggestionCursor(c, sc, selectionArgs[0]);
679 return new MySuggestionCursor(c, null, selectionArgs[0]);
683 String[] projection = null;
684 if (projectionIn != null && projectionIn.length > 0) {
685 projection = new String[projectionIn.length + 1];
686 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
687 projection[projectionIn.length] = "_id AS _id";
690 StringBuilder whereClause = new StringBuilder(256);
691 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
692 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
696 // Tack on the user's selection, if present
697 if (selection != null && selection.length() > 0) {
698 if (whereClause.length() > 0) {
699 whereClause.append(" AND ");
702 whereClause.append('(');
703 whereClause.append(selection);
704 whereClause.append(')');
706 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
707 whereClause.toString(), selectionArgs, null, null, sortOrder,
709 c.setNotificationUri(getContext().getContentResolver(), url);
714 public String getType(Uri url) {
715 int match = URI_MATCHER.match(url);
717 case URI_MATCH_BOOKMARKS:
718 return "vnd.android.cursor.dir/bookmark";
720 case URI_MATCH_BOOKMARKS_ID:
721 return "vnd.android.cursor.item/bookmark";
723 case URI_MATCH_SEARCHES:
724 return "vnd.android.cursor.dir/searches";
726 case URI_MATCH_SEARCHES_ID:
727 return "vnd.android.cursor.item/searches";
729 case URI_MATCH_SUGGEST:
730 return SearchManager.SUGGEST_MIME_TYPE;
733 throw new IllegalArgumentException("Unknown URL");
738 public Uri insert(Uri url, ContentValues initialValues) {
739 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
741 int match = URI_MATCHER.match(url);
744 case URI_MATCH_BOOKMARKS: {
745 // Insert into the bookmarks table
746 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
749 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
755 case URI_MATCH_SEARCHES: {
756 // Insert into the searches table
757 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
760 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
767 throw new IllegalArgumentException("Unknown URL");
771 throw new IllegalArgumentException("Unknown URL");
773 getContext().getContentResolver().notifyChange(uri, null);
778 public int delete(Uri url, String where, String[] whereArgs) {
779 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
781 int match = URI_MATCHER.match(url);
782 if (match == -1 || match == URI_MATCH_SUGGEST) {
783 throw new IllegalArgumentException("Unknown URL");
786 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
787 StringBuilder sb = new StringBuilder();
788 if (where != null && where.length() > 0) {
791 sb.append(" ) AND ");
794 sb.append(url.getPathSegments().get(1));
795 where = sb.toString();
798 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
799 getContext().getContentResolver().notifyChange(url, null);
804 public int update(Uri url, ContentValues values, String where,
805 String[] whereArgs) {
806 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
808 int match = URI_MATCHER.match(url);
809 if (match == -1 || match == URI_MATCH_SUGGEST) {
810 throw new IllegalArgumentException("Unknown URL");
813 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
814 StringBuilder sb = new StringBuilder();
815 if (where != null && where.length() > 0) {
818 sb.append(" ) AND ");
821 sb.append(url.getPathSegments().get(1));
822 where = sb.toString();
825 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
826 getContext().getContentResolver().notifyChange(url, null);
831 * Strips the provided url of preceding "http://" and any trailing "/". Does not
832 * strip "https://". If the provided string cannot be stripped, the original string
835 * TODO: Put this in TextUtils to be used by other packages doing something similar.
837 * @param url a url to strip, like "http://www.google.com/"
838 * @return a stripped url like "www.google.com", or the original string if it could
841 private static String stripUrl(String url) {
842 if (url == null) return null;
843 Matcher m = STRIP_URL_PATTERN.matcher(url);
844 if (m.matches() && m.groupCount() == 3) {