blob: a7da2464e10ac51b0d8382617b781ad4579f53b4 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Bjorn Bringertd2670652010-09-13 14:06:41 +010019import com.android.browser.search.SearchEngine;
20
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.app.SearchManager;
Christopher Tateb6a65442010-03-05 15:47:48 -080022import android.app.backup.BackupManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.content.ContentProvider;
Christopher Tatef0c36f72009-07-28 15:24:05 -070024import android.content.ContentResolver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.content.ContentUris;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070029import android.content.SharedPreferences;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070030import android.content.SharedPreferences.Editor;
Bjorn Bringertd2670652010-09-13 14:06:41 +010031import android.content.UriMatcher;
The Android Open Source Project0c908882009-03-03 19:32:16 -080032import android.database.AbstractCursor;
33import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.database.sqlite.SQLiteDatabase;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010035import android.database.sqlite.SQLiteOpenHelper;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.net.Uri;
Andrei Popescu93bea962010-03-23 15:04:36 +000037import android.os.Process;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070038import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.provider.Browser;
Christopher Tatef0c36f72009-07-28 15:24:05 -070040import android.provider.Browser.BookmarkColumns;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050041import android.speech.RecognizerResultsIntent;
Mike LeBeau21beb132009-05-13 14:57:50 -070042import android.text.TextUtils;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010043import android.util.Log;
Dianne Hackborn385effd2010-02-24 20:03:04 -080044import android.util.Patterns;
Dan Egnor5ee906c2009-11-18 12:11:49 -080045
Andrei Popescu1b20b9d2009-09-21 18:49:42 +010046import java.io.File;
47import java.io.FilenameFilter;
Leon Scroggins58d56c62010-01-28 15:12:40 -050048import java.util.ArrayList;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010049import java.util.Date;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070050import java.util.regex.Matcher;
51import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -080052
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070053
The Android Open Source Project0c908882009-03-03 19:32:16 -080054public class BrowserProvider extends ContentProvider {
55
56 private SQLiteOpenHelper mOpenHelper;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070057 private BackupManager mBackupManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080058 private static final String sDatabaseName = "browser.db";
59 private static final String TAG = "BrowserProvider";
60 private static final String ORDER_BY = "visits DESC, date DESC";
61
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070062 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
63 "viewer?source=androidclient";
64
The Android Open Source Project0c908882009-03-03 19:32:16 -080065 private static final String[] TABLE_NAMES = new String[] {
Bjorn Bringerta7611812010-03-24 11:12:02 +000066 "bookmarks", "searches"
The Android Open Source Project0c908882009-03-03 19:32:16 -080067 };
68 private static final String[] SUGGEST_PROJECTION = new String[] {
Leon Scrogginsb4464432009-11-25 12:37:50 -050069 "_id", "url", "title", "bookmark", "user_entered"
The Android Open Source Project0c908882009-03-03 19:32:16 -080070 };
Satish Sampath565505b2009-05-29 15:37:27 +010071 private static final String SUGGEST_SELECTION =
Leon Scrogginsb4464432009-11-25 12:37:50 -050072 "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?"
73 + " OR title LIKE ?) AND (bookmark = 1 OR user_entered = 1)";
Leon Scrogginsbd359cc2009-05-26 15:57:35 -040074 private String[] SUGGEST_ARGS = new String[5];
The Android Open Source Project0c908882009-03-03 19:32:16 -080075
76 // shared suggestion array index, make sure to match COLUMNS
77 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
78 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
79 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
80 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +000081 private static final int SUGGEST_COLUMN_TEXT_2_URL_ID = 5;
82 private static final int SUGGEST_COLUMN_ICON_1_ID = 6;
83 private static final int SUGGEST_COLUMN_ICON_2_ID = 7;
84 private static final int SUGGEST_COLUMN_QUERY_ID = 8;
Bjorn Bringert04851702009-09-22 10:36:01 +010085 private static final int SUGGEST_COLUMN_INTENT_EXTRA_DATA = 9;
The Android Open Source Project0c908882009-03-03 19:32:16 -080086
87 // shared suggestion columns
88 private static final String[] COLUMNS = new String[] {
89 "_id",
90 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
91 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
92 SearchManager.SUGGEST_COLUMN_TEXT_1,
93 SearchManager.SUGGEST_COLUMN_TEXT_2,
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +000094 SearchManager.SUGGEST_COLUMN_TEXT_2_URL,
The Android Open Source Project0c908882009-03-03 19:32:16 -080095 SearchManager.SUGGEST_COLUMN_ICON_1,
96 SearchManager.SUGGEST_COLUMN_ICON_2,
Mike LeBeau1ef26a32009-05-13 20:11:00 -070097 SearchManager.SUGGEST_COLUMN_QUERY,
Bjorn Bringert04851702009-09-22 10:36:01 +010098 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA};
The Android Open Source Project0c908882009-03-03 19:32:16 -080099
100 private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3;
101 private static final int MAX_SUGGESTION_LONG_ENTRIES = 6;
Leon Scroggins31887fd2009-05-18 16:58:08 -0400102 private static final String MAX_SUGGESTION_LONG_ENTRIES_STRING =
103 Integer.valueOf(MAX_SUGGESTION_LONG_ENTRIES).toString();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800104
105 // make sure that these match the index of TABLE_NAMES
106 private static final int URI_MATCH_BOOKMARKS = 0;
107 private static final int URI_MATCH_SEARCHES = 1;
108 // (id % 10) should match the table name index
109 private static final int URI_MATCH_BOOKMARKS_ID = 10;
110 private static final int URI_MATCH_SEARCHES_ID = 11;
111 //
112 private static final int URI_MATCH_SUGGEST = 20;
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100113 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114
115 private static final UriMatcher URI_MATCHER;
116
117 static {
118 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
119 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
120 URI_MATCH_BOOKMARKS);
121 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
122 URI_MATCH_BOOKMARKS_ID);
123 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
124 URI_MATCH_SEARCHES);
125 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
126 URI_MATCH_SEARCHES_ID);
127 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
128 URI_MATCH_SUGGEST);
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100129 URI_MATCHER.addURI("browser",
130 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
131 URI_MATCH_BOOKMARKS_SUGGEST);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132 }
133
134 // 1 -> 2 add cache table
135 // 2 -> 3 update history table
136 // 3 -> 4 add passwords table
137 // 4 -> 5 add settings table
138 // 5 -> 6 ?
139 // 6 -> 7 ?
140 // 7 -> 8 drop proxy table
141 // 8 -> 9 drop settings table
142 // 9 -> 10 add form_urls and form_data
143 // 10 -> 11 add searches table
144 // 11 -> 12 modify cache table
145 // 12 -> 13 modify cache table
146 // 13 -> 14 correspond with Google Bookmarks schema
147 // 14 -> 15 move couple of tables to either browser private database or webview database
148 // 15 -> 17 Set it up for the SearchManager
149 // 17 -> 18 Added favicon in bookmarks table for Home shortcuts
150 // 18 -> 19 Remove labels table
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400151 // 19 -> 20 Added thumbnail
Patrick Scott3918d442009-08-04 13:22:29 -0400152 // 20 -> 21 Added touch_icon
Grace Kloba6b52a552009-09-03 16:29:56 -0700153 // 21 -> 22 Remove "clientid"
Leon Scrogginsb4464432009-11-25 12:37:50 -0500154 // 22 -> 23 Added user_entered
155 private static final int DATABASE_VERSION = 23;
Satish Sampath565505b2009-05-29 15:37:27 +0100156
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700157 // Regular expression which matches http://, followed by some stuff, followed by
158 // optionally a trailing slash, all matched as separate groups.
159 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
Satish Sampath565505b2009-05-29 15:37:27 +0100160
Bjorn Bringertd2670652010-09-13 14:06:41 +0100161 private BrowserSettings mSettings;
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100162
The Android Open Source Project0c908882009-03-03 19:32:16 -0800163 public BrowserProvider() {
164 }
Satish Sampath565505b2009-05-29 15:37:27 +0100165
Patrick Scott43914692010-02-19 10:10:10 -0500166 // XXX: This is a major hack to remove our dependency on gsf constants and
167 // its content provider. http://b/issue?id=2425179
168 static String getClientId(ContentResolver cr) {
169 String ret = "android-google";
170 Cursor c = null;
171 try {
172 c = cr.query(Uri.parse("content://com.google.settings/partner"),
173 new String[] { "value" }, "name='client_id'", null, null);
174 if (c != null && c.moveToNext()) {
175 ret = c.getString(0);
176 }
177 } catch (RuntimeException ex) {
178 // fall through to return the default
179 } finally {
180 if (c != null) {
181 c.close();
182 }
183 }
184 return ret;
185 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700187 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800188 StringBuffer sb = new StringBuffer();
189 int lastCharLoc = 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100190
Patrick Scott43914692010-02-19 10:10:10 -0500191 final String client_id = getClientId(context.getContentResolver());
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700192
The Android Open Source Project0c908882009-03-03 19:32:16 -0800193 for (int i = 0; i < srcString.length(); ++i) {
194 char c = srcString.charAt(i);
195 if (c == '{') {
196 sb.append(srcString.subSequence(lastCharLoc, i));
197 lastCharLoc = i;
198 inner:
199 for (int j = i; j < srcString.length(); ++j) {
200 char k = srcString.charAt(j);
201 if (k == '}') {
202 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700203 if (propertyKeyValue.equals("CLIENT_ID")) {
204 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800205 } else {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700206 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800207 }
208 lastCharLoc = j + 1;
209 i = j;
210 break inner;
211 }
212 }
213 }
214 }
215 if (srcString.length() - lastCharLoc > 0) {
216 // Put on the tail, if there is one
217 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
218 }
219 return sb;
220 }
221
222 private static class DatabaseHelper extends SQLiteOpenHelper {
223 private Context mContext;
224
225 public DatabaseHelper(Context context) {
226 super(context, sDatabaseName, null, DATABASE_VERSION);
227 mContext = context;
228 }
229
230 @Override
231 public void onCreate(SQLiteDatabase db) {
232 db.execSQL("CREATE TABLE bookmarks (" +
233 "_id INTEGER PRIMARY KEY," +
234 "title TEXT," +
235 "url TEXT," +
236 "visits INTEGER," +
237 "date LONG," +
238 "created LONG," +
239 "description TEXT," +
240 "bookmark INTEGER," +
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400241 "favicon BLOB DEFAULT NULL," +
Patrick Scott3918d442009-08-04 13:22:29 -0400242 "thumbnail BLOB DEFAULT NULL," +
Leon Scrogginsb4464432009-11-25 12:37:50 -0500243 "touch_icon BLOB DEFAULT NULL," +
244 "user_entered INTEGER" +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800245 ");");
246
247 final CharSequence[] bookmarks = mContext.getResources()
248 .getTextArray(R.array.bookmarks);
249 int size = bookmarks.length;
250 try {
251 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700252 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800253 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
254 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100255 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800256 "', 0, 0, 0, 1);");
257 }
258 } catch (ArrayIndexOutOfBoundsException e) {
259 }
260
261 db.execSQL("CREATE TABLE searches (" +
262 "_id INTEGER PRIMARY KEY," +
263 "search TEXT," +
264 "date LONG" +
265 ");");
266 }
267
268 @Override
269 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
270 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400271 + newVersion);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800272 if (oldVersion == 18) {
273 db.execSQL("DROP TABLE IF EXISTS labels");
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400274 }
275 if (oldVersion <= 19) {
276 db.execSQL("ALTER TABLE bookmarks ADD COLUMN thumbnail BLOB DEFAULT NULL;");
Patrick Scott3918d442009-08-04 13:22:29 -0400277 }
278 if (oldVersion < 21) {
279 db.execSQL("ALTER TABLE bookmarks ADD COLUMN touch_icon BLOB DEFAULT NULL;");
Grace Kloba6b52a552009-09-03 16:29:56 -0700280 }
281 if (oldVersion < 22) {
282 db.execSQL("DELETE FROM bookmarks WHERE (bookmark = 0 AND url LIKE \"%.google.%client=ms-%\")");
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100283 removeGears();
Leon Scrogginsb4464432009-11-25 12:37:50 -0500284 }
285 if (oldVersion < 23) {
286 db.execSQL("ALTER TABLE bookmarks ADD COLUMN user_entered INTEGER;");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800287 } else {
288 db.execSQL("DROP TABLE IF EXISTS bookmarks");
289 db.execSQL("DROP TABLE IF EXISTS searches");
290 onCreate(db);
291 }
292 }
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100293
294 private void removeGears() {
Andrei Popescu93bea962010-03-23 15:04:36 +0000295 new Thread() {
296 @Override
297 public void run() {
298 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100299 String browserDataDirString = mContext.getApplicationInfo().dataDir;
300 final String appPluginsDirString = "app_plugins";
301 final String gearsPrefix = "gears";
302 File appPluginsDir = new File(browserDataDirString + File.separator
303 + appPluginsDirString);
304 if (!appPluginsDir.exists()) {
Andrei Popescu93bea962010-03-23 15:04:36 +0000305 return;
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100306 }
307 // Delete the Gears plugin files
308 File[] gearsFiles = appPluginsDir.listFiles(new FilenameFilter() {
309 public boolean accept(File dir, String filename) {
310 return filename.startsWith(gearsPrefix);
311 }
312 });
313 for (int i = 0; i < gearsFiles.length; ++i) {
314 if (gearsFiles[i].isDirectory()) {
315 deleteDirectory(gearsFiles[i]);
316 } else {
317 gearsFiles[i].delete();
318 }
319 }
320 // Delete the Gears data files
321 File gearsDataDir = new File(browserDataDirString + File.separator
322 + gearsPrefix);
323 if (!gearsDataDir.exists()) {
Andrei Popescu93bea962010-03-23 15:04:36 +0000324 return;
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100325 }
326 deleteDirectory(gearsDataDir);
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100327 }
328
329 private void deleteDirectory(File currentDir) {
330 File[] files = currentDir.listFiles();
331 for (int i = 0; i < files.length; ++i) {
332 if (files[i].isDirectory()) {
333 deleteDirectory(files[i]);
334 }
335 files[i].delete();
336 }
337 currentDir.delete();
338 }
Andrei Popescu93bea962010-03-23 15:04:36 +0000339 }.start();
Andrei Popescu1b20b9d2009-09-21 18:49:42 +0100340 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800341 }
342
343 @Override
344 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700345 final Context context = getContext();
346 mOpenHelper = new DatabaseHelper(context);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700347 mBackupManager = new BackupManager(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100348 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700349 // To avoid erasing the bookmark table, we added it explicitly for
350 // version 18 and 19 as in the other cases, we will erase the table.
351 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
352 SharedPreferences p = PreferenceManager
353 .getDefaultSharedPreferences(context);
354 boolean fix = p.getBoolean("fix_picasa", true);
355 if (fix) {
356 fixPicasaBookmark();
357 Editor ed = p.edit();
358 ed.putBoolean("fix_picasa", false);
Brad Fitzpatrick1a6e0e82010-09-01 16:29:03 -0700359 ed.apply();
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700360 }
361 }
Bjorn Bringertd2670652010-09-13 14:06:41 +0100362 mSettings = BrowserSettings.getInstance();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800363 return true;
364 }
365
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700366 private void fixPicasaBookmark() {
367 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
368 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
369 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
370 try {
371 if (!cursor.moveToFirst()) {
372 // set "created" so that it will be on the top of the list
373 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
374 "date, created, bookmark)" + " VALUES('" +
375 getContext().getString(R.string.picasa) + "', '"
376 + PICASA_URL + "', 0, 0, " + new Date().getTime()
377 + ", 1);");
378 }
379 } finally {
380 if (cursor != null) {
381 cursor.close();
382 }
383 }
384 }
385
The Android Open Source Project0c908882009-03-03 19:32:16 -0800386 /*
387 * Subclass AbstractCursor so we can combine multiple Cursors and add
Grace Kloba391df7c2010-03-01 19:51:49 -0800388 * "Search the web".
The Android Open Source Project0c908882009-03-03 19:32:16 -0800389 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100390 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
Grace Kloba391df7c2010-03-01 19:51:49 -0800391 * "Search the web";
392 * 2. If bookmark/history entries has a match, "Search the web" shows up at
393 * the second place. Otherwise, "Search the web" shows up at the first
394 * place.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800395 */
396 private class MySuggestionCursor extends AbstractCursor {
397 private Cursor mHistoryCursor;
398 private Cursor mSuggestCursor;
399 private int mHistoryCount;
400 private int mSuggestionCount;
Grace Klobad3992d42010-01-28 11:44:38 -0800401 private boolean mIncludeWebSearch;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800402 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100403 private int mSuggestText1Id;
404 private int mSuggestText2Id;
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000405 private int mSuggestText2UrlId;
Satish Sampath565505b2009-05-29 15:37:27 +0100406 private int mSuggestQueryId;
Bjorn Bringert04851702009-09-22 10:36:01 +0100407 private int mSuggestIntentExtraDataId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800408
409 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
410 mHistoryCursor = hc;
411 mSuggestCursor = sc;
412 mHistoryCount = hc.getCount();
413 mSuggestionCount = sc != null ? sc.getCount() : 0;
414 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
415 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
416 }
417 mString = string;
Grace Klobad3992d42010-01-28 11:44:38 -0800418 mIncludeWebSearch = string.length() > 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100419
420 // Some web suggest providers only give suggestions and have no description string for
421 // items. The order of the result columns may be different as well. So retrieve the
422 // column indices for the fields we need now and check before using below.
423 if (mSuggestCursor == null) {
424 mSuggestText1Id = -1;
425 mSuggestText2Id = -1;
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000426 mSuggestText2UrlId = -1;
Satish Sampath565505b2009-05-29 15:37:27 +0100427 mSuggestQueryId = -1;
Bjorn Bringert04851702009-09-22 10:36:01 +0100428 mSuggestIntentExtraDataId = -1;
Satish Sampath565505b2009-05-29 15:37:27 +0100429 } else {
430 mSuggestText1Id = mSuggestCursor.getColumnIndex(
431 SearchManager.SUGGEST_COLUMN_TEXT_1);
432 mSuggestText2Id = mSuggestCursor.getColumnIndex(
433 SearchManager.SUGGEST_COLUMN_TEXT_2);
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000434 mSuggestText2UrlId = mSuggestCursor.getColumnIndex(
435 SearchManager.SUGGEST_COLUMN_TEXT_2_URL);
Satish Sampath565505b2009-05-29 15:37:27 +0100436 mSuggestQueryId = mSuggestCursor.getColumnIndex(
437 SearchManager.SUGGEST_COLUMN_QUERY);
Bjorn Bringert04851702009-09-22 10:36:01 +0100438 mSuggestIntentExtraDataId = mSuggestCursor.getColumnIndex(
439 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA);
Satish Sampath565505b2009-05-29 15:37:27 +0100440 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800441 }
442
443 @Override
444 public boolean onMove(int oldPosition, int newPosition) {
445 if (mHistoryCursor == null) {
446 return false;
447 }
Grace Klobad3992d42010-01-28 11:44:38 -0800448 if (mIncludeWebSearch) {
Grace Kloba391df7c2010-03-01 19:51:49 -0800449 if (mHistoryCount == 0 && newPosition == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800450 return true;
Grace Kloba391df7c2010-03-01 19:51:49 -0800451 } else if (mHistoryCount > 0) {
452 if (newPosition == 0) {
453 mHistoryCursor.moveToPosition(0);
454 return true;
455 } else if (newPosition == 1) {
456 return true;
457 }
Grace Klobad3992d42010-01-28 11:44:38 -0800458 }
Grace Kloba391df7c2010-03-01 19:51:49 -0800459 newPosition--;
Grace Klobad3992d42010-01-28 11:44:38 -0800460 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800461 if (mHistoryCount > newPosition) {
462 mHistoryCursor.moveToPosition(newPosition);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800463 } else {
Grace Klobad3992d42010-01-28 11:44:38 -0800464 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800465 }
466 return true;
467 }
468
469 @Override
470 public int getCount() {
Grace Klobad3992d42010-01-28 11:44:38 -0800471 if (mIncludeWebSearch) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472 return mHistoryCount + mSuggestionCount + 1;
473 } else {
474 return mHistoryCount + mSuggestionCount;
475 }
476 }
477
478 @Override
479 public String[] getColumnNames() {
480 return COLUMNS;
481 }
Satish Sampath565505b2009-05-29 15:37:27 +0100482
The Android Open Source Project0c908882009-03-03 19:32:16 -0800483 @Override
484 public String getString(int columnIndex) {
485 if ((mPos != -1 && mHistoryCursor != null)) {
Grace Kloba391df7c2010-03-01 19:51:49 -0800486 int type = -1; // 0: web search; 1: history; 2: suggestion
487 if (mIncludeWebSearch) {
488 if (mHistoryCount == 0 && mPos == 0) {
489 type = 0;
490 } else if (mHistoryCount > 0) {
491 if (mPos == 0) {
492 type = 1;
493 } else if (mPos == 1) {
494 type = 0;
495 }
496 }
497 if (type == -1) type = (mPos - 1) < mHistoryCount ? 1 : 2;
498 } else {
499 type = mPos < mHistoryCount ? 1 : 2;
500 }
501
The Android Open Source Project0c908882009-03-03 19:32:16 -0800502 switch(columnIndex) {
503 case SUGGEST_COLUMN_INTENT_ACTION_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800504 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800505 return Intent.ACTION_VIEW;
506 } else {
507 return Intent.ACTION_SEARCH;
508 }
509
510 case SUGGEST_COLUMN_INTENT_DATA_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800511 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800512 return mHistoryCursor.getString(1);
513 } else {
514 return null;
515 }
516
517 case SUGGEST_COLUMN_TEXT_1_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800518 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800519 return mString;
Grace Kloba391df7c2010-03-01 19:51:49 -0800520 } else if (type == 1) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700521 return getHistoryTitle();
Grace Klobad3992d42010-01-28 11:44:38 -0800522 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100523 if (mSuggestText1Id == -1) return null;
524 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800525 }
526
527 case SUGGEST_COLUMN_TEXT_2_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800528 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800529 return getContext().getString(R.string.search_the_web);
Grace Kloba391df7c2010-03-01 19:51:49 -0800530 } else if (type == 1) {
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000531 return null; // Use TEXT_2_URL instead
Grace Klobad3992d42010-01-28 11:44:38 -0800532 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100533 if (mSuggestText2Id == -1) return null;
534 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800535 }
536
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000537 case SUGGEST_COLUMN_TEXT_2_URL_ID:
538 if (type == 0) {
539 return null;
540 } else if (type == 1) {
541 return getHistoryUrl();
542 } else {
543 if (mSuggestText2UrlId == -1) return null;
544 return mSuggestCursor.getString(mSuggestText2UrlId);
545 }
546
The Android Open Source Project0c908882009-03-03 19:32:16 -0800547 case SUGGEST_COLUMN_ICON_1_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800548 if (type == 1) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800549 if (mHistoryCursor.getInt(3) == 1) {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400550 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800551 R.drawable.ic_search_category_bookmark)
552 .toString();
553 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400554 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800555 R.drawable.ic_search_category_history)
556 .toString();
557 }
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_suggest)
561 .toString();
562 }
563
564 case SUGGEST_COLUMN_ICON_2_ID:
Leon Scroggins31887fd2009-05-18 16:58:08 -0400565 return "0";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800566
567 case SUGGEST_COLUMN_QUERY_ID:
Grace Kloba391df7c2010-03-01 19:51:49 -0800568 if (type == 0) {
Grace Klobad3992d42010-01-28 11:44:38 -0800569 return mString;
Grace Kloba391df7c2010-03-01 19:51:49 -0800570 } else if (type == 1) {
Mike LeBeau2af73052009-06-23 17:36:59 -0700571 // Return the url in the intent query column. This is ignored
572 // within the browser because our searchable is set to
573 // android:searchMode="queryRewriteFromData", but it is used by
574 // global search for query rewriting.
575 return mHistoryCursor.getString(1);
Grace Klobad3992d42010-01-28 11:44:38 -0800576 } else {
Satish Sampath565505b2009-05-29 15:37:27 +0100577 if (mSuggestQueryId == -1) return null;
578 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800579 }
Satish Sampath565505b2009-05-29 15:37:27 +0100580
Bjorn Bringert04851702009-09-22 10:36:01 +0100581 case SUGGEST_COLUMN_INTENT_EXTRA_DATA:
Grace Kloba391df7c2010-03-01 19:51:49 -0800582 if (type == 0) {
Bjorn Bringert04851702009-09-22 10:36:01 +0100583 return null;
Grace Kloba391df7c2010-03-01 19:51:49 -0800584 } else if (type == 1) {
Grace Klobad3992d42010-01-28 11:44:38 -0800585 return null;
586 } else {
Bjorn Bringert04851702009-09-22 10:36:01 +0100587 if (mSuggestIntentExtraDataId == -1) return null;
588 return mSuggestCursor.getString(mSuggestIntentExtraDataId);
Bjorn Bringert04851702009-09-22 10:36:01 +0100589 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800590 }
591 }
592 return null;
593 }
594
595 @Override
596 public double getDouble(int column) {
597 throw new UnsupportedOperationException();
598 }
599
600 @Override
601 public float getFloat(int column) {
602 throw new UnsupportedOperationException();
603 }
604
605 @Override
606 public int getInt(int column) {
607 throw new UnsupportedOperationException();
608 }
609
610 @Override
611 public long getLong(int column) {
612 if ((mPos != -1) && column == 0) {
613 return mPos; // use row# as the _Id
614 }
615 throw new UnsupportedOperationException();
616 }
617
618 @Override
619 public short getShort(int column) {
620 throw new UnsupportedOperationException();
621 }
622
623 @Override
624 public boolean isNull(int column) {
625 throw new UnsupportedOperationException();
626 }
627
628 // TODO Temporary change, finalize after jq's changes go in
629 public void deactivate() {
630 if (mHistoryCursor != null) {
631 mHistoryCursor.deactivate();
632 }
633 if (mSuggestCursor != null) {
634 mSuggestCursor.deactivate();
635 }
636 super.deactivate();
637 }
638
639 public boolean requery() {
640 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
641 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
642 }
643
644 // TODO Temporary change, finalize after jq's changes go in
645 public void close() {
646 super.close();
647 if (mHistoryCursor != null) {
648 mHistoryCursor.close();
649 mHistoryCursor = null;
650 }
651 if (mSuggestCursor != null) {
652 mSuggestCursor.close();
653 mSuggestCursor = null;
654 }
655 }
Satish Sampath565505b2009-05-29 15:37:27 +0100656
Mike LeBeau21beb132009-05-13 14:57:50 -0700657 /**
658 * Provides the title (text line 1) for a browser suggestion, which should be the
659 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100660 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700661 * @return the title string to use
662 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700663 private String getHistoryTitle() {
664 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700665 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000666 title = stripUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700667 }
668 return title;
669 }
Satish Sampath565505b2009-05-29 15:37:27 +0100670
Mike LeBeau21beb132009-05-13 14:57:50 -0700671 /**
672 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
673 * webpage url. If the webpage title is empty, then the url should go in the title
674 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100675 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700676 * @return the subtitle string to use, or null if none
677 */
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000678 private String getHistoryUrl() {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700679 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700680 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
681 return null;
682 } else {
Bjorn Bringertc7c0fce2010-03-02 11:20:29 +0000683 return stripUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700684 }
685 }
Satish Sampath565505b2009-05-29 15:37:27 +0100686
The Android Open Source Project0c908882009-03-03 19:32:16 -0800687 }
688
Leon Scroggins58d56c62010-01-28 15:12:40 -0500689 private static class ResultsCursor extends AbstractCursor {
690 // Array indices for RESULTS_COLUMNS
691 private static final int RESULT_ACTION_ID = 1;
692 private static final int RESULT_DATA_ID = 2;
693 private static final int RESULT_TEXT_ID = 3;
694 private static final int RESULT_ICON_ID = 4;
695 private static final int RESULT_EXTRA_ID = 5;
696
697 private static final String[] RESULTS_COLUMNS = new String[] {
698 "_id",
699 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
700 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
701 SearchManager.SUGGEST_COLUMN_TEXT_1,
702 SearchManager.SUGGEST_COLUMN_ICON_1,
703 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA
704 };
705 private final ArrayList<String> mResults;
706 public ResultsCursor(ArrayList<String> results) {
707 mResults = results;
708 }
709 public int getCount() { return mResults.size(); }
710
711 public String[] getColumnNames() {
712 return RESULTS_COLUMNS;
713 }
714
715 public String getString(int column) {
716 switch (column) {
717 case RESULT_ACTION_ID:
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500718 return RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500719 case RESULT_TEXT_ID:
720 // The data is used when the phone is in landscape mode. We
721 // still want to show the result string.
722 case RESULT_DATA_ID:
723 return mResults.get(mPos);
724 case RESULT_EXTRA_ID:
725 // The Intent's extra data will store the index into
726 // mResults so the BrowserActivity will know which result to
727 // use.
728 return Integer.toString(mPos);
729 case RESULT_ICON_ID:
730 return Integer.valueOf(R.drawable.magnifying_glass)
731 .toString();
732 default:
733 return null;
734 }
735 }
736 public short getShort(int column) {
737 throw new UnsupportedOperationException();
738 }
739 public int getInt(int column) {
740 throw new UnsupportedOperationException();
741 }
742 public long getLong(int column) {
743 if ((mPos != -1) && column == 0) {
744 return mPos; // use row# as the _id
745 }
746 throw new UnsupportedOperationException();
747 }
748 public float getFloat(int column) {
749 throw new UnsupportedOperationException();
750 }
751 public double getDouble(int column) {
752 throw new UnsupportedOperationException();
753 }
754 public boolean isNull(int column) {
755 throw new UnsupportedOperationException();
756 }
757 }
758
759 private ResultsCursor mResultsCursor;
760
761 /**
762 * Provide a set of results to be returned to query, intended to be used
763 * by the SearchDialog when the BrowserActivity is in voice search mode.
764 * @param results Strings to display in the dropdown from the SearchDialog
765 */
766 /* package */ void setQueryResults(ArrayList<String> results) {
767 if (results == null) {
768 mResultsCursor = null;
769 } else {
770 mResultsCursor = new ResultsCursor(results);
771 }
772 }
773
The Android Open Source Project0c908882009-03-03 19:32:16 -0800774 @Override
775 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100776 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800777 throws IllegalStateException {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800778 int match = URI_MATCHER.match(url);
779 if (match == -1) {
780 throw new IllegalArgumentException("Unknown URL");
781 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500782 if (match == URI_MATCH_SUGGEST && mResultsCursor != null) {
783 Cursor results = mResultsCursor;
784 mResultsCursor = null;
785 return results;
786 }
787 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800788
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100789 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800790 String suggestSelection;
791 String [] myArgs;
792 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
793 suggestSelection = null;
794 myArgs = null;
795 } else {
796 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700797 if (selectionArgs[0].startsWith("http")
798 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800799 myArgs = new String[1];
800 myArgs[0] = like;
801 suggestSelection = selection;
802 } else {
803 SUGGEST_ARGS[0] = "http://" + like;
804 SUGGEST_ARGS[1] = "http://www." + like;
805 SUGGEST_ARGS[2] = "https://" + like;
806 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400807 // To match against titles.
808 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800809 myArgs = SUGGEST_ARGS;
810 suggestSelection = SUGGEST_SELECTION;
811 }
812 }
813
814 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
815 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Leon Scroggins31887fd2009-05-18 16:58:08 -0400816 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800817
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100818 if (match == URI_MATCH_BOOKMARKS_SUGGEST
Dan Egnor5ee906c2009-11-18 12:11:49 -0800819 || Patterns.WEB_URL.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800820 return new MySuggestionCursor(c, null, "");
821 } else {
Bjorn Bringertd2670652010-09-13 14:06:41 +0100822 // get search suggestions if there is still space in the list
The Android Open Source Project0c908882009-03-03 19:32:16 -0800823 if (myArgs != null && myArgs.length > 1
Bjorn Bringertd2670652010-09-13 14:06:41 +0100824 && mSettings.getShowSearchSuggestions()
The Android Open Source Project0c908882009-03-03 19:32:16 -0800825 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringertd2670652010-09-13 14:06:41 +0100826 SearchEngine searchEngine = mSettings.getSearchEngine();
827 if (searchEngine != null && searchEngine.supportsSuggestions()) {
828 Cursor sc = searchEngine.getSuggestions(getContext(), selectionArgs[0]);
829 return new MySuggestionCursor(c, sc, selectionArgs[0]);
830 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800831 }
832 return new MySuggestionCursor(c, null, selectionArgs[0]);
833 }
834 }
835
836 String[] projection = null;
837 if (projectionIn != null && projectionIn.length > 0) {
838 projection = new String[projectionIn.length + 1];
839 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
840 projection[projectionIn.length] = "_id AS _id";
841 }
842
843 StringBuilder whereClause = new StringBuilder(256);
844 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
845 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
846 .append(")");
847 }
848
849 // Tack on the user's selection, if present
850 if (selection != null && selection.length() > 0) {
851 if (whereClause.length() > 0) {
852 whereClause.append(" AND ");
853 }
854
855 whereClause.append('(');
856 whereClause.append(selection);
857 whereClause.append(')');
858 }
859 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
860 whereClause.toString(), selectionArgs, null, null, sortOrder,
861 null);
862 c.setNotificationUri(getContext().getContentResolver(), url);
863 return c;
864 }
865
866 @Override
867 public String getType(Uri url) {
868 int match = URI_MATCHER.match(url);
869 switch (match) {
870 case URI_MATCH_BOOKMARKS:
871 return "vnd.android.cursor.dir/bookmark";
872
873 case URI_MATCH_BOOKMARKS_ID:
874 return "vnd.android.cursor.item/bookmark";
875
876 case URI_MATCH_SEARCHES:
877 return "vnd.android.cursor.dir/searches";
878
879 case URI_MATCH_SEARCHES_ID:
880 return "vnd.android.cursor.item/searches";
881
882 case URI_MATCH_SUGGEST:
883 return SearchManager.SUGGEST_MIME_TYPE;
884
885 default:
886 throw new IllegalArgumentException("Unknown URL");
887 }
888 }
889
890 @Override
891 public Uri insert(Uri url, ContentValues initialValues) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700892 boolean isBookmarkTable = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800893 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
894
895 int match = URI_MATCHER.match(url);
896 Uri uri = null;
897 switch (match) {
898 case URI_MATCH_BOOKMARKS: {
899 // Insert into the bookmarks table
900 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
901 initialValues);
902 if (rowID > 0) {
903 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
904 rowID);
905 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700906 isBookmarkTable = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800907 break;
908 }
909
910 case URI_MATCH_SEARCHES: {
911 // Insert into the searches table
912 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
913 initialValues);
914 if (rowID > 0) {
915 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
916 rowID);
917 }
918 break;
919 }
920
921 default:
922 throw new IllegalArgumentException("Unknown URL");
923 }
924
925 if (uri == null) {
926 throw new IllegalArgumentException("Unknown URL");
927 }
928 getContext().getContentResolver().notifyChange(uri, null);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700929
Christopher Tatef0c36f72009-07-28 15:24:05 -0700930 // Back up the new bookmark set if we just inserted one.
931 // A row created when bookmarks are added from scratch will have
932 // bookmark=1 in the initial value set.
933 if (isBookmarkTable
934 && initialValues.containsKey(BookmarkColumns.BOOKMARK)
935 && initialValues.getAsInteger(BookmarkColumns.BOOKMARK) != 0) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700936 mBackupManager.dataChanged();
937 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800938 return uri;
939 }
940
941 @Override
942 public int delete(Uri url, String where, String[] whereArgs) {
943 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
944
945 int match = URI_MATCHER.match(url);
946 if (match == -1 || match == URI_MATCH_SUGGEST) {
947 throw new IllegalArgumentException("Unknown URL");
948 }
949
Christopher Tatef0c36f72009-07-28 15:24:05 -0700950 // need to know whether it's the bookmarks table for a couple of reasons
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700951 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
Christopher Tatef0c36f72009-07-28 15:24:05 -0700952 String id = null;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700953
954 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800955 StringBuilder sb = new StringBuilder();
956 if (where != null && where.length() > 0) {
957 sb.append("( ");
958 sb.append(where);
959 sb.append(" ) AND ");
960 }
Christopher Tatef0c36f72009-07-28 15:24:05 -0700961 id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800962 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -0700963 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800964 where = sb.toString();
965 }
966
Christopher Tatef0c36f72009-07-28 15:24:05 -0700967 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700968
Christopher Tatef0c36f72009-07-28 15:24:05 -0700969 // we'lll need to back up the bookmark set if we are about to delete one
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700970 if (isBookmarkTable) {
Christopher Tatef0c36f72009-07-28 15:24:05 -0700971 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
972 new String[] { BookmarkColumns.BOOKMARK },
973 "_id = " + id, null, null);
974 if (cursor.moveToNext()) {
975 if (cursor.getInt(0) != 0) {
976 // yep, this record is a bookmark
977 mBackupManager.dataChanged();
978 }
979 }
980 cursor.close();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700981 }
Christopher Tatef0c36f72009-07-28 15:24:05 -0700982
983 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
984 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800985 return count;
986 }
987
988 @Override
989 public int update(Uri url, ContentValues values, String where,
990 String[] whereArgs) {
991 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
992
993 int match = URI_MATCHER.match(url);
994 if (match == -1 || match == URI_MATCH_SUGGEST) {
995 throw new IllegalArgumentException("Unknown URL");
996 }
997
Leon Scrogginsf2463ae2010-02-23 14:28:51 -0500998 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800999 StringBuilder sb = new StringBuilder();
1000 if (where != null && where.length() > 0) {
1001 sb.append("( ");
1002 sb.append(where);
1003 sb.append(" ) AND ");
1004 }
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001005 String id = url.getPathSegments().get(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001006 sb.append("_id = ");
Christopher Tatef0c36f72009-07-28 15:24:05 -07001007 sb.append(id);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001008 where = sb.toString();
1009 }
1010
Christopher Tatef0c36f72009-07-28 15:24:05 -07001011 ContentResolver cr = getContext().getContentResolver();
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001012
Christopher Tatef0c36f72009-07-28 15:24:05 -07001013 // Not all bookmark-table updates should be backed up. Look to see
1014 // whether we changed the title, url, or "is a bookmark" state, and
1015 // request a backup if so.
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001016 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_BOOKMARKS) {
1017 boolean changingBookmarks = false;
Christopher Tatef0c36f72009-07-28 15:24:05 -07001018 // Alterations to the bookmark field inherently change the bookmark
1019 // set, so we don't need to query the record; we know a priori that
1020 // we will need to back up this change.
1021 if (values.containsKey(BookmarkColumns.BOOKMARK)) {
1022 changingBookmarks = true;
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001023 } else if ((values.containsKey(BookmarkColumns.TITLE)
1024 || values.containsKey(BookmarkColumns.URL))
1025 && values.containsKey(BookmarkColumns._ID)) {
1026 // If a title or URL has been changed, check to see if it is to
1027 // a bookmark. The ID should have been included in the update,
1028 // so use it.
Christopher Tatef0c36f72009-07-28 15:24:05 -07001029 Cursor cursor = cr.query(Browser.BOOKMARKS_URI,
1030 new String[] { BookmarkColumns.BOOKMARK },
Leon Scrogginsf2463ae2010-02-23 14:28:51 -05001031 BookmarkColumns._ID + " = "
1032 + values.getAsString(BookmarkColumns._ID), null, null);
Christopher Tatef0c36f72009-07-28 15:24:05 -07001033 if (cursor.moveToNext()) {
1034 changingBookmarks = (cursor.getInt(0) != 0);
1035 }
1036 cursor.close();
1037 }
1038
1039 // if this *is* a bookmark row we're altering, we need to back it up.
1040 if (changingBookmarks) {
1041 mBackupManager.dataChanged();
1042 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -07001043 }
Christopher Tatef0c36f72009-07-28 15:24:05 -07001044
1045 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
1046 cr.notifyChange(url, null);
The Android Open Source Project0c908882009-03-03 19:32:16 -08001047 return ret;
1048 }
Satish Sampath565505b2009-05-29 15:37:27 +01001049
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001050 /**
1051 * Strips the provided url of preceding "http://" and any trailing "/". Does not
1052 * strip "https://". If the provided string cannot be stripped, the original string
1053 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +01001054 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001055 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +01001056 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -07001057 * @param url a url to strip, like "http://www.google.com/"
1058 * @return a stripped url like "www.google.com", or the original string if it could
1059 * not be stripped
1060 */
1061 private static String stripUrl(String url) {
1062 if (url == null) return null;
1063 Matcher m = STRIP_URL_PATTERN.matcher(url);
1064 if (m.matches() && m.groupCount() == 3) {
1065 return m.group(2);
1066 } else {
1067 return url;
1068 }
1069 }
1070
The Android Open Source Project0c908882009-03-03 19:32:16 -08001071}