blob: 501a52a73e1644aac3de672aa0d9729836cd21ae [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 Tateb6a65442010-03-05 15:47:48 -080021import android.app.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;
Leon Scroggins62b71f72009-06-12 17:51:22 -040040import android.os.Handler;
Andrei Popescu93bea962010-03-23 15:04:36 +000041import android.os.Process;
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;
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +000089 private static final int SUGGEST_COLUMN_TEXT_2_URL_ID = 5;
90 private static final int SUGGEST_COLUMN_ICON_1_ID = 6;
91 private static final int SUGGEST_COLUMN_ICON_2_ID = 7;
92 private static final int SUGGEST_COLUMN_QUERY_ID = 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,
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000102 SearchManager.SUGGEST_COLUMN_TEXT_2_URL,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800103 SearchManager.SUGGEST_COLUMN_ICON_1,
104 SearchManager.SUGGEST_COLUMN_ICON_2,
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700105 SearchManager.SUGGEST_COLUMN_QUERY,
Bjorn Bringert04851702009-09-22 10:36:01 +0100106 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
The Android Open Source Project0c908882009-03-03 19:32:16 -0800174 public BrowserProvider() {
175 }
Satish Sampath565505b2009-05-29 15:37:27 +0100176
Patrick Scott43914692010-02-19 10:10:10 -0500177 // XXX: This is a major hack to remove our dependency on gsf constants and
178 // its content provider. http://b/issue?id=2425179
179 static String getClientId(ContentResolver cr) {
180 String ret = "android-google";
181 Cursor c = null;
182 try {
183 c = cr.query(Uri.parse("content://com.google.settings/partner"),
184 new String[] { "value" }, "name='client_id'", null, null);
185 if (c != null && c.moveToNext()) {
186 ret = c.getString(0);
187 }
188 } catch (RuntimeException ex) {
189 // fall through to return the default
190 } finally {
191 if (c != null) {
192 c.close();
193 }
194 }
195 return ret;
196 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800197
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700198 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800199 StringBuffer sb = new StringBuffer();
200 int lastCharLoc = 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100201
Patrick Scott43914692010-02-19 10:10:10 -0500202 final String client_id = getClientId(context.getContentResolver());
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700203
The Android Open Source Project0c908882009-03-03 19:32:16 -0800204 for (int i = 0; i < srcString.length(); ++i) {
205 char c = srcString.charAt(i);
206 if (c == '{') {
207 sb.append(srcString.subSequence(lastCharLoc, i));
208 lastCharLoc = i;
209 inner:
210 for (int j = i; j < srcString.length(); ++j) {
211 char k = srcString.charAt(j);
212 if (k == '}') {
213 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700214 if (propertyKeyValue.equals("CLIENT_ID")) {
215 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800216 } else {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700217 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800218 }
219 lastCharLoc = j + 1;
220 i = j;
221 break inner;
222 }
223 }
224 }
225 }
226 if (srcString.length() - lastCharLoc > 0) {
227 // Put on the tail, if there is one
228 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
229 }
230 return sb;
231 }
232
233 private static class DatabaseHelper extends SQLiteOpenHelper {
234 private Context mContext;
235
236 public DatabaseHelper(Context context) {
237 super(context, sDatabaseName, null, DATABASE_VERSION);
238 mContext = context;
239 }
240
241 @Override
242 public void onCreate(SQLiteDatabase db) {
243 db.execSQL("CREATE TABLE bookmarks (" +
244 "_id INTEGER PRIMARY KEY," +
245 "title TEXT," +
246 "url TEXT," +
247 "visits INTEGER," +
248 "date LONG," +
249 "created LONG," +
250 "description TEXT," +
251 "bookmark INTEGER," +
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400252 "favicon BLOB DEFAULT NULL," +
Patrick Scott3918d442009-08-04 13:22:29 -0400253 "thumbnail BLOB DEFAULT NULL," +
Leon Scrogginsb4464432009-11-25 12:37:50 -0500254 "touch_icon BLOB DEFAULT NULL," +
255 "user_entered INTEGER" +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800256 ");");
257
258 final CharSequence[] bookmarks = mContext.getResources()
259 .getTextArray(R.array.bookmarks);
260 int size = bookmarks.length;
261 try {
262 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700263 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800264 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
265 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100266 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800267 "', 0, 0, 0, 1);");
268 }
269 } catch (ArrayIndexOutOfBoundsException e) {
270 }
271
272 db.execSQL("CREATE TABLE searches (" +
273 "_id INTEGER PRIMARY KEY," +
274 "search TEXT," +
275 "date LONG" +
276 ");");
277 }
278
279 @Override
280 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
281 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400282 + newVersion);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800283 if (oldVersion == 18) {
284 db.execSQL("DROP TABLE IF EXISTS labels");
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400285 }
286 if (oldVersion <= 19) {
287 db.execSQL("ALTER TABLE bookmarks ADD COLUMN thumbnail BLOB DEFAULT NULL;");
Patrick Scott3918d442009-08-04 13:22:29 -0400288 }
289 if (oldVersion < 21) {
290 db.execSQL("ALTER TABLE bookmarks ADD COLUMN touch_icon BLOB DEFAULT NULL;");
Grace Kloba6b52a552009-09-03 16:29:56 -0700291 }
292 if (oldVersion < 22) {
293 db.execSQL("DELETE FROM bookmarks WHERE (bookmark = 0 AND url LIKE \"%.google.%client=ms-%\")");
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100294 removeGears();
Leon Scrogginsb4464432009-11-25 12:37:50 -0500295 }
296 if (oldVersion < 23) {
297 db.execSQL("ALTER TABLE bookmarks ADD COLUMN user_entered INTEGER;");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800298 } else {
299 db.execSQL("DROP TABLE IF EXISTS bookmarks");
300 db.execSQL("DROP TABLE IF EXISTS searches");
301 onCreate(db);
302 }
303 }
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100304
305 private void removeGears() {
Andrei Popescu93bea962010-03-23 15:04:36 +0000306 new Thread() {
307 @Override
308 public void run() {
309 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100310 String browserDataDirString = mContext.getApplicationInfo().dataDir;
311 final String appPluginsDirString = "app_plugins";
312 final String gearsPrefix = "gears";
313 File appPluginsDir = new File(browserDataDirString + File.separator
314 + appPluginsDirString);
315 if (!appPluginsDir.exists()) {
Andrei Popescu93bea962010-03-23 15:04:36 +0000316 return;
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100317 }
318 // Delete the Gears plugin files
319 File[] gearsFiles = appPluginsDir.listFiles(new FilenameFilter() {
320 public boolean accept(File dir, String filename) {
321 return filename.startsWith(gearsPrefix);
322 }
323 });
324 for (int i = 0; i < gearsFiles.length; ++i) {
325 if (gearsFiles[i].isDirectory()) {
326 deleteDirectory(gearsFiles[i]);
327 } else {
328 gearsFiles[i].delete();
329 }
330 }
331 // Delete the Gears data files
332 File gearsDataDir = new File(browserDataDirString + File.separator
333 + gearsPrefix);
334 if (!gearsDataDir.exists()) {
Andrei Popescu93bea962010-03-23 15:04:36 +0000335 return;
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100336 }
337 deleteDirectory(gearsDataDir);
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100338 }
339
340 private void deleteDirectory(File currentDir) {
341 File[] files = currentDir.listFiles();
342 for (int i = 0; i < files.length; ++i) {
343 if (files[i].isDirectory()) {
344 deleteDirectory(files[i]);
345 }
346 files[i].delete();
347 }
348 currentDir.delete();
349 }
Andrei Popescu93bea962010-03-23 15:04:36 +0000350 }.start();
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100351 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800352 }
353
354 @Override
355 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700356 final Context context = getContext();
357 mOpenHelper = new DatabaseHelper(context);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700358 mBackupManager = new BackupManager(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100359 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700360 // To avoid erasing the bookmark table, we added it explicitly for
361 // version 18 and 19 as in the other cases, we will erase the table.
362 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
363 SharedPreferences p = PreferenceManager
364 .getDefaultSharedPreferences(context);
365 boolean fix = p.getBoolean("fix_picasa", true);
366 if (fix) {
367 fixPicasaBookmark();
368 Editor ed = p.edit();
369 ed.putBoolean("fix_picasa", false);
370 ed.commit();
371 }
372 }
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100373 mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400374 mShowWebSuggestionsSettingChangeObserver
375 = new ShowWebSuggestionsSettingChangeObserver();
376 context.getContentResolver().registerContentObserver(
377 Settings.System.getUriFor(
378 Settings.System.SHOW_WEB_SUGGESTIONS),
379 true, mShowWebSuggestionsSettingChangeObserver);
380 updateShowWebSuggestions();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800381 return true;
382 }
383
Leon Scroggins62b71f72009-06-12 17:51:22 -0400384 /**
385 * This Observer will ensure that if the user changes the system
386 * setting of whether to display web suggestions, we will
387 * change accordingly.
388 */
389 /* package */ class ShowWebSuggestionsSettingChangeObserver
390 extends ContentObserver {
391 public ShowWebSuggestionsSettingChangeObserver() {
392 super(new Handler());
393 }
394
395 @Override
396 public void onChange(boolean selfChange) {
397 updateShowWebSuggestions();
398 }
399 }
400
401 private ShowWebSuggestionsSettingChangeObserver
402 mShowWebSuggestionsSettingChangeObserver;
403
404 // If non-null, then the system is set to show web suggestions,
405 // and this is the SearchableInfo to use to get them.
406 private SearchableInfo mSearchableInfo;
407
408 /**
409 * Check the system settings to see whether web suggestions are
410 * allowed. If so, store the SearchableInfo to grab suggestions
411 * while the user is typing.
412 */
413 private void updateShowWebSuggestions() {
414 mSearchableInfo = null;
415 Context context = getContext();
416 if (Settings.System.getInt(context.getContentResolver(),
417 Settings.System.SHOW_WEB_SUGGESTIONS,
418 1 /* default on */) == 1) {
Bjorn Bringert32747542010-02-18 21:59:21 +0000419 ComponentName webSearchComponent = mSearchManager.getWebSearchActivity();
420 if (webSearchComponent != null) {
421 mSearchableInfo = mSearchManager.getSearchableInfo(webSearchComponent);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400422 }
423 }
424 }
425
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700426 private void fixPicasaBookmark() {
427 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
428 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
429 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
430 try {
431 if (!cursor.moveToFirst()) {
432 // set "created" so that it will be on the top of the list
433 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
434 "date, created, bookmark)" + " VALUES('" +
435 getContext().getString(R.string.picasa) + "', '"
436 + PICASA_URL + "', 0, 0, " + new Date().getTime()
437 + ", 1);");
438 }
439 } finally {
440 if (cursor != null) {
441 cursor.close();
442 }
443 }
444 }
445
The Android Open Source Project0c908882009-03-03 19:32:16 -0800446 /*
447 * Subclass AbstractCursor so we can combine multiple Cursors and add
Grace Kloba391df7c2010-03-01 19:51:49 -0800448 * "Search the web".
The Android Open Source Project0c908882009-03-03 19:32:16 -0800449 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100450 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
Grace Kloba391df7c2010-03-01 19:51:49 -0800451 * "Search the web";
452 * 2. If bookmark/history entries has a match, "Search the web" shows up at
453 * the second place. Otherwise, "Search the web" shows up at the first
454 * place.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800455 */
456 private class MySuggestionCursor extends AbstractCursor {
457 private Cursor mHistoryCursor;
458 private Cursor mSuggestCursor;
459 private int mHistoryCount;
460 private int mSuggestionCount;
Grace Klobad3992d42010-01-28 11:44:38 -0800461 private boolean mIncludeWebSearch;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800462 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100463 private int mSuggestText1Id;
464 private int mSuggestText2Id;
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000465 private int mSuggestText2UrlId;
Satish Sampath565505b2009-05-29 15:37:27 +0100466 private int mSuggestQueryId;
Bjorn Bringert04851702009-09-22 10:36:01 +0100467 private int mSuggestIntentExtraDataId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800468
469 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
470 mHistoryCursor = hc;
471 mSuggestCursor = sc;
472 mHistoryCount = hc.getCount();
473 mSuggestionCount = sc != null ? sc.getCount() : 0;
474 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
475 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
476 }
477 mString = string;
Grace Klobad3992d42010-01-28 11:44:38 -0800478 mIncludeWebSearch = string.length() > 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100479
480 // Some web suggest providers only give suggestions and have no description string for
481 // items. The order of the result columns may be different as well. So retrieve the
482 // column indices for the fields we need now and check before using below.
483 if (mSuggestCursor == null) {
484 mSuggestText1Id = -1;
485 mSuggestText2Id = -1;
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000486 mSuggestText2UrlId = -1;
Satish Sampath565505b2009-05-29 15:37:27 +0100487 mSuggestQueryId = -1;
Bjorn Bringert04851702009-09-22 10:36:01 +0100488 mSuggestIntentExtraDataId = -1;
Satish Sampath565505b2009-05-29 15:37:27 +0100489 } else {
490 mSuggestText1Id = mSuggestCursor.getColumnIndex(
491 SearchManager.SUGGEST_COLUMN_TEXT_1);
492 mSuggestText2Id = mSuggestCursor.getColumnIndex(
493 SearchManager.SUGGEST_COLUMN_TEXT_2);
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000494 mSuggestText2UrlId = mSuggestCursor.getColumnIndex(
495 SearchManager.SUGGEST_COLUMN_TEXT_2_URL);
Satish Sampath565505b2009-05-29 15:37:27 +0100496 mSuggestQueryId = mSuggestCursor.getColumnIndex(
497 SearchManager.SUGGEST_COLUMN_QUERY);
Bjorn Bringert04851702009-09-22 10:36:01 +0100498 mSuggestIntentExtraDataId = mSuggestCursor.getColumnIndex(
499 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA);
Satish Sampath565505b2009-05-29 15:37:27 +0100500 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800501 }
502
503 @Override
504 public boolean onMove(int oldPosition, int newPosition) {
505 if (mHistoryCursor == null) {
506 return false;
507 }
Grace Klobad3992d42010-01-28 11:44:38 -0800508 if (mIncludeWebSearch) {
Grace Kloba391df7c2010-03-01 19:51:49 -0800509 if (mHistoryCount == 0 && newPosition == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800510 return true;
Grace Kloba391df7c2010-03-01 19:51:49 -0800511 } else if (mHistoryCount > 0) {
512 if (newPosition == 0) {
513 mHistoryCursor.moveToPosition(0);
514 return true;
515 } else if (newPosition == 1) {
516 return true;
517 }
Grace Klobad3992d42010-01-28 11:44:38 -0800518 }
Grace Kloba391df7c2010-03-01 19:51:49 -0800519 newPosition--;
Grace Klobad3992d42010-01-28 11:44:38 -0800520 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800521 if (mHistoryCount > newPosition) {
522 mHistoryCursor.moveToPosition(newPosition);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800523 } else {
Grace Klobad3992d42010-01-28 11:44:38 -0800524 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800525 }
526 return true;
527 }
528
529 @Override
530 public int getCount() {
Grace Klobad3992d42010-01-28 11:44:38 -0800531 if (mIncludeWebSearch) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800532 return mHistoryCount + mSuggestionCount + 1;
533 } else {
534 return mHistoryCount + mSuggestionCount;
535 }
536 }
537
538 @Override
539 public String[] getColumnNames() {
540 return COLUMNS;
541 }
Satish Sampath565505b2009-05-29 15:37:27 +0100542
The Android Open Source Project0c908882009-03-03 19:32:16 -0800543 @Override
544 public String getString(int columnIndex) {
545 if ((mPos != -1 && mHistoryCursor != null)) {
Grace Kloba391df7c2010-03-01 19:51:49 -0800546 int type = -1; // 0: web search; 1: history; 2: suggestion
547 if (mIncludeWebSearch) {
548 if (mHistoryCount == 0 && mPos == 0) {
549 type = 0;
550 } else if (mHistoryCount > 0) {
551 if (mPos == 0) {
552 type = 1;
553 } else if (mPos == 1) {
554 type = 0;
555 }
556 }
557 if (type == -1) type = (mPos - 1) < mHistoryCount ? 1 : 2;
558 } else {
559 type = mPos < mHistoryCount ? 1 : 2;
560 }
561
The Android Open Source Project0c908882009-03-03 19:32:16 -0800562 switch(columnIndex) {
563 case SUGGEST_COLUMN_INTENT_ACTION_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800564 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800565 return Intent.ACTION_VIEW;
566 } else {
567 return Intent.ACTION_SEARCH;
568 }
569
570 case SUGGEST_COLUMN_INTENT_DATA_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800571 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800572 return mHistoryCursor.getString(1);
573 } else {
574 return null;
575 }
576
577 case SUGGEST_COLUMN_TEXT_1_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800578 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800579 return mString;
Grace Kloba391df7c2010-03-01 19:51:49 -0800580 } else if (type == 1) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700581 return getHistoryTitle();
Grace Klobad3992d42010-01-28 11:44:38 -0800582 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100583 if (mSuggestText1Id == -1) return null;
584 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800585 }
586
587 case SUGGEST_COLUMN_TEXT_2_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800588 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800589 return getContext().getString(R.string.search_the_web);
Grace Kloba391df7c2010-03-01 19:51:49 -0800590 } else if (type == 1) {
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000591 return null; // Use TEXT_2_URL instead
Grace Klobad3992d42010-01-28 11:44:38 -0800592 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100593 if (mSuggestText2Id == -1) return null;
594 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800595 }
596
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000597 case SUGGEST_COLUMN_TEXT_2_URL_ID:
598 if (type == 0) {
599 return null;
600 } else if (type == 1) {
601 return getHistoryUrl();
602 } else {
603 if (mSuggestText2UrlId == -1) return null;
604 return mSuggestCursor.getString(mSuggestText2UrlId);
605 }
606
The Android Open Source Project0c908882009-03-03 19:32:16 -0800607 case SUGGEST_COLUMN_ICON_1_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800608 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800609 if (mHistoryCursor.getInt(3) == 1) {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400610 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800611 R.drawable.ic_search_category_bookmark)
612 .toString();
613 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400614 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800615 R.drawable.ic_search_category_history)
616 .toString();
617 }
618 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400619 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800620 R.drawable.ic_search_category_suggest)
621 .toString();
622 }
623
624 case SUGGEST_COLUMN_ICON_2_ID:
Leon Scroggins31887fd2009-05-18 16:58:08 -0400625 return "0";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800626
627 case SUGGEST_COLUMN_QUERY_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800628 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800629 return mString;
Grace Kloba391df7c2010-03-01 19:51:49 -0800630 } else if (type == 1) {
Mike LeBeau2af73052009-06-23 17:36:59 -0700631 // Return the url in the intent query column. This is ignored
632 // within the browser because our searchable is set to
633 // android:searchMode="queryRewriteFromData", but it is used by
634 // global search for query rewriting.
635 return mHistoryCursor.getString(1);
Grace Klobad3992d42010-01-28 11:44:38 -0800636 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100637 if (mSuggestQueryId == -1) return null;
638 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800639 }
Satish Sampath565505b2009-05-29 15:37:27 +0100640
Bjorn Bringert04851702009-09-22 10:36:01 +0100641 case SUGGEST_COLUMN_INTENT_EXTRA_DATA:
Grace Kloba391df7c2010-03-01 19:51:49 -0800642 if (type == 0) {
Bjorn Bringert04851702009-09-22 10:36:01 +0100643 return null;
Grace Kloba391df7c2010-03-01 19:51:49 -0800644 } else if (type == 1) {
Grace Klobad3992d42010-01-28 11:44:38 -0800645 return null;
646 } else {
Bjorn Bringert04851702009-09-22 10:36:01 +0100647 if (mSuggestIntentExtraDataId == -1) return null;
648 return mSuggestCursor.getString(mSuggestIntentExtraDataId);
Bjorn Bringert04851702009-09-22 10:36:01 +0100649 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800650 }
651 }
652 return null;
653 }
654
655 @Override
656 public double getDouble(int column) {
657 throw new UnsupportedOperationException();
658 }
659
660 @Override
661 public float getFloat(int column) {
662 throw new UnsupportedOperationException();
663 }
664
665 @Override
666 public int getInt(int column) {
667 throw new UnsupportedOperationException();
668 }
669
670 @Override
671 public long getLong(int column) {
672 if ((mPos != -1) && column == 0) {
673 return mPos; // use row# as the _Id
674 }
675 throw new UnsupportedOperationException();
676 }
677
678 @Override
679 public short getShort(int column) {
680 throw new UnsupportedOperationException();
681 }
682
683 @Override
684 public boolean isNull(int column) {
685 throw new UnsupportedOperationException();
686 }
687
688 // TODO Temporary change, finalize after jq's changes go in
689 public void deactivate() {
690 if (mHistoryCursor != null) {
691 mHistoryCursor.deactivate();
692 }
693 if (mSuggestCursor != null) {
694 mSuggestCursor.deactivate();
695 }
696 super.deactivate();
697 }
698
699 public boolean requery() {
700 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
701 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
702 }
703
704 // TODO Temporary change, finalize after jq's changes go in
705 public void close() {
706 super.close();
707 if (mHistoryCursor != null) {
708 mHistoryCursor.close();
709 mHistoryCursor = null;
710 }
711 if (mSuggestCursor != null) {
712 mSuggestCursor.close();
713 mSuggestCursor = null;
714 }
715 }
Satish Sampath565505b2009-05-29 15:37:27 +0100716
Mike LeBeau21beb132009-05-13 14:57:50 -0700717 /**
718 * Provides the title (text line 1) for a browser suggestion, which should be the
719 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100720 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700721 * @return the title string to use
722 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700723 private String getHistoryTitle() {
724 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700725 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000726 title = stripUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700727 }
728 return title;
729 }
Satish Sampath565505b2009-05-29 15:37:27 +0100730
Mike LeBeau21beb132009-05-13 14:57:50 -0700731 /**
732 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
733 * webpage url. If the webpage title is empty, then the url should go in the title
734 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100735 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700736 * @return the subtitle string to use, or null if none
737 */
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000738 private String getHistoryUrl() {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700739 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700740 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
741 return null;
742 } else {
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000743 return stripUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700744 }
745 }
Satish Sampath565505b2009-05-29 15:37:27 +0100746
The Android Open Source Project0c908882009-03-03 19:32:16 -0800747 }
748
Leon Scroggins58d56c62010-01-28 15:12:40 -0500749 private static class ResultsCursor extends AbstractCursor {
750 // Array indices for RESULTS_COLUMNS
751 private static final int RESULT_ACTION_ID = 1;
752 private static final int RESULT_DATA_ID = 2;
753 private static final int RESULT_TEXT_ID = 3;
754 private static final int RESULT_ICON_ID = 4;
755 private static final int RESULT_EXTRA_ID = 5;
756
757 private static final String[] RESULTS_COLUMNS = new String[] {
758 "_id",
759 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
760 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
761 SearchManager.SUGGEST_COLUMN_TEXT_1,
762 SearchManager.SUGGEST_COLUMN_ICON_1,
763 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA
764 };
765 private final ArrayList<String> mResults;
766 public ResultsCursor(ArrayList<String> results) {
767 mResults = results;
768 }
769 public int getCount() { return mResults.size(); }
770
771 public String[] getColumnNames() {
772 return RESULTS_COLUMNS;
773 }
774
775 public String getString(int column) {
776 switch (column) {
777 case RESULT_ACTION_ID:
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500778 return RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500779 case RESULT_TEXT_ID:
780 // The data is used when the phone is in landscape mode. We
781 // still want to show the result string.
782 case RESULT_DATA_ID:
783 return mResults.get(mPos);
784 case RESULT_EXTRA_ID:
785 // The Intent's extra data will store the index into
786 // mResults so the BrowserActivity will know which result to
787 // use.
788 return Integer.toString(mPos);
789 case RESULT_ICON_ID:
790 return Integer.valueOf(R.drawable.magnifying_glass)
791 .toString();
792 default:
793 return null;
794 }
795 }
796 public short getShort(int column) {
797 throw new UnsupportedOperationException();
798 }
799 public int getInt(int column) {
800 throw new UnsupportedOperationException();
801 }
802 public long getLong(int column) {
803 if ((mPos != -1) && column == 0) {
804 return mPos; // use row# as the _id
805 }
806 throw new UnsupportedOperationException();
807 }
808 public float getFloat(int column) {
809 throw new UnsupportedOperationException();
810 }
811 public double getDouble(int column) {
812 throw new UnsupportedOperationException();
813 }
814 public boolean isNull(int column) {
815 throw new UnsupportedOperationException();
816 }
817 }
818
819 private ResultsCursor mResultsCursor;
820
821 /**
822 * Provide a set of results to be returned to query, intended to be used
823 * by the SearchDialog when the BrowserActivity is in voice search mode.
824 * @param results Strings to display in the dropdown from the SearchDialog
825 */
826 /* package */ void setQueryResults(ArrayList<String> results) {
827 if (results == null) {
828 mResultsCursor = null;
829 } else {
830 mResultsCursor = new ResultsCursor(results);
831 }
832 }
833
The Android Open Source Project0c908882009-03-03 19:32:16 -0800834 @Override
835 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100836 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800837 throws IllegalStateException {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800838 int match = URI_MATCHER.match(url);
839 if (match == -1) {
840 throw new IllegalArgumentException("Unknown URL");
841 }
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000842 if (match == URI_MATCH_GEOLOCATION) {
843 throw new UnsupportedOperationException("query() not supported for geolocation");
844 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500845 if (match == URI_MATCH_SUGGEST && mResultsCursor != null) {
846 Cursor results = mResultsCursor;
847 mResultsCursor = null;
848 return results;
849 }
850 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800851
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100852 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800853 String suggestSelection;
854 String [] myArgs;
855 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
856 suggestSelection = null;
857 myArgs = null;
858 } else {
859 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700860 if (selectionArgs[0].startsWith("http")
861 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800862 myArgs = new String[1];
863 myArgs[0] = like;
864 suggestSelection = selection;
865 } else {
866 SUGGEST_ARGS[0] = "http://" + like;
867 SUGGEST_ARGS[1] = "http://www." + like;
868 SUGGEST_ARGS[2] = "https://" + like;
869 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400870 // To match against titles.
871 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800872 myArgs = SUGGEST_ARGS;
873 suggestSelection = SUGGEST_SELECTION;
874 }
875 }
876
877 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
878 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Leon Scroggins31887fd2009-05-18 16:58:08 -0400879 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800880
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100881 if (match == URI_MATCH_BOOKMARKS_SUGGEST
Dan Egnor5ee906c2009-11-18 12:11:49 -0800882 || Patterns.WEB_URL.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800883 return new MySuggestionCursor(c, null, "");
884 } else {
885 // get Google suggest if there is still space in the list
886 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400887 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800888 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100889 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400890 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800891 }
892 return new MySuggestionCursor(c, null, selectionArgs[0]);
893 }
894 }
895
896 String[] projection = null;
897 if (projectionIn != null && projectionIn.length > 0) {
898 projection = new String[projectionIn.length + 1];
899 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
900 projection[projectionIn.length] = "_id AS _id";
901 }
902
903 StringBuilder whereClause = new StringBuilder(256);
904 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
905 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
906 .append(")");
907 }
908
909 // Tack on the user's selection, if present
910 if (selection != null && selection.length() > 0) {
911 if (whereClause.length() > 0) {
912 whereClause.append(" AND ");
913 }
914
915 whereClause.append('(');
916 whereClause.append(selection);
917 whereClause.append(')');
918 }
919 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
920 whereClause.toString(), selectionArgs, null, null, sortOrder,
921 null);
922 c.setNotificationUri(getContext().getContentResolver(), url);
923 return c;
924 }
925
926 @Override
927 public String getType(Uri url) {
928 int match = URI_MATCHER.match(url);
929 switch (match) {
930 case URI_MATCH_BOOKMARKS:
931 return "vnd.android.cursor.dir/bookmark";
932
933 case URI_MATCH_BOOKMARKS_ID:
934 return "vnd.android.cursor.item/bookmark";
935
936 case URI_MATCH_SEARCHES:
937 return "vnd.android.cursor.dir/searches";
938
939 case URI_MATCH_SEARCHES_ID:
940 return "vnd.android.cursor.item/searches";
941
942 case URI_MATCH_SUGGEST:
943 return SearchManager.SUGGEST_MIME_TYPE;
944
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000945 case URI_MATCH_GEOLOCATION:
946 return "vnd.android.cursor.dir/geolocation";
947
The Android Open Source Project0c908882009-03-03 19:32:16 -0800948 default:
949 throw new IllegalArgumentException("Unknown URL");
950 }
951 }
952
953 @Override
954 public Uri insert(Uri url, ContentValues initialValues) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700955 boolean isBookmarkTable = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800956 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
957
958 int match = URI_MATCHER.match(url);
959 Uri uri = null;
960 switch (match) {
961 case URI_MATCH_BOOKMARKS: {
962 // Insert into the bookmarks table
963 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
964 initialValues);
965 if (rowID > 0) {
966 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
967 rowID);
968 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700969 isBookmarkTable = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800970 break;
971 }
972
973 case URI_MATCH_SEARCHES: {
974 // Insert into the searches table
975 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
976 initialValues);
977 if (rowID > 0) {
978 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
979 rowID);
980 }
981 break;
982 }
983
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000984 case URI_MATCH_GEOLOCATION:
985 String origin = initialValues.getAsString(Browser.GeolocationColumns.ORIGIN);
986 if (TextUtils.isEmpty(origin)) {
987 throw new IllegalArgumentException("Empty origin");
988 }
989 GeolocationPermissions.getInstance().allow(origin);
990 // TODO: Should we have one URI per permission?
991 uri = Browser.GEOLOCATION_URI;
992 break;
993
The Android Open Source Project0c908882009-03-03 19:32:16 -0800994 default:
995 throw new IllegalArgumentException("Unknown URL");
996 }
997
998 if (uri == null) {
999 throw new IllegalArgumentException("Unknown URL");
1000 }
1001 getContext().getContentResolver().notifyChange(uri, null);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001002
Christopher Tatef0c36f72009-07-28 15:24:05 -07001003 // Back up the new bookmark set if we just inserted one.
1004 // A row created when bookmarks are added from scratch will have
1005 // bookmark=1 in the initial value set.
1006 if (isBookmarkTable
1007 && initialValues.containsKey(BookmarkColumns.BOOKMARK)
1008 && initialValues.getAsInteger(BookmarkColumns.BOOKMARK) != 0) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001009 mBackupManager.dataChanged();
1010 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001011 return uri;
1012 }
1013
1014 @Override
1015 public int delete(Uri url, String where, String[] whereArgs) {
1016 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
1017
1018 int match = URI_MATCHER.match(url);
1019 if (match == -1 || match == URI_MATCH_SUGGEST) {
1020 throw new IllegalArgumentException("Unknown URL");
1021 }
1022
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +00001023 if (match == URI_MATCH_GEOLOCATION) {
1024 return deleteGeolocation(url, where, whereArgs);
1025 }
1026
Christopher Tatef0c36f72009-07-28 15:24:05 -07001027 // need to know whether it's the bookmarks table for a couple of reasons
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001028 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
Christopher Tatef0c36f72009-07-28 15:24:05 -07001029 String id = null;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001030
1031 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001032 StringBuilder sb = new StringBuilder();
1033 if (where != null && where.length() > 0) {
1034 sb.append("( ");
1035 sb.append(where);
1036 sb.append(" ) AND ");
1037 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001038 id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001039 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -07001040 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001041 where = sb.toString();
1042 }
1043
Christopher Tatef0c36f72009-07-28 15:24:05 -07001044 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001045
Christopher Tatef0c36f72009-07-28 15:24:05 -07001046 // we'lll need to back up the bookmark set if we are about to delete one
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001047 if (isBookmarkTable) {
Christopher Tatef0c36f72009-07-28 15:24:05 -07001048 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
1049 new String[] { BookmarkColumns.BOOKMARK },
1050 "_id = " + id, null, null);
1051 if (cursor.moveToNext()) {
1052 if (cursor.getInt(0) != 0) {
1053 // yep, this record is a bookmark
1054 mBackupManager.dataChanged();
1055 }
1056 }
1057 cursor.close();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001058 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001059
1060 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
1061 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001062 return count;
1063 }
1064
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +00001065 private int deleteGeolocation(Uri uri, String where, String[] whereArgs) {
1066 if (whereArgs.length != 1) {
1067 throw new IllegalArgumentException("Bad where arguments");
1068 }
1069 String origin = whereArgs[0];
1070 if (TextUtils.isEmpty(origin)) {
1071 throw new IllegalArgumentException("Empty origin");
1072 }
1073 GeolocationPermissions.getInstance().clear(origin);
1074 getContext().getContentResolver().notifyChange(Browser.GEOLOCATION_URI, null);
1075 return 1; // We always return 1, to avoid having to check whether anything was actually removed
1076 }
1077
The Android Open Source Project0c908882009-03-03 19:32:16 -08001078 @Override
1079 public int update(Uri url, ContentValues values, String where,
1080 String[] whereArgs) {
1081 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
1082
1083 int match = URI_MATCHER.match(url);
1084 if (match == -1 || match == URI_MATCH_SUGGEST) {
1085 throw new IllegalArgumentException("Unknown URL");
1086 }
1087
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001088 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001089 StringBuilder sb = new StringBuilder();
1090 if (where != null && where.length() > 0) {
1091 sb.append("( ");
1092 sb.append(where);
1093 sb.append(" ) AND ");
1094 }
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001095 String id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001096 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -07001097 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001098 where = sb.toString();
1099 }
1100
Christopher Tatef0c36f72009-07-28 15:24:05 -07001101 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001102
Christopher Tatef0c36f72009-07-28 15:24:05 -07001103 // Not all bookmark-table updates should be backed up. Look to see
1104 // whether we changed the title, url, or "is a bookmark" state, and
1105 // request a backup if so.
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001106 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_BOOKMARKS) {
1107 boolean changingBookmarks = false;
Christopher Tatef0c36f72009-07-28 15:24:05 -07001108 // Alterations to the bookmark field inherently change the bookmark
1109 // set, so we don't need to query the record; we know a priori that
1110 // we will need to back up this change.
1111 if (values.containsKey(BookmarkColumns.BOOKMARK)) {
1112 changingBookmarks = true;
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001113 } else if ((values.containsKey(BookmarkColumns.TITLE)
1114 || values.containsKey(BookmarkColumns.URL))
1115 && values.containsKey(BookmarkColumns._ID)) {
1116 // If a title or URL has been changed, check to see if it is to
1117 // a bookmark. The ID should have been included in the update,
1118 // so use it.
Christopher Tatef0c36f72009-07-28 15:24:05 -07001119 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
1120 new String[] { BookmarkColumns.BOOKMARK },
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001121 BookmarkColumns._ID + " = "
1122 + values.getAsString(BookmarkColumns._ID), null, null);
Christopher Tatef0c36f72009-07-28 15:24:05 -07001123 if (cursor.moveToNext()) {
1124 changingBookmarks = (cursor.getInt(0) != 0);
1125 }
1126 cursor.close();
1127 }
1128
1129 // if this *is* a bookmark row we're altering, we need to back it up.
1130 if (changingBookmarks) {
1131 mBackupManager.dataChanged();
1132 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001133 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001134
1135 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
1136 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001137 return ret;
1138 }
Satish Sampath565505b2009-05-29 15:37:27 +01001139
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001140 /**
1141 * Strips the provided url of preceding "http://" and any trailing "/". Does not
1142 * strip "https://". If the provided string cannot be stripped, the original string
1143 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +01001144 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001145 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +01001146 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001147 * @param url a url to strip, like "http://www.google.com/"
1148 * @return a stripped url like "www.google.com", or the original string if it could
1149 * not be stripped
1150 */
1151 private static String stripUrl(String url) {
1152 if (url == null) return null;
1153 Matcher m = STRIP_URL_PATTERN.matcher(url);
1154 if (m.matches() && m.groupCount() == 3) {
1155 return m.group(2);
1156 } else {
1157 return url;
1158 }
1159 }
1160
The Android Open Source Project0c908882009-03-03 19:32:16 -08001161}