The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
Ramanan Rajeswaran | f447f26 | 2009-03-24 20:40:12 -0700 | [diff] [blame] | 19 | import com.google.android.providers.GoogleSettings.Partner; |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 20 | import java.util.Date; |
| 21 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 22 | import android.app.ISearchManager; |
| 23 | import android.app.SearchManager; |
| 24 | import android.content.ComponentName; |
| 25 | import android.content.ContentProvider; |
| 26 | import android.content.ContentUris; |
Ramanan Rajeswaran | f447f26 | 2009-03-24 20:40:12 -0700 | [diff] [blame] | 27 | import android.content.ContentResolver; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 28 | import android.content.ContentValues; |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 31 | import android.content.SharedPreferences; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 32 | import android.content.UriMatcher; |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 33 | import android.content.SharedPreferences.Editor; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 34 | import android.database.AbstractCursor; |
| 35 | import android.database.Cursor; |
| 36 | import android.database.sqlite.SQLiteOpenHelper; |
| 37 | import android.database.sqlite.SQLiteDatabase; |
| 38 | import android.net.Uri; |
| 39 | import android.os.RemoteException; |
| 40 | import android.os.ServiceManager; |
| 41 | import android.os.SystemProperties; |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 42 | import android.preference.PreferenceManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 43 | import android.provider.Browser; |
| 44 | import android.util.Log; |
| 45 | import android.server.search.SearchableInfo; |
| 46 | import android.text.util.Regex; |
| 47 | |
Ramanan Rajeswaran | f447f26 | 2009-03-24 20:40:12 -0700 | [diff] [blame] | 48 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 49 | public 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 Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 56 | private static final String PICASA_URL = "http://picasaweb.google.com/m/" + |
| 57 | "viewer?source=androidclient"; |
| 58 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 59 | private static final String[] TABLE_NAMES = new String[] { |
| 60 | "bookmarks", "searches" |
| 61 | }; |
| 62 | private static final String[] SUGGEST_PROJECTION = new String[] { |
| 63 | "_id", "url", "title", "bookmark" |
| 64 | }; |
| 65 | private static final String SUGGEST_SELECTION = |
| 66 | "url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?"; |
| 67 | private String[] SUGGEST_ARGS = new String[4]; |
| 68 | |
| 69 | // shared suggestion array index, make sure to match COLUMNS |
| 70 | private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1; |
| 71 | private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2; |
| 72 | private static final int SUGGEST_COLUMN_TEXT_1_ID = 3; |
| 73 | private static final int SUGGEST_COLUMN_TEXT_2_ID = 4; |
| 74 | private static final int SUGGEST_COLUMN_ICON_1_ID = 5; |
| 75 | private static final int SUGGEST_COLUMN_ICON_2_ID = 6; |
| 76 | private static final int SUGGEST_COLUMN_QUERY_ID = 7; |
| 77 | |
| 78 | // shared suggestion columns |
| 79 | private static final String[] COLUMNS = new String[] { |
| 80 | "_id", |
| 81 | SearchManager.SUGGEST_COLUMN_INTENT_ACTION, |
| 82 | SearchManager.SUGGEST_COLUMN_INTENT_DATA, |
| 83 | SearchManager.SUGGEST_COLUMN_TEXT_1, |
| 84 | SearchManager.SUGGEST_COLUMN_TEXT_2, |
| 85 | SearchManager.SUGGEST_COLUMN_ICON_1, |
| 86 | SearchManager.SUGGEST_COLUMN_ICON_2, |
| 87 | SearchManager.SUGGEST_COLUMN_QUERY}; |
| 88 | |
| 89 | private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3; |
| 90 | private static final int MAX_SUGGESTION_LONG_ENTRIES = 6; |
| 91 | |
| 92 | // make sure that these match the index of TABLE_NAMES |
| 93 | private static final int URI_MATCH_BOOKMARKS = 0; |
| 94 | private static final int URI_MATCH_SEARCHES = 1; |
| 95 | // (id % 10) should match the table name index |
| 96 | private static final int URI_MATCH_BOOKMARKS_ID = 10; |
| 97 | private static final int URI_MATCH_SEARCHES_ID = 11; |
| 98 | // |
| 99 | private static final int URI_MATCH_SUGGEST = 20; |
| 100 | |
| 101 | private static final UriMatcher URI_MATCHER; |
| 102 | |
| 103 | static { |
| 104 | URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH); |
| 105 | URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS], |
| 106 | URI_MATCH_BOOKMARKS); |
| 107 | URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#", |
| 108 | URI_MATCH_BOOKMARKS_ID); |
| 109 | URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES], |
| 110 | URI_MATCH_SEARCHES); |
| 111 | URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#", |
| 112 | URI_MATCH_SEARCHES_ID); |
| 113 | URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY, |
| 114 | URI_MATCH_SUGGEST); |
| 115 | } |
| 116 | |
| 117 | // 1 -> 2 add cache table |
| 118 | // 2 -> 3 update history table |
| 119 | // 3 -> 4 add passwords table |
| 120 | // 4 -> 5 add settings table |
| 121 | // 5 -> 6 ? |
| 122 | // 6 -> 7 ? |
| 123 | // 7 -> 8 drop proxy table |
| 124 | // 8 -> 9 drop settings table |
| 125 | // 9 -> 10 add form_urls and form_data |
| 126 | // 10 -> 11 add searches table |
| 127 | // 11 -> 12 modify cache table |
| 128 | // 12 -> 13 modify cache table |
| 129 | // 13 -> 14 correspond with Google Bookmarks schema |
| 130 | // 14 -> 15 move couple of tables to either browser private database or webview database |
| 131 | // 15 -> 17 Set it up for the SearchManager |
| 132 | // 17 -> 18 Added favicon in bookmarks table for Home shortcuts |
| 133 | // 18 -> 19 Remove labels table |
| 134 | private static final int DATABASE_VERSION = 19; |
| 135 | |
| 136 | public BrowserProvider() { |
| 137 | } |
| 138 | |
| 139 | |
Ramanan Rajeswaran | f447f26 | 2009-03-24 20:40:12 -0700 | [diff] [blame] | 140 | private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 141 | StringBuffer sb = new StringBuffer(); |
| 142 | int lastCharLoc = 0; |
Ramanan Rajeswaran | f447f26 | 2009-03-24 20:40:12 -0700 | [diff] [blame] | 143 | |
| 144 | final String client_id = Partner.getString(context.getContentResolver(), Partner.CLIENT_ID); |
| 145 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 146 | 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 Rajeswaran | f447f26 | 2009-03-24 20:40:12 -0700 | [diff] [blame] | 156 | if (propertyKeyValue.equals("CLIENT_ID")) { |
| 157 | sb.append(client_id); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 158 | } else { |
Ramanan Rajeswaran | f447f26 | 2009-03-24 20:40:12 -0700 | [diff] [blame] | 159 | sb.append("unknown"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 160 | } |
| 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 Rajeswaran | f447f26 | 2009-03-24 20:40:12 -0700 | [diff] [blame] | 202 | CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 203 | 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 Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 234 | 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 250 | return true; |
| 251 | } |
| 252 | |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 253 | 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 273 | /* |
| 274 | * Subclass AbstractCursor so we can combine multiple Cursors and add |
| 275 | * "Google Search". |
| 276 | * Here are the rules. |
| 277 | * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus |
| 278 | * "Google Search"; |
| 279 | * 2. If bookmark/history entries are less than |
| 280 | * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest. |
| 281 | */ |
| 282 | private class MySuggestionCursor extends AbstractCursor { |
| 283 | private Cursor mHistoryCursor; |
| 284 | private Cursor mSuggestCursor; |
| 285 | private int mHistoryCount; |
| 286 | private int mSuggestionCount; |
| 287 | private boolean mBeyondCursor; |
| 288 | private String mString; |
| 289 | |
| 290 | public MySuggestionCursor(Cursor hc, Cursor sc, String string) { |
| 291 | mHistoryCursor = hc; |
| 292 | mSuggestCursor = sc; |
| 293 | mHistoryCount = hc.getCount(); |
| 294 | mSuggestionCount = sc != null ? sc.getCount() : 0; |
| 295 | if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) { |
| 296 | mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount; |
| 297 | } |
| 298 | mString = string; |
| 299 | mBeyondCursor = false; |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public boolean onMove(int oldPosition, int newPosition) { |
| 304 | if (mHistoryCursor == null) { |
| 305 | return false; |
| 306 | } |
| 307 | if (mHistoryCount > newPosition) { |
| 308 | mHistoryCursor.moveToPosition(newPosition); |
| 309 | mBeyondCursor = false; |
| 310 | } else if (mHistoryCount + mSuggestionCount > newPosition) { |
| 311 | mSuggestCursor.moveToPosition(newPosition - mHistoryCount); |
| 312 | mBeyondCursor = false; |
| 313 | } else { |
| 314 | mBeyondCursor = true; |
| 315 | } |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public int getCount() { |
| 321 | if (mString.length() > 0) { |
| 322 | return mHistoryCount + mSuggestionCount + 1; |
| 323 | } else { |
| 324 | return mHistoryCount + mSuggestionCount; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public String[] getColumnNames() { |
| 330 | return COLUMNS; |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public String getString(int columnIndex) { |
| 335 | if ((mPos != -1 && mHistoryCursor != null)) { |
| 336 | switch(columnIndex) { |
| 337 | case SUGGEST_COLUMN_INTENT_ACTION_ID: |
| 338 | if (mHistoryCount > mPos) { |
| 339 | return Intent.ACTION_VIEW; |
| 340 | } else { |
| 341 | return Intent.ACTION_SEARCH; |
| 342 | } |
| 343 | |
| 344 | case SUGGEST_COLUMN_INTENT_DATA_ID: |
| 345 | if (mHistoryCount > mPos) { |
| 346 | return mHistoryCursor.getString(1); |
| 347 | } else { |
| 348 | return null; |
| 349 | } |
| 350 | |
| 351 | case SUGGEST_COLUMN_TEXT_1_ID: |
| 352 | if (mHistoryCount > mPos) { |
| 353 | return mHistoryCursor.getString(1); |
| 354 | } else if (!mBeyondCursor) { |
| 355 | return mSuggestCursor.getString(1); |
| 356 | } else { |
| 357 | return mString; |
| 358 | } |
| 359 | |
| 360 | case SUGGEST_COLUMN_TEXT_2_ID: |
| 361 | if (mHistoryCount > mPos) { |
| 362 | return mHistoryCursor.getString(2); |
| 363 | } else if (!mBeyondCursor) { |
| 364 | return mSuggestCursor.getString(2); |
| 365 | } else { |
| 366 | return getContext().getString(R.string.search_google); |
| 367 | } |
| 368 | |
| 369 | case SUGGEST_COLUMN_ICON_1_ID: |
| 370 | if (mHistoryCount > mPos) { |
| 371 | if (mHistoryCursor.getInt(3) == 1) { |
| 372 | return new Integer( |
| 373 | R.drawable.ic_search_category_bookmark) |
| 374 | .toString(); |
| 375 | } else { |
| 376 | return new Integer( |
| 377 | R.drawable.ic_search_category_history) |
| 378 | .toString(); |
| 379 | } |
| 380 | } else { |
| 381 | return new Integer( |
| 382 | R.drawable.ic_search_category_suggest) |
| 383 | .toString(); |
| 384 | } |
| 385 | |
| 386 | case SUGGEST_COLUMN_ICON_2_ID: |
| 387 | return new String("0"); |
| 388 | |
| 389 | case SUGGEST_COLUMN_QUERY_ID: |
| 390 | if (mHistoryCount > mPos) { |
| 391 | return null; |
| 392 | } else if (!mBeyondCursor) { |
| 393 | return mSuggestCursor.getString(3); |
| 394 | } else { |
| 395 | return mString; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | return null; |
| 400 | } |
| 401 | |
| 402 | @Override |
| 403 | public double getDouble(int column) { |
| 404 | throw new UnsupportedOperationException(); |
| 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public float getFloat(int column) { |
| 409 | throw new UnsupportedOperationException(); |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public int getInt(int column) { |
| 414 | throw new UnsupportedOperationException(); |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public long getLong(int column) { |
| 419 | if ((mPos != -1) && column == 0) { |
| 420 | return mPos; // use row# as the _Id |
| 421 | } |
| 422 | throw new UnsupportedOperationException(); |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public short getShort(int column) { |
| 427 | throw new UnsupportedOperationException(); |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public boolean isNull(int column) { |
| 432 | throw new UnsupportedOperationException(); |
| 433 | } |
| 434 | |
| 435 | // TODO Temporary change, finalize after jq's changes go in |
| 436 | public void deactivate() { |
| 437 | if (mHistoryCursor != null) { |
| 438 | mHistoryCursor.deactivate(); |
| 439 | } |
| 440 | if (mSuggestCursor != null) { |
| 441 | mSuggestCursor.deactivate(); |
| 442 | } |
| 443 | super.deactivate(); |
| 444 | } |
| 445 | |
| 446 | public boolean requery() { |
| 447 | return (mHistoryCursor != null ? mHistoryCursor.requery() : false) | |
| 448 | (mSuggestCursor != null ? mSuggestCursor.requery() : false); |
| 449 | } |
| 450 | |
| 451 | // TODO Temporary change, finalize after jq's changes go in |
| 452 | public void close() { |
| 453 | super.close(); |
| 454 | if (mHistoryCursor != null) { |
| 455 | mHistoryCursor.close(); |
| 456 | mHistoryCursor = null; |
| 457 | } |
| 458 | if (mSuggestCursor != null) { |
| 459 | mSuggestCursor.close(); |
| 460 | mSuggestCursor = null; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | @Override |
| 466 | public Cursor query(Uri url, String[] projectionIn, String selection, |
| 467 | String[] selectionArgs, String sortOrder) |
| 468 | throws IllegalStateException { |
| 469 | SQLiteDatabase db = mOpenHelper.getReadableDatabase(); |
| 470 | |
| 471 | int match = URI_MATCHER.match(url); |
| 472 | if (match == -1) { |
| 473 | throw new IllegalArgumentException("Unknown URL"); |
| 474 | } |
| 475 | |
| 476 | if (match == URI_MATCH_SUGGEST) { |
| 477 | String suggestSelection; |
| 478 | String [] myArgs; |
| 479 | if (selectionArgs[0] == null || selectionArgs[0].equals("")) { |
| 480 | suggestSelection = null; |
| 481 | myArgs = null; |
| 482 | } else { |
| 483 | String like = selectionArgs[0] + "%"; |
| 484 | if (selectionArgs[0].startsWith("http")) { |
| 485 | myArgs = new String[1]; |
| 486 | myArgs[0] = like; |
| 487 | suggestSelection = selection; |
| 488 | } else { |
| 489 | SUGGEST_ARGS[0] = "http://" + like; |
| 490 | SUGGEST_ARGS[1] = "http://www." + like; |
| 491 | SUGGEST_ARGS[2] = "https://" + like; |
| 492 | SUGGEST_ARGS[3] = "https://www." + like; |
| 493 | myArgs = SUGGEST_ARGS; |
| 494 | suggestSelection = SUGGEST_SELECTION; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS], |
| 499 | SUGGEST_PROJECTION, suggestSelection, myArgs, null, null, |
| 500 | ORDER_BY, |
| 501 | (new Integer(MAX_SUGGESTION_LONG_ENTRIES)).toString()); |
| 502 | |
| 503 | if (Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) { |
| 504 | return new MySuggestionCursor(c, null, ""); |
| 505 | } else { |
| 506 | // get Google suggest if there is still space in the list |
| 507 | if (myArgs != null && myArgs.length > 1 |
| 508 | && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) { |
Bjorn Bringert | 24e1ec6 | 2009-04-29 16:10:43 +0100 | [diff] [blame^] | 509 | // TODO: This shouldn't be hard-coded. Instead, it should use the |
| 510 | // default web search provider. But the API for that is not implemented yet. |
| 511 | ComponentName googleSearchComponent = |
| 512 | new ComponentName("com.android.googlesearch", |
| 513 | "com.android.googlesearch.GoogleSearch"); |
| 514 | SearchableInfo si = |
| 515 | SearchManager.getSearchableInfo(googleSearchComponent, false); |
| 516 | Cursor sc = SearchManager.getSuggestions(getContext(), si, selectionArgs[0]); |
| 517 | return new MySuggestionCursor(c, sc, selectionArgs[0]); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 518 | } |
| 519 | return new MySuggestionCursor(c, null, selectionArgs[0]); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | String[] projection = null; |
| 524 | if (projectionIn != null && projectionIn.length > 0) { |
| 525 | projection = new String[projectionIn.length + 1]; |
| 526 | System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length); |
| 527 | projection[projectionIn.length] = "_id AS _id"; |
| 528 | } |
| 529 | |
| 530 | StringBuilder whereClause = new StringBuilder(256); |
| 531 | if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) { |
| 532 | whereClause.append("(_id = ").append(url.getPathSegments().get(1)) |
| 533 | .append(")"); |
| 534 | } |
| 535 | |
| 536 | // Tack on the user's selection, if present |
| 537 | if (selection != null && selection.length() > 0) { |
| 538 | if (whereClause.length() > 0) { |
| 539 | whereClause.append(" AND "); |
| 540 | } |
| 541 | |
| 542 | whereClause.append('('); |
| 543 | whereClause.append(selection); |
| 544 | whereClause.append(')'); |
| 545 | } |
| 546 | Cursor c = db.query(TABLE_NAMES[match % 10], projection, |
| 547 | whereClause.toString(), selectionArgs, null, null, sortOrder, |
| 548 | null); |
| 549 | c.setNotificationUri(getContext().getContentResolver(), url); |
| 550 | return c; |
| 551 | } |
| 552 | |
| 553 | @Override |
| 554 | public String getType(Uri url) { |
| 555 | int match = URI_MATCHER.match(url); |
| 556 | switch (match) { |
| 557 | case URI_MATCH_BOOKMARKS: |
| 558 | return "vnd.android.cursor.dir/bookmark"; |
| 559 | |
| 560 | case URI_MATCH_BOOKMARKS_ID: |
| 561 | return "vnd.android.cursor.item/bookmark"; |
| 562 | |
| 563 | case URI_MATCH_SEARCHES: |
| 564 | return "vnd.android.cursor.dir/searches"; |
| 565 | |
| 566 | case URI_MATCH_SEARCHES_ID: |
| 567 | return "vnd.android.cursor.item/searches"; |
| 568 | |
| 569 | case URI_MATCH_SUGGEST: |
| 570 | return SearchManager.SUGGEST_MIME_TYPE; |
| 571 | |
| 572 | default: |
| 573 | throw new IllegalArgumentException("Unknown URL"); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | @Override |
| 578 | public Uri insert(Uri url, ContentValues initialValues) { |
| 579 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 580 | |
| 581 | int match = URI_MATCHER.match(url); |
| 582 | Uri uri = null; |
| 583 | switch (match) { |
| 584 | case URI_MATCH_BOOKMARKS: { |
| 585 | // Insert into the bookmarks table |
| 586 | long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url", |
| 587 | initialValues); |
| 588 | if (rowID > 0) { |
| 589 | uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI, |
| 590 | rowID); |
| 591 | } |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | case URI_MATCH_SEARCHES: { |
| 596 | // Insert into the searches table |
| 597 | long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url", |
| 598 | initialValues); |
| 599 | if (rowID > 0) { |
| 600 | uri = ContentUris.withAppendedId(Browser.SEARCHES_URI, |
| 601 | rowID); |
| 602 | } |
| 603 | break; |
| 604 | } |
| 605 | |
| 606 | default: |
| 607 | throw new IllegalArgumentException("Unknown URL"); |
| 608 | } |
| 609 | |
| 610 | if (uri == null) { |
| 611 | throw new IllegalArgumentException("Unknown URL"); |
| 612 | } |
| 613 | getContext().getContentResolver().notifyChange(uri, null); |
| 614 | return uri; |
| 615 | } |
| 616 | |
| 617 | @Override |
| 618 | public int delete(Uri url, String where, String[] whereArgs) { |
| 619 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 620 | |
| 621 | int match = URI_MATCHER.match(url); |
| 622 | if (match == -1 || match == URI_MATCH_SUGGEST) { |
| 623 | throw new IllegalArgumentException("Unknown URL"); |
| 624 | } |
| 625 | |
| 626 | if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) { |
| 627 | StringBuilder sb = new StringBuilder(); |
| 628 | if (where != null && where.length() > 0) { |
| 629 | sb.append("( "); |
| 630 | sb.append(where); |
| 631 | sb.append(" ) AND "); |
| 632 | } |
| 633 | sb.append("_id = "); |
| 634 | sb.append(url.getPathSegments().get(1)); |
| 635 | where = sb.toString(); |
| 636 | } |
| 637 | |
| 638 | int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs); |
| 639 | getContext().getContentResolver().notifyChange(url, null); |
| 640 | return count; |
| 641 | } |
| 642 | |
| 643 | @Override |
| 644 | public int update(Uri url, ContentValues values, String where, |
| 645 | String[] whereArgs) { |
| 646 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 647 | |
| 648 | int match = URI_MATCHER.match(url); |
| 649 | if (match == -1 || match == URI_MATCH_SUGGEST) { |
| 650 | throw new IllegalArgumentException("Unknown URL"); |
| 651 | } |
| 652 | |
| 653 | if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) { |
| 654 | StringBuilder sb = new StringBuilder(); |
| 655 | if (where != null && where.length() > 0) { |
| 656 | sb.append("( "); |
| 657 | sb.append(where); |
| 658 | sb.append(" ) AND "); |
| 659 | } |
| 660 | sb.append("_id = "); |
| 661 | sb.append(url.getPathSegments().get(1)); |
| 662 | where = sb.toString(); |
| 663 | } |
| 664 | |
| 665 | int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs); |
| 666 | getContext().getContentResolver().notifyChange(url, null); |
| 667 | return ret; |
| 668 | } |
| 669 | } |