blob: 70641807ed66650c05f95ab84e6828f854b2c9a6 [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;
Michael Kolbfe251992010-07-08 15:41:55 -070032import android.content.res.Configuration;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.database.AbstractCursor;
Leon Scroggins62b71f72009-06-12 17:51:22 -040034import android.database.ContentObserver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.database.sqlite.SQLiteDatabase;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010037import android.database.sqlite.SQLiteOpenHelper;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.net.Uri;
Leon Scroggins62b71f72009-06-12 17:51:22 -040039import android.os.Handler;
Andrei Popescu93bea962010-03-23 15:04:36 +000040import android.os.Process;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070041import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042import android.provider.Browser;
Leon Scroggins62b71f72009-06-12 17:51:22 -040043import android.provider.Settings;
Christopher Tatef0c36f72009-07-28 15:24:05 -070044import android.provider.Browser.BookmarkColumns;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050045import android.speech.RecognizerResultsIntent;
Mike LeBeau21beb132009-05-13 14:57:50 -070046import android.text.TextUtils;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010047import android.util.Log;
Dianne Hackborn385effd2010-02-24 20:03:04 -080048import android.util.Patterns;
Dan Egnor5ee906c2009-11-18 12:11:49 -080049
Andrei Popescu1b20b9d2009-09-21 18:49:42 +010050import java.io.File;
51import java.io.FilenameFilter;
Leon Scroggins58d56c62010-01-28 15:12:40 -050052import java.util.ArrayList;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010053import java.util.Date;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070054import java.util.regex.Matcher;
55import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -080056
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070057
The Android Open Source Project0c908882009-03-03 19:32:16 -080058public class BrowserProvider extends ContentProvider {
59
60 private SQLiteOpenHelper mOpenHelper;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070061 private BackupManager mBackupManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080062 private static final String sDatabaseName = "browser.db";
63 private static final String TAG = "BrowserProvider";
64 private static final String ORDER_BY = "visits DESC, date DESC";
65
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070066 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
67 "viewer?source=androidclient";
68
The Android Open Source Project0c908882009-03-03 19:32:16 -080069 private static final String[] TABLE_NAMES = new String[] {
Bjorn Bringerta7611812010-03-24 11:12:02 +000070 "bookmarks", "searches"
The Android Open Source Project0c908882009-03-03 19:32:16 -080071 };
72 private static final String[] SUGGEST_PROJECTION = new String[] {
Leon Scrogginsb4464432009-11-25 12:37:50 -050073 "_id", "url", "title", "bookmark", "user_entered"
The Android Open Source Project0c908882009-03-03 19:32:16 -080074 };
Satish Sampath565505b2009-05-29 15:37:27 +010075 private static final String SUGGEST_SELECTION =
Leon Scrogginsb4464432009-11-25 12:37:50 -050076 "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?"
77 + " OR title LIKE ?) AND (bookmark = 1 OR user_entered = 1)";
Leon Scrogginsbd359cc2009-05-26 15:57:35 -040078 private String[] SUGGEST_ARGS = new String[5];
The Android Open Source Project0c908882009-03-03 19:32:16 -080079
80 // shared suggestion array index, make sure to match COLUMNS
81 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
82 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
83 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
84 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +000085 private static final int SUGGEST_COLUMN_TEXT_2_URL_ID = 5;
86 private static final int SUGGEST_COLUMN_ICON_1_ID = 6;
87 private static final int SUGGEST_COLUMN_ICON_2_ID = 7;
88 private static final int SUGGEST_COLUMN_QUERY_ID = 8;
Bjorn Bringert04851702009-09-22 10:36:01 +010089 private static final int SUGGEST_COLUMN_INTENT_EXTRA_DATA = 9;
The Android Open Source Project0c908882009-03-03 19:32:16 -080090
Michael Kolbfe251992010-07-08 15:41:55 -070091 // how many suggestions will be shown in dropdown
92 // 0..SHORT: filled by browser db
93 private static final int MAX_SUGGEST_SHORT_SMALL = 3;
94 // SHORT..LONG: filled by search suggestions
95 private static final int MAX_SUGGEST_LONG_SMALL = 6;
96
97 // large screen size shows more
98 private static final int MAX_SUGGEST_SHORT_LARGE = 6;
99 private static final int MAX_SUGGEST_LONG_LARGE = 9;
100
101
The Android Open Source Project0c908882009-03-03 19:32:16 -0800102 // shared suggestion columns
103 private static final String[] COLUMNS = new String[] {
104 "_id",
105 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
106 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
107 SearchManager.SUGGEST_COLUMN_TEXT_1,
108 SearchManager.SUGGEST_COLUMN_TEXT_2,
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000109 SearchManager.SUGGEST_COLUMN_TEXT_2_URL,
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110 SearchManager.SUGGEST_COLUMN_ICON_1,
111 SearchManager.SUGGEST_COLUMN_ICON_2,
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700112 SearchManager.SUGGEST_COLUMN_QUERY,
Bjorn Bringert04851702009-09-22 10:36:01 +0100113 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA};
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114
The Android Open Source Project0c908882009-03-03 19:32:16 -0800115
116 // make sure that these match the index of TABLE_NAMES
117 private static final int URI_MATCH_BOOKMARKS = 0;
118 private static final int URI_MATCH_SEARCHES = 1;
119 // (id % 10) should match the table name index
120 private static final int URI_MATCH_BOOKMARKS_ID = 10;
121 private static final int URI_MATCH_SEARCHES_ID = 11;
122 //
123 private static final int URI_MATCH_SUGGEST = 20;
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100124 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800125
126 private static final UriMatcher URI_MATCHER;
127
128 static {
129 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
130 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
131 URI_MATCH_BOOKMARKS);
132 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
133 URI_MATCH_BOOKMARKS_ID);
134 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
135 URI_MATCH_SEARCHES);
136 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
137 URI_MATCH_SEARCHES_ID);
138 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
139 URI_MATCH_SUGGEST);
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100140 URI_MATCHER.addURI("browser",
141 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
142 URI_MATCH_BOOKMARKS_SUGGEST);
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
Michael Kolbfe251992010-07-08 15:41:55 -0700174 private int mMaxSuggestionShortSize;
175 private int mMaxSuggestionLongSize;
176
The Android Open Source Project0c908882009-03-03 19:32:16 -0800177 public BrowserProvider() {
178 }
Satish Sampath565505b2009-05-29 15:37:27 +0100179
Patrick Scott43914692010-02-19 10:10:10 -0500180 // XXX: This is a major hack to remove our dependency on gsf constants and
181 // its content provider. http://b/issue?id=2425179
182 static String getClientId(ContentResolver cr) {
183 String ret = "android-google";
184 Cursor c = null;
185 try {
186 c = cr.query(Uri.parse("content://com.google.settings/partner"),
187 new String[] { "value" }, "name='client_id'", null, null);
188 if (c != null && c.moveToNext()) {
189 ret = c.getString(0);
190 }
191 } catch (RuntimeException ex) {
192 // fall through to return the default
193 } finally {
194 if (c != null) {
195 c.close();
196 }
197 }
198 return ret;
199 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800200
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700201 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800202 StringBuffer sb = new StringBuffer();
203 int lastCharLoc = 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100204
Patrick Scott43914692010-02-19 10:10:10 -0500205 final String client_id = getClientId(context.getContentResolver());
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700206
The Android Open Source Project0c908882009-03-03 19:32:16 -0800207 for (int i = 0; i < srcString.length(); ++i) {
208 char c = srcString.charAt(i);
209 if (c == '{') {
210 sb.append(srcString.subSequence(lastCharLoc, i));
211 lastCharLoc = i;
212 inner:
213 for (int j = i; j < srcString.length(); ++j) {
214 char k = srcString.charAt(j);
215 if (k == '}') {
216 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700217 if (propertyKeyValue.equals("CLIENT_ID")) {
218 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800219 } else {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700220 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800221 }
222 lastCharLoc = j + 1;
223 i = j;
224 break inner;
225 }
226 }
227 }
228 }
229 if (srcString.length() - lastCharLoc > 0) {
230 // Put on the tail, if there is one
231 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
232 }
233 return sb;
234 }
235
236 private static class DatabaseHelper extends SQLiteOpenHelper {
237 private Context mContext;
238
239 public DatabaseHelper(Context context) {
240 super(context, sDatabaseName, null, DATABASE_VERSION);
241 mContext = context;
242 }
243
244 @Override
245 public void onCreate(SQLiteDatabase db) {
246 db.execSQL("CREATE TABLE bookmarks (" +
247 "_id INTEGER PRIMARY KEY," +
248 "title TEXT," +
249 "url TEXT," +
250 "visits INTEGER," +
251 "date LONG," +
252 "created LONG," +
253 "description TEXT," +
254 "bookmark INTEGER," +
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400255 "favicon BLOB DEFAULT NULL," +
Patrick Scott3918d442009-08-04 13:22:29 -0400256 "thumbnail BLOB DEFAULT NULL," +
Leon Scrogginsb4464432009-11-25 12:37:50 -0500257 "touch_icon BLOB DEFAULT NULL," +
258 "user_entered INTEGER" +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800259 ");");
260
261 final CharSequence[] bookmarks = mContext.getResources()
262 .getTextArray(R.array.bookmarks);
263 int size = bookmarks.length;
264 try {
265 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700266 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800267 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
268 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100269 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800270 "', 0, 0, 0, 1);");
271 }
272 } catch (ArrayIndexOutOfBoundsException e) {
273 }
274
275 db.execSQL("CREATE TABLE searches (" +
276 "_id INTEGER PRIMARY KEY," +
277 "search TEXT," +
278 "date LONG" +
279 ");");
280 }
281
282 @Override
283 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
284 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400285 + newVersion);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800286 if (oldVersion == 18) {
287 db.execSQL("DROP TABLE IF EXISTS labels");
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400288 }
289 if (oldVersion <= 19) {
290 db.execSQL("ALTER TABLE bookmarks ADD COLUMN thumbnail BLOB DEFAULT NULL;");
Patrick Scott3918d442009-08-04 13:22:29 -0400291 }
292 if (oldVersion < 21) {
293 db.execSQL("ALTER TABLE bookmarks ADD COLUMN touch_icon BLOB DEFAULT NULL;");
Grace Kloba6b52a552009-09-03 16:29:56 -0700294 }
295 if (oldVersion < 22) {
296 db.execSQL("DELETE FROM bookmarks WHERE (bookmark = 0 AND url LIKE \"%.google.%client=ms-%\")");
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100297 removeGears();
Leon Scrogginsb4464432009-11-25 12:37:50 -0500298 }
299 if (oldVersion < 23) {
300 db.execSQL("ALTER TABLE bookmarks ADD COLUMN user_entered INTEGER;");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800301 } else {
302 db.execSQL("DROP TABLE IF EXISTS bookmarks");
303 db.execSQL("DROP TABLE IF EXISTS searches");
304 onCreate(db);
305 }
306 }
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100307
308 private void removeGears() {
Andrei Popescu93bea962010-03-23 15:04:36 +0000309 new Thread() {
310 @Override
311 public void run() {
312 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100313 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()) {
Andrei Popescu93bea962010-03-23 15:04:36 +0000319 return;
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100320 }
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()) {
Andrei Popescu93bea962010-03-23 15:04:36 +0000338 return;
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100339 }
340 deleteDirectory(gearsDataDir);
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100341 }
342
343 private void deleteDirectory(File currentDir) {
344 File[] files = currentDir.listFiles();
345 for (int i = 0; i < files.length; ++i) {
346 if (files[i].isDirectory()) {
347 deleteDirectory(files[i]);
348 }
349 files[i].delete();
350 }
351 currentDir.delete();
352 }
Andrei Popescu93bea962010-03-23 15:04:36 +0000353 }.start();
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100354 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800355 }
356
357 @Override
358 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700359 final Context context = getContext();
Michael Kolbfe251992010-07-08 15:41:55 -0700360 boolean xlargeScreenSize = (context.getResources().getConfiguration().screenLayout
361 & Configuration.SCREENLAYOUT_SIZE_MASK)
362 == Configuration.SCREENLAYOUT_SIZE_XLARGE;
363 boolean isPortrait = (context.getResources().getConfiguration().orientation
364 == Configuration.ORIENTATION_PORTRAIT);
365
366
367 if (xlargeScreenSize && isPortrait) {
368 mMaxSuggestionLongSize = MAX_SUGGEST_LONG_LARGE;
369 mMaxSuggestionShortSize = MAX_SUGGEST_SHORT_LARGE;
370 } else {
371 mMaxSuggestionLongSize = MAX_SUGGEST_LONG_SMALL;
372 mMaxSuggestionShortSize = MAX_SUGGEST_SHORT_SMALL;
373 }
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700374 mOpenHelper = new DatabaseHelper(context);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700375 mBackupManager = new BackupManager(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100376 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700377 // To avoid erasing the bookmark table, we added it explicitly for
378 // version 18 and 19 as in the other cases, we will erase the table.
379 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
380 SharedPreferences p = PreferenceManager
381 .getDefaultSharedPreferences(context);
382 boolean fix = p.getBoolean("fix_picasa", true);
383 if (fix) {
384 fixPicasaBookmark();
385 Editor ed = p.edit();
386 ed.putBoolean("fix_picasa", false);
387 ed.commit();
388 }
389 }
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100390 mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400391 mShowWebSuggestionsSettingChangeObserver
392 = new ShowWebSuggestionsSettingChangeObserver();
393 context.getContentResolver().registerContentObserver(
394 Settings.System.getUriFor(
395 Settings.System.SHOW_WEB_SUGGESTIONS),
396 true, mShowWebSuggestionsSettingChangeObserver);
397 updateShowWebSuggestions();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800398 return true;
399 }
400
Leon Scroggins62b71f72009-06-12 17:51:22 -0400401 /**
402 * This Observer will ensure that if the user changes the system
403 * setting of whether to display web suggestions, we will
404 * change accordingly.
405 */
406 /* package */ class ShowWebSuggestionsSettingChangeObserver
407 extends ContentObserver {
408 public ShowWebSuggestionsSettingChangeObserver() {
409 super(new Handler());
410 }
411
412 @Override
413 public void onChange(boolean selfChange) {
414 updateShowWebSuggestions();
415 }
416 }
417
418 private ShowWebSuggestionsSettingChangeObserver
419 mShowWebSuggestionsSettingChangeObserver;
420
421 // If non-null, then the system is set to show web suggestions,
422 // and this is the SearchableInfo to use to get them.
423 private SearchableInfo mSearchableInfo;
424
425 /**
426 * Check the system settings to see whether web suggestions are
427 * allowed. If so, store the SearchableInfo to grab suggestions
428 * while the user is typing.
429 */
430 private void updateShowWebSuggestions() {
431 mSearchableInfo = null;
432 Context context = getContext();
433 if (Settings.System.getInt(context.getContentResolver(),
434 Settings.System.SHOW_WEB_SUGGESTIONS,
435 1 /* default on */) == 1) {
Bjorn Bringert32747542010-02-18 21:59:21 +0000436 ComponentName webSearchComponent = mSearchManager.getWebSearchActivity();
437 if (webSearchComponent != null) {
438 mSearchableInfo = mSearchManager.getSearchableInfo(webSearchComponent);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400439 }
440 }
441 }
442
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700443 private void fixPicasaBookmark() {
444 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
445 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
446 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
447 try {
448 if (!cursor.moveToFirst()) {
449 // set "created" so that it will be on the top of the list
450 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
451 "date, created, bookmark)" + " VALUES('" +
452 getContext().getString(R.string.picasa) + "', '"
453 + PICASA_URL + "', 0, 0, " + new Date().getTime()
454 + ", 1);");
455 }
456 } finally {
457 if (cursor != null) {
458 cursor.close();
459 }
460 }
461 }
462
The Android Open Source Project0c908882009-03-03 19:32:16 -0800463 /*
464 * Subclass AbstractCursor so we can combine multiple Cursors and add
Grace Kloba391df7c2010-03-01 19:51:49 -0800465 * "Search the web".
The Android Open Source Project0c908882009-03-03 19:32:16 -0800466 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100467 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
Grace Kloba391df7c2010-03-01 19:51:49 -0800468 * "Search the web";
469 * 2. If bookmark/history entries has a match, "Search the web" shows up at
470 * the second place. Otherwise, "Search the web" shows up at the first
471 * place.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472 */
473 private class MySuggestionCursor extends AbstractCursor {
474 private Cursor mHistoryCursor;
475 private Cursor mSuggestCursor;
476 private int mHistoryCount;
477 private int mSuggestionCount;
Grace Klobad3992d42010-01-28 11:44:38 -0800478 private boolean mIncludeWebSearch;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800479 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100480 private int mSuggestText1Id;
481 private int mSuggestText2Id;
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000482 private int mSuggestText2UrlId;
Satish Sampath565505b2009-05-29 15:37:27 +0100483 private int mSuggestQueryId;
Bjorn Bringert04851702009-09-22 10:36:01 +0100484 private int mSuggestIntentExtraDataId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800485
486 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
487 mHistoryCursor = hc;
488 mSuggestCursor = sc;
Grace Klobaf9b04272010-06-15 13:36:22 -0700489 mHistoryCount = hc != null ? hc.getCount() : 0;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800490 mSuggestionCount = sc != null ? sc.getCount() : 0;
Michael Kolbfe251992010-07-08 15:41:55 -0700491 if (mSuggestionCount > (mMaxSuggestionLongSize - mHistoryCount)) {
492 mSuggestionCount = mMaxSuggestionLongSize - mHistoryCount;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800493 }
494 mString = string;
Grace Klobad3992d42010-01-28 11:44:38 -0800495 mIncludeWebSearch = string.length() > 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100496
497 // Some web suggest providers only give suggestions and have no description string for
498 // items. The order of the result columns may be different as well. So retrieve the
499 // column indices for the fields we need now and check before using below.
500 if (mSuggestCursor == null) {
501 mSuggestText1Id = -1;
502 mSuggestText2Id = -1;
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000503 mSuggestText2UrlId = -1;
Satish Sampath565505b2009-05-29 15:37:27 +0100504 mSuggestQueryId = -1;
Bjorn Bringert04851702009-09-22 10:36:01 +0100505 mSuggestIntentExtraDataId = -1;
Satish Sampath565505b2009-05-29 15:37:27 +0100506 } else {
507 mSuggestText1Id = mSuggestCursor.getColumnIndex(
508 SearchManager.SUGGEST_COLUMN_TEXT_1);
509 mSuggestText2Id = mSuggestCursor.getColumnIndex(
510 SearchManager.SUGGEST_COLUMN_TEXT_2);
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000511 mSuggestText2UrlId = mSuggestCursor.getColumnIndex(
512 SearchManager.SUGGEST_COLUMN_TEXT_2_URL);
Satish Sampath565505b2009-05-29 15:37:27 +0100513 mSuggestQueryId = mSuggestCursor.getColumnIndex(
514 SearchManager.SUGGEST_COLUMN_QUERY);
Bjorn Bringert04851702009-09-22 10:36:01 +0100515 mSuggestIntentExtraDataId = mSuggestCursor.getColumnIndex(
516 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA);
Satish Sampath565505b2009-05-29 15:37:27 +0100517 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800518 }
519
520 @Override
521 public boolean onMove(int oldPosition, int newPosition) {
522 if (mHistoryCursor == null) {
523 return false;
524 }
Grace Klobad3992d42010-01-28 11:44:38 -0800525 if (mIncludeWebSearch) {
Grace Kloba391df7c2010-03-01 19:51:49 -0800526 if (mHistoryCount == 0 && newPosition == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800527 return true;
Grace Kloba391df7c2010-03-01 19:51:49 -0800528 } else if (mHistoryCount > 0) {
529 if (newPosition == 0) {
530 mHistoryCursor.moveToPosition(0);
531 return true;
532 } else if (newPosition == 1) {
533 return true;
534 }
Grace Klobad3992d42010-01-28 11:44:38 -0800535 }
Grace Kloba391df7c2010-03-01 19:51:49 -0800536 newPosition--;
Grace Klobad3992d42010-01-28 11:44:38 -0800537 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800538 if (mHistoryCount > newPosition) {
539 mHistoryCursor.moveToPosition(newPosition);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800540 } else {
Grace Klobad3992d42010-01-28 11:44:38 -0800541 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800542 }
543 return true;
544 }
545
546 @Override
547 public int getCount() {
Grace Klobad3992d42010-01-28 11:44:38 -0800548 if (mIncludeWebSearch) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800549 return mHistoryCount + mSuggestionCount + 1;
550 } else {
551 return mHistoryCount + mSuggestionCount;
552 }
553 }
554
555 @Override
556 public String[] getColumnNames() {
557 return COLUMNS;
558 }
Satish Sampath565505b2009-05-29 15:37:27 +0100559
The Android Open Source Project0c908882009-03-03 19:32:16 -0800560 @Override
561 public String getString(int columnIndex) {
562 if ((mPos != -1 && mHistoryCursor != null)) {
Grace Kloba391df7c2010-03-01 19:51:49 -0800563 int type = -1; // 0: web search; 1: history; 2: suggestion
564 if (mIncludeWebSearch) {
565 if (mHistoryCount == 0 && mPos == 0) {
566 type = 0;
567 } else if (mHistoryCount > 0) {
568 if (mPos == 0) {
569 type = 1;
570 } else if (mPos == 1) {
571 type = 0;
572 }
573 }
574 if (type == -1) type = (mPos - 1) < mHistoryCount ? 1 : 2;
575 } else {
576 type = mPos < mHistoryCount ? 1 : 2;
577 }
578
The Android Open Source Project0c908882009-03-03 19:32:16 -0800579 switch(columnIndex) {
580 case SUGGEST_COLUMN_INTENT_ACTION_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800581 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800582 return Intent.ACTION_VIEW;
583 } else {
584 return Intent.ACTION_SEARCH;
585 }
586
587 case SUGGEST_COLUMN_INTENT_DATA_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800588 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800589 return mHistoryCursor.getString(1);
590 } else {
591 return null;
592 }
593
594 case SUGGEST_COLUMN_TEXT_1_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800595 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800596 return mString;
Grace Kloba391df7c2010-03-01 19:51:49 -0800597 } else if (type == 1) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700598 return getHistoryTitle();
Grace Klobad3992d42010-01-28 11:44:38 -0800599 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100600 if (mSuggestText1Id == -1) return null;
601 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800602 }
603
604 case SUGGEST_COLUMN_TEXT_2_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800605 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800606 return getContext().getString(R.string.search_the_web);
Grace Kloba391df7c2010-03-01 19:51:49 -0800607 } else if (type == 1) {
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000608 return null; // Use TEXT_2_URL instead
Grace Klobad3992d42010-01-28 11:44:38 -0800609 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100610 if (mSuggestText2Id == -1) return null;
611 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800612 }
613
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000614 case SUGGEST_COLUMN_TEXT_2_URL_ID:
615 if (type == 0) {
616 return null;
617 } else if (type == 1) {
618 return getHistoryUrl();
619 } else {
620 if (mSuggestText2UrlId == -1) return null;
621 return mSuggestCursor.getString(mSuggestText2UrlId);
622 }
623
The Android Open Source Project0c908882009-03-03 19:32:16 -0800624 case SUGGEST_COLUMN_ICON_1_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800625 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800626 if (mHistoryCursor.getInt(3) == 1) {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400627 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800628 R.drawable.ic_search_category_bookmark)
629 .toString();
630 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400631 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800632 R.drawable.ic_search_category_history)
633 .toString();
634 }
635 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400636 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800637 R.drawable.ic_search_category_suggest)
638 .toString();
639 }
640
641 case SUGGEST_COLUMN_ICON_2_ID:
Leon Scroggins31887fd2009-05-18 16:58:08 -0400642 return "0";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800643
644 case SUGGEST_COLUMN_QUERY_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800645 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800646 return mString;
Grace Kloba391df7c2010-03-01 19:51:49 -0800647 } else if (type == 1) {
Mike LeBeau2af73052009-06-23 17:36:59 -0700648 // Return the url in the intent query column. This is ignored
649 // within the browser because our searchable is set to
650 // android:searchMode="queryRewriteFromData", but it is used by
651 // global search for query rewriting.
652 return mHistoryCursor.getString(1);
Grace Klobad3992d42010-01-28 11:44:38 -0800653 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100654 if (mSuggestQueryId == -1) return null;
655 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800656 }
Satish Sampath565505b2009-05-29 15:37:27 +0100657
Bjorn Bringert04851702009-09-22 10:36:01 +0100658 case SUGGEST_COLUMN_INTENT_EXTRA_DATA:
Grace Kloba391df7c2010-03-01 19:51:49 -0800659 if (type == 0) {
Bjorn Bringert04851702009-09-22 10:36:01 +0100660 return null;
Grace Kloba391df7c2010-03-01 19:51:49 -0800661 } else if (type == 1) {
Grace Klobad3992d42010-01-28 11:44:38 -0800662 return null;
663 } else {
Bjorn Bringert04851702009-09-22 10:36:01 +0100664 if (mSuggestIntentExtraDataId == -1) return null;
665 return mSuggestCursor.getString(mSuggestIntentExtraDataId);
Bjorn Bringert04851702009-09-22 10:36:01 +0100666 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800667 }
668 }
669 return null;
670 }
671
672 @Override
673 public double getDouble(int column) {
674 throw new UnsupportedOperationException();
675 }
676
677 @Override
678 public float getFloat(int column) {
679 throw new UnsupportedOperationException();
680 }
681
682 @Override
683 public int getInt(int column) {
684 throw new UnsupportedOperationException();
685 }
686
687 @Override
688 public long getLong(int column) {
689 if ((mPos != -1) && column == 0) {
690 return mPos; // use row# as the _Id
691 }
692 throw new UnsupportedOperationException();
693 }
694
695 @Override
696 public short getShort(int column) {
697 throw new UnsupportedOperationException();
698 }
699
700 @Override
701 public boolean isNull(int column) {
702 throw new UnsupportedOperationException();
703 }
704
705 // TODO Temporary change, finalize after jq's changes go in
Michael Kolbfe251992010-07-08 15:41:55 -0700706 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800707 public void deactivate() {
708 if (mHistoryCursor != null) {
709 mHistoryCursor.deactivate();
710 }
711 if (mSuggestCursor != null) {
712 mSuggestCursor.deactivate();
713 }
714 super.deactivate();
715 }
716
Michael Kolbfe251992010-07-08 15:41:55 -0700717 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800718 public boolean requery() {
719 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
720 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
721 }
722
723 // TODO Temporary change, finalize after jq's changes go in
Michael Kolbfe251992010-07-08 15:41:55 -0700724 @Override
The Android Open Source Project0c908882009-03-03 19:32:16 -0800725 public void close() {
726 super.close();
727 if (mHistoryCursor != null) {
728 mHistoryCursor.close();
729 mHistoryCursor = null;
730 }
731 if (mSuggestCursor != null) {
732 mSuggestCursor.close();
733 mSuggestCursor = null;
734 }
735 }
Satish Sampath565505b2009-05-29 15:37:27 +0100736
Mike LeBeau21beb132009-05-13 14:57:50 -0700737 /**
738 * Provides the title (text line 1) for a browser suggestion, which should be the
739 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100740 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700741 * @return the title string to use
742 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700743 private String getHistoryTitle() {
744 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700745 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000746 title = stripUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700747 }
748 return title;
749 }
Satish Sampath565505b2009-05-29 15:37:27 +0100750
Mike LeBeau21beb132009-05-13 14:57:50 -0700751 /**
752 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
753 * webpage url. If the webpage title is empty, then the url should go in the title
754 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100755 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700756 * @return the subtitle string to use, or null if none
757 */
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000758 private String getHistoryUrl() {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700759 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700760 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
761 return null;
762 } else {
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000763 return stripUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700764 }
765 }
Satish Sampath565505b2009-05-29 15:37:27 +0100766
The Android Open Source Project0c908882009-03-03 19:32:16 -0800767 }
768
Leon Scroggins58d56c62010-01-28 15:12:40 -0500769 private static class ResultsCursor extends AbstractCursor {
770 // Array indices for RESULTS_COLUMNS
771 private static final int RESULT_ACTION_ID = 1;
772 private static final int RESULT_DATA_ID = 2;
773 private static final int RESULT_TEXT_ID = 3;
774 private static final int RESULT_ICON_ID = 4;
775 private static final int RESULT_EXTRA_ID = 5;
776
777 private static final String[] RESULTS_COLUMNS = new String[] {
778 "_id",
779 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
780 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
781 SearchManager.SUGGEST_COLUMN_TEXT_1,
782 SearchManager.SUGGEST_COLUMN_ICON_1,
783 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA
784 };
785 private final ArrayList<String> mResults;
786 public ResultsCursor(ArrayList<String> results) {
787 mResults = results;
788 }
Michael Kolbfe251992010-07-08 15:41:55 -0700789 @Override
Leon Scroggins58d56c62010-01-28 15:12:40 -0500790 public int getCount() { return mResults.size(); }
791
Michael Kolbfe251992010-07-08 15:41:55 -0700792 @Override
Leon Scroggins58d56c62010-01-28 15:12:40 -0500793 public String[] getColumnNames() {
794 return RESULTS_COLUMNS;
795 }
796
Michael Kolbfe251992010-07-08 15:41:55 -0700797 @Override
Leon Scroggins58d56c62010-01-28 15:12:40 -0500798 public String getString(int column) {
799 switch (column) {
800 case RESULT_ACTION_ID:
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500801 return RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500802 case RESULT_TEXT_ID:
803 // The data is used when the phone is in landscape mode. We
804 // still want to show the result string.
805 case RESULT_DATA_ID:
806 return mResults.get(mPos);
807 case RESULT_EXTRA_ID:
808 // The Intent's extra data will store the index into
809 // mResults so the BrowserActivity will know which result to
810 // use.
811 return Integer.toString(mPos);
812 case RESULT_ICON_ID:
813 return Integer.valueOf(R.drawable.magnifying_glass)
814 .toString();
815 default:
816 return null;
817 }
818 }
Michael Kolbfe251992010-07-08 15:41:55 -0700819 @Override
Leon Scroggins58d56c62010-01-28 15:12:40 -0500820 public short getShort(int column) {
821 throw new UnsupportedOperationException();
822 }
Michael Kolbfe251992010-07-08 15:41:55 -0700823 @Override
Leon Scroggins58d56c62010-01-28 15:12:40 -0500824 public int getInt(int column) {
825 throw new UnsupportedOperationException();
826 }
Michael Kolbfe251992010-07-08 15:41:55 -0700827 @Override
Leon Scroggins58d56c62010-01-28 15:12:40 -0500828 public long getLong(int column) {
829 if ((mPos != -1) && column == 0) {
830 return mPos; // use row# as the _id
831 }
832 throw new UnsupportedOperationException();
833 }
Michael Kolbfe251992010-07-08 15:41:55 -0700834 @Override
Leon Scroggins58d56c62010-01-28 15:12:40 -0500835 public float getFloat(int column) {
836 throw new UnsupportedOperationException();
837 }
Michael Kolbfe251992010-07-08 15:41:55 -0700838 @Override
Leon Scroggins58d56c62010-01-28 15:12:40 -0500839 public double getDouble(int column) {
840 throw new UnsupportedOperationException();
841 }
Michael Kolbfe251992010-07-08 15:41:55 -0700842 @Override
Leon Scroggins58d56c62010-01-28 15:12:40 -0500843 public boolean isNull(int column) {
844 throw new UnsupportedOperationException();
845 }
846 }
847
848 private ResultsCursor mResultsCursor;
849
850 /**
851 * Provide a set of results to be returned to query, intended to be used
852 * by the SearchDialog when the BrowserActivity is in voice search mode.
853 * @param results Strings to display in the dropdown from the SearchDialog
854 */
855 /* package */ void setQueryResults(ArrayList<String> results) {
856 if (results == null) {
857 mResultsCursor = null;
858 } else {
859 mResultsCursor = new ResultsCursor(results);
860 }
861 }
862
The Android Open Source Project0c908882009-03-03 19:32:16 -0800863 @Override
864 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100865 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800866 throws IllegalStateException {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800867 int match = URI_MATCHER.match(url);
868 if (match == -1) {
869 throw new IllegalArgumentException("Unknown URL");
870 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500871 if (match == URI_MATCH_SUGGEST && mResultsCursor != null) {
872 Cursor results = mResultsCursor;
873 mResultsCursor = null;
874 return results;
875 }
876 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800877
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100878 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800879 String suggestSelection;
880 String [] myArgs;
881 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
Grace Klobaf9b04272010-06-15 13:36:22 -0700882 return new MySuggestionCursor(null, null, "");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800883 } else {
884 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700885 if (selectionArgs[0].startsWith("http")
886 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800887 myArgs = new String[1];
888 myArgs[0] = like;
889 suggestSelection = selection;
890 } else {
891 SUGGEST_ARGS[0] = "http://" + like;
892 SUGGEST_ARGS[1] = "http://www." + like;
893 SUGGEST_ARGS[2] = "https://" + like;
894 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400895 // To match against titles.
896 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800897 myArgs = SUGGEST_ARGS;
898 suggestSelection = SUGGEST_SELECTION;
899 }
900 }
901
902 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
903 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Michael Kolbfe251992010-07-08 15:41:55 -0700904 ORDER_BY, Integer.toString(mMaxSuggestionLongSize));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800905
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100906 if (match == URI_MATCH_BOOKMARKS_SUGGEST
Dan Egnor5ee906c2009-11-18 12:11:49 -0800907 || Patterns.WEB_URL.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800908 return new MySuggestionCursor(c, null, "");
909 } else {
910 // get Google suggest if there is still space in the list
911 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400912 && mSearchableInfo != null
Michael Kolbfe251992010-07-08 15:41:55 -0700913 && c.getCount() < (mMaxSuggestionShortSize - 1)) {
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100914 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400915 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800916 }
917 return new MySuggestionCursor(c, null, selectionArgs[0]);
918 }
919 }
920
921 String[] projection = null;
922 if (projectionIn != null && projectionIn.length > 0) {
923 projection = new String[projectionIn.length + 1];
924 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
925 projection[projectionIn.length] = "_id AS _id";
926 }
927
928 StringBuilder whereClause = new StringBuilder(256);
929 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
930 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
931 .append(")");
932 }
933
934 // Tack on the user's selection, if present
935 if (selection != null && selection.length() > 0) {
936 if (whereClause.length() > 0) {
937 whereClause.append(" AND ");
938 }
939
940 whereClause.append('(');
941 whereClause.append(selection);
942 whereClause.append(')');
943 }
944 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
945 whereClause.toString(), selectionArgs, null, null, sortOrder,
946 null);
947 c.setNotificationUri(getContext().getContentResolver(), url);
948 return c;
949 }
950
951 @Override
952 public String getType(Uri url) {
953 int match = URI_MATCHER.match(url);
954 switch (match) {
955 case URI_MATCH_BOOKMARKS:
956 return "vnd.android.cursor.dir/bookmark";
957
958 case URI_MATCH_BOOKMARKS_ID:
959 return "vnd.android.cursor.item/bookmark";
960
961 case URI_MATCH_SEARCHES:
962 return "vnd.android.cursor.dir/searches";
963
964 case URI_MATCH_SEARCHES_ID:
965 return "vnd.android.cursor.item/searches";
966
967 case URI_MATCH_SUGGEST:
968 return SearchManager.SUGGEST_MIME_TYPE;
969
970 default:
971 throw new IllegalArgumentException("Unknown URL");
972 }
973 }
974
975 @Override
976 public Uri insert(Uri url, ContentValues initialValues) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700977 boolean isBookmarkTable = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800978 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
979
980 int match = URI_MATCHER.match(url);
981 Uri uri = null;
982 switch (match) {
983 case URI_MATCH_BOOKMARKS: {
984 // Insert into the bookmarks table
985 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
986 initialValues);
987 if (rowID > 0) {
988 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
989 rowID);
990 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700991 isBookmarkTable = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800992 break;
993 }
994
995 case URI_MATCH_SEARCHES: {
996 // Insert into the searches table
997 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
998 initialValues);
999 if (rowID > 0) {
1000 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
1001 rowID);
1002 }
1003 break;
1004 }
1005
1006 default:
1007 throw new IllegalArgumentException("Unknown URL");
1008 }
1009
1010 if (uri == null) {
1011 throw new IllegalArgumentException("Unknown URL");
1012 }
1013 getContext().getContentResolver().notifyChange(uri, null);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001014
Christopher Tatef0c36f72009-07-28 15:24:05 -07001015 // Back up the new bookmark set if we just inserted one.
1016 // A row created when bookmarks are added from scratch will have
1017 // bookmark=1 in the initial value set.
1018 if (isBookmarkTable
1019 && initialValues.containsKey(BookmarkColumns.BOOKMARK)
1020 && initialValues.getAsInteger(BookmarkColumns.BOOKMARK) != 0) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001021 mBackupManager.dataChanged();
1022 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001023 return uri;
1024 }
1025
1026 @Override
1027 public int delete(Uri url, String where, String[] whereArgs) {
1028 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
1029
1030 int match = URI_MATCHER.match(url);
1031 if (match == -1 || match == URI_MATCH_SUGGEST) {
1032 throw new IllegalArgumentException("Unknown URL");
1033 }
1034
Christopher Tatef0c36f72009-07-28 15:24:05 -07001035 // need to know whether it's the bookmarks table for a couple of reasons
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001036 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
Christopher Tatef0c36f72009-07-28 15:24:05 -07001037 String id = null;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001038
1039 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001040 StringBuilder sb = new StringBuilder();
1041 if (where != null && where.length() > 0) {
1042 sb.append("( ");
1043 sb.append(where);
1044 sb.append(" ) AND ");
1045 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001046 id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001047 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -07001048 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001049 where = sb.toString();
1050 }
1051
Christopher Tatef0c36f72009-07-28 15:24:05 -07001052 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001053
Christopher Tatef0c36f72009-07-28 15:24:05 -07001054 // we'lll need to back up the bookmark set if we are about to delete one
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001055 if (isBookmarkTable) {
Christopher Tatef0c36f72009-07-28 15:24:05 -07001056 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
1057 new String[] { BookmarkColumns.BOOKMARK },
1058 "_id = " + id, null, null);
1059 if (cursor.moveToNext()) {
1060 if (cursor.getInt(0) != 0) {
1061 // yep, this record is a bookmark
1062 mBackupManager.dataChanged();
1063 }
1064 }
1065 cursor.close();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001066 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001067
1068 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
1069 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001070 return count;
1071 }
1072
1073 @Override
1074 public int update(Uri url, ContentValues values, String where,
1075 String[] whereArgs) {
1076 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
1077
1078 int match = URI_MATCHER.match(url);
1079 if (match == -1 || match == URI_MATCH_SUGGEST) {
1080 throw new IllegalArgumentException("Unknown URL");
1081 }
1082
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001083 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001084 StringBuilder sb = new StringBuilder();
1085 if (where != null && where.length() > 0) {
1086 sb.append("( ");
1087 sb.append(where);
1088 sb.append(" ) AND ");
1089 }
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001090 String id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001091 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -07001092 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001093 where = sb.toString();
1094 }
1095
Christopher Tatef0c36f72009-07-28 15:24:05 -07001096 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001097
Christopher Tatef0c36f72009-07-28 15:24:05 -07001098 // Not all bookmark-table updates should be backed up. Look to see
1099 // whether we changed the title, url, or "is a bookmark" state, and
1100 // request a backup if so.
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001101 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_BOOKMARKS) {
1102 boolean changingBookmarks = false;
Christopher Tatef0c36f72009-07-28 15:24:05 -07001103 // Alterations to the bookmark field inherently change the bookmark
1104 // set, so we don't need to query the record; we know a priori that
1105 // we will need to back up this change.
1106 if (values.containsKey(BookmarkColumns.BOOKMARK)) {
1107 changingBookmarks = true;
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001108 } else if ((values.containsKey(BookmarkColumns.TITLE)
1109 || values.containsKey(BookmarkColumns.URL))
1110 && values.containsKey(BookmarkColumns._ID)) {
1111 // If a title or URL has been changed, check to see if it is to
1112 // a bookmark. The ID should have been included in the update,
1113 // so use it.
Christopher Tatef0c36f72009-07-28 15:24:05 -07001114 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
1115 new String[] { BookmarkColumns.BOOKMARK },
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001116 BookmarkColumns._ID + " = "
1117 + values.getAsString(BookmarkColumns._ID), null, null);
Christopher Tatef0c36f72009-07-28 15:24:05 -07001118 if (cursor.moveToNext()) {
1119 changingBookmarks = (cursor.getInt(0) != 0);
1120 }
1121 cursor.close();
1122 }
1123
1124 // if this *is* a bookmark row we're altering, we need to back it up.
1125 if (changingBookmarks) {
1126 mBackupManager.dataChanged();
1127 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001128 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001129
1130 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
1131 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001132 return ret;
1133 }
Satish Sampath565505b2009-05-29 15:37:27 +01001134
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001135 /**
1136 * Strips the provided url of preceding "http://" and any trailing "/". Does not
1137 * strip "https://". If the provided string cannot be stripped, the original string
1138 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +01001139 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001140 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +01001141 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001142 * @param url a url to strip, like "http://www.google.com/"
1143 * @return a stripped url like "www.google.com", or the original string if it could
1144 * not be stripped
1145 */
1146 private static String stripUrl(String url) {
1147 if (url == null) return null;
1148 Matcher m = STRIP_URL_PATTERN.matcher(url);
1149 if (m.matches() && m.groupCount() == 3) {
1150 return m.group(2);
1151 } else {
1152 return url;
1153 }
1154 }
1155
Michael Kolbfe251992010-07-08 15:41:55 -07001156 public static Cursor getBookmarksSuggestions(ContentResolver cr, String constraint) {
1157 Uri uri = Uri.parse("content://browser/" + SearchManager.SUGGEST_URI_PATH_QUERY);
1158 return cr.query(uri, SUGGEST_PROJECTION, SUGGEST_SELECTION,
1159 new String[] { constraint }, ORDER_BY);
1160 }
1161
The Android Open Source Project0c908882009-03-03 19:32:16 -08001162}