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