blob: 3ab7676bf10cc4535106862aced14e3648ca14e8 [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;
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 Rajeswarandd4f4292009-03-24 20:41:19 -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 Rajeswarandd4f4292009-03-24 20:41:19 -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 Rajeswarandd4f4292009-03-24 20:41:19 -0700160 if (propertyKeyValue.equals("CLIENT_ID")) {
161 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800162 } else {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -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 Rajeswarandd4f4292009-03-24 20:41:19 -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] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700488 if (selectionArgs[0].startsWith("http")
489 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800490 myArgs = new String[1];
491 myArgs[0] = like;
492 suggestSelection = selection;
493 } else {
494 SUGGEST_ARGS[0] = "http://" + like;
495 SUGGEST_ARGS[1] = "http://www." + like;
496 SUGGEST_ARGS[2] = "https://" + like;
497 SUGGEST_ARGS[3] = "https://www." + like;
498 myArgs = SUGGEST_ARGS;
499 suggestSelection = SUGGEST_SELECTION;
500 }
501 }
502
503 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
504 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
505 ORDER_BY,
506 (new Integer(MAX_SUGGESTION_LONG_ENTRIES)).toString());
507
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100508 if (match == URI_MATCH_BOOKMARKS_SUGGEST
509 || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800510 return new MySuggestionCursor(c, null, "");
511 } else {
512 // get Google suggest if there is still space in the list
513 if (myArgs != null && myArgs.length > 1
514 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringert24e1ec62009-04-29 16:10:43 +0100515 // TODO: This shouldn't be hard-coded. Instead, it should use the
516 // default web search provider. But the API for that is not implemented yet.
517 ComponentName googleSearchComponent =
518 new ComponentName("com.android.googlesearch",
519 "com.android.googlesearch.GoogleSearch");
520 SearchableInfo si =
521 SearchManager.getSearchableInfo(googleSearchComponent, false);
522 Cursor sc = SearchManager.getSuggestions(getContext(), si, selectionArgs[0]);
523 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800524 }
525 return new MySuggestionCursor(c, null, selectionArgs[0]);
526 }
527 }
528
529 String[] projection = null;
530 if (projectionIn != null && projectionIn.length > 0) {
531 projection = new String[projectionIn.length + 1];
532 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
533 projection[projectionIn.length] = "_id AS _id";
534 }
535
536 StringBuilder whereClause = new StringBuilder(256);
537 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
538 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
539 .append(")");
540 }
541
542 // Tack on the user's selection, if present
543 if (selection != null && selection.length() > 0) {
544 if (whereClause.length() > 0) {
545 whereClause.append(" AND ");
546 }
547
548 whereClause.append('(');
549 whereClause.append(selection);
550 whereClause.append(')');
551 }
552 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
553 whereClause.toString(), selectionArgs, null, null, sortOrder,
554 null);
555 c.setNotificationUri(getContext().getContentResolver(), url);
556 return c;
557 }
558
559 @Override
560 public String getType(Uri url) {
561 int match = URI_MATCHER.match(url);
562 switch (match) {
563 case URI_MATCH_BOOKMARKS:
564 return "vnd.android.cursor.dir/bookmark";
565
566 case URI_MATCH_BOOKMARKS_ID:
567 return "vnd.android.cursor.item/bookmark";
568
569 case URI_MATCH_SEARCHES:
570 return "vnd.android.cursor.dir/searches";
571
572 case URI_MATCH_SEARCHES_ID:
573 return "vnd.android.cursor.item/searches";
574
575 case URI_MATCH_SUGGEST:
576 return SearchManager.SUGGEST_MIME_TYPE;
577
578 default:
579 throw new IllegalArgumentException("Unknown URL");
580 }
581 }
582
583 @Override
584 public Uri insert(Uri url, ContentValues initialValues) {
585 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
586
587 int match = URI_MATCHER.match(url);
588 Uri uri = null;
589 switch (match) {
590 case URI_MATCH_BOOKMARKS: {
591 // Insert into the bookmarks table
592 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
593 initialValues);
594 if (rowID > 0) {
595 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
596 rowID);
597 }
598 break;
599 }
600
601 case URI_MATCH_SEARCHES: {
602 // Insert into the searches table
603 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
604 initialValues);
605 if (rowID > 0) {
606 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
607 rowID);
608 }
609 break;
610 }
611
612 default:
613 throw new IllegalArgumentException("Unknown URL");
614 }
615
616 if (uri == null) {
617 throw new IllegalArgumentException("Unknown URL");
618 }
619 getContext().getContentResolver().notifyChange(uri, null);
620 return uri;
621 }
622
623 @Override
624 public int delete(Uri url, String where, String[] whereArgs) {
625 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
626
627 int match = URI_MATCHER.match(url);
628 if (match == -1 || match == URI_MATCH_SUGGEST) {
629 throw new IllegalArgumentException("Unknown URL");
630 }
631
632 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
633 StringBuilder sb = new StringBuilder();
634 if (where != null && where.length() > 0) {
635 sb.append("( ");
636 sb.append(where);
637 sb.append(" ) AND ");
638 }
639 sb.append("_id = ");
640 sb.append(url.getPathSegments().get(1));
641 where = sb.toString();
642 }
643
644 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
645 getContext().getContentResolver().notifyChange(url, null);
646 return count;
647 }
648
649 @Override
650 public int update(Uri url, ContentValues values, String where,
651 String[] whereArgs) {
652 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
653
654 int match = URI_MATCHER.match(url);
655 if (match == -1 || match == URI_MATCH_SUGGEST) {
656 throw new IllegalArgumentException("Unknown URL");
657 }
658
659 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
660 StringBuilder sb = new StringBuilder();
661 if (where != null && where.length() > 0) {
662 sb.append("( ");
663 sb.append(where);
664 sb.append(" ) AND ");
665 }
666 sb.append("_id = ");
667 sb.append(url.getPathSegments().get(1));
668 where = sb.toString();
669 }
670
671 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
672 getContext().getContentResolver().notifyChange(url, null);
673 return ret;
674 }
675}