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