blob: 4bf7d3f2f691b67300d25c2775edb35db32cc405 [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;
Andrei Popescu1b20b9d2009-09-21 18:49:42 +010040import android.os.AsyncTask;
Leon Scroggins62b71f72009-06-12 17:51:22 -040041import android.os.Handler;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070042import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.provider.Browser;
Leon Scroggins62b71f72009-06-12 17:51:22 -040044import android.provider.Settings;
Christopher Tatef0c36f72009-07-28 15:24:05 -070045import android.provider.Browser.BookmarkColumns;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050046import android.speech.RecognizerResultsIntent;
Mike LeBeau21beb132009-05-13 14:57:50 -070047import android.text.TextUtils;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010048import android.util.Log;
Dianne Hackborn385effd2010-02-24 20:03:04 -080049import android.util.Patterns;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070050import android.util.TypedValue;
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +000051import android.webkit.GeolocationPermissions;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010052
Dan Egnor5ee906c2009-11-18 12:11:49 -080053
Andrei Popescu1b20b9d2009-09-21 18:49:42 +010054import java.io.File;
55import java.io.FilenameFilter;
Leon Scroggins58d56c62010-01-28 15:12:40 -050056import java.util.ArrayList;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010057import java.util.Date;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070058import java.util.regex.Matcher;
59import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -080060
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070061
The Android Open Source Project0c908882009-03-03 19:32:16 -080062public class BrowserProvider extends ContentProvider {
63
64 private SQLiteOpenHelper mOpenHelper;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070065 private BackupManager mBackupManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080066 private static final String sDatabaseName = "browser.db";
67 private static final String TAG = "BrowserProvider";
68 private static final String ORDER_BY = "visits DESC, date DESC";
69
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070070 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
71 "viewer?source=androidclient";
72
The Android Open Source Project0c908882009-03-03 19:32:16 -080073 private static final String[] TABLE_NAMES = new String[] {
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +000074 "bookmarks", "searches", "geolocation"
The Android Open Source Project0c908882009-03-03 19:32:16 -080075 };
76 private static final String[] SUGGEST_PROJECTION = new String[] {
Leon Scrogginsb4464432009-11-25 12:37:50 -050077 "_id", "url", "title", "bookmark", "user_entered"
The Android Open Source Project0c908882009-03-03 19:32:16 -080078 };
Satish Sampath565505b2009-05-29 15:37:27 +010079 private static final String SUGGEST_SELECTION =
Leon Scrogginsb4464432009-11-25 12:37:50 -050080 "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?"
81 + " OR title LIKE ?) AND (bookmark = 1 OR user_entered = 1)";
Leon Scrogginsbd359cc2009-05-26 15:57:35 -040082 private String[] SUGGEST_ARGS = new String[5];
The Android Open Source Project0c908882009-03-03 19:32:16 -080083
84 // shared suggestion array index, make sure to match COLUMNS
85 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
86 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
87 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
88 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
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() {
306 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
307 public Void doInBackground(Void... unused) {
308 String browserDataDirString = mContext.getApplicationInfo().dataDir;
309 final String appPluginsDirString = "app_plugins";
310 final String gearsPrefix = "gears";
311 File appPluginsDir = new File(browserDataDirString + File.separator
312 + appPluginsDirString);
313 if (!appPluginsDir.exists()) {
314 return null;
315 }
316 // Delete the Gears plugin files
317 File[] gearsFiles = appPluginsDir.listFiles(new FilenameFilter() {
318 public boolean accept(File dir, String filename) {
319 return filename.startsWith(gearsPrefix);
320 }
321 });
322 for (int i = 0; i < gearsFiles.length; ++i) {
323 if (gearsFiles[i].isDirectory()) {
324 deleteDirectory(gearsFiles[i]);
325 } else {
326 gearsFiles[i].delete();
327 }
328 }
329 // Delete the Gears data files
330 File gearsDataDir = new File(browserDataDirString + File.separator
331 + gearsPrefix);
332 if (!gearsDataDir.exists()) {
333 return null;
334 }
335 deleteDirectory(gearsDataDir);
336 return null;
337 }
338
339 private void deleteDirectory(File currentDir) {
340 File[] files = currentDir.listFiles();
341 for (int i = 0; i < files.length; ++i) {
342 if (files[i].isDirectory()) {
343 deleteDirectory(files[i]);
344 }
345 files[i].delete();
346 }
347 currentDir.delete();
348 }
349 };
350
351 task.execute();
352 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800353 }
354
355 @Override
356 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700357 final Context context = getContext();
358 mOpenHelper = new DatabaseHelper(context);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700359 mBackupManager = new BackupManager(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100360 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700361 // To avoid erasing the bookmark table, we added it explicitly for
362 // version 18 and 19 as in the other cases, we will erase the table.
363 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
364 SharedPreferences p = PreferenceManager
365 .getDefaultSharedPreferences(context);
366 boolean fix = p.getBoolean("fix_picasa", true);
367 if (fix) {
368 fixPicasaBookmark();
369 Editor ed = p.edit();
370 ed.putBoolean("fix_picasa", false);
371 ed.commit();
372 }
373 }
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100374 mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400375 mShowWebSuggestionsSettingChangeObserver
376 = new ShowWebSuggestionsSettingChangeObserver();
377 context.getContentResolver().registerContentObserver(
378 Settings.System.getUriFor(
379 Settings.System.SHOW_WEB_SUGGESTIONS),
380 true, mShowWebSuggestionsSettingChangeObserver);
381 updateShowWebSuggestions();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800382 return true;
383 }
384
Leon Scroggins62b71f72009-06-12 17:51:22 -0400385 /**
386 * This Observer will ensure that if the user changes the system
387 * setting of whether to display web suggestions, we will
388 * change accordingly.
389 */
390 /* package */ class ShowWebSuggestionsSettingChangeObserver
391 extends ContentObserver {
392 public ShowWebSuggestionsSettingChangeObserver() {
393 super(new Handler());
394 }
395
396 @Override
397 public void onChange(boolean selfChange) {
398 updateShowWebSuggestions();
399 }
400 }
401
402 private ShowWebSuggestionsSettingChangeObserver
403 mShowWebSuggestionsSettingChangeObserver;
404
405 // If non-null, then the system is set to show web suggestions,
406 // and this is the SearchableInfo to use to get them.
407 private SearchableInfo mSearchableInfo;
408
409 /**
410 * Check the system settings to see whether web suggestions are
411 * allowed. If so, store the SearchableInfo to grab suggestions
412 * while the user is typing.
413 */
414 private void updateShowWebSuggestions() {
415 mSearchableInfo = null;
416 Context context = getContext();
417 if (Settings.System.getInt(context.getContentResolver(),
418 Settings.System.SHOW_WEB_SUGGESTIONS,
419 1 /* default on */) == 1) {
Bjorn Bringert32747542010-02-18 21:59:21 +0000420 ComponentName webSearchComponent = mSearchManager.getWebSearchActivity();
421 if (webSearchComponent != null) {
422 mSearchableInfo = mSearchManager.getSearchableInfo(webSearchComponent);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400423 }
424 }
425 }
426
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700427 private void fixPicasaBookmark() {
428 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
429 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
430 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
431 try {
432 if (!cursor.moveToFirst()) {
433 // set "created" so that it will be on the top of the list
434 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
435 "date, created, bookmark)" + " VALUES('" +
436 getContext().getString(R.string.picasa) + "', '"
437 + PICASA_URL + "', 0, 0, " + new Date().getTime()
438 + ", 1);");
439 }
440 } finally {
441 if (cursor != null) {
442 cursor.close();
443 }
444 }
445 }
446
The Android Open Source Project0c908882009-03-03 19:32:16 -0800447 /*
448 * Subclass AbstractCursor so we can combine multiple Cursors and add
Grace Kloba391df7c2010-03-01 19:51:49 -0800449 * "Search the web".
The Android Open Source Project0c908882009-03-03 19:32:16 -0800450 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100451 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
Grace Kloba391df7c2010-03-01 19:51:49 -0800452 * "Search the web";
453 * 2. If bookmark/history entries has a match, "Search the web" shows up at
454 * the second place. Otherwise, "Search the web" shows up at the first
455 * place.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800456 */
457 private class MySuggestionCursor extends AbstractCursor {
458 private Cursor mHistoryCursor;
459 private Cursor mSuggestCursor;
460 private int mHistoryCount;
461 private int mSuggestionCount;
Grace Klobad3992d42010-01-28 11:44:38 -0800462 private boolean mIncludeWebSearch;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800463 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100464 private int mSuggestText1Id;
465 private int mSuggestText2Id;
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000466 private int mSuggestText2UrlId;
Satish Sampath565505b2009-05-29 15:37:27 +0100467 private int mSuggestQueryId;
Bjorn Bringert04851702009-09-22 10:36:01 +0100468 private int mSuggestIntentExtraDataId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800469
470 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
471 mHistoryCursor = hc;
472 mSuggestCursor = sc;
473 mHistoryCount = hc.getCount();
474 mSuggestionCount = sc != null ? sc.getCount() : 0;
475 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
476 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
477 }
478 mString = string;
Grace Klobad3992d42010-01-28 11:44:38 -0800479 mIncludeWebSearch = string.length() > 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100480
481 // Some web suggest providers only give suggestions and have no description string for
482 // items. The order of the result columns may be different as well. So retrieve the
483 // column indices for the fields we need now and check before using below.
484 if (mSuggestCursor == null) {
485 mSuggestText1Id = -1;
486 mSuggestText2Id = -1;
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000487 mSuggestText2UrlId = -1;
Satish Sampath565505b2009-05-29 15:37:27 +0100488 mSuggestQueryId = -1;
Bjorn Bringert04851702009-09-22 10:36:01 +0100489 mSuggestIntentExtraDataId = -1;
Satish Sampath565505b2009-05-29 15:37:27 +0100490 } else {
491 mSuggestText1Id = mSuggestCursor.getColumnIndex(
492 SearchManager.SUGGEST_COLUMN_TEXT_1);
493 mSuggestText2Id = mSuggestCursor.getColumnIndex(
494 SearchManager.SUGGEST_COLUMN_TEXT_2);
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000495 mSuggestText2UrlId = mSuggestCursor.getColumnIndex(
496 SearchManager.SUGGEST_COLUMN_TEXT_2_URL);
Satish Sampath565505b2009-05-29 15:37:27 +0100497 mSuggestQueryId = mSuggestCursor.getColumnIndex(
498 SearchManager.SUGGEST_COLUMN_QUERY);
Bjorn Bringert04851702009-09-22 10:36:01 +0100499 mSuggestIntentExtraDataId = mSuggestCursor.getColumnIndex(
500 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA);
Satish Sampath565505b2009-05-29 15:37:27 +0100501 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800502 }
503
504 @Override
505 public boolean onMove(int oldPosition, int newPosition) {
506 if (mHistoryCursor == null) {
507 return false;
508 }
Grace Klobad3992d42010-01-28 11:44:38 -0800509 if (mIncludeWebSearch) {
Grace Kloba391df7c2010-03-01 19:51:49 -0800510 if (mHistoryCount == 0 && newPosition == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800511 return true;
Grace Kloba391df7c2010-03-01 19:51:49 -0800512 } else if (mHistoryCount > 0) {
513 if (newPosition == 0) {
514 mHistoryCursor.moveToPosition(0);
515 return true;
516 } else if (newPosition == 1) {
517 return true;
518 }
Grace Klobad3992d42010-01-28 11:44:38 -0800519 }
Grace Kloba391df7c2010-03-01 19:51:49 -0800520 newPosition--;
Grace Klobad3992d42010-01-28 11:44:38 -0800521 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800522 if (mHistoryCount > newPosition) {
523 mHistoryCursor.moveToPosition(newPosition);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800524 } else {
Grace Klobad3992d42010-01-28 11:44:38 -0800525 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800526 }
527 return true;
528 }
529
530 @Override
531 public int getCount() {
Grace Klobad3992d42010-01-28 11:44:38 -0800532 if (mIncludeWebSearch) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800533 return mHistoryCount + mSuggestionCount + 1;
534 } else {
535 return mHistoryCount + mSuggestionCount;
536 }
537 }
538
539 @Override
540 public String[] getColumnNames() {
541 return COLUMNS;
542 }
Satish Sampath565505b2009-05-29 15:37:27 +0100543
The Android Open Source Project0c908882009-03-03 19:32:16 -0800544 @Override
545 public String getString(int columnIndex) {
546 if ((mPos != -1 && mHistoryCursor != null)) {
Grace Kloba391df7c2010-03-01 19:51:49 -0800547 int type = -1; // 0: web search; 1: history; 2: suggestion
548 if (mIncludeWebSearch) {
549 if (mHistoryCount == 0 && mPos == 0) {
550 type = 0;
551 } else if (mHistoryCount > 0) {
552 if (mPos == 0) {
553 type = 1;
554 } else if (mPos == 1) {
555 type = 0;
556 }
557 }
558 if (type == -1) type = (mPos - 1) < mHistoryCount ? 1 : 2;
559 } else {
560 type = mPos < mHistoryCount ? 1 : 2;
561 }
562
The Android Open Source Project0c908882009-03-03 19:32:16 -0800563 switch(columnIndex) {
564 case SUGGEST_COLUMN_INTENT_ACTION_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800565 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800566 return Intent.ACTION_VIEW;
567 } else {
568 return Intent.ACTION_SEARCH;
569 }
570
571 case SUGGEST_COLUMN_INTENT_DATA_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800572 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800573 return mHistoryCursor.getString(1);
574 } else {
575 return null;
576 }
577
578 case SUGGEST_COLUMN_TEXT_1_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800579 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800580 return mString;
Grace Kloba391df7c2010-03-01 19:51:49 -0800581 } else if (type == 1) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700582 return getHistoryTitle();
Grace Klobad3992d42010-01-28 11:44:38 -0800583 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100584 if (mSuggestText1Id == -1) return null;
585 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800586 }
587
588 case SUGGEST_COLUMN_TEXT_2_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800589 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800590 return getContext().getString(R.string.search_the_web);
Grace Kloba391df7c2010-03-01 19:51:49 -0800591 } else if (type == 1) {
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000592 return null; // Use TEXT_2_URL instead
Grace Klobad3992d42010-01-28 11:44:38 -0800593 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100594 if (mSuggestText2Id == -1) return null;
595 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800596 }
597
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000598 case SUGGEST_COLUMN_TEXT_2_URL_ID:
599 if (type == 0) {
600 return null;
601 } else if (type == 1) {
602 return getHistoryUrl();
603 } else {
604 if (mSuggestText2UrlId == -1) return null;
605 return mSuggestCursor.getString(mSuggestText2UrlId);
606 }
607
The Android Open Source Project0c908882009-03-03 19:32:16 -0800608 case SUGGEST_COLUMN_ICON_1_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800609 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800610 if (mHistoryCursor.getInt(3) == 1) {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400611 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800612 R.drawable.ic_search_category_bookmark)
613 .toString();
614 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400615 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800616 R.drawable.ic_search_category_history)
617 .toString();
618 }
619 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400620 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800621 R.drawable.ic_search_category_suggest)
622 .toString();
623 }
624
625 case SUGGEST_COLUMN_ICON_2_ID:
Leon Scroggins31887fd2009-05-18 16:58:08 -0400626 return "0";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800627
628 case SUGGEST_COLUMN_QUERY_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800629 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800630 return mString;
Grace Kloba391df7c2010-03-01 19:51:49 -0800631 } else if (type == 1) {
Mike LeBeau2af73052009-06-23 17:36:59 -0700632 // Return the url in the intent query column. This is ignored
633 // within the browser because our searchable is set to
634 // android:searchMode="queryRewriteFromData", but it is used by
635 // global search for query rewriting.
636 return mHistoryCursor.getString(1);
Grace Klobad3992d42010-01-28 11:44:38 -0800637 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100638 if (mSuggestQueryId == -1) return null;
639 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800640 }
Satish Sampath565505b2009-05-29 15:37:27 +0100641
Bjorn Bringert04851702009-09-22 10:36:01 +0100642 case SUGGEST_COLUMN_INTENT_EXTRA_DATA:
Grace Kloba391df7c2010-03-01 19:51:49 -0800643 if (type == 0) {
Bjorn Bringert04851702009-09-22 10:36:01 +0100644 return null;
Grace Kloba391df7c2010-03-01 19:51:49 -0800645 } else if (type == 1) {
Grace Klobad3992d42010-01-28 11:44:38 -0800646 return null;
647 } else {
Bjorn Bringert04851702009-09-22 10:36:01 +0100648 if (mSuggestIntentExtraDataId == -1) return null;
649 return mSuggestCursor.getString(mSuggestIntentExtraDataId);
Bjorn Bringert04851702009-09-22 10:36:01 +0100650 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800651 }
652 }
653 return null;
654 }
655
656 @Override
657 public double getDouble(int column) {
658 throw new UnsupportedOperationException();
659 }
660
661 @Override
662 public float getFloat(int column) {
663 throw new UnsupportedOperationException();
664 }
665
666 @Override
667 public int getInt(int column) {
668 throw new UnsupportedOperationException();
669 }
670
671 @Override
672 public long getLong(int column) {
673 if ((mPos != -1) && column == 0) {
674 return mPos; // use row# as the _Id
675 }
676 throw new UnsupportedOperationException();
677 }
678
679 @Override
680 public short getShort(int column) {
681 throw new UnsupportedOperationException();
682 }
683
684 @Override
685 public boolean isNull(int column) {
686 throw new UnsupportedOperationException();
687 }
688
689 // TODO Temporary change, finalize after jq's changes go in
690 public void deactivate() {
691 if (mHistoryCursor != null) {
692 mHistoryCursor.deactivate();
693 }
694 if (mSuggestCursor != null) {
695 mSuggestCursor.deactivate();
696 }
697 super.deactivate();
698 }
699
700 public boolean requery() {
701 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
702 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
703 }
704
705 // TODO Temporary change, finalize after jq's changes go in
706 public void close() {
707 super.close();
708 if (mHistoryCursor != null) {
709 mHistoryCursor.close();
710 mHistoryCursor = null;
711 }
712 if (mSuggestCursor != null) {
713 mSuggestCursor.close();
714 mSuggestCursor = null;
715 }
716 }
Satish Sampath565505b2009-05-29 15:37:27 +0100717
Mike LeBeau21beb132009-05-13 14:57:50 -0700718 /**
719 * Provides the title (text line 1) for a browser suggestion, which should be the
720 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100721 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700722 * @return the title string to use
723 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700724 private String getHistoryTitle() {
725 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700726 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000727 title = stripUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700728 }
729 return title;
730 }
Satish Sampath565505b2009-05-29 15:37:27 +0100731
Mike LeBeau21beb132009-05-13 14:57:50 -0700732 /**
733 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
734 * webpage url. If the webpage title is empty, then the url should go in the title
735 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100736 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700737 * @return the subtitle string to use, or null if none
738 */
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000739 private String getHistoryUrl() {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700740 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700741 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
742 return null;
743 } else {
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000744 return stripUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700745 }
746 }
Satish Sampath565505b2009-05-29 15:37:27 +0100747
The Android Open Source Project0c908882009-03-03 19:32:16 -0800748 }
749
Leon Scroggins58d56c62010-01-28 15:12:40 -0500750 private static class ResultsCursor extends AbstractCursor {
751 // Array indices for RESULTS_COLUMNS
752 private static final int RESULT_ACTION_ID = 1;
753 private static final int RESULT_DATA_ID = 2;
754 private static final int RESULT_TEXT_ID = 3;
755 private static final int RESULT_ICON_ID = 4;
756 private static final int RESULT_EXTRA_ID = 5;
757
758 private static final String[] RESULTS_COLUMNS = new String[] {
759 "_id",
760 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
761 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
762 SearchManager.SUGGEST_COLUMN_TEXT_1,
763 SearchManager.SUGGEST_COLUMN_ICON_1,
764 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA
765 };
766 private final ArrayList<String> mResults;
767 public ResultsCursor(ArrayList<String> results) {
768 mResults = results;
769 }
770 public int getCount() { return mResults.size(); }
771
772 public String[] getColumnNames() {
773 return RESULTS_COLUMNS;
774 }
775
776 public String getString(int column) {
777 switch (column) {
778 case RESULT_ACTION_ID:
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500779 return RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500780 case RESULT_TEXT_ID:
781 // The data is used when the phone is in landscape mode. We
782 // still want to show the result string.
783 case RESULT_DATA_ID:
784 return mResults.get(mPos);
785 case RESULT_EXTRA_ID:
786 // The Intent's extra data will store the index into
787 // mResults so the BrowserActivity will know which result to
788 // use.
789 return Integer.toString(mPos);
790 case RESULT_ICON_ID:
791 return Integer.valueOf(R.drawable.magnifying_glass)
792 .toString();
793 default:
794 return null;
795 }
796 }
797 public short getShort(int column) {
798 throw new UnsupportedOperationException();
799 }
800 public int getInt(int column) {
801 throw new UnsupportedOperationException();
802 }
803 public long getLong(int column) {
804 if ((mPos != -1) && column == 0) {
805 return mPos; // use row# as the _id
806 }
807 throw new UnsupportedOperationException();
808 }
809 public float getFloat(int column) {
810 throw new UnsupportedOperationException();
811 }
812 public double getDouble(int column) {
813 throw new UnsupportedOperationException();
814 }
815 public boolean isNull(int column) {
816 throw new UnsupportedOperationException();
817 }
818 }
819
820 private ResultsCursor mResultsCursor;
821
822 /**
823 * Provide a set of results to be returned to query, intended to be used
824 * by the SearchDialog when the BrowserActivity is in voice search mode.
825 * @param results Strings to display in the dropdown from the SearchDialog
826 */
827 /* package */ void setQueryResults(ArrayList<String> results) {
828 if (results == null) {
829 mResultsCursor = null;
830 } else {
831 mResultsCursor = new ResultsCursor(results);
832 }
833 }
834
The Android Open Source Project0c908882009-03-03 19:32:16 -0800835 @Override
836 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100837 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800838 throws IllegalStateException {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800839 int match = URI_MATCHER.match(url);
840 if (match == -1) {
841 throw new IllegalArgumentException("Unknown URL");
842 }
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000843 if (match == URI_MATCH_GEOLOCATION) {
844 throw new UnsupportedOperationException("query() not supported for geolocation");
845 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500846 if (match == URI_MATCH_SUGGEST && mResultsCursor != null) {
847 Cursor results = mResultsCursor;
848 mResultsCursor = null;
849 return results;
850 }
851 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800852
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100853 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800854 String suggestSelection;
855 String [] myArgs;
856 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
857 suggestSelection = null;
858 myArgs = null;
859 } else {
860 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700861 if (selectionArgs[0].startsWith("http")
862 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800863 myArgs = new String[1];
864 myArgs[0] = like;
865 suggestSelection = selection;
866 } else {
867 SUGGEST_ARGS[0] = "http://" + like;
868 SUGGEST_ARGS[1] = "http://www." + like;
869 SUGGEST_ARGS[2] = "https://" + like;
870 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400871 // To match against titles.
872 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800873 myArgs = SUGGEST_ARGS;
874 suggestSelection = SUGGEST_SELECTION;
875 }
876 }
877
878 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
879 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Leon Scroggins31887fd2009-05-18 16:58:08 -0400880 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800881
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100882 if (match == URI_MATCH_BOOKMARKS_SUGGEST
Dan Egnor5ee906c2009-11-18 12:11:49 -0800883 || Patterns.WEB_URL.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800884 return new MySuggestionCursor(c, null, "");
885 } else {
886 // get Google suggest if there is still space in the list
887 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400888 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800889 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100890 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400891 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800892 }
893 return new MySuggestionCursor(c, null, selectionArgs[0]);
894 }
895 }
896
897 String[] projection = null;
898 if (projectionIn != null && projectionIn.length > 0) {
899 projection = new String[projectionIn.length + 1];
900 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
901 projection[projectionIn.length] = "_id AS _id";
902 }
903
904 StringBuilder whereClause = new StringBuilder(256);
905 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
906 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
907 .append(")");
908 }
909
910 // Tack on the user's selection, if present
911 if (selection != null && selection.length() > 0) {
912 if (whereClause.length() > 0) {
913 whereClause.append(" AND ");
914 }
915
916 whereClause.append('(');
917 whereClause.append(selection);
918 whereClause.append(')');
919 }
920 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
921 whereClause.toString(), selectionArgs, null, null, sortOrder,
922 null);
923 c.setNotificationUri(getContext().getContentResolver(), url);
924 return c;
925 }
926
927 @Override
928 public String getType(Uri url) {
929 int match = URI_MATCHER.match(url);
930 switch (match) {
931 case URI_MATCH_BOOKMARKS:
932 return "vnd.android.cursor.dir/bookmark";
933
934 case URI_MATCH_BOOKMARKS_ID:
935 return "vnd.android.cursor.item/bookmark";
936
937 case URI_MATCH_SEARCHES:
938 return "vnd.android.cursor.dir/searches";
939
940 case URI_MATCH_SEARCHES_ID:
941 return "vnd.android.cursor.item/searches";
942
943 case URI_MATCH_SUGGEST:
944 return SearchManager.SUGGEST_MIME_TYPE;
945
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000946 case URI_MATCH_GEOLOCATION:
947 return "vnd.android.cursor.dir/geolocation";
948
The Android Open Source Project0c908882009-03-03 19:32:16 -0800949 default:
950 throw new IllegalArgumentException("Unknown URL");
951 }
952 }
953
954 @Override
955 public Uri insert(Uri url, ContentValues initialValues) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700956 boolean isBookmarkTable = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800957 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
958
959 int match = URI_MATCHER.match(url);
960 Uri uri = null;
961 switch (match) {
962 case URI_MATCH_BOOKMARKS: {
963 // Insert into the bookmarks table
964 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
965 initialValues);
966 if (rowID > 0) {
967 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
968 rowID);
969 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700970 isBookmarkTable = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800971 break;
972 }
973
974 case URI_MATCH_SEARCHES: {
975 // Insert into the searches table
976 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
977 initialValues);
978 if (rowID > 0) {
979 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
980 rowID);
981 }
982 break;
983 }
984
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +0000985 case URI_MATCH_GEOLOCATION:
986 String origin = initialValues.getAsString(Browser.GeolocationColumns.ORIGIN);
987 if (TextUtils.isEmpty(origin)) {
988 throw new IllegalArgumentException("Empty origin");
989 }
990 GeolocationPermissions.getInstance().allow(origin);
991 // TODO: Should we have one URI per permission?
992 uri = Browser.GEOLOCATION_URI;
993 break;
994
The Android Open Source Project0c908882009-03-03 19:32:16 -0800995 default:
996 throw new IllegalArgumentException("Unknown URL");
997 }
998
999 if (uri == null) {
1000 throw new IllegalArgumentException("Unknown URL");
1001 }
1002 getContext().getContentResolver().notifyChange(uri, null);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001003
Christopher Tatef0c36f72009-07-28 15:24:05 -07001004 // Back up the new bookmark set if we just inserted one.
1005 // A row created when bookmarks are added from scratch will have
1006 // bookmark=1 in the initial value set.
1007 if (isBookmarkTable
1008 && initialValues.containsKey(BookmarkColumns.BOOKMARK)
1009 && initialValues.getAsInteger(BookmarkColumns.BOOKMARK) != 0) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001010 mBackupManager.dataChanged();
1011 }
The Android Open Source Project0c908882009-03-03 19:32:16 -08001012 return uri;
1013 }
1014
1015 @Override
1016 public int delete(Uri url, String where, String[] whereArgs) {
1017 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
1018
1019 int match = URI_MATCHER.match(url);
1020 if (match == -1 || match == URI_MATCH_SUGGEST) {
1021 throw new IllegalArgumentException("Unknown URL");
1022 }
1023
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +00001024 if (match == URI_MATCH_GEOLOCATION) {
1025 return deleteGeolocation(url, where, whereArgs);
1026 }
1027
Christopher Tatef0c36f72009-07-28 15:24:05 -07001028 // need to know whether it's the bookmarks table for a couple of reasons
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001029 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
Christopher Tatef0c36f72009-07-28 15:24:05 -07001030 String id = null;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001031
1032 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001033 StringBuilder sb = new StringBuilder();
1034 if (where != null && where.length() > 0) {
1035 sb.append("( ");
1036 sb.append(where);
1037 sb.append(" ) AND ");
1038 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001039 id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001040 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -07001041 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001042 where = sb.toString();
1043 }
1044
Christopher Tatef0c36f72009-07-28 15:24:05 -07001045 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001046
Christopher Tatef0c36f72009-07-28 15:24:05 -07001047 // we'lll need to back up the bookmark set if we are about to delete one
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001048 if (isBookmarkTable) {
Christopher Tatef0c36f72009-07-28 15:24:05 -07001049 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
1050 new String[] { BookmarkColumns.BOOKMARK },
1051 "_id = " + id, null, null);
1052 if (cursor.moveToNext()) {
1053 if (cursor.getInt(0) != 0) {
1054 // yep, this record is a bookmark
1055 mBackupManager.dataChanged();
1056 }
1057 }
1058 cursor.close();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001059 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001060
1061 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
1062 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001063 return count;
1064 }
1065
Bjorn Bringerte6c4bd82010-02-09 12:11:27 +00001066 private int deleteGeolocation(Uri uri, String where, String[] whereArgs) {
1067 if (whereArgs.length != 1) {
1068 throw new IllegalArgumentException("Bad where arguments");
1069 }
1070 String origin = whereArgs[0];
1071 if (TextUtils.isEmpty(origin)) {
1072 throw new IllegalArgumentException("Empty origin");
1073 }
1074 GeolocationPermissions.getInstance().clear(origin);
1075 getContext().getContentResolver().notifyChange(Browser.GEOLOCATION_URI, null);
1076 return 1; // We always return 1, to avoid having to check whether anything was actually removed
1077 }
1078
The Android Open Source Project0c908882009-03-03 19:32:16 -08001079 @Override
1080 public int update(Uri url, ContentValues values, String where,
1081 String[] whereArgs) {
1082 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
1083
1084 int match = URI_MATCHER.match(url);
1085 if (match == -1 || match == URI_MATCH_SUGGEST) {
1086 throw new IllegalArgumentException("Unknown URL");
1087 }
1088
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001089 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001090 StringBuilder sb = new StringBuilder();
1091 if (where != null && where.length() > 0) {
1092 sb.append("( ");
1093 sb.append(where);
1094 sb.append(" ) AND ");
1095 }
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001096 String id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001097 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -07001098 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001099 where = sb.toString();
1100 }
1101
Christopher Tatef0c36f72009-07-28 15:24:05 -07001102 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001103
Christopher Tatef0c36f72009-07-28 15:24:05 -07001104 // Not all bookmark-table updates should be backed up. Look to see
1105 // whether we changed the title, url, or "is a bookmark" state, and
1106 // request a backup if so.
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001107 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_BOOKMARKS) {
1108 boolean changingBookmarks = false;
Christopher Tatef0c36f72009-07-28 15:24:05 -07001109 // Alterations to the bookmark field inherently change the bookmark
1110 // set, so we don't need to query the record; we know a priori that
1111 // we will need to back up this change.
1112 if (values.containsKey(BookmarkColumns.BOOKMARK)) {
1113 changingBookmarks = true;
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001114 } else if ((values.containsKey(BookmarkColumns.TITLE)
1115 || values.containsKey(BookmarkColumns.URL))
1116 && values.containsKey(BookmarkColumns._ID)) {
1117 // If a title or URL has been changed, check to see if it is to
1118 // a bookmark. The ID should have been included in the update,
1119 // so use it.
Christopher Tatef0c36f72009-07-28 15:24:05 -07001120 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
1121 new String[] { BookmarkColumns.BOOKMARK },
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001122 BookmarkColumns._ID + " = "
1123 + values.getAsString(BookmarkColumns._ID), null, null);
Christopher Tatef0c36f72009-07-28 15:24:05 -07001124 if (cursor.moveToNext()) {
1125 changingBookmarks = (cursor.getInt(0) != 0);
1126 }
1127 cursor.close();
1128 }
1129
1130 // if this *is* a bookmark row we're altering, we need to back it up.
1131 if (changingBookmarks) {
1132 mBackupManager.dataChanged();
1133 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001134 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001135
1136 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
1137 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001138 return ret;
1139 }
Satish Sampath565505b2009-05-29 15:37:27 +01001140
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001141 /**
1142 * Strips the provided url of preceding "http://" and any trailing "/". Does not
1143 * strip "https://". If the provided string cannot be stripped, the original string
1144 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +01001145 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001146 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +01001147 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001148 * @param url a url to strip, like "http://www.google.com/"
1149 * @return a stripped url like "www.google.com", or the original string if it could
1150 * not be stripped
1151 */
1152 private static String stripUrl(String url) {
1153 if (url == null) return null;
1154 Matcher m = STRIP_URL_PATTERN.matcher(url);
1155 if (m.matches() && m.groupCount() == 3) {
1156 return m.group(2);
1157 } else {
1158 return url;
1159 }
1160 }
1161
The Android Open Source Project0c908882009-03-03 19:32:16 -08001162}