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