blob: 8c30873849d5f23ea6d0b3144d641e35f2a59e8f [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 -070020
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.app.SearchManager;
22import android.content.ComponentName;
23import android.content.ContentProvider;
24import android.content.ContentUris;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.Intent;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070028import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.content.UriMatcher;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070030import android.content.SharedPreferences.Editor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.database.AbstractCursor;
32import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.database.sqlite.SQLiteDatabase;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010034import android.database.sqlite.SQLiteOpenHelper;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.net.Uri;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070036import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.provider.Browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.server.search.SearchableInfo;
39import android.text.util.Regex;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010040import android.util.Log;
41
42import java.util.Date;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070044
The Android Open Source Project0c908882009-03-03 19:32:16 -080045public class BrowserProvider extends ContentProvider {
46
47 private SQLiteOpenHelper mOpenHelper;
48 private static final String sDatabaseName = "browser.db";
49 private static final String TAG = "BrowserProvider";
50 private static final String ORDER_BY = "visits DESC, date DESC";
51
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070052 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
53 "viewer?source=androidclient";
54
The Android Open Source Project0c908882009-03-03 19:32:16 -080055 private static final String[] TABLE_NAMES = new String[] {
56 "bookmarks", "searches"
57 };
58 private static final String[] SUGGEST_PROJECTION = new String[] {
59 "_id", "url", "title", "bookmark"
60 };
61 private static final String SUGGEST_SELECTION =
62 "url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?";
63 private String[] SUGGEST_ARGS = new String[4];
64
65 // shared suggestion array index, make sure to match COLUMNS
66 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
67 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
68 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
69 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
70 private static final int SUGGEST_COLUMN_ICON_1_ID = 5;
71 private static final int SUGGEST_COLUMN_ICON_2_ID = 6;
72 private static final int SUGGEST_COLUMN_QUERY_ID = 7;
73
74 // shared suggestion columns
75 private static final String[] COLUMNS = new String[] {
76 "_id",
77 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
78 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
79 SearchManager.SUGGEST_COLUMN_TEXT_1,
80 SearchManager.SUGGEST_COLUMN_TEXT_2,
81 SearchManager.SUGGEST_COLUMN_ICON_1,
82 SearchManager.SUGGEST_COLUMN_ICON_2,
83 SearchManager.SUGGEST_COLUMN_QUERY};
84
85 private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3;
86 private static final int MAX_SUGGESTION_LONG_ENTRIES = 6;
87
88 // make sure that these match the index of TABLE_NAMES
89 private static final int URI_MATCH_BOOKMARKS = 0;
90 private static final int URI_MATCH_SEARCHES = 1;
91 // (id % 10) should match the table name index
92 private static final int URI_MATCH_BOOKMARKS_ID = 10;
93 private static final int URI_MATCH_SEARCHES_ID = 11;
94 //
95 private static final int URI_MATCH_SUGGEST = 20;
Bjorn Bringert346dafb2009-04-29 21:41:47 +010096 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
The Android Open Source Project0c908882009-03-03 19:32:16 -080097
98 private static final UriMatcher URI_MATCHER;
99
100 static {
101 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
102 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
103 URI_MATCH_BOOKMARKS);
104 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
105 URI_MATCH_BOOKMARKS_ID);
106 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
107 URI_MATCH_SEARCHES);
108 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
109 URI_MATCH_SEARCHES_ID);
110 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
111 URI_MATCH_SUGGEST);
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100112 URI_MATCHER.addURI("browser",
113 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
114 URI_MATCH_BOOKMARKS_SUGGEST);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115 }
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
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100476 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800477 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
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100504 if (match == URI_MATCH_BOOKMARKS_SUGGEST
505 || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800506 return new MySuggestionCursor(c, null, "");
507 } else {
508 // get Google suggest if there is still space in the list
509 if (myArgs != null && myArgs.length > 1
510 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringert24e1ec62009-04-29 16:10:43 +0100511 // TODO: This shouldn't be hard-coded. Instead, it should use the
512 // default web search provider. But the API for that is not implemented yet.
513 ComponentName googleSearchComponent =
514 new ComponentName("com.android.googlesearch",
515 "com.android.googlesearch.GoogleSearch");
516 SearchableInfo si =
517 SearchManager.getSearchableInfo(googleSearchComponent, false);
518 Cursor sc = SearchManager.getSuggestions(getContext(), si, selectionArgs[0]);
519 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800520 }
521 return new MySuggestionCursor(c, null, selectionArgs[0]);
522 }
523 }
524
525 String[] projection = null;
526 if (projectionIn != null && projectionIn.length > 0) {
527 projection = new String[projectionIn.length + 1];
528 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
529 projection[projectionIn.length] = "_id AS _id";
530 }
531
532 StringBuilder whereClause = new StringBuilder(256);
533 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
534 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
535 .append(")");
536 }
537
538 // Tack on the user's selection, if present
539 if (selection != null && selection.length() > 0) {
540 if (whereClause.length() > 0) {
541 whereClause.append(" AND ");
542 }
543
544 whereClause.append('(');
545 whereClause.append(selection);
546 whereClause.append(')');
547 }
548 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
549 whereClause.toString(), selectionArgs, null, null, sortOrder,
550 null);
551 c.setNotificationUri(getContext().getContentResolver(), url);
552 return c;
553 }
554
555 @Override
556 public String getType(Uri url) {
557 int match = URI_MATCHER.match(url);
558 switch (match) {
559 case URI_MATCH_BOOKMARKS:
560 return "vnd.android.cursor.dir/bookmark";
561
562 case URI_MATCH_BOOKMARKS_ID:
563 return "vnd.android.cursor.item/bookmark";
564
565 case URI_MATCH_SEARCHES:
566 return "vnd.android.cursor.dir/searches";
567
568 case URI_MATCH_SEARCHES_ID:
569 return "vnd.android.cursor.item/searches";
570
571 case URI_MATCH_SUGGEST:
572 return SearchManager.SUGGEST_MIME_TYPE;
573
574 default:
575 throw new IllegalArgumentException("Unknown URL");
576 }
577 }
578
579 @Override
580 public Uri insert(Uri url, ContentValues initialValues) {
581 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
582
583 int match = URI_MATCHER.match(url);
584 Uri uri = null;
585 switch (match) {
586 case URI_MATCH_BOOKMARKS: {
587 // Insert into the bookmarks table
588 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
589 initialValues);
590 if (rowID > 0) {
591 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
592 rowID);
593 }
594 break;
595 }
596
597 case URI_MATCH_SEARCHES: {
598 // Insert into the searches table
599 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
600 initialValues);
601 if (rowID > 0) {
602 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
603 rowID);
604 }
605 break;
606 }
607
608 default:
609 throw new IllegalArgumentException("Unknown URL");
610 }
611
612 if (uri == null) {
613 throw new IllegalArgumentException("Unknown URL");
614 }
615 getContext().getContentResolver().notifyChange(uri, null);
616 return uri;
617 }
618
619 @Override
620 public int delete(Uri url, String where, String[] whereArgs) {
621 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
622
623 int match = URI_MATCHER.match(url);
624 if (match == -1 || match == URI_MATCH_SUGGEST) {
625 throw new IllegalArgumentException("Unknown URL");
626 }
627
628 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
629 StringBuilder sb = new StringBuilder();
630 if (where != null && where.length() > 0) {
631 sb.append("( ");
632 sb.append(where);
633 sb.append(" ) AND ");
634 }
635 sb.append("_id = ");
636 sb.append(url.getPathSegments().get(1));
637 where = sb.toString();
638 }
639
640 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
641 getContext().getContentResolver().notifyChange(url, null);
642 return count;
643 }
644
645 @Override
646 public int update(Uri url, ContentValues values, String where,
647 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 ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
668 getContext().getContentResolver().notifyChange(url, null);
669 return ret;
670 }
671}