blob: b74c9eb28f94d9feb2f77b0e443f69677a6529bc [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
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
17package com.android.browser;
18
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070019import com.google.android.providers.GoogleSettings.Partner;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070020
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.app.SearchManager;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070022import android.backup.BackupManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.content.ComponentName;
24import android.content.ContentProvider;
25import android.content.ContentUris;
26import android.content.ContentValues;
27import android.content.Context;
28import android.content.Intent;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070029import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.content.UriMatcher;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070031import android.content.SharedPreferences.Editor;
Satish Sampath565505b2009-05-29 15:37:27 +010032import android.content.pm.PackageManager;
33import android.content.pm.ResolveInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.database.AbstractCursor;
Leon Scroggins62b71f72009-06-12 17:51:22 -040035import android.database.ContentObserver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.database.sqlite.SQLiteDatabase;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010038import android.database.sqlite.SQLiteOpenHelper;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.net.Uri;
Leon Scroggins62b71f72009-06-12 17:51:22 -040040import android.os.Handler;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070041import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042import android.provider.Browser;
Leon Scroggins62b71f72009-06-12 17:51:22 -040043import android.provider.Settings;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044import android.server.search.SearchableInfo;
Mike LeBeau21beb132009-05-13 14:57:50 -070045import android.text.TextUtils;
The Android Open Source Project0c908882009-03-03 19:32:16 -080046import android.text.util.Regex;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010047import android.util.Log;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070048import android.util.TypedValue;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010049
50import java.util.Date;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070051import java.util.regex.Matcher;
52import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -080053
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070054
The Android Open Source Project0c908882009-03-03 19:32:16 -080055public class BrowserProvider extends ContentProvider {
56
57 private SQLiteOpenHelper mOpenHelper;
Christopher Tate9c0dd8c2009-07-10 17:51:48 -070058 private BackupManager mBackupManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080059 private static final String sDatabaseName = "browser.db";
60 private static final String TAG = "BrowserProvider";
61 private static final String ORDER_BY = "visits DESC, date DESC";
62
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070063 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
64 "viewer?source=androidclient";
65
The Android Open Source Project0c908882009-03-03 19:32:16 -080066 private static final String[] TABLE_NAMES = new String[] {
67 "bookmarks", "searches"
68 };
69 private static final String[] SUGGEST_PROJECTION = new String[] {
70 "_id", "url", "title", "bookmark"
71 };
Satish Sampath565505b2009-05-29 15:37:27 +010072 private static final String SUGGEST_SELECTION =
Leon Scrogginsbd359cc2009-05-26 15:57:35 -040073 "url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?"
74 + " OR title LIKE ?";
75 private String[] SUGGEST_ARGS = new String[5];
The Android Open Source Project0c908882009-03-03 19:32:16 -080076
77 // shared suggestion array index, make sure to match COLUMNS
78 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
79 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
80 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
81 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
82 private static final int SUGGEST_COLUMN_ICON_1_ID = 5;
83 private static final int SUGGEST_COLUMN_ICON_2_ID = 6;
84 private static final int SUGGEST_COLUMN_QUERY_ID = 7;
Mike LeBeau1ef26a32009-05-13 20:11:00 -070085 private static final int SUGGEST_COLUMN_FORMAT = 8;
The Android Open Source Project0c908882009-03-03 19:32:16 -080086
87 // shared suggestion columns
88 private static final String[] COLUMNS = new String[] {
89 "_id",
90 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
91 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
92 SearchManager.SUGGEST_COLUMN_TEXT_1,
93 SearchManager.SUGGEST_COLUMN_TEXT_2,
94 SearchManager.SUGGEST_COLUMN_ICON_1,
95 SearchManager.SUGGEST_COLUMN_ICON_2,
Mike LeBeau1ef26a32009-05-13 20:11:00 -070096 SearchManager.SUGGEST_COLUMN_QUERY,
97 SearchManager.SUGGEST_COLUMN_FORMAT};
The Android Open Source Project0c908882009-03-03 19:32:16 -080098
99 private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3;
100 private static final int MAX_SUGGESTION_LONG_ENTRIES = 6;
Leon Scroggins31887fd2009-05-18 16:58:08 -0400101 private static final String MAX_SUGGESTION_LONG_ENTRIES_STRING =
102 Integer.valueOf(MAX_SUGGESTION_LONG_ENTRIES).toString();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800103
104 // make sure that these match the index of TABLE_NAMES
105 private static final int URI_MATCH_BOOKMARKS = 0;
106 private static final int URI_MATCH_SEARCHES = 1;
107 // (id % 10) should match the table name index
108 private static final int URI_MATCH_BOOKMARKS_ID = 10;
109 private static final int URI_MATCH_SEARCHES_ID = 11;
110 //
111 private static final int URI_MATCH_SUGGEST = 20;
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100112 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800113
114 private static final UriMatcher URI_MATCHER;
115
116 static {
117 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
118 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
119 URI_MATCH_BOOKMARKS);
120 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
121 URI_MATCH_BOOKMARKS_ID);
122 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
123 URI_MATCH_SEARCHES);
124 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
125 URI_MATCH_SEARCHES_ID);
126 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
127 URI_MATCH_SUGGEST);
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100128 URI_MATCHER.addURI("browser",
129 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
130 URI_MATCH_BOOKMARKS_SUGGEST);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800131 }
132
133 // 1 -> 2 add cache table
134 // 2 -> 3 update history table
135 // 3 -> 4 add passwords table
136 // 4 -> 5 add settings table
137 // 5 -> 6 ?
138 // 6 -> 7 ?
139 // 7 -> 8 drop proxy table
140 // 8 -> 9 drop settings table
141 // 9 -> 10 add form_urls and form_data
142 // 10 -> 11 add searches table
143 // 11 -> 12 modify cache table
144 // 12 -> 13 modify cache table
145 // 13 -> 14 correspond with Google Bookmarks schema
146 // 14 -> 15 move couple of tables to either browser private database or webview database
147 // 15 -> 17 Set it up for the SearchManager
148 // 17 -> 18 Added favicon in bookmarks table for Home shortcuts
149 // 18 -> 19 Remove labels table
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400150 // 19 -> 20 Added thumbnail
151 private static final int DATABASE_VERSION = 20;
Satish Sampath565505b2009-05-29 15:37:27 +0100152
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700153 // Regular expression which matches http://, followed by some stuff, followed by
154 // optionally a trailing slash, all matched as separate groups.
155 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
Satish Sampath565505b2009-05-29 15:37:27 +0100156
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100157 private SearchManager mSearchManager;
158
Satish Sampath60d24e22009-07-09 16:46:08 +0100159 // The ID of the ColorStateList to be applied to urls of website suggestions, as derived from
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700160 // the current theme. This is not set until/unless beautifyUrl is called, at which point
161 // this variable caches the color value.
Satish Sampath60d24e22009-07-09 16:46:08 +0100162 private static String mSearchUrlColorId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800163
164 public BrowserProvider() {
165 }
Satish Sampath565505b2009-05-29 15:37:27 +0100166
The Android Open Source Project0c908882009-03-03 19:32:16 -0800167
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700168 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800169 StringBuffer sb = new StringBuffer();
170 int lastCharLoc = 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100171
Ramanan Rajeswaran378e5722009-07-01 08:46:58 -0700172 final String client_id = Partner.getString(context.getContentResolver(),
173 Partner.CLIENT_ID, "android-google");
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700174
The Android Open Source Project0c908882009-03-03 19:32:16 -0800175 for (int i = 0; i < srcString.length(); ++i) {
176 char c = srcString.charAt(i);
177 if (c == '{') {
178 sb.append(srcString.subSequence(lastCharLoc, i));
179 lastCharLoc = i;
180 inner:
181 for (int j = i; j < srcString.length(); ++j) {
182 char k = srcString.charAt(j);
183 if (k == '}') {
184 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700185 if (propertyKeyValue.equals("CLIENT_ID")) {
186 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800187 } else {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700188 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800189 }
190 lastCharLoc = j + 1;
191 i = j;
192 break inner;
193 }
194 }
195 }
196 }
197 if (srcString.length() - lastCharLoc > 0) {
198 // Put on the tail, if there is one
199 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
200 }
201 return sb;
202 }
203
204 private static class DatabaseHelper extends SQLiteOpenHelper {
205 private Context mContext;
206
207 public DatabaseHelper(Context context) {
208 super(context, sDatabaseName, null, DATABASE_VERSION);
209 mContext = context;
210 }
211
212 @Override
213 public void onCreate(SQLiteDatabase db) {
214 db.execSQL("CREATE TABLE bookmarks (" +
215 "_id INTEGER PRIMARY KEY," +
216 "title TEXT," +
217 "url TEXT," +
218 "visits INTEGER," +
219 "date LONG," +
220 "created LONG," +
221 "description TEXT," +
222 "bookmark INTEGER," +
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400223 "favicon BLOB DEFAULT NULL," +
224 "thumbnail BLOB DEFAULT NULL" +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800225 ");");
226
227 final CharSequence[] bookmarks = mContext.getResources()
228 .getTextArray(R.array.bookmarks);
229 int size = bookmarks.length;
230 try {
231 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700232 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800233 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
234 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100235 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800236 "', 0, 0, 0, 1);");
237 }
238 } catch (ArrayIndexOutOfBoundsException e) {
239 }
240
241 db.execSQL("CREATE TABLE searches (" +
242 "_id INTEGER PRIMARY KEY," +
243 "search TEXT," +
244 "date LONG" +
245 ");");
246 }
247
248 @Override
249 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
250 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400251 + newVersion);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800252 if (oldVersion == 18) {
253 db.execSQL("DROP TABLE IF EXISTS labels");
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400254 }
255 if (oldVersion <= 19) {
256 db.execSQL("ALTER TABLE bookmarks ADD COLUMN thumbnail BLOB DEFAULT NULL;");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800257 } else {
258 db.execSQL("DROP TABLE IF EXISTS bookmarks");
259 db.execSQL("DROP TABLE IF EXISTS searches");
260 onCreate(db);
261 }
262 }
263 }
264
265 @Override
266 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700267 final Context context = getContext();
268 mOpenHelper = new DatabaseHelper(context);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700269 mBackupManager = new BackupManager(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100270 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700271 // To avoid erasing the bookmark table, we added it explicitly for
272 // version 18 and 19 as in the other cases, we will erase the table.
273 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
274 SharedPreferences p = PreferenceManager
275 .getDefaultSharedPreferences(context);
276 boolean fix = p.getBoolean("fix_picasa", true);
277 if (fix) {
278 fixPicasaBookmark();
279 Editor ed = p.edit();
280 ed.putBoolean("fix_picasa", false);
281 ed.commit();
282 }
283 }
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100284 mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400285 mShowWebSuggestionsSettingChangeObserver
286 = new ShowWebSuggestionsSettingChangeObserver();
287 context.getContentResolver().registerContentObserver(
288 Settings.System.getUriFor(
289 Settings.System.SHOW_WEB_SUGGESTIONS),
290 true, mShowWebSuggestionsSettingChangeObserver);
291 updateShowWebSuggestions();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800292 return true;
293 }
294
Leon Scroggins62b71f72009-06-12 17:51:22 -0400295 /**
296 * This Observer will ensure that if the user changes the system
297 * setting of whether to display web suggestions, we will
298 * change accordingly.
299 */
300 /* package */ class ShowWebSuggestionsSettingChangeObserver
301 extends ContentObserver {
302 public ShowWebSuggestionsSettingChangeObserver() {
303 super(new Handler());
304 }
305
306 @Override
307 public void onChange(boolean selfChange) {
308 updateShowWebSuggestions();
309 }
310 }
311
312 private ShowWebSuggestionsSettingChangeObserver
313 mShowWebSuggestionsSettingChangeObserver;
314
315 // If non-null, then the system is set to show web suggestions,
316 // and this is the SearchableInfo to use to get them.
317 private SearchableInfo mSearchableInfo;
318
319 /**
320 * Check the system settings to see whether web suggestions are
321 * allowed. If so, store the SearchableInfo to grab suggestions
322 * while the user is typing.
323 */
324 private void updateShowWebSuggestions() {
325 mSearchableInfo = null;
326 Context context = getContext();
327 if (Settings.System.getInt(context.getContentResolver(),
328 Settings.System.SHOW_WEB_SUGGESTIONS,
329 1 /* default on */) == 1) {
330 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
331 intent.addCategory(Intent.CATEGORY_DEFAULT);
332 ResolveInfo info = context.getPackageManager().resolveActivity(
333 intent, PackageManager.MATCH_DEFAULT_ONLY);
334 if (info != null) {
335 ComponentName googleSearchComponent =
336 new ComponentName(info.activityInfo.packageName,
337 info.activityInfo.name);
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100338 mSearchableInfo = mSearchManager.getSearchableInfo(
Leon Scroggins62b71f72009-06-12 17:51:22 -0400339 googleSearchComponent, false);
340 }
341 }
342 }
343
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700344 private void fixPicasaBookmark() {
345 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
346 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
347 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
348 try {
349 if (!cursor.moveToFirst()) {
350 // set "created" so that it will be on the top of the list
351 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
352 "date, created, bookmark)" + " VALUES('" +
353 getContext().getString(R.string.picasa) + "', '"
354 + PICASA_URL + "', 0, 0, " + new Date().getTime()
355 + ", 1);");
356 }
357 } finally {
358 if (cursor != null) {
359 cursor.close();
360 }
361 }
362 }
363
The Android Open Source Project0c908882009-03-03 19:32:16 -0800364 /*
365 * Subclass AbstractCursor so we can combine multiple Cursors and add
366 * "Google Search".
367 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100368 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
The Android Open Source Project0c908882009-03-03 19:32:16 -0800369 * "Google Search";
Satish Sampath565505b2009-05-29 15:37:27 +0100370 * 2. If bookmark/history entries are less than
The Android Open Source Project0c908882009-03-03 19:32:16 -0800371 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
372 */
373 private class MySuggestionCursor extends AbstractCursor {
374 private Cursor mHistoryCursor;
375 private Cursor mSuggestCursor;
376 private int mHistoryCount;
377 private int mSuggestionCount;
378 private boolean mBeyondCursor;
379 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100380 private int mSuggestText1Id;
381 private int mSuggestText2Id;
382 private int mSuggestQueryId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800383
384 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
385 mHistoryCursor = hc;
386 mSuggestCursor = sc;
387 mHistoryCount = hc.getCount();
388 mSuggestionCount = sc != null ? sc.getCount() : 0;
389 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
390 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
391 }
392 mString = string;
393 mBeyondCursor = false;
Satish Sampath565505b2009-05-29 15:37:27 +0100394
395 // Some web suggest providers only give suggestions and have no description string for
396 // items. The order of the result columns may be different as well. So retrieve the
397 // column indices for the fields we need now and check before using below.
398 if (mSuggestCursor == null) {
399 mSuggestText1Id = -1;
400 mSuggestText2Id = -1;
401 mSuggestQueryId = -1;
402 } else {
403 mSuggestText1Id = mSuggestCursor.getColumnIndex(
404 SearchManager.SUGGEST_COLUMN_TEXT_1);
405 mSuggestText2Id = mSuggestCursor.getColumnIndex(
406 SearchManager.SUGGEST_COLUMN_TEXT_2);
407 mSuggestQueryId = mSuggestCursor.getColumnIndex(
408 SearchManager.SUGGEST_COLUMN_QUERY);
409 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800410 }
411
412 @Override
413 public boolean onMove(int oldPosition, int newPosition) {
414 if (mHistoryCursor == null) {
415 return false;
416 }
417 if (mHistoryCount > newPosition) {
418 mHistoryCursor.moveToPosition(newPosition);
419 mBeyondCursor = false;
420 } else if (mHistoryCount + mSuggestionCount > newPosition) {
421 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
422 mBeyondCursor = false;
423 } else {
424 mBeyondCursor = true;
425 }
426 return true;
427 }
428
429 @Override
430 public int getCount() {
431 if (mString.length() > 0) {
432 return mHistoryCount + mSuggestionCount + 1;
433 } else {
434 return mHistoryCount + mSuggestionCount;
435 }
436 }
437
438 @Override
439 public String[] getColumnNames() {
440 return COLUMNS;
441 }
Satish Sampath565505b2009-05-29 15:37:27 +0100442
The Android Open Source Project0c908882009-03-03 19:32:16 -0800443 @Override
444 public String getString(int columnIndex) {
445 if ((mPos != -1 && mHistoryCursor != null)) {
446 switch(columnIndex) {
447 case SUGGEST_COLUMN_INTENT_ACTION_ID:
448 if (mHistoryCount > mPos) {
449 return Intent.ACTION_VIEW;
450 } else {
451 return Intent.ACTION_SEARCH;
452 }
453
454 case SUGGEST_COLUMN_INTENT_DATA_ID:
455 if (mHistoryCount > mPos) {
456 return mHistoryCursor.getString(1);
457 } else {
458 return null;
459 }
460
461 case SUGGEST_COLUMN_TEXT_1_ID:
462 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700463 return getHistoryTitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800464 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100465 if (mSuggestText1Id == -1) return null;
466 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 } else {
468 return mString;
469 }
470
471 case SUGGEST_COLUMN_TEXT_2_ID:
472 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700473 return getHistorySubtitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800474 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100475 if (mSuggestText2Id == -1) return null;
476 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800477 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700478 return getContext().getString(R.string.search_the_web);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800479 }
480
481 case SUGGEST_COLUMN_ICON_1_ID:
482 if (mHistoryCount > mPos) {
483 if (mHistoryCursor.getInt(3) == 1) {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400484 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800485 R.drawable.ic_search_category_bookmark)
486 .toString();
487 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400488 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800489 R.drawable.ic_search_category_history)
490 .toString();
491 }
492 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400493 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800494 R.drawable.ic_search_category_suggest)
495 .toString();
496 }
497
498 case SUGGEST_COLUMN_ICON_2_ID:
Leon Scroggins31887fd2009-05-18 16:58:08 -0400499 return "0";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800500
501 case SUGGEST_COLUMN_QUERY_ID:
502 if (mHistoryCount > mPos) {
Mike LeBeau2af73052009-06-23 17:36:59 -0700503 // Return the url in the intent query column. This is ignored
504 // within the browser because our searchable is set to
505 // android:searchMode="queryRewriteFromData", but it is used by
506 // global search for query rewriting.
507 return mHistoryCursor.getString(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800508 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100509 if (mSuggestQueryId == -1) return null;
510 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800511 } else {
512 return mString;
513 }
Satish Sampath565505b2009-05-29 15:37:27 +0100514
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700515 case SUGGEST_COLUMN_FORMAT:
516 return "html";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800517 }
518 }
519 return null;
520 }
521
522 @Override
523 public double getDouble(int column) {
524 throw new UnsupportedOperationException();
525 }
526
527 @Override
528 public float getFloat(int column) {
529 throw new UnsupportedOperationException();
530 }
531
532 @Override
533 public int getInt(int column) {
534 throw new UnsupportedOperationException();
535 }
536
537 @Override
538 public long getLong(int column) {
539 if ((mPos != -1) && column == 0) {
540 return mPos; // use row# as the _Id
541 }
542 throw new UnsupportedOperationException();
543 }
544
545 @Override
546 public short getShort(int column) {
547 throw new UnsupportedOperationException();
548 }
549
550 @Override
551 public boolean isNull(int column) {
552 throw new UnsupportedOperationException();
553 }
554
555 // TODO Temporary change, finalize after jq's changes go in
556 public void deactivate() {
557 if (mHistoryCursor != null) {
558 mHistoryCursor.deactivate();
559 }
560 if (mSuggestCursor != null) {
561 mSuggestCursor.deactivate();
562 }
563 super.deactivate();
564 }
565
566 public boolean requery() {
567 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
568 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
569 }
570
571 // TODO Temporary change, finalize after jq's changes go in
572 public void close() {
573 super.close();
574 if (mHistoryCursor != null) {
575 mHistoryCursor.close();
576 mHistoryCursor = null;
577 }
578 if (mSuggestCursor != null) {
579 mSuggestCursor.close();
580 mSuggestCursor = null;
581 }
582 }
Satish Sampath565505b2009-05-29 15:37:27 +0100583
Mike LeBeau21beb132009-05-13 14:57:50 -0700584 /**
585 * Provides the title (text line 1) for a browser suggestion, which should be the
586 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100587 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700588 * @return the title string to use
589 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700590 private String getHistoryTitle() {
591 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700592 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700593 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700594 }
595 return title;
596 }
Satish Sampath565505b2009-05-29 15:37:27 +0100597
Mike LeBeau21beb132009-05-13 14:57:50 -0700598 /**
599 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
600 * webpage url. If the webpage title is empty, then the url should go in the title
601 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100602 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700603 * @return the subtitle string to use, or null if none
604 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700605 private String getHistorySubtitle() {
606 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700607 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
608 return null;
609 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700610 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700611 }
612 }
Satish Sampath565505b2009-05-29 15:37:27 +0100613
Mike LeBeau21beb132009-05-13 14:57:50 -0700614 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700615 * Strips "http://" from the beginning of a url and "/" from the end,
616 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700617 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700618 private String beautifyUrl(String url) {
Satish Sampath60d24e22009-07-09 16:46:08 +0100619 if (mSearchUrlColorId == null) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700620 // Get the color used for this purpose from the current theme.
621 TypedValue colorValue = new TypedValue();
622 getContext().getTheme().resolveAttribute(
623 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
Satish Sampath60d24e22009-07-09 16:46:08 +0100624 mSearchUrlColorId = Integer.toString(colorValue.resourceId);
Mike LeBeau21beb132009-05-13 14:57:50 -0700625 }
Satish Sampath565505b2009-05-29 15:37:27 +0100626
Satish Sampath60d24e22009-07-09 16:46:08 +0100627 return "<font color=\"@" + mSearchUrlColorId + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700628 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800629 }
630
631 @Override
632 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100633 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800634 throws IllegalStateException {
635 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
636
637 int match = URI_MATCHER.match(url);
638 if (match == -1) {
639 throw new IllegalArgumentException("Unknown URL");
640 }
641
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100642 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800643 String suggestSelection;
644 String [] myArgs;
645 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
646 suggestSelection = null;
647 myArgs = null;
648 } else {
649 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700650 if (selectionArgs[0].startsWith("http")
651 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800652 myArgs = new String[1];
653 myArgs[0] = like;
654 suggestSelection = selection;
655 } else {
656 SUGGEST_ARGS[0] = "http://" + like;
657 SUGGEST_ARGS[1] = "http://www." + like;
658 SUGGEST_ARGS[2] = "https://" + like;
659 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400660 // To match against titles.
661 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800662 myArgs = SUGGEST_ARGS;
663 suggestSelection = SUGGEST_SELECTION;
664 }
665 }
666
667 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
668 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Leon Scroggins31887fd2009-05-18 16:58:08 -0400669 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800670
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100671 if (match == URI_MATCH_BOOKMARKS_SUGGEST
672 || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800673 return new MySuggestionCursor(c, null, "");
674 } else {
675 // get Google suggest if there is still space in the list
676 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400677 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800678 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100679 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400680 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800681 }
682 return new MySuggestionCursor(c, null, selectionArgs[0]);
683 }
684 }
685
686 String[] projection = null;
687 if (projectionIn != null && projectionIn.length > 0) {
688 projection = new String[projectionIn.length + 1];
689 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
690 projection[projectionIn.length] = "_id AS _id";
691 }
692
693 StringBuilder whereClause = new StringBuilder(256);
694 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
695 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
696 .append(")");
697 }
698
699 // Tack on the user's selection, if present
700 if (selection != null && selection.length() > 0) {
701 if (whereClause.length() > 0) {
702 whereClause.append(" AND ");
703 }
704
705 whereClause.append('(');
706 whereClause.append(selection);
707 whereClause.append(')');
708 }
709 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
710 whereClause.toString(), selectionArgs, null, null, sortOrder,
711 null);
712 c.setNotificationUri(getContext().getContentResolver(), url);
713 return c;
714 }
715
716 @Override
717 public String getType(Uri url) {
718 int match = URI_MATCHER.match(url);
719 switch (match) {
720 case URI_MATCH_BOOKMARKS:
721 return "vnd.android.cursor.dir/bookmark";
722
723 case URI_MATCH_BOOKMARKS_ID:
724 return "vnd.android.cursor.item/bookmark";
725
726 case URI_MATCH_SEARCHES:
727 return "vnd.android.cursor.dir/searches";
728
729 case URI_MATCH_SEARCHES_ID:
730 return "vnd.android.cursor.item/searches";
731
732 case URI_MATCH_SUGGEST:
733 return SearchManager.SUGGEST_MIME_TYPE;
734
735 default:
736 throw new IllegalArgumentException("Unknown URL");
737 }
738 }
739
740 @Override
741 public Uri insert(Uri url, ContentValues initialValues) {
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700742 boolean isBookmarkTable = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800743 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
744
745 int match = URI_MATCHER.match(url);
746 Uri uri = null;
747 switch (match) {
748 case URI_MATCH_BOOKMARKS: {
749 // Insert into the bookmarks table
750 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
751 initialValues);
752 if (rowID > 0) {
753 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
754 rowID);
755 }
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700756 isBookmarkTable = true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800757 break;
758 }
759
760 case URI_MATCH_SEARCHES: {
761 // Insert into the searches table
762 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
763 initialValues);
764 if (rowID > 0) {
765 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
766 rowID);
767 }
768 break;
769 }
770
771 default:
772 throw new IllegalArgumentException("Unknown URL");
773 }
774
775 if (uri == null) {
776 throw new IllegalArgumentException("Unknown URL");
777 }
778 getContext().getContentResolver().notifyChange(uri, null);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700779
780 // back up the new bookmark set if we just inserted one
781 if (isBookmarkTable) {
782 mBackupManager.dataChanged();
783 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800784 return uri;
785 }
786
787 @Override
788 public int delete(Uri url, String where, String[] whereArgs) {
789 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
790
791 int match = URI_MATCHER.match(url);
792 if (match == -1 || match == URI_MATCH_SUGGEST) {
793 throw new IllegalArgumentException("Unknown URL");
794 }
795
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700796 // need to know whether it's the bookmarks table for a couple fo reasons
797 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
798
799 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800800 StringBuilder sb = new StringBuilder();
801 if (where != null && where.length() > 0) {
802 sb.append("( ");
803 sb.append(where);
804 sb.append(" ) AND ");
805 }
806 sb.append("_id = ");
807 sb.append(url.getPathSegments().get(1));
808 where = sb.toString();
809 }
810
811 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
812 getContext().getContentResolver().notifyChange(url, null);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700813
814 // back up the new bookmark set if we just deleted one
815 if (isBookmarkTable) {
816 mBackupManager.dataChanged();
817 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800818 return count;
819 }
820
821 @Override
822 public int update(Uri url, ContentValues values, String where,
823 String[] whereArgs) {
824 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
825
826 int match = URI_MATCHER.match(url);
827 if (match == -1 || match == URI_MATCH_SUGGEST) {
828 throw new IllegalArgumentException("Unknown URL");
829 }
830
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700831 boolean isBookmarkTable = (match == URI_MATCH_BOOKMARKS_ID);
832
833 if (isBookmarkTable || match == URI_MATCH_SEARCHES_ID) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800834 StringBuilder sb = new StringBuilder();
835 if (where != null && where.length() > 0) {
836 sb.append("( ");
837 sb.append(where);
838 sb.append(" ) AND ");
839 }
840 sb.append("_id = ");
841 sb.append(url.getPathSegments().get(1));
842 where = sb.toString();
843 }
844
845 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
846 getContext().getContentResolver().notifyChange(url, null);
Christopher Tate9c0dd8c2009-07-10 17:51:48 -0700847
848 // back up the new bookmark set if we just changed one
849 if (isBookmarkTable) {
850 mBackupManager.dataChanged();
851 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800852 return ret;
853 }
Satish Sampath565505b2009-05-29 15:37:27 +0100854
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700855 /**
856 * Strips the provided url of preceding "http://" and any trailing "/". Does not
857 * strip "https://". If the provided string cannot be stripped, the original string
858 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +0100859 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700860 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +0100861 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700862 * @param url a url to strip, like "http://www.google.com/"
863 * @return a stripped url like "www.google.com", or the original string if it could
864 * not be stripped
865 */
866 private static String stripUrl(String url) {
867 if (url == null) return null;
868 Matcher m = STRIP_URL_PATTERN.matcher(url);
869 if (m.matches() && m.groupCount() == 3) {
870 return m.group(2);
871 } else {
872 return url;
873 }
874 }
875
The Android Open Source Project0c908882009-03-03 19:32:16 -0800876}