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