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