blob: 47477217ccaf781948f441b21917c5e7c5e1d751 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.app.SearchManager;
Bjorn Bringertabc3ac82009-12-04 12:59:14 +000020import android.app.SearchableInfo;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070021import android.backup.BackupManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.content.ComponentName;
23import android.content.ContentProvider;
Christopher Tatef0c36f72009-07-28 15:24:05 -070024import android.content.ContentResolver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.content.ContentUris;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070029import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.content.UriMatcher;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070031import android.content.SharedPreferences.Editor;
Satish Sampath565505b2009-05-29 15:37:27 +010032import android.content.pm.PackageManager;
33import android.content.pm.ResolveInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.database.AbstractCursor;
Leon Scroggins62b71f72009-06-12 17:51:22 -040035import android.database.ContentObserver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.database.sqlite.SQLiteDatabase;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010038import android.database.sqlite.SQLiteOpenHelper;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.net.Uri;
Andrei Popescu1b20b9d2009-09-21 18:49:42 +010040import android.os.AsyncTask;
Leon Scroggins62b71f72009-06-12 17:51:22 -040041import android.os.Handler;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070042import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.provider.Browser;
Leon Scroggins62b71f72009-06-12 17:51:22 -040044import android.provider.Settings;
Christopher Tatef0c36f72009-07-28 15:24:05 -070045import android.provider.Browser.BookmarkColumns;
Mike LeBeau21beb132009-05-13 14:57:50 -070046import android.text.TextUtils;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010047import android.util.Log;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070048import android.util.TypedValue;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010049
Dan Egnor5ee906c2009-11-18 12:11:49 -080050import com.android.common.Patterns;
51
52import com.google.android.providers.GoogleSettings.Partner;
53
Andrei Popescu1b20b9d2009-09-21 18:49:42 +010054import java.io.File;
55import java.io.FilenameFilter;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010056import java.util.Date;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070057import java.util.regex.Matcher;
58import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -080059
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070060
The Android Open Source Project0c908882009-03-03 19:32:16 -080061public class BrowserProvider extends ContentProvider {
62
63 private SQLiteOpenHelper mOpenHelper;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070064 private BackupManager mBackupManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080065 private static final String sDatabaseName = "browser.db";
66 private static final String TAG = "BrowserProvider";
67 private static final String ORDER_BY = "visits DESC, date DESC";
68
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070069 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
70 "viewer?source=androidclient";
71
The Android Open Source Project0c908882009-03-03 19:32:16 -080072 private static final String[] TABLE_NAMES = new String[] {
73 "bookmarks", "searches"
74 };
75 private static final String[] SUGGEST_PROJECTION = new String[] {
Leon Scrogginsb4464432009-11-25 12:37:50 -050076 "_id", "url", "title", "bookmark", "user_entered"
The Android Open Source Project0c908882009-03-03 19:32:16 -080077 };
Satish Sampath565505b2009-05-29 15:37:27 +010078 private static final String SUGGEST_SELECTION =
Leon Scrogginsb4464432009-11-25 12:37:50 -050079 "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?"
80 + " OR title LIKE ?) AND (bookmark = 1 OR user_entered = 1)";
Leon Scrogginsbd359cc2009-05-26 15:57:35 -040081 private String[] SUGGEST_ARGS = new String[5];
The Android Open Source Project0c908882009-03-03 19:32:16 -080082
83 // shared suggestion array index, make sure to match COLUMNS
84 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
85 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
86 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
87 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
88 private static final int SUGGEST_COLUMN_ICON_1_ID = 5;
89 private static final int SUGGEST_COLUMN_ICON_2_ID = 6;
90 private static final int SUGGEST_COLUMN_QUERY_ID = 7;
Mike LeBeau1ef26a32009-05-13 20:11:00 -070091 private static final int SUGGEST_COLUMN_FORMAT = 8;
Bjorn Bringert04851702009-09-22 10:36:01 +010092 private static final int SUGGEST_COLUMN_INTENT_EXTRA_DATA = 9;
The Android Open Source Project0c908882009-03-03 19:32:16 -080093
94 // shared suggestion columns
95 private static final String[] COLUMNS = new String[] {
96 "_id",
97 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
98 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
99 SearchManager.SUGGEST_COLUMN_TEXT_1,
100 SearchManager.SUGGEST_COLUMN_TEXT_2,
101 SearchManager.SUGGEST_COLUMN_ICON_1,
102 SearchManager.SUGGEST_COLUMN_ICON_2,
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700103 SearchManager.SUGGEST_COLUMN_QUERY,
Bjorn Bringert04851702009-09-22 10:36:01 +0100104 SearchManager.SUGGEST_COLUMN_FORMAT,
105 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA};
The Android Open Source Project0c908882009-03-03 19:32:16 -0800106
107 private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3;
108 private static final int MAX_SUGGESTION_LONG_ENTRIES = 6;
Leon Scroggins31887fd2009-05-18 16:58:08 -0400109 private static final String MAX_SUGGESTION_LONG_ENTRIES_STRING =
110 Integer.valueOf(MAX_SUGGESTION_LONG_ENTRIES).toString();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800111
112 // make sure that these match the index of TABLE_NAMES
113 private static final int URI_MATCH_BOOKMARKS = 0;
114 private static final int URI_MATCH_SEARCHES = 1;
115 // (id % 10) should match the table name index
116 private static final int URI_MATCH_BOOKMARKS_ID = 10;
117 private static final int URI_MATCH_SEARCHES_ID = 11;
118 //
119 private static final int URI_MATCH_SUGGEST = 20;
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100120 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121
122 private static final UriMatcher URI_MATCHER;
123
124 static {
125 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
126 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
127 URI_MATCH_BOOKMARKS);
128 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
129 URI_MATCH_BOOKMARKS_ID);
130 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
131 URI_MATCH_SEARCHES);
132 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
133 URI_MATCH_SEARCHES_ID);
134 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
135 URI_MATCH_SUGGEST);
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100136 URI_MATCHER.addURI("browser",
137 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
138 URI_MATCH_BOOKMARKS_SUGGEST);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800139 }
140
141 // 1 -> 2 add cache table
142 // 2 -> 3 update history table
143 // 3 -> 4 add passwords table
144 // 4 -> 5 add settings table
145 // 5 -> 6 ?
146 // 6 -> 7 ?
147 // 7 -> 8 drop proxy table
148 // 8 -> 9 drop settings table
149 // 9 -> 10 add form_urls and form_data
150 // 10 -> 11 add searches table
151 // 11 -> 12 modify cache table
152 // 12 -> 13 modify cache table
153 // 13 -> 14 correspond with Google Bookmarks schema
154 // 14 -> 15 move couple of tables to either browser private database or webview database
155 // 15 -> 17 Set it up for the SearchManager
156 // 17 -> 18 Added favicon in bookmarks table for Home shortcuts
157 // 18 -> 19 Remove labels table
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400158 // 19 -> 20 Added thumbnail
Patrick Scott3918d442009-08-04 13:22:29 -0400159 // 20 -> 21 Added touch_icon
Grace Kloba6b52a552009-09-03 16:29:56 -0700160 // 21 -> 22 Remove "clientid"
Leon Scrogginsb4464432009-11-25 12:37:50 -0500161 // 22 -> 23 Added user_entered
162 private static final int DATABASE_VERSION = 23;
Satish Sampath565505b2009-05-29 15:37:27 +0100163
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700164 // Regular expression which matches http://, followed by some stuff, followed by
165 // optionally a trailing slash, all matched as separate groups.
166 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
Satish Sampath565505b2009-05-29 15:37:27 +0100167
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100168 private SearchManager mSearchManager;
169
Satish Sampath60d24e22009-07-09 16:46:08 +0100170 // The ID of the ColorStateList to be applied to urls of website suggestions, as derived from
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700171 // the current theme. This is not set until/unless beautifyUrl is called, at which point
172 // this variable caches the color value.
Satish Sampath60d24e22009-07-09 16:46:08 +0100173 private static String mSearchUrlColorId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800174
175 public BrowserProvider() {
176 }
Satish Sampath565505b2009-05-29 15:37:27 +0100177
The Android Open Source Project0c908882009-03-03 19:32:16 -0800178
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700179 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800180 StringBuffer sb = new StringBuffer();
181 int lastCharLoc = 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100182
Ramanan Rajeswaran378e5722009-07-01 08:46:58 -0700183 final String client_id = Partner.getString(context.getContentResolver(),
184 Partner.CLIENT_ID, "android-google");
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700185
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 for (int i = 0; i < srcString.length(); ++i) {
187 char c = srcString.charAt(i);
188 if (c == '{') {
189 sb.append(srcString.subSequence(lastCharLoc, i));
190 lastCharLoc = i;
191 inner:
192 for (int j = i; j < srcString.length(); ++j) {
193 char k = srcString.charAt(j);
194 if (k == '}') {
195 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700196 if (propertyKeyValue.equals("CLIENT_ID")) {
197 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800198 } else {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700199 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800200 }
201 lastCharLoc = j + 1;
202 i = j;
203 break inner;
204 }
205 }
206 }
207 }
208 if (srcString.length() - lastCharLoc > 0) {
209 // Put on the tail, if there is one
210 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
211 }
212 return sb;
213 }
214
215 private static class DatabaseHelper extends SQLiteOpenHelper {
216 private Context mContext;
217
218 public DatabaseHelper(Context context) {
219 super(context, sDatabaseName, null, DATABASE_VERSION);
220 mContext = context;
221 }
222
223 @Override
224 public void onCreate(SQLiteDatabase db) {
225 db.execSQL("CREATE TABLE bookmarks (" +
226 "_id INTEGER PRIMARY KEY," +
227 "title TEXT," +
228 "url TEXT," +
229 "visits INTEGER," +
230 "date LONG," +
231 "created LONG," +
232 "description TEXT," +
233 "bookmark INTEGER," +
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400234 "favicon BLOB DEFAULT NULL," +
Patrick Scott3918d442009-08-04 13:22:29 -0400235 "thumbnail BLOB DEFAULT NULL," +
Leon Scrogginsb4464432009-11-25 12:37:50 -0500236 "touch_icon BLOB DEFAULT NULL," +
237 "user_entered INTEGER" +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800238 ");");
239
240 final CharSequence[] bookmarks = mContext.getResources()
241 .getTextArray(R.array.bookmarks);
242 int size = bookmarks.length;
243 try {
244 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700245 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800246 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
247 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100248 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800249 "', 0, 0, 0, 1);");
250 }
251 } catch (ArrayIndexOutOfBoundsException e) {
252 }
253
254 db.execSQL("CREATE TABLE searches (" +
255 "_id INTEGER PRIMARY KEY," +
256 "search TEXT," +
257 "date LONG" +
258 ");");
259 }
260
261 @Override
262 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
263 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400264 + newVersion);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800265 if (oldVersion == 18) {
266 db.execSQL("DROP TABLE IF EXISTS labels");
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400267 }
268 if (oldVersion <= 19) {
269 db.execSQL("ALTER TABLE bookmarks ADD COLUMN thumbnail BLOB DEFAULT NULL;");
Patrick Scott3918d442009-08-04 13:22:29 -0400270 }
271 if (oldVersion < 21) {
272 db.execSQL("ALTER TABLE bookmarks ADD COLUMN touch_icon BLOB DEFAULT NULL;");
Grace Kloba6b52a552009-09-03 16:29:56 -0700273 }
274 if (oldVersion < 22) {
275 db.execSQL("DELETE FROM bookmarks WHERE (bookmark = 0 AND url LIKE \"%.google.%client=ms-%\")");
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100276 removeGears();
Leon Scrogginsb4464432009-11-25 12:37:50 -0500277 }
278 if (oldVersion < 23) {
279 db.execSQL("ALTER TABLE bookmarks ADD COLUMN user_entered INTEGER;");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800280 } else {
281 db.execSQL("DROP TABLE IF EXISTS bookmarks");
282 db.execSQL("DROP TABLE IF EXISTS searches");
283 onCreate(db);
284 }
285 }
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100286
287 private void removeGears() {
288 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
289 public Void doInBackground(Void... unused) {
290 String browserDataDirString = mContext.getApplicationInfo().dataDir;
291 final String appPluginsDirString = "app_plugins";
292 final String gearsPrefix = "gears";
293 File appPluginsDir = new File(browserDataDirString + File.separator
294 + appPluginsDirString);
295 if (!appPluginsDir.exists()) {
296 return null;
297 }
298 // Delete the Gears plugin files
299 File[] gearsFiles = appPluginsDir.listFiles(new FilenameFilter() {
300 public boolean accept(File dir, String filename) {
301 return filename.startsWith(gearsPrefix);
302 }
303 });
304 for (int i = 0; i < gearsFiles.length; ++i) {
305 if (gearsFiles[i].isDirectory()) {
306 deleteDirectory(gearsFiles[i]);
307 } else {
308 gearsFiles[i].delete();
309 }
310 }
311 // Delete the Gears data files
312 File gearsDataDir = new File(browserDataDirString + File.separator
313 + gearsPrefix);
314 if (!gearsDataDir.exists()) {
315 return null;
316 }
317 deleteDirectory(gearsDataDir);
318 return null;
319 }
320
321 private void deleteDirectory(File currentDir) {
322 File[] files = currentDir.listFiles();
323 for (int i = 0; i < files.length; ++i) {
324 if (files[i].isDirectory()) {
325 deleteDirectory(files[i]);
326 }
327 files[i].delete();
328 }
329 currentDir.delete();
330 }
331 };
332
333 task.execute();
334 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800335 }
336
337 @Override
338 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700339 final Context context = getContext();
340 mOpenHelper = new DatabaseHelper(context);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700341 mBackupManager = new BackupManager(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100342 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700343 // To avoid erasing the bookmark table, we added it explicitly for
344 // version 18 and 19 as in the other cases, we will erase the table.
345 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
346 SharedPreferences p = PreferenceManager
347 .getDefaultSharedPreferences(context);
348 boolean fix = p.getBoolean("fix_picasa", true);
349 if (fix) {
350 fixPicasaBookmark();
351 Editor ed = p.edit();
352 ed.putBoolean("fix_picasa", false);
353 ed.commit();
354 }
355 }
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100356 mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400357 mShowWebSuggestionsSettingChangeObserver
358 = new ShowWebSuggestionsSettingChangeObserver();
359 context.getContentResolver().registerContentObserver(
360 Settings.System.getUriFor(
361 Settings.System.SHOW_WEB_SUGGESTIONS),
362 true, mShowWebSuggestionsSettingChangeObserver);
363 updateShowWebSuggestions();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800364 return true;
365 }
366
Leon Scroggins62b71f72009-06-12 17:51:22 -0400367 /**
368 * This Observer will ensure that if the user changes the system
369 * setting of whether to display web suggestions, we will
370 * change accordingly.
371 */
372 /* package */ class ShowWebSuggestionsSettingChangeObserver
373 extends ContentObserver {
374 public ShowWebSuggestionsSettingChangeObserver() {
375 super(new Handler());
376 }
377
378 @Override
379 public void onChange(boolean selfChange) {
380 updateShowWebSuggestions();
381 }
382 }
383
384 private ShowWebSuggestionsSettingChangeObserver
385 mShowWebSuggestionsSettingChangeObserver;
386
387 // If non-null, then the system is set to show web suggestions,
388 // and this is the SearchableInfo to use to get them.
389 private SearchableInfo mSearchableInfo;
390
391 /**
392 * Check the system settings to see whether web suggestions are
393 * allowed. If so, store the SearchableInfo to grab suggestions
394 * while the user is typing.
395 */
396 private void updateShowWebSuggestions() {
397 mSearchableInfo = null;
398 Context context = getContext();
399 if (Settings.System.getInt(context.getContentResolver(),
400 Settings.System.SHOW_WEB_SUGGESTIONS,
401 1 /* default on */) == 1) {
402 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
403 intent.addCategory(Intent.CATEGORY_DEFAULT);
404 ResolveInfo info = context.getPackageManager().resolveActivity(
405 intent, PackageManager.MATCH_DEFAULT_ONLY);
406 if (info != null) {
407 ComponentName googleSearchComponent =
408 new ComponentName(info.activityInfo.packageName,
409 info.activityInfo.name);
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100410 mSearchableInfo = mSearchManager.getSearchableInfo(
Leon Scroggins62b71f72009-06-12 17:51:22 -0400411 googleSearchComponent, false);
412 }
413 }
414 }
415
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700416 private void fixPicasaBookmark() {
417 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
418 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
419 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
420 try {
421 if (!cursor.moveToFirst()) {
422 // set "created" so that it will be on the top of the list
423 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
424 "date, created, bookmark)" + " VALUES('" +
425 getContext().getString(R.string.picasa) + "', '"
426 + PICASA_URL + "', 0, 0, " + new Date().getTime()
427 + ", 1);");
428 }
429 } finally {
430 if (cursor != null) {
431 cursor.close();
432 }
433 }
434 }
435
The Android Open Source Project0c908882009-03-03 19:32:16 -0800436 /*
437 * Subclass AbstractCursor so we can combine multiple Cursors and add
438 * "Google Search".
439 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100440 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
The Android Open Source Project0c908882009-03-03 19:32:16 -0800441 * "Google Search";
Satish Sampath565505b2009-05-29 15:37:27 +0100442 * 2. If bookmark/history entries are less than
The Android Open Source Project0c908882009-03-03 19:32:16 -0800443 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
444 */
445 private class MySuggestionCursor extends AbstractCursor {
446 private Cursor mHistoryCursor;
447 private Cursor mSuggestCursor;
448 private int mHistoryCount;
449 private int mSuggestionCount;
Grace Klobad3992d42010-01-28 11:44:38 -0800450 private boolean mIncludeWebSearch;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800451 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100452 private int mSuggestText1Id;
453 private int mSuggestText2Id;
454 private int mSuggestQueryId;
Bjorn Bringert04851702009-09-22 10:36:01 +0100455 private int mSuggestIntentExtraDataId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800456
457 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
458 mHistoryCursor = hc;
459 mSuggestCursor = sc;
460 mHistoryCount = hc.getCount();
461 mSuggestionCount = sc != null ? sc.getCount() : 0;
462 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
463 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
464 }
465 mString = string;
Grace Klobad3992d42010-01-28 11:44:38 -0800466 mIncludeWebSearch = string.length() > 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100467
468 // Some web suggest providers only give suggestions and have no description string for
469 // items. The order of the result columns may be different as well. So retrieve the
470 // column indices for the fields we need now and check before using below.
471 if (mSuggestCursor == null) {
472 mSuggestText1Id = -1;
473 mSuggestText2Id = -1;
474 mSuggestQueryId = -1;
Bjorn Bringert04851702009-09-22 10:36:01 +0100475 mSuggestIntentExtraDataId = -1;
Satish Sampath565505b2009-05-29 15:37:27 +0100476 } else {
477 mSuggestText1Id = mSuggestCursor.getColumnIndex(
478 SearchManager.SUGGEST_COLUMN_TEXT_1);
479 mSuggestText2Id = mSuggestCursor.getColumnIndex(
480 SearchManager.SUGGEST_COLUMN_TEXT_2);
481 mSuggestQueryId = mSuggestCursor.getColumnIndex(
482 SearchManager.SUGGEST_COLUMN_QUERY);
Bjorn Bringert04851702009-09-22 10:36:01 +0100483 mSuggestIntentExtraDataId = mSuggestCursor.getColumnIndex(
484 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA);
Satish Sampath565505b2009-05-29 15:37:27 +0100485 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800486 }
487
488 @Override
489 public boolean onMove(int oldPosition, int newPosition) {
490 if (mHistoryCursor == null) {
491 return false;
492 }
Grace Klobad3992d42010-01-28 11:44:38 -0800493 if (mIncludeWebSearch) {
494 if (newPosition == 0) {
495 return true;
496 } else {
497 newPosition--;
498 }
499 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800500 if (mHistoryCount > newPosition) {
501 mHistoryCursor.moveToPosition(newPosition);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800502 } else {
Grace Klobad3992d42010-01-28 11:44:38 -0800503 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800504 }
505 return true;
506 }
507
508 @Override
509 public int getCount() {
Grace Klobad3992d42010-01-28 11:44:38 -0800510 if (mIncludeWebSearch) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800511 return mHistoryCount + mSuggestionCount + 1;
512 } else {
513 return mHistoryCount + mSuggestionCount;
514 }
515 }
516
517 @Override
518 public String[] getColumnNames() {
519 return COLUMNS;
520 }
Satish Sampath565505b2009-05-29 15:37:27 +0100521
The Android Open Source Project0c908882009-03-03 19:32:16 -0800522 @Override
523 public String getString(int columnIndex) {
524 if ((mPos != -1 && mHistoryCursor != null)) {
Grace Klobad3992d42010-01-28 11:44:38 -0800525 int position = mIncludeWebSearch ? mPos - 1 : mPos;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800526 switch(columnIndex) {
527 case SUGGEST_COLUMN_INTENT_ACTION_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800528 if (position >= 0 && position < mHistoryCount) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800529 return Intent.ACTION_VIEW;
530 } else {
531 return Intent.ACTION_SEARCH;
532 }
533
534 case SUGGEST_COLUMN_INTENT_DATA_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800535 if (position >= 0 && position < mHistoryCount) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800536 return mHistoryCursor.getString(1);
537 } else {
538 return null;
539 }
540
541 case SUGGEST_COLUMN_TEXT_1_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800542 if (position < 0) {
543 return mString;
544 } else if (mHistoryCount > position) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700545 return getHistoryTitle();
Grace Klobad3992d42010-01-28 11:44:38 -0800546 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100547 if (mSuggestText1Id == -1) return null;
548 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800549 }
550
551 case SUGGEST_COLUMN_TEXT_2_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800552 if (position < 0) {
553 return getContext().getString(R.string.search_the_web);
554 } else if (mHistoryCount > position) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700555 return getHistorySubtitle();
Grace Klobad3992d42010-01-28 11:44:38 -0800556 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100557 if (mSuggestText2Id == -1) return null;
558 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800559 }
560
561 case SUGGEST_COLUMN_ICON_1_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800562 if (position >= 0 && position < mHistoryCount) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800563 if (mHistoryCursor.getInt(3) == 1) {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400564 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800565 R.drawable.ic_search_category_bookmark)
566 .toString();
567 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400568 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800569 R.drawable.ic_search_category_history)
570 .toString();
571 }
572 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400573 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800574 R.drawable.ic_search_category_suggest)
575 .toString();
576 }
577
578 case SUGGEST_COLUMN_ICON_2_ID:
Leon Scroggins31887fd2009-05-18 16:58:08 -0400579 return "0";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800580
581 case SUGGEST_COLUMN_QUERY_ID:
Grace Klobad3992d42010-01-28 11:44:38 -0800582 if (position < 0) {
583 return mString;
584 } else if (mHistoryCount > position) {
Mike LeBeau2af73052009-06-23 17:36:59 -0700585 // Return the url in the intent query column. This is ignored
586 // within the browser because our searchable is set to
587 // android:searchMode="queryRewriteFromData", but it is used by
588 // global search for query rewriting.
589 return mHistoryCursor.getString(1);
Grace Klobad3992d42010-01-28 11:44:38 -0800590 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100591 if (mSuggestQueryId == -1) return null;
592 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800593 }
Satish Sampath565505b2009-05-29 15:37:27 +0100594
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700595 case SUGGEST_COLUMN_FORMAT:
596 return "html";
Bjorn Bringert04851702009-09-22 10:36:01 +0100597
598 case SUGGEST_COLUMN_INTENT_EXTRA_DATA:
Grace Klobad3992d42010-01-28 11:44:38 -0800599 if (position < 0) {
Bjorn Bringert04851702009-09-22 10:36:01 +0100600 return null;
Grace Klobad3992d42010-01-28 11:44:38 -0800601 } else if (mHistoryCount > position) {
602 return null;
603 } else {
Bjorn Bringert04851702009-09-22 10:36:01 +0100604 if (mSuggestIntentExtraDataId == -1) return null;
605 return mSuggestCursor.getString(mSuggestIntentExtraDataId);
Bjorn Bringert04851702009-09-22 10:36:01 +0100606 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800607 }
608 }
609 return null;
610 }
611
612 @Override
613 public double getDouble(int column) {
614 throw new UnsupportedOperationException();
615 }
616
617 @Override
618 public float getFloat(int column) {
619 throw new UnsupportedOperationException();
620 }
621
622 @Override
623 public int getInt(int column) {
624 throw new UnsupportedOperationException();
625 }
626
627 @Override
628 public long getLong(int column) {
629 if ((mPos != -1) && column == 0) {
630 return mPos; // use row# as the _Id
631 }
632 throw new UnsupportedOperationException();
633 }
634
635 @Override
636 public short getShort(int column) {
637 throw new UnsupportedOperationException();
638 }
639
640 @Override
641 public boolean isNull(int column) {
642 throw new UnsupportedOperationException();
643 }
644
645 // TODO Temporary change, finalize after jq's changes go in
646 public void deactivate() {
647 if (mHistoryCursor != null) {
648 mHistoryCursor.deactivate();
649 }
650 if (mSuggestCursor != null) {
651 mSuggestCursor.deactivate();
652 }
653 super.deactivate();
654 }
655
656 public boolean requery() {
657 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
658 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
659 }
660
661 // TODO Temporary change, finalize after jq's changes go in
662 public void close() {
663 super.close();
664 if (mHistoryCursor != null) {
665 mHistoryCursor.close();
666 mHistoryCursor = null;
667 }
668 if (mSuggestCursor != null) {
669 mSuggestCursor.close();
670 mSuggestCursor = null;
671 }
672 }
Satish Sampath565505b2009-05-29 15:37:27 +0100673
Mike LeBeau21beb132009-05-13 14:57:50 -0700674 /**
675 * Provides the title (text line 1) for a browser suggestion, which should be the
676 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100677 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700678 * @return the title string to use
679 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700680 private String getHistoryTitle() {
681 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700682 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700683 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700684 }
685 return title;
686 }
Satish Sampath565505b2009-05-29 15:37:27 +0100687
Mike LeBeau21beb132009-05-13 14:57:50 -0700688 /**
689 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
690 * webpage url. If the webpage title is empty, then the url should go in the title
691 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100692 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700693 * @return the subtitle string to use, or null if none
694 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700695 private String getHistorySubtitle() {
696 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700697 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
698 return null;
699 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700700 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700701 }
702 }
Satish Sampath565505b2009-05-29 15:37:27 +0100703
Mike LeBeau21beb132009-05-13 14:57:50 -0700704 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700705 * Strips "http://" from the beginning of a url and "/" from the end,
706 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700707 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700708 private String beautifyUrl(String url) {
Satish Sampath60d24e22009-07-09 16:46:08 +0100709 if (mSearchUrlColorId == null) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700710 // Get the color used for this purpose from the current theme.
711 TypedValue colorValue = new TypedValue();
712 getContext().getTheme().resolveAttribute(
713 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
Satish Sampath60d24e22009-07-09 16:46:08 +0100714 mSearchUrlColorId = Integer.toString(colorValue.resourceId);
Mike LeBeau21beb132009-05-13 14:57:50 -0700715 }
Satish Sampath565505b2009-05-29 15:37:27 +0100716
Satish Sampath60d24e22009-07-09 16:46:08 +0100717 return "<font color=\"@" + mSearchUrlColorId + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700718 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800719 }
720
721 @Override
722 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100723 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800724 throws IllegalStateException {
725 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
726
727 int match = URI_MATCHER.match(url);
728 if (match == -1) {
729 throw new IllegalArgumentException("Unknown URL");
730 }
731
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100732 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800733 String suggestSelection;
734 String [] myArgs;
735 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
736 suggestSelection = null;
737 myArgs = null;
738 } else {
739 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700740 if (selectionArgs[0].startsWith("http")
741 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800742 myArgs = new String[1];
743 myArgs[0] = like;
744 suggestSelection = selection;
745 } else {
746 SUGGEST_ARGS[0] = "http://" + like;
747 SUGGEST_ARGS[1] = "http://www." + like;
748 SUGGEST_ARGS[2] = "https://" + like;
749 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400750 // To match against titles.
751 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800752 myArgs = SUGGEST_ARGS;
753 suggestSelection = SUGGEST_SELECTION;
754 }
755 }
756
757 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
758 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Leon Scroggins31887fd2009-05-18 16:58:08 -0400759 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800760
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100761 if (match == URI_MATCH_BOOKMARKS_SUGGEST
Dan Egnor5ee906c2009-11-18 12:11:49 -0800762 || Patterns.WEB_URL.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800763 return new MySuggestionCursor(c, null, "");
764 } else {
765 // get Google suggest if there is still space in the list
766 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400767 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800768 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100769 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400770 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800771 }
772 return new MySuggestionCursor(c, null, selectionArgs[0]);
773 }
774 }
775
776 String[] projection = null;
777 if (projectionIn != null && projectionIn.length > 0) {
778 projection = new String[projectionIn.length + 1];
779 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
780 projection[projectionIn.length] = "_id AS _id";
781 }
782
783 StringBuilder whereClause = new StringBuilder(256);
784 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
785 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
786 .append(")");
787 }
788
789 // Tack on the user's selection, if present
790 if (selection != null && selection.length() > 0) {
791 if (whereClause.length() > 0) {
792 whereClause.append(" AND ");
793 }
794
795 whereClause.append('(');
796 whereClause.append(selection);
797 whereClause.append(')');
798 }
799 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
800 whereClause.toString(), selectionArgs, null, null, sortOrder,
801 null);
802 c.setNotificationUri(getContext().getContentResolver(), url);
803 return c;
804 }
805
806 @Override
807 public String getType(Uri url) {
808 int match = URI_MATCHER.match(url);
809 switch (match) {
810 case URI_MATCH_BOOKMARKS:
811 return "vnd.android.cursor.dir/bookmark";
812
813 case URI_MATCH_BOOKMARKS_ID:
814 return "vnd.android.cursor.item/bookmark";
815
816 case URI_MATCH_SEARCHES:
817 return "vnd.android.cursor.dir/searches";
818
819 case URI_MATCH_SEARCHES_ID:
820 return "vnd.android.cursor.item/searches";
821
822 case URI_MATCH_SUGGEST:
823 return SearchManager.SUGGEST_MIME_TYPE;
824
825 default:
826 throw new IllegalArgumentException("Unknown URL");
827 }
828 }
829
830 @Override
831 public Uri insert(Uri url, ContentValues initialValues) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700832 boolean isBookmarkTable = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800833 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
834
835 int match = URI_MATCHER.match(url);
836 Uri uri = null;
837 switch (match) {
838 case URI_MATCH_BOOKMARKS: {
839 // Insert into the bookmarks table
840 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
841 initialValues);
842 if (rowID > 0) {
843 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
844 rowID);
845 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700846 isBookmarkTable = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800847 break;
848 }
849
850 case URI_MATCH_SEARCHES: {
851 // Insert into the searches table
852 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
853 initialValues);
854 if (rowID > 0) {
855 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
856 rowID);
857 }
858 break;
859 }
860
861 default:
862 throw new IllegalArgumentException("Unknown URL");
863 }
864
865 if (uri == null) {
866 throw new IllegalArgumentException("Unknown URL");
867 }
868 getContext().getContentResolver().notifyChange(uri, null);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700869
Christopher Tatef0c36f72009-07-28 15:24:05 -0700870 // Back up the new bookmark set if we just inserted one.
871 // A row created when bookmarks are added from scratch will have
872 // bookmark=1 in the initial value set.
873 if (isBookmarkTable
874 && initialValues.containsKey(BookmarkColumns.BOOKMARK)
875 && initialValues.getAsInteger(BookmarkColumns.BOOKMARK) != 0) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700876 mBackupManager.dataChanged();
877 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800878 return uri;
879 }
880
881 @Override
882 public int delete(Uri url, String where, String[] whereArgs) {
883 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
884
885 int match = URI_MATCHER.match(url);
886 if (match == -1 || match == URI_MATCH_SUGGEST) {
887 throw new IllegalArgumentException("Unknown URL");
888 }
889
Christopher Tatef0c36f72009-07-28 15:24:05 -0700890 // need to know whether it's the bookmarks table for a couple of reasons
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700891 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
Christopher Tatef0c36f72009-07-28 15:24:05 -0700892 String id = null;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700893
894 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800895 StringBuilder sb = new StringBuilder();
896 if (where != null && where.length() > 0) {
897 sb.append("( ");
898 sb.append(where);
899 sb.append(" ) AND ");
900 }
Christopher Tatef0c36f72009-07-28 15:24:05 -0700901 id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800902 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -0700903 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800904 where = sb.toString();
905 }
906
Christopher Tatef0c36f72009-07-28 15:24:05 -0700907 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700908
Christopher Tatef0c36f72009-07-28 15:24:05 -0700909 // we'lll need to back up the bookmark set if we are about to delete one
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700910 if (isBookmarkTable) {
Christopher Tatef0c36f72009-07-28 15:24:05 -0700911 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
912 new String[] { BookmarkColumns.BOOKMARK },
913 "_id = " + id, null, null);
914 if (cursor.moveToNext()) {
915 if (cursor.getInt(0) != 0) {
916 // yep, this record is a bookmark
917 mBackupManager.dataChanged();
918 }
919 }
920 cursor.close();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700921 }
Christopher Tatef0c36f72009-07-28 15:24:05 -0700922
923 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
924 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800925 return count;
926 }
927
928 @Override
929 public int update(Uri url, ContentValues values, String where,
930 String[] whereArgs) {
931 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
932
933 int match = URI_MATCHER.match(url);
934 if (match == -1 || match == URI_MATCH_SUGGEST) {
935 throw new IllegalArgumentException("Unknown URL");
936 }
937
Christopher Tatef0c36f72009-07-28 15:24:05 -0700938 String id = null;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700939 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
Christopher Tatef0c36f72009-07-28 15:24:05 -0700940 boolean changingBookmarks = false;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700941
942 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800943 StringBuilder sb = new StringBuilder();
944 if (where != null && where.length() > 0) {
945 sb.append("( ");
946 sb.append(where);
947 sb.append(" ) AND ");
948 }
Christopher Tatef0c36f72009-07-28 15:24:05 -0700949 id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800950 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -0700951 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800952 where = sb.toString();
953 }
954
Christopher Tatef0c36f72009-07-28 15:24:05 -0700955 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700956
Christopher Tatef0c36f72009-07-28 15:24:05 -0700957 // Not all bookmark-table updates should be backed up. Look to see
958 // whether we changed the title, url, or "is a bookmark" state, and
959 // request a backup if so.
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700960 if (isBookmarkTable) {
Christopher Tatef0c36f72009-07-28 15:24:05 -0700961 // Alterations to the bookmark field inherently change the bookmark
962 // set, so we don't need to query the record; we know a priori that
963 // we will need to back up this change.
964 if (values.containsKey(BookmarkColumns.BOOKMARK)) {
965 changingBookmarks = true;
966 }
967 // changing the title or URL of a bookmark record requires a backup,
968 // but we don't know wether such an update is on a bookmark without
969 // querying the record
970 if (!changingBookmarks &&
971 (values.containsKey(BookmarkColumns.TITLE)
972 || values.containsKey(BookmarkColumns.URL))) {
973 // when isBookmarkTable is true, the 'id' var was assigned above
974 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
975 new String[] { BookmarkColumns.BOOKMARK },
976 "_id = " + id, null, null);
977 if (cursor.moveToNext()) {
978 changingBookmarks = (cursor.getInt(0) != 0);
979 }
980 cursor.close();
981 }
982
983 // if this *is* a bookmark row we're altering, we need to back it up.
984 if (changingBookmarks) {
985 mBackupManager.dataChanged();
986 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700987 }
Christopher Tatef0c36f72009-07-28 15:24:05 -0700988
989 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
990 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800991 return ret;
992 }
Satish Sampath565505b2009-05-29 15:37:27 +0100993
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700994 /**
995 * Strips the provided url of preceding "http://" and any trailing "/". Does not
996 * strip "https://". If the provided string cannot be stripped, the original string
997 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +0100998 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700999 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +01001000 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001001 * @param url a url to strip, like "http://www.google.com/"
1002 * @return a stripped url like "www.google.com", or the original string if it could
1003 * not be stripped
1004 */
1005 private static String stripUrl(String url) {
1006 if (url == null) return null;
1007 Matcher m = STRIP_URL_PATTERN.matcher(url);
1008 if (m.matches() && m.groupCount() == 3) {
1009 return m.group(2);
1010 } else {
1011 return url;
1012 }
1013 }
1014
The Android Open Source Project0c908882009-03-03 19:32:16 -08001015}