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