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 | |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 19 | import com.android.browser.search.SearchEngine; |
| 20 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 21 | import android.app.SearchManager; |
Christopher Tate | b6a6544 | 2010-03-05 15:47:48 -0800 | [diff] [blame] | 22 | import android.app.backup.BackupManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 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 | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 30 | import android.content.SharedPreferences.Editor; |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 31 | import android.content.UriMatcher; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 32 | import android.database.AbstractCursor; |
| 33 | import android.database.Cursor; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 34 | import android.database.sqlite.SQLiteDatabase; |
Bjorn Bringert | bcd20b3 | 2009-04-29 21:52:09 +0100 | [diff] [blame] | 35 | import android.database.sqlite.SQLiteOpenHelper; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 36 | import android.net.Uri; |
Andrei Popescu | 93bea96 | 2010-03-23 15:04:36 +0000 | [diff] [blame] | 37 | import android.os.Process; |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 38 | import android.preference.PreferenceManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 39 | import android.provider.Browser; |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 40 | import android.provider.Browser.BookmarkColumns; |
Leon Scroggins | a1cc3fd | 2010-02-01 16:14:11 -0500 | [diff] [blame] | 41 | import android.speech.RecognizerResultsIntent; |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 42 | import android.text.TextUtils; |
Bjorn Bringert | bcd20b3 | 2009-04-29 21:52:09 +0100 | [diff] [blame] | 43 | import android.util.Log; |
Dianne Hackborn | 385effd | 2010-02-24 20:03:04 -0800 | [diff] [blame] | 44 | import android.util.Patterns; |
Dan Egnor | 5ee906c | 2009-11-18 12:11:49 -0800 | [diff] [blame] | 45 | |
Andrei Popescu | 1b20b9d | 2009-09-21 18:49:42 +0100 | [diff] [blame] | 46 | import java.io.File; |
| 47 | import java.io.FilenameFilter; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 48 | import java.util.ArrayList; |
Bjorn Bringert | bcd20b3 | 2009-04-29 21:52:09 +0100 | [diff] [blame] | 49 | import java.util.Date; |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 50 | import java.util.regex.Matcher; |
| 51 | import java.util.regex.Pattern; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 52 | |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 53 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 54 | public class BrowserProvider extends ContentProvider { |
| 55 | |
| 56 | private SQLiteOpenHelper mOpenHelper; |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 57 | private BackupManager mBackupManager; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 58 | 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 Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 62 | private static final String PICASA_URL = "http://picasaweb.google.com/m/" + |
| 63 | "viewer?source=androidclient"; |
| 64 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 65 | private static final String[] TABLE_NAMES = new String[] { |
Bjorn Bringert | a761181 | 2010-03-24 11:12:02 +0000 | [diff] [blame] | 66 | "bookmarks", "searches" |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 67 | }; |
| 68 | private static final String[] SUGGEST_PROJECTION = new String[] { |
Leon Scroggins | b446443 | 2009-11-25 12:37:50 -0500 | [diff] [blame] | 69 | "_id", "url", "title", "bookmark", "user_entered" |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 70 | }; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 71 | private static final String SUGGEST_SELECTION = |
Leon Scroggins | b446443 | 2009-11-25 12:37:50 -0500 | [diff] [blame] | 72 | "(url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?" |
| 73 | + " OR title LIKE ?) AND (bookmark = 1 OR user_entered = 1)"; |
Leon Scroggins | bd359cc | 2009-05-26 15:57:35 -0400 | [diff] [blame] | 74 | private String[] SUGGEST_ARGS = new String[5]; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 75 | |
| 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 Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 81 | 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 Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 85 | 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] | 86 | |
| 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 Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 94 | SearchManager.SUGGEST_COLUMN_TEXT_2_URL, |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 95 | SearchManager.SUGGEST_COLUMN_ICON_1, |
| 96 | SearchManager.SUGGEST_COLUMN_ICON_2, |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 97 | SearchManager.SUGGEST_COLUMN_QUERY, |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 98 | SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA}; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 99 | |
| 100 | private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3; |
| 101 | private static final int MAX_SUGGESTION_LONG_ENTRIES = 6; |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame] | 102 | private static final String MAX_SUGGESTION_LONG_ENTRIES_STRING = |
| 103 | Integer.valueOf(MAX_SUGGESTION_LONG_ENTRIES).toString(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 104 | |
| 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 Bringert | 346dafb | 2009-04-29 21:41:47 +0100 | [diff] [blame] | 113 | private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 114 | |
| 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 Bringert | 346dafb | 2009-04-29 21:41:47 +0100 | [diff] [blame] | 129 | URI_MATCHER.addURI("browser", |
| 130 | TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY, |
| 131 | URI_MATCH_BOOKMARKS_SUGGEST); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 132 | } |
| 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 Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 151 | // 19 -> 20 Added thumbnail |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 152 | // 20 -> 21 Added touch_icon |
Grace Kloba | 6b52a55 | 2009-09-03 16:29:56 -0700 | [diff] [blame] | 153 | // 21 -> 22 Remove "clientid" |
Leon Scroggins | b446443 | 2009-11-25 12:37:50 -0500 | [diff] [blame] | 154 | // 22 -> 23 Added user_entered |
| 155 | private static final int DATABASE_VERSION = 23; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 156 | |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 157 | // 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 Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 160 | |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 161 | private BrowserSettings mSettings; |
Bjorn Bringert | d8b0ad2 | 2009-06-22 10:36:29 +0100 | [diff] [blame] | 162 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 163 | public BrowserProvider() { |
| 164 | } |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 165 | |
Patrick Scott | 4391469 | 2010-02-19 10:10:10 -0500 | [diff] [blame] | 166 | // 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 186 | |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 187 | private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 188 | StringBuffer sb = new StringBuffer(); |
| 189 | int lastCharLoc = 0; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 190 | |
Patrick Scott | 4391469 | 2010-02-19 10:10:10 -0500 | [diff] [blame] | 191 | final String client_id = getClientId(context.getContentResolver()); |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 192 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 193 | 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 Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 203 | if (propertyKeyValue.equals("CLIENT_ID")) { |
| 204 | sb.append(client_id); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 205 | } else { |
Ramanan Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 206 | sb.append("unknown"); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 207 | } |
| 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 Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 241 | "favicon BLOB DEFAULT NULL," + |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 242 | "thumbnail BLOB DEFAULT NULL," + |
Leon Scroggins | b446443 | 2009-11-25 12:37:50 -0500 | [diff] [blame] | 243 | "touch_icon BLOB DEFAULT NULL," + |
| 244 | "user_entered INTEGER" + |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 245 | ");"); |
| 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 Rajeswaran | dd4f429 | 2009-03-24 20:41:19 -0700 | [diff] [blame] | 252 | CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 253 | db.execSQL("INSERT INTO bookmarks (title, url, visits, " + |
| 254 | "date, created, bookmark)" + " VALUES('" + |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 255 | bookmarks[i] + "', '" + bookmarkDestination + |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 256 | "', 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 Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 271 | + newVersion); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 272 | if (oldVersion == 18) { |
| 273 | db.execSQL("DROP TABLE IF EXISTS labels"); |
Leon Scroggins | b6b7f9e | 2009-06-18 12:05:28 -0400 | [diff] [blame] | 274 | } |
| 275 | if (oldVersion <= 19) { |
| 276 | db.execSQL("ALTER TABLE bookmarks ADD COLUMN thumbnail BLOB DEFAULT NULL;"); |
Patrick Scott | 3918d44 | 2009-08-04 13:22:29 -0400 | [diff] [blame] | 277 | } |
| 278 | if (oldVersion < 21) { |
| 279 | db.execSQL("ALTER TABLE bookmarks ADD COLUMN touch_icon BLOB DEFAULT NULL;"); |
Grace Kloba | 6b52a55 | 2009-09-03 16:29:56 -0700 | [diff] [blame] | 280 | } |
| 281 | if (oldVersion < 22) { |
| 282 | 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] | 283 | removeGears(); |
Leon Scroggins | b446443 | 2009-11-25 12:37:50 -0500 | [diff] [blame] | 284 | } |
| 285 | if (oldVersion < 23) { |
| 286 | 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] | 287 | } else { |
| 288 | db.execSQL("DROP TABLE IF EXISTS bookmarks"); |
| 289 | db.execSQL("DROP TABLE IF EXISTS searches"); |
| 290 | onCreate(db); |
| 291 | } |
| 292 | } |
Andrei Popescu | 1b20b9d | 2009-09-21 18:49:42 +0100 | [diff] [blame] | 293 | |
| 294 | private void removeGears() { |
Andrei Popescu | 93bea96 | 2010-03-23 15:04:36 +0000 | [diff] [blame] | 295 | new Thread() { |
| 296 | @Override |
| 297 | public void run() { |
| 298 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Andrei Popescu | 1b20b9d | 2009-09-21 18:49:42 +0100 | [diff] [blame] | 299 | 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 Popescu | 93bea96 | 2010-03-23 15:04:36 +0000 | [diff] [blame] | 305 | return; |
Andrei Popescu | 1b20b9d | 2009-09-21 18:49:42 +0100 | [diff] [blame] | 306 | } |
| 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 Popescu | 93bea96 | 2010-03-23 15:04:36 +0000 | [diff] [blame] | 324 | return; |
Andrei Popescu | 1b20b9d | 2009-09-21 18:49:42 +0100 | [diff] [blame] | 325 | } |
| 326 | deleteDirectory(gearsDataDir); |
Andrei Popescu | 1b20b9d | 2009-09-21 18:49:42 +0100 | [diff] [blame] | 327 | } |
| 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 Popescu | 93bea96 | 2010-03-23 15:04:36 +0000 | [diff] [blame] | 339 | }.start(); |
Andrei Popescu | 1b20b9d | 2009-09-21 18:49:42 +0100 | [diff] [blame] | 340 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public boolean onCreate() { |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 345 | final Context context = getContext(); |
| 346 | mOpenHelper = new DatabaseHelper(context); |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 347 | mBackupManager = new BackupManager(context); |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 348 | // 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] | 349 | // 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 Fitzpatrick | 1a6e0e8 | 2010-09-01 16:29:03 -0700 | [diff] [blame] | 359 | ed.apply(); |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 362 | mSettings = BrowserSettings.getInstance(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 363 | return true; |
| 364 | } |
| 365 | |
The Android Open Source Project | a3c0aab | 2009-03-18 17:39:48 -0700 | [diff] [blame] | 366 | 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 386 | /* |
| 387 | * Subclass AbstractCursor so we can combine multiple Cursors and add |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 388 | * "Search the web". |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 389 | * Here are the rules. |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 390 | * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 391 | * "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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 395 | */ |
| 396 | private class MySuggestionCursor extends AbstractCursor { |
| 397 | private Cursor mHistoryCursor; |
| 398 | private Cursor mSuggestCursor; |
| 399 | private int mHistoryCount; |
| 400 | private int mSuggestionCount; |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 401 | private boolean mIncludeWebSearch; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 402 | private String mString; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 403 | private int mSuggestText1Id; |
| 404 | private int mSuggestText2Id; |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 405 | private int mSuggestText2UrlId; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 406 | private int mSuggestQueryId; |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 407 | private int mSuggestIntentExtraDataId; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 408 | |
| 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 Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 418 | mIncludeWebSearch = string.length() > 0; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 419 | |
| 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 Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 426 | mSuggestText2UrlId = -1; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 427 | mSuggestQueryId = -1; |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 428 | mSuggestIntentExtraDataId = -1; |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 429 | } else { |
| 430 | mSuggestText1Id = mSuggestCursor.getColumnIndex( |
| 431 | SearchManager.SUGGEST_COLUMN_TEXT_1); |
| 432 | mSuggestText2Id = mSuggestCursor.getColumnIndex( |
| 433 | SearchManager.SUGGEST_COLUMN_TEXT_2); |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 434 | mSuggestText2UrlId = mSuggestCursor.getColumnIndex( |
| 435 | SearchManager.SUGGEST_COLUMN_TEXT_2_URL); |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 436 | mSuggestQueryId = mSuggestCursor.getColumnIndex( |
| 437 | SearchManager.SUGGEST_COLUMN_QUERY); |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 438 | mSuggestIntentExtraDataId = mSuggestCursor.getColumnIndex( |
| 439 | SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA); |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 440 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public boolean onMove(int oldPosition, int newPosition) { |
| 445 | if (mHistoryCursor == null) { |
| 446 | return false; |
| 447 | } |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 448 | if (mIncludeWebSearch) { |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 449 | if (mHistoryCount == 0 && newPosition == 0) { |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 450 | return true; |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 451 | } 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 Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 458 | } |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 459 | newPosition--; |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 460 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 461 | if (mHistoryCount > newPosition) { |
| 462 | mHistoryCursor.moveToPosition(newPosition); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 463 | } else { |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 464 | mSuggestCursor.moveToPosition(newPosition - mHistoryCount); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 465 | } |
| 466 | return true; |
| 467 | } |
| 468 | |
| 469 | @Override |
| 470 | public int getCount() { |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 471 | if (mIncludeWebSearch) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 472 | return mHistoryCount + mSuggestionCount + 1; |
| 473 | } else { |
| 474 | return mHistoryCount + mSuggestionCount; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public String[] getColumnNames() { |
| 480 | return COLUMNS; |
| 481 | } |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 482 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 483 | @Override |
| 484 | public String getString(int columnIndex) { |
| 485 | if ((mPos != -1 && mHistoryCursor != null)) { |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 486 | 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 502 | switch(columnIndex) { |
| 503 | case SUGGEST_COLUMN_INTENT_ACTION_ID: |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 504 | if (type == 1) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 505 | return Intent.ACTION_VIEW; |
| 506 | } else { |
| 507 | return Intent.ACTION_SEARCH; |
| 508 | } |
| 509 | |
| 510 | case SUGGEST_COLUMN_INTENT_DATA_ID: |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 511 | if (type == 1) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 512 | return mHistoryCursor.getString(1); |
| 513 | } else { |
| 514 | return null; |
| 515 | } |
| 516 | |
| 517 | case SUGGEST_COLUMN_TEXT_1_ID: |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 518 | if (type == 0) { |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 519 | return mString; |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 520 | } else if (type == 1) { |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 521 | return getHistoryTitle(); |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 522 | } else { |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 523 | if (mSuggestText1Id == -1) return null; |
| 524 | return mSuggestCursor.getString(mSuggestText1Id); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | case SUGGEST_COLUMN_TEXT_2_ID: |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 528 | if (type == 0) { |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 529 | return getContext().getString(R.string.search_the_web); |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 530 | } else if (type == 1) { |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 531 | return null; // Use TEXT_2_URL instead |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 532 | } else { |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 533 | if (mSuggestText2Id == -1) return null; |
| 534 | return mSuggestCursor.getString(mSuggestText2Id); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 535 | } |
| 536 | |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 537 | 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 547 | case SUGGEST_COLUMN_ICON_1_ID: |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 548 | if (type == 1) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 549 | if (mHistoryCursor.getInt(3) == 1) { |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame] | 550 | return Integer.valueOf( |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 551 | R.drawable.ic_search_category_bookmark) |
| 552 | .toString(); |
| 553 | } else { |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame] | 554 | return Integer.valueOf( |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 555 | R.drawable.ic_search_category_history) |
| 556 | .toString(); |
| 557 | } |
| 558 | } else { |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame] | 559 | return Integer.valueOf( |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 560 | R.drawable.ic_search_category_suggest) |
| 561 | .toString(); |
| 562 | } |
| 563 | |
| 564 | case SUGGEST_COLUMN_ICON_2_ID: |
Leon Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame] | 565 | return "0"; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 566 | |
| 567 | case SUGGEST_COLUMN_QUERY_ID: |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 568 | if (type == 0) { |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 569 | return mString; |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 570 | } else if (type == 1) { |
Mike LeBeau | 2af7305 | 2009-06-23 17:36:59 -0700 | [diff] [blame] | 571 | // 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 Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 576 | } else { |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 577 | if (mSuggestQueryId == -1) return null; |
| 578 | return mSuggestCursor.getString(mSuggestQueryId); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 579 | } |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 580 | |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 581 | case SUGGEST_COLUMN_INTENT_EXTRA_DATA: |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 582 | if (type == 0) { |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 583 | return null; |
Grace Kloba | 391df7c | 2010-03-01 19:51:49 -0800 | [diff] [blame] | 584 | } else if (type == 1) { |
Grace Kloba | d3992d4 | 2010-01-28 11:44:38 -0800 | [diff] [blame] | 585 | return null; |
| 586 | } else { |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 587 | if (mSuggestIntentExtraDataId == -1) return null; |
| 588 | return mSuggestCursor.getString(mSuggestIntentExtraDataId); |
Bjorn Bringert | 0485170 | 2009-09-22 10:36:01 +0100 | [diff] [blame] | 589 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 590 | } |
| 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 Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 656 | |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 657 | /** |
| 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 Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 660 | * |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 661 | * @return the title string to use |
| 662 | */ |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 663 | private String getHistoryTitle() { |
| 664 | String title = mHistoryCursor.getString(2 /* webpage title */); |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 665 | if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) { |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 666 | title = stripUrl(mHistoryCursor.getString(1 /* url */)); |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 667 | } |
| 668 | return title; |
| 669 | } |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 670 | |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 671 | /** |
| 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 Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 675 | * |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 676 | * @return the subtitle string to use, or null if none |
| 677 | */ |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 678 | private String getHistoryUrl() { |
Mike LeBeau | 1ef26a3 | 2009-05-13 20:11:00 -0700 | [diff] [blame] | 679 | String title = mHistoryCursor.getString(2 /* webpage title */); |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 680 | if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) { |
| 681 | return null; |
| 682 | } else { |
Bjorn Bringert | c7c0fce | 2010-03-02 11:20:29 +0000 | [diff] [blame] | 683 | return stripUrl(mHistoryCursor.getString(1 /* url */)); |
Mike LeBeau | 21beb13 | 2009-05-13 14:57:50 -0700 | [diff] [blame] | 684 | } |
| 685 | } |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 686 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 689 | 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 Scroggins | a1cc3fd | 2010-02-01 16:14:11 -0500 | [diff] [blame] | 718 | return RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 719 | 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 774 | @Override |
| 775 | public Cursor query(Uri url, String[] projectionIn, String selection, |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 776 | String[] selectionArgs, String sortOrder) |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 777 | throws IllegalStateException { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 778 | int match = URI_MATCHER.match(url); |
| 779 | if (match == -1) { |
| 780 | throw new IllegalArgumentException("Unknown URL"); |
| 781 | } |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 782 | 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 Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 788 | |
Bjorn Bringert | 346dafb | 2009-04-29 21:41:47 +0100 | [diff] [blame] | 789 | 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] | 790 | 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 Scroggins | faa15db | 2009-04-03 10:16:06 -0700 | [diff] [blame] | 797 | if (selectionArgs[0].startsWith("http") |
| 798 | || selectionArgs[0].startsWith("file")) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 799 | 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 Scroggins | bd359cc | 2009-05-26 15:57:35 -0400 | [diff] [blame] | 807 | // To match against titles. |
| 808 | SUGGEST_ARGS[4] = like; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 809 | 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 Scroggins | 31887fd | 2009-05-18 16:58:08 -0400 | [diff] [blame] | 816 | ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 817 | |
Bjorn Bringert | 346dafb | 2009-04-29 21:41:47 +0100 | [diff] [blame] | 818 | if (match == URI_MATCH_BOOKMARKS_SUGGEST |
Dan Egnor | 5ee906c | 2009-11-18 12:11:49 -0800 | [diff] [blame] | 819 | || Patterns.WEB_URL.matcher(selectionArgs[0]).matches()) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 820 | return new MySuggestionCursor(c, null, ""); |
| 821 | } else { |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 822 | // get search suggestions if there is still space in the list |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 823 | if (myArgs != null && myArgs.length > 1 |
| 824 | && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) { |
Bjorn Bringert | d267065 | 2010-09-13 14:06:41 +0100 | [diff] [blame] | 825 | SearchEngine searchEngine = mSettings.getSearchEngine(); |
| 826 | if (searchEngine != null && searchEngine.supportsSuggestions()) { |
| 827 | Cursor sc = searchEngine.getSuggestions(getContext(), selectionArgs[0]); |
| 828 | return new MySuggestionCursor(c, sc, selectionArgs[0]); |
| 829 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 830 | } |
| 831 | return new MySuggestionCursor(c, null, selectionArgs[0]); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | String[] projection = null; |
| 836 | if (projectionIn != null && projectionIn.length > 0) { |
| 837 | projection = new String[projectionIn.length + 1]; |
| 838 | System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length); |
| 839 | projection[projectionIn.length] = "_id AS _id"; |
| 840 | } |
| 841 | |
| 842 | StringBuilder whereClause = new StringBuilder(256); |
| 843 | if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) { |
| 844 | whereClause.append("(_id = ").append(url.getPathSegments().get(1)) |
| 845 | .append(")"); |
| 846 | } |
| 847 | |
| 848 | // Tack on the user's selection, if present |
| 849 | if (selection != null && selection.length() > 0) { |
| 850 | if (whereClause.length() > 0) { |
| 851 | whereClause.append(" AND "); |
| 852 | } |
| 853 | |
| 854 | whereClause.append('('); |
| 855 | whereClause.append(selection); |
| 856 | whereClause.append(')'); |
| 857 | } |
| 858 | Cursor c = db.query(TABLE_NAMES[match % 10], projection, |
| 859 | whereClause.toString(), selectionArgs, null, null, sortOrder, |
| 860 | null); |
| 861 | c.setNotificationUri(getContext().getContentResolver(), url); |
| 862 | return c; |
| 863 | } |
| 864 | |
| 865 | @Override |
| 866 | public String getType(Uri url) { |
| 867 | int match = URI_MATCHER.match(url); |
| 868 | switch (match) { |
| 869 | case URI_MATCH_BOOKMARKS: |
| 870 | return "vnd.android.cursor.dir/bookmark"; |
| 871 | |
| 872 | case URI_MATCH_BOOKMARKS_ID: |
| 873 | return "vnd.android.cursor.item/bookmark"; |
| 874 | |
| 875 | case URI_MATCH_SEARCHES: |
| 876 | return "vnd.android.cursor.dir/searches"; |
| 877 | |
| 878 | case URI_MATCH_SEARCHES_ID: |
| 879 | return "vnd.android.cursor.item/searches"; |
| 880 | |
| 881 | case URI_MATCH_SUGGEST: |
| 882 | return SearchManager.SUGGEST_MIME_TYPE; |
| 883 | |
| 884 | default: |
| 885 | throw new IllegalArgumentException("Unknown URL"); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | @Override |
| 890 | public Uri insert(Uri url, ContentValues initialValues) { |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 891 | boolean isBookmarkTable = false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 892 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 893 | |
| 894 | int match = URI_MATCHER.match(url); |
| 895 | Uri uri = null; |
| 896 | switch (match) { |
| 897 | case URI_MATCH_BOOKMARKS: { |
| 898 | // Insert into the bookmarks table |
| 899 | long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url", |
| 900 | initialValues); |
| 901 | if (rowID > 0) { |
| 902 | uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI, |
| 903 | rowID); |
| 904 | } |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 905 | isBookmarkTable = true; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 906 | break; |
| 907 | } |
| 908 | |
| 909 | case URI_MATCH_SEARCHES: { |
| 910 | // Insert into the searches table |
| 911 | long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url", |
| 912 | initialValues); |
| 913 | if (rowID > 0) { |
| 914 | uri = ContentUris.withAppendedId(Browser.SEARCHES_URI, |
| 915 | rowID); |
| 916 | } |
| 917 | break; |
| 918 | } |
| 919 | |
| 920 | default: |
| 921 | throw new IllegalArgumentException("Unknown URL"); |
| 922 | } |
| 923 | |
| 924 | if (uri == null) { |
| 925 | throw new IllegalArgumentException("Unknown URL"); |
| 926 | } |
| 927 | getContext().getContentResolver().notifyChange(uri, null); |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 928 | |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 929 | // Back up the new bookmark set if we just inserted one. |
| 930 | // A row created when bookmarks are added from scratch will have |
| 931 | // bookmark=1 in the initial value set. |
| 932 | if (isBookmarkTable |
| 933 | && initialValues.containsKey(BookmarkColumns.BOOKMARK) |
| 934 | && initialValues.getAsInteger(BookmarkColumns.BOOKMARK) != 0) { |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 935 | mBackupManager.dataChanged(); |
| 936 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 937 | return uri; |
| 938 | } |
| 939 | |
| 940 | @Override |
| 941 | public int delete(Uri url, String where, String[] whereArgs) { |
| 942 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 943 | |
| 944 | int match = URI_MATCHER.match(url); |
| 945 | if (match == -1 || match == URI_MATCH_SUGGEST) { |
| 946 | throw new IllegalArgumentException("Unknown URL"); |
| 947 | } |
| 948 | |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 949 | // 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] | 950 | boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID); |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 951 | String id = null; |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 952 | |
| 953 | if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 954 | StringBuilder sb = new StringBuilder(); |
| 955 | if (where != null && where.length() > 0) { |
| 956 | sb.append("( "); |
| 957 | sb.append(where); |
| 958 | sb.append(" ) AND "); |
| 959 | } |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 960 | id = url.getPathSegments().get(1); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 961 | sb.append("_id = "); |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 962 | sb.append(id); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 963 | where = sb.toString(); |
| 964 | } |
| 965 | |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 966 | ContentResolver cr = getContext().getContentResolver(); |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 967 | |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 968 | // 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] | 969 | if (isBookmarkTable) { |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 970 | Cursor cursor = cr.query(Browser.BOOKMARKS_URI, |
| 971 | new String[] { BookmarkColumns.BOOKMARK }, |
| 972 | "_id = " + id, null, null); |
| 973 | if (cursor.moveToNext()) { |
| 974 | if (cursor.getInt(0) != 0) { |
| 975 | // yep, this record is a bookmark |
| 976 | mBackupManager.dataChanged(); |
| 977 | } |
| 978 | } |
| 979 | cursor.close(); |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 980 | } |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 981 | |
| 982 | int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs); |
| 983 | cr.notifyChange(url, null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 984 | return count; |
| 985 | } |
| 986 | |
| 987 | @Override |
| 988 | public int update(Uri url, ContentValues values, String where, |
| 989 | String[] whereArgs) { |
| 990 | SQLiteDatabase db = mOpenHelper.getWritableDatabase(); |
| 991 | |
| 992 | int match = URI_MATCHER.match(url); |
| 993 | if (match == -1 || match == URI_MATCH_SUGGEST) { |
| 994 | throw new IllegalArgumentException("Unknown URL"); |
| 995 | } |
| 996 | |
Leon Scroggins | f2463ae | 2010-02-23 14:28:51 -0500 | [diff] [blame] | 997 | 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] | 998 | StringBuilder sb = new StringBuilder(); |
| 999 | if (where != null && where.length() > 0) { |
| 1000 | sb.append("( "); |
| 1001 | sb.append(where); |
| 1002 | sb.append(" ) AND "); |
| 1003 | } |
Leon Scroggins | f2463ae | 2010-02-23 14:28:51 -0500 | [diff] [blame] | 1004 | String id = url.getPathSegments().get(1); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1005 | sb.append("_id = "); |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1006 | sb.append(id); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1007 | where = sb.toString(); |
| 1008 | } |
| 1009 | |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1010 | ContentResolver cr = getContext().getContentResolver(); |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 1011 | |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1012 | // Not all bookmark-table updates should be backed up. Look to see |
| 1013 | // whether we changed the title, url, or "is a bookmark" state, and |
| 1014 | // request a backup if so. |
Leon Scroggins | f2463ae | 2010-02-23 14:28:51 -0500 | [diff] [blame] | 1015 | if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_BOOKMARKS) { |
| 1016 | boolean changingBookmarks = false; |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1017 | // Alterations to the bookmark field inherently change the bookmark |
| 1018 | // set, so we don't need to query the record; we know a priori that |
| 1019 | // we will need to back up this change. |
| 1020 | if (values.containsKey(BookmarkColumns.BOOKMARK)) { |
| 1021 | changingBookmarks = true; |
Leon Scroggins | f2463ae | 2010-02-23 14:28:51 -0500 | [diff] [blame] | 1022 | } else if ((values.containsKey(BookmarkColumns.TITLE) |
| 1023 | || values.containsKey(BookmarkColumns.URL)) |
| 1024 | && values.containsKey(BookmarkColumns._ID)) { |
| 1025 | // If a title or URL has been changed, check to see if it is to |
| 1026 | // a bookmark. The ID should have been included in the update, |
| 1027 | // so use it. |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1028 | Cursor cursor = cr.query(Browser.BOOKMARKS_URI, |
| 1029 | new String[] { BookmarkColumns.BOOKMARK }, |
Leon Scroggins | f2463ae | 2010-02-23 14:28:51 -0500 | [diff] [blame] | 1030 | BookmarkColumns._ID + " = " |
| 1031 | + values.getAsString(BookmarkColumns._ID), null, null); |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1032 | if (cursor.moveToNext()) { |
| 1033 | changingBookmarks = (cursor.getInt(0) != 0); |
| 1034 | } |
| 1035 | cursor.close(); |
| 1036 | } |
| 1037 | |
| 1038 | // if this *is* a bookmark row we're altering, we need to back it up. |
| 1039 | if (changingBookmarks) { |
| 1040 | mBackupManager.dataChanged(); |
| 1041 | } |
Christopher Tate | 9c0dd8c | 2009-07-10 17:51:48 -0700 | [diff] [blame] | 1042 | } |
Christopher Tate | f0c36f7 | 2009-07-28 15:24:05 -0700 | [diff] [blame] | 1043 | |
| 1044 | int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs); |
| 1045 | cr.notifyChange(url, null); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1046 | return ret; |
| 1047 | } |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 1048 | |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 1049 | /** |
| 1050 | * Strips the provided url of preceding "http://" and any trailing "/". Does not |
| 1051 | * strip "https://". If the provided string cannot be stripped, the original string |
| 1052 | * is returned. |
Satish Sampath | 565505b | 2009-05-29 15:37:27 +0100 | [diff] [blame] | 1053 | * |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 1054 | * 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] | 1055 | * |
Mike LeBeau | c42f81b | 2009-05-14 15:04:19 -0700 | [diff] [blame] | 1056 | * @param url a url to strip, like "http://www.google.com/" |
| 1057 | * @return a stripped url like "www.google.com", or the original string if it could |
| 1058 | * not be stripped |
| 1059 | */ |
| 1060 | private static String stripUrl(String url) { |
| 1061 | if (url == null) return null; |
| 1062 | Matcher m = STRIP_URL_PATTERN.matcher(url); |
| 1063 | if (m.matches() && m.groupCount() == 3) { |
| 1064 | return m.group(2); |
| 1065 | } else { |
| 1066 | return url; |
| 1067 | } |
| 1068 | } |
| 1069 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1070 | } |