blob: 94d7ecafbe49e2bea63a86649662f60224fdff89 [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
454 * "Google Search".
455 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100456 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
The Android Open Source Project0c908882009-03-03 19:32:16 -0800457 * "Google Search";
Satish Sampath565505b2009-05-29 15:37:27 +0100458 * 2. If bookmark/history entries are less than
The Android Open Source Project0c908882009-03-03 19:32:16 -0800459 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
460 */
461 private class MySuggestionCursor extends AbstractCursor {
462 private Cursor mHistoryCursor;
463 private Cursor mSuggestCursor;
464 private int mHistoryCount;
465 private int mSuggestionCount;
Grace Klobad3992d42010-01-28 11:44:38 -0800466 private boolean mIncludeWebSearch;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100468 private int mSuggestText1Id;
469 private int mSuggestText2Id;
470 private int mSuggestQueryId;
Bjorn Bringert04851702009-09-22 10:36:01 +0100471 private int mSuggestIntentExtraDataId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472
473 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
474 mHistoryCursor = hc;
475 mSuggestCursor = sc;
476 mHistoryCount = hc.getCount();
477 mSuggestionCount = sc != null ? sc.getCount() : 0;
478 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
479 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
480 }
481 mString = string;
Grace Klobad3992d42010-01-28 11:44:38 -0800482 mIncludeWebSearch = string.length() > 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100483
484 // Some web suggest providers only give suggestions and have no description string for
485 // items. The order of the result columns may be different as well. So retrieve the
486 // column indices for the fields we need now and check before using below.
487 if (mSuggestCursor == null) {
488 mSuggestText1Id = -1;
489 mSuggestText2Id = -1;
490 mSuggestQueryId = -1;
Bjorn Bringert04851702009-09-22 10:36:01 +0100491 mSuggestIntentExtraDataId = -1;
Satish Sampath565505b2009-05-29 15:37:27 +0100492 } else {
493 mSuggestText1Id = mSuggestCursor.getColumnIndex(
494 SearchManager.SUGGEST_COLUMN_TEXT_1);
495 mSuggestText2Id = mSuggestCursor.getColumnIndex(
496 SearchManager.SUGGEST_COLUMN_TEXT_2);
497 mSuggestQueryId = mSuggestCursor.getColumnIndex(
498 SearchManager.SUGGEST_COLUMN_QUERY);
Bjorn Bringert04851702009-09-22 10:36:01 +0100499 mSuggestIntentExtraDataId = mSuggestCursor.getColumnIndex(
500 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA);
Satish Sampath565505b2009-05-29 15:37:27 +0100501 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800502 }
503
504 @Override
505 public boolean onMove(int oldPosition, int newPosition) {
506 if (mHistoryCursor == null) {
507 return false;
508 }
Grace Klobad3992d42010-01-28 11:44:38 -0800509 if (mIncludeWebSearch) {
510 if (newPosition == 0) {
511 return true;
512 } else {
513 newPosition--;
514 }
515 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800516 if (mHistoryCount > newPosition) {
517 mHistoryCursor.moveToPosition(newPosition);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800518 } else {
Grace Klobad3992d42010-01-28 11:44:38 -0800519 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800520 }
521 return true;
522 }
523
524 @Override
525 public int getCount() {
Grace Klobad3992d42010-01-28 11:44:38 -0800526 if (mIncludeWebSearch) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800527 return mHistoryCount + mSuggestionCount + 1;
528 } else {
529 return mHistoryCount + mSuggestionCount;
530 }
531 }
532
533 @Override
534 public String[] getColumnNames() {
535 return COLUMNS;
536 }
Satish Sampath565505b2009-05-29 15:37:27 +0100537
The Android Open Source Project0c908882009-03-03 19:32:16 -0800538 @Override
539 public String getString(int columnIndex) {
540 if ((mPos != -1 && mHistoryCursor != null)) {
Grace Klobad3992d42010-01-28 11:44:38 -0800541 int position = mIncludeWebSearch ? mPos - 1 : mPos;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800542 switch(columnIndex) {
543 case SUGGEST_COLUMN_INTENT_ACTION_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800544 if (position >= 0 && position < mHistoryCount) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800545 return Intent.ACTION_VIEW;
546 } else {
547 return Intent.ACTION_SEARCH;
548 }
549
550 case SUGGEST_COLUMN_INTENT_DATA_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800551 if (position >= 0 && position < mHistoryCount) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800552 return mHistoryCursor.getString(1);
553 } else {
554 return null;
555 }
556
557 case SUGGEST_COLUMN_TEXT_1_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800558 if (position < 0) {
559 return mString;
560 } else if (mHistoryCount > position) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700561 return getHistoryTitle();
Grace Klobad3992d42010-01-28 11:44:38 -0800562 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100563 if (mSuggestText1Id == -1) return null;
564 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800565 }
566
567 case SUGGEST_COLUMN_TEXT_2_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800568 if (position < 0) {
569 return getContext().getString(R.string.search_the_web);
570 } else if (mHistoryCount > position) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700571 return getHistorySubtitle();
Grace Klobad3992d42010-01-28 11:44:38 -0800572 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100573 if (mSuggestText2Id == -1) return null;
574 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800575 }
576
577 case SUGGEST_COLUMN_ICON_1_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800578 if (position >= 0 && position < mHistoryCount) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800579 if (mHistoryCursor.getInt(3) == 1) {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400580 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800581 R.drawable.ic_search_category_bookmark)
582 .toString();
583 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400584 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800585 R.drawable.ic_search_category_history)
586 .toString();
587 }
588 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400589 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800590 R.drawable.ic_search_category_suggest)
591 .toString();
592 }
593
594 case SUGGEST_COLUMN_ICON_2_ID:
Leon Scroggins31887fd2009-05-18 16:58:08 -0400595 return "0";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800596
597 case SUGGEST_COLUMN_QUERY_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800598 if (position < 0) {
599 return mString;
600 } else if (mHistoryCount > position) {
Mike LeBeau2af73052009-06-23 17:36:59 -0700601 // Return the url in the intent query column. This is ignored
602 // within the browser because our searchable is set to
603 // android:searchMode="queryRewriteFromData", but it is used by
604 // global search for query rewriting.
605 return mHistoryCursor.getString(1);
Grace Klobad3992d42010-01-28 11:44:38 -0800606 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100607 if (mSuggestQueryId == -1) return null;
608 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800609 }
Satish Sampath565505b2009-05-29 15:37:27 +0100610
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700611 case SUGGEST_COLUMN_FORMAT:
612 return "html";
Bjorn Bringert04851702009-09-22 10:36:01 +0100613
614 case SUGGEST_COLUMN_INTENT_EXTRA_DATA:
Grace Klobad3992d42010-01-28 11:44:38 -0800615 if (position < 0) {
Bjorn Bringert04851702009-09-22 10:36:01 +0100616 return null;
Grace Klobad3992d42010-01-28 11:44:38 -0800617 } else if (mHistoryCount > position) {
618 return null;
619 } else {
Bjorn Bringert04851702009-09-22 10:36:01 +0100620 if (mSuggestIntentExtraDataId == -1) return null;
621 return mSuggestCursor.getString(mSuggestIntentExtraDataId);
Bjorn Bringert04851702009-09-22 10:36:01 +0100622 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800623 }
624 }
625 return null;
626 }
627
628 @Override
629 public double getDouble(int column) {
630 throw new UnsupportedOperationException();
631 }
632
633 @Override
634 public float getFloat(int column) {
635 throw new UnsupportedOperationException();
636 }
637
638 @Override
639 public int getInt(int column) {
640 throw new UnsupportedOperationException();
641 }
642
643 @Override
644 public long getLong(int column) {
645 if ((mPos != -1) && column == 0) {
646 return mPos; // use row# as the _Id
647 }
648 throw new UnsupportedOperationException();
649 }
650
651 @Override
652 public short getShort(int column) {
653 throw new UnsupportedOperationException();
654 }
655
656 @Override
657 public boolean isNull(int column) {
658 throw new UnsupportedOperationException();
659 }
660
661 // TODO Temporary change, finalize after jq's changes go in
662 public void deactivate() {
663 if (mHistoryCursor != null) {
664 mHistoryCursor.deactivate();
665 }
666 if (mSuggestCursor != null) {
667 mSuggestCursor.deactivate();
668 }
669 super.deactivate();
670 }
671
672 public boolean requery() {
673 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
674 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
675 }
676
677 // TODO Temporary change, finalize after jq's changes go in
678 public void close() {
679 super.close();
680 if (mHistoryCursor != null) {
681 mHistoryCursor.close();
682 mHistoryCursor = null;
683 }
684 if (mSuggestCursor != null) {
685 mSuggestCursor.close();
686 mSuggestCursor = null;
687 }
688 }
Satish Sampath565505b2009-05-29 15:37:27 +0100689
Mike LeBeau21beb132009-05-13 14:57:50 -0700690 /**
691 * Provides the title (text line 1) for a browser suggestion, which should be the
692 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100693 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700694 * @return the title string to use
695 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700696 private String getHistoryTitle() {
697 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700698 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700699 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700700 }
701 return title;
702 }
Satish Sampath565505b2009-05-29 15:37:27 +0100703
Mike LeBeau21beb132009-05-13 14:57:50 -0700704 /**
705 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
706 * webpage url. If the webpage title is empty, then the url should go in the title
707 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100708 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700709 * @return the subtitle string to use, or null if none
710 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700711 private String getHistorySubtitle() {
712 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700713 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
714 return null;
715 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700716 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700717 }
718 }
Satish Sampath565505b2009-05-29 15:37:27 +0100719
Mike LeBeau21beb132009-05-13 14:57:50 -0700720 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700721 * Strips "http://" from the beginning of a url and "/" from the end,
722 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700723 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700724 private String beautifyUrl(String url) {
Satish Sampath60d24e22009-07-09 16:46:08 +0100725 if (mSearchUrlColorId == null) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700726 // Get the color used for this purpose from the current theme.
727 TypedValue colorValue = new TypedValue();
728 getContext().getTheme().resolveAttribute(
729 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
Satish Sampath60d24e22009-07-09 16:46:08 +0100730 mSearchUrlColorId = Integer.toString(colorValue.resourceId);
Mike LeBeau21beb132009-05-13 14:57:50 -0700731 }
Satish Sampath565505b2009-05-29 15:37:27 +0100732
Satish Sampath60d24e22009-07-09 16:46:08 +0100733 return "<font color=\"@" + mSearchUrlColorId + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700734 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800735 }
736
Leon Scroggins58d56c62010-01-28 15:12:40 -0500737 private static class ResultsCursor extends AbstractCursor {
738 // Array indices for RESULTS_COLUMNS
739 private static final int RESULT_ACTION_ID = 1;
740 private static final int RESULT_DATA_ID = 2;
741 private static final int RESULT_TEXT_ID = 3;
742 private static final int RESULT_ICON_ID = 4;
743 private static final int RESULT_EXTRA_ID = 5;
744
745 private static final String[] RESULTS_COLUMNS = new String[] {
746 "_id",
747 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
748 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
749 SearchManager.SUGGEST_COLUMN_TEXT_1,
750 SearchManager.SUGGEST_COLUMN_ICON_1,
751 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA
752 };
753 private final ArrayList<String> mResults;
754 public ResultsCursor(ArrayList<String> results) {
755 mResults = results;
756 }
757 public int getCount() { return mResults.size(); }
758
759 public String[] getColumnNames() {
760 return RESULTS_COLUMNS;
761 }
762
763 public String getString(int column) {
764 switch (column) {
765 case RESULT_ACTION_ID:
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500766 return RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500767 case RESULT_TEXT_ID:
768 // The data is used when the phone is in landscape mode. We
769 // still want to show the result string.
770 case RESULT_DATA_ID:
771 return mResults.get(mPos);
772 case RESULT_EXTRA_ID:
773 // The Intent's extra data will store the index into
774 // mResults so the BrowserActivity will know which result to
775 // use.
776 return Integer.toString(mPos);
777 case RESULT_ICON_ID:
778 return Integer.valueOf(R.drawable.magnifying_glass)
779 .toString();
780 default:
781 return null;
782 }
783 }
784 public short getShort(int column) {
785 throw new UnsupportedOperationException();
786 }
787 public int getInt(int column) {
788 throw new UnsupportedOperationException();
789 }
790 public long getLong(int column) {
791 if ((mPos != -1) && column == 0) {
792 return mPos; // use row# as the _id
793 }
794 throw new UnsupportedOperationException();
795 }
796 public float getFloat(int column) {
797 throw new UnsupportedOperationException();
798 }
799 public double getDouble(int column) {
800 throw new UnsupportedOperationException();
801 }
802 public boolean isNull(int column) {
803 throw new UnsupportedOperationException();
804 }
805 }
806
807 private ResultsCursor mResultsCursor;
808
809 /**
810 * Provide a set of results to be returned to query, intended to be used
811 * by the SearchDialog when the BrowserActivity is in voice search mode.
812 * @param results Strings to display in the dropdown from the SearchDialog
813 */
814 /* package */ void setQueryResults(ArrayList<String> results) {
815 if (results == null) {
816 mResultsCursor = null;
817 } else {
818 mResultsCursor = new ResultsCursor(results);
819 }
820 }
821
The Android Open Source Project0c908882009-03-03 19:32:16 -0800822 @Override
823 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100824 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800825 throws IllegalStateException {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800826 int match = URI_MATCHER.match(url);
827 if (match == -1) {
828 throw new IllegalArgumentException("Unknown URL");
829 }
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000830 if (match == URI_MATCH_GEOLOCATION) {
831 throw new UnsupportedOperationException("query() not supported for geolocation");
832 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500833 if (match == URI_MATCH_SUGGEST && mResultsCursor != null) {
834 Cursor results = mResultsCursor;
835 mResultsCursor = null;
836 return results;
837 }
838 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800839
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100840 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800841 String suggestSelection;
842 String [] myArgs;
843 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
844 suggestSelection = null;
845 myArgs = null;
846 } else {
847 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700848 if (selectionArgs[0].startsWith("http")
849 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800850 myArgs = new String[1];
851 myArgs[0] = like;
852 suggestSelection = selection;
853 } else {
854 SUGGEST_ARGS[0] = "http://" + like;
855 SUGGEST_ARGS[1] = "http://www." + like;
856 SUGGEST_ARGS[2] = "https://" + like;
857 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400858 // To match against titles.
859 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800860 myArgs = SUGGEST_ARGS;
861 suggestSelection = SUGGEST_SELECTION;
862 }
863 }
864
865 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
866 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Leon Scroggins31887fd2009-05-18 16:58:08 -0400867 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800868
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100869 if (match == URI_MATCH_BOOKMARKS_SUGGEST
Dan Egnor5ee906c2009-11-18 12:11:49 -0800870 || Patterns.WEB_URL.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800871 return new MySuggestionCursor(c, null, "");
872 } else {
873 // get Google suggest if there is still space in the list
874 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400875 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800876 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100877 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400878 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800879 }
880 return new MySuggestionCursor(c, null, selectionArgs[0]);
881 }
882 }
883
884 String[] projection = null;
885 if (projectionIn != null && projectionIn.length > 0) {
886 projection = new String[projectionIn.length + 1];
887 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
888 projection[projectionIn.length] = "_id AS _id";
889 }
890
891 StringBuilder whereClause = new StringBuilder(256);
892 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
893 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
894 .append(")");
895 }
896
897 // Tack on the user's selection, if present
898 if (selection != null && selection.length() > 0) {
899 if (whereClause.length() > 0) {
900 whereClause.append(" AND ");
901 }
902
903 whereClause.append('(');
904 whereClause.append(selection);
905 whereClause.append(')');
906 }
907 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
908 whereClause.toString(), selectionArgs, null, null, sortOrder,
909 null);
910 c.setNotificationUri(getContext().getContentResolver(), url);
911 return c;
912 }
913
914 @Override
915 public String getType(Uri url) {
916 int match = URI_MATCHER.match(url);
917 switch (match) {
918 case URI_MATCH_BOOKMARKS:
919 return "vnd.android.cursor.dir/bookmark";
920
921 case URI_MATCH_BOOKMARKS_ID:
922 return "vnd.android.cursor.item/bookmark";
923
924 case URI_MATCH_SEARCHES:
925 return "vnd.android.cursor.dir/searches";
926
927 case URI_MATCH_SEARCHES_ID:
928 return "vnd.android.cursor.item/searches";
929
930 case URI_MATCH_SUGGEST:
931 return SearchManager.SUGGEST_MIME_TYPE;
932
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000933 case URI_MATCH_GEOLOCATION:
934 return "vnd.android.cursor.dir/geolocation";
935
The Android Open Source Project0c908882009-03-03 19:32:16 -0800936 default:
937 throw new IllegalArgumentException("Unknown URL");
938 }
939 }
940
941 @Override
942 public Uri insert(Uri url, ContentValues initialValues) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700943 boolean isBookmarkTable = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800944 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
945
946 int match = URI_MATCHER.match(url);
947 Uri uri = null;
948 switch (match) {
949 case URI_MATCH_BOOKMARKS: {
950 // Insert into the bookmarks table
951 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
952 initialValues);
953 if (rowID > 0) {
954 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
955 rowID);
956 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700957 isBookmarkTable = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800958 break;
959 }
960
961 case URI_MATCH_SEARCHES: {
962 // Insert into the searches table
963 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
964 initialValues);
965 if (rowID > 0) {
966 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
967 rowID);
968 }
969 break;
970 }
971
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000972 case URI_MATCH_GEOLOCATION:
973 String origin = initialValues.getAsString(Browser.GeolocationColumns.ORIGIN);
974 if (TextUtils.isEmpty(origin)) {
975 throw new IllegalArgumentException("Empty origin");
976 }
977 GeolocationPermissions.getInstance().allow(origin);
978 // TODO: Should we have one URI per permission?
979 uri = Browser.GEOLOCATION_URI;
980 break;
981
The Android Open Source Project0c908882009-03-03 19:32:16 -0800982 default:
983 throw new IllegalArgumentException("Unknown URL");
984 }
985
986 if (uri == null) {
987 throw new IllegalArgumentException("Unknown URL");
988 }
989 getContext().getContentResolver().notifyChange(uri, null);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700990
Christopher Tatef0c36f72009-07-28 15:24:05 -0700991 // Back up the new bookmark set if we just inserted one.
992 // A row created when bookmarks are added from scratch will have
993 // bookmark=1 in the initial value set.
994 if (isBookmarkTable
995 && initialValues.containsKey(BookmarkColumns.BOOKMARK)
996 && initialValues.getAsInteger(BookmarkColumns.BOOKMARK) != 0) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700997 mBackupManager.dataChanged();
998 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800999 return uri;
1000 }
1001
1002 @Override
1003 public int delete(Uri url, String where, String[] whereArgs) {
1004 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
1005
1006 int match = URI_MATCHER.match(url);
1007 if (match == -1 || match == URI_MATCH_SUGGEST) {
1008 throw new IllegalArgumentException("Unknown URL");
1009 }
1010
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +00001011 if (match == URI_MATCH_GEOLOCATION) {
1012 return deleteGeolocation(url, where, whereArgs);
1013 }
1014
Christopher Tatef0c36f72009-07-28 15:24:05 -07001015 // need to know whether it's the bookmarks table for a couple of reasons
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001016 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
Christopher Tatef0c36f72009-07-28 15:24:05 -07001017 String id = null;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001018
1019 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001020 StringBuilder sb = new StringBuilder();
1021 if (where != null && where.length() > 0) {
1022 sb.append("( ");
1023 sb.append(where);
1024 sb.append(" ) AND ");
1025 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001026 id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001027 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -07001028 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001029 where = sb.toString();
1030 }
1031
Christopher Tatef0c36f72009-07-28 15:24:05 -07001032 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001033
Christopher Tatef0c36f72009-07-28 15:24:05 -07001034 // we'lll need to back up the bookmark set if we are about to delete one
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001035 if (isBookmarkTable) {
Christopher Tatef0c36f72009-07-28 15:24:05 -07001036 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
1037 new String[] { BookmarkColumns.BOOKMARK },
1038 "_id = " + id, null, null);
1039 if (cursor.moveToNext()) {
1040 if (cursor.getInt(0) != 0) {
1041 // yep, this record is a bookmark
1042 mBackupManager.dataChanged();
1043 }
1044 }
1045 cursor.close();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001046 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001047
1048 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
1049 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001050 return count;
1051 }
1052
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +00001053 private int deleteGeolocation(Uri uri, String where, String[] whereArgs) {
1054 if (whereArgs.length != 1) {
1055 throw new IllegalArgumentException("Bad where arguments");
1056 }
1057 String origin = whereArgs[0];
1058 if (TextUtils.isEmpty(origin)) {
1059 throw new IllegalArgumentException("Empty origin");
1060 }
1061 GeolocationPermissions.getInstance().clear(origin);
1062 getContext().getContentResolver().notifyChange(Browser.GEOLOCATION_URI, null);
1063 return 1; // We always return 1, to avoid having to check whether anything was actually removed
1064 }
1065
The Android Open Source Project0c908882009-03-03 19:32:16 -08001066 @Override
1067 public int update(Uri url, ContentValues values, String where,
1068 String[] whereArgs) {
1069 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
1070
1071 int match = URI_MATCHER.match(url);
1072 if (match == -1 || match == URI_MATCH_SUGGEST) {
1073 throw new IllegalArgumentException("Unknown URL");
1074 }
1075
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001076 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001077 StringBuilder sb = new StringBuilder();
1078 if (where != null && where.length() > 0) {
1079 sb.append("( ");
1080 sb.append(where);
1081 sb.append(" ) AND ");
1082 }
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001083 String id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001084 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -07001085 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001086 where = sb.toString();
1087 }
1088
Christopher Tatef0c36f72009-07-28 15:24:05 -07001089 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001090
Christopher Tatef0c36f72009-07-28 15:24:05 -07001091 // Not all bookmark-table updates should be backed up. Look to see
1092 // whether we changed the title, url, or "is a bookmark" state, and
1093 // request a backup if so.
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001094 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_BOOKMARKS) {
1095 boolean changingBookmarks = false;
Christopher Tatef0c36f72009-07-28 15:24:05 -07001096 // Alterations to the bookmark field inherently change the bookmark
1097 // set, so we don't need to query the record; we know a priori that
1098 // we will need to back up this change.
1099 if (values.containsKey(BookmarkColumns.BOOKMARK)) {
1100 changingBookmarks = true;
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001101 } else if ((values.containsKey(BookmarkColumns.TITLE)
1102 || values.containsKey(BookmarkColumns.URL))
1103 && values.containsKey(BookmarkColumns._ID)) {
1104 // If a title or URL has been changed, check to see if it is to
1105 // a bookmark. The ID should have been included in the update,
1106 // so use it.
Christopher Tatef0c36f72009-07-28 15:24:05 -07001107 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
1108 new String[] { BookmarkColumns.BOOKMARK },
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001109 BookmarkColumns._ID + " = "
1110 + values.getAsString(BookmarkColumns._ID), null, null);
Christopher Tatef0c36f72009-07-28 15:24:05 -07001111 if (cursor.moveToNext()) {
1112 changingBookmarks = (cursor.getInt(0) != 0);
1113 }
1114 cursor.close();
1115 }
1116
1117 // if this *is* a bookmark row we're altering, we need to back it up.
1118 if (changingBookmarks) {
1119 mBackupManager.dataChanged();
1120 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001121 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001122
1123 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
1124 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001125 return ret;
1126 }
Satish Sampath565505b2009-05-29 15:37:27 +01001127
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001128 /**
1129 * Strips the provided url of preceding "http://" and any trailing "/". Does not
1130 * strip "https://". If the provided string cannot be stripped, the original string
1131 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +01001132 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001133 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +01001134 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001135 * @param url a url to strip, like "http://www.google.com/"
1136 * @return a stripped url like "www.google.com", or the original string if it could
1137 * not be stripped
1138 */
1139 private static String stripUrl(String url) {
1140 if (url == null) return null;
1141 Matcher m = STRIP_URL_PATTERN.matcher(url);
1142 if (m.matches() && m.groupCount() == 3) {
1143 return m.group(2);
1144 } else {
1145 return url;
1146 }
1147 }
1148
The Android Open Source Project0c908882009-03-03 19:32:16 -08001149}