blob: 618afc1827f6d204efa4589ce0f795cd05e31495 [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;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070020import android.backup.BackupManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.content.ComponentName;
22import android.content.ContentProvider;
Christopher Tatef0c36f72009-07-28 15:24:05 -070023import android.content.ContentResolver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import android.content.ContentUris;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.Intent;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070028import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.content.UriMatcher;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070030import android.content.SharedPreferences.Editor;
Satish Sampath565505b2009-05-29 15:37:27 +010031import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.database.AbstractCursor;
Leon Scroggins62b71f72009-06-12 17:51:22 -040034import android.database.ContentObserver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.database.sqlite.SQLiteDatabase;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010037import android.database.sqlite.SQLiteOpenHelper;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.net.Uri;
Andrei Popescu1b20b9d2009-09-21 18:49:42 +010039import android.os.AsyncTask;
Leon Scroggins62b71f72009-06-12 17:51:22 -040040import android.os.Handler;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070041import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042import android.provider.Browser;
Leon Scroggins62b71f72009-06-12 17:51:22 -040043import android.provider.Settings;
Christopher Tatef0c36f72009-07-28 15:24:05 -070044import android.provider.Browser.BookmarkColumns;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.server.search.SearchableInfo;
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;
450 private boolean mBeyondCursor;
451 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;
466 mBeyondCursor = false;
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 }
493 if (mHistoryCount > newPosition) {
494 mHistoryCursor.moveToPosition(newPosition);
495 mBeyondCursor = false;
496 } else if (mHistoryCount + mSuggestionCount > newPosition) {
497 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
498 mBeyondCursor = false;
499 } else {
500 mBeyondCursor = true;
501 }
502 return true;
503 }
504
505 @Override
506 public int getCount() {
507 if (mString.length() > 0) {
508 return mHistoryCount + mSuggestionCount + 1;
509 } else {
510 return mHistoryCount + mSuggestionCount;
511 }
512 }
513
514 @Override
515 public String[] getColumnNames() {
516 return COLUMNS;
517 }
Satish Sampath565505b2009-05-29 15:37:27 +0100518
The Android Open Source Project0c908882009-03-03 19:32:16 -0800519 @Override
520 public String getString(int columnIndex) {
521 if ((mPos != -1 && mHistoryCursor != null)) {
522 switch(columnIndex) {
523 case SUGGEST_COLUMN_INTENT_ACTION_ID:
524 if (mHistoryCount > mPos) {
525 return Intent.ACTION_VIEW;
526 } else {
527 return Intent.ACTION_SEARCH;
528 }
529
530 case SUGGEST_COLUMN_INTENT_DATA_ID:
531 if (mHistoryCount > mPos) {
532 return mHistoryCursor.getString(1);
533 } else {
534 return null;
535 }
536
537 case SUGGEST_COLUMN_TEXT_1_ID:
538 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700539 return getHistoryTitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800540 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100541 if (mSuggestText1Id == -1) return null;
542 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800543 } else {
544 return mString;
545 }
546
547 case SUGGEST_COLUMN_TEXT_2_ID:
548 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700549 return getHistorySubtitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800550 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100551 if (mSuggestText2Id == -1) return null;
552 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800553 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700554 return getContext().getString(R.string.search_the_web);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800555 }
556
557 case SUGGEST_COLUMN_ICON_1_ID:
558 if (mHistoryCount > mPos) {
559 if (mHistoryCursor.getInt(3) == 1) {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400560 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800561 R.drawable.ic_search_category_bookmark)
562 .toString();
563 } else {
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_history)
566 .toString();
567 }
568 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400569 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800570 R.drawable.ic_search_category_suggest)
571 .toString();
572 }
573
574 case SUGGEST_COLUMN_ICON_2_ID:
Leon Scroggins31887fd2009-05-18 16:58:08 -0400575 return "0";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800576
577 case SUGGEST_COLUMN_QUERY_ID:
578 if (mHistoryCount > mPos) {
Mike LeBeau2af73052009-06-23 17:36:59 -0700579 // Return the url in the intent query column. This is ignored
580 // within the browser because our searchable is set to
581 // android:searchMode="queryRewriteFromData", but it is used by
582 // global search for query rewriting.
583 return mHistoryCursor.getString(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800584 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100585 if (mSuggestQueryId == -1) return null;
586 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800587 } else {
588 return mString;
589 }
Satish Sampath565505b2009-05-29 15:37:27 +0100590
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700591 case SUGGEST_COLUMN_FORMAT:
592 return "html";
Bjorn Bringert04851702009-09-22 10:36:01 +0100593
594 case SUGGEST_COLUMN_INTENT_EXTRA_DATA:
595 if (mHistoryCount > mPos) {
596 return null;
597 } else if (!mBeyondCursor) {
598 if (mSuggestIntentExtraDataId == -1) return null;
599 return mSuggestCursor.getString(mSuggestIntentExtraDataId);
600 } else {
601 return null;
602 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800603 }
604 }
605 return null;
606 }
607
608 @Override
609 public double getDouble(int column) {
610 throw new UnsupportedOperationException();
611 }
612
613 @Override
614 public float getFloat(int column) {
615 throw new UnsupportedOperationException();
616 }
617
618 @Override
619 public int getInt(int column) {
620 throw new UnsupportedOperationException();
621 }
622
623 @Override
624 public long getLong(int column) {
625 if ((mPos != -1) && column == 0) {
626 return mPos; // use row# as the _Id
627 }
628 throw new UnsupportedOperationException();
629 }
630
631 @Override
632 public short getShort(int column) {
633 throw new UnsupportedOperationException();
634 }
635
636 @Override
637 public boolean isNull(int column) {
638 throw new UnsupportedOperationException();
639 }
640
641 // TODO Temporary change, finalize after jq's changes go in
642 public void deactivate() {
643 if (mHistoryCursor != null) {
644 mHistoryCursor.deactivate();
645 }
646 if (mSuggestCursor != null) {
647 mSuggestCursor.deactivate();
648 }
649 super.deactivate();
650 }
651
652 public boolean requery() {
653 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
654 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
655 }
656
657 // TODO Temporary change, finalize after jq's changes go in
658 public void close() {
659 super.close();
660 if (mHistoryCursor != null) {
661 mHistoryCursor.close();
662 mHistoryCursor = null;
663 }
664 if (mSuggestCursor != null) {
665 mSuggestCursor.close();
666 mSuggestCursor = null;
667 }
668 }
Satish Sampath565505b2009-05-29 15:37:27 +0100669
Mike LeBeau21beb132009-05-13 14:57:50 -0700670 /**
671 * Provides the title (text line 1) for a browser suggestion, which should be the
672 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100673 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700674 * @return the title string to use
675 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700676 private String getHistoryTitle() {
677 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700678 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700679 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700680 }
681 return title;
682 }
Satish Sampath565505b2009-05-29 15:37:27 +0100683
Mike LeBeau21beb132009-05-13 14:57:50 -0700684 /**
685 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
686 * webpage url. If the webpage title is empty, then the url should go in the title
687 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100688 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700689 * @return the subtitle string to use, or null if none
690 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700691 private String getHistorySubtitle() {
692 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700693 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
694 return null;
695 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700696 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700697 }
698 }
Satish Sampath565505b2009-05-29 15:37:27 +0100699
Mike LeBeau21beb132009-05-13 14:57:50 -0700700 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700701 * Strips "http://" from the beginning of a url and "/" from the end,
702 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700703 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700704 private String beautifyUrl(String url) {
Satish Sampath60d24e22009-07-09 16:46:08 +0100705 if (mSearchUrlColorId == null) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700706 // Get the color used for this purpose from the current theme.
707 TypedValue colorValue = new TypedValue();
708 getContext().getTheme().resolveAttribute(
709 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
Satish Sampath60d24e22009-07-09 16:46:08 +0100710 mSearchUrlColorId = Integer.toString(colorValue.resourceId);
Mike LeBeau21beb132009-05-13 14:57:50 -0700711 }
Satish Sampath565505b2009-05-29 15:37:27 +0100712
Satish Sampath60d24e22009-07-09 16:46:08 +0100713 return "<font color=\"@" + mSearchUrlColorId + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700714 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800715 }
716
717 @Override
718 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100719 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800720 throws IllegalStateException {
721 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
722
723 int match = URI_MATCHER.match(url);
724 if (match == -1) {
725 throw new IllegalArgumentException("Unknown URL");
726 }
727
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100728 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800729 String suggestSelection;
730 String [] myArgs;
731 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
732 suggestSelection = null;
733 myArgs = null;
734 } else {
735 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700736 if (selectionArgs[0].startsWith("http")
737 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800738 myArgs = new String[1];
739 myArgs[0] = like;
740 suggestSelection = selection;
741 } else {
742 SUGGEST_ARGS[0] = "http://" + like;
743 SUGGEST_ARGS[1] = "http://www." + like;
744 SUGGEST_ARGS[2] = "https://" + like;
745 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400746 // To match against titles.
747 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800748 myArgs = SUGGEST_ARGS;
749 suggestSelection = SUGGEST_SELECTION;
750 }
751 }
752
753 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
754 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Leon Scroggins31887fd2009-05-18 16:58:08 -0400755 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800756
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100757 if (match == URI_MATCH_BOOKMARKS_SUGGEST
Dan Egnor5ee906c2009-11-18 12:11:49 -0800758 || Patterns.WEB_URL.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800759 return new MySuggestionCursor(c, null, "");
760 } else {
761 // get Google suggest if there is still space in the list
762 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400763 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800764 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100765 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400766 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800767 }
768 return new MySuggestionCursor(c, null, selectionArgs[0]);
769 }
770 }
771
772 String[] projection = null;
773 if (projectionIn != null && projectionIn.length > 0) {
774 projection = new String[projectionIn.length + 1];
775 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
776 projection[projectionIn.length] = "_id AS _id";
777 }
778
779 StringBuilder whereClause = new StringBuilder(256);
780 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
781 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
782 .append(")");
783 }
784
785 // Tack on the user's selection, if present
786 if (selection != null && selection.length() > 0) {
787 if (whereClause.length() > 0) {
788 whereClause.append(" AND ");
789 }
790
791 whereClause.append('(');
792 whereClause.append(selection);
793 whereClause.append(')');
794 }
795 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
796 whereClause.toString(), selectionArgs, null, null, sortOrder,
797 null);
798 c.setNotificationUri(getContext().getContentResolver(), url);
799 return c;
800 }
801
802 @Override
803 public String getType(Uri url) {
804 int match = URI_MATCHER.match(url);
805 switch (match) {
806 case URI_MATCH_BOOKMARKS:
807 return "vnd.android.cursor.dir/bookmark";
808
809 case URI_MATCH_BOOKMARKS_ID:
810 return "vnd.android.cursor.item/bookmark";
811
812 case URI_MATCH_SEARCHES:
813 return "vnd.android.cursor.dir/searches";
814
815 case URI_MATCH_SEARCHES_ID:
816 return "vnd.android.cursor.item/searches";
817
818 case URI_MATCH_SUGGEST:
819 return SearchManager.SUGGEST_MIME_TYPE;
820
821 default:
822 throw new IllegalArgumentException("Unknown URL");
823 }
824 }
825
826 @Override
827 public Uri insert(Uri url, ContentValues initialValues) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700828 boolean isBookmarkTable = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800829 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
830
831 int match = URI_MATCHER.match(url);
832 Uri uri = null;
833 switch (match) {
834 case URI_MATCH_BOOKMARKS: {
835 // Insert into the bookmarks table
836 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
837 initialValues);
838 if (rowID > 0) {
839 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
840 rowID);
841 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700842 isBookmarkTable = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800843 break;
844 }
845
846 case URI_MATCH_SEARCHES: {
847 // Insert into the searches table
848 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
849 initialValues);
850 if (rowID > 0) {
851 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
852 rowID);
853 }
854 break;
855 }
856
857 default:
858 throw new IllegalArgumentException("Unknown URL");
859 }
860
861 if (uri == null) {
862 throw new IllegalArgumentException("Unknown URL");
863 }
864 getContext().getContentResolver().notifyChange(uri, null);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700865
Christopher Tatef0c36f72009-07-28 15:24:05 -0700866 // Back up the new bookmark set if we just inserted one.
867 // A row created when bookmarks are added from scratch will have
868 // bookmark=1 in the initial value set.
869 if (isBookmarkTable
870 && initialValues.containsKey(BookmarkColumns.BOOKMARK)
871 && initialValues.getAsInteger(BookmarkColumns.BOOKMARK) != 0) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700872 mBackupManager.dataChanged();
873 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800874 return uri;
875 }
876
877 @Override
878 public int delete(Uri url, String where, String[] whereArgs) {
879 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
880
881 int match = URI_MATCHER.match(url);
882 if (match == -1 || match == URI_MATCH_SUGGEST) {
883 throw new IllegalArgumentException("Unknown URL");
884 }
885
Christopher Tatef0c36f72009-07-28 15:24:05 -0700886 // need to know whether it's the bookmarks table for a couple of reasons
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700887 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
Christopher Tatef0c36f72009-07-28 15:24:05 -0700888 String id = null;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700889
890 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800891 StringBuilder sb = new StringBuilder();
892 if (where != null && where.length() > 0) {
893 sb.append("( ");
894 sb.append(where);
895 sb.append(" ) AND ");
896 }
Christopher Tatef0c36f72009-07-28 15:24:05 -0700897 id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800898 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -0700899 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800900 where = sb.toString();
901 }
902
Christopher Tatef0c36f72009-07-28 15:24:05 -0700903 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700904
Christopher Tatef0c36f72009-07-28 15:24:05 -0700905 // we'lll need to back up the bookmark set if we are about to delete one
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700906 if (isBookmarkTable) {
Christopher Tatef0c36f72009-07-28 15:24:05 -0700907 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
908 new String[] { BookmarkColumns.BOOKMARK },
909 "_id = " + id, null, null);
910 if (cursor.moveToNext()) {
911 if (cursor.getInt(0) != 0) {
912 // yep, this record is a bookmark
913 mBackupManager.dataChanged();
914 }
915 }
916 cursor.close();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700917 }
Christopher Tatef0c36f72009-07-28 15:24:05 -0700918
919 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
920 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800921 return count;
922 }
923
924 @Override
925 public int update(Uri url, ContentValues values, String where,
926 String[] whereArgs) {
927 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
928
929 int match = URI_MATCHER.match(url);
930 if (match == -1 || match == URI_MATCH_SUGGEST) {
931 throw new IllegalArgumentException("Unknown URL");
932 }
933
Christopher Tatef0c36f72009-07-28 15:24:05 -0700934 String id = null;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700935 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
Christopher Tatef0c36f72009-07-28 15:24:05 -0700936 boolean changingBookmarks = false;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700937
938 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800939 StringBuilder sb = new StringBuilder();
940 if (where != null && where.length() > 0) {
941 sb.append("( ");
942 sb.append(where);
943 sb.append(" ) AND ");
944 }
Christopher Tatef0c36f72009-07-28 15:24:05 -0700945 id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800946 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -0700947 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800948 where = sb.toString();
949 }
950
Christopher Tatef0c36f72009-07-28 15:24:05 -0700951 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700952
Christopher Tatef0c36f72009-07-28 15:24:05 -0700953 // Not all bookmark-table updates should be backed up. Look to see
954 // whether we changed the title, url, or "is a bookmark" state, and
955 // request a backup if so.
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700956 if (isBookmarkTable) {
Christopher Tatef0c36f72009-07-28 15:24:05 -0700957 // Alterations to the bookmark field inherently change the bookmark
958 // set, so we don't need to query the record; we know a priori that
959 // we will need to back up this change.
960 if (values.containsKey(BookmarkColumns.BOOKMARK)) {
961 changingBookmarks = true;
962 }
963 // changing the title or URL of a bookmark record requires a backup,
964 // but we don't know wether such an update is on a bookmark without
965 // querying the record
966 if (!changingBookmarks &&
967 (values.containsKey(BookmarkColumns.TITLE)
968 || values.containsKey(BookmarkColumns.URL))) {
969 // when isBookmarkTable is true, the 'id' var was assigned above
970 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
971 new String[] { BookmarkColumns.BOOKMARK },
972 "_id = " + id, null, null);
973 if (cursor.moveToNext()) {
974 changingBookmarks = (cursor.getInt(0) != 0);
975 }
976 cursor.close();
977 }
978
979 // if this *is* a bookmark row we're altering, we need to back it up.
980 if (changingBookmarks) {
981 mBackupManager.dataChanged();
982 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700983 }
Christopher Tatef0c36f72009-07-28 15:24:05 -0700984
985 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
986 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800987 return ret;
988 }
Satish Sampath565505b2009-05-29 15:37:27 +0100989
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700990 /**
991 * Strips the provided url of preceding "http://" and any trailing "/". Does not
992 * strip "https://". If the provided string cannot be stripped, the original string
993 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +0100994 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700995 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +0100996 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700997 * @param url a url to strip, like "http://www.google.com/"
998 * @return a stripped url like "www.google.com", or the original string if it could
999 * not be stripped
1000 */
1001 private static String stripUrl(String url) {
1002 if (url == null) return null;
1003 Matcher m = STRIP_URL_PATTERN.matcher(url);
1004 if (m.matches() && m.groupCount() == 3) {
1005 return m.group(2);
1006 } else {
1007 return url;
1008 }
1009 }
1010
The Android Open Source Project0c908882009-03-03 19:32:16 -08001011}