The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 19 | import android.app.SearchManager; |
Bjorn Bringert | abc3ac8 | 2009-12-04 12:59:14 +0000 | [diff] [blame] | 20 | import android.app.SearchableInfo; |
Christopher Tate | b6a6544 | 2010-03-05 15:47:48 -0800 | [diff] [blame] | 21 | import android.app.backup.BackupManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 22 | import android.content.ComponentName; |
| 23 | import android.content.ContentProvider; |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 24 | import android.content.ContentResolver; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 25 | import android.content.ContentUris; |
| 26 | import android.content.ContentValues; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 29 | import android.content.SharedPreferences; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 30 | import android.content.UriMatcher; |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 31 | import android.content.SharedPreferences.Editor; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
| 33 | import android.content.pm.ResolveInfo; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 34 | import android.database.AbstractCursor; |
Leon Scroggins | 62b71f7 | 2009-06-12 17:51:22 -0400 | [diff] [blame] | 35 | import android.database.ContentObserver; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 36 | import android.database.Cursor; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 37 | import android.database.sqlite.SQLiteDatabase; |
Bjorn Bringert | bcd20b3 | 2009-04-29 21:52:09 +0100 | [diff] [blame] | 38 | import android.database.sqlite.SQLiteOpenHelper; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 39 | import android.net.Uri; |
Leon Scroggins | 62b71f7 | 2009-06-12 17:51:22 -0400 | [diff] [blame] | 40 | import android.os.Handler; |
Andrei Popescu | 93bea96 | 2010-03-23 15:04:36 +0000 | [diff] [blame] | 41 | import android.os.Process; |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 42 | import android.preference.PreferenceManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 43 | import android.provider.Browser; |
Leon Scroggins | 62b71f7 | 2009-06-12 17:51:22 -0400 | [diff] [blame] | 44 | import android.provider.Settings; |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 45 | import android.provider.Browser.BookmarkColumns; |
Leon Scroggins | a1cc3fd | 2010-02-01 16:14:11 -0500 | [diff] [blame] | 46 | import android.speech.RecognizerResultsIntent; |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 47 | import android.text.TextUtils; |
Bjorn Bringert | bcd20b3 | 2009-04-29 21:52:09 +0100 | [diff] [blame] | 48 | import android.util.Log; |
Dianne Hackborn | 385effd | 2010-02-24 20:03:04 -0800 | [diff] [blame] | 49 | import android.util.Patterns; |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 50 | import android.util.TypedValue; |
Bjorn Bringert | bcd20b3 | 2009-04-29 21:52:09 +0100 | [diff] [blame] | 51 | |
Dan Egnor | 5ee906c | 2009-11-18 12:11:49 -0800 | [diff] [blame] | 52 | |
Andrei Popescu | 1b20b9d | 2009-09-21 18:49:42 +0100 | [diff] [blame] | 53 | import java.io.File; |
| 54 | import java.io.FilenameFilter; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 55 | import java.util.ArrayList; |
Bjorn Bringert | bcd20b3 | 2009-04-29 21:52:09 +0100 | [diff] [blame] | 56 | import java.util.Date; |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 57 | import java.util.regex.Matcher; |
| 58 | import java.util.regex.Pattern; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 59 | |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 60 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 61 | public class BrowserProvider extends ContentProvider { |
| 62 | |
| 63 | private SQLiteOpenHelper mOpenHelper; |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 64 | private BackupManager mBackupManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 65 | 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 Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 69 | private static final String PICASA_URL = "http://picasaweb.google.com/m/" + |
| 70 | "viewer?source=androidclient"; |
| 71 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 72 | private static final String[] TABLE_NAMES = new String[] { |
Bjorn Bringert | a761181 | 2010-03-24 11:12:02 +0000 | [diff] [blame] | 73 | "bookmarks", "searches" |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 74 | }; |
| 75 | private static final String[] SUGGEST_PROJECTION = new String[] { |
Leon Scroggins | b446443 | 2009-11-25 12:37:50 -0500 | [diff] [blame] | 76 | "_id", "url", "title", "bookmark", "user_entered" |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 77 | }; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 78 | private static final String SUGGEST_SELECTION = |
Leon Scroggins | b446443 | 2009-11-25 12:37:50 -0500 | [diff] [blame] | 79 | "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?" |
| 80 | + " OR title LIKE ?) AND (bookmark = 1 OR user_entered = 1)"; |
Leon Scroggins | bd359cc | 2009-05-26 15:57:35 -0400 | [diff] [blame] | 81 | private String[] SUGGEST_ARGS = new String[5]; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 82 | |
| 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; |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 88 | private static final int SUGGEST_COLUMN_TEXT_2_URL_ID = 5; |
| 89 | private static final int SUGGEST_COLUMN_ICON_1_ID = 6; |
| 90 | private static final int SUGGEST_COLUMN_ICON_2_ID = 7; |
| 91 | private static final int SUGGEST_COLUMN_QUERY_ID = 8; |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 92 | private static final int SUGGEST_COLUMN_INTENT_EXTRA_DATA = 9; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 93 | |
| 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, |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 101 | SearchManager.SUGGEST_COLUMN_TEXT_2_URL, |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 102 | SearchManager.SUGGEST_COLUMN_ICON_1, |
| 103 | SearchManager.SUGGEST_COLUMN_ICON_2, |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 104 | SearchManager.SUGGEST_COLUMN_QUERY, |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 105 | SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA}; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 106 | |
| 107 | private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3; |
| 108 | private static final int MAX_SUGGESTION_LONG_ENTRIES = 6; |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame] | 109 | private static final String MAX_SUGGESTION_LONG_ENTRIES_STRING = |
| 110 | Integer.valueOf(MAX_SUGGESTION_LONG_ENTRIES).toString(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 111 | |
| 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 Bringert | 346dafb | 2009-04-29 21:41:47 +0100 | [diff] [blame] | 120 | private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 121 | |
| 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 Bringert | 346dafb | 2009-04-29 21:41:47 +0100 | [diff] [blame] | 136 | URI_MATCHER.addURI("browser", |
| 137 | TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY, |
| 138 | URI_MATCH_BOOKMARKS_SUGGEST); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 139 | } |
| 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 Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 158 | // 19 -> 20 Added thumbnail |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 159 | // 20 -> 21 Added touch_icon |
Grace Kloba | 6b52a55 | 2009-09-03 16:29:56 -0700 | [diff] [blame] | 160 | // 21 -> 22 Remove "clientid" |
Leon Scroggins | b446443 | 2009-11-25 12:37:50 -0500 | [diff] [blame] | 161 | // 22 -> 23 Added user_entered |
| 162 | private static final int DATABASE_VERSION = 23; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 163 | |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 164 | // Regular expression which matches http://, followed by some stuff, followed by |
| 165 | // optionally a trailing slash, all matched as separate groups. |
| 166 | private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?"); |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 167 | |
Bjorn Bringert | d8b0ad2 | 2009-06-22 10:36:29 +0100 | [diff] [blame] | 168 | private SearchManager mSearchManager; |
| 169 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 170 | public BrowserProvider() { |
| 171 | } |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 172 | |
Patrick Scott | 4391469 | 2010-02-19 10:10:10 -0500 | [diff] [blame] | 173 | // XXX: This is a major hack to remove our dependency on gsf constants and |
| 174 | // its content provider. http://b/issue?id=2425179 |
| 175 | static String getClientId(ContentResolver cr) { |
| 176 | String ret = "android-google"; |
| 177 | Cursor c = null; |
| 178 | try { |
| 179 | c = cr.query(Uri.parse("content://com.google.settings/partner"), |
| 180 | new String[] { "value" }, "name='client_id'", null, null); |
| 181 | if (c != null && c.moveToNext()) { |
| 182 | ret = c.getString(0); |
| 183 | } |
| 184 | } catch (RuntimeException ex) { |
| 185 | // fall through to return the default |
| 186 | } finally { |
| 187 | if (c != null) { |
| 188 | c.close(); |
| 189 | } |
| 190 | } |
| 191 | return ret; |
| 192 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 193 | |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 194 | private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 195 | StringBuffer sb = new StringBuffer(); |
| 196 | int lastCharLoc = 0; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 197 | |
Patrick Scott | 4391469 | 2010-02-19 10:10:10 -0500 | [diff] [blame] | 198 | final String client_id = getClientId(context.getContentResolver()); |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 199 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 200 | for (int i = 0; i < srcString.length(); ++i) { |
| 201 | char c = srcString.charAt(i); |
| 202 | if (c == '{') { |
| 203 | sb.append(srcString.subSequence(lastCharLoc, i)); |
| 204 | lastCharLoc = i; |
| 205 | inner: |
| 206 | for (int j = i; j < srcString.length(); ++j) { |
| 207 | char k = srcString.charAt(j); |
| 208 | if (k == '}') { |
| 209 | String propertyKeyValue = srcString.subSequence(i + 1, j).toString(); |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 210 | if (propertyKeyValue.equals("CLIENT_ID")) { |
| 211 | sb.append(client_id); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 212 | } else { |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 213 | sb.append("unknown"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 214 | } |
| 215 | lastCharLoc = j + 1; |
| 216 | i = j; |
| 217 | break inner; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | if (srcString.length() - lastCharLoc > 0) { |
| 223 | // Put on the tail, if there is one |
| 224 | sb.append(srcString.subSequence(lastCharLoc, srcString.length())); |
| 225 | } |
| 226 | return sb; |
| 227 | } |
| 228 | |
| 229 | private static class DatabaseHelper extends SQLiteOpenHelper { |
| 230 | private Context mContext; |
| 231 | |
| 232 | public DatabaseHelper(Context context) { |
| 233 | super(context, sDatabaseName, null, DATABASE_VERSION); |
| 234 | mContext = context; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public void onCreate(SQLiteDatabase db) { |
| 239 | db.execSQL("CREATE TABLE bookmarks (" + |
| 240 | "_id INTEGER PRIMARY KEY," + |
| 241 | "title TEXT," + |
| 242 | "url TEXT," + |
| 243 | "visits INTEGER," + |
| 244 | "date LONG," + |
| 245 | "created LONG," + |
| 246 | "description TEXT," + |
| 247 | "bookmark INTEGER," + |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 248 | "favicon BLOB DEFAULT NULL," + |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 249 | "thumbnail BLOB DEFAULT NULL," + |
Leon Scroggins | b446443 | 2009-11-25 12:37:50 -0500 | [diff] [blame] | 250 | "touch_icon BLOB DEFAULT NULL," + |
| 251 | "user_entered INTEGER" + |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 252 | ");"); |
| 253 | |
| 254 | final CharSequence[] bookmarks = mContext.getResources() |
| 255 | .getTextArray(R.array.bookmarks); |
| 256 | int size = bookmarks.length; |
| 257 | try { |
| 258 | for (int i = 0; i < size; i = i + 2) { |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 259 | CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 260 | db.execSQL("INSERT INTO bookmarks (title, url, visits, " + |
| 261 | "date, created, bookmark)" + " VALUES('" + |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 262 | bookmarks[i] + "', '" + bookmarkDestination + |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 263 | "', 0, 0, 0, 1);"); |
| 264 | } |
| 265 | } catch (ArrayIndexOutOfBoundsException e) { |
| 266 | } |
| 267 | |
| 268 | db.execSQL("CREATE TABLE searches (" + |
| 269 | "_id INTEGER PRIMARY KEY," + |
| 270 | "search TEXT," + |
| 271 | "date LONG" + |
| 272 | ");"); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 277 | Log.w(TAG, "Upgrading database from version " + oldVersion + " to " |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 278 | + newVersion); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 279 | if (oldVersion == 18) { |
| 280 | db.execSQL("DROP TABLE IF EXISTS labels"); |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 281 | } |
| 282 | if (oldVersion <= 19) { |
| 283 | db.execSQL("ALTER TABLE bookmarks ADD COLUMN thumbnail BLOB DEFAULT NULL;"); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 284 | } |
| 285 | if (oldVersion < 21) { |
| 286 | db.execSQL("ALTER TABLE bookmarks ADD COLUMN touch_icon BLOB DEFAULT NULL;"); |
Grace Kloba | 6b52a55 | 2009-09-03 16:29:56 -0700 | [diff] [blame] | 287 | } |
| 288 | if (oldVersion < 22) { |
| 289 | db.execSQL("DELETE FROM bookmarks WHERE (bookmark = 0 AND url LIKE \"%.google.%client=ms-%\")"); |
Andrei Popescu | 1b20b9d | 2009-09-21 18:49:42 +0100 | [diff] [blame] | 290 | removeGears(); |
Leon Scroggins | b446443 | 2009-11-25 12:37:50 -0500 | [diff] [blame] | 291 | } |
| 292 | if (oldVersion < 23) { |
| 293 | db.execSQL("ALTER TABLE bookmarks ADD COLUMN user_entered INTEGER;"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 294 | } else { |
| 295 | db.execSQL("DROP TABLE IF EXISTS bookmarks"); |
| 296 | db.execSQL("DROP TABLE IF EXISTS searches"); |
| 297 | onCreate(db); |
| 298 | } |
| 299 | } |
Andrei Popescu | 1b20b9d | 2009-09-21 18:49:42 +0100 | [diff] [blame] | 300 | |
| 301 | private void removeGears() { |
Andrei Popescu | 93bea96 | 2010-03-23 15:04:36 +0000 | [diff] [blame] | 302 | new Thread() { |
| 303 | @Override |
| 304 | public void run() { |
| 305 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Andrei Popescu | 1b20b9d | 2009-09-21 18:49:42 +0100 | [diff] [blame] | 306 | String browserDataDirString = mContext.getApplicationInfo().dataDir; |
| 307 | final String appPluginsDirString = "app_plugins"; |
| 308 | final String gearsPrefix = "gears"; |
| 309 | File appPluginsDir = new File(browserDataDirString + File.separator |
| 310 | + appPluginsDirString); |
| 311 | if (!appPluginsDir.exists()) { |
Andrei Popescu | 93bea96 | 2010-03-23 15:04:36 +0000 | [diff] [blame] | 312 | return; |
Andrei Popescu | 1b20b9d | 2009-09-21 18:49:42 +0100 | [diff] [blame] | 313 | } |
| 314 | // Delete the Gears plugin files |
| 315 | File[] gearsFiles = appPluginsDir.listFiles(new FilenameFilter() { |
| 316 | public boolean accept(File dir, String filename) { |
| 317 | return filename.startsWith(gearsPrefix); |
| 318 | } |
| 319 | }); |
| 320 | for (int i = 0; i < gearsFiles.length; ++i) { |
| 321 | if (gearsFiles[i].isDirectory()) { |
| 322 | deleteDirectory(gearsFiles[i]); |
| 323 | } else { |
| 324 | gearsFiles[i].delete(); |
| 325 | } |
| 326 | } |
| 327 | // Delete the Gears data files |
| 328 | File gearsDataDir = new File(browserDataDirString + File.separator |
| 329 | + gearsPrefix); |
| 330 | if (!gearsDataDir.exists()) { |
Andrei Popescu | 93bea96 | 2010-03-23 15:04:36 +0000 | [diff] [blame] | 331 | return; |
Andrei Popescu | 1b20b9d | 2009-09-21 18:49:42 +0100 | [diff] [blame] | 332 | } |
| 333 | deleteDirectory(gearsDataDir); |
Andrei Popescu | 1b20b9d | 2009-09-21 18:49:42 +0100 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | private void deleteDirectory(File currentDir) { |
| 337 | File[] files = currentDir.listFiles(); |
| 338 | for (int i = 0; i < files.length; ++i) { |
| 339 | if (files[i].isDirectory()) { |
| 340 | deleteDirectory(files[i]); |
| 341 | } |
| 342 | files[i].delete(); |
| 343 | } |
| 344 | currentDir.delete(); |
| 345 | } |
Andrei Popescu | 93bea96 | 2010-03-23 15:04:36 +0000 | [diff] [blame] | 346 | }.start(); |
Andrei Popescu | 1b20b9d | 2009-09-21 18:49:42 +0100 | [diff] [blame] | 347 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public boolean onCreate() { |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 352 | final Context context = getContext(); |
| 353 | mOpenHelper = new DatabaseHelper(context); |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 354 | mBackupManager = new BackupManager(context); |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 355 | // we added "picasa web album" into default bookmarks for version 19. |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 356 | // To avoid erasing the bookmark table, we added it explicitly for |
| 357 | // version 18 and 19 as in the other cases, we will erase the table. |
| 358 | if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) { |
| 359 | SharedPreferences p = PreferenceManager |
| 360 | .getDefaultSharedPreferences(context); |
| 361 | boolean fix = p.getBoolean("fix_picasa", true); |
| 362 | if (fix) { |
| 363 | fixPicasaBookmark(); |
| 364 | Editor ed = p.edit(); |
| 365 | ed.putBoolean("fix_picasa", false); |
| 366 | ed.commit(); |
| 367 | } |
| 368 | } |
Bjorn Bringert | d8b0ad2 | 2009-06-22 10:36:29 +0100 | [diff] [blame] | 369 | mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE); |
Leon Scroggins | 62b71f7 | 2009-06-12 17:51:22 -0400 | [diff] [blame] | 370 | mShowWebSuggestionsSettingChangeObserver |
| 371 | = new ShowWebSuggestionsSettingChangeObserver(); |
| 372 | context.getContentResolver().registerContentObserver( |
| 373 | Settings.System.getUriFor( |
| 374 | Settings.System.SHOW_WEB_SUGGESTIONS), |
| 375 | true, mShowWebSuggestionsSettingChangeObserver); |
| 376 | updateShowWebSuggestions(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 377 | return true; |
| 378 | } |
| 379 | |
Leon Scroggins | 62b71f7 | 2009-06-12 17:51:22 -0400 | [diff] [blame] | 380 | /** |
| 381 | * This Observer will ensure that if the user changes the system |
| 382 | * setting of whether to display web suggestions, we will |
| 383 | * change accordingly. |
| 384 | */ |
| 385 | /* package */ class ShowWebSuggestionsSettingChangeObserver |
| 386 | extends ContentObserver { |
| 387 | public ShowWebSuggestionsSettingChangeObserver() { |
| 388 | super(new Handler()); |
| 389 | } |
| 390 | |
| 391 | @Override |
| 392 | public void onChange(boolean selfChange) { |
| 393 | updateShowWebSuggestions(); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | private ShowWebSuggestionsSettingChangeObserver |
| 398 | mShowWebSuggestionsSettingChangeObserver; |
| 399 | |
| 400 | // If non-null, then the system is set to show web suggestions, |
| 401 | // and this is the SearchableInfo to use to get them. |
| 402 | private SearchableInfo mSearchableInfo; |
| 403 | |
| 404 | /** |
| 405 | * Check the system settings to see whether web suggestions are |
| 406 | * allowed. If so, store the SearchableInfo to grab suggestions |
| 407 | * while the user is typing. |
| 408 | */ |
| 409 | private void updateShowWebSuggestions() { |
| 410 | mSearchableInfo = null; |
| 411 | Context context = getContext(); |
| 412 | if (Settings.System.getInt(context.getContentResolver(), |
| 413 | Settings.System.SHOW_WEB_SUGGESTIONS, |
| 414 | 1 /* default on */) == 1) { |
Bjorn Bringert | 3274754 | 2010-02-18 21:59:21 +0000 | [diff] [blame] | 415 | ComponentName webSearchComponent = mSearchManager.getWebSearchActivity(); |
| 416 | if (webSearchComponent != null) { |
| 417 | mSearchableInfo = mSearchManager.getSearchableInfo(webSearchComponent); |
Leon Scroggins | 62b71f7 | 2009-06-12 17:51:22 -0400 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 422 | private void fixPicasaBookmark() { |
| 423 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 424 | Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " + |
| 425 | "bookmark = 1 AND url = ?", new String[] { PICASA_URL }); |
| 426 | try { |
| 427 | if (!cursor.moveToFirst()) { |
| 428 | // set "created" so that it will be on the top of the list |
| 429 | db.execSQL("INSERT INTO bookmarks (title, url, visits, " + |
| 430 | "date, created, bookmark)" + " VALUES('" + |
| 431 | getContext().getString(R.string.picasa) + "', '" |
| 432 | + PICASA_URL + "', 0, 0, " + new Date().getTime() |
| 433 | + ", 1);"); |
| 434 | } |
| 435 | } finally { |
| 436 | if (cursor != null) { |
| 437 | cursor.close(); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 442 | /* |
| 443 | * Subclass AbstractCursor so we can combine multiple Cursors and add |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 444 | * "Search the web". |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 445 | * Here are the rules. |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 446 | * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 447 | * "Search the web"; |
| 448 | * 2. If bookmark/history entries has a match, "Search the web" shows up at |
| 449 | * the second place. Otherwise, "Search the web" shows up at the first |
| 450 | * place. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 451 | */ |
| 452 | private class MySuggestionCursor extends AbstractCursor { |
| 453 | private Cursor mHistoryCursor; |
| 454 | private Cursor mSuggestCursor; |
| 455 | private int mHistoryCount; |
| 456 | private int mSuggestionCount; |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 457 | private boolean mIncludeWebSearch; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 458 | private String mString; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 459 | private int mSuggestText1Id; |
| 460 | private int mSuggestText2Id; |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 461 | private int mSuggestText2UrlId; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 462 | private int mSuggestQueryId; |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 463 | private int mSuggestIntentExtraDataId; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 464 | |
| 465 | public MySuggestionCursor(Cursor hc, Cursor sc, String string) { |
| 466 | mHistoryCursor = hc; |
| 467 | mSuggestCursor = sc; |
Grace Kloba | f9b0427 | 2010-06-15 13:36:22 -0700 | [diff] [blame^] | 468 | mHistoryCount = hc != null ? hc.getCount() : 0; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 469 | mSuggestionCount = sc != null ? sc.getCount() : 0; |
| 470 | if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) { |
| 471 | mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount; |
| 472 | } |
| 473 | mString = string; |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 474 | mIncludeWebSearch = string.length() > 0; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 475 | |
| 476 | // Some web suggest providers only give suggestions and have no description string for |
| 477 | // items. The order of the result columns may be different as well. So retrieve the |
| 478 | // column indices for the fields we need now and check before using below. |
| 479 | if (mSuggestCursor == null) { |
| 480 | mSuggestText1Id = -1; |
| 481 | mSuggestText2Id = -1; |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 482 | mSuggestText2UrlId = -1; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 483 | mSuggestQueryId = -1; |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 484 | mSuggestIntentExtraDataId = -1; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 485 | } else { |
| 486 | mSuggestText1Id = mSuggestCursor.getColumnIndex( |
| 487 | SearchManager.SUGGEST_COLUMN_TEXT_1); |
| 488 | mSuggestText2Id = mSuggestCursor.getColumnIndex( |
| 489 | SearchManager.SUGGEST_COLUMN_TEXT_2); |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 490 | mSuggestText2UrlId = mSuggestCursor.getColumnIndex( |
| 491 | SearchManager.SUGGEST_COLUMN_TEXT_2_URL); |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 492 | mSuggestQueryId = mSuggestCursor.getColumnIndex( |
| 493 | SearchManager.SUGGEST_COLUMN_QUERY); |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 494 | mSuggestIntentExtraDataId = mSuggestCursor.getColumnIndex( |
| 495 | SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA); |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 496 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | @Override |
| 500 | public boolean onMove(int oldPosition, int newPosition) { |
| 501 | if (mHistoryCursor == null) { |
| 502 | return false; |
| 503 | } |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 504 | if (mIncludeWebSearch) { |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 505 | if (mHistoryCount == 0 && newPosition == 0) { |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 506 | return true; |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 507 | } else if (mHistoryCount > 0) { |
| 508 | if (newPosition == 0) { |
| 509 | mHistoryCursor.moveToPosition(0); |
| 510 | return true; |
| 511 | } else if (newPosition == 1) { |
| 512 | return true; |
| 513 | } |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 514 | } |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 515 | newPosition--; |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 516 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 517 | if (mHistoryCount > newPosition) { |
| 518 | mHistoryCursor.moveToPosition(newPosition); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 519 | } else { |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 520 | mSuggestCursor.moveToPosition(newPosition - mHistoryCount); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 521 | } |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | @Override |
| 526 | public int getCount() { |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 527 | if (mIncludeWebSearch) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 528 | return mHistoryCount + mSuggestionCount + 1; |
| 529 | } else { |
| 530 | return mHistoryCount + mSuggestionCount; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public String[] getColumnNames() { |
| 536 | return COLUMNS; |
| 537 | } |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 538 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 539 | @Override |
| 540 | public String getString(int columnIndex) { |
| 541 | if ((mPos != -1 && mHistoryCursor != null)) { |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 542 | int type = -1; // 0: web search; 1: history; 2: suggestion |
| 543 | if (mIncludeWebSearch) { |
| 544 | if (mHistoryCount == 0 && mPos == 0) { |
| 545 | type = 0; |
| 546 | } else if (mHistoryCount > 0) { |
| 547 | if (mPos == 0) { |
| 548 | type = 1; |
| 549 | } else if (mPos == 1) { |
| 550 | type = 0; |
| 551 | } |
| 552 | } |
| 553 | if (type == -1) type = (mPos - 1) < mHistoryCount ? 1 : 2; |
| 554 | } else { |
| 555 | type = mPos < mHistoryCount ? 1 : 2; |
| 556 | } |
| 557 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 558 | switch(columnIndex) { |
| 559 | case SUGGEST_COLUMN_INTENT_ACTION_ID: |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 560 | if (type == 1) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 561 | return Intent.ACTION_VIEW; |
| 562 | } else { |
| 563 | return Intent.ACTION_SEARCH; |
| 564 | } |
| 565 | |
| 566 | case SUGGEST_COLUMN_INTENT_DATA_ID: |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 567 | if (type == 1) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 568 | return mHistoryCursor.getString(1); |
| 569 | } else { |
| 570 | return null; |
| 571 | } |
| 572 | |
| 573 | case SUGGEST_COLUMN_TEXT_1_ID: |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 574 | if (type == 0) { |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 575 | return mString; |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 576 | } else if (type == 1) { |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 577 | return getHistoryTitle(); |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 578 | } else { |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 579 | if (mSuggestText1Id == -1) return null; |
| 580 | return mSuggestCursor.getString(mSuggestText1Id); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | case SUGGEST_COLUMN_TEXT_2_ID: |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 584 | if (type == 0) { |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 585 | return getContext().getString(R.string.search_the_web); |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 586 | } else if (type == 1) { |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 587 | return null; // Use TEXT_2_URL instead |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 588 | } else { |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 589 | if (mSuggestText2Id == -1) return null; |
| 590 | return mSuggestCursor.getString(mSuggestText2Id); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 593 | case SUGGEST_COLUMN_TEXT_2_URL_ID: |
| 594 | if (type == 0) { |
| 595 | return null; |
| 596 | } else if (type == 1) { |
| 597 | return getHistoryUrl(); |
| 598 | } else { |
| 599 | if (mSuggestText2UrlId == -1) return null; |
| 600 | return mSuggestCursor.getString(mSuggestText2UrlId); |
| 601 | } |
| 602 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 603 | case SUGGEST_COLUMN_ICON_1_ID: |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 604 | if (type == 1) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 605 | if (mHistoryCursor.getInt(3) == 1) { |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame] | 606 | return Integer.valueOf( |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 607 | R.drawable.ic_search_category_bookmark) |
| 608 | .toString(); |
| 609 | } else { |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame] | 610 | return Integer.valueOf( |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 611 | R.drawable.ic_search_category_history) |
| 612 | .toString(); |
| 613 | } |
| 614 | } else { |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame] | 615 | return Integer.valueOf( |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 616 | R.drawable.ic_search_category_suggest) |
| 617 | .toString(); |
| 618 | } |
| 619 | |
| 620 | case SUGGEST_COLUMN_ICON_2_ID: |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame] | 621 | return "0"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 622 | |
| 623 | case SUGGEST_COLUMN_QUERY_ID: |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 624 | if (type == 0) { |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 625 | return mString; |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 626 | } else if (type == 1) { |
Mike LeBeau | 2af7305 | 2009-06-23 17:36:59 -0700 | [diff] [blame] | 627 | // Return the url in the intent query column. This is ignored |
| 628 | // within the browser because our searchable is set to |
| 629 | // android:searchMode="queryRewriteFromData", but it is used by |
| 630 | // global search for query rewriting. |
| 631 | return mHistoryCursor.getString(1); |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 632 | } else { |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 633 | if (mSuggestQueryId == -1) return null; |
| 634 | return mSuggestCursor.getString(mSuggestQueryId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 635 | } |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 636 | |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 637 | case SUGGEST_COLUMN_INTENT_EXTRA_DATA: |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 638 | if (type == 0) { |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 639 | return null; |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 640 | } else if (type == 1) { |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 641 | return null; |
| 642 | } else { |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 643 | if (mSuggestIntentExtraDataId == -1) return null; |
| 644 | return mSuggestCursor.getString(mSuggestIntentExtraDataId); |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 645 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | return null; |
| 649 | } |
| 650 | |
| 651 | @Override |
| 652 | public double getDouble(int column) { |
| 653 | throw new UnsupportedOperationException(); |
| 654 | } |
| 655 | |
| 656 | @Override |
| 657 | public float getFloat(int column) { |
| 658 | throw new UnsupportedOperationException(); |
| 659 | } |
| 660 | |
| 661 | @Override |
| 662 | public int getInt(int column) { |
| 663 | throw new UnsupportedOperationException(); |
| 664 | } |
| 665 | |
| 666 | @Override |
| 667 | public long getLong(int column) { |
| 668 | if ((mPos != -1) && column == 0) { |
| 669 | return mPos; // use row# as the _Id |
| 670 | } |
| 671 | throw new UnsupportedOperationException(); |
| 672 | } |
| 673 | |
| 674 | @Override |
| 675 | public short getShort(int column) { |
| 676 | throw new UnsupportedOperationException(); |
| 677 | } |
| 678 | |
| 679 | @Override |
| 680 | public boolean isNull(int column) { |
| 681 | throw new UnsupportedOperationException(); |
| 682 | } |
| 683 | |
| 684 | // TODO Temporary change, finalize after jq's changes go in |
| 685 | public void deactivate() { |
| 686 | if (mHistoryCursor != null) { |
| 687 | mHistoryCursor.deactivate(); |
| 688 | } |
| 689 | if (mSuggestCursor != null) { |
| 690 | mSuggestCursor.deactivate(); |
| 691 | } |
| 692 | super.deactivate(); |
| 693 | } |
| 694 | |
| 695 | public boolean requery() { |
| 696 | return (mHistoryCursor != null ? mHistoryCursor.requery() : false) | |
| 697 | (mSuggestCursor != null ? mSuggestCursor.requery() : false); |
| 698 | } |
| 699 | |
| 700 | // TODO Temporary change, finalize after jq's changes go in |
| 701 | public void close() { |
| 702 | super.close(); |
| 703 | if (mHistoryCursor != null) { |
| 704 | mHistoryCursor.close(); |
| 705 | mHistoryCursor = null; |
| 706 | } |
| 707 | if (mSuggestCursor != null) { |
| 708 | mSuggestCursor.close(); |
| 709 | mSuggestCursor = null; |
| 710 | } |
| 711 | } |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 712 | |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 713 | /** |
| 714 | * Provides the title (text line 1) for a browser suggestion, which should be the |
| 715 | * webpage title. If the webpage title is empty, returns the stripped url instead. |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 716 | * |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 717 | * @return the title string to use |
| 718 | */ |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 719 | private String getHistoryTitle() { |
| 720 | String title = mHistoryCursor.getString(2 /* webpage title */); |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 721 | if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) { |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 722 | title = stripUrl(mHistoryCursor.getString(1 /* url */)); |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 723 | } |
| 724 | return title; |
| 725 | } |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 726 | |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 727 | /** |
| 728 | * Provides the subtitle (text line 2) for a browser suggestion, which should be the |
| 729 | * webpage url. If the webpage title is empty, then the url should go in the title |
| 730 | * instead, and the subtitle should be empty, so this would return null. |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 731 | * |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 732 | * @return the subtitle string to use, or null if none |
| 733 | */ |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 734 | private String getHistoryUrl() { |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 735 | String title = mHistoryCursor.getString(2 /* webpage title */); |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 736 | if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) { |
| 737 | return null; |
| 738 | } else { |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 739 | return stripUrl(mHistoryCursor.getString(1 /* url */)); |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 740 | } |
| 741 | } |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 742 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 743 | } |
| 744 | |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 745 | private static class ResultsCursor extends AbstractCursor { |
| 746 | // Array indices for RESULTS_COLUMNS |
| 747 | private static final int RESULT_ACTION_ID = 1; |
| 748 | private static final int RESULT_DATA_ID = 2; |
| 749 | private static final int RESULT_TEXT_ID = 3; |
| 750 | private static final int RESULT_ICON_ID = 4; |
| 751 | private static final int RESULT_EXTRA_ID = 5; |
| 752 | |
| 753 | private static final String[] RESULTS_COLUMNS = new String[] { |
| 754 | "_id", |
| 755 | SearchManager.SUGGEST_COLUMN_INTENT_ACTION, |
| 756 | SearchManager.SUGGEST_COLUMN_INTENT_DATA, |
| 757 | SearchManager.SUGGEST_COLUMN_TEXT_1, |
| 758 | SearchManager.SUGGEST_COLUMN_ICON_1, |
| 759 | SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA |
| 760 | }; |
| 761 | private final ArrayList<String> mResults; |
| 762 | public ResultsCursor(ArrayList<String> results) { |
| 763 | mResults = results; |
| 764 | } |
| 765 | public int getCount() { return mResults.size(); } |
| 766 | |
| 767 | public String[] getColumnNames() { |
| 768 | return RESULTS_COLUMNS; |
| 769 | } |
| 770 | |
| 771 | public String getString(int column) { |
| 772 | switch (column) { |
| 773 | case RESULT_ACTION_ID: |
Leon Scroggins | a1cc3fd | 2010-02-01 16:14:11 -0500 | [diff] [blame] | 774 | return RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 775 | case RESULT_TEXT_ID: |
| 776 | // The data is used when the phone is in landscape mode. We |
| 777 | // still want to show the result string. |
| 778 | case RESULT_DATA_ID: |
| 779 | return mResults.get(mPos); |
| 780 | case RESULT_EXTRA_ID: |
| 781 | // The Intent's extra data will store the index into |
| 782 | // mResults so the BrowserActivity will know which result to |
| 783 | // use. |
| 784 | return Integer.toString(mPos); |
| 785 | case RESULT_ICON_ID: |
| 786 | return Integer.valueOf(R.drawable.magnifying_glass) |
| 787 | .toString(); |
| 788 | default: |
| 789 | return null; |
| 790 | } |
| 791 | } |
| 792 | public short getShort(int column) { |
| 793 | throw new UnsupportedOperationException(); |
| 794 | } |
| 795 | public int getInt(int column) { |
| 796 | throw new UnsupportedOperationException(); |
| 797 | } |
| 798 | public long getLong(int column) { |
| 799 | if ((mPos != -1) && column == 0) { |
| 800 | return mPos; // use row# as the _id |
| 801 | } |
| 802 | throw new UnsupportedOperationException(); |
| 803 | } |
| 804 | public float getFloat(int column) { |
| 805 | throw new UnsupportedOperationException(); |
| 806 | } |
| 807 | public double getDouble(int column) { |
| 808 | throw new UnsupportedOperationException(); |
| 809 | } |
| 810 | public boolean isNull(int column) { |
| 811 | throw new UnsupportedOperationException(); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | private ResultsCursor mResultsCursor; |
| 816 | |
| 817 | /** |
| 818 | * Provide a set of results to be returned to query, intended to be used |
| 819 | * by the SearchDialog when the BrowserActivity is in voice search mode. |
| 820 | * @param results Strings to display in the dropdown from the SearchDialog |
| 821 | */ |
| 822 | /* package */ void setQueryResults(ArrayList<String> results) { |
| 823 | if (results == null) { |
| 824 | mResultsCursor = null; |
| 825 | } else { |
| 826 | mResultsCursor = new ResultsCursor(results); |
| 827 | } |
| 828 | } |
| 829 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 830 | @Override |
| 831 | public Cursor query(Uri url, String[] projectionIn, String selection, |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 832 | String[] selectionArgs, String sortOrder) |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 833 | throws IllegalStateException { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 834 | int match = URI_MATCHER.match(url); |
| 835 | if (match == -1) { |
| 836 | throw new IllegalArgumentException("Unknown URL"); |
| 837 | } |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 838 | if (match == URI_MATCH_SUGGEST && mResultsCursor != null) { |
| 839 | Cursor results = mResultsCursor; |
| 840 | mResultsCursor = null; |
| 841 | return results; |
| 842 | } |
| 843 | SQLiteDatabase db = mOpenHelper.getReadableDatabase(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 844 | |
Bjorn Bringert | 346dafb | 2009-04-29 21:41:47 +0100 | [diff] [blame] | 845 | if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 846 | String suggestSelection; |
| 847 | String [] myArgs; |
| 848 | if (selectionArgs[0] == null || selectionArgs[0].equals("")) { |
Grace Kloba | f9b0427 | 2010-06-15 13:36:22 -0700 | [diff] [blame^] | 849 | return new MySuggestionCursor(null, null, ""); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 850 | } else { |
| 851 | String like = selectionArgs[0] + "%"; |
Leon Scroggins | faa15db | 2009-04-03 10:16:06 -0700 | [diff] [blame] | 852 | if (selectionArgs[0].startsWith("http") |
| 853 | || selectionArgs[0].startsWith("file")) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 854 | myArgs = new String[1]; |
| 855 | myArgs[0] = like; |
| 856 | suggestSelection = selection; |
| 857 | } else { |
| 858 | SUGGEST_ARGS[0] = "http://" + like; |
| 859 | SUGGEST_ARGS[1] = "http://www." + like; |
| 860 | SUGGEST_ARGS[2] = "https://" + like; |
| 861 | SUGGEST_ARGS[3] = "https://www." + like; |
Leon Scroggins | bd359cc | 2009-05-26 15:57:35 -0400 | [diff] [blame] | 862 | // To match against titles. |
| 863 | SUGGEST_ARGS[4] = like; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 864 | myArgs = SUGGEST_ARGS; |
| 865 | suggestSelection = SUGGEST_SELECTION; |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS], |
| 870 | SUGGEST_PROJECTION, suggestSelection, myArgs, null, null, |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame] | 871 | ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 872 | |
Bjorn Bringert | 346dafb | 2009-04-29 21:41:47 +0100 | [diff] [blame] | 873 | if (match == URI_MATCH_BOOKMARKS_SUGGEST |
Dan Egnor | 5ee906c | 2009-11-18 12:11:49 -0800 | [diff] [blame] | 874 | || Patterns.WEB_URL.matcher(selectionArgs[0]).matches()) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 875 | return new MySuggestionCursor(c, null, ""); |
| 876 | } else { |
| 877 | // get Google suggest if there is still space in the list |
| 878 | if (myArgs != null && myArgs.length > 1 |
Leon Scroggins | 62b71f7 | 2009-06-12 17:51:22 -0400 | [diff] [blame] | 879 | && mSearchableInfo != null |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 880 | && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) { |
Bjorn Bringert | d8b0ad2 | 2009-06-22 10:36:29 +0100 | [diff] [blame] | 881 | Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]); |
Leon Scroggins | 62b71f7 | 2009-06-12 17:51:22 -0400 | [diff] [blame] | 882 | return new MySuggestionCursor(c, sc, selectionArgs[0]); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 883 | } |
| 884 | return new MySuggestionCursor(c, null, selectionArgs[0]); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | String[] projection = null; |
| 889 | if (projectionIn != null && projectionIn.length > 0) { |
| 890 | projection = new String[projectionIn.length + 1]; |
| 891 | System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length); |
| 892 | projection[projectionIn.length] = "_id AS _id"; |
| 893 | } |
| 894 | |
| 895 | StringBuilder whereClause = new StringBuilder(256); |
| 896 | if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) { |
| 897 | whereClause.append("(_id = ").append(url.getPathSegments().get(1)) |
| 898 | .append(")"); |
| 899 | } |
| 900 | |
| 901 | // Tack on the user's selection, if present |
| 902 | if (selection != null && selection.length() > 0) { |
| 903 | if (whereClause.length() > 0) { |
| 904 | whereClause.append(" AND "); |
| 905 | } |
| 906 | |
| 907 | whereClause.append('('); |
| 908 | whereClause.append(selection); |
| 909 | whereClause.append(')'); |
| 910 | } |
| 911 | Cursor c = db.query(TABLE_NAMES[match % 10], projection, |
| 912 | whereClause.toString(), selectionArgs, null, null, sortOrder, |
| 913 | null); |
| 914 | c.setNotificationUri(getContext().getContentResolver(), url); |
| 915 | return c; |
| 916 | } |
| 917 | |
| 918 | @Override |
| 919 | public String getType(Uri url) { |
| 920 | int match = URI_MATCHER.match(url); |
| 921 | switch (match) { |
| 922 | case URI_MATCH_BOOKMARKS: |
| 923 | return "vnd.android.cursor.dir/bookmark"; |
| 924 | |
| 925 | case URI_MATCH_BOOKMARKS_ID: |
| 926 | return "vnd.android.cursor.item/bookmark"; |
| 927 | |
| 928 | case URI_MATCH_SEARCHES: |
| 929 | return "vnd.android.cursor.dir/searches"; |
| 930 | |
| 931 | case URI_MATCH_SEARCHES_ID: |
| 932 | return "vnd.android.cursor.item/searches"; |
| 933 | |
| 934 | case URI_MATCH_SUGGEST: |
| 935 | return SearchManager.SUGGEST_MIME_TYPE; |
| 936 | |
| 937 | default: |
| 938 | throw new IllegalArgumentException("Unknown URL"); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | @Override |
| 943 | public Uri insert(Uri url, ContentValues initialValues) { |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 944 | boolean isBookmarkTable = false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 945 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 946 | |
| 947 | int match = URI_MATCHER.match(url); |
| 948 | Uri uri = null; |
| 949 | switch (match) { |
| 950 | case URI_MATCH_BOOKMARKS: { |
| 951 | // Insert into the bookmarks table |
| 952 | long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url", |
| 953 | initialValues); |
| 954 | if (rowID > 0) { |
| 955 | uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI, |
| 956 | rowID); |
| 957 | } |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 958 | isBookmarkTable = true; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 959 | break; |
| 960 | } |
| 961 | |
| 962 | case URI_MATCH_SEARCHES: { |
| 963 | // Insert into the searches table |
| 964 | long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url", |
| 965 | initialValues); |
| 966 | if (rowID > 0) { |
| 967 | uri = ContentUris.withAppendedId(Browser.SEARCHES_URI, |
| 968 | rowID); |
| 969 | } |
| 970 | break; |
| 971 | } |
| 972 | |
| 973 | default: |
| 974 | throw new IllegalArgumentException("Unknown URL"); |
| 975 | } |
| 976 | |
| 977 | if (uri == null) { |
| 978 | throw new IllegalArgumentException("Unknown URL"); |
| 979 | } |
| 980 | getContext().getContentResolver().notifyChange(uri, null); |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 981 | |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 982 | // Back up the new bookmark set if we just inserted one. |
| 983 | // A row created when bookmarks are added from scratch will have |
| 984 | // bookmark=1 in the initial value set. |
| 985 | if (isBookmarkTable |
| 986 | && initialValues.containsKey(BookmarkColumns.BOOKMARK) |
| 987 | && initialValues.getAsInteger(BookmarkColumns.BOOKMARK) != 0) { |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 988 | mBackupManager.dataChanged(); |
| 989 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 990 | return uri; |
| 991 | } |
| 992 | |
| 993 | @Override |
| 994 | public int delete(Uri url, String where, String[] whereArgs) { |
| 995 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 996 | |
| 997 | int match = URI_MATCHER.match(url); |
| 998 | if (match == -1 || match == URI_MATCH_SUGGEST) { |
| 999 | throw new IllegalArgumentException("Unknown URL"); |
| 1000 | } |
| 1001 | |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1002 | // need to know whether it's the bookmarks table for a couple of reasons |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 1003 | boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID); |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1004 | String id = null; |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 1005 | |
| 1006 | if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1007 | StringBuilder sb = new StringBuilder(); |
| 1008 | if (where != null && where.length() > 0) { |
| 1009 | sb.append("( "); |
| 1010 | sb.append(where); |
| 1011 | sb.append(" ) AND "); |
| 1012 | } |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1013 | id = url.getPathSegments().get(1); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1014 | sb.append("_id = "); |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1015 | sb.append(id); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1016 | where = sb.toString(); |
| 1017 | } |
| 1018 | |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1019 | ContentResolver cr = getContext().getContentResolver(); |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 1020 | |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1021 | // we'lll need to back up the bookmark set if we are about to delete one |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 1022 | if (isBookmarkTable) { |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1023 | Cursor cursor = cr.query(Browser.BOOKMARKS_URI, |
| 1024 | new String[] { BookmarkColumns.BOOKMARK }, |
| 1025 | "_id = " + id, null, null); |
| 1026 | if (cursor.moveToNext()) { |
| 1027 | if (cursor.getInt(0) != 0) { |
| 1028 | // yep, this record is a bookmark |
| 1029 | mBackupManager.dataChanged(); |
| 1030 | } |
| 1031 | } |
| 1032 | cursor.close(); |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 1033 | } |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1034 | |
| 1035 | int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs); |
| 1036 | cr.notifyChange(url, null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1037 | return count; |
| 1038 | } |
| 1039 | |
| 1040 | @Override |
| 1041 | public int update(Uri url, ContentValues values, String where, |
| 1042 | String[] whereArgs) { |
| 1043 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 1044 | |
| 1045 | int match = URI_MATCHER.match(url); |
| 1046 | if (match == -1 || match == URI_MATCH_SUGGEST) { |
| 1047 | throw new IllegalArgumentException("Unknown URL"); |
| 1048 | } |
| 1049 | |
Leon Scroggins | f2463ae | 2010-02-23 14:28:51 -0500 | [diff] [blame] | 1050 | if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1051 | StringBuilder sb = new StringBuilder(); |
| 1052 | if (where != null && where.length() > 0) { |
| 1053 | sb.append("( "); |
| 1054 | sb.append(where); |
| 1055 | sb.append(" ) AND "); |
| 1056 | } |
Leon Scroggins | f2463ae | 2010-02-23 14:28:51 -0500 | [diff] [blame] | 1057 | String id = url.getPathSegments().get(1); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1058 | sb.append("_id = "); |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1059 | sb.append(id); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1060 | where = sb.toString(); |
| 1061 | } |
| 1062 | |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1063 | ContentResolver cr = getContext().getContentResolver(); |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 1064 | |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1065 | // Not all bookmark-table updates should be backed up. Look to see |
| 1066 | // whether we changed the title, url, or "is a bookmark" state, and |
| 1067 | // request a backup if so. |
Leon Scroggins | f2463ae | 2010-02-23 14:28:51 -0500 | [diff] [blame] | 1068 | if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_BOOKMARKS) { |
| 1069 | boolean changingBookmarks = false; |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1070 | // Alterations to the bookmark field inherently change the bookmark |
| 1071 | // set, so we don't need to query the record; we know a priori that |
| 1072 | // we will need to back up this change. |
| 1073 | if (values.containsKey(BookmarkColumns.BOOKMARK)) { |
| 1074 | changingBookmarks = true; |
Leon Scroggins | f2463ae | 2010-02-23 14:28:51 -0500 | [diff] [blame] | 1075 | } else if ((values.containsKey(BookmarkColumns.TITLE) |
| 1076 | || values.containsKey(BookmarkColumns.URL)) |
| 1077 | && values.containsKey(BookmarkColumns._ID)) { |
| 1078 | // If a title or URL has been changed, check to see if it is to |
| 1079 | // a bookmark. The ID should have been included in the update, |
| 1080 | // so use it. |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1081 | Cursor cursor = cr.query(Browser.BOOKMARKS_URI, |
| 1082 | new String[] { BookmarkColumns.BOOKMARK }, |
Leon Scroggins | f2463ae | 2010-02-23 14:28:51 -0500 | [diff] [blame] | 1083 | BookmarkColumns._ID + " = " |
| 1084 | + values.getAsString(BookmarkColumns._ID), null, null); |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1085 | if (cursor.moveToNext()) { |
| 1086 | changingBookmarks = (cursor.getInt(0) != 0); |
| 1087 | } |
| 1088 | cursor.close(); |
| 1089 | } |
| 1090 | |
| 1091 | // if this *is* a bookmark row we're altering, we need to back it up. |
| 1092 | if (changingBookmarks) { |
| 1093 | mBackupManager.dataChanged(); |
| 1094 | } |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 1095 | } |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1096 | |
| 1097 | int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs); |
| 1098 | cr.notifyChange(url, null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1099 | return ret; |
| 1100 | } |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 1101 | |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 1102 | /** |
| 1103 | * Strips the provided url of preceding "http://" and any trailing "/". Does not |
| 1104 | * strip "https://". If the provided string cannot be stripped, the original string |
| 1105 | * is returned. |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 1106 | * |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 1107 | * TODO: Put this in TextUtils to be used by other packages doing something similar. |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 1108 | * |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 1109 | * @param url a url to strip, like "http://www.google.com/" |
| 1110 | * @return a stripped url like "www.google.com", or the original string if it could |
| 1111 | * not be stripped |
| 1112 | */ |
| 1113 | private static String stripUrl(String url) { |
| 1114 | if (url == null) return null; |
| 1115 | Matcher m = STRIP_URL_PATTERN.matcher(url); |
| 1116 | if (m.matches() && m.groupCount() == 3) { |
| 1117 | return m.group(2); |
| 1118 | } else { |
| 1119 | return url; |
| 1120 | } |
| 1121 | } |
| 1122 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1123 | } |