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 | dd4f429 | 2009-03-24 20:41:19 -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 | dd4f429 | 2009-03-24 20:41:19 -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; |
Grace Kloba | 87dae9a | 2009-03-24 22:26:59 -0700 | [diff] [blame] | 39 | import android.os.Process; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 40 | import android.os.RemoteException; |
| 41 | import android.os.ServiceManager; |
| 42 | import android.os.SystemProperties; |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 43 | import android.preference.PreferenceManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 44 | import android.provider.Browser; |
| 45 | import android.util.Log; |
| 46 | import android.server.search.SearchableInfo; |
| 47 | import android.text.util.Regex; |
| 48 | |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 49 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 50 | public class BrowserProvider extends ContentProvider { |
| 51 | |
| 52 | private SQLiteOpenHelper mOpenHelper; |
| 53 | private static final String sDatabaseName = "browser.db"; |
| 54 | private static final String TAG = "BrowserProvider"; |
| 55 | private static final String ORDER_BY = "visits DESC, date DESC"; |
| 56 | |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 57 | private static final String PICASA_URL = "http://picasaweb.google.com/m/" + |
| 58 | "viewer?source=androidclient"; |
| 59 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 60 | private static final String[] TABLE_NAMES = new String[] { |
| 61 | "bookmarks", "searches" |
| 62 | }; |
| 63 | private static final String[] SUGGEST_PROJECTION = new String[] { |
| 64 | "_id", "url", "title", "bookmark" |
| 65 | }; |
| 66 | private static final String SUGGEST_SELECTION = |
| 67 | "url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?"; |
| 68 | private String[] SUGGEST_ARGS = new String[4]; |
| 69 | |
| 70 | // shared suggestion array index, make sure to match COLUMNS |
| 71 | private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1; |
| 72 | private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2; |
| 73 | private static final int SUGGEST_COLUMN_TEXT_1_ID = 3; |
| 74 | private static final int SUGGEST_COLUMN_TEXT_2_ID = 4; |
| 75 | private static final int SUGGEST_COLUMN_ICON_1_ID = 5; |
| 76 | private static final int SUGGEST_COLUMN_ICON_2_ID = 6; |
| 77 | private static final int SUGGEST_COLUMN_QUERY_ID = 7; |
| 78 | |
| 79 | // shared suggestion columns |
| 80 | private static final String[] COLUMNS = new String[] { |
| 81 | "_id", |
| 82 | SearchManager.SUGGEST_COLUMN_INTENT_ACTION, |
| 83 | SearchManager.SUGGEST_COLUMN_INTENT_DATA, |
| 84 | SearchManager.SUGGEST_COLUMN_TEXT_1, |
| 85 | SearchManager.SUGGEST_COLUMN_TEXT_2, |
| 86 | SearchManager.SUGGEST_COLUMN_ICON_1, |
| 87 | SearchManager.SUGGEST_COLUMN_ICON_2, |
| 88 | SearchManager.SUGGEST_COLUMN_QUERY}; |
| 89 | |
| 90 | private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3; |
| 91 | private static final int MAX_SUGGESTION_LONG_ENTRIES = 6; |
| 92 | |
| 93 | // make sure that these match the index of TABLE_NAMES |
| 94 | private static final int URI_MATCH_BOOKMARKS = 0; |
| 95 | private static final int URI_MATCH_SEARCHES = 1; |
| 96 | // (id % 10) should match the table name index |
| 97 | private static final int URI_MATCH_BOOKMARKS_ID = 10; |
| 98 | private static final int URI_MATCH_SEARCHES_ID = 11; |
| 99 | // |
| 100 | private static final int URI_MATCH_SUGGEST = 20; |
| 101 | |
| 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); |
| 116 | } |
| 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 Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 141 | private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 142 | StringBuffer sb = new StringBuffer(); |
| 143 | int lastCharLoc = 0; |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 144 | |
| 145 | final String client_id = Partner.getString(context.getContentResolver(), Partner.CLIENT_ID); |
| 146 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 147 | 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 Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 157 | if (propertyKeyValue.equals("CLIENT_ID")) { |
| 158 | sb.append(client_id); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 159 | } else { |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 160 | sb.append("unknown"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 161 | } |
| 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 Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 203 | CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 204 | 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() { |
Grace Kloba | 87dae9a | 2009-03-24 22:26:59 -0700 | [diff] [blame] | 235 | Log.d(TAG, "onCreate start"); |
| 236 | long start = Process.getElapsedCpuTime(); |
| 237 | |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 238 | 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 | } |
Grace Kloba | 87dae9a | 2009-03-24 22:26:59 -0700 | [diff] [blame] | 254 | Log.d(TAG, "onCreate used " + (Process.getElapsedCpuTime() - start) + " ms"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 255 | return true; |
| 256 | } |
| 257 | |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 258 | private void fixPicasaBookmark() { |
| 259 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 260 | Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " + |
| 261 | "bookmark = 1 AND url = ?", new String[] { PICASA_URL }); |
| 262 | try { |
| 263 | if (!cursor.moveToFirst()) { |
| 264 | // set "created" so that it will be on the top of the list |
| 265 | db.execSQL("INSERT INTO bookmarks (title, url, visits, " + |
| 266 | "date, created, bookmark)" + " VALUES('" + |
| 267 | getContext().getString(R.string.picasa) + "', '" |
| 268 | + PICASA_URL + "', 0, 0, " + new Date().getTime() |
| 269 | + ", 1);"); |
| 270 | } |
| 271 | } finally { |
| 272 | if (cursor != null) { |
| 273 | cursor.close(); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 278 | /* |
| 279 | * Subclass AbstractCursor so we can combine multiple Cursors and add |
| 280 | * "Google Search". |
| 281 | * Here are the rules. |
| 282 | * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus |
| 283 | * "Google Search"; |
| 284 | * 2. If bookmark/history entries are less than |
| 285 | * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest. |
| 286 | */ |
| 287 | private class MySuggestionCursor extends AbstractCursor { |
| 288 | private Cursor mHistoryCursor; |
| 289 | private Cursor mSuggestCursor; |
| 290 | private int mHistoryCount; |
| 291 | private int mSuggestionCount; |
| 292 | private boolean mBeyondCursor; |
| 293 | private String mString; |
| 294 | |
| 295 | public MySuggestionCursor(Cursor hc, Cursor sc, String string) { |
| 296 | mHistoryCursor = hc; |
| 297 | mSuggestCursor = sc; |
| 298 | mHistoryCount = hc.getCount(); |
| 299 | mSuggestionCount = sc != null ? sc.getCount() : 0; |
| 300 | if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) { |
| 301 | mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount; |
| 302 | } |
| 303 | mString = string; |
| 304 | mBeyondCursor = false; |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public boolean onMove(int oldPosition, int newPosition) { |
| 309 | if (mHistoryCursor == null) { |
| 310 | return false; |
| 311 | } |
| 312 | if (mHistoryCount > newPosition) { |
| 313 | mHistoryCursor.moveToPosition(newPosition); |
| 314 | mBeyondCursor = false; |
| 315 | } else if (mHistoryCount + mSuggestionCount > newPosition) { |
| 316 | mSuggestCursor.moveToPosition(newPosition - mHistoryCount); |
| 317 | mBeyondCursor = false; |
| 318 | } else { |
| 319 | mBeyondCursor = true; |
| 320 | } |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public int getCount() { |
| 326 | if (mString.length() > 0) { |
| 327 | return mHistoryCount + mSuggestionCount + 1; |
| 328 | } else { |
| 329 | return mHistoryCount + mSuggestionCount; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public String[] getColumnNames() { |
| 335 | return COLUMNS; |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public String getString(int columnIndex) { |
| 340 | if ((mPos != -1 && mHistoryCursor != null)) { |
| 341 | switch(columnIndex) { |
| 342 | case SUGGEST_COLUMN_INTENT_ACTION_ID: |
| 343 | if (mHistoryCount > mPos) { |
| 344 | return Intent.ACTION_VIEW; |
| 345 | } else { |
| 346 | return Intent.ACTION_SEARCH; |
| 347 | } |
| 348 | |
| 349 | case SUGGEST_COLUMN_INTENT_DATA_ID: |
| 350 | if (mHistoryCount > mPos) { |
| 351 | return mHistoryCursor.getString(1); |
| 352 | } else { |
| 353 | return null; |
| 354 | } |
| 355 | |
| 356 | case SUGGEST_COLUMN_TEXT_1_ID: |
| 357 | if (mHistoryCount > mPos) { |
| 358 | return mHistoryCursor.getString(1); |
| 359 | } else if (!mBeyondCursor) { |
| 360 | return mSuggestCursor.getString(1); |
| 361 | } else { |
| 362 | return mString; |
| 363 | } |
| 364 | |
| 365 | case SUGGEST_COLUMN_TEXT_2_ID: |
| 366 | if (mHistoryCount > mPos) { |
| 367 | return mHistoryCursor.getString(2); |
| 368 | } else if (!mBeyondCursor) { |
| 369 | return mSuggestCursor.getString(2); |
| 370 | } else { |
| 371 | return getContext().getString(R.string.search_google); |
| 372 | } |
| 373 | |
| 374 | case SUGGEST_COLUMN_ICON_1_ID: |
| 375 | if (mHistoryCount > mPos) { |
| 376 | if (mHistoryCursor.getInt(3) == 1) { |
| 377 | return new Integer( |
| 378 | R.drawable.ic_search_category_bookmark) |
| 379 | .toString(); |
| 380 | } else { |
| 381 | return new Integer( |
| 382 | R.drawable.ic_search_category_history) |
| 383 | .toString(); |
| 384 | } |
| 385 | } else { |
| 386 | return new Integer( |
| 387 | R.drawable.ic_search_category_suggest) |
| 388 | .toString(); |
| 389 | } |
| 390 | |
| 391 | case SUGGEST_COLUMN_ICON_2_ID: |
| 392 | return new String("0"); |
| 393 | |
| 394 | case SUGGEST_COLUMN_QUERY_ID: |
| 395 | if (mHistoryCount > mPos) { |
| 396 | return null; |
| 397 | } else if (!mBeyondCursor) { |
| 398 | return mSuggestCursor.getString(3); |
| 399 | } else { |
| 400 | return mString; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | return null; |
| 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public double getDouble(int column) { |
| 409 | throw new UnsupportedOperationException(); |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public float getFloat(int column) { |
| 414 | throw new UnsupportedOperationException(); |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public int getInt(int column) { |
| 419 | throw new UnsupportedOperationException(); |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public long getLong(int column) { |
| 424 | if ((mPos != -1) && column == 0) { |
| 425 | return mPos; // use row# as the _Id |
| 426 | } |
| 427 | throw new UnsupportedOperationException(); |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public short getShort(int column) { |
| 432 | throw new UnsupportedOperationException(); |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public boolean isNull(int column) { |
| 437 | throw new UnsupportedOperationException(); |
| 438 | } |
| 439 | |
| 440 | // TODO Temporary change, finalize after jq's changes go in |
| 441 | public void deactivate() { |
| 442 | if (mHistoryCursor != null) { |
| 443 | mHistoryCursor.deactivate(); |
| 444 | } |
| 445 | if (mSuggestCursor != null) { |
| 446 | mSuggestCursor.deactivate(); |
| 447 | } |
| 448 | super.deactivate(); |
| 449 | } |
| 450 | |
| 451 | public boolean requery() { |
| 452 | return (mHistoryCursor != null ? mHistoryCursor.requery() : false) | |
| 453 | (mSuggestCursor != null ? mSuggestCursor.requery() : false); |
| 454 | } |
| 455 | |
| 456 | // TODO Temporary change, finalize after jq's changes go in |
| 457 | public void close() { |
| 458 | super.close(); |
| 459 | if (mHistoryCursor != null) { |
| 460 | mHistoryCursor.close(); |
| 461 | mHistoryCursor = null; |
| 462 | } |
| 463 | if (mSuggestCursor != null) { |
| 464 | mSuggestCursor.close(); |
| 465 | mSuggestCursor = null; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | @Override |
| 471 | public Cursor query(Uri url, String[] projectionIn, String selection, |
| 472 | String[] selectionArgs, String sortOrder) |
| 473 | throws IllegalStateException { |
| 474 | SQLiteDatabase db = mOpenHelper.getReadableDatabase(); |
| 475 | |
| 476 | int match = URI_MATCHER.match(url); |
| 477 | if (match == -1) { |
| 478 | throw new IllegalArgumentException("Unknown URL"); |
| 479 | } |
| 480 | |
| 481 | if (match == URI_MATCH_SUGGEST) { |
| 482 | String suggestSelection; |
| 483 | String [] myArgs; |
| 484 | if (selectionArgs[0] == null || selectionArgs[0].equals("")) { |
| 485 | suggestSelection = null; |
| 486 | myArgs = null; |
| 487 | } else { |
| 488 | String like = selectionArgs[0] + "%"; |
| 489 | if (selectionArgs[0].startsWith("http")) { |
| 490 | 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 | |
| 508 | if (Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) { |
| 509 | return new MySuggestionCursor(c, null, ""); |
| 510 | } else { |
| 511 | // get Google suggest if there is still space in the list |
| 512 | if (myArgs != null && myArgs.length > 1 |
| 513 | && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) { |
| 514 | ISearchManager sm = ISearchManager.Stub |
| 515 | .asInterface(ServiceManager |
| 516 | .getService(Context.SEARCH_SERVICE)); |
| 517 | SearchableInfo si = null; |
| 518 | try { |
| 519 | // use the global search to get Google suggest provider |
| 520 | si = sm.getSearchableInfo(new ComponentName( |
| 521 | getContext(), "com.android.browser"), true); |
| 522 | |
| 523 | // similar to the getSuggestions() in SearchDialog.java |
| 524 | StringBuilder uriStr = new StringBuilder("content://"); |
| 525 | uriStr.append(si.getSuggestAuthority()); |
| 526 | // if content path provided, insert it now |
| 527 | final String contentPath = si.getSuggestPath(); |
| 528 | if (contentPath != null) { |
| 529 | uriStr.append('/'); |
| 530 | uriStr.append(contentPath); |
| 531 | } |
| 532 | // append standard suggestion query path |
| 533 | uriStr.append('/' + SearchManager.SUGGEST_URI_PATH_QUERY); |
| 534 | // inject query, either as selection args or inline |
| 535 | String[] selArgs = null; |
| 536 | if (si.getSuggestSelection() != null) { |
| 537 | selArgs = new String[] {selectionArgs[0]}; |
| 538 | } else { |
| 539 | uriStr.append('/'); |
| 540 | uriStr.append(Uri.encode(selectionArgs[0])); |
| 541 | } |
| 542 | |
| 543 | // finally, make the query |
| 544 | Cursor sc = getContext().getContentResolver().query( |
| 545 | Uri.parse(uriStr.toString()), null, |
| 546 | si.getSuggestSelection(), selArgs, null); |
| 547 | |
| 548 | return new MySuggestionCursor(c, sc, selectionArgs[0]); |
| 549 | } catch (RemoteException e) { |
| 550 | } |
| 551 | } |
| 552 | return new MySuggestionCursor(c, null, selectionArgs[0]); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | String[] projection = null; |
| 557 | if (projectionIn != null && projectionIn.length > 0) { |
| 558 | projection = new String[projectionIn.length + 1]; |
| 559 | System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length); |
| 560 | projection[projectionIn.length] = "_id AS _id"; |
| 561 | } |
| 562 | |
| 563 | StringBuilder whereClause = new StringBuilder(256); |
| 564 | if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) { |
| 565 | whereClause.append("(_id = ").append(url.getPathSegments().get(1)) |
| 566 | .append(")"); |
| 567 | } |
| 568 | |
| 569 | // Tack on the user's selection, if present |
| 570 | if (selection != null && selection.length() > 0) { |
| 571 | if (whereClause.length() > 0) { |
| 572 | whereClause.append(" AND "); |
| 573 | } |
| 574 | |
| 575 | whereClause.append('('); |
| 576 | whereClause.append(selection); |
| 577 | whereClause.append(')'); |
| 578 | } |
| 579 | Cursor c = db.query(TABLE_NAMES[match % 10], projection, |
| 580 | whereClause.toString(), selectionArgs, null, null, sortOrder, |
| 581 | null); |
| 582 | c.setNotificationUri(getContext().getContentResolver(), url); |
| 583 | return c; |
| 584 | } |
| 585 | |
| 586 | @Override |
| 587 | public String getType(Uri url) { |
| 588 | int match = URI_MATCHER.match(url); |
| 589 | switch (match) { |
| 590 | case URI_MATCH_BOOKMARKS: |
| 591 | return "vnd.android.cursor.dir/bookmark"; |
| 592 | |
| 593 | case URI_MATCH_BOOKMARKS_ID: |
| 594 | return "vnd.android.cursor.item/bookmark"; |
| 595 | |
| 596 | case URI_MATCH_SEARCHES: |
| 597 | return "vnd.android.cursor.dir/searches"; |
| 598 | |
| 599 | case URI_MATCH_SEARCHES_ID: |
| 600 | return "vnd.android.cursor.item/searches"; |
| 601 | |
| 602 | case URI_MATCH_SUGGEST: |
| 603 | return SearchManager.SUGGEST_MIME_TYPE; |
| 604 | |
| 605 | default: |
| 606 | throw new IllegalArgumentException("Unknown URL"); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | @Override |
| 611 | public Uri insert(Uri url, ContentValues initialValues) { |
| 612 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 613 | |
| 614 | int match = URI_MATCHER.match(url); |
| 615 | Uri uri = null; |
| 616 | switch (match) { |
| 617 | case URI_MATCH_BOOKMARKS: { |
| 618 | // Insert into the bookmarks table |
| 619 | long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url", |
| 620 | initialValues); |
| 621 | if (rowID > 0) { |
| 622 | uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI, |
| 623 | rowID); |
| 624 | } |
| 625 | break; |
| 626 | } |
| 627 | |
| 628 | case URI_MATCH_SEARCHES: { |
| 629 | // Insert into the searches table |
| 630 | long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url", |
| 631 | initialValues); |
| 632 | if (rowID > 0) { |
| 633 | uri = ContentUris.withAppendedId(Browser.SEARCHES_URI, |
| 634 | rowID); |
| 635 | } |
| 636 | break; |
| 637 | } |
| 638 | |
| 639 | default: |
| 640 | throw new IllegalArgumentException("Unknown URL"); |
| 641 | } |
| 642 | |
| 643 | if (uri == null) { |
| 644 | throw new IllegalArgumentException("Unknown URL"); |
| 645 | } |
| 646 | getContext().getContentResolver().notifyChange(uri, null); |
| 647 | return uri; |
| 648 | } |
| 649 | |
| 650 | @Override |
| 651 | public int delete(Uri url, String where, 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 count = db.delete(TABLE_NAMES[match % 10], where, whereArgs); |
| 672 | getContext().getContentResolver().notifyChange(url, null); |
| 673 | return count; |
| 674 | } |
| 675 | |
| 676 | @Override |
| 677 | public int update(Uri url, ContentValues values, String where, |
| 678 | String[] whereArgs) { |
| 679 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 680 | |
| 681 | int match = URI_MATCHER.match(url); |
| 682 | if (match == -1 || match == URI_MATCH_SUGGEST) { |
| 683 | throw new IllegalArgumentException("Unknown URL"); |
| 684 | } |
| 685 | |
| 686 | if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) { |
| 687 | StringBuilder sb = new StringBuilder(); |
| 688 | if (where != null && where.length() > 0) { |
| 689 | sb.append("( "); |
| 690 | sb.append(where); |
| 691 | sb.append(" ) AND "); |
| 692 | } |
| 693 | sb.append("_id = "); |
| 694 | sb.append(url.getPathSegments().get(1)); |
| 695 | where = sb.toString(); |
| 696 | } |
| 697 | |
| 698 | int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs); |
| 699 | getContext().getContentResolver().notifyChange(url, null); |
| 700 | return ret; |
| 701 | } |
| 702 | } |