blob: cecc8d255f472523680d3a171b4b2a0e1e644389 [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[] {
76 "_id", "url", "title", "bookmark"
77 };
Satish Sampath565505b2009-05-29 15:37:27 +010078 private static final String SUGGEST_SELECTION =
Leon Scrogginsbd359cc2009-05-26 15:57:35 -040079 "url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?"
80 + " OR title LIKE ?";
81 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"
161 private static final int DATABASE_VERSION = 22;
Satish Sampath565505b2009-05-29 15:37:27 +0100162
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700163 // Regular expression which matches http://, followed by some stuff, followed by
164 // optionally a trailing slash, all matched as separate groups.
165 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
Satish Sampath565505b2009-05-29 15:37:27 +0100166
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100167 private SearchManager mSearchManager;
168
Satish Sampath60d24e22009-07-09 16:46:08 +0100169 // The ID of the ColorStateList to be applied to urls of website suggestions, as derived from
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700170 // the current theme. This is not set until/unless beautifyUrl is called, at which point
171 // this variable caches the color value.
Satish Sampath60d24e22009-07-09 16:46:08 +0100172 private static String mSearchUrlColorId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800173
174 public BrowserProvider() {
175 }
Satish Sampath565505b2009-05-29 15:37:27 +0100176
The Android Open Source Project0c908882009-03-03 19:32:16 -0800177
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700178 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800179 StringBuffer sb = new StringBuffer();
180 int lastCharLoc = 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100181
Ramanan Rajeswaran378e5722009-07-01 08:46:58 -0700182 final String client_id = Partner.getString(context.getContentResolver(),
183 Partner.CLIENT_ID, "android-google");
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700184
The Android Open Source Project0c908882009-03-03 19:32:16 -0800185 for (int i = 0; i < srcString.length(); ++i) {
186 char c = srcString.charAt(i);
187 if (c == '{') {
188 sb.append(srcString.subSequence(lastCharLoc, i));
189 lastCharLoc = i;
190 inner:
191 for (int j = i; j < srcString.length(); ++j) {
192 char k = srcString.charAt(j);
193 if (k == '}') {
194 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700195 if (propertyKeyValue.equals("CLIENT_ID")) {
196 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800197 } else {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700198 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800199 }
200 lastCharLoc = j + 1;
201 i = j;
202 break inner;
203 }
204 }
205 }
206 }
207 if (srcString.length() - lastCharLoc > 0) {
208 // Put on the tail, if there is one
209 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
210 }
211 return sb;
212 }
213
214 private static class DatabaseHelper extends SQLiteOpenHelper {
215 private Context mContext;
216
217 public DatabaseHelper(Context context) {
218 super(context, sDatabaseName, null, DATABASE_VERSION);
219 mContext = context;
220 }
221
222 @Override
223 public void onCreate(SQLiteDatabase db) {
224 db.execSQL("CREATE TABLE bookmarks (" +
225 "_id INTEGER PRIMARY KEY," +
226 "title TEXT," +
227 "url TEXT," +
228 "visits INTEGER," +
229 "date LONG," +
230 "created LONG," +
231 "description TEXT," +
232 "bookmark INTEGER," +
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400233 "favicon BLOB DEFAULT NULL," +
Patrick Scott3918d442009-08-04 13:22:29 -0400234 "thumbnail BLOB DEFAULT NULL," +
235 "touch_icon BLOB DEFAULT NULL" +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800236 ");");
237
238 final CharSequence[] bookmarks = mContext.getResources()
239 .getTextArray(R.array.bookmarks);
240 int size = bookmarks.length;
241 try {
242 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700243 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800244 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
245 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100246 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800247 "', 0, 0, 0, 1);");
248 }
249 } catch (ArrayIndexOutOfBoundsException e) {
250 }
251
252 db.execSQL("CREATE TABLE searches (" +
253 "_id INTEGER PRIMARY KEY," +
254 "search TEXT," +
255 "date LONG" +
256 ");");
257 }
258
259 @Override
260 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
261 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400262 + newVersion);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800263 if (oldVersion == 18) {
264 db.execSQL("DROP TABLE IF EXISTS labels");
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400265 }
266 if (oldVersion <= 19) {
267 db.execSQL("ALTER TABLE bookmarks ADD COLUMN thumbnail BLOB DEFAULT NULL;");
Patrick Scott3918d442009-08-04 13:22:29 -0400268 }
269 if (oldVersion < 21) {
270 db.execSQL("ALTER TABLE bookmarks ADD COLUMN touch_icon BLOB DEFAULT NULL;");
Grace Kloba6b52a552009-09-03 16:29:56 -0700271 }
272 if (oldVersion < 22) {
273 db.execSQL("DELETE FROM bookmarks WHERE (bookmark = 0 AND url LIKE \"%.google.%client=ms-%\")");
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100274 removeGears();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800275 } else {
276 db.execSQL("DROP TABLE IF EXISTS bookmarks");
277 db.execSQL("DROP TABLE IF EXISTS searches");
278 onCreate(db);
279 }
280 }
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100281
282 private void removeGears() {
283 AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
284 public Void doInBackground(Void... unused) {
285 String browserDataDirString = mContext.getApplicationInfo().dataDir;
286 final String appPluginsDirString = "app_plugins";
287 final String gearsPrefix = "gears";
288 File appPluginsDir = new File(browserDataDirString + File.separator
289 + appPluginsDirString);
290 if (!appPluginsDir.exists()) {
291 return null;
292 }
293 // Delete the Gears plugin files
294 File[] gearsFiles = appPluginsDir.listFiles(new FilenameFilter() {
295 public boolean accept(File dir, String filename) {
296 return filename.startsWith(gearsPrefix);
297 }
298 });
299 for (int i = 0; i < gearsFiles.length; ++i) {
300 if (gearsFiles[i].isDirectory()) {
301 deleteDirectory(gearsFiles[i]);
302 } else {
303 gearsFiles[i].delete();
304 }
305 }
306 // Delete the Gears data files
307 File gearsDataDir = new File(browserDataDirString + File.separator
308 + gearsPrefix);
309 if (!gearsDataDir.exists()) {
310 return null;
311 }
312 deleteDirectory(gearsDataDir);
313 return null;
314 }
315
316 private void deleteDirectory(File currentDir) {
317 File[] files = currentDir.listFiles();
318 for (int i = 0; i < files.length; ++i) {
319 if (files[i].isDirectory()) {
320 deleteDirectory(files[i]);
321 }
322 files[i].delete();
323 }
324 currentDir.delete();
325 }
326 };
327
328 task.execute();
329 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800330 }
331
332 @Override
333 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700334 final Context context = getContext();
335 mOpenHelper = new DatabaseHelper(context);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700336 mBackupManager = new BackupManager(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100337 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700338 // To avoid erasing the bookmark table, we added it explicitly for
339 // version 18 and 19 as in the other cases, we will erase the table.
340 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
341 SharedPreferences p = PreferenceManager
342 .getDefaultSharedPreferences(context);
343 boolean fix = p.getBoolean("fix_picasa", true);
344 if (fix) {
345 fixPicasaBookmark();
346 Editor ed = p.edit();
347 ed.putBoolean("fix_picasa", false);
348 ed.commit();
349 }
350 }
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100351 mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400352 mShowWebSuggestionsSettingChangeObserver
353 = new ShowWebSuggestionsSettingChangeObserver();
354 context.getContentResolver().registerContentObserver(
355 Settings.System.getUriFor(
356 Settings.System.SHOW_WEB_SUGGESTIONS),
357 true, mShowWebSuggestionsSettingChangeObserver);
358 updateShowWebSuggestions();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800359 return true;
360 }
361
Leon Scroggins62b71f72009-06-12 17:51:22 -0400362 /**
363 * This Observer will ensure that if the user changes the system
364 * setting of whether to display web suggestions, we will
365 * change accordingly.
366 */
367 /* package */ class ShowWebSuggestionsSettingChangeObserver
368 extends ContentObserver {
369 public ShowWebSuggestionsSettingChangeObserver() {
370 super(new Handler());
371 }
372
373 @Override
374 public void onChange(boolean selfChange) {
375 updateShowWebSuggestions();
376 }
377 }
378
379 private ShowWebSuggestionsSettingChangeObserver
380 mShowWebSuggestionsSettingChangeObserver;
381
382 // If non-null, then the system is set to show web suggestions,
383 // and this is the SearchableInfo to use to get them.
384 private SearchableInfo mSearchableInfo;
385
386 /**
387 * Check the system settings to see whether web suggestions are
388 * allowed. If so, store the SearchableInfo to grab suggestions
389 * while the user is typing.
390 */
391 private void updateShowWebSuggestions() {
392 mSearchableInfo = null;
393 Context context = getContext();
394 if (Settings.System.getInt(context.getContentResolver(),
395 Settings.System.SHOW_WEB_SUGGESTIONS,
396 1 /* default on */) == 1) {
397 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
398 intent.addCategory(Intent.CATEGORY_DEFAULT);
399 ResolveInfo info = context.getPackageManager().resolveActivity(
400 intent, PackageManager.MATCH_DEFAULT_ONLY);
401 if (info != null) {
402 ComponentName googleSearchComponent =
403 new ComponentName(info.activityInfo.packageName,
404 info.activityInfo.name);
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100405 mSearchableInfo = mSearchManager.getSearchableInfo(
Leon Scroggins62b71f72009-06-12 17:51:22 -0400406 googleSearchComponent, false);
407 }
408 }
409 }
410
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700411 private void fixPicasaBookmark() {
412 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
413 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
414 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
415 try {
416 if (!cursor.moveToFirst()) {
417 // set "created" so that it will be on the top of the list
418 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
419 "date, created, bookmark)" + " VALUES('" +
420 getContext().getString(R.string.picasa) + "', '"
421 + PICASA_URL + "', 0, 0, " + new Date().getTime()
422 + ", 1);");
423 }
424 } finally {
425 if (cursor != null) {
426 cursor.close();
427 }
428 }
429 }
430
The Android Open Source Project0c908882009-03-03 19:32:16 -0800431 /*
432 * Subclass AbstractCursor so we can combine multiple Cursors and add
433 * "Google Search".
434 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100435 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
The Android Open Source Project0c908882009-03-03 19:32:16 -0800436 * "Google Search";
Satish Sampath565505b2009-05-29 15:37:27 +0100437 * 2. If bookmark/history entries are less than
The Android Open Source Project0c908882009-03-03 19:32:16 -0800438 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
439 */
440 private class MySuggestionCursor extends AbstractCursor {
441 private Cursor mHistoryCursor;
442 private Cursor mSuggestCursor;
443 private int mHistoryCount;
444 private int mSuggestionCount;
445 private boolean mBeyondCursor;
446 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100447 private int mSuggestText1Id;
448 private int mSuggestText2Id;
449 private int mSuggestQueryId;
Bjorn Bringert04851702009-09-22 10:36:01 +0100450 private int mSuggestIntentExtraDataId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800451
452 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
453 mHistoryCursor = hc;
454 mSuggestCursor = sc;
455 mHistoryCount = hc.getCount();
456 mSuggestionCount = sc != null ? sc.getCount() : 0;
457 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
458 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
459 }
460 mString = string;
461 mBeyondCursor = false;
Satish Sampath565505b2009-05-29 15:37:27 +0100462
463 // Some web suggest providers only give suggestions and have no description string for
464 // items. The order of the result columns may be different as well. So retrieve the
465 // column indices for the fields we need now and check before using below.
466 if (mSuggestCursor == null) {
467 mSuggestText1Id = -1;
468 mSuggestText2Id = -1;
469 mSuggestQueryId = -1;
Bjorn Bringert04851702009-09-22 10:36:01 +0100470 mSuggestIntentExtraDataId = -1;
Satish Sampath565505b2009-05-29 15:37:27 +0100471 } else {
472 mSuggestText1Id = mSuggestCursor.getColumnIndex(
473 SearchManager.SUGGEST_COLUMN_TEXT_1);
474 mSuggestText2Id = mSuggestCursor.getColumnIndex(
475 SearchManager.SUGGEST_COLUMN_TEXT_2);
476 mSuggestQueryId = mSuggestCursor.getColumnIndex(
477 SearchManager.SUGGEST_COLUMN_QUERY);
Bjorn Bringert04851702009-09-22 10:36:01 +0100478 mSuggestIntentExtraDataId = mSuggestCursor.getColumnIndex(
479 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA);
Satish Sampath565505b2009-05-29 15:37:27 +0100480 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800481 }
482
483 @Override
484 public boolean onMove(int oldPosition, int newPosition) {
485 if (mHistoryCursor == null) {
486 return false;
487 }
488 if (mHistoryCount > newPosition) {
489 mHistoryCursor.moveToPosition(newPosition);
490 mBeyondCursor = false;
491 } else if (mHistoryCount + mSuggestionCount > newPosition) {
492 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
493 mBeyondCursor = false;
494 } else {
495 mBeyondCursor = true;
496 }
497 return true;
498 }
499
500 @Override
501 public int getCount() {
502 if (mString.length() > 0) {
503 return mHistoryCount + mSuggestionCount + 1;
504 } else {
505 return mHistoryCount + mSuggestionCount;
506 }
507 }
508
509 @Override
510 public String[] getColumnNames() {
511 return COLUMNS;
512 }
Satish Sampath565505b2009-05-29 15:37:27 +0100513
The Android Open Source Project0c908882009-03-03 19:32:16 -0800514 @Override
515 public String getString(int columnIndex) {
516 if ((mPos != -1 && mHistoryCursor != null)) {
517 switch(columnIndex) {
518 case SUGGEST_COLUMN_INTENT_ACTION_ID:
519 if (mHistoryCount > mPos) {
520 return Intent.ACTION_VIEW;
521 } else {
522 return Intent.ACTION_SEARCH;
523 }
524
525 case SUGGEST_COLUMN_INTENT_DATA_ID:
526 if (mHistoryCount > mPos) {
527 return mHistoryCursor.getString(1);
528 } else {
529 return null;
530 }
531
532 case SUGGEST_COLUMN_TEXT_1_ID:
533 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700534 return getHistoryTitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800535 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100536 if (mSuggestText1Id == -1) return null;
537 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800538 } else {
539 return mString;
540 }
541
542 case SUGGEST_COLUMN_TEXT_2_ID:
543 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700544 return getHistorySubtitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800545 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100546 if (mSuggestText2Id == -1) return null;
547 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800548 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700549 return getContext().getString(R.string.search_the_web);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800550 }
551
552 case SUGGEST_COLUMN_ICON_1_ID:
553 if (mHistoryCount > mPos) {
554 if (mHistoryCursor.getInt(3) == 1) {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400555 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800556 R.drawable.ic_search_category_bookmark)
557 .toString();
558 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400559 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800560 R.drawable.ic_search_category_history)
561 .toString();
562 }
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_suggest)
566 .toString();
567 }
568
569 case SUGGEST_COLUMN_ICON_2_ID:
Leon Scroggins31887fd2009-05-18 16:58:08 -0400570 return "0";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800571
572 case SUGGEST_COLUMN_QUERY_ID:
573 if (mHistoryCount > mPos) {
Mike LeBeau2af73052009-06-23 17:36:59 -0700574 // Return the url in the intent query column. This is ignored
575 // within the browser because our searchable is set to
576 // android:searchMode="queryRewriteFromData", but it is used by
577 // global search for query rewriting.
578 return mHistoryCursor.getString(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800579 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100580 if (mSuggestQueryId == -1) return null;
581 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800582 } else {
583 return mString;
584 }
Satish Sampath565505b2009-05-29 15:37:27 +0100585
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700586 case SUGGEST_COLUMN_FORMAT:
587 return "html";
Bjorn Bringert04851702009-09-22 10:36:01 +0100588
589 case SUGGEST_COLUMN_INTENT_EXTRA_DATA:
590 if (mHistoryCount > mPos) {
591 return null;
592 } else if (!mBeyondCursor) {
593 if (mSuggestIntentExtraDataId == -1) return null;
594 return mSuggestCursor.getString(mSuggestIntentExtraDataId);
595 } else {
596 return null;
597 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800598 }
599 }
600 return null;
601 }
602
603 @Override
604 public double getDouble(int column) {
605 throw new UnsupportedOperationException();
606 }
607
608 @Override
609 public float getFloat(int column) {
610 throw new UnsupportedOperationException();
611 }
612
613 @Override
614 public int getInt(int column) {
615 throw new UnsupportedOperationException();
616 }
617
618 @Override
619 public long getLong(int column) {
620 if ((mPos != -1) && column == 0) {
621 return mPos; // use row# as the _Id
622 }
623 throw new UnsupportedOperationException();
624 }
625
626 @Override
627 public short getShort(int column) {
628 throw new UnsupportedOperationException();
629 }
630
631 @Override
632 public boolean isNull(int column) {
633 throw new UnsupportedOperationException();
634 }
635
636 // TODO Temporary change, finalize after jq's changes go in
637 public void deactivate() {
638 if (mHistoryCursor != null) {
639 mHistoryCursor.deactivate();
640 }
641 if (mSuggestCursor != null) {
642 mSuggestCursor.deactivate();
643 }
644 super.deactivate();
645 }
646
647 public boolean requery() {
648 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
649 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
650 }
651
652 // TODO Temporary change, finalize after jq's changes go in
653 public void close() {
654 super.close();
655 if (mHistoryCursor != null) {
656 mHistoryCursor.close();
657 mHistoryCursor = null;
658 }
659 if (mSuggestCursor != null) {
660 mSuggestCursor.close();
661 mSuggestCursor = null;
662 }
663 }
Satish Sampath565505b2009-05-29 15:37:27 +0100664
Mike LeBeau21beb132009-05-13 14:57:50 -0700665 /**
666 * Provides the title (text line 1) for a browser suggestion, which should be the
667 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100668 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700669 * @return the title string to use
670 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700671 private String getHistoryTitle() {
672 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700673 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700674 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700675 }
676 return title;
677 }
Satish Sampath565505b2009-05-29 15:37:27 +0100678
Mike LeBeau21beb132009-05-13 14:57:50 -0700679 /**
680 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
681 * webpage url. If the webpage title is empty, then the url should go in the title
682 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100683 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700684 * @return the subtitle string to use, or null if none
685 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700686 private String getHistorySubtitle() {
687 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700688 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
689 return null;
690 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700691 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700692 }
693 }
Satish Sampath565505b2009-05-29 15:37:27 +0100694
Mike LeBeau21beb132009-05-13 14:57:50 -0700695 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700696 * Strips "http://" from the beginning of a url and "/" from the end,
697 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700698 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700699 private String beautifyUrl(String url) {
Satish Sampath60d24e22009-07-09 16:46:08 +0100700 if (mSearchUrlColorId == null) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700701 // Get the color used for this purpose from the current theme.
702 TypedValue colorValue = new TypedValue();
703 getContext().getTheme().resolveAttribute(
704 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
Satish Sampath60d24e22009-07-09 16:46:08 +0100705 mSearchUrlColorId = Integer.toString(colorValue.resourceId);
Mike LeBeau21beb132009-05-13 14:57:50 -0700706 }
Satish Sampath565505b2009-05-29 15:37:27 +0100707
Satish Sampath60d24e22009-07-09 16:46:08 +0100708 return "<font color=\"@" + mSearchUrlColorId + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700709 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800710 }
711
712 @Override
713 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100714 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800715 throws IllegalStateException {
716 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
717
718 int match = URI_MATCHER.match(url);
719 if (match == -1) {
720 throw new IllegalArgumentException("Unknown URL");
721 }
722
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100723 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800724 String suggestSelection;
725 String [] myArgs;
726 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
727 suggestSelection = null;
728 myArgs = null;
729 } else {
730 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700731 if (selectionArgs[0].startsWith("http")
732 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800733 myArgs = new String[1];
734 myArgs[0] = like;
735 suggestSelection = selection;
736 } else {
737 SUGGEST_ARGS[0] = "http://" + like;
738 SUGGEST_ARGS[1] = "http://www." + like;
739 SUGGEST_ARGS[2] = "https://" + like;
740 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400741 // To match against titles.
742 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800743 myArgs = SUGGEST_ARGS;
744 suggestSelection = SUGGEST_SELECTION;
745 }
746 }
747
748 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
749 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Leon Scroggins31887fd2009-05-18 16:58:08 -0400750 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800751
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100752 if (match == URI_MATCH_BOOKMARKS_SUGGEST
Dan Egnor5ee906c2009-11-18 12:11:49 -0800753 || Patterns.WEB_URL.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800754 return new MySuggestionCursor(c, null, "");
755 } else {
756 // get Google suggest if there is still space in the list
757 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400758 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800759 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100760 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400761 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800762 }
763 return new MySuggestionCursor(c, null, selectionArgs[0]);
764 }
765 }
766
767 String[] projection = null;
768 if (projectionIn != null && projectionIn.length > 0) {
769 projection = new String[projectionIn.length + 1];
770 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
771 projection[projectionIn.length] = "_id AS _id";
772 }
773
774 StringBuilder whereClause = new StringBuilder(256);
775 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
776 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
777 .append(")");
778 }
779
780 // Tack on the user's selection, if present
781 if (selection != null && selection.length() > 0) {
782 if (whereClause.length() > 0) {
783 whereClause.append(" AND ");
784 }
785
786 whereClause.append('(');
787 whereClause.append(selection);
788 whereClause.append(')');
789 }
790 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
791 whereClause.toString(), selectionArgs, null, null, sortOrder,
792 null);
793 c.setNotificationUri(getContext().getContentResolver(), url);
794 return c;
795 }
796
797 @Override
798 public String getType(Uri url) {
799 int match = URI_MATCHER.match(url);
800 switch (match) {
801 case URI_MATCH_BOOKMARKS:
802 return "vnd.android.cursor.dir/bookmark";
803
804 case URI_MATCH_BOOKMARKS_ID:
805 return "vnd.android.cursor.item/bookmark";
806
807 case URI_MATCH_SEARCHES:
808 return "vnd.android.cursor.dir/searches";
809
810 case URI_MATCH_SEARCHES_ID:
811 return "vnd.android.cursor.item/searches";
812
813 case URI_MATCH_SUGGEST:
814 return SearchManager.SUGGEST_MIME_TYPE;
815
816 default:
817 throw new IllegalArgumentException("Unknown URL");
818 }
819 }
820
821 @Override
822 public Uri insert(Uri url, ContentValues initialValues) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700823 boolean isBookmarkTable = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800824 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
825
826 int match = URI_MATCHER.match(url);
827 Uri uri = null;
828 switch (match) {
829 case URI_MATCH_BOOKMARKS: {
830 // Insert into the bookmarks table
831 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
832 initialValues);
833 if (rowID > 0) {
834 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
835 rowID);
836 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700837 isBookmarkTable = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800838 break;
839 }
840
841 case URI_MATCH_SEARCHES: {
842 // Insert into the searches table
843 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
844 initialValues);
845 if (rowID > 0) {
846 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
847 rowID);
848 }
849 break;
850 }
851
852 default:
853 throw new IllegalArgumentException("Unknown URL");
854 }
855
856 if (uri == null) {
857 throw new IllegalArgumentException("Unknown URL");
858 }
859 getContext().getContentResolver().notifyChange(uri, null);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700860
Christopher Tatef0c36f72009-07-28 15:24:05 -0700861 // Back up the new bookmark set if we just inserted one.
862 // A row created when bookmarks are added from scratch will have
863 // bookmark=1 in the initial value set.
864 if (isBookmarkTable
865 && initialValues.containsKey(BookmarkColumns.BOOKMARK)
866 && initialValues.getAsInteger(BookmarkColumns.BOOKMARK) != 0) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700867 mBackupManager.dataChanged();
868 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800869 return uri;
870 }
871
872 @Override
873 public int delete(Uri url, String where, String[] whereArgs) {
874 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
875
876 int match = URI_MATCHER.match(url);
877 if (match == -1 || match == URI_MATCH_SUGGEST) {
878 throw new IllegalArgumentException("Unknown URL");
879 }
880
Christopher Tatef0c36f72009-07-28 15:24:05 -0700881 // need to know whether it's the bookmarks table for a couple of reasons
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700882 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
Christopher Tatef0c36f72009-07-28 15:24:05 -0700883 String id = null;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700884
885 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800886 StringBuilder sb = new StringBuilder();
887 if (where != null && where.length() > 0) {
888 sb.append("( ");
889 sb.append(where);
890 sb.append(" ) AND ");
891 }
Christopher Tatef0c36f72009-07-28 15:24:05 -0700892 id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800893 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -0700894 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800895 where = sb.toString();
896 }
897
Christopher Tatef0c36f72009-07-28 15:24:05 -0700898 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700899
Christopher Tatef0c36f72009-07-28 15:24:05 -0700900 // we'lll need to back up the bookmark set if we are about to delete one
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700901 if (isBookmarkTable) {
Christopher Tatef0c36f72009-07-28 15:24:05 -0700902 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
903 new String[] { BookmarkColumns.BOOKMARK },
904 "_id = " + id, null, null);
905 if (cursor.moveToNext()) {
906 if (cursor.getInt(0) != 0) {
907 // yep, this record is a bookmark
908 mBackupManager.dataChanged();
909 }
910 }
911 cursor.close();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700912 }
Christopher Tatef0c36f72009-07-28 15:24:05 -0700913
914 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
915 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800916 return count;
917 }
918
919 @Override
920 public int update(Uri url, ContentValues values, String where,
921 String[] whereArgs) {
922 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
923
924 int match = URI_MATCHER.match(url);
925 if (match == -1 || match == URI_MATCH_SUGGEST) {
926 throw new IllegalArgumentException("Unknown URL");
927 }
928
Christopher Tatef0c36f72009-07-28 15:24:05 -0700929 String id = null;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700930 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
Christopher Tatef0c36f72009-07-28 15:24:05 -0700931 boolean changingBookmarks = false;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700932
933 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800934 StringBuilder sb = new StringBuilder();
935 if (where != null && where.length() > 0) {
936 sb.append("( ");
937 sb.append(where);
938 sb.append(" ) AND ");
939 }
Christopher Tatef0c36f72009-07-28 15:24:05 -0700940 id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800941 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -0700942 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800943 where = sb.toString();
944 }
945
Christopher Tatef0c36f72009-07-28 15:24:05 -0700946 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700947
Christopher Tatef0c36f72009-07-28 15:24:05 -0700948 // Not all bookmark-table updates should be backed up. Look to see
949 // whether we changed the title, url, or "is a bookmark" state, and
950 // request a backup if so.
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700951 if (isBookmarkTable) {
Christopher Tatef0c36f72009-07-28 15:24:05 -0700952 // Alterations to the bookmark field inherently change the bookmark
953 // set, so we don't need to query the record; we know a priori that
954 // we will need to back up this change.
955 if (values.containsKey(BookmarkColumns.BOOKMARK)) {
956 changingBookmarks = true;
957 }
958 // changing the title or URL of a bookmark record requires a backup,
959 // but we don't know wether such an update is on a bookmark without
960 // querying the record
961 if (!changingBookmarks &&
962 (values.containsKey(BookmarkColumns.TITLE)
963 || values.containsKey(BookmarkColumns.URL))) {
964 // when isBookmarkTable is true, the 'id' var was assigned above
965 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
966 new String[] { BookmarkColumns.BOOKMARK },
967 "_id = " + id, null, null);
968 if (cursor.moveToNext()) {
969 changingBookmarks = (cursor.getInt(0) != 0);
970 }
971 cursor.close();
972 }
973
974 // if this *is* a bookmark row we're altering, we need to back it up.
975 if (changingBookmarks) {
976 mBackupManager.dataChanged();
977 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700978 }
Christopher Tatef0c36f72009-07-28 15:24:05 -0700979
980 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
981 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800982 return ret;
983 }
Satish Sampath565505b2009-05-29 15:37:27 +0100984
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700985 /**
986 * Strips the provided url of preceding "http://" and any trailing "/". Does not
987 * strip "https://". If the provided string cannot be stripped, the original string
988 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +0100989 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700990 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +0100991 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700992 * @param url a url to strip, like "http://www.google.com/"
993 * @return a stripped url like "www.google.com", or the original string if it could
994 * not be stripped
995 */
996 private static String stripUrl(String url) {
997 if (url == null) return null;
998 Matcher m = STRIP_URL_PATTERN.matcher(url);
999 if (m.matches() && m.groupCount() == 3) {
1000 return m.group(2);
1001 } else {
1002 return url;
1003 }
1004 }
1005
The Android Open Source Project0c908882009-03-03 19:32:16 -08001006}