blob: ce156a7ced249e4968d4a14868431de01fcb3675 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -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 com.android.browser;
18
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070019import com.google.android.providers.GoogleSettings.Partner;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070020import java.util.Date;
21
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.app.ISearchManager;
23import android.app.SearchManager;
24import android.content.ComponentName;
25import android.content.ContentProvider;
26import android.content.ContentUris;
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070027import android.content.ContentResolver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.content.ContentValues;
29import android.content.Context;
30import android.content.Intent;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070031import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.content.UriMatcher;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070033import android.content.SharedPreferences.Editor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.database.AbstractCursor;
35import android.database.Cursor;
36import android.database.sqlite.SQLiteOpenHelper;
37import android.database.sqlite.SQLiteDatabase;
38import android.net.Uri;
39import android.os.RemoteException;
40import android.os.ServiceManager;
41import android.os.SystemProperties;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070042import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.provider.Browser;
44import android.util.Log;
45import android.server.search.SearchableInfo;
46import android.text.util.Regex;
47
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070048
The Android Open Source Project0c908882009-03-03 19:32:16 -080049public class BrowserProvider extends ContentProvider {
50
51 private SQLiteOpenHelper mOpenHelper;
52 private static final String sDatabaseName = "browser.db";
53 private static final String TAG = "BrowserProvider";
54 private static final String ORDER_BY = "visits DESC, date DESC";
55
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070056 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
57 "viewer?source=androidclient";
58
The Android Open Source Project0c908882009-03-03 19:32:16 -080059 private static final String[] TABLE_NAMES = new String[] {
60 "bookmarks", "searches"
61 };
62 private static final String[] SUGGEST_PROJECTION = new String[] {
63 "_id", "url", "title", "bookmark"
64 };
65 private static final String SUGGEST_SELECTION =
66 "url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?";
67 private String[] SUGGEST_ARGS = new String[4];
68
69 // shared suggestion array index, make sure to match COLUMNS
70 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
71 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
72 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
73 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
74 private static final int SUGGEST_COLUMN_ICON_1_ID = 5;
75 private static final int SUGGEST_COLUMN_ICON_2_ID = 6;
76 private static final int SUGGEST_COLUMN_QUERY_ID = 7;
77
78 // shared suggestion columns
79 private static final String[] COLUMNS = new String[] {
80 "_id",
81 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
82 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
83 SearchManager.SUGGEST_COLUMN_TEXT_1,
84 SearchManager.SUGGEST_COLUMN_TEXT_2,
85 SearchManager.SUGGEST_COLUMN_ICON_1,
86 SearchManager.SUGGEST_COLUMN_ICON_2,
87 SearchManager.SUGGEST_COLUMN_QUERY};
88
89 private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3;
90 private static final int MAX_SUGGESTION_LONG_ENTRIES = 6;
91
92 // make sure that these match the index of TABLE_NAMES
93 private static final int URI_MATCH_BOOKMARKS = 0;
94 private static final int URI_MATCH_SEARCHES = 1;
95 // (id % 10) should match the table name index
96 private static final int URI_MATCH_BOOKMARKS_ID = 10;
97 private static final int URI_MATCH_SEARCHES_ID = 11;
98 //
99 private static final int URI_MATCH_SUGGEST = 20;
100
101 private static final UriMatcher URI_MATCHER;
102
103 static {
104 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
105 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
106 URI_MATCH_BOOKMARKS);
107 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
108 URI_MATCH_BOOKMARKS_ID);
109 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
110 URI_MATCH_SEARCHES);
111 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
112 URI_MATCH_SEARCHES_ID);
113 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
114 URI_MATCH_SUGGEST);
115 }
116
117 // 1 -> 2 add cache table
118 // 2 -> 3 update history table
119 // 3 -> 4 add passwords table
120 // 4 -> 5 add settings table
121 // 5 -> 6 ?
122 // 6 -> 7 ?
123 // 7 -> 8 drop proxy table
124 // 8 -> 9 drop settings table
125 // 9 -> 10 add form_urls and form_data
126 // 10 -> 11 add searches table
127 // 11 -> 12 modify cache table
128 // 12 -> 13 modify cache table
129 // 13 -> 14 correspond with Google Bookmarks schema
130 // 14 -> 15 move couple of tables to either browser private database or webview database
131 // 15 -> 17 Set it up for the SearchManager
132 // 17 -> 18 Added favicon in bookmarks table for Home shortcuts
133 // 18 -> 19 Remove labels table
134 private static final int DATABASE_VERSION = 19;
135
136 public BrowserProvider() {
137 }
138
139
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700140 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800141 StringBuffer sb = new StringBuffer();
142 int lastCharLoc = 0;
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700143
144 final String client_id = Partner.getString(context.getContentResolver(), Partner.CLIENT_ID);
145
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 for (int i = 0; i < srcString.length(); ++i) {
147 char c = srcString.charAt(i);
148 if (c == '{') {
149 sb.append(srcString.subSequence(lastCharLoc, i));
150 lastCharLoc = i;
151 inner:
152 for (int j = i; j < srcString.length(); ++j) {
153 char k = srcString.charAt(j);
154 if (k == '}') {
155 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700156 if (propertyKeyValue.equals("CLIENT_ID")) {
157 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158 } else {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700159 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800160 }
161 lastCharLoc = j + 1;
162 i = j;
163 break inner;
164 }
165 }
166 }
167 }
168 if (srcString.length() - lastCharLoc > 0) {
169 // Put on the tail, if there is one
170 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
171 }
172 return sb;
173 }
174
175 private static class DatabaseHelper extends SQLiteOpenHelper {
176 private Context mContext;
177
178 public DatabaseHelper(Context context) {
179 super(context, sDatabaseName, null, DATABASE_VERSION);
180 mContext = context;
181 }
182
183 @Override
184 public void onCreate(SQLiteDatabase db) {
185 db.execSQL("CREATE TABLE bookmarks (" +
186 "_id INTEGER PRIMARY KEY," +
187 "title TEXT," +
188 "url TEXT," +
189 "visits INTEGER," +
190 "date LONG," +
191 "created LONG," +
192 "description TEXT," +
193 "bookmark INTEGER," +
194 "favicon BLOB DEFAULT NULL" +
195 ");");
196
197 final CharSequence[] bookmarks = mContext.getResources()
198 .getTextArray(R.array.bookmarks);
199 int size = bookmarks.length;
200 try {
201 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700202 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800203 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
204 "date, created, bookmark)" + " VALUES('" +
205 bookmarks[i] + "', '" + bookmarkDestination +
206 "', 0, 0, 0, 1);");
207 }
208 } catch (ArrayIndexOutOfBoundsException e) {
209 }
210
211 db.execSQL("CREATE TABLE searches (" +
212 "_id INTEGER PRIMARY KEY," +
213 "search TEXT," +
214 "date LONG" +
215 ");");
216 }
217
218 @Override
219 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
220 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
221 + newVersion + ", which will destroy all old data");
222 if (oldVersion == 18) {
223 db.execSQL("DROP TABLE IF EXISTS labels");
224 } else {
225 db.execSQL("DROP TABLE IF EXISTS bookmarks");
226 db.execSQL("DROP TABLE IF EXISTS searches");
227 onCreate(db);
228 }
229 }
230 }
231
232 @Override
233 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700234 final Context context = getContext();
235 mOpenHelper = new DatabaseHelper(context);
236 // we added "picasa web album" into default bookmarks for version 19.
237 // To avoid erasing the bookmark table, we added it explicitly for
238 // version 18 and 19 as in the other cases, we will erase the table.
239 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
240 SharedPreferences p = PreferenceManager
241 .getDefaultSharedPreferences(context);
242 boolean fix = p.getBoolean("fix_picasa", true);
243 if (fix) {
244 fixPicasaBookmark();
245 Editor ed = p.edit();
246 ed.putBoolean("fix_picasa", false);
247 ed.commit();
248 }
249 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800250 return true;
251 }
252
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700253 private void fixPicasaBookmark() {
254 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
255 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
256 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
257 try {
258 if (!cursor.moveToFirst()) {
259 // set "created" so that it will be on the top of the list
260 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
261 "date, created, bookmark)" + " VALUES('" +
262 getContext().getString(R.string.picasa) + "', '"
263 + PICASA_URL + "', 0, 0, " + new Date().getTime()
264 + ", 1);");
265 }
266 } finally {
267 if (cursor != null) {
268 cursor.close();
269 }
270 }
271 }
272
The Android Open Source Project0c908882009-03-03 19:32:16 -0800273 /*
274 * Subclass AbstractCursor so we can combine multiple Cursors and add
275 * "Google Search".
276 * Here are the rules.
277 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
278 * "Google Search";
279 * 2. If bookmark/history entries are less than
280 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
281 */
282 private class MySuggestionCursor extends AbstractCursor {
283 private Cursor mHistoryCursor;
284 private Cursor mSuggestCursor;
285 private int mHistoryCount;
286 private int mSuggestionCount;
287 private boolean mBeyondCursor;
288 private String mString;
289
290 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
291 mHistoryCursor = hc;
292 mSuggestCursor = sc;
293 mHistoryCount = hc.getCount();
294 mSuggestionCount = sc != null ? sc.getCount() : 0;
295 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
296 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
297 }
298 mString = string;
299 mBeyondCursor = false;
300 }
301
302 @Override
303 public boolean onMove(int oldPosition, int newPosition) {
304 if (mHistoryCursor == null) {
305 return false;
306 }
307 if (mHistoryCount > newPosition) {
308 mHistoryCursor.moveToPosition(newPosition);
309 mBeyondCursor = false;
310 } else if (mHistoryCount + mSuggestionCount > newPosition) {
311 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
312 mBeyondCursor = false;
313 } else {
314 mBeyondCursor = true;
315 }
316 return true;
317 }
318
319 @Override
320 public int getCount() {
321 if (mString.length() > 0) {
322 return mHistoryCount + mSuggestionCount + 1;
323 } else {
324 return mHistoryCount + mSuggestionCount;
325 }
326 }
327
328 @Override
329 public String[] getColumnNames() {
330 return COLUMNS;
331 }
332
333 @Override
334 public String getString(int columnIndex) {
335 if ((mPos != -1 && mHistoryCursor != null)) {
336 switch(columnIndex) {
337 case SUGGEST_COLUMN_INTENT_ACTION_ID:
338 if (mHistoryCount > mPos) {
339 return Intent.ACTION_VIEW;
340 } else {
341 return Intent.ACTION_SEARCH;
342 }
343
344 case SUGGEST_COLUMN_INTENT_DATA_ID:
345 if (mHistoryCount > mPos) {
346 return mHistoryCursor.getString(1);
347 } else {
348 return null;
349 }
350
351 case SUGGEST_COLUMN_TEXT_1_ID:
352 if (mHistoryCount > mPos) {
353 return mHistoryCursor.getString(1);
354 } else if (!mBeyondCursor) {
355 return mSuggestCursor.getString(1);
356 } else {
357 return mString;
358 }
359
360 case SUGGEST_COLUMN_TEXT_2_ID:
361 if (mHistoryCount > mPos) {
362 return mHistoryCursor.getString(2);
363 } else if (!mBeyondCursor) {
364 return mSuggestCursor.getString(2);
365 } else {
366 return getContext().getString(R.string.search_google);
367 }
368
369 case SUGGEST_COLUMN_ICON_1_ID:
370 if (mHistoryCount > mPos) {
371 if (mHistoryCursor.getInt(3) == 1) {
372 return new Integer(
373 R.drawable.ic_search_category_bookmark)
374 .toString();
375 } else {
376 return new Integer(
377 R.drawable.ic_search_category_history)
378 .toString();
379 }
380 } else {
381 return new Integer(
382 R.drawable.ic_search_category_suggest)
383 .toString();
384 }
385
386 case SUGGEST_COLUMN_ICON_2_ID:
387 return new String("0");
388
389 case SUGGEST_COLUMN_QUERY_ID:
390 if (mHistoryCount > mPos) {
391 return null;
392 } else if (!mBeyondCursor) {
393 return mSuggestCursor.getString(3);
394 } else {
395 return mString;
396 }
397 }
398 }
399 return null;
400 }
401
402 @Override
403 public double getDouble(int column) {
404 throw new UnsupportedOperationException();
405 }
406
407 @Override
408 public float getFloat(int column) {
409 throw new UnsupportedOperationException();
410 }
411
412 @Override
413 public int getInt(int column) {
414 throw new UnsupportedOperationException();
415 }
416
417 @Override
418 public long getLong(int column) {
419 if ((mPos != -1) && column == 0) {
420 return mPos; // use row# as the _Id
421 }
422 throw new UnsupportedOperationException();
423 }
424
425 @Override
426 public short getShort(int column) {
427 throw new UnsupportedOperationException();
428 }
429
430 @Override
431 public boolean isNull(int column) {
432 throw new UnsupportedOperationException();
433 }
434
435 // TODO Temporary change, finalize after jq's changes go in
436 public void deactivate() {
437 if (mHistoryCursor != null) {
438 mHistoryCursor.deactivate();
439 }
440 if (mSuggestCursor != null) {
441 mSuggestCursor.deactivate();
442 }
443 super.deactivate();
444 }
445
446 public boolean requery() {
447 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
448 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
449 }
450
451 // TODO Temporary change, finalize after jq's changes go in
452 public void close() {
453 super.close();
454 if (mHistoryCursor != null) {
455 mHistoryCursor.close();
456 mHistoryCursor = null;
457 }
458 if (mSuggestCursor != null) {
459 mSuggestCursor.close();
460 mSuggestCursor = null;
461 }
462 }
463 }
464
465 @Override
466 public Cursor query(Uri url, String[] projectionIn, String selection,
467 String[] selectionArgs, String sortOrder)
468 throws IllegalStateException {
469 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
470
471 int match = URI_MATCHER.match(url);
472 if (match == -1) {
473 throw new IllegalArgumentException("Unknown URL");
474 }
475
476 if (match == URI_MATCH_SUGGEST) {
477 String suggestSelection;
478 String [] myArgs;
479 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
480 suggestSelection = null;
481 myArgs = null;
482 } else {
483 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700484 if (selectionArgs[0].startsWith("http")
485 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800486 myArgs = new String[1];
487 myArgs[0] = like;
488 suggestSelection = selection;
489 } else {
490 SUGGEST_ARGS[0] = "http://" + like;
491 SUGGEST_ARGS[1] = "http://www." + like;
492 SUGGEST_ARGS[2] = "https://" + like;
493 SUGGEST_ARGS[3] = "https://www." + like;
494 myArgs = SUGGEST_ARGS;
495 suggestSelection = SUGGEST_SELECTION;
496 }
497 }
498
499 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
500 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
501 ORDER_BY,
502 (new Integer(MAX_SUGGESTION_LONG_ENTRIES)).toString());
503
504 if (Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
505 return new MySuggestionCursor(c, null, "");
506 } else {
507 // get Google suggest if there is still space in the list
508 if (myArgs != null && myArgs.length > 1
509 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
510 ISearchManager sm = ISearchManager.Stub
511 .asInterface(ServiceManager
512 .getService(Context.SEARCH_SERVICE));
513 SearchableInfo si = null;
514 try {
515 // use the global search to get Google suggest provider
516 si = sm.getSearchableInfo(new ComponentName(
517 getContext(), "com.android.browser"), true);
518
519 // similar to the getSuggestions() in SearchDialog.java
520 StringBuilder uriStr = new StringBuilder("content://");
521 uriStr.append(si.getSuggestAuthority());
522 // if content path provided, insert it now
523 final String contentPath = si.getSuggestPath();
524 if (contentPath != null) {
525 uriStr.append('/');
526 uriStr.append(contentPath);
527 }
528 // append standard suggestion query path
529 uriStr.append('/' + SearchManager.SUGGEST_URI_PATH_QUERY);
530 // inject query, either as selection args or inline
531 String[] selArgs = null;
532 if (si.getSuggestSelection() != null) {
533 selArgs = new String[] {selectionArgs[0]};
534 } else {
535 uriStr.append('/');
536 uriStr.append(Uri.encode(selectionArgs[0]));
537 }
538
539 // finally, make the query
540 Cursor sc = getContext().getContentResolver().query(
541 Uri.parse(uriStr.toString()), null,
542 si.getSuggestSelection(), selArgs, null);
543
544 return new MySuggestionCursor(c, sc, selectionArgs[0]);
545 } catch (RemoteException e) {
546 }
547 }
548 return new MySuggestionCursor(c, null, selectionArgs[0]);
549 }
550 }
551
552 String[] projection = null;
553 if (projectionIn != null && projectionIn.length > 0) {
554 projection = new String[projectionIn.length + 1];
555 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
556 projection[projectionIn.length] = "_id AS _id";
557 }
558
559 StringBuilder whereClause = new StringBuilder(256);
560 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
561 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
562 .append(")");
563 }
564
565 // Tack on the user's selection, if present
566 if (selection != null && selection.length() > 0) {
567 if (whereClause.length() > 0) {
568 whereClause.append(" AND ");
569 }
570
571 whereClause.append('(');
572 whereClause.append(selection);
573 whereClause.append(')');
574 }
575 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
576 whereClause.toString(), selectionArgs, null, null, sortOrder,
577 null);
578 c.setNotificationUri(getContext().getContentResolver(), url);
579 return c;
580 }
581
582 @Override
583 public String getType(Uri url) {
584 int match = URI_MATCHER.match(url);
585 switch (match) {
586 case URI_MATCH_BOOKMARKS:
587 return "vnd.android.cursor.dir/bookmark";
588
589 case URI_MATCH_BOOKMARKS_ID:
590 return "vnd.android.cursor.item/bookmark";
591
592 case URI_MATCH_SEARCHES:
593 return "vnd.android.cursor.dir/searches";
594
595 case URI_MATCH_SEARCHES_ID:
596 return "vnd.android.cursor.item/searches";
597
598 case URI_MATCH_SUGGEST:
599 return SearchManager.SUGGEST_MIME_TYPE;
600
601 default:
602 throw new IllegalArgumentException("Unknown URL");
603 }
604 }
605
606 @Override
607 public Uri insert(Uri url, ContentValues initialValues) {
608 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
609
610 int match = URI_MATCHER.match(url);
611 Uri uri = null;
612 switch (match) {
613 case URI_MATCH_BOOKMARKS: {
614 // Insert into the bookmarks table
615 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
616 initialValues);
617 if (rowID > 0) {
618 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
619 rowID);
620 }
621 break;
622 }
623
624 case URI_MATCH_SEARCHES: {
625 // Insert into the searches table
626 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
627 initialValues);
628 if (rowID > 0) {
629 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
630 rowID);
631 }
632 break;
633 }
634
635 default:
636 throw new IllegalArgumentException("Unknown URL");
637 }
638
639 if (uri == null) {
640 throw new IllegalArgumentException("Unknown URL");
641 }
642 getContext().getContentResolver().notifyChange(uri, null);
643 return uri;
644 }
645
646 @Override
647 public int delete(Uri url, String where, String[] whereArgs) {
648 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
649
650 int match = URI_MATCHER.match(url);
651 if (match == -1 || match == URI_MATCH_SUGGEST) {
652 throw new IllegalArgumentException("Unknown URL");
653 }
654
655 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
656 StringBuilder sb = new StringBuilder();
657 if (where != null && where.length() > 0) {
658 sb.append("( ");
659 sb.append(where);
660 sb.append(" ) AND ");
661 }
662 sb.append("_id = ");
663 sb.append(url.getPathSegments().get(1));
664 where = sb.toString();
665 }
666
667 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
668 getContext().getContentResolver().notifyChange(url, null);
669 return count;
670 }
671
672 @Override
673 public int update(Uri url, ContentValues values, String where,
674 String[] whereArgs) {
675 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
676
677 int match = URI_MATCHER.match(url);
678 if (match == -1 || match == URI_MATCH_SUGGEST) {
679 throw new IllegalArgumentException("Unknown URL");
680 }
681
682 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
683 StringBuilder sb = new StringBuilder();
684 if (where != null && where.length() > 0) {
685 sb.append("( ");
686 sb.append(where);
687 sb.append(" ) AND ");
688 }
689 sb.append("_id = ");
690 sb.append(url.getPathSegments().get(1));
691 where = sb.toString();
692 }
693
694 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
695 getContext().getContentResolver().notifyChange(url, null);
696 return ret;
697 }
698}