blob: 47c9bf9c96d87ff3ff07553e313ee74d2d2e4840 [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;
Dianne Hackborn385effd2010-02-24 20:03:04 -080049import android.util.Patterns;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070050import android.util.TypedValue;
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +000051import android.webkit.GeolocationPermissions;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010052
Dan Egnor5ee906c2009-11-18 12:11:49 -080053
Andrei Popescu1b20b9d2009-09-21 18:49:42 +010054import java.io.File;
55import java.io.FilenameFilter;
Leon Scroggins58d56c62010-01-28 15:12:40 -050056import java.util.ArrayList;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010057import java.util.Date;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070058import java.util.regex.Matcher;
59import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -080060
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070061
The Android Open Source Project0c908882009-03-03 19:32:16 -080062public class BrowserProvider extends ContentProvider {
63
64 private SQLiteOpenHelper mOpenHelper;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070065 private BackupManager mBackupManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080066 private static final String sDatabaseName = "browser.db";
67 private static final String TAG = "BrowserProvider";
68 private static final String ORDER_BY = "visits DESC, date DESC";
69
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070070 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
71 "viewer?source=androidclient";
72
The Android Open Source Project0c908882009-03-03 19:32:16 -080073 private static final String[] TABLE_NAMES = new String[] {
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +000074 "bookmarks", "searches", "geolocation"
The Android Open Source Project0c908882009-03-03 19:32:16 -080075 };
76 private static final String[] SUGGEST_PROJECTION = new String[] {
Leon Scrogginsb4464432009-11-25 12:37:50 -050077 "_id", "url", "title", "bookmark", "user_entered"
The Android Open Source Project0c908882009-03-03 19:32:16 -080078 };
Satish Sampath565505b2009-05-29 15:37:27 +010079 private static final String SUGGEST_SELECTION =
Leon Scrogginsb4464432009-11-25 12:37:50 -050080 "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?"
81 + " OR title LIKE ?) AND (bookmark = 1 OR user_entered = 1)";
Leon Scrogginsbd359cc2009-05-26 15:57:35 -040082 private String[] SUGGEST_ARGS = new String[5];
The Android Open Source Project0c908882009-03-03 19:32:16 -080083
84 // shared suggestion array index, make sure to match COLUMNS
85 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
86 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
87 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
88 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
89 private static final int SUGGEST_COLUMN_ICON_1_ID = 5;
90 private static final int SUGGEST_COLUMN_ICON_2_ID = 6;
91 private static final int SUGGEST_COLUMN_QUERY_ID = 7;
Mike LeBeau1ef26a32009-05-13 20:11:00 -070092 private static final int SUGGEST_COLUMN_FORMAT = 8;
Bjorn Bringert04851702009-09-22 10:36:01 +010093 private static final int SUGGEST_COLUMN_INTENT_EXTRA_DATA = 9;
The Android Open Source Project0c908882009-03-03 19:32:16 -080094
95 // shared suggestion columns
96 private static final String[] COLUMNS = new String[] {
97 "_id",
98 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
99 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
100 SearchManager.SUGGEST_COLUMN_TEXT_1,
101 SearchManager.SUGGEST_COLUMN_TEXT_2,
102 SearchManager.SUGGEST_COLUMN_ICON_1,
103 SearchManager.SUGGEST_COLUMN_ICON_2,
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700104 SearchManager.SUGGEST_COLUMN_QUERY,
Bjorn Bringert04851702009-09-22 10:36:01 +0100105 SearchManager.SUGGEST_COLUMN_FORMAT,
106 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA};
The Android Open Source Project0c908882009-03-03 19:32:16 -0800107
108 private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3;
109 private static final int MAX_SUGGESTION_LONG_ENTRIES = 6;
Leon Scroggins31887fd2009-05-18 16:58:08 -0400110 private static final String MAX_SUGGESTION_LONG_ENTRIES_STRING =
111 Integer.valueOf(MAX_SUGGESTION_LONG_ENTRIES).toString();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800112
113 // make sure that these match the index of TABLE_NAMES
114 private static final int URI_MATCH_BOOKMARKS = 0;
115 private static final int URI_MATCH_SEARCHES = 1;
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000116 private static final int URI_MATCH_GEOLOCATION = 2;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800117 // (id % 10) should match the table name index
118 private static final int URI_MATCH_BOOKMARKS_ID = 10;
119 private static final int URI_MATCH_SEARCHES_ID = 11;
120 //
121 private static final int URI_MATCH_SUGGEST = 20;
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100122 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123
124 private static final UriMatcher URI_MATCHER;
125
126 static {
127 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
128 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
129 URI_MATCH_BOOKMARKS);
130 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
131 URI_MATCH_BOOKMARKS_ID);
132 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
133 URI_MATCH_SEARCHES);
134 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
135 URI_MATCH_SEARCHES_ID);
136 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
137 URI_MATCH_SUGGEST);
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100138 URI_MATCHER.addURI("browser",
139 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
140 URI_MATCH_BOOKMARKS_SUGGEST);
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000141 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_GEOLOCATION],
142 URI_MATCH_GEOLOCATION);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800143 }
144
145 // 1 -> 2 add cache table
146 // 2 -> 3 update history table
147 // 3 -> 4 add passwords table
148 // 4 -> 5 add settings table
149 // 5 -> 6 ?
150 // 6 -> 7 ?
151 // 7 -> 8 drop proxy table
152 // 8 -> 9 drop settings table
153 // 9 -> 10 add form_urls and form_data
154 // 10 -> 11 add searches table
155 // 11 -> 12 modify cache table
156 // 12 -> 13 modify cache table
157 // 13 -> 14 correspond with Google Bookmarks schema
158 // 14 -> 15 move couple of tables to either browser private database or webview database
159 // 15 -> 17 Set it up for the SearchManager
160 // 17 -> 18 Added favicon in bookmarks table for Home shortcuts
161 // 18 -> 19 Remove labels table
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400162 // 19 -> 20 Added thumbnail
Patrick Scott3918d442009-08-04 13:22:29 -0400163 // 20 -> 21 Added touch_icon
Grace Kloba6b52a552009-09-03 16:29:56 -0700164 // 21 -> 22 Remove "clientid"
Leon Scrogginsb4464432009-11-25 12:37:50 -0500165 // 22 -> 23 Added user_entered
166 private static final int DATABASE_VERSION = 23;
Satish Sampath565505b2009-05-29 15:37:27 +0100167
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700168 // Regular expression which matches http://, followed by some stuff, followed by
169 // optionally a trailing slash, all matched as separate groups.
170 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
Satish Sampath565505b2009-05-29 15:37:27 +0100171
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100172 private SearchManager mSearchManager;
173
Satish Sampath60d24e22009-07-09 16:46:08 +0100174 // The ID of the ColorStateList to be applied to urls of website suggestions, as derived from
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700175 // the current theme. This is not set until/unless beautifyUrl is called, at which point
176 // this variable caches the color value.
Satish Sampath60d24e22009-07-09 16:46:08 +0100177 private static String mSearchUrlColorId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178
179 public BrowserProvider() {
180 }
Satish Sampath565505b2009-05-29 15:37:27 +0100181
Patrick Scott43914692010-02-19 10:10:10 -0500182 // XXX: This is a major hack to remove our dependency on gsf constants and
183 // its content provider. http://b/issue?id=2425179
184 static String getClientId(ContentResolver cr) {
185 String ret = "android-google";
186 Cursor c = null;
187 try {
188 c = cr.query(Uri.parse("content://com.google.settings/partner"),
189 new String[] { "value" }, "name='client_id'", null, null);
190 if (c != null && c.moveToNext()) {
191 ret = c.getString(0);
192 }
193 } catch (RuntimeException ex) {
194 // fall through to return the default
195 } finally {
196 if (c != null) {
197 c.close();
198 }
199 }
200 return ret;
201 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800202
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700203 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800204 StringBuffer sb = new StringBuffer();
205 int lastCharLoc = 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100206
Patrick Scott43914692010-02-19 10:10:10 -0500207 final String client_id = getClientId(context.getContentResolver());
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700208
The Android Open Source Project0c908882009-03-03 19:32:16 -0800209 for (int i = 0; i < srcString.length(); ++i) {
210 char c = srcString.charAt(i);
211 if (c == '{') {
212 sb.append(srcString.subSequence(lastCharLoc, i));
213 lastCharLoc = i;
214 inner:
215 for (int j = i; j < srcString.length(); ++j) {
216 char k = srcString.charAt(j);
217 if (k == '}') {
218 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700219 if (propertyKeyValue.equals("CLIENT_ID")) {
220 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800221 } else {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700222 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800223 }
224 lastCharLoc = j + 1;
225 i = j;
226 break inner;
227 }
228 }
229 }
230 }
231 if (srcString.length() - lastCharLoc > 0) {
232 // Put on the tail, if there is one
233 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
234 }
235 return sb;
236 }
237
238 private static class DatabaseHelper extends SQLiteOpenHelper {
239 private Context mContext;
240
241 public DatabaseHelper(Context context) {
242 super(context, sDatabaseName, null, DATABASE_VERSION);
243 mContext = context;
244 }
245
246 @Override
247 public void onCreate(SQLiteDatabase db) {
248 db.execSQL("CREATE TABLE bookmarks (" +
249 "_id INTEGER PRIMARY KEY," +
250 "title TEXT," +
251 "url TEXT," +
252 "visits INTEGER," +
253 "date LONG," +
254 "created LONG," +
255 "description TEXT," +
256 "bookmark INTEGER," +
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400257 "favicon BLOB DEFAULT NULL," +
Patrick Scott3918d442009-08-04 13:22:29 -0400258 "thumbnail BLOB DEFAULT NULL," +
Leon Scrogginsb4464432009-11-25 12:37:50 -0500259 "touch_icon BLOB DEFAULT NULL," +
260 "user_entered INTEGER" +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800261 ");");
262
263 final CharSequence[] bookmarks = mContext.getResources()
264 .getTextArray(R.array.bookmarks);
265 int size = bookmarks.length;
266 try {
267 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700268 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800269 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
270 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100271 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800272 "', 0, 0, 0, 1);");
273 }
274 } catch (ArrayIndexOutOfBoundsException e) {
275 }
276
277 db.execSQL("CREATE TABLE searches (" +
278 "_id INTEGER PRIMARY KEY," +
279 "search TEXT," +
280 "date LONG" +
281 ");");
282 }
283
284 @Override
285 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
286 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400287 + newVersion);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800288 if (oldVersion == 18) {
289 db.execSQL("DROP TABLE IF EXISTS labels");
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400290 }
291 if (oldVersion <= 19) {
292 db.execSQL("ALTER TABLE bookmarks ADD COLUMN thumbnail BLOB DEFAULT NULL;");
Patrick Scott3918d442009-08-04 13:22:29 -0400293 }
294 if (oldVersion < 21) {
295 db.execSQL("ALTER TABLE bookmarks ADD COLUMN touch_icon BLOB DEFAULT NULL;");
Grace Kloba6b52a552009-09-03 16:29:56 -0700296 }
297 if (oldVersion < 22) {
298 db.execSQL("DELETE FROM bookmarks WHERE (bookmark = 0 AND url LIKE \"%.google.%client=ms-%\")");
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100299 removeGears();
Leon Scrogginsb4464432009-11-25 12:37:50 -0500300 }
301 if (oldVersion < 23) {
302 db.execSQL("ALTER TABLE bookmarks ADD COLUMN user_entered INTEGER;");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800303 } else {
304 db.execSQL("DROP TABLE IF EXISTS bookmarks");
305 db.execSQL("DROP TABLE IF EXISTS searches");
306 onCreate(db);
307 }
308 }
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100309
310 private void removeGears() {
311 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
312 public Void doInBackground(Void... unused) {
313 String browserDataDirString = mContext.getApplicationInfo().dataDir;
314 final String appPluginsDirString = "app_plugins";
315 final String gearsPrefix = "gears";
316 File appPluginsDir = new File(browserDataDirString + File.separator
317 + appPluginsDirString);
318 if (!appPluginsDir.exists()) {
319 return null;
320 }
321 // Delete the Gears plugin files
322 File[] gearsFiles = appPluginsDir.listFiles(new FilenameFilter() {
323 public boolean accept(File dir, String filename) {
324 return filename.startsWith(gearsPrefix);
325 }
326 });
327 for (int i = 0; i < gearsFiles.length; ++i) {
328 if (gearsFiles[i].isDirectory()) {
329 deleteDirectory(gearsFiles[i]);
330 } else {
331 gearsFiles[i].delete();
332 }
333 }
334 // Delete the Gears data files
335 File gearsDataDir = new File(browserDataDirString + File.separator
336 + gearsPrefix);
337 if (!gearsDataDir.exists()) {
338 return null;
339 }
340 deleteDirectory(gearsDataDir);
341 return null;
342 }
343
344 private void deleteDirectory(File currentDir) {
345 File[] files = currentDir.listFiles();
346 for (int i = 0; i < files.length; ++i) {
347 if (files[i].isDirectory()) {
348 deleteDirectory(files[i]);
349 }
350 files[i].delete();
351 }
352 currentDir.delete();
353 }
354 };
355
356 task.execute();
357 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800358 }
359
360 @Override
361 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700362 final Context context = getContext();
363 mOpenHelper = new DatabaseHelper(context);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700364 mBackupManager = new BackupManager(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100365 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700366 // To avoid erasing the bookmark table, we added it explicitly for
367 // version 18 and 19 as in the other cases, we will erase the table.
368 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
369 SharedPreferences p = PreferenceManager
370 .getDefaultSharedPreferences(context);
371 boolean fix = p.getBoolean("fix_picasa", true);
372 if (fix) {
373 fixPicasaBookmark();
374 Editor ed = p.edit();
375 ed.putBoolean("fix_picasa", false);
376 ed.commit();
377 }
378 }
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100379 mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400380 mShowWebSuggestionsSettingChangeObserver
381 = new ShowWebSuggestionsSettingChangeObserver();
382 context.getContentResolver().registerContentObserver(
383 Settings.System.getUriFor(
384 Settings.System.SHOW_WEB_SUGGESTIONS),
385 true, mShowWebSuggestionsSettingChangeObserver);
386 updateShowWebSuggestions();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800387 return true;
388 }
389
Leon Scroggins62b71f72009-06-12 17:51:22 -0400390 /**
391 * This Observer will ensure that if the user changes the system
392 * setting of whether to display web suggestions, we will
393 * change accordingly.
394 */
395 /* package */ class ShowWebSuggestionsSettingChangeObserver
396 extends ContentObserver {
397 public ShowWebSuggestionsSettingChangeObserver() {
398 super(new Handler());
399 }
400
401 @Override
402 public void onChange(boolean selfChange) {
403 updateShowWebSuggestions();
404 }
405 }
406
407 private ShowWebSuggestionsSettingChangeObserver
408 mShowWebSuggestionsSettingChangeObserver;
409
410 // If non-null, then the system is set to show web suggestions,
411 // and this is the SearchableInfo to use to get them.
412 private SearchableInfo mSearchableInfo;
413
414 /**
415 * Check the system settings to see whether web suggestions are
416 * allowed. If so, store the SearchableInfo to grab suggestions
417 * while the user is typing.
418 */
419 private void updateShowWebSuggestions() {
420 mSearchableInfo = null;
421 Context context = getContext();
422 if (Settings.System.getInt(context.getContentResolver(),
423 Settings.System.SHOW_WEB_SUGGESTIONS,
424 1 /* default on */) == 1) {
Bjorn Bringert32747542010-02-18 21:59:21 +0000425 ComponentName webSearchComponent = mSearchManager.getWebSearchActivity();
426 if (webSearchComponent != null) {
427 mSearchableInfo = mSearchManager.getSearchableInfo(webSearchComponent);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400428 }
429 }
430 }
431
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700432 private void fixPicasaBookmark() {
433 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
434 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
435 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
436 try {
437 if (!cursor.moveToFirst()) {
438 // set "created" so that it will be on the top of the list
439 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
440 "date, created, bookmark)" + " VALUES('" +
441 getContext().getString(R.string.picasa) + "', '"
442 + PICASA_URL + "', 0, 0, " + new Date().getTime()
443 + ", 1);");
444 }
445 } finally {
446 if (cursor != null) {
447 cursor.close();
448 }
449 }
450 }
451
The Android Open Source Project0c908882009-03-03 19:32:16 -0800452 /*
453 * Subclass AbstractCursor so we can combine multiple Cursors and add
Grace Kloba391df7c2010-03-01 19:51:49 -0800454 * "Search the web".
The Android Open Source Project0c908882009-03-03 19:32:16 -0800455 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100456 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
Grace Kloba391df7c2010-03-01 19:51:49 -0800457 * "Search the web";
458 * 2. If bookmark/history entries has a match, "Search the web" shows up at
459 * the second place. Otherwise, "Search the web" shows up at the first
460 * place.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800461 */
462 private class MySuggestionCursor extends AbstractCursor {
463 private Cursor mHistoryCursor;
464 private Cursor mSuggestCursor;
465 private int mHistoryCount;
466 private int mSuggestionCount;
Grace Klobad3992d42010-01-28 11:44:38 -0800467 private boolean mIncludeWebSearch;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800468 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100469 private int mSuggestText1Id;
470 private int mSuggestText2Id;
471 private int mSuggestQueryId;
Bjorn Bringert04851702009-09-22 10:36:01 +0100472 private int mSuggestIntentExtraDataId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800473
474 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
475 mHistoryCursor = hc;
476 mSuggestCursor = sc;
477 mHistoryCount = hc.getCount();
478 mSuggestionCount = sc != null ? sc.getCount() : 0;
479 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
480 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
481 }
482 mString = string;
Grace Klobad3992d42010-01-28 11:44:38 -0800483 mIncludeWebSearch = string.length() > 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100484
485 // Some web suggest providers only give suggestions and have no description string for
486 // items. The order of the result columns may be different as well. So retrieve the
487 // column indices for the fields we need now and check before using below.
488 if (mSuggestCursor == null) {
489 mSuggestText1Id = -1;
490 mSuggestText2Id = -1;
491 mSuggestQueryId = -1;
Bjorn Bringert04851702009-09-22 10:36:01 +0100492 mSuggestIntentExtraDataId = -1;
Satish Sampath565505b2009-05-29 15:37:27 +0100493 } else {
494 mSuggestText1Id = mSuggestCursor.getColumnIndex(
495 SearchManager.SUGGEST_COLUMN_TEXT_1);
496 mSuggestText2Id = mSuggestCursor.getColumnIndex(
497 SearchManager.SUGGEST_COLUMN_TEXT_2);
498 mSuggestQueryId = mSuggestCursor.getColumnIndex(
499 SearchManager.SUGGEST_COLUMN_QUERY);
Bjorn Bringert04851702009-09-22 10:36:01 +0100500 mSuggestIntentExtraDataId = mSuggestCursor.getColumnIndex(
501 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA);
Satish Sampath565505b2009-05-29 15:37:27 +0100502 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800503 }
504
505 @Override
506 public boolean onMove(int oldPosition, int newPosition) {
507 if (mHistoryCursor == null) {
508 return false;
509 }
Grace Klobad3992d42010-01-28 11:44:38 -0800510 if (mIncludeWebSearch) {
Grace Kloba391df7c2010-03-01 19:51:49 -0800511 if (mHistoryCount == 0 && newPosition == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800512 return true;
Grace Kloba391df7c2010-03-01 19:51:49 -0800513 } else if (mHistoryCount > 0) {
514 if (newPosition == 0) {
515 mHistoryCursor.moveToPosition(0);
516 return true;
517 } else if (newPosition == 1) {
518 return true;
519 }
Grace Klobad3992d42010-01-28 11:44:38 -0800520 }
Grace Kloba391df7c2010-03-01 19:51:49 -0800521 newPosition--;
Grace Klobad3992d42010-01-28 11:44:38 -0800522 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800523 if (mHistoryCount > newPosition) {
524 mHistoryCursor.moveToPosition(newPosition);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800525 } else {
Grace Klobad3992d42010-01-28 11:44:38 -0800526 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800527 }
528 return true;
529 }
530
531 @Override
532 public int getCount() {
Grace Klobad3992d42010-01-28 11:44:38 -0800533 if (mIncludeWebSearch) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800534 return mHistoryCount + mSuggestionCount + 1;
535 } else {
536 return mHistoryCount + mSuggestionCount;
537 }
538 }
539
540 @Override
541 public String[] getColumnNames() {
542 return COLUMNS;
543 }
Satish Sampath565505b2009-05-29 15:37:27 +0100544
The Android Open Source Project0c908882009-03-03 19:32:16 -0800545 @Override
546 public String getString(int columnIndex) {
547 if ((mPos != -1 && mHistoryCursor != null)) {
Grace Kloba391df7c2010-03-01 19:51:49 -0800548 int type = -1; // 0: web search; 1: history; 2: suggestion
549 if (mIncludeWebSearch) {
550 if (mHistoryCount == 0 && mPos == 0) {
551 type = 0;
552 } else if (mHistoryCount > 0) {
553 if (mPos == 0) {
554 type = 1;
555 } else if (mPos == 1) {
556 type = 0;
557 }
558 }
559 if (type == -1) type = (mPos - 1) < mHistoryCount ? 1 : 2;
560 } else {
561 type = mPos < mHistoryCount ? 1 : 2;
562 }
563
The Android Open Source Project0c908882009-03-03 19:32:16 -0800564 switch(columnIndex) {
565 case SUGGEST_COLUMN_INTENT_ACTION_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800566 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800567 return Intent.ACTION_VIEW;
568 } else {
569 return Intent.ACTION_SEARCH;
570 }
571
572 case SUGGEST_COLUMN_INTENT_DATA_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800573 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800574 return mHistoryCursor.getString(1);
575 } else {
576 return null;
577 }
578
579 case SUGGEST_COLUMN_TEXT_1_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800580 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800581 return mString;
Grace Kloba391df7c2010-03-01 19:51:49 -0800582 } else if (type == 1) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700583 return getHistoryTitle();
Grace Klobad3992d42010-01-28 11:44:38 -0800584 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100585 if (mSuggestText1Id == -1) return null;
586 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800587 }
588
589 case SUGGEST_COLUMN_TEXT_2_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800590 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800591 return getContext().getString(R.string.search_the_web);
Grace Kloba391df7c2010-03-01 19:51:49 -0800592 } else if (type == 1) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700593 return getHistorySubtitle();
Grace Klobad3992d42010-01-28 11:44:38 -0800594 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100595 if (mSuggestText2Id == -1) return null;
596 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800597 }
598
599 case SUGGEST_COLUMN_ICON_1_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800600 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800601 if (mHistoryCursor.getInt(3) == 1) {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400602 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800603 R.drawable.ic_search_category_bookmark)
604 .toString();
605 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400606 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800607 R.drawable.ic_search_category_history)
608 .toString();
609 }
610 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400611 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800612 R.drawable.ic_search_category_suggest)
613 .toString();
614 }
615
616 case SUGGEST_COLUMN_ICON_2_ID:
Leon Scroggins31887fd2009-05-18 16:58:08 -0400617 return "0";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800618
619 case SUGGEST_COLUMN_QUERY_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800620 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800621 return mString;
Grace Kloba391df7c2010-03-01 19:51:49 -0800622 } else if (type == 1) {
Mike LeBeau2af73052009-06-23 17:36:59 -0700623 // Return the url in the intent query column. This is ignored
624 // within the browser because our searchable is set to
625 // android:searchMode="queryRewriteFromData", but it is used by
626 // global search for query rewriting.
627 return mHistoryCursor.getString(1);
Grace Klobad3992d42010-01-28 11:44:38 -0800628 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100629 if (mSuggestQueryId == -1) return null;
630 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800631 }
Satish Sampath565505b2009-05-29 15:37:27 +0100632
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700633 case SUGGEST_COLUMN_FORMAT:
634 return "html";
Bjorn Bringert04851702009-09-22 10:36:01 +0100635
636 case SUGGEST_COLUMN_INTENT_EXTRA_DATA:
Grace Kloba391df7c2010-03-01 19:51:49 -0800637 if (type == 0) {
Bjorn Bringert04851702009-09-22 10:36:01 +0100638 return null;
Grace Kloba391df7c2010-03-01 19:51:49 -0800639 } else if (type == 1) {
Grace Klobad3992d42010-01-28 11:44:38 -0800640 return null;
641 } else {
Bjorn Bringert04851702009-09-22 10:36:01 +0100642 if (mSuggestIntentExtraDataId == -1) return null;
643 return mSuggestCursor.getString(mSuggestIntentExtraDataId);
Bjorn Bringert04851702009-09-22 10:36:01 +0100644 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800645 }
646 }
647 return null;
648 }
649
650 @Override
651 public double getDouble(int column) {
652 throw new UnsupportedOperationException();
653 }
654
655 @Override
656 public float getFloat(int column) {
657 throw new UnsupportedOperationException();
658 }
659
660 @Override
661 public int getInt(int column) {
662 throw new UnsupportedOperationException();
663 }
664
665 @Override
666 public long getLong(int column) {
667 if ((mPos != -1) && column == 0) {
668 return mPos; // use row# as the _Id
669 }
670 throw new UnsupportedOperationException();
671 }
672
673 @Override
674 public short getShort(int column) {
675 throw new UnsupportedOperationException();
676 }
677
678 @Override
679 public boolean isNull(int column) {
680 throw new UnsupportedOperationException();
681 }
682
683 // TODO Temporary change, finalize after jq's changes go in
684 public void deactivate() {
685 if (mHistoryCursor != null) {
686 mHistoryCursor.deactivate();
687 }
688 if (mSuggestCursor != null) {
689 mSuggestCursor.deactivate();
690 }
691 super.deactivate();
692 }
693
694 public boolean requery() {
695 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
696 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
697 }
698
699 // TODO Temporary change, finalize after jq's changes go in
700 public void close() {
701 super.close();
702 if (mHistoryCursor != null) {
703 mHistoryCursor.close();
704 mHistoryCursor = null;
705 }
706 if (mSuggestCursor != null) {
707 mSuggestCursor.close();
708 mSuggestCursor = null;
709 }
710 }
Satish Sampath565505b2009-05-29 15:37:27 +0100711
Mike LeBeau21beb132009-05-13 14:57:50 -0700712 /**
713 * Provides the title (text line 1) for a browser suggestion, which should be the
714 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100715 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700716 * @return the title string to use
717 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700718 private String getHistoryTitle() {
719 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700720 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700721 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700722 }
723 return title;
724 }
Satish Sampath565505b2009-05-29 15:37:27 +0100725
Mike LeBeau21beb132009-05-13 14:57:50 -0700726 /**
727 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
728 * webpage url. If the webpage title is empty, then the url should go in the title
729 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100730 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700731 * @return the subtitle string to use, or null if none
732 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700733 private String getHistorySubtitle() {
734 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700735 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
736 return null;
737 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700738 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700739 }
740 }
Satish Sampath565505b2009-05-29 15:37:27 +0100741
Mike LeBeau21beb132009-05-13 14:57:50 -0700742 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700743 * Strips "http://" from the beginning of a url and "/" from the end,
744 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700745 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700746 private String beautifyUrl(String url) {
Satish Sampath60d24e22009-07-09 16:46:08 +0100747 if (mSearchUrlColorId == null) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700748 // Get the color used for this purpose from the current theme.
749 TypedValue colorValue = new TypedValue();
750 getContext().getTheme().resolveAttribute(
751 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
Satish Sampath60d24e22009-07-09 16:46:08 +0100752 mSearchUrlColorId = Integer.toString(colorValue.resourceId);
Mike LeBeau21beb132009-05-13 14:57:50 -0700753 }
Satish Sampath565505b2009-05-29 15:37:27 +0100754
Satish Sampath60d24e22009-07-09 16:46:08 +0100755 return "<font color=\"@" + mSearchUrlColorId + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700756 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800757 }
758
Leon Scroggins58d56c62010-01-28 15:12:40 -0500759 private static class ResultsCursor extends AbstractCursor {
760 // Array indices for RESULTS_COLUMNS
761 private static final int RESULT_ACTION_ID = 1;
762 private static final int RESULT_DATA_ID = 2;
763 private static final int RESULT_TEXT_ID = 3;
764 private static final int RESULT_ICON_ID = 4;
765 private static final int RESULT_EXTRA_ID = 5;
766
767 private static final String[] RESULTS_COLUMNS = new String[] {
768 "_id",
769 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
770 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
771 SearchManager.SUGGEST_COLUMN_TEXT_1,
772 SearchManager.SUGGEST_COLUMN_ICON_1,
773 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA
774 };
775 private final ArrayList<String> mResults;
776 public ResultsCursor(ArrayList<String> results) {
777 mResults = results;
778 }
779 public int getCount() { return mResults.size(); }
780
781 public String[] getColumnNames() {
782 return RESULTS_COLUMNS;
783 }
784
785 public String getString(int column) {
786 switch (column) {
787 case RESULT_ACTION_ID:
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500788 return RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500789 case RESULT_TEXT_ID:
790 // The data is used when the phone is in landscape mode. We
791 // still want to show the result string.
792 case RESULT_DATA_ID:
793 return mResults.get(mPos);
794 case RESULT_EXTRA_ID:
795 // The Intent's extra data will store the index into
796 // mResults so the BrowserActivity will know which result to
797 // use.
798 return Integer.toString(mPos);
799 case RESULT_ICON_ID:
800 return Integer.valueOf(R.drawable.magnifying_glass)
801 .toString();
802 default:
803 return null;
804 }
805 }
806 public short getShort(int column) {
807 throw new UnsupportedOperationException();
808 }
809 public int getInt(int column) {
810 throw new UnsupportedOperationException();
811 }
812 public long getLong(int column) {
813 if ((mPos != -1) && column == 0) {
814 return mPos; // use row# as the _id
815 }
816 throw new UnsupportedOperationException();
817 }
818 public float getFloat(int column) {
819 throw new UnsupportedOperationException();
820 }
821 public double getDouble(int column) {
822 throw new UnsupportedOperationException();
823 }
824 public boolean isNull(int column) {
825 throw new UnsupportedOperationException();
826 }
827 }
828
829 private ResultsCursor mResultsCursor;
830
831 /**
832 * Provide a set of results to be returned to query, intended to be used
833 * by the SearchDialog when the BrowserActivity is in voice search mode.
834 * @param results Strings to display in the dropdown from the SearchDialog
835 */
836 /* package */ void setQueryResults(ArrayList<String> results) {
837 if (results == null) {
838 mResultsCursor = null;
839 } else {
840 mResultsCursor = new ResultsCursor(results);
841 }
842 }
843
The Android Open Source Project0c908882009-03-03 19:32:16 -0800844 @Override
845 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100846 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800847 throws IllegalStateException {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800848 int match = URI_MATCHER.match(url);
849 if (match == -1) {
850 throw new IllegalArgumentException("Unknown URL");
851 }
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000852 if (match == URI_MATCH_GEOLOCATION) {
853 throw new UnsupportedOperationException("query() not supported for geolocation");
854 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500855 if (match == URI_MATCH_SUGGEST && mResultsCursor != null) {
856 Cursor results = mResultsCursor;
857 mResultsCursor = null;
858 return results;
859 }
860 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800861
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100862 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800863 String suggestSelection;
864 String [] myArgs;
865 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
866 suggestSelection = null;
867 myArgs = null;
868 } else {
869 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700870 if (selectionArgs[0].startsWith("http")
871 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800872 myArgs = new String[1];
873 myArgs[0] = like;
874 suggestSelection = selection;
875 } else {
876 SUGGEST_ARGS[0] = "http://" + like;
877 SUGGEST_ARGS[1] = "http://www." + like;
878 SUGGEST_ARGS[2] = "https://" + like;
879 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400880 // To match against titles.
881 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800882 myArgs = SUGGEST_ARGS;
883 suggestSelection = SUGGEST_SELECTION;
884 }
885 }
886
887 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
888 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Leon Scroggins31887fd2009-05-18 16:58:08 -0400889 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800890
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100891 if (match == URI_MATCH_BOOKMARKS_SUGGEST
Dan Egnor5ee906c2009-11-18 12:11:49 -0800892 || Patterns.WEB_URL.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800893 return new MySuggestionCursor(c, null, "");
894 } else {
895 // get Google suggest if there is still space in the list
896 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400897 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800898 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100899 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400900 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800901 }
902 return new MySuggestionCursor(c, null, selectionArgs[0]);
903 }
904 }
905
906 String[] projection = null;
907 if (projectionIn != null && projectionIn.length > 0) {
908 projection = new String[projectionIn.length + 1];
909 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
910 projection[projectionIn.length] = "_id AS _id";
911 }
912
913 StringBuilder whereClause = new StringBuilder(256);
914 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
915 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
916 .append(")");
917 }
918
919 // Tack on the user's selection, if present
920 if (selection != null && selection.length() > 0) {
921 if (whereClause.length() > 0) {
922 whereClause.append(" AND ");
923 }
924
925 whereClause.append('(');
926 whereClause.append(selection);
927 whereClause.append(')');
928 }
929 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
930 whereClause.toString(), selectionArgs, null, null, sortOrder,
931 null);
932 c.setNotificationUri(getContext().getContentResolver(), url);
933 return c;
934 }
935
936 @Override
937 public String getType(Uri url) {
938 int match = URI_MATCHER.match(url);
939 switch (match) {
940 case URI_MATCH_BOOKMARKS:
941 return "vnd.android.cursor.dir/bookmark";
942
943 case URI_MATCH_BOOKMARKS_ID:
944 return "vnd.android.cursor.item/bookmark";
945
946 case URI_MATCH_SEARCHES:
947 return "vnd.android.cursor.dir/searches";
948
949 case URI_MATCH_SEARCHES_ID:
950 return "vnd.android.cursor.item/searches";
951
952 case URI_MATCH_SUGGEST:
953 return SearchManager.SUGGEST_MIME_TYPE;
954
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000955 case URI_MATCH_GEOLOCATION:
956 return "vnd.android.cursor.dir/geolocation";
957
The Android Open Source Project0c908882009-03-03 19:32:16 -0800958 default:
959 throw new IllegalArgumentException("Unknown URL");
960 }
961 }
962
963 @Override
964 public Uri insert(Uri url, ContentValues initialValues) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700965 boolean isBookmarkTable = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800966 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
967
968 int match = URI_MATCHER.match(url);
969 Uri uri = null;
970 switch (match) {
971 case URI_MATCH_BOOKMARKS: {
972 // Insert into the bookmarks table
973 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
974 initialValues);
975 if (rowID > 0) {
976 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
977 rowID);
978 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700979 isBookmarkTable = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800980 break;
981 }
982
983 case URI_MATCH_SEARCHES: {
984 // Insert into the searches table
985 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
986 initialValues);
987 if (rowID > 0) {
988 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
989 rowID);
990 }
991 break;
992 }
993
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000994 case URI_MATCH_GEOLOCATION:
995 String origin = initialValues.getAsString(Browser.GeolocationColumns.ORIGIN);
996 if (TextUtils.isEmpty(origin)) {
997 throw new IllegalArgumentException("Empty origin");
998 }
999 GeolocationPermissions.getInstance().allow(origin);
1000 // TODO: Should we have one URI per permission?
1001 uri = Browser.GEOLOCATION_URI;
1002 break;
1003
The Android Open Source Project0c908882009-03-03 19:32:16 -08001004 default:
1005 throw new IllegalArgumentException("Unknown URL");
1006 }
1007
1008 if (uri == null) {
1009 throw new IllegalArgumentException("Unknown URL");
1010 }
1011 getContext().getContentResolver().notifyChange(uri, null);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001012
Christopher Tatef0c36f72009-07-28 15:24:05 -07001013 // Back up the new bookmark set if we just inserted one.
1014 // A row created when bookmarks are added from scratch will have
1015 // bookmark=1 in the initial value set.
1016 if (isBookmarkTable
1017 && initialValues.containsKey(BookmarkColumns.BOOKMARK)
1018 && initialValues.getAsInteger(BookmarkColumns.BOOKMARK) != 0) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001019 mBackupManager.dataChanged();
1020 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001021 return uri;
1022 }
1023
1024 @Override
1025 public int delete(Uri url, String where, String[] whereArgs) {
1026 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
1027
1028 int match = URI_MATCHER.match(url);
1029 if (match == -1 || match == URI_MATCH_SUGGEST) {
1030 throw new IllegalArgumentException("Unknown URL");
1031 }
1032
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +00001033 if (match == URI_MATCH_GEOLOCATION) {
1034 return deleteGeolocation(url, where, whereArgs);
1035 }
1036
Christopher Tatef0c36f72009-07-28 15:24:05 -07001037 // need to know whether it's the bookmarks table for a couple of reasons
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001038 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
Christopher Tatef0c36f72009-07-28 15:24:05 -07001039 String id = null;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001040
1041 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001042 StringBuilder sb = new StringBuilder();
1043 if (where != null && where.length() > 0) {
1044 sb.append("( ");
1045 sb.append(where);
1046 sb.append(" ) AND ");
1047 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001048 id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001049 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -07001050 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001051 where = sb.toString();
1052 }
1053
Christopher Tatef0c36f72009-07-28 15:24:05 -07001054 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001055
Christopher Tatef0c36f72009-07-28 15:24:05 -07001056 // we'lll need to back up the bookmark set if we are about to delete one
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001057 if (isBookmarkTable) {
Christopher Tatef0c36f72009-07-28 15:24:05 -07001058 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
1059 new String[] { BookmarkColumns.BOOKMARK },
1060 "_id = " + id, null, null);
1061 if (cursor.moveToNext()) {
1062 if (cursor.getInt(0) != 0) {
1063 // yep, this record is a bookmark
1064 mBackupManager.dataChanged();
1065 }
1066 }
1067 cursor.close();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001068 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001069
1070 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
1071 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001072 return count;
1073 }
1074
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +00001075 private int deleteGeolocation(Uri uri, String where, String[] whereArgs) {
1076 if (whereArgs.length != 1) {
1077 throw new IllegalArgumentException("Bad where arguments");
1078 }
1079 String origin = whereArgs[0];
1080 if (TextUtils.isEmpty(origin)) {
1081 throw new IllegalArgumentException("Empty origin");
1082 }
1083 GeolocationPermissions.getInstance().clear(origin);
1084 getContext().getContentResolver().notifyChange(Browser.GEOLOCATION_URI, null);
1085 return 1; // We always return 1, to avoid having to check whether anything was actually removed
1086 }
1087
The Android Open Source Project0c908882009-03-03 19:32:16 -08001088 @Override
1089 public int update(Uri url, ContentValues values, String where,
1090 String[] whereArgs) {
1091 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
1092
1093 int match = URI_MATCHER.match(url);
1094 if (match == -1 || match == URI_MATCH_SUGGEST) {
1095 throw new IllegalArgumentException("Unknown URL");
1096 }
1097
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001098 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001099 StringBuilder sb = new StringBuilder();
1100 if (where != null && where.length() > 0) {
1101 sb.append("( ");
1102 sb.append(where);
1103 sb.append(" ) AND ");
1104 }
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001105 String id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001106 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -07001107 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001108 where = sb.toString();
1109 }
1110
Christopher Tatef0c36f72009-07-28 15:24:05 -07001111 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001112
Christopher Tatef0c36f72009-07-28 15:24:05 -07001113 // Not all bookmark-table updates should be backed up. Look to see
1114 // whether we changed the title, url, or "is a bookmark" state, and
1115 // request a backup if so.
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001116 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_BOOKMARKS) {
1117 boolean changingBookmarks = false;
Christopher Tatef0c36f72009-07-28 15:24:05 -07001118 // Alterations to the bookmark field inherently change the bookmark
1119 // set, so we don't need to query the record; we know a priori that
1120 // we will need to back up this change.
1121 if (values.containsKey(BookmarkColumns.BOOKMARK)) {
1122 changingBookmarks = true;
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001123 } else if ((values.containsKey(BookmarkColumns.TITLE)
1124 || values.containsKey(BookmarkColumns.URL))
1125 && values.containsKey(BookmarkColumns._ID)) {
1126 // If a title or URL has been changed, check to see if it is to
1127 // a bookmark. The ID should have been included in the update,
1128 // so use it.
Christopher Tatef0c36f72009-07-28 15:24:05 -07001129 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
1130 new String[] { BookmarkColumns.BOOKMARK },
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001131 BookmarkColumns._ID + " = "
1132 + values.getAsString(BookmarkColumns._ID), null, null);
Christopher Tatef0c36f72009-07-28 15:24:05 -07001133 if (cursor.moveToNext()) {
1134 changingBookmarks = (cursor.getInt(0) != 0);
1135 }
1136 cursor.close();
1137 }
1138
1139 // if this *is* a bookmark row we're altering, we need to back it up.
1140 if (changingBookmarks) {
1141 mBackupManager.dataChanged();
1142 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001143 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001144
1145 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
1146 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001147 return ret;
1148 }
Satish Sampath565505b2009-05-29 15:37:27 +01001149
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001150 /**
1151 * Strips the provided url of preceding "http://" and any trailing "/". Does not
1152 * strip "https://". If the provided string cannot be stripped, the original string
1153 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +01001154 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001155 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +01001156 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001157 * @param url a url to strip, like "http://www.google.com/"
1158 * @return a stripped url like "www.google.com", or the original string if it could
1159 * not be stripped
1160 */
1161 private static String stripUrl(String url) {
1162 if (url == null) return null;
1163 Matcher m = STRIP_URL_PATTERN.matcher(url);
1164 if (m.matches() && m.groupCount() == 3) {
1165 return m.group(2);
1166 } else {
1167 return url;
1168 }
1169 }
1170
The Android Open Source Project0c908882009-03-03 19:32:16 -08001171}