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 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 21 | import android.app.SearchManager; |
| 22 | import android.content.ComponentName; |
| 23 | import android.content.ContentProvider; |
| 24 | import android.content.ContentUris; |
| 25 | import android.content.ContentValues; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 28 | import android.content.SharedPreferences; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 29 | import android.content.UriMatcher; |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 30 | import android.content.SharedPreferences.Editor; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 31 | import android.database.AbstractCursor; |
| 32 | import android.database.Cursor; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 33 | import android.database.sqlite.SQLiteDatabase; |
Bjorn Bringert | bcd20b3 | 2009-04-29 21:52:09 +0100 | [diff] [blame] | 34 | import android.database.sqlite.SQLiteOpenHelper; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 35 | import android.net.Uri; |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 36 | import android.preference.PreferenceManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 37 | import android.provider.Browser; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 38 | import android.server.search.SearchableInfo; |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 39 | import android.text.TextUtils; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 40 | import android.text.util.Regex; |
Bjorn Bringert | bcd20b3 | 2009-04-29 21:52:09 +0100 | [diff] [blame] | 41 | import android.util.Log; |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 42 | import android.util.TypedValue; |
Bjorn Bringert | bcd20b3 | 2009-04-29 21:52:09 +0100 | [diff] [blame] | 43 | |
| 44 | import java.util.Date; |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 45 | import java.util.regex.Matcher; |
| 46 | import java.util.regex.Pattern; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 47 | |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -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; |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 77 | private static final int SUGGEST_COLUMN_FORMAT = 8; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 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, |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 88 | SearchManager.SUGGEST_COLUMN_QUERY, |
| 89 | SearchManager.SUGGEST_COLUMN_FORMAT}; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 90 | |
| 91 | private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3; |
| 92 | private static final int MAX_SUGGESTION_LONG_ENTRIES = 6; |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame^] | 93 | private static final String MAX_SUGGESTION_LONG_ENTRIES_STRING = |
| 94 | Integer.valueOf(MAX_SUGGESTION_LONG_ENTRIES).toString(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 95 | |
| 96 | // make sure that these match the index of TABLE_NAMES |
| 97 | private static final int URI_MATCH_BOOKMARKS = 0; |
| 98 | private static final int URI_MATCH_SEARCHES = 1; |
| 99 | // (id % 10) should match the table name index |
| 100 | private static final int URI_MATCH_BOOKMARKS_ID = 10; |
| 101 | private static final int URI_MATCH_SEARCHES_ID = 11; |
| 102 | // |
| 103 | private static final int URI_MATCH_SUGGEST = 20; |
Bjorn Bringert | 346dafb | 2009-04-29 21:41:47 +0100 | [diff] [blame] | 104 | private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 105 | |
| 106 | private static final UriMatcher URI_MATCHER; |
| 107 | |
| 108 | static { |
| 109 | URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH); |
| 110 | URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS], |
| 111 | URI_MATCH_BOOKMARKS); |
| 112 | URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#", |
| 113 | URI_MATCH_BOOKMARKS_ID); |
| 114 | URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES], |
| 115 | URI_MATCH_SEARCHES); |
| 116 | URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#", |
| 117 | URI_MATCH_SEARCHES_ID); |
| 118 | URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY, |
| 119 | URI_MATCH_SUGGEST); |
Bjorn Bringert | 346dafb | 2009-04-29 21:41:47 +0100 | [diff] [blame] | 120 | URI_MATCHER.addURI("browser", |
| 121 | TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY, |
| 122 | URI_MATCH_BOOKMARKS_SUGGEST); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // 1 -> 2 add cache table |
| 126 | // 2 -> 3 update history table |
| 127 | // 3 -> 4 add passwords table |
| 128 | // 4 -> 5 add settings table |
| 129 | // 5 -> 6 ? |
| 130 | // 6 -> 7 ? |
| 131 | // 7 -> 8 drop proxy table |
| 132 | // 8 -> 9 drop settings table |
| 133 | // 9 -> 10 add form_urls and form_data |
| 134 | // 10 -> 11 add searches table |
| 135 | // 11 -> 12 modify cache table |
| 136 | // 12 -> 13 modify cache table |
| 137 | // 13 -> 14 correspond with Google Bookmarks schema |
| 138 | // 14 -> 15 move couple of tables to either browser private database or webview database |
| 139 | // 15 -> 17 Set it up for the SearchManager |
| 140 | // 17 -> 18 Added favicon in bookmarks table for Home shortcuts |
| 141 | // 18 -> 19 Remove labels table |
| 142 | private static final int DATABASE_VERSION = 19; |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 143 | |
| 144 | // Regular expression which matches http://, followed by some stuff, followed by |
| 145 | // optionally a trailing slash, all matched as separate groups. |
| 146 | private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?"); |
| 147 | |
| 148 | // The hex color string to be applied to urls of website suggestions, as derived from |
| 149 | // the current theme. This is not set until/unless beautifyUrl is called, at which point |
| 150 | // this variable caches the color value. |
| 151 | private static String mSearchUrlColorHex; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 152 | |
| 153 | public BrowserProvider() { |
| 154 | } |
| 155 | |
| 156 | |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 157 | private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 158 | StringBuffer sb = new StringBuffer(); |
| 159 | int lastCharLoc = 0; |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 160 | |
| 161 | final String client_id = Partner.getString(context.getContentResolver(), Partner.CLIENT_ID); |
| 162 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 163 | for (int i = 0; i < srcString.length(); ++i) { |
| 164 | char c = srcString.charAt(i); |
| 165 | if (c == '{') { |
| 166 | sb.append(srcString.subSequence(lastCharLoc, i)); |
| 167 | lastCharLoc = i; |
| 168 | inner: |
| 169 | for (int j = i; j < srcString.length(); ++j) { |
| 170 | char k = srcString.charAt(j); |
| 171 | if (k == '}') { |
| 172 | String propertyKeyValue = srcString.subSequence(i + 1, j).toString(); |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 173 | if (propertyKeyValue.equals("CLIENT_ID")) { |
| 174 | sb.append(client_id); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 175 | } else { |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 176 | sb.append("unknown"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 177 | } |
| 178 | lastCharLoc = j + 1; |
| 179 | i = j; |
| 180 | break inner; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | if (srcString.length() - lastCharLoc > 0) { |
| 186 | // Put on the tail, if there is one |
| 187 | sb.append(srcString.subSequence(lastCharLoc, srcString.length())); |
| 188 | } |
| 189 | return sb; |
| 190 | } |
| 191 | |
| 192 | private static class DatabaseHelper extends SQLiteOpenHelper { |
| 193 | private Context mContext; |
| 194 | |
| 195 | public DatabaseHelper(Context context) { |
| 196 | super(context, sDatabaseName, null, DATABASE_VERSION); |
| 197 | mContext = context; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void onCreate(SQLiteDatabase db) { |
| 202 | db.execSQL("CREATE TABLE bookmarks (" + |
| 203 | "_id INTEGER PRIMARY KEY," + |
| 204 | "title TEXT," + |
| 205 | "url TEXT," + |
| 206 | "visits INTEGER," + |
| 207 | "date LONG," + |
| 208 | "created LONG," + |
| 209 | "description TEXT," + |
| 210 | "bookmark INTEGER," + |
| 211 | "favicon BLOB DEFAULT NULL" + |
| 212 | ");"); |
| 213 | |
| 214 | final CharSequence[] bookmarks = mContext.getResources() |
| 215 | .getTextArray(R.array.bookmarks); |
| 216 | int size = bookmarks.length; |
| 217 | try { |
| 218 | for (int i = 0; i < size; i = i + 2) { |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 219 | CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 220 | db.execSQL("INSERT INTO bookmarks (title, url, visits, " + |
| 221 | "date, created, bookmark)" + " VALUES('" + |
| 222 | bookmarks[i] + "', '" + bookmarkDestination + |
| 223 | "', 0, 0, 0, 1);"); |
| 224 | } |
| 225 | } catch (ArrayIndexOutOfBoundsException e) { |
| 226 | } |
| 227 | |
| 228 | db.execSQL("CREATE TABLE searches (" + |
| 229 | "_id INTEGER PRIMARY KEY," + |
| 230 | "search TEXT," + |
| 231 | "date LONG" + |
| 232 | ");"); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 237 | Log.w(TAG, "Upgrading database from version " + oldVersion + " to " |
| 238 | + newVersion + ", which will destroy all old data"); |
| 239 | if (oldVersion == 18) { |
| 240 | db.execSQL("DROP TABLE IF EXISTS labels"); |
| 241 | } else { |
| 242 | db.execSQL("DROP TABLE IF EXISTS bookmarks"); |
| 243 | db.execSQL("DROP TABLE IF EXISTS searches"); |
| 244 | onCreate(db); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public boolean onCreate() { |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 251 | final Context context = getContext(); |
| 252 | mOpenHelper = new DatabaseHelper(context); |
| 253 | // we added "picasa web album" into default bookmarks for version 19. |
| 254 | // To avoid erasing the bookmark table, we added it explicitly for |
| 255 | // version 18 and 19 as in the other cases, we will erase the table. |
| 256 | if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) { |
| 257 | SharedPreferences p = PreferenceManager |
| 258 | .getDefaultSharedPreferences(context); |
| 259 | boolean fix = p.getBoolean("fix_picasa", true); |
| 260 | if (fix) { |
| 261 | fixPicasaBookmark(); |
| 262 | Editor ed = p.edit(); |
| 263 | ed.putBoolean("fix_picasa", false); |
| 264 | ed.commit(); |
| 265 | } |
| 266 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 267 | return true; |
| 268 | } |
| 269 | |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 270 | private void fixPicasaBookmark() { |
| 271 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 272 | Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " + |
| 273 | "bookmark = 1 AND url = ?", new String[] { PICASA_URL }); |
| 274 | try { |
| 275 | if (!cursor.moveToFirst()) { |
| 276 | // set "created" so that it will be on the top of the list |
| 277 | db.execSQL("INSERT INTO bookmarks (title, url, visits, " + |
| 278 | "date, created, bookmark)" + " VALUES('" + |
| 279 | getContext().getString(R.string.picasa) + "', '" |
| 280 | + PICASA_URL + "', 0, 0, " + new Date().getTime() |
| 281 | + ", 1);"); |
| 282 | } |
| 283 | } finally { |
| 284 | if (cursor != null) { |
| 285 | cursor.close(); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 290 | /* |
| 291 | * Subclass AbstractCursor so we can combine multiple Cursors and add |
| 292 | * "Google Search". |
| 293 | * Here are the rules. |
| 294 | * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus |
| 295 | * "Google Search"; |
| 296 | * 2. If bookmark/history entries are less than |
| 297 | * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest. |
| 298 | */ |
| 299 | private class MySuggestionCursor extends AbstractCursor { |
| 300 | private Cursor mHistoryCursor; |
| 301 | private Cursor mSuggestCursor; |
| 302 | private int mHistoryCount; |
| 303 | private int mSuggestionCount; |
| 304 | private boolean mBeyondCursor; |
| 305 | private String mString; |
| 306 | |
| 307 | public MySuggestionCursor(Cursor hc, Cursor sc, String string) { |
| 308 | mHistoryCursor = hc; |
| 309 | mSuggestCursor = sc; |
| 310 | mHistoryCount = hc.getCount(); |
| 311 | mSuggestionCount = sc != null ? sc.getCount() : 0; |
| 312 | if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) { |
| 313 | mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount; |
| 314 | } |
| 315 | mString = string; |
| 316 | mBeyondCursor = false; |
| 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public boolean onMove(int oldPosition, int newPosition) { |
| 321 | if (mHistoryCursor == null) { |
| 322 | return false; |
| 323 | } |
| 324 | if (mHistoryCount > newPosition) { |
| 325 | mHistoryCursor.moveToPosition(newPosition); |
| 326 | mBeyondCursor = false; |
| 327 | } else if (mHistoryCount + mSuggestionCount > newPosition) { |
| 328 | mSuggestCursor.moveToPosition(newPosition - mHistoryCount); |
| 329 | mBeyondCursor = false; |
| 330 | } else { |
| 331 | mBeyondCursor = true; |
| 332 | } |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public int getCount() { |
| 338 | if (mString.length() > 0) { |
| 339 | return mHistoryCount + mSuggestionCount + 1; |
| 340 | } else { |
| 341 | return mHistoryCount + mSuggestionCount; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public String[] getColumnNames() { |
| 347 | return COLUMNS; |
| 348 | } |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 349 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 350 | @Override |
| 351 | public String getString(int columnIndex) { |
| 352 | if ((mPos != -1 && mHistoryCursor != null)) { |
| 353 | switch(columnIndex) { |
| 354 | case SUGGEST_COLUMN_INTENT_ACTION_ID: |
| 355 | if (mHistoryCount > mPos) { |
| 356 | return Intent.ACTION_VIEW; |
| 357 | } else { |
| 358 | return Intent.ACTION_SEARCH; |
| 359 | } |
| 360 | |
| 361 | case SUGGEST_COLUMN_INTENT_DATA_ID: |
| 362 | if (mHistoryCount > mPos) { |
| 363 | return mHistoryCursor.getString(1); |
| 364 | } else { |
| 365 | return null; |
| 366 | } |
| 367 | |
| 368 | case SUGGEST_COLUMN_TEXT_1_ID: |
| 369 | if (mHistoryCount > mPos) { |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 370 | return getHistoryTitle(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 371 | } else if (!mBeyondCursor) { |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 372 | return mSuggestCursor.getString(1); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 373 | } else { |
| 374 | return mString; |
| 375 | } |
| 376 | |
| 377 | case SUGGEST_COLUMN_TEXT_2_ID: |
| 378 | if (mHistoryCount > mPos) { |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 379 | return getHistorySubtitle(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 380 | } else if (!mBeyondCursor) { |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 381 | return mSuggestCursor.getString(2); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 382 | } else { |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 383 | return getContext().getString(R.string.search_the_web); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | case SUGGEST_COLUMN_ICON_1_ID: |
| 387 | if (mHistoryCount > mPos) { |
| 388 | if (mHistoryCursor.getInt(3) == 1) { |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame^] | 389 | return Integer.valueOf( |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 390 | R.drawable.ic_search_category_bookmark) |
| 391 | .toString(); |
| 392 | } else { |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame^] | 393 | return Integer.valueOf( |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 394 | R.drawable.ic_search_category_history) |
| 395 | .toString(); |
| 396 | } |
| 397 | } else { |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame^] | 398 | return Integer.valueOf( |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 399 | R.drawable.ic_search_category_suggest) |
| 400 | .toString(); |
| 401 | } |
| 402 | |
| 403 | case SUGGEST_COLUMN_ICON_2_ID: |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame^] | 404 | return "0"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 405 | |
| 406 | case SUGGEST_COLUMN_QUERY_ID: |
| 407 | if (mHistoryCount > mPos) { |
| 408 | return null; |
| 409 | } else if (!mBeyondCursor) { |
| 410 | return mSuggestCursor.getString(3); |
| 411 | } else { |
| 412 | return mString; |
| 413 | } |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 414 | |
| 415 | case SUGGEST_COLUMN_FORMAT: |
| 416 | return "html"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | return null; |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public double getDouble(int column) { |
| 424 | throw new UnsupportedOperationException(); |
| 425 | } |
| 426 | |
| 427 | @Override |
| 428 | public float getFloat(int column) { |
| 429 | throw new UnsupportedOperationException(); |
| 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public int getInt(int column) { |
| 434 | throw new UnsupportedOperationException(); |
| 435 | } |
| 436 | |
| 437 | @Override |
| 438 | public long getLong(int column) { |
| 439 | if ((mPos != -1) && column == 0) { |
| 440 | return mPos; // use row# as the _Id |
| 441 | } |
| 442 | throw new UnsupportedOperationException(); |
| 443 | } |
| 444 | |
| 445 | @Override |
| 446 | public short getShort(int column) { |
| 447 | throw new UnsupportedOperationException(); |
| 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public boolean isNull(int column) { |
| 452 | throw new UnsupportedOperationException(); |
| 453 | } |
| 454 | |
| 455 | // TODO Temporary change, finalize after jq's changes go in |
| 456 | public void deactivate() { |
| 457 | if (mHistoryCursor != null) { |
| 458 | mHistoryCursor.deactivate(); |
| 459 | } |
| 460 | if (mSuggestCursor != null) { |
| 461 | mSuggestCursor.deactivate(); |
| 462 | } |
| 463 | super.deactivate(); |
| 464 | } |
| 465 | |
| 466 | public boolean requery() { |
| 467 | return (mHistoryCursor != null ? mHistoryCursor.requery() : false) | |
| 468 | (mSuggestCursor != null ? mSuggestCursor.requery() : false); |
| 469 | } |
| 470 | |
| 471 | // TODO Temporary change, finalize after jq's changes go in |
| 472 | public void close() { |
| 473 | super.close(); |
| 474 | if (mHistoryCursor != null) { |
| 475 | mHistoryCursor.close(); |
| 476 | mHistoryCursor = null; |
| 477 | } |
| 478 | if (mSuggestCursor != null) { |
| 479 | mSuggestCursor.close(); |
| 480 | mSuggestCursor = null; |
| 481 | } |
| 482 | } |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 483 | |
| 484 | /** |
| 485 | * Provides the title (text line 1) for a browser suggestion, which should be the |
| 486 | * webpage title. If the webpage title is empty, returns the stripped url instead. |
| 487 | * |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 488 | * @return the title string to use |
| 489 | */ |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 490 | private String getHistoryTitle() { |
| 491 | String title = mHistoryCursor.getString(2 /* webpage title */); |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 492 | if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) { |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 493 | title = beautifyUrl(mHistoryCursor.getString(1 /* url */)); |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 494 | } |
| 495 | return title; |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Provides the subtitle (text line 2) for a browser suggestion, which should be the |
| 500 | * webpage url. If the webpage title is empty, then the url should go in the title |
| 501 | * instead, and the subtitle should be empty, so this would return null. |
| 502 | * |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 503 | * @return the subtitle string to use, or null if none |
| 504 | */ |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 505 | private String getHistorySubtitle() { |
| 506 | String title = mHistoryCursor.getString(2 /* webpage title */); |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 507 | if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) { |
| 508 | return null; |
| 509 | } else { |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 510 | return beautifyUrl(mHistoryCursor.getString(1 /* url */)); |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | |
| 514 | /** |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 515 | * Strips "http://" from the beginning of a url and "/" from the end, |
| 516 | * and adds html formatting to make it green. |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 517 | */ |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 518 | private String beautifyUrl(String url) { |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 519 | if (mSearchUrlColorHex == null) { |
| 520 | // Get the color used for this purpose from the current theme. |
| 521 | TypedValue colorValue = new TypedValue(); |
| 522 | getContext().getTheme().resolveAttribute( |
| 523 | com.android.internal.R.attr.textColorSearchUrl, colorValue, true); |
| 524 | int color = getContext().getResources().getColor(colorValue.resourceId); |
| 525 | |
| 526 | // Convert the int color value into a hex string, and strip the first two |
| 527 | // characters which will be the alpha transparency (html doesn't want this). |
| 528 | mSearchUrlColorHex = Integer.toHexString(color).substring(2); |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 529 | } |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 530 | |
| 531 | return "<font color=\"#" + mSearchUrlColorHex + "\">" + stripUrl(url) + "</font>"; |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 532 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | @Override |
| 536 | public Cursor query(Uri url, String[] projectionIn, String selection, |
| 537 | String[] selectionArgs, String sortOrder) |
| 538 | throws IllegalStateException { |
| 539 | SQLiteDatabase db = mOpenHelper.getReadableDatabase(); |
| 540 | |
| 541 | int match = URI_MATCHER.match(url); |
| 542 | if (match == -1) { |
| 543 | throw new IllegalArgumentException("Unknown URL"); |
| 544 | } |
| 545 | |
Bjorn Bringert | 346dafb | 2009-04-29 21:41:47 +0100 | [diff] [blame] | 546 | if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 547 | String suggestSelection; |
| 548 | String [] myArgs; |
| 549 | if (selectionArgs[0] == null || selectionArgs[0].equals("")) { |
| 550 | suggestSelection = null; |
| 551 | myArgs = null; |
| 552 | } else { |
| 553 | String like = selectionArgs[0] + "%"; |
Leon Scroggins | faa15db | 2009-04-03 10:16:06 -0700 | [diff] [blame] | 554 | if (selectionArgs[0].startsWith("http") |
| 555 | || selectionArgs[0].startsWith("file")) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 556 | myArgs = new String[1]; |
| 557 | myArgs[0] = like; |
| 558 | suggestSelection = selection; |
| 559 | } else { |
| 560 | SUGGEST_ARGS[0] = "http://" + like; |
| 561 | SUGGEST_ARGS[1] = "http://www." + like; |
| 562 | SUGGEST_ARGS[2] = "https://" + like; |
| 563 | SUGGEST_ARGS[3] = "https://www." + like; |
| 564 | myArgs = SUGGEST_ARGS; |
| 565 | suggestSelection = SUGGEST_SELECTION; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS], |
| 570 | SUGGEST_PROJECTION, suggestSelection, myArgs, null, null, |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame^] | 571 | ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 572 | |
Bjorn Bringert | 346dafb | 2009-04-29 21:41:47 +0100 | [diff] [blame] | 573 | if (match == URI_MATCH_BOOKMARKS_SUGGEST |
| 574 | || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 575 | return new MySuggestionCursor(c, null, ""); |
| 576 | } else { |
| 577 | // get Google suggest if there is still space in the list |
| 578 | if (myArgs != null && myArgs.length > 1 |
| 579 | && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) { |
Bjorn Bringert | 24e1ec6 | 2009-04-29 16:10:43 +0100 | [diff] [blame] | 580 | // TODO: This shouldn't be hard-coded. Instead, it should use the |
| 581 | // default web search provider. But the API for that is not implemented yet. |
| 582 | ComponentName googleSearchComponent = |
| 583 | new ComponentName("com.android.googlesearch", |
| 584 | "com.android.googlesearch.GoogleSearch"); |
| 585 | SearchableInfo si = |
| 586 | SearchManager.getSearchableInfo(googleSearchComponent, false); |
| 587 | Cursor sc = SearchManager.getSuggestions(getContext(), si, selectionArgs[0]); |
| 588 | return new MySuggestionCursor(c, sc, selectionArgs[0]); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 589 | } |
| 590 | return new MySuggestionCursor(c, null, selectionArgs[0]); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | String[] projection = null; |
| 595 | if (projectionIn != null && projectionIn.length > 0) { |
| 596 | projection = new String[projectionIn.length + 1]; |
| 597 | System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length); |
| 598 | projection[projectionIn.length] = "_id AS _id"; |
| 599 | } |
| 600 | |
| 601 | StringBuilder whereClause = new StringBuilder(256); |
| 602 | if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) { |
| 603 | whereClause.append("(_id = ").append(url.getPathSegments().get(1)) |
| 604 | .append(")"); |
| 605 | } |
| 606 | |
| 607 | // Tack on the user's selection, if present |
| 608 | if (selection != null && selection.length() > 0) { |
| 609 | if (whereClause.length() > 0) { |
| 610 | whereClause.append(" AND "); |
| 611 | } |
| 612 | |
| 613 | whereClause.append('('); |
| 614 | whereClause.append(selection); |
| 615 | whereClause.append(')'); |
| 616 | } |
| 617 | Cursor c = db.query(TABLE_NAMES[match % 10], projection, |
| 618 | whereClause.toString(), selectionArgs, null, null, sortOrder, |
| 619 | null); |
| 620 | c.setNotificationUri(getContext().getContentResolver(), url); |
| 621 | return c; |
| 622 | } |
| 623 | |
| 624 | @Override |
| 625 | public String getType(Uri url) { |
| 626 | int match = URI_MATCHER.match(url); |
| 627 | switch (match) { |
| 628 | case URI_MATCH_BOOKMARKS: |
| 629 | return "vnd.android.cursor.dir/bookmark"; |
| 630 | |
| 631 | case URI_MATCH_BOOKMARKS_ID: |
| 632 | return "vnd.android.cursor.item/bookmark"; |
| 633 | |
| 634 | case URI_MATCH_SEARCHES: |
| 635 | return "vnd.android.cursor.dir/searches"; |
| 636 | |
| 637 | case URI_MATCH_SEARCHES_ID: |
| 638 | return "vnd.android.cursor.item/searches"; |
| 639 | |
| 640 | case URI_MATCH_SUGGEST: |
| 641 | return SearchManager.SUGGEST_MIME_TYPE; |
| 642 | |
| 643 | default: |
| 644 | throw new IllegalArgumentException("Unknown URL"); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | @Override |
| 649 | public Uri insert(Uri url, ContentValues initialValues) { |
| 650 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 651 | |
| 652 | int match = URI_MATCHER.match(url); |
| 653 | Uri uri = null; |
| 654 | switch (match) { |
| 655 | case URI_MATCH_BOOKMARKS: { |
| 656 | // Insert into the bookmarks table |
| 657 | long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url", |
| 658 | initialValues); |
| 659 | if (rowID > 0) { |
| 660 | uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI, |
| 661 | rowID); |
| 662 | } |
| 663 | break; |
| 664 | } |
| 665 | |
| 666 | case URI_MATCH_SEARCHES: { |
| 667 | // Insert into the searches table |
| 668 | long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url", |
| 669 | initialValues); |
| 670 | if (rowID > 0) { |
| 671 | uri = ContentUris.withAppendedId(Browser.SEARCHES_URI, |
| 672 | rowID); |
| 673 | } |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | default: |
| 678 | throw new IllegalArgumentException("Unknown URL"); |
| 679 | } |
| 680 | |
| 681 | if (uri == null) { |
| 682 | throw new IllegalArgumentException("Unknown URL"); |
| 683 | } |
| 684 | getContext().getContentResolver().notifyChange(uri, null); |
| 685 | return uri; |
| 686 | } |
| 687 | |
| 688 | @Override |
| 689 | public int delete(Uri url, String where, 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 count = db.delete(TABLE_NAMES[match % 10], where, whereArgs); |
| 710 | getContext().getContentResolver().notifyChange(url, null); |
| 711 | return count; |
| 712 | } |
| 713 | |
| 714 | @Override |
| 715 | public int update(Uri url, ContentValues values, String where, |
| 716 | String[] whereArgs) { |
| 717 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 718 | |
| 719 | int match = URI_MATCHER.match(url); |
| 720 | if (match == -1 || match == URI_MATCH_SUGGEST) { |
| 721 | throw new IllegalArgumentException("Unknown URL"); |
| 722 | } |
| 723 | |
| 724 | if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) { |
| 725 | StringBuilder sb = new StringBuilder(); |
| 726 | if (where != null && where.length() > 0) { |
| 727 | sb.append("( "); |
| 728 | sb.append(where); |
| 729 | sb.append(" ) AND "); |
| 730 | } |
| 731 | sb.append("_id = "); |
| 732 | sb.append(url.getPathSegments().get(1)); |
| 733 | where = sb.toString(); |
| 734 | } |
| 735 | |
| 736 | int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs); |
| 737 | getContext().getContentResolver().notifyChange(url, null); |
| 738 | return ret; |
| 739 | } |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 740 | |
| 741 | /** |
| 742 | * Strips the provided url of preceding "http://" and any trailing "/". Does not |
| 743 | * strip "https://". If the provided string cannot be stripped, the original string |
| 744 | * is returned. |
| 745 | * |
| 746 | * TODO: Put this in TextUtils to be used by other packages doing something similar. |
| 747 | * |
| 748 | * @param url a url to strip, like "http://www.google.com/" |
| 749 | * @return a stripped url like "www.google.com", or the original string if it could |
| 750 | * not be stripped |
| 751 | */ |
| 752 | private static String stripUrl(String url) { |
| 753 | if (url == null) return null; |
| 754 | Matcher m = STRIP_URL_PATTERN.matcher(url); |
| 755 | if (m.matches() && m.groupCount() == 3) { |
| 756 | return m.group(2); |
| 757 | } else { |
| 758 | return url; |
| 759 | } |
| 760 | } |
| 761 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 762 | } |