blob: 72ec81937f867ce3d1c9c9102e728ade2e21a79a [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
Bjorn Bringertd2670652010-09-13 14:06:41 +010019import com.android.browser.search.SearchEngine;
20
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.app.SearchManager;
Christopher Tateb6a65442010-03-05 15:47:48 -080022import android.app.backup.BackupManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import 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 Projecta3c0aab2009-03-18 17:39:48 -070030import android.content.SharedPreferences.Editor;
Bjorn Bringertd2670652010-09-13 14:06:41 +010031import android.content.UriMatcher;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.database.AbstractCursor;
33import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.database.sqlite.SQLiteDatabase;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010035import android.database.sqlite.SQLiteOpenHelper;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.net.Uri;
Andrei Popescu93bea962010-03-23 15:04:36 +000037import android.os.Process;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070038import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.provider.Browser;
Christopher Tatef0c36f72009-07-28 15:24:05 -070040import android.provider.Browser.BookmarkColumns;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050041import android.speech.RecognizerResultsIntent;
Mike LeBeau21beb132009-05-13 14:57:50 -070042import android.text.TextUtils;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010043import android.util.Log;
Dianne Hackborn385effd2010-02-24 20:03:04 -080044import android.util.Patterns;
Dan Egnor5ee906c2009-11-18 12:11:49 -080045
Andrei Popescu1b20b9d2009-09-21 18:49:42 +010046import java.io.File;
47import java.io.FilenameFilter;
Leon Scroggins58d56c62010-01-28 15:12:40 -050048import java.util.ArrayList;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010049import java.util.Date;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070050import java.util.regex.Matcher;
51import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -080052
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070053
The Android Open Source Project0c908882009-03-03 19:32:16 -080054public class BrowserProvider extends ContentProvider {
55
56 private SQLiteOpenHelper mOpenHelper;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070057 private BackupManager mBackupManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080058 private static final String sDatabaseName = "browser.db";
59 private static final String TAG = "BrowserProvider";
60 private static final String ORDER_BY = "visits DESC, date DESC";
61
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070062 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
63 "viewer?source=androidclient";
64
The Android Open Source Project0c908882009-03-03 19:32:16 -080065 private static final String[] TABLE_NAMES = new String[] {
Bjorn Bringerta7611812010-03-24 11:12:02 +000066 "bookmarks", "searches"
The Android Open Source Project0c908882009-03-03 19:32:16 -080067 };
68 private static final String[] SUGGEST_PROJECTION = new String[] {
Leon Scrogginsb4464432009-11-25 12:37:50 -050069 "_id", "url", "title", "bookmark", "user_entered"
The Android Open Source Project0c908882009-03-03 19:32:16 -080070 };
Satish Sampath565505b2009-05-29 15:37:27 +010071 private static final String SUGGEST_SELECTION =
Leon Scrogginsb4464432009-11-25 12:37:50 -050072 "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?"
73 + " OR title LIKE ?) AND (bookmark = 1 OR user_entered = 1)";
Leon Scrogginsbd359cc2009-05-26 15:57:35 -040074 private String[] SUGGEST_ARGS = new String[5];
The Android Open Source Project0c908882009-03-03 19:32:16 -080075
76 // shared suggestion array index, make sure to match COLUMNS
77 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
78 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
79 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
80 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +000081 private static final int SUGGEST_COLUMN_TEXT_2_URL_ID = 5;
82 private static final int SUGGEST_COLUMN_ICON_1_ID = 6;
83 private static final int SUGGEST_COLUMN_ICON_2_ID = 7;
84 private static final int SUGGEST_COLUMN_QUERY_ID = 8;
Bjorn Bringert04851702009-09-22 10:36:01 +010085 private static final int SUGGEST_COLUMN_INTENT_EXTRA_DATA = 9;
The Android Open Source Project0c908882009-03-03 19:32:16 -080086
87 // shared suggestion columns
88 private static final String[] COLUMNS = new String[] {
89 "_id",
90 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
91 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
92 SearchManager.SUGGEST_COLUMN_TEXT_1,
93 SearchManager.SUGGEST_COLUMN_TEXT_2,
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +000094 SearchManager.SUGGEST_COLUMN_TEXT_2_URL,
The Android Open Source Project0c908882009-03-03 19:32:16 -080095 SearchManager.SUGGEST_COLUMN_ICON_1,
96 SearchManager.SUGGEST_COLUMN_ICON_2,
Mike LeBeau1ef26a32009-05-13 20:11:00 -070097 SearchManager.SUGGEST_COLUMN_QUERY,
Bjorn Bringert04851702009-09-22 10:36:01 +010098 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA};
The Android Open Source Project0c908882009-03-03 19:32:16 -080099
100 private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3;
101 private static final int MAX_SUGGESTION_LONG_ENTRIES = 6;
Leon Scroggins31887fd2009-05-18 16:58:08 -0400102 private static final String MAX_SUGGESTION_LONG_ENTRIES_STRING =
103 Integer.valueOf(MAX_SUGGESTION_LONG_ENTRIES).toString();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800104
105 // make sure that these match the index of TABLE_NAMES
106 private static final int URI_MATCH_BOOKMARKS = 0;
107 private static final int URI_MATCH_SEARCHES = 1;
108 // (id % 10) should match the table name index
109 private static final int URI_MATCH_BOOKMARKS_ID = 10;
110 private static final int URI_MATCH_SEARCHES_ID = 11;
111 //
112 private static final int URI_MATCH_SUGGEST = 20;
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100113 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114
115 private static final UriMatcher URI_MATCHER;
116
117 static {
118 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
119 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
120 URI_MATCH_BOOKMARKS);
121 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
122 URI_MATCH_BOOKMARKS_ID);
123 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
124 URI_MATCH_SEARCHES);
125 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
126 URI_MATCH_SEARCHES_ID);
127 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
128 URI_MATCH_SUGGEST);
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100129 URI_MATCHER.addURI("browser",
130 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
131 URI_MATCH_BOOKMARKS_SUGGEST);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132 }
133
134 // 1 -> 2 add cache table
135 // 2 -> 3 update history table
136 // 3 -> 4 add passwords table
137 // 4 -> 5 add settings table
138 // 5 -> 6 ?
139 // 6 -> 7 ?
140 // 7 -> 8 drop proxy table
141 // 8 -> 9 drop settings table
142 // 9 -> 10 add form_urls and form_data
143 // 10 -> 11 add searches table
144 // 11 -> 12 modify cache table
145 // 12 -> 13 modify cache table
146 // 13 -> 14 correspond with Google Bookmarks schema
147 // 14 -> 15 move couple of tables to either browser private database or webview database
148 // 15 -> 17 Set it up for the SearchManager
149 // 17 -> 18 Added favicon in bookmarks table for Home shortcuts
150 // 18 -> 19 Remove labels table
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400151 // 19 -> 20 Added thumbnail
Patrick Scott3918d442009-08-04 13:22:29 -0400152 // 20 -> 21 Added touch_icon
Grace Kloba6b52a552009-09-03 16:29:56 -0700153 // 21 -> 22 Remove "clientid"
Leon Scrogginsb4464432009-11-25 12:37:50 -0500154 // 22 -> 23 Added user_entered
Magnus Lindhult655e8672010-09-29 18:36:07 +0200155 // 23 -> 24 Url not allowed to be null anymore.
156 private static final int DATABASE_VERSION = 24;
Satish Sampath565505b2009-05-29 15:37:27 +0100157
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700158 // Regular expression which matches http://, followed by some stuff, followed by
159 // optionally a trailing slash, all matched as separate groups.
160 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
Satish Sampath565505b2009-05-29 15:37:27 +0100161
Bjorn Bringertd2670652010-09-13 14:06:41 +0100162 private BrowserSettings mSettings;
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100163
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164 public BrowserProvider() {
165 }
Satish Sampath565505b2009-05-29 15:37:27 +0100166
Patrick Scott43914692010-02-19 10:10:10 -0500167 // XXX: This is a major hack to remove our dependency on gsf constants and
168 // its content provider. http://b/issue?id=2425179
169 static String getClientId(ContentResolver cr) {
170 String ret = "android-google";
171 Cursor c = null;
172 try {
173 c = cr.query(Uri.parse("content://com.google.settings/partner"),
174 new String[] { "value" }, "name='client_id'", null, null);
175 if (c != null && c.moveToNext()) {
176 ret = c.getString(0);
177 }
178 } catch (RuntimeException ex) {
179 // fall through to return the default
180 } finally {
181 if (c != null) {
182 c.close();
183 }
184 }
185 return ret;
186 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800187
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700188 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800189 StringBuffer sb = new StringBuffer();
190 int lastCharLoc = 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100191
Patrick Scott43914692010-02-19 10:10:10 -0500192 final String client_id = getClientId(context.getContentResolver());
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700193
The Android Open Source Project0c908882009-03-03 19:32:16 -0800194 for (int i = 0; i < srcString.length(); ++i) {
195 char c = srcString.charAt(i);
196 if (c == '{') {
197 sb.append(srcString.subSequence(lastCharLoc, i));
198 lastCharLoc = i;
199 inner:
200 for (int j = i; j < srcString.length(); ++j) {
201 char k = srcString.charAt(j);
202 if (k == '}') {
203 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700204 if (propertyKeyValue.equals("CLIENT_ID")) {
205 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800206 } else {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700207 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800208 }
209 lastCharLoc = j + 1;
210 i = j;
211 break inner;
212 }
213 }
214 }
215 }
216 if (srcString.length() - lastCharLoc > 0) {
217 // Put on the tail, if there is one
218 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
219 }
220 return sb;
221 }
222
223 private static class DatabaseHelper extends SQLiteOpenHelper {
224 private Context mContext;
225
226 public DatabaseHelper(Context context) {
227 super(context, sDatabaseName, null, DATABASE_VERSION);
228 mContext = context;
229 }
230
231 @Override
232 public void onCreate(SQLiteDatabase db) {
233 db.execSQL("CREATE TABLE bookmarks (" +
234 "_id INTEGER PRIMARY KEY," +
235 "title TEXT," +
Magnus Lindhult655e8672010-09-29 18:36:07 +0200236 "url TEXT NOT NULL," +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800237 "visits INTEGER," +
238 "date LONG," +
239 "created LONG," +
240 "description TEXT," +
241 "bookmark INTEGER," +
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400242 "favicon BLOB DEFAULT NULL," +
Patrick Scott3918d442009-08-04 13:22:29 -0400243 "thumbnail BLOB DEFAULT NULL," +
Leon Scrogginsb4464432009-11-25 12:37:50 -0500244 "touch_icon BLOB DEFAULT NULL," +
245 "user_entered INTEGER" +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800246 ");");
247
248 final CharSequence[] bookmarks = mContext.getResources()
249 .getTextArray(R.array.bookmarks);
250 int size = bookmarks.length;
251 try {
252 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700253 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800254 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
255 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100256 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800257 "', 0, 0, 0, 1);");
258 }
259 } catch (ArrayIndexOutOfBoundsException e) {
260 }
261
262 db.execSQL("CREATE TABLE searches (" +
263 "_id INTEGER PRIMARY KEY," +
264 "search TEXT," +
265 "date LONG" +
266 ");");
267 }
268
269 @Override
270 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
271 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400272 + newVersion);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800273 if (oldVersion == 18) {
274 db.execSQL("DROP TABLE IF EXISTS labels");
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400275 }
276 if (oldVersion <= 19) {
277 db.execSQL("ALTER TABLE bookmarks ADD COLUMN thumbnail BLOB DEFAULT NULL;");
Patrick Scott3918d442009-08-04 13:22:29 -0400278 }
279 if (oldVersion < 21) {
280 db.execSQL("ALTER TABLE bookmarks ADD COLUMN touch_icon BLOB DEFAULT NULL;");
Grace Kloba6b52a552009-09-03 16:29:56 -0700281 }
282 if (oldVersion < 22) {
283 db.execSQL("DELETE FROM bookmarks WHERE (bookmark = 0 AND url LIKE \"%.google.%client=ms-%\")");
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100284 removeGears();
Leon Scrogginsb4464432009-11-25 12:37:50 -0500285 }
286 if (oldVersion < 23) {
287 db.execSQL("ALTER TABLE bookmarks ADD COLUMN user_entered INTEGER;");
Magnus Lindhult655e8672010-09-29 18:36:07 +0200288 }
289 if (oldVersion < 24) {
290 /* SQLite does not support ALTER COLUMN, hence the lengthy code. */
291 db.execSQL("DELETE FROM bookmarks WHERE url IS NULL;");
292 db.execSQL("ALTER TABLE bookmarks RENAME TO bookmarks_temp;");
293 db.execSQL("CREATE TABLE bookmarks (" +
294 "_id INTEGER PRIMARY KEY," +
295 "title TEXT," +
296 "url TEXT NOT NULL," +
297 "visits INTEGER," +
298 "date LONG," +
299 "created LONG," +
300 "description TEXT," +
301 "bookmark INTEGER," +
302 "favicon BLOB DEFAULT NULL," +
303 "thumbnail BLOB DEFAULT NULL," +
304 "touch_icon BLOB DEFAULT NULL," +
305 "user_entered INTEGER" +
306 ");");
307 db.execSQL("INSERT INTO bookmarks SELECT * FROM bookmarks_temp;");
308 db.execSQL("DROP TABLE bookmarks_temp;");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800309 } else {
310 db.execSQL("DROP TABLE IF EXISTS bookmarks");
311 db.execSQL("DROP TABLE IF EXISTS searches");
312 onCreate(db);
313 }
314 }
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100315
316 private void removeGears() {
Andrei Popescu93bea962010-03-23 15:04:36 +0000317 new Thread() {
318 @Override
319 public void run() {
320 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100321 String browserDataDirString = mContext.getApplicationInfo().dataDir;
322 final String appPluginsDirString = "app_plugins";
323 final String gearsPrefix = "gears";
324 File appPluginsDir = new File(browserDataDirString + File.separator
325 + appPluginsDirString);
326 if (!appPluginsDir.exists()) {
Andrei Popescu93bea962010-03-23 15:04:36 +0000327 return;
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100328 }
329 // Delete the Gears plugin files
330 File[] gearsFiles = appPluginsDir.listFiles(new FilenameFilter() {
331 public boolean accept(File dir, String filename) {
332 return filename.startsWith(gearsPrefix);
333 }
334 });
335 for (int i = 0; i < gearsFiles.length; ++i) {
336 if (gearsFiles[i].isDirectory()) {
337 deleteDirectory(gearsFiles[i]);
338 } else {
339 gearsFiles[i].delete();
340 }
341 }
342 // Delete the Gears data files
343 File gearsDataDir = new File(browserDataDirString + File.separator
344 + gearsPrefix);
345 if (!gearsDataDir.exists()) {
Andrei Popescu93bea962010-03-23 15:04:36 +0000346 return;
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100347 }
348 deleteDirectory(gearsDataDir);
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100349 }
350
351 private void deleteDirectory(File currentDir) {
352 File[] files = currentDir.listFiles();
353 for (int i = 0; i < files.length; ++i) {
354 if (files[i].isDirectory()) {
355 deleteDirectory(files[i]);
356 }
357 files[i].delete();
358 }
359 currentDir.delete();
360 }
Andrei Popescu93bea962010-03-23 15:04:36 +0000361 }.start();
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100362 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800363 }
364
365 @Override
366 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700367 final Context context = getContext();
368 mOpenHelper = new DatabaseHelper(context);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700369 mBackupManager = new BackupManager(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100370 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700371 // To avoid erasing the bookmark table, we added it explicitly for
372 // version 18 and 19 as in the other cases, we will erase the table.
373 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
374 SharedPreferences p = PreferenceManager
375 .getDefaultSharedPreferences(context);
376 boolean fix = p.getBoolean("fix_picasa", true);
377 if (fix) {
378 fixPicasaBookmark();
379 Editor ed = p.edit();
380 ed.putBoolean("fix_picasa", false);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700381 ed.apply();
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700382 }
383 }
Bjorn Bringertd2670652010-09-13 14:06:41 +0100384 mSettings = BrowserSettings.getInstance();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800385 return true;
386 }
387
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700388 private void fixPicasaBookmark() {
389 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
390 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
391 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
392 try {
393 if (!cursor.moveToFirst()) {
394 // set "created" so that it will be on the top of the list
395 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
396 "date, created, bookmark)" + " VALUES('" +
397 getContext().getString(R.string.picasa) + "', '"
398 + PICASA_URL + "', 0, 0, " + new Date().getTime()
399 + ", 1);");
400 }
401 } finally {
402 if (cursor != null) {
403 cursor.close();
404 }
405 }
406 }
407
The Android Open Source Project0c908882009-03-03 19:32:16 -0800408 /*
409 * Subclass AbstractCursor so we can combine multiple Cursors and add
Grace Kloba391df7c2010-03-01 19:51:49 -0800410 * "Search the web".
The Android Open Source Project0c908882009-03-03 19:32:16 -0800411 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100412 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
Grace Kloba391df7c2010-03-01 19:51:49 -0800413 * "Search the web";
414 * 2. If bookmark/history entries has a match, "Search the web" shows up at
415 * the second place. Otherwise, "Search the web" shows up at the first
416 * place.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800417 */
418 private class MySuggestionCursor extends AbstractCursor {
419 private Cursor mHistoryCursor;
420 private Cursor mSuggestCursor;
421 private int mHistoryCount;
422 private int mSuggestionCount;
Grace Klobad3992d42010-01-28 11:44:38 -0800423 private boolean mIncludeWebSearch;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800424 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100425 private int mSuggestText1Id;
426 private int mSuggestText2Id;
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000427 private int mSuggestText2UrlId;
Satish Sampath565505b2009-05-29 15:37:27 +0100428 private int mSuggestQueryId;
Bjorn Bringert04851702009-09-22 10:36:01 +0100429 private int mSuggestIntentExtraDataId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800430
431 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
432 mHistoryCursor = hc;
433 mSuggestCursor = sc;
434 mHistoryCount = hc.getCount();
435 mSuggestionCount = sc != null ? sc.getCount() : 0;
436 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
437 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
438 }
439 mString = string;
Grace Klobad3992d42010-01-28 11:44:38 -0800440 mIncludeWebSearch = string.length() > 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100441
442 // Some web suggest providers only give suggestions and have no description string for
443 // items. The order of the result columns may be different as well. So retrieve the
444 // column indices for the fields we need now and check before using below.
445 if (mSuggestCursor == null) {
446 mSuggestText1Id = -1;
447 mSuggestText2Id = -1;
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000448 mSuggestText2UrlId = -1;
Satish Sampath565505b2009-05-29 15:37:27 +0100449 mSuggestQueryId = -1;
Bjorn Bringert04851702009-09-22 10:36:01 +0100450 mSuggestIntentExtraDataId = -1;
Satish Sampath565505b2009-05-29 15:37:27 +0100451 } else {
452 mSuggestText1Id = mSuggestCursor.getColumnIndex(
453 SearchManager.SUGGEST_COLUMN_TEXT_1);
454 mSuggestText2Id = mSuggestCursor.getColumnIndex(
455 SearchManager.SUGGEST_COLUMN_TEXT_2);
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000456 mSuggestText2UrlId = mSuggestCursor.getColumnIndex(
457 SearchManager.SUGGEST_COLUMN_TEXT_2_URL);
Satish Sampath565505b2009-05-29 15:37:27 +0100458 mSuggestQueryId = mSuggestCursor.getColumnIndex(
459 SearchManager.SUGGEST_COLUMN_QUERY);
Bjorn Bringert04851702009-09-22 10:36:01 +0100460 mSuggestIntentExtraDataId = mSuggestCursor.getColumnIndex(
461 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA);
Satish Sampath565505b2009-05-29 15:37:27 +0100462 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800463 }
464
465 @Override
466 public boolean onMove(int oldPosition, int newPosition) {
467 if (mHistoryCursor == null) {
468 return false;
469 }
Grace Klobad3992d42010-01-28 11:44:38 -0800470 if (mIncludeWebSearch) {
Grace Kloba391df7c2010-03-01 19:51:49 -0800471 if (mHistoryCount == 0 && newPosition == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800472 return true;
Grace Kloba391df7c2010-03-01 19:51:49 -0800473 } else if (mHistoryCount > 0) {
474 if (newPosition == 0) {
475 mHistoryCursor.moveToPosition(0);
476 return true;
477 } else if (newPosition == 1) {
478 return true;
479 }
Grace Klobad3992d42010-01-28 11:44:38 -0800480 }
Grace Kloba391df7c2010-03-01 19:51:49 -0800481 newPosition--;
Grace Klobad3992d42010-01-28 11:44:38 -0800482 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800483 if (mHistoryCount > newPosition) {
484 mHistoryCursor.moveToPosition(newPosition);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800485 } else {
Grace Klobad3992d42010-01-28 11:44:38 -0800486 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800487 }
488 return true;
489 }
490
491 @Override
492 public int getCount() {
Grace Klobad3992d42010-01-28 11:44:38 -0800493 if (mIncludeWebSearch) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800494 return mHistoryCount + mSuggestionCount + 1;
495 } else {
496 return mHistoryCount + mSuggestionCount;
497 }
498 }
499
500 @Override
501 public String[] getColumnNames() {
502 return COLUMNS;
503 }
Satish Sampath565505b2009-05-29 15:37:27 +0100504
The Android Open Source Project0c908882009-03-03 19:32:16 -0800505 @Override
506 public String getString(int columnIndex) {
507 if ((mPos != -1 && mHistoryCursor != null)) {
Grace Kloba391df7c2010-03-01 19:51:49 -0800508 int type = -1; // 0: web search; 1: history; 2: suggestion
509 if (mIncludeWebSearch) {
510 if (mHistoryCount == 0 && mPos == 0) {
511 type = 0;
512 } else if (mHistoryCount > 0) {
513 if (mPos == 0) {
514 type = 1;
515 } else if (mPos == 1) {
516 type = 0;
517 }
518 }
519 if (type == -1) type = (mPos - 1) < mHistoryCount ? 1 : 2;
520 } else {
521 type = mPos < mHistoryCount ? 1 : 2;
522 }
523
The Android Open Source Project0c908882009-03-03 19:32:16 -0800524 switch(columnIndex) {
525 case SUGGEST_COLUMN_INTENT_ACTION_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800526 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800527 return Intent.ACTION_VIEW;
528 } else {
529 return Intent.ACTION_SEARCH;
530 }
531
532 case SUGGEST_COLUMN_INTENT_DATA_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800533 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800534 return mHistoryCursor.getString(1);
535 } else {
536 return null;
537 }
538
539 case SUGGEST_COLUMN_TEXT_1_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800540 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800541 return mString;
Grace Kloba391df7c2010-03-01 19:51:49 -0800542 } else if (type == 1) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700543 return getHistoryTitle();
Grace Klobad3992d42010-01-28 11:44:38 -0800544 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100545 if (mSuggestText1Id == -1) return null;
546 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800547 }
548
549 case SUGGEST_COLUMN_TEXT_2_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800550 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800551 return getContext().getString(R.string.search_the_web);
Grace Kloba391df7c2010-03-01 19:51:49 -0800552 } else if (type == 1) {
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000553 return null; // Use TEXT_2_URL instead
Grace Klobad3992d42010-01-28 11:44:38 -0800554 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100555 if (mSuggestText2Id == -1) return null;
556 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800557 }
558
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000559 case SUGGEST_COLUMN_TEXT_2_URL_ID:
560 if (type == 0) {
561 return null;
562 } else if (type == 1) {
563 return getHistoryUrl();
564 } else {
565 if (mSuggestText2UrlId == -1) return null;
566 return mSuggestCursor.getString(mSuggestText2UrlId);
567 }
568
The Android Open Source Project0c908882009-03-03 19:32:16 -0800569 case SUGGEST_COLUMN_ICON_1_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800570 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800571 if (mHistoryCursor.getInt(3) == 1) {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400572 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800573 R.drawable.ic_search_category_bookmark)
574 .toString();
575 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400576 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800577 R.drawable.ic_search_category_history)
578 .toString();
579 }
580 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400581 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800582 R.drawable.ic_search_category_suggest)
583 .toString();
584 }
585
586 case SUGGEST_COLUMN_ICON_2_ID:
Leon Scroggins31887fd2009-05-18 16:58:08 -0400587 return "0";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800588
589 case SUGGEST_COLUMN_QUERY_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800590 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800591 return mString;
Grace Kloba391df7c2010-03-01 19:51:49 -0800592 } else if (type == 1) {
Mike LeBeau2af73052009-06-23 17:36:59 -0700593 // Return the url in the intent query column. This is ignored
594 // within the browser because our searchable is set to
595 // android:searchMode="queryRewriteFromData", but it is used by
596 // global search for query rewriting.
597 return mHistoryCursor.getString(1);
Grace Klobad3992d42010-01-28 11:44:38 -0800598 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100599 if (mSuggestQueryId == -1) return null;
600 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800601 }
Satish Sampath565505b2009-05-29 15:37:27 +0100602
Bjorn Bringert04851702009-09-22 10:36:01 +0100603 case SUGGEST_COLUMN_INTENT_EXTRA_DATA:
Grace Kloba391df7c2010-03-01 19:51:49 -0800604 if (type == 0) {
Bjorn Bringert04851702009-09-22 10:36:01 +0100605 return null;
Grace Kloba391df7c2010-03-01 19:51:49 -0800606 } else if (type == 1) {
Grace Klobad3992d42010-01-28 11:44:38 -0800607 return null;
608 } else {
Bjorn Bringert04851702009-09-22 10:36:01 +0100609 if (mSuggestIntentExtraDataId == -1) return null;
610 return mSuggestCursor.getString(mSuggestIntentExtraDataId);
Bjorn Bringert04851702009-09-22 10:36:01 +0100611 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800612 }
613 }
614 return null;
615 }
616
617 @Override
618 public double getDouble(int column) {
619 throw new UnsupportedOperationException();
620 }
621
622 @Override
623 public float getFloat(int column) {
624 throw new UnsupportedOperationException();
625 }
626
627 @Override
628 public int getInt(int column) {
629 throw new UnsupportedOperationException();
630 }
631
632 @Override
633 public long getLong(int column) {
634 if ((mPos != -1) && column == 0) {
635 return mPos; // use row# as the _Id
636 }
637 throw new UnsupportedOperationException();
638 }
639
640 @Override
641 public short getShort(int column) {
642 throw new UnsupportedOperationException();
643 }
644
645 @Override
646 public boolean isNull(int column) {
647 throw new UnsupportedOperationException();
648 }
649
650 // TODO Temporary change, finalize after jq's changes go in
651 public void deactivate() {
652 if (mHistoryCursor != null) {
653 mHistoryCursor.deactivate();
654 }
655 if (mSuggestCursor != null) {
656 mSuggestCursor.deactivate();
657 }
658 super.deactivate();
659 }
660
661 public boolean requery() {
662 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
663 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
664 }
665
666 // TODO Temporary change, finalize after jq's changes go in
667 public void close() {
668 super.close();
669 if (mHistoryCursor != null) {
670 mHistoryCursor.close();
671 mHistoryCursor = null;
672 }
673 if (mSuggestCursor != null) {
674 mSuggestCursor.close();
675 mSuggestCursor = null;
676 }
677 }
Satish Sampath565505b2009-05-29 15:37:27 +0100678
Mike LeBeau21beb132009-05-13 14:57:50 -0700679 /**
680 * Provides the title (text line 1) for a browser suggestion, which should be the
681 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100682 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700683 * @return the title string to use
684 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700685 private String getHistoryTitle() {
686 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700687 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000688 title = stripUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700689 }
690 return title;
691 }
Satish Sampath565505b2009-05-29 15:37:27 +0100692
Mike LeBeau21beb132009-05-13 14:57:50 -0700693 /**
694 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
695 * webpage url. If the webpage title is empty, then the url should go in the title
696 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100697 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700698 * @return the subtitle string to use, or null if none
699 */
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000700 private String getHistoryUrl() {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700701 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700702 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
703 return null;
704 } else {
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000705 return stripUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700706 }
707 }
Satish Sampath565505b2009-05-29 15:37:27 +0100708
The Android Open Source Project0c908882009-03-03 19:32:16 -0800709 }
710
Leon Scroggins58d56c62010-01-28 15:12:40 -0500711 private static class ResultsCursor extends AbstractCursor {
712 // Array indices for RESULTS_COLUMNS
713 private static final int RESULT_ACTION_ID = 1;
714 private static final int RESULT_DATA_ID = 2;
715 private static final int RESULT_TEXT_ID = 3;
716 private static final int RESULT_ICON_ID = 4;
717 private static final int RESULT_EXTRA_ID = 5;
718
719 private static final String[] RESULTS_COLUMNS = new String[] {
720 "_id",
721 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
722 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
723 SearchManager.SUGGEST_COLUMN_TEXT_1,
724 SearchManager.SUGGEST_COLUMN_ICON_1,
725 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA
726 };
727 private final ArrayList<String> mResults;
728 public ResultsCursor(ArrayList<String> results) {
729 mResults = results;
730 }
731 public int getCount() { return mResults.size(); }
732
733 public String[] getColumnNames() {
734 return RESULTS_COLUMNS;
735 }
736
737 public String getString(int column) {
738 switch (column) {
739 case RESULT_ACTION_ID:
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500740 return RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500741 case RESULT_TEXT_ID:
742 // The data is used when the phone is in landscape mode. We
743 // still want to show the result string.
744 case RESULT_DATA_ID:
745 return mResults.get(mPos);
746 case RESULT_EXTRA_ID:
747 // The Intent's extra data will store the index into
748 // mResults so the BrowserActivity will know which result to
749 // use.
750 return Integer.toString(mPos);
751 case RESULT_ICON_ID:
752 return Integer.valueOf(R.drawable.magnifying_glass)
753 .toString();
754 default:
755 return null;
756 }
757 }
758 public short getShort(int column) {
759 throw new UnsupportedOperationException();
760 }
761 public int getInt(int column) {
762 throw new UnsupportedOperationException();
763 }
764 public long getLong(int column) {
765 if ((mPos != -1) && column == 0) {
766 return mPos; // use row# as the _id
767 }
768 throw new UnsupportedOperationException();
769 }
770 public float getFloat(int column) {
771 throw new UnsupportedOperationException();
772 }
773 public double getDouble(int column) {
774 throw new UnsupportedOperationException();
775 }
776 public boolean isNull(int column) {
777 throw new UnsupportedOperationException();
778 }
779 }
780
781 private ResultsCursor mResultsCursor;
782
783 /**
784 * Provide a set of results to be returned to query, intended to be used
785 * by the SearchDialog when the BrowserActivity is in voice search mode.
786 * @param results Strings to display in the dropdown from the SearchDialog
787 */
788 /* package */ void setQueryResults(ArrayList<String> results) {
789 if (results == null) {
790 mResultsCursor = null;
791 } else {
792 mResultsCursor = new ResultsCursor(results);
793 }
794 }
795
The Android Open Source Project0c908882009-03-03 19:32:16 -0800796 @Override
797 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100798 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800799 throws IllegalStateException {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800800 int match = URI_MATCHER.match(url);
801 if (match == -1) {
802 throw new IllegalArgumentException("Unknown URL");
803 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500804 if (match == URI_MATCH_SUGGEST && mResultsCursor != null) {
805 Cursor results = mResultsCursor;
806 mResultsCursor = null;
807 return results;
808 }
809 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800810
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100811 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800812 String suggestSelection;
813 String [] myArgs;
814 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
815 suggestSelection = null;
816 myArgs = null;
817 } else {
818 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700819 if (selectionArgs[0].startsWith("http")
820 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800821 myArgs = new String[1];
822 myArgs[0] = like;
823 suggestSelection = selection;
824 } else {
825 SUGGEST_ARGS[0] = "http://" + like;
826 SUGGEST_ARGS[1] = "http://www." + like;
827 SUGGEST_ARGS[2] = "https://" + like;
828 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400829 // To match against titles.
830 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800831 myArgs = SUGGEST_ARGS;
832 suggestSelection = SUGGEST_SELECTION;
833 }
834 }
835
836 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
837 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Leon Scroggins31887fd2009-05-18 16:58:08 -0400838 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800839
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100840 if (match == URI_MATCH_BOOKMARKS_SUGGEST
Dan Egnor5ee906c2009-11-18 12:11:49 -0800841 || Patterns.WEB_URL.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800842 return new MySuggestionCursor(c, null, "");
843 } else {
Bjorn Bringertd2670652010-09-13 14:06:41 +0100844 // get search suggestions if there is still space in the list
The Android Open Source Project0c908882009-03-03 19:32:16 -0800845 if (myArgs != null && myArgs.length > 1
846 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringertd2670652010-09-13 14:06:41 +0100847 SearchEngine searchEngine = mSettings.getSearchEngine();
848 if (searchEngine != null && searchEngine.supportsSuggestions()) {
849 Cursor sc = searchEngine.getSuggestions(getContext(), selectionArgs[0]);
850 return new MySuggestionCursor(c, sc, selectionArgs[0]);
851 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800852 }
853 return new MySuggestionCursor(c, null, selectionArgs[0]);
854 }
855 }
856
857 String[] projection = null;
858 if (projectionIn != null && projectionIn.length > 0) {
859 projection = new String[projectionIn.length + 1];
860 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
861 projection[projectionIn.length] = "_id AS _id";
862 }
863
864 StringBuilder whereClause = new StringBuilder(256);
865 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
866 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
867 .append(")");
868 }
869
870 // Tack on the user's selection, if present
871 if (selection != null && selection.length() > 0) {
872 if (whereClause.length() > 0) {
873 whereClause.append(" AND ");
874 }
875
876 whereClause.append('(');
877 whereClause.append(selection);
878 whereClause.append(')');
879 }
880 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
881 whereClause.toString(), selectionArgs, null, null, sortOrder,
882 null);
883 c.setNotificationUri(getContext().getContentResolver(), url);
884 return c;
885 }
886
887 @Override
888 public String getType(Uri url) {
889 int match = URI_MATCHER.match(url);
890 switch (match) {
891 case URI_MATCH_BOOKMARKS:
892 return "vnd.android.cursor.dir/bookmark";
893
894 case URI_MATCH_BOOKMARKS_ID:
895 return "vnd.android.cursor.item/bookmark";
896
897 case URI_MATCH_SEARCHES:
898 return "vnd.android.cursor.dir/searches";
899
900 case URI_MATCH_SEARCHES_ID:
901 return "vnd.android.cursor.item/searches";
902
903 case URI_MATCH_SUGGEST:
904 return SearchManager.SUGGEST_MIME_TYPE;
905
906 default:
907 throw new IllegalArgumentException("Unknown URL");
908 }
909 }
910
911 @Override
912 public Uri insert(Uri url, ContentValues initialValues) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700913 boolean isBookmarkTable = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800914 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
915
916 int match = URI_MATCHER.match(url);
917 Uri uri = null;
918 switch (match) {
919 case URI_MATCH_BOOKMARKS: {
920 // Insert into the bookmarks table
921 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
922 initialValues);
923 if (rowID > 0) {
924 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
925 rowID);
926 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700927 isBookmarkTable = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800928 break;
929 }
930
931 case URI_MATCH_SEARCHES: {
932 // Insert into the searches table
933 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
934 initialValues);
935 if (rowID > 0) {
936 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
937 rowID);
938 }
939 break;
940 }
941
942 default:
943 throw new IllegalArgumentException("Unknown URL");
944 }
945
946 if (uri == null) {
947 throw new IllegalArgumentException("Unknown URL");
948 }
949 getContext().getContentResolver().notifyChange(uri, null);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700950
Christopher Tatef0c36f72009-07-28 15:24:05 -0700951 // Back up the new bookmark set if we just inserted one.
952 // A row created when bookmarks are added from scratch will have
953 // bookmark=1 in the initial value set.
954 if (isBookmarkTable
955 && initialValues.containsKey(BookmarkColumns.BOOKMARK)
956 && initialValues.getAsInteger(BookmarkColumns.BOOKMARK) != 0) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700957 mBackupManager.dataChanged();
958 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800959 return uri;
960 }
961
962 @Override
963 public int delete(Uri url, String where, String[] whereArgs) {
964 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
965
966 int match = URI_MATCHER.match(url);
967 if (match == -1 || match == URI_MATCH_SUGGEST) {
968 throw new IllegalArgumentException("Unknown URL");
969 }
970
Christopher Tatef0c36f72009-07-28 15:24:05 -0700971 // need to know whether it's the bookmarks table for a couple of reasons
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700972 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
Christopher Tatef0c36f72009-07-28 15:24:05 -0700973 String id = null;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700974
975 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800976 StringBuilder sb = new StringBuilder();
977 if (where != null && where.length() > 0) {
978 sb.append("( ");
979 sb.append(where);
980 sb.append(" ) AND ");
981 }
Christopher Tatef0c36f72009-07-28 15:24:05 -0700982 id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800983 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -0700984 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800985 where = sb.toString();
986 }
987
Christopher Tatef0c36f72009-07-28 15:24:05 -0700988 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700989
Christopher Tatef0c36f72009-07-28 15:24:05 -0700990 // we'lll need to back up the bookmark set if we are about to delete one
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700991 if (isBookmarkTable) {
Christopher Tatef0c36f72009-07-28 15:24:05 -0700992 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
993 new String[] { BookmarkColumns.BOOKMARK },
994 "_id = " + id, null, null);
995 if (cursor.moveToNext()) {
996 if (cursor.getInt(0) != 0) {
997 // yep, this record is a bookmark
998 mBackupManager.dataChanged();
999 }
1000 }
1001 cursor.close();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001002 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001003
1004 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
1005 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001006 return count;
1007 }
1008
1009 @Override
1010 public int update(Uri url, ContentValues values, String where,
1011 String[] whereArgs) {
1012 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
1013
1014 int match = URI_MATCHER.match(url);
1015 if (match == -1 || match == URI_MATCH_SUGGEST) {
1016 throw new IllegalArgumentException("Unknown URL");
1017 }
1018
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001019 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -08001020 StringBuilder sb = new StringBuilder();
1021 if (where != null && where.length() > 0) {
1022 sb.append("( ");
1023 sb.append(where);
1024 sb.append(" ) AND ");
1025 }
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001026 String id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001027 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -07001028 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001029 where = sb.toString();
1030 }
1031
Christopher Tatef0c36f72009-07-28 15:24:05 -07001032 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001033
Christopher Tatef0c36f72009-07-28 15:24:05 -07001034 // Not all bookmark-table updates should be backed up. Look to see
1035 // whether we changed the title, url, or "is a bookmark" state, and
1036 // request a backup if so.
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001037 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_BOOKMARKS) {
1038 boolean changingBookmarks = false;
Christopher Tatef0c36f72009-07-28 15:24:05 -07001039 // Alterations to the bookmark field inherently change the bookmark
1040 // set, so we don't need to query the record; we know a priori that
1041 // we will need to back up this change.
1042 if (values.containsKey(BookmarkColumns.BOOKMARK)) {
1043 changingBookmarks = true;
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001044 } else if ((values.containsKey(BookmarkColumns.TITLE)
1045 || values.containsKey(BookmarkColumns.URL))
1046 && values.containsKey(BookmarkColumns._ID)) {
1047 // If a title or URL has been changed, check to see if it is to
1048 // a bookmark. The ID should have been included in the update,
1049 // so use it.
Christopher Tatef0c36f72009-07-28 15:24:05 -07001050 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
1051 new String[] { BookmarkColumns.BOOKMARK },
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001052 BookmarkColumns._ID + " = "
1053 + values.getAsString(BookmarkColumns._ID), null, null);
Christopher Tatef0c36f72009-07-28 15:24:05 -07001054 if (cursor.moveToNext()) {
1055 changingBookmarks = (cursor.getInt(0) != 0);
1056 }
1057 cursor.close();
1058 }
1059
1060 // if this *is* a bookmark row we're altering, we need to back it up.
1061 if (changingBookmarks) {
1062 mBackupManager.dataChanged();
1063 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001064 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001065
1066 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
1067 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001068 return ret;
1069 }
Satish Sampath565505b2009-05-29 15:37:27 +01001070
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001071 /**
1072 * Strips the provided url of preceding "http://" and any trailing "/". Does not
1073 * strip "https://". If the provided string cannot be stripped, the original string
1074 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +01001075 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001076 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +01001077 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001078 * @param url a url to strip, like "http://www.google.com/"
1079 * @return a stripped url like "www.google.com", or the original string if it could
1080 * not be stripped
1081 */
1082 private static String stripUrl(String url) {
1083 if (url == null) return null;
1084 Matcher m = STRIP_URL_PATTERN.matcher(url);
1085 if (m.matches() && m.groupCount() == 3) {
1086 return m.group(2);
1087 } else {
1088 return url;
1089 }
1090 }
1091
The Android Open Source Project0c908882009-03-03 19:32:16 -08001092}