blob: cc96b9cf058059156293e22a092c54b0e40c22c9 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.app.SearchManager;
Bjorn Bringertabc3ac82009-12-04 12:59:14 +000020import android.app.SearchableInfo;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070021import android.backup.BackupManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.content.ComponentName;
23import android.content.ContentProvider;
Christopher Tatef0c36f72009-07-28 15:24:05 -070024import android.content.ContentResolver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.content.ContentUris;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070029import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.content.UriMatcher;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070031import android.content.SharedPreferences.Editor;
Satish Sampath565505b2009-05-29 15:37:27 +010032import android.content.pm.PackageManager;
33import android.content.pm.ResolveInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.database.AbstractCursor;
Leon Scroggins62b71f72009-06-12 17:51:22 -040035import android.database.ContentObserver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.database.sqlite.SQLiteDatabase;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010038import android.database.sqlite.SQLiteOpenHelper;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.net.Uri;
Andrei Popescu1b20b9d2009-09-21 18:49:42 +010040import android.os.AsyncTask;
Leon Scroggins62b71f72009-06-12 17:51:22 -040041import android.os.Handler;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070042import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.provider.Browser;
Leon Scroggins62b71f72009-06-12 17:51:22 -040044import android.provider.Settings;
Christopher Tatef0c36f72009-07-28 15:24:05 -070045import android.provider.Browser.BookmarkColumns;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050046import android.speech.RecognizerResultsIntent;
Mike LeBeau21beb132009-05-13 14:57:50 -070047import android.text.TextUtils;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010048import android.util.Log;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070049import android.util.TypedValue;
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +000050import android.webkit.GeolocationPermissions;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010051
Dan Egnor5ee906c2009-11-18 12:11:49 -080052import com.android.common.Patterns;
53
Jeff Hamilton4db482f2010-02-03 23:30:25 -060054import com.google.android.gsf.GoogleSettingsContract.Partner;
Dan Egnor5ee906c2009-11-18 12:11:49 -080055
Andrei Popescu1b20b9d2009-09-21 18:49:42 +010056import java.io.File;
57import java.io.FilenameFilter;
Leon Scroggins58d56c62010-01-28 15:12:40 -050058import java.util.ArrayList;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010059import java.util.Date;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070060import java.util.regex.Matcher;
61import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -080062
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070063
The Android Open Source Project0c908882009-03-03 19:32:16 -080064public class BrowserProvider extends ContentProvider {
65
66 private SQLiteOpenHelper mOpenHelper;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070067 private BackupManager mBackupManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080068 private static final String sDatabaseName = "browser.db";
69 private static final String TAG = "BrowserProvider";
70 private static final String ORDER_BY = "visits DESC, date DESC";
71
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070072 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
73 "viewer?source=androidclient";
74
The Android Open Source Project0c908882009-03-03 19:32:16 -080075 private static final String[] TABLE_NAMES = new String[] {
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +000076 "bookmarks", "searches", "geolocation"
The Android Open Source Project0c908882009-03-03 19:32:16 -080077 };
78 private static final String[] SUGGEST_PROJECTION = new String[] {
Leon Scrogginsb4464432009-11-25 12:37:50 -050079 "_id", "url", "title", "bookmark", "user_entered"
The Android Open Source Project0c908882009-03-03 19:32:16 -080080 };
Satish Sampath565505b2009-05-29 15:37:27 +010081 private static final String SUGGEST_SELECTION =
Leon Scrogginsb4464432009-11-25 12:37:50 -050082 "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?"
83 + " OR title LIKE ?) AND (bookmark = 1 OR user_entered = 1)";
Leon Scrogginsbd359cc2009-05-26 15:57:35 -040084 private String[] SUGGEST_ARGS = new String[5];
The Android Open Source Project0c908882009-03-03 19:32:16 -080085
86 // shared suggestion array index, make sure to match COLUMNS
87 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
88 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
89 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
90 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
91 private static final int SUGGEST_COLUMN_ICON_1_ID = 5;
92 private static final int SUGGEST_COLUMN_ICON_2_ID = 6;
93 private static final int SUGGEST_COLUMN_QUERY_ID = 7;
Mike LeBeau1ef26a32009-05-13 20:11:00 -070094 private static final int SUGGEST_COLUMN_FORMAT = 8;
Bjorn Bringert04851702009-09-22 10:36:01 +010095 private static final int SUGGEST_COLUMN_INTENT_EXTRA_DATA = 9;
The Android Open Source Project0c908882009-03-03 19:32:16 -080096
97 // shared suggestion columns
98 private static final String[] COLUMNS = new String[] {
99 "_id",
100 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
101 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
102 SearchManager.SUGGEST_COLUMN_TEXT_1,
103 SearchManager.SUGGEST_COLUMN_TEXT_2,
104 SearchManager.SUGGEST_COLUMN_ICON_1,
105 SearchManager.SUGGEST_COLUMN_ICON_2,
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700106 SearchManager.SUGGEST_COLUMN_QUERY,
Bjorn Bringert04851702009-09-22 10:36:01 +0100107 SearchManager.SUGGEST_COLUMN_FORMAT,
108 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA};
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109
110 private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3;
111 private static final int MAX_SUGGESTION_LONG_ENTRIES = 6;
Leon Scroggins31887fd2009-05-18 16:58:08 -0400112 private static final String MAX_SUGGESTION_LONG_ENTRIES_STRING =
113 Integer.valueOf(MAX_SUGGESTION_LONG_ENTRIES).toString();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114
115 // make sure that these match the index of TABLE_NAMES
116 private static final int URI_MATCH_BOOKMARKS = 0;
117 private static final int URI_MATCH_SEARCHES = 1;
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000118 private static final int URI_MATCH_GEOLOCATION = 2;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119 // (id % 10) should match the table name index
120 private static final int URI_MATCH_BOOKMARKS_ID = 10;
121 private static final int URI_MATCH_SEARCHES_ID = 11;
122 //
123 private static final int URI_MATCH_SUGGEST = 20;
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100124 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800125
126 private static final UriMatcher URI_MATCHER;
127
128 static {
129 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
130 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
131 URI_MATCH_BOOKMARKS);
132 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
133 URI_MATCH_BOOKMARKS_ID);
134 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
135 URI_MATCH_SEARCHES);
136 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
137 URI_MATCH_SEARCHES_ID);
138 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
139 URI_MATCH_SUGGEST);
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100140 URI_MATCHER.addURI("browser",
141 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
142 URI_MATCH_BOOKMARKS_SUGGEST);
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000143 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_GEOLOCATION],
144 URI_MATCH_GEOLOCATION);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800145 }
146
147 // 1 -> 2 add cache table
148 // 2 -> 3 update history table
149 // 3 -> 4 add passwords table
150 // 4 -> 5 add settings table
151 // 5 -> 6 ?
152 // 6 -> 7 ?
153 // 7 -> 8 drop proxy table
154 // 8 -> 9 drop settings table
155 // 9 -> 10 add form_urls and form_data
156 // 10 -> 11 add searches table
157 // 11 -> 12 modify cache table
158 // 12 -> 13 modify cache table
159 // 13 -> 14 correspond with Google Bookmarks schema
160 // 14 -> 15 move couple of tables to either browser private database or webview database
161 // 15 -> 17 Set it up for the SearchManager
162 // 17 -> 18 Added favicon in bookmarks table for Home shortcuts
163 // 18 -> 19 Remove labels table
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400164 // 19 -> 20 Added thumbnail
Patrick Scott3918d442009-08-04 13:22:29 -0400165 // 20 -> 21 Added touch_icon
Grace Kloba6b52a552009-09-03 16:29:56 -0700166 // 21 -> 22 Remove "clientid"
Leon Scrogginsb4464432009-11-25 12:37:50 -0500167 // 22 -> 23 Added user_entered
168 private static final int DATABASE_VERSION = 23;
Satish Sampath565505b2009-05-29 15:37:27 +0100169
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700170 // Regular expression which matches http://, followed by some stuff, followed by
171 // optionally a trailing slash, all matched as separate groups.
172 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
Satish Sampath565505b2009-05-29 15:37:27 +0100173
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100174 private SearchManager mSearchManager;
175
Satish Sampath60d24e22009-07-09 16:46:08 +0100176 // The ID of the ColorStateList to be applied to urls of website suggestions, as derived from
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700177 // the current theme. This is not set until/unless beautifyUrl is called, at which point
178 // this variable caches the color value.
Satish Sampath60d24e22009-07-09 16:46:08 +0100179 private static String mSearchUrlColorId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800180
181 public BrowserProvider() {
182 }
Satish Sampath565505b2009-05-29 15:37:27 +0100183
The Android Open Source Project0c908882009-03-03 19:32:16 -0800184
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700185 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 StringBuffer sb = new StringBuffer();
187 int lastCharLoc = 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100188
Ramanan Rajeswaran378e5722009-07-01 08:46:58 -0700189 final String client_id = Partner.getString(context.getContentResolver(),
190 Partner.CLIENT_ID, "android-google");
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700191
The Android Open Source Project0c908882009-03-03 19:32:16 -0800192 for (int i = 0; i < srcString.length(); ++i) {
193 char c = srcString.charAt(i);
194 if (c == '{') {
195 sb.append(srcString.subSequence(lastCharLoc, i));
196 lastCharLoc = i;
197 inner:
198 for (int j = i; j < srcString.length(); ++j) {
199 char k = srcString.charAt(j);
200 if (k == '}') {
201 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700202 if (propertyKeyValue.equals("CLIENT_ID")) {
203 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800204 } else {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700205 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800206 }
207 lastCharLoc = j + 1;
208 i = j;
209 break inner;
210 }
211 }
212 }
213 }
214 if (srcString.length() - lastCharLoc > 0) {
215 // Put on the tail, if there is one
216 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
217 }
218 return sb;
219 }
220
221 private static class DatabaseHelper extends SQLiteOpenHelper {
222 private Context mContext;
223
224 public DatabaseHelper(Context context) {
225 super(context, sDatabaseName, null, DATABASE_VERSION);
226 mContext = context;
227 }
228
229 @Override
230 public void onCreate(SQLiteDatabase db) {
231 db.execSQL("CREATE TABLE bookmarks (" +
232 "_id INTEGER PRIMARY KEY," +
233 "title TEXT," +
234 "url TEXT," +
235 "visits INTEGER," +
236 "date LONG," +
237 "created LONG," +
238 "description TEXT," +
239 "bookmark INTEGER," +
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400240 "favicon BLOB DEFAULT NULL," +
Patrick Scott3918d442009-08-04 13:22:29 -0400241 "thumbnail BLOB DEFAULT NULL," +
Leon Scrogginsb4464432009-11-25 12:37:50 -0500242 "touch_icon BLOB DEFAULT NULL," +
243 "user_entered INTEGER" +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800244 ");");
245
246 final CharSequence[] bookmarks = mContext.getResources()
247 .getTextArray(R.array.bookmarks);
248 int size = bookmarks.length;
249 try {
250 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700251 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800252 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
253 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100254 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800255 "', 0, 0, 0, 1);");
256 }
257 } catch (ArrayIndexOutOfBoundsException e) {
258 }
259
260 db.execSQL("CREATE TABLE searches (" +
261 "_id INTEGER PRIMARY KEY," +
262 "search TEXT," +
263 "date LONG" +
264 ");");
265 }
266
267 @Override
268 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
269 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400270 + newVersion);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800271 if (oldVersion == 18) {
272 db.execSQL("DROP TABLE IF EXISTS labels");
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400273 }
274 if (oldVersion <= 19) {
275 db.execSQL("ALTER TABLE bookmarks ADD COLUMN thumbnail BLOB DEFAULT NULL;");
Patrick Scott3918d442009-08-04 13:22:29 -0400276 }
277 if (oldVersion < 21) {
278 db.execSQL("ALTER TABLE bookmarks ADD COLUMN touch_icon BLOB DEFAULT NULL;");
Grace Kloba6b52a552009-09-03 16:29:56 -0700279 }
280 if (oldVersion < 22) {
281 db.execSQL("DELETE FROM bookmarks WHERE (bookmark = 0 AND url LIKE \"%.google.%client=ms-%\")");
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100282 removeGears();
Leon Scrogginsb4464432009-11-25 12:37:50 -0500283 }
284 if (oldVersion < 23) {
285 db.execSQL("ALTER TABLE bookmarks ADD COLUMN user_entered INTEGER;");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800286 } else {
287 db.execSQL("DROP TABLE IF EXISTS bookmarks");
288 db.execSQL("DROP TABLE IF EXISTS searches");
289 onCreate(db);
290 }
291 }
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100292
293 private void removeGears() {
294 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
295 public Void doInBackground(Void... unused) {
296 String browserDataDirString = mContext.getApplicationInfo().dataDir;
297 final String appPluginsDirString = "app_plugins";
298 final String gearsPrefix = "gears";
299 File appPluginsDir = new File(browserDataDirString + File.separator
300 + appPluginsDirString);
301 if (!appPluginsDir.exists()) {
302 return null;
303 }
304 // Delete the Gears plugin files
305 File[] gearsFiles = appPluginsDir.listFiles(new FilenameFilter() {
306 public boolean accept(File dir, String filename) {
307 return filename.startsWith(gearsPrefix);
308 }
309 });
310 for (int i = 0; i < gearsFiles.length; ++i) {
311 if (gearsFiles[i].isDirectory()) {
312 deleteDirectory(gearsFiles[i]);
313 } else {
314 gearsFiles[i].delete();
315 }
316 }
317 // Delete the Gears data files
318 File gearsDataDir = new File(browserDataDirString + File.separator
319 + gearsPrefix);
320 if (!gearsDataDir.exists()) {
321 return null;
322 }
323 deleteDirectory(gearsDataDir);
324 return null;
325 }
326
327 private void deleteDirectory(File currentDir) {
328 File[] files = currentDir.listFiles();
329 for (int i = 0; i < files.length; ++i) {
330 if (files[i].isDirectory()) {
331 deleteDirectory(files[i]);
332 }
333 files[i].delete();
334 }
335 currentDir.delete();
336 }
337 };
338
339 task.execute();
340 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800341 }
342
343 @Override
344 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700345 final Context context = getContext();
346 mOpenHelper = new DatabaseHelper(context);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700347 mBackupManager = new BackupManager(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100348 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700349 // To avoid erasing the bookmark table, we added it explicitly for
350 // version 18 and 19 as in the other cases, we will erase the table.
351 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
352 SharedPreferences p = PreferenceManager
353 .getDefaultSharedPreferences(context);
354 boolean fix = p.getBoolean("fix_picasa", true);
355 if (fix) {
356 fixPicasaBookmark();
357 Editor ed = p.edit();
358 ed.putBoolean("fix_picasa", false);
359 ed.commit();
360 }
361 }
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100362 mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400363 mShowWebSuggestionsSettingChangeObserver
364 = new ShowWebSuggestionsSettingChangeObserver();
365 context.getContentResolver().registerContentObserver(
366 Settings.System.getUriFor(
367 Settings.System.SHOW_WEB_SUGGESTIONS),
368 true, mShowWebSuggestionsSettingChangeObserver);
369 updateShowWebSuggestions();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800370 return true;
371 }
372
Leon Scroggins62b71f72009-06-12 17:51:22 -0400373 /**
374 * This Observer will ensure that if the user changes the system
375 * setting of whether to display web suggestions, we will
376 * change accordingly.
377 */
378 /* package */ class ShowWebSuggestionsSettingChangeObserver
379 extends ContentObserver {
380 public ShowWebSuggestionsSettingChangeObserver() {
381 super(new Handler());
382 }
383
384 @Override
385 public void onChange(boolean selfChange) {
386 updateShowWebSuggestions();
387 }
388 }
389
390 private ShowWebSuggestionsSettingChangeObserver
391 mShowWebSuggestionsSettingChangeObserver;
392
393 // If non-null, then the system is set to show web suggestions,
394 // and this is the SearchableInfo to use to get them.
395 private SearchableInfo mSearchableInfo;
396
397 /**
398 * Check the system settings to see whether web suggestions are
399 * allowed. If so, store the SearchableInfo to grab suggestions
400 * while the user is typing.
401 */
402 private void updateShowWebSuggestions() {
403 mSearchableInfo = null;
404 Context context = getContext();
405 if (Settings.System.getInt(context.getContentResolver(),
406 Settings.System.SHOW_WEB_SUGGESTIONS,
407 1 /* default on */) == 1) {
408 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
409 intent.addCategory(Intent.CATEGORY_DEFAULT);
410 ResolveInfo info = context.getPackageManager().resolveActivity(
411 intent, PackageManager.MATCH_DEFAULT_ONLY);
412 if (info != null) {
413 ComponentName googleSearchComponent =
414 new ComponentName(info.activityInfo.packageName,
415 info.activityInfo.name);
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100416 mSearchableInfo = mSearchManager.getSearchableInfo(
Leon Scroggins62b71f72009-06-12 17:51:22 -0400417 googleSearchComponent, false);
418 }
419 }
420 }
421
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700422 private void fixPicasaBookmark() {
423 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
424 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
425 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
426 try {
427 if (!cursor.moveToFirst()) {
428 // set "created" so that it will be on the top of the list
429 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
430 "date, created, bookmark)" + " VALUES('" +
431 getContext().getString(R.string.picasa) + "', '"
432 + PICASA_URL + "', 0, 0, " + new Date().getTime()
433 + ", 1);");
434 }
435 } finally {
436 if (cursor != null) {
437 cursor.close();
438 }
439 }
440 }
441
The Android Open Source Project0c908882009-03-03 19:32:16 -0800442 /*
443 * Subclass AbstractCursor so we can combine multiple Cursors and add
444 * "Google Search".
445 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100446 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
The Android Open Source Project0c908882009-03-03 19:32:16 -0800447 * "Google Search";
Satish Sampath565505b2009-05-29 15:37:27 +0100448 * 2. If bookmark/history entries are less than
The Android Open Source Project0c908882009-03-03 19:32:16 -0800449 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
450 */
451 private class MySuggestionCursor extends AbstractCursor {
452 private Cursor mHistoryCursor;
453 private Cursor mSuggestCursor;
454 private int mHistoryCount;
455 private int mSuggestionCount;
Grace Klobad3992d42010-01-28 11:44:38 -0800456 private boolean mIncludeWebSearch;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800457 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100458 private int mSuggestText1Id;
459 private int mSuggestText2Id;
460 private int mSuggestQueryId;
Bjorn Bringert04851702009-09-22 10:36:01 +0100461 private int mSuggestIntentExtraDataId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800462
463 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
464 mHistoryCursor = hc;
465 mSuggestCursor = sc;
466 mHistoryCount = hc.getCount();
467 mSuggestionCount = sc != null ? sc.getCount() : 0;
468 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
469 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
470 }
471 mString = string;
Grace Klobad3992d42010-01-28 11:44:38 -0800472 mIncludeWebSearch = string.length() > 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100473
474 // Some web suggest providers only give suggestions and have no description string for
475 // items. The order of the result columns may be different as well. So retrieve the
476 // column indices for the fields we need now and check before using below.
477 if (mSuggestCursor == null) {
478 mSuggestText1Id = -1;
479 mSuggestText2Id = -1;
480 mSuggestQueryId = -1;
Bjorn Bringert04851702009-09-22 10:36:01 +0100481 mSuggestIntentExtraDataId = -1;
Satish Sampath565505b2009-05-29 15:37:27 +0100482 } else {
483 mSuggestText1Id = mSuggestCursor.getColumnIndex(
484 SearchManager.SUGGEST_COLUMN_TEXT_1);
485 mSuggestText2Id = mSuggestCursor.getColumnIndex(
486 SearchManager.SUGGEST_COLUMN_TEXT_2);
487 mSuggestQueryId = mSuggestCursor.getColumnIndex(
488 SearchManager.SUGGEST_COLUMN_QUERY);
Bjorn Bringert04851702009-09-22 10:36:01 +0100489 mSuggestIntentExtraDataId = mSuggestCursor.getColumnIndex(
490 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA);
Satish Sampath565505b2009-05-29 15:37:27 +0100491 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800492 }
493
494 @Override
495 public boolean onMove(int oldPosition, int newPosition) {
496 if (mHistoryCursor == null) {
497 return false;
498 }
Grace Klobad3992d42010-01-28 11:44:38 -0800499 if (mIncludeWebSearch) {
500 if (newPosition == 0) {
501 return true;
502 } else {
503 newPosition--;
504 }
505 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800506 if (mHistoryCount > newPosition) {
507 mHistoryCursor.moveToPosition(newPosition);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800508 } else {
Grace Klobad3992d42010-01-28 11:44:38 -0800509 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800510 }
511 return true;
512 }
513
514 @Override
515 public int getCount() {
Grace Klobad3992d42010-01-28 11:44:38 -0800516 if (mIncludeWebSearch) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800517 return mHistoryCount + mSuggestionCount + 1;
518 } else {
519 return mHistoryCount + mSuggestionCount;
520 }
521 }
522
523 @Override
524 public String[] getColumnNames() {
525 return COLUMNS;
526 }
Satish Sampath565505b2009-05-29 15:37:27 +0100527
The Android Open Source Project0c908882009-03-03 19:32:16 -0800528 @Override
529 public String getString(int columnIndex) {
530 if ((mPos != -1 && mHistoryCursor != null)) {
Grace Klobad3992d42010-01-28 11:44:38 -0800531 int position = mIncludeWebSearch ? mPos - 1 : mPos;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800532 switch(columnIndex) {
533 case SUGGEST_COLUMN_INTENT_ACTION_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800534 if (position >= 0 && position < mHistoryCount) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800535 return Intent.ACTION_VIEW;
536 } else {
537 return Intent.ACTION_SEARCH;
538 }
539
540 case SUGGEST_COLUMN_INTENT_DATA_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800541 if (position >= 0 && position < mHistoryCount) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800542 return mHistoryCursor.getString(1);
543 } else {
544 return null;
545 }
546
547 case SUGGEST_COLUMN_TEXT_1_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800548 if (position < 0) {
549 return mString;
550 } else if (mHistoryCount > position) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700551 return getHistoryTitle();
Grace Klobad3992d42010-01-28 11:44:38 -0800552 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100553 if (mSuggestText1Id == -1) return null;
554 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800555 }
556
557 case SUGGEST_COLUMN_TEXT_2_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800558 if (position < 0) {
559 return getContext().getString(R.string.search_the_web);
560 } else if (mHistoryCount > position) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700561 return getHistorySubtitle();
Grace Klobad3992d42010-01-28 11:44:38 -0800562 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100563 if (mSuggestText2Id == -1) return null;
564 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800565 }
566
567 case SUGGEST_COLUMN_ICON_1_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800568 if (position >= 0 && position < mHistoryCount) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800569 if (mHistoryCursor.getInt(3) == 1) {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400570 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800571 R.drawable.ic_search_category_bookmark)
572 .toString();
573 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400574 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800575 R.drawable.ic_search_category_history)
576 .toString();
577 }
578 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400579 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800580 R.drawable.ic_search_category_suggest)
581 .toString();
582 }
583
584 case SUGGEST_COLUMN_ICON_2_ID:
Leon Scroggins31887fd2009-05-18 16:58:08 -0400585 return "0";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800586
587 case SUGGEST_COLUMN_QUERY_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800588 if (position < 0) {
589 return mString;
590 } else if (mHistoryCount > position) {
Mike LeBeau2af73052009-06-23 17:36:59 -0700591 // Return the url in the intent query column. This is ignored
592 // within the browser because our searchable is set to
593 // android:searchMode="queryRewriteFromData", but it is used by
594 // global search for query rewriting.
595 return mHistoryCursor.getString(1);
Grace Klobad3992d42010-01-28 11:44:38 -0800596 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100597 if (mSuggestQueryId == -1) return null;
598 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800599 }
Satish Sampath565505b2009-05-29 15:37:27 +0100600
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700601 case SUGGEST_COLUMN_FORMAT:
602 return "html";
Bjorn Bringert04851702009-09-22 10:36:01 +0100603
604 case SUGGEST_COLUMN_INTENT_EXTRA_DATA:
Grace Klobad3992d42010-01-28 11:44:38 -0800605 if (position < 0) {
Bjorn Bringert04851702009-09-22 10:36:01 +0100606 return null;
Grace Klobad3992d42010-01-28 11:44:38 -0800607 } else if (mHistoryCount > position) {
608 return null;
609 } else {
Bjorn Bringert04851702009-09-22 10:36:01 +0100610 if (mSuggestIntentExtraDataId == -1) return null;
611 return mSuggestCursor.getString(mSuggestIntentExtraDataId);
Bjorn Bringert04851702009-09-22 10:36:01 +0100612 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800613 }
614 }
615 return null;
616 }
617
618 @Override
619 public double getDouble(int column) {
620 throw new UnsupportedOperationException();
621 }
622
623 @Override
624 public float getFloat(int column) {
625 throw new UnsupportedOperationException();
626 }
627
628 @Override
629 public int getInt(int column) {
630 throw new UnsupportedOperationException();
631 }
632
633 @Override
634 public long getLong(int column) {
635 if ((mPos != -1) && column == 0) {
636 return mPos; // use row# as the _Id
637 }
638 throw new UnsupportedOperationException();
639 }
640
641 @Override
642 public short getShort(int column) {
643 throw new UnsupportedOperationException();
644 }
645
646 @Override
647 public boolean isNull(int column) {
648 throw new UnsupportedOperationException();
649 }
650
651 // TODO Temporary change, finalize after jq's changes go in
652 public void deactivate() {
653 if (mHistoryCursor != null) {
654 mHistoryCursor.deactivate();
655 }
656 if (mSuggestCursor != null) {
657 mSuggestCursor.deactivate();
658 }
659 super.deactivate();
660 }
661
662 public boolean requery() {
663 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
664 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
665 }
666
667 // TODO Temporary change, finalize after jq's changes go in
668 public void close() {
669 super.close();
670 if (mHistoryCursor != null) {
671 mHistoryCursor.close();
672 mHistoryCursor = null;
673 }
674 if (mSuggestCursor != null) {
675 mSuggestCursor.close();
676 mSuggestCursor = null;
677 }
678 }
Satish Sampath565505b2009-05-29 15:37:27 +0100679
Mike LeBeau21beb132009-05-13 14:57:50 -0700680 /**
681 * Provides the title (text line 1) for a browser suggestion, which should be the
682 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100683 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700684 * @return the title string to use
685 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700686 private String getHistoryTitle() {
687 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700688 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700689 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700690 }
691 return title;
692 }
Satish Sampath565505b2009-05-29 15:37:27 +0100693
Mike LeBeau21beb132009-05-13 14:57:50 -0700694 /**
695 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
696 * webpage url. If the webpage title is empty, then the url should go in the title
697 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100698 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700699 * @return the subtitle string to use, or null if none
700 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700701 private String getHistorySubtitle() {
702 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700703 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
704 return null;
705 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700706 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700707 }
708 }
Satish Sampath565505b2009-05-29 15:37:27 +0100709
Mike LeBeau21beb132009-05-13 14:57:50 -0700710 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700711 * Strips "http://" from the beginning of a url and "/" from the end,
712 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700713 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700714 private String beautifyUrl(String url) {
Satish Sampath60d24e22009-07-09 16:46:08 +0100715 if (mSearchUrlColorId == null) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700716 // Get the color used for this purpose from the current theme.
717 TypedValue colorValue = new TypedValue();
718 getContext().getTheme().resolveAttribute(
719 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
Satish Sampath60d24e22009-07-09 16:46:08 +0100720 mSearchUrlColorId = Integer.toString(colorValue.resourceId);
Mike LeBeau21beb132009-05-13 14:57:50 -0700721 }
Satish Sampath565505b2009-05-29 15:37:27 +0100722
Satish Sampath60d24e22009-07-09 16:46:08 +0100723 return "<font color=\"@" + mSearchUrlColorId + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700724 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800725 }
726
Leon Scroggins58d56c62010-01-28 15:12:40 -0500727 private static class ResultsCursor extends AbstractCursor {
728 // Array indices for RESULTS_COLUMNS
729 private static final int RESULT_ACTION_ID = 1;
730 private static final int RESULT_DATA_ID = 2;
731 private static final int RESULT_TEXT_ID = 3;
732 private static final int RESULT_ICON_ID = 4;
733 private static final int RESULT_EXTRA_ID = 5;
734
735 private static final String[] RESULTS_COLUMNS = new String[] {
736 "_id",
737 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
738 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
739 SearchManager.SUGGEST_COLUMN_TEXT_1,
740 SearchManager.SUGGEST_COLUMN_ICON_1,
741 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA
742 };
743 private final ArrayList<String> mResults;
744 public ResultsCursor(ArrayList<String> results) {
745 mResults = results;
746 }
747 public int getCount() { return mResults.size(); }
748
749 public String[] getColumnNames() {
750 return RESULTS_COLUMNS;
751 }
752
753 public String getString(int column) {
754 switch (column) {
755 case RESULT_ACTION_ID:
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500756 return RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500757 case RESULT_TEXT_ID:
758 // The data is used when the phone is in landscape mode. We
759 // still want to show the result string.
760 case RESULT_DATA_ID:
761 return mResults.get(mPos);
762 case RESULT_EXTRA_ID:
763 // The Intent's extra data will store the index into
764 // mResults so the BrowserActivity will know which result to
765 // use.
766 return Integer.toString(mPos);
767 case RESULT_ICON_ID:
768 return Integer.valueOf(R.drawable.magnifying_glass)
769 .toString();
770 default:
771 return null;
772 }
773 }
774 public short getShort(int column) {
775 throw new UnsupportedOperationException();
776 }
777 public int getInt(int column) {
778 throw new UnsupportedOperationException();
779 }
780 public long getLong(int column) {
781 if ((mPos != -1) && column == 0) {
782 return mPos; // use row# as the _id
783 }
784 throw new UnsupportedOperationException();
785 }
786 public float getFloat(int column) {
787 throw new UnsupportedOperationException();
788 }
789 public double getDouble(int column) {
790 throw new UnsupportedOperationException();
791 }
792 public boolean isNull(int column) {
793 throw new UnsupportedOperationException();
794 }
795 }
796
797 private ResultsCursor mResultsCursor;
798
799 /**
800 * Provide a set of results to be returned to query, intended to be used
801 * by the SearchDialog when the BrowserActivity is in voice search mode.
802 * @param results Strings to display in the dropdown from the SearchDialog
803 */
804 /* package */ void setQueryResults(ArrayList<String> results) {
805 if (results == null) {
806 mResultsCursor = null;
807 } else {
808 mResultsCursor = new ResultsCursor(results);
809 }
810 }
811
The Android Open Source Project0c908882009-03-03 19:32:16 -0800812 @Override
813 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100814 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800815 throws IllegalStateException {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800816 int match = URI_MATCHER.match(url);
817 if (match == -1) {
818 throw new IllegalArgumentException("Unknown URL");
819 }
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000820 if (match == URI_MATCH_GEOLOCATION) {
821 throw new UnsupportedOperationException("query() not supported for geolocation");
822 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500823 if (match == URI_MATCH_SUGGEST && mResultsCursor != null) {
824 Cursor results = mResultsCursor;
825 mResultsCursor = null;
826 return results;
827 }
828 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800829
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100830 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800831 String suggestSelection;
832 String [] myArgs;
833 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
834 suggestSelection = null;
835 myArgs = null;
836 } else {
837 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700838 if (selectionArgs[0].startsWith("http")
839 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800840 myArgs = new String[1];
841 myArgs[0] = like;
842 suggestSelection = selection;
843 } else {
844 SUGGEST_ARGS[0] = "http://" + like;
845 SUGGEST_ARGS[1] = "http://www." + like;
846 SUGGEST_ARGS[2] = "https://" + like;
847 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400848 // To match against titles.
849 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800850 myArgs = SUGGEST_ARGS;
851 suggestSelection = SUGGEST_SELECTION;
852 }
853 }
854
855 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
856 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Leon Scroggins31887fd2009-05-18 16:58:08 -0400857 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800858
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100859 if (match == URI_MATCH_BOOKMARKS_SUGGEST
Dan Egnor5ee906c2009-11-18 12:11:49 -0800860 || Patterns.WEB_URL.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800861 return new MySuggestionCursor(c, null, "");
862 } else {
863 // get Google suggest if there is still space in the list
864 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400865 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800866 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100867 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400868 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800869 }
870 return new MySuggestionCursor(c, null, selectionArgs[0]);
871 }
872 }
873
874 String[] projection = null;
875 if (projectionIn != null && projectionIn.length > 0) {
876 projection = new String[projectionIn.length + 1];
877 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
878 projection[projectionIn.length] = "_id AS _id";
879 }
880
881 StringBuilder whereClause = new StringBuilder(256);
882 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
883 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
884 .append(")");
885 }
886
887 // Tack on the user's selection, if present
888 if (selection != null && selection.length() > 0) {
889 if (whereClause.length() > 0) {
890 whereClause.append(" AND ");
891 }
892
893 whereClause.append('(');
894 whereClause.append(selection);
895 whereClause.append(')');
896 }
897 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
898 whereClause.toString(), selectionArgs, null, null, sortOrder,
899 null);
900 c.setNotificationUri(getContext().getContentResolver(), url);
901 return c;
902 }
903
904 @Override
905 public String getType(Uri url) {
906 int match = URI_MATCHER.match(url);
907 switch (match) {
908 case URI_MATCH_BOOKMARKS:
909 return "vnd.android.cursor.dir/bookmark";
910
911 case URI_MATCH_BOOKMARKS_ID:
912 return "vnd.android.cursor.item/bookmark";
913
914 case URI_MATCH_SEARCHES:
915 return "vnd.android.cursor.dir/searches";
916
917 case URI_MATCH_SEARCHES_ID:
918 return "vnd.android.cursor.item/searches";
919
920 case URI_MATCH_SUGGEST:
921 return SearchManager.SUGGEST_MIME_TYPE;
922
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000923 case URI_MATCH_GEOLOCATION:
924 return "vnd.android.cursor.dir/geolocation";
925
The Android Open Source Project0c908882009-03-03 19:32:16 -0800926 default:
927 throw new IllegalArgumentException("Unknown URL");
928 }
929 }
930
931 @Override
932 public Uri insert(Uri url, ContentValues initialValues) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700933 boolean isBookmarkTable = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800934 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
935
936 int match = URI_MATCHER.match(url);
937 Uri uri = null;
938 switch (match) {
939 case URI_MATCH_BOOKMARKS: {
940 // Insert into the bookmarks table
941 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
942 initialValues);
943 if (rowID > 0) {
944 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
945 rowID);
946 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700947 isBookmarkTable = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800948 break;
949 }
950
951 case URI_MATCH_SEARCHES: {
952 // Insert into the searches table
953 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
954 initialValues);
955 if (rowID > 0) {
956 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
957 rowID);
958 }
959 break;
960 }
961
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000962 case URI_MATCH_GEOLOCATION:
963 String origin = initialValues.getAsString(Browser.GeolocationColumns.ORIGIN);
964 if (TextUtils.isEmpty(origin)) {
965 throw new IllegalArgumentException("Empty origin");
966 }
967 GeolocationPermissions.getInstance().allow(origin);
968 // TODO: Should we have one URI per permission?
969 uri = Browser.GEOLOCATION_URI;
970 break;
971
The Android Open Source Project0c908882009-03-03 19:32:16 -0800972 default:
973 throw new IllegalArgumentException("Unknown URL");
974 }
975
976 if (uri == null) {
977 throw new IllegalArgumentException("Unknown URL");
978 }
979 getContext().getContentResolver().notifyChange(uri, null);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700980
Christopher Tatef0c36f72009-07-28 15:24:05 -0700981 // Back up the new bookmark set if we just inserted one.
982 // A row created when bookmarks are added from scratch will have
983 // bookmark=1 in the initial value set.
984 if (isBookmarkTable
985 && initialValues.containsKey(BookmarkColumns.BOOKMARK)
986 && initialValues.getAsInteger(BookmarkColumns.BOOKMARK) != 0) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700987 mBackupManager.dataChanged();
988 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800989 return uri;
990 }
991
992 @Override
993 public int delete(Uri url, String where, String[] whereArgs) {
994 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
995
996 int match = URI_MATCHER.match(url);
997 if (match == -1 || match == URI_MATCH_SUGGEST) {
998 throw new IllegalArgumentException("Unknown URL");
999 }
1000
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +00001001 if (match == URI_MATCH_GEOLOCATION) {
1002 return deleteGeolocation(url, where, whereArgs);
1003 }
1004
Christopher Tatef0c36f72009-07-28 15:24:05 -07001005 // need to know whether it's the bookmarks table for a couple of reasons
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001006 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
Christopher Tatef0c36f72009-07-28 15:24:05 -07001007 String id = null;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001008
1009 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001010 StringBuilder sb = new StringBuilder();
1011 if (where != null && where.length() > 0) {
1012 sb.append("( ");
1013 sb.append(where);
1014 sb.append(" ) AND ");
1015 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001016 id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001017 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -07001018 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001019 where = sb.toString();
1020 }
1021
Christopher Tatef0c36f72009-07-28 15:24:05 -07001022 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001023
Christopher Tatef0c36f72009-07-28 15:24:05 -07001024 // we'lll need to back up the bookmark set if we are about to delete one
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001025 if (isBookmarkTable) {
Christopher Tatef0c36f72009-07-28 15:24:05 -07001026 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
1027 new String[] { BookmarkColumns.BOOKMARK },
1028 "_id = " + id, null, null);
1029 if (cursor.moveToNext()) {
1030 if (cursor.getInt(0) != 0) {
1031 // yep, this record is a bookmark
1032 mBackupManager.dataChanged();
1033 }
1034 }
1035 cursor.close();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001036 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001037
1038 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
1039 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001040 return count;
1041 }
1042
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +00001043 private int deleteGeolocation(Uri uri, String where, String[] whereArgs) {
1044 if (whereArgs.length != 1) {
1045 throw new IllegalArgumentException("Bad where arguments");
1046 }
1047 String origin = whereArgs[0];
1048 if (TextUtils.isEmpty(origin)) {
1049 throw new IllegalArgumentException("Empty origin");
1050 }
1051 GeolocationPermissions.getInstance().clear(origin);
1052 getContext().getContentResolver().notifyChange(Browser.GEOLOCATION_URI, null);
1053 return 1; // We always return 1, to avoid having to check whether anything was actually removed
1054 }
1055
The Android Open Source Project0c908882009-03-03 19:32:16 -08001056 @Override
1057 public int update(Uri url, ContentValues values, String where,
1058 String[] whereArgs) {
1059 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
1060
1061 int match = URI_MATCHER.match(url);
1062 if (match == -1 || match == URI_MATCH_SUGGEST) {
1063 throw new IllegalArgumentException("Unknown URL");
1064 }
1065
Christopher Tatef0c36f72009-07-28 15:24:05 -07001066 String id = null;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001067 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
Christopher Tatef0c36f72009-07-28 15:24:05 -07001068 boolean changingBookmarks = false;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001069
1070 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001071 StringBuilder sb = new StringBuilder();
1072 if (where != null && where.length() > 0) {
1073 sb.append("( ");
1074 sb.append(where);
1075 sb.append(" ) AND ");
1076 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001077 id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001078 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -07001079 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001080 where = sb.toString();
1081 }
1082
Christopher Tatef0c36f72009-07-28 15:24:05 -07001083 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001084
Christopher Tatef0c36f72009-07-28 15:24:05 -07001085 // Not all bookmark-table updates should be backed up. Look to see
1086 // whether we changed the title, url, or "is a bookmark" state, and
1087 // request a backup if so.
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001088 if (isBookmarkTable) {
Christopher Tatef0c36f72009-07-28 15:24:05 -07001089 // Alterations to the bookmark field inherently change the bookmark
1090 // set, so we don't need to query the record; we know a priori that
1091 // we will need to back up this change.
1092 if (values.containsKey(BookmarkColumns.BOOKMARK)) {
1093 changingBookmarks = true;
1094 }
1095 // changing the title or URL of a bookmark record requires a backup,
1096 // but we don't know wether such an update is on a bookmark without
1097 // querying the record
1098 if (!changingBookmarks &&
1099 (values.containsKey(BookmarkColumns.TITLE)
1100 || values.containsKey(BookmarkColumns.URL))) {
1101 // when isBookmarkTable is true, the 'id' var was assigned above
1102 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
1103 new String[] { BookmarkColumns.BOOKMARK },
1104 "_id = " + id, null, null);
1105 if (cursor.moveToNext()) {
1106 changingBookmarks = (cursor.getInt(0) != 0);
1107 }
1108 cursor.close();
1109 }
1110
1111 // if this *is* a bookmark row we're altering, we need to back it up.
1112 if (changingBookmarks) {
1113 mBackupManager.dataChanged();
1114 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001115 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001116
1117 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
1118 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001119 return ret;
1120 }
Satish Sampath565505b2009-05-29 15:37:27 +01001121
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001122 /**
1123 * Strips the provided url of preceding "http://" and any trailing "/". Does not
1124 * strip "https://". If the provided string cannot be stripped, the original string
1125 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +01001126 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001127 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +01001128 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001129 * @param url a url to strip, like "http://www.google.com/"
1130 * @return a stripped url like "www.google.com", or the original string if it could
1131 * not be stripped
1132 */
1133 private static String stripUrl(String url) {
1134 if (url == null) return null;
1135 Matcher m = STRIP_URL_PATTERN.matcher(url);
1136 if (m.matches() && m.groupCount() == 3) {
1137 return m.group(2);
1138 } else {
1139 return url;
1140 }
1141 }
1142
The Android Open Source Project0c908882009-03-03 19:32:16 -08001143}