blob: 6633a363ddb83cb331a892b1ac12dba09fc2544c [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 Rajeswaranf447f262009-03-24 20:40:12 -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 Rajeswaranf447f262009-03-24 20:40:12 -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 Rajeswaranf447f262009-03-24 20:40:12 -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;
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100100 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800101
102 private static final UriMatcher URI_MATCHER;
103
104 static {
105 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
106 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
107 URI_MATCH_BOOKMARKS);
108 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
109 URI_MATCH_BOOKMARKS_ID);
110 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
111 URI_MATCH_SEARCHES);
112 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
113 URI_MATCH_SEARCHES_ID);
114 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
115 URI_MATCH_SUGGEST);
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100116 URI_MATCHER.addURI("browser",
117 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
118 URI_MATCH_BOOKMARKS_SUGGEST);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119 }
120
121 // 1 -> 2 add cache table
122 // 2 -> 3 update history table
123 // 3 -> 4 add passwords table
124 // 4 -> 5 add settings table
125 // 5 -> 6 ?
126 // 6 -> 7 ?
127 // 7 -> 8 drop proxy table
128 // 8 -> 9 drop settings table
129 // 9 -> 10 add form_urls and form_data
130 // 10 -> 11 add searches table
131 // 11 -> 12 modify cache table
132 // 12 -> 13 modify cache table
133 // 13 -> 14 correspond with Google Bookmarks schema
134 // 14 -> 15 move couple of tables to either browser private database or webview database
135 // 15 -> 17 Set it up for the SearchManager
136 // 17 -> 18 Added favicon in bookmarks table for Home shortcuts
137 // 18 -> 19 Remove labels table
138 private static final int DATABASE_VERSION = 19;
139
140 public BrowserProvider() {
141 }
142
143
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700144 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800145 StringBuffer sb = new StringBuffer();
146 int lastCharLoc = 0;
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700147
148 final String client_id = Partner.getString(context.getContentResolver(), Partner.CLIENT_ID);
149
The Android Open Source Project0c908882009-03-03 19:32:16 -0800150 for (int i = 0; i < srcString.length(); ++i) {
151 char c = srcString.charAt(i);
152 if (c == '{') {
153 sb.append(srcString.subSequence(lastCharLoc, i));
154 lastCharLoc = i;
155 inner:
156 for (int j = i; j < srcString.length(); ++j) {
157 char k = srcString.charAt(j);
158 if (k == '}') {
159 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700160 if (propertyKeyValue.equals("CLIENT_ID")) {
161 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800162 } else {
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700163 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164 }
165 lastCharLoc = j + 1;
166 i = j;
167 break inner;
168 }
169 }
170 }
171 }
172 if (srcString.length() - lastCharLoc > 0) {
173 // Put on the tail, if there is one
174 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
175 }
176 return sb;
177 }
178
179 private static class DatabaseHelper extends SQLiteOpenHelper {
180 private Context mContext;
181
182 public DatabaseHelper(Context context) {
183 super(context, sDatabaseName, null, DATABASE_VERSION);
184 mContext = context;
185 }
186
187 @Override
188 public void onCreate(SQLiteDatabase db) {
189 db.execSQL("CREATE TABLE bookmarks (" +
190 "_id INTEGER PRIMARY KEY," +
191 "title TEXT," +
192 "url TEXT," +
193 "visits INTEGER," +
194 "date LONG," +
195 "created LONG," +
196 "description TEXT," +
197 "bookmark INTEGER," +
198 "favicon BLOB DEFAULT NULL" +
199 ");");
200
201 final CharSequence[] bookmarks = mContext.getResources()
202 .getTextArray(R.array.bookmarks);
203 int size = bookmarks.length;
204 try {
205 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700206 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800207 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
208 "date, created, bookmark)" + " VALUES('" +
209 bookmarks[i] + "', '" + bookmarkDestination +
210 "', 0, 0, 0, 1);");
211 }
212 } catch (ArrayIndexOutOfBoundsException e) {
213 }
214
215 db.execSQL("CREATE TABLE searches (" +
216 "_id INTEGER PRIMARY KEY," +
217 "search TEXT," +
218 "date LONG" +
219 ");");
220 }
221
222 @Override
223 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
224 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
225 + newVersion + ", which will destroy all old data");
226 if (oldVersion == 18) {
227 db.execSQL("DROP TABLE IF EXISTS labels");
228 } else {
229 db.execSQL("DROP TABLE IF EXISTS bookmarks");
230 db.execSQL("DROP TABLE IF EXISTS searches");
231 onCreate(db);
232 }
233 }
234 }
235
236 @Override
237 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700238 final Context context = getContext();
239 mOpenHelper = new DatabaseHelper(context);
240 // we added "picasa web album" into default bookmarks for version 19.
241 // To avoid erasing the bookmark table, we added it explicitly for
242 // version 18 and 19 as in the other cases, we will erase the table.
243 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
244 SharedPreferences p = PreferenceManager
245 .getDefaultSharedPreferences(context);
246 boolean fix = p.getBoolean("fix_picasa", true);
247 if (fix) {
248 fixPicasaBookmark();
249 Editor ed = p.edit();
250 ed.putBoolean("fix_picasa", false);
251 ed.commit();
252 }
253 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800254 return true;
255 }
256
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700257 private void fixPicasaBookmark() {
258 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
259 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
260 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
261 try {
262 if (!cursor.moveToFirst()) {
263 // set "created" so that it will be on the top of the list
264 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
265 "date, created, bookmark)" + " VALUES('" +
266 getContext().getString(R.string.picasa) + "', '"
267 + PICASA_URL + "', 0, 0, " + new Date().getTime()
268 + ", 1);");
269 }
270 } finally {
271 if (cursor != null) {
272 cursor.close();
273 }
274 }
275 }
276
The Android Open Source Project0c908882009-03-03 19:32:16 -0800277 /*
278 * Subclass AbstractCursor so we can combine multiple Cursors and add
279 * "Google Search".
280 * Here are the rules.
281 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
282 * "Google Search";
283 * 2. If bookmark/history entries are less than
284 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
285 */
286 private class MySuggestionCursor extends AbstractCursor {
287 private Cursor mHistoryCursor;
288 private Cursor mSuggestCursor;
289 private int mHistoryCount;
290 private int mSuggestionCount;
291 private boolean mBeyondCursor;
292 private String mString;
293
294 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
295 mHistoryCursor = hc;
296 mSuggestCursor = sc;
297 mHistoryCount = hc.getCount();
298 mSuggestionCount = sc != null ? sc.getCount() : 0;
299 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
300 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
301 }
302 mString = string;
303 mBeyondCursor = false;
304 }
305
306 @Override
307 public boolean onMove(int oldPosition, int newPosition) {
308 if (mHistoryCursor == null) {
309 return false;
310 }
311 if (mHistoryCount > newPosition) {
312 mHistoryCursor.moveToPosition(newPosition);
313 mBeyondCursor = false;
314 } else if (mHistoryCount + mSuggestionCount > newPosition) {
315 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
316 mBeyondCursor = false;
317 } else {
318 mBeyondCursor = true;
319 }
320 return true;
321 }
322
323 @Override
324 public int getCount() {
325 if (mString.length() > 0) {
326 return mHistoryCount + mSuggestionCount + 1;
327 } else {
328 return mHistoryCount + mSuggestionCount;
329 }
330 }
331
332 @Override
333 public String[] getColumnNames() {
334 return COLUMNS;
335 }
336
337 @Override
338 public String getString(int columnIndex) {
339 if ((mPos != -1 && mHistoryCursor != null)) {
340 switch(columnIndex) {
341 case SUGGEST_COLUMN_INTENT_ACTION_ID:
342 if (mHistoryCount > mPos) {
343 return Intent.ACTION_VIEW;
344 } else {
345 return Intent.ACTION_SEARCH;
346 }
347
348 case SUGGEST_COLUMN_INTENT_DATA_ID:
349 if (mHistoryCount > mPos) {
350 return mHistoryCursor.getString(1);
351 } else {
352 return null;
353 }
354
355 case SUGGEST_COLUMN_TEXT_1_ID:
356 if (mHistoryCount > mPos) {
357 return mHistoryCursor.getString(1);
358 } else if (!mBeyondCursor) {
359 return mSuggestCursor.getString(1);
360 } else {
361 return mString;
362 }
363
364 case SUGGEST_COLUMN_TEXT_2_ID:
365 if (mHistoryCount > mPos) {
366 return mHistoryCursor.getString(2);
367 } else if (!mBeyondCursor) {
368 return mSuggestCursor.getString(2);
369 } else {
370 return getContext().getString(R.string.search_google);
371 }
372
373 case SUGGEST_COLUMN_ICON_1_ID:
374 if (mHistoryCount > mPos) {
375 if (mHistoryCursor.getInt(3) == 1) {
376 return new Integer(
377 R.drawable.ic_search_category_bookmark)
378 .toString();
379 } else {
380 return new Integer(
381 R.drawable.ic_search_category_history)
382 .toString();
383 }
384 } else {
385 return new Integer(
386 R.drawable.ic_search_category_suggest)
387 .toString();
388 }
389
390 case SUGGEST_COLUMN_ICON_2_ID:
391 return new String("0");
392
393 case SUGGEST_COLUMN_QUERY_ID:
394 if (mHistoryCount > mPos) {
395 return null;
396 } else if (!mBeyondCursor) {
397 return mSuggestCursor.getString(3);
398 } else {
399 return mString;
400 }
401 }
402 }
403 return null;
404 }
405
406 @Override
407 public double getDouble(int column) {
408 throw new UnsupportedOperationException();
409 }
410
411 @Override
412 public float getFloat(int column) {
413 throw new UnsupportedOperationException();
414 }
415
416 @Override
417 public int getInt(int column) {
418 throw new UnsupportedOperationException();
419 }
420
421 @Override
422 public long getLong(int column) {
423 if ((mPos != -1) && column == 0) {
424 return mPos; // use row# as the _Id
425 }
426 throw new UnsupportedOperationException();
427 }
428
429 @Override
430 public short getShort(int column) {
431 throw new UnsupportedOperationException();
432 }
433
434 @Override
435 public boolean isNull(int column) {
436 throw new UnsupportedOperationException();
437 }
438
439 // TODO Temporary change, finalize after jq's changes go in
440 public void deactivate() {
441 if (mHistoryCursor != null) {
442 mHistoryCursor.deactivate();
443 }
444 if (mSuggestCursor != null) {
445 mSuggestCursor.deactivate();
446 }
447 super.deactivate();
448 }
449
450 public boolean requery() {
451 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
452 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
453 }
454
455 // TODO Temporary change, finalize after jq's changes go in
456 public void close() {
457 super.close();
458 if (mHistoryCursor != null) {
459 mHistoryCursor.close();
460 mHistoryCursor = null;
461 }
462 if (mSuggestCursor != null) {
463 mSuggestCursor.close();
464 mSuggestCursor = null;
465 }
466 }
467 }
468
469 @Override
470 public Cursor query(Uri url, String[] projectionIn, String selection,
471 String[] selectionArgs, String sortOrder)
472 throws IllegalStateException {
473 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
474
475 int match = URI_MATCHER.match(url);
476 if (match == -1) {
477 throw new IllegalArgumentException("Unknown URL");
478 }
479
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100480 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800481 String suggestSelection;
482 String [] myArgs;
483 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
484 suggestSelection = null;
485 myArgs = null;
486 } else {
487 String like = selectionArgs[0] + "%";
488 if (selectionArgs[0].startsWith("http")) {
489 myArgs = new String[1];
490 myArgs[0] = like;
491 suggestSelection = selection;
492 } else {
493 SUGGEST_ARGS[0] = "http://" + like;
494 SUGGEST_ARGS[1] = "http://www." + like;
495 SUGGEST_ARGS[2] = "https://" + like;
496 SUGGEST_ARGS[3] = "https://www." + like;
497 myArgs = SUGGEST_ARGS;
498 suggestSelection = SUGGEST_SELECTION;
499 }
500 }
501
502 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
503 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
504 ORDER_BY,
505 (new Integer(MAX_SUGGESTION_LONG_ENTRIES)).toString());
506
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100507 if (match == URI_MATCH_BOOKMARKS_SUGGEST
508 || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800509 return new MySuggestionCursor(c, null, "");
510 } else {
511 // get Google suggest if there is still space in the list
512 if (myArgs != null && myArgs.length > 1
513 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringert24e1ec62009-04-29 16:10:43 +0100514 // TODO: This shouldn't be hard-coded. Instead, it should use the
515 // default web search provider. But the API for that is not implemented yet.
516 ComponentName googleSearchComponent =
517 new ComponentName("com.android.googlesearch",
518 "com.android.googlesearch.GoogleSearch");
519 SearchableInfo si =
520 SearchManager.getSearchableInfo(googleSearchComponent, false);
521 Cursor sc = SearchManager.getSuggestions(getContext(), si, selectionArgs[0]);
522 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800523 }
524 return new MySuggestionCursor(c, null, selectionArgs[0]);
525 }
526 }
527
528 String[] projection = null;
529 if (projectionIn != null && projectionIn.length > 0) {
530 projection = new String[projectionIn.length + 1];
531 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
532 projection[projectionIn.length] = "_id AS _id";
533 }
534
535 StringBuilder whereClause = new StringBuilder(256);
536 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
537 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
538 .append(")");
539 }
540
541 // Tack on the user's selection, if present
542 if (selection != null && selection.length() > 0) {
543 if (whereClause.length() > 0) {
544 whereClause.append(" AND ");
545 }
546
547 whereClause.append('(');
548 whereClause.append(selection);
549 whereClause.append(')');
550 }
551 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
552 whereClause.toString(), selectionArgs, null, null, sortOrder,
553 null);
554 c.setNotificationUri(getContext().getContentResolver(), url);
555 return c;
556 }
557
558 @Override
559 public String getType(Uri url) {
560 int match = URI_MATCHER.match(url);
561 switch (match) {
562 case URI_MATCH_BOOKMARKS:
563 return "vnd.android.cursor.dir/bookmark";
564
565 case URI_MATCH_BOOKMARKS_ID:
566 return "vnd.android.cursor.item/bookmark";
567
568 case URI_MATCH_SEARCHES:
569 return "vnd.android.cursor.dir/searches";
570
571 case URI_MATCH_SEARCHES_ID:
572 return "vnd.android.cursor.item/searches";
573
574 case URI_MATCH_SUGGEST:
575 return SearchManager.SUGGEST_MIME_TYPE;
576
577 default:
578 throw new IllegalArgumentException("Unknown URL");
579 }
580 }
581
582 @Override
583 public Uri insert(Uri url, ContentValues initialValues) {
584 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
585
586 int match = URI_MATCHER.match(url);
587 Uri uri = null;
588 switch (match) {
589 case URI_MATCH_BOOKMARKS: {
590 // Insert into the bookmarks table
591 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
592 initialValues);
593 if (rowID > 0) {
594 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
595 rowID);
596 }
597 break;
598 }
599
600 case URI_MATCH_SEARCHES: {
601 // Insert into the searches table
602 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
603 initialValues);
604 if (rowID > 0) {
605 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
606 rowID);
607 }
608 break;
609 }
610
611 default:
612 throw new IllegalArgumentException("Unknown URL");
613 }
614
615 if (uri == null) {
616 throw new IllegalArgumentException("Unknown URL");
617 }
618 getContext().getContentResolver().notifyChange(uri, null);
619 return uri;
620 }
621
622 @Override
623 public int delete(Uri url, String where, String[] whereArgs) {
624 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
625
626 int match = URI_MATCHER.match(url);
627 if (match == -1 || match == URI_MATCH_SUGGEST) {
628 throw new IllegalArgumentException("Unknown URL");
629 }
630
631 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
632 StringBuilder sb = new StringBuilder();
633 if (where != null && where.length() > 0) {
634 sb.append("( ");
635 sb.append(where);
636 sb.append(" ) AND ");
637 }
638 sb.append("_id = ");
639 sb.append(url.getPathSegments().get(1));
640 where = sb.toString();
641 }
642
643 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
644 getContext().getContentResolver().notifyChange(url, null);
645 return count;
646 }
647
648 @Override
649 public int update(Uri url, ContentValues values, String where,
650 String[] whereArgs) {
651 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
652
653 int match = URI_MATCHER.match(url);
654 if (match == -1 || match == URI_MATCH_SUGGEST) {
655 throw new IllegalArgumentException("Unknown URL");
656 }
657
658 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
659 StringBuilder sb = new StringBuilder();
660 if (where != null && where.length() > 0) {
661 sb.append("( ");
662 sb.append(where);
663 sb.append(" ) AND ");
664 }
665 sb.append("_id = ");
666 sb.append(url.getPathSegments().get(1));
667 where = sb.toString();
668 }
669
670 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
671 getContext().getContentResolver().notifyChange(url, null);
672 return ret;
673 }
674}