blob: a3f249bbc12c3f63da81bf93ec7a5a56b66824f6 [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;
22import android.content.ComponentName;
23import android.content.ContentProvider;
24import android.content.ContentUris;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.Intent;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070028import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.content.UriMatcher;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070030import android.content.SharedPreferences.Editor;
Satish Sampath565505b2009-05-29 15:37:27 +010031import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.database.AbstractCursor;
Leon Scroggins62b71f72009-06-12 17:51:22 -040034import android.database.ContentObserver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.database.sqlite.SQLiteDatabase;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010037import android.database.sqlite.SQLiteOpenHelper;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.net.Uri;
Leon Scroggins62b71f72009-06-12 17:51:22 -040039import android.os.Handler;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070040import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041import android.provider.Browser;
Leon Scroggins62b71f72009-06-12 17:51:22 -040042import android.provider.Settings;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.server.search.SearchableInfo;
Mike LeBeau21beb132009-05-13 14:57:50 -070044import android.text.TextUtils;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.text.util.Regex;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010046import android.util.Log;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070047import android.util.TypedValue;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010048
49import java.util.Date;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070050import java.util.regex.Matcher;
51import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -080052
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070053
The Android Open Source Project0c908882009-03-03 19:32:16 -080054public class BrowserProvider extends ContentProvider {
55
56 private SQLiteOpenHelper mOpenHelper;
57 private static final String sDatabaseName = "browser.db";
58 private static final String TAG = "BrowserProvider";
59 private static final String ORDER_BY = "visits DESC, date DESC";
60
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070061 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
62 "viewer?source=androidclient";
63
The Android Open Source Project0c908882009-03-03 19:32:16 -080064 private static final String[] TABLE_NAMES = new String[] {
65 "bookmarks", "searches"
66 };
67 private static final String[] SUGGEST_PROJECTION = new String[] {
68 "_id", "url", "title", "bookmark"
69 };
Satish Sampath565505b2009-05-29 15:37:27 +010070 private static final String SUGGEST_SELECTION =
Leon Scrogginsbd359cc2009-05-26 15:57:35 -040071 "url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?"
72 + " OR title LIKE ?";
73 private String[] SUGGEST_ARGS = new String[5];
The Android Open Source Project0c908882009-03-03 19:32:16 -080074
75 // shared suggestion array index, make sure to match COLUMNS
76 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
77 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
78 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
79 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
80 private static final int SUGGEST_COLUMN_ICON_1_ID = 5;
81 private static final int SUGGEST_COLUMN_ICON_2_ID = 6;
82 private static final int SUGGEST_COLUMN_QUERY_ID = 7;
Mike LeBeau1ef26a32009-05-13 20:11:00 -070083 private static final int SUGGEST_COLUMN_FORMAT = 8;
The Android Open Source Project0c908882009-03-03 19:32:16 -080084
85 // shared suggestion columns
86 private static final String[] COLUMNS = new String[] {
87 "_id",
88 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
89 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
90 SearchManager.SUGGEST_COLUMN_TEXT_1,
91 SearchManager.SUGGEST_COLUMN_TEXT_2,
92 SearchManager.SUGGEST_COLUMN_ICON_1,
93 SearchManager.SUGGEST_COLUMN_ICON_2,
Mike LeBeau1ef26a32009-05-13 20:11:00 -070094 SearchManager.SUGGEST_COLUMN_QUERY,
95 SearchManager.SUGGEST_COLUMN_FORMAT};
The Android Open Source Project0c908882009-03-03 19:32:16 -080096
97 private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3;
98 private static final int MAX_SUGGESTION_LONG_ENTRIES = 6;
Leon Scroggins31887fd2009-05-18 16:58:08 -040099 private static final String MAX_SUGGESTION_LONG_ENTRIES_STRING =
100 Integer.valueOf(MAX_SUGGESTION_LONG_ENTRIES).toString();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800101
102 // make sure that these match the index of TABLE_NAMES
103 private static final int URI_MATCH_BOOKMARKS = 0;
104 private static final int URI_MATCH_SEARCHES = 1;
105 // (id % 10) should match the table name index
106 private static final int URI_MATCH_BOOKMARKS_ID = 10;
107 private static final int URI_MATCH_SEARCHES_ID = 11;
108 //
109 private static final int URI_MATCH_SUGGEST = 20;
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100110 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800111
112 private static final UriMatcher URI_MATCHER;
113
114 static {
115 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
116 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
117 URI_MATCH_BOOKMARKS);
118 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
119 URI_MATCH_BOOKMARKS_ID);
120 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
121 URI_MATCH_SEARCHES);
122 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
123 URI_MATCH_SEARCHES_ID);
124 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
125 URI_MATCH_SUGGEST);
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100126 URI_MATCHER.addURI("browser",
127 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
128 URI_MATCH_BOOKMARKS_SUGGEST);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800129 }
130
131 // 1 -> 2 add cache table
132 // 2 -> 3 update history table
133 // 3 -> 4 add passwords table
134 // 4 -> 5 add settings table
135 // 5 -> 6 ?
136 // 6 -> 7 ?
137 // 7 -> 8 drop proxy table
138 // 8 -> 9 drop settings table
139 // 9 -> 10 add form_urls and form_data
140 // 10 -> 11 add searches table
141 // 11 -> 12 modify cache table
142 // 12 -> 13 modify cache table
143 // 13 -> 14 correspond with Google Bookmarks schema
144 // 14 -> 15 move couple of tables to either browser private database or webview database
145 // 15 -> 17 Set it up for the SearchManager
146 // 17 -> 18 Added favicon in bookmarks table for Home shortcuts
147 // 18 -> 19 Remove labels table
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400148 // 19 -> 20 Added thumbnail
149 private static final int DATABASE_VERSION = 20;
Satish Sampath565505b2009-05-29 15:37:27 +0100150
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700151 // Regular expression which matches http://, followed by some stuff, followed by
152 // optionally a trailing slash, all matched as separate groups.
153 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
Satish Sampath565505b2009-05-29 15:37:27 +0100154
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700155 // The hex color string to be applied to urls of website suggestions, as derived from
156 // the current theme. This is not set until/unless beautifyUrl is called, at which point
157 // this variable caches the color value.
158 private static String mSearchUrlColorHex;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800159
160 public BrowserProvider() {
161 }
Satish Sampath565505b2009-05-29 15:37:27 +0100162
The Android Open Source Project0c908882009-03-03 19:32:16 -0800163
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700164 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800165 StringBuffer sb = new StringBuffer();
166 int lastCharLoc = 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100167
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700168 final String client_id = Partner.getString(context.getContentResolver(), Partner.CLIENT_ID);
169
The Android Open Source Project0c908882009-03-03 19:32:16 -0800170 for (int i = 0; i < srcString.length(); ++i) {
171 char c = srcString.charAt(i);
172 if (c == '{') {
173 sb.append(srcString.subSequence(lastCharLoc, i));
174 lastCharLoc = i;
175 inner:
176 for (int j = i; j < srcString.length(); ++j) {
177 char k = srcString.charAt(j);
178 if (k == '}') {
179 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700180 if (propertyKeyValue.equals("CLIENT_ID")) {
181 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800182 } else {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700183 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800184 }
185 lastCharLoc = j + 1;
186 i = j;
187 break inner;
188 }
189 }
190 }
191 }
192 if (srcString.length() - lastCharLoc > 0) {
193 // Put on the tail, if there is one
194 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
195 }
196 return sb;
197 }
198
199 private static class DatabaseHelper extends SQLiteOpenHelper {
200 private Context mContext;
201
202 public DatabaseHelper(Context context) {
203 super(context, sDatabaseName, null, DATABASE_VERSION);
204 mContext = context;
205 }
206
207 @Override
208 public void onCreate(SQLiteDatabase db) {
209 db.execSQL("CREATE TABLE bookmarks (" +
210 "_id INTEGER PRIMARY KEY," +
211 "title TEXT," +
212 "url TEXT," +
213 "visits INTEGER," +
214 "date LONG," +
215 "created LONG," +
216 "description TEXT," +
217 "bookmark INTEGER," +
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400218 "favicon BLOB DEFAULT NULL," +
219 "thumbnail BLOB DEFAULT NULL" +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800220 ");");
221
222 final CharSequence[] bookmarks = mContext.getResources()
223 .getTextArray(R.array.bookmarks);
224 int size = bookmarks.length;
225 try {
226 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700227 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800228 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
229 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100230 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800231 "', 0, 0, 0, 1);");
232 }
233 } catch (ArrayIndexOutOfBoundsException e) {
234 }
235
236 db.execSQL("CREATE TABLE searches (" +
237 "_id INTEGER PRIMARY KEY," +
238 "search TEXT," +
239 "date LONG" +
240 ");");
241 }
242
243 @Override
244 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
245 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400246 + newVersion);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800247 if (oldVersion == 18) {
248 db.execSQL("DROP TABLE IF EXISTS labels");
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400249 }
250 if (oldVersion <= 19) {
251 db.execSQL("ALTER TABLE bookmarks ADD COLUMN thumbnail BLOB DEFAULT NULL;");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800252 } else {
253 db.execSQL("DROP TABLE IF EXISTS bookmarks");
254 db.execSQL("DROP TABLE IF EXISTS searches");
255 onCreate(db);
256 }
257 }
258 }
259
260 @Override
261 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700262 final Context context = getContext();
263 mOpenHelper = new DatabaseHelper(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100264 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700265 // To avoid erasing the bookmark table, we added it explicitly for
266 // version 18 and 19 as in the other cases, we will erase the table.
267 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
268 SharedPreferences p = PreferenceManager
269 .getDefaultSharedPreferences(context);
270 boolean fix = p.getBoolean("fix_picasa", true);
271 if (fix) {
272 fixPicasaBookmark();
273 Editor ed = p.edit();
274 ed.putBoolean("fix_picasa", false);
275 ed.commit();
276 }
277 }
Leon Scroggins62b71f72009-06-12 17:51:22 -0400278 mShowWebSuggestionsSettingChangeObserver
279 = new ShowWebSuggestionsSettingChangeObserver();
280 context.getContentResolver().registerContentObserver(
281 Settings.System.getUriFor(
282 Settings.System.SHOW_WEB_SUGGESTIONS),
283 true, mShowWebSuggestionsSettingChangeObserver);
284 updateShowWebSuggestions();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800285 return true;
286 }
287
Leon Scroggins62b71f72009-06-12 17:51:22 -0400288 /**
289 * This Observer will ensure that if the user changes the system
290 * setting of whether to display web suggestions, we will
291 * change accordingly.
292 */
293 /* package */ class ShowWebSuggestionsSettingChangeObserver
294 extends ContentObserver {
295 public ShowWebSuggestionsSettingChangeObserver() {
296 super(new Handler());
297 }
298
299 @Override
300 public void onChange(boolean selfChange) {
301 updateShowWebSuggestions();
302 }
303 }
304
305 private ShowWebSuggestionsSettingChangeObserver
306 mShowWebSuggestionsSettingChangeObserver;
307
308 // If non-null, then the system is set to show web suggestions,
309 // and this is the SearchableInfo to use to get them.
310 private SearchableInfo mSearchableInfo;
311
312 /**
313 * Check the system settings to see whether web suggestions are
314 * allowed. If so, store the SearchableInfo to grab suggestions
315 * while the user is typing.
316 */
317 private void updateShowWebSuggestions() {
318 mSearchableInfo = null;
319 Context context = getContext();
320 if (Settings.System.getInt(context.getContentResolver(),
321 Settings.System.SHOW_WEB_SUGGESTIONS,
322 1 /* default on */) == 1) {
323 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
324 intent.addCategory(Intent.CATEGORY_DEFAULT);
325 ResolveInfo info = context.getPackageManager().resolveActivity(
326 intent, PackageManager.MATCH_DEFAULT_ONLY);
327 if (info != null) {
328 ComponentName googleSearchComponent =
329 new ComponentName(info.activityInfo.packageName,
330 info.activityInfo.name);
331 mSearchableInfo = SearchManager.getSearchableInfo(
332 googleSearchComponent, false);
333 }
334 }
335 }
336
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700337 private void fixPicasaBookmark() {
338 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
339 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
340 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
341 try {
342 if (!cursor.moveToFirst()) {
343 // set "created" so that it will be on the top of the list
344 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
345 "date, created, bookmark)" + " VALUES('" +
346 getContext().getString(R.string.picasa) + "', '"
347 + PICASA_URL + "', 0, 0, " + new Date().getTime()
348 + ", 1);");
349 }
350 } finally {
351 if (cursor != null) {
352 cursor.close();
353 }
354 }
355 }
356
The Android Open Source Project0c908882009-03-03 19:32:16 -0800357 /*
358 * Subclass AbstractCursor so we can combine multiple Cursors and add
359 * "Google Search".
360 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100361 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
The Android Open Source Project0c908882009-03-03 19:32:16 -0800362 * "Google Search";
Satish Sampath565505b2009-05-29 15:37:27 +0100363 * 2. If bookmark/history entries are less than
The Android Open Source Project0c908882009-03-03 19:32:16 -0800364 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
365 */
366 private class MySuggestionCursor extends AbstractCursor {
367 private Cursor mHistoryCursor;
368 private Cursor mSuggestCursor;
369 private int mHistoryCount;
370 private int mSuggestionCount;
371 private boolean mBeyondCursor;
372 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100373 private int mSuggestText1Id;
374 private int mSuggestText2Id;
375 private int mSuggestQueryId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800376
377 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
378 mHistoryCursor = hc;
379 mSuggestCursor = sc;
380 mHistoryCount = hc.getCount();
381 mSuggestionCount = sc != null ? sc.getCount() : 0;
382 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
383 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
384 }
385 mString = string;
386 mBeyondCursor = false;
Satish Sampath565505b2009-05-29 15:37:27 +0100387
388 // Some web suggest providers only give suggestions and have no description string for
389 // items. The order of the result columns may be different as well. So retrieve the
390 // column indices for the fields we need now and check before using below.
391 if (mSuggestCursor == null) {
392 mSuggestText1Id = -1;
393 mSuggestText2Id = -1;
394 mSuggestQueryId = -1;
395 } else {
396 mSuggestText1Id = mSuggestCursor.getColumnIndex(
397 SearchManager.SUGGEST_COLUMN_TEXT_1);
398 mSuggestText2Id = mSuggestCursor.getColumnIndex(
399 SearchManager.SUGGEST_COLUMN_TEXT_2);
400 mSuggestQueryId = mSuggestCursor.getColumnIndex(
401 SearchManager.SUGGEST_COLUMN_QUERY);
402 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800403 }
404
405 @Override
406 public boolean onMove(int oldPosition, int newPosition) {
407 if (mHistoryCursor == null) {
408 return false;
409 }
410 if (mHistoryCount > newPosition) {
411 mHistoryCursor.moveToPosition(newPosition);
412 mBeyondCursor = false;
413 } else if (mHistoryCount + mSuggestionCount > newPosition) {
414 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
415 mBeyondCursor = false;
416 } else {
417 mBeyondCursor = true;
418 }
419 return true;
420 }
421
422 @Override
423 public int getCount() {
424 if (mString.length() > 0) {
425 return mHistoryCount + mSuggestionCount + 1;
426 } else {
427 return mHistoryCount + mSuggestionCount;
428 }
429 }
430
431 @Override
432 public String[] getColumnNames() {
433 return COLUMNS;
434 }
Satish Sampath565505b2009-05-29 15:37:27 +0100435
The Android Open Source Project0c908882009-03-03 19:32:16 -0800436 @Override
437 public String getString(int columnIndex) {
438 if ((mPos != -1 && mHistoryCursor != null)) {
439 switch(columnIndex) {
440 case SUGGEST_COLUMN_INTENT_ACTION_ID:
441 if (mHistoryCount > mPos) {
442 return Intent.ACTION_VIEW;
443 } else {
444 return Intent.ACTION_SEARCH;
445 }
446
447 case SUGGEST_COLUMN_INTENT_DATA_ID:
448 if (mHistoryCount > mPos) {
449 return mHistoryCursor.getString(1);
450 } else {
451 return null;
452 }
453
454 case SUGGEST_COLUMN_TEXT_1_ID:
455 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700456 return getHistoryTitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800457 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100458 if (mSuggestText1Id == -1) return null;
459 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800460 } else {
461 return mString;
462 }
463
464 case SUGGEST_COLUMN_TEXT_2_ID:
465 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700466 return getHistorySubtitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100468 if (mSuggestText2Id == -1) return null;
469 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800470 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700471 return getContext().getString(R.string.search_the_web);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800472 }
473
474 case SUGGEST_COLUMN_ICON_1_ID:
475 if (mHistoryCount > mPos) {
476 if (mHistoryCursor.getInt(3) == 1) {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400477 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800478 R.drawable.ic_search_category_bookmark)
479 .toString();
480 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400481 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800482 R.drawable.ic_search_category_history)
483 .toString();
484 }
485 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400486 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800487 R.drawable.ic_search_category_suggest)
488 .toString();
489 }
490
491 case SUGGEST_COLUMN_ICON_2_ID:
Leon Scroggins31887fd2009-05-18 16:58:08 -0400492 return "0";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800493
494 case SUGGEST_COLUMN_QUERY_ID:
495 if (mHistoryCount > mPos) {
496 return null;
497 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100498 if (mSuggestQueryId == -1) return null;
499 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800500 } else {
501 return mString;
502 }
Satish Sampath565505b2009-05-29 15:37:27 +0100503
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700504 case SUGGEST_COLUMN_FORMAT:
505 return "html";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800506 }
507 }
508 return null;
509 }
510
511 @Override
512 public double getDouble(int column) {
513 throw new UnsupportedOperationException();
514 }
515
516 @Override
517 public float getFloat(int column) {
518 throw new UnsupportedOperationException();
519 }
520
521 @Override
522 public int getInt(int column) {
523 throw new UnsupportedOperationException();
524 }
525
526 @Override
527 public long getLong(int column) {
528 if ((mPos != -1) && column == 0) {
529 return mPos; // use row# as the _Id
530 }
531 throw new UnsupportedOperationException();
532 }
533
534 @Override
535 public short getShort(int column) {
536 throw new UnsupportedOperationException();
537 }
538
539 @Override
540 public boolean isNull(int column) {
541 throw new UnsupportedOperationException();
542 }
543
544 // TODO Temporary change, finalize after jq's changes go in
545 public void deactivate() {
546 if (mHistoryCursor != null) {
547 mHistoryCursor.deactivate();
548 }
549 if (mSuggestCursor != null) {
550 mSuggestCursor.deactivate();
551 }
552 super.deactivate();
553 }
554
555 public boolean requery() {
556 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
557 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
558 }
559
560 // TODO Temporary change, finalize after jq's changes go in
561 public void close() {
562 super.close();
563 if (mHistoryCursor != null) {
564 mHistoryCursor.close();
565 mHistoryCursor = null;
566 }
567 if (mSuggestCursor != null) {
568 mSuggestCursor.close();
569 mSuggestCursor = null;
570 }
571 }
Satish Sampath565505b2009-05-29 15:37:27 +0100572
Mike LeBeau21beb132009-05-13 14:57:50 -0700573 /**
574 * Provides the title (text line 1) for a browser suggestion, which should be the
575 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100576 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700577 * @return the title string to use
578 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700579 private String getHistoryTitle() {
580 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700581 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700582 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700583 }
584 return title;
585 }
Satish Sampath565505b2009-05-29 15:37:27 +0100586
Mike LeBeau21beb132009-05-13 14:57:50 -0700587 /**
588 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
589 * webpage url. If the webpage title is empty, then the url should go in the title
590 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100591 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700592 * @return the subtitle string to use, or null if none
593 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700594 private String getHistorySubtitle() {
595 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700596 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
597 return null;
598 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700599 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700600 }
601 }
Satish Sampath565505b2009-05-29 15:37:27 +0100602
Mike LeBeau21beb132009-05-13 14:57:50 -0700603 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700604 * Strips "http://" from the beginning of a url and "/" from the end,
605 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700606 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700607 private String beautifyUrl(String url) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700608 if (mSearchUrlColorHex == null) {
609 // Get the color used for this purpose from the current theme.
610 TypedValue colorValue = new TypedValue();
611 getContext().getTheme().resolveAttribute(
612 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
613 int color = getContext().getResources().getColor(colorValue.resourceId);
Satish Sampath565505b2009-05-29 15:37:27 +0100614
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700615 // Convert the int color value into a hex string, and strip the first two
616 // characters which will be the alpha transparency (html doesn't want this).
617 mSearchUrlColorHex = Integer.toHexString(color).substring(2);
Mike LeBeau21beb132009-05-13 14:57:50 -0700618 }
Satish Sampath565505b2009-05-29 15:37:27 +0100619
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700620 return "<font color=\"#" + mSearchUrlColorHex + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700621 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800622 }
623
624 @Override
625 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100626 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800627 throws IllegalStateException {
628 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
629
630 int match = URI_MATCHER.match(url);
631 if (match == -1) {
632 throw new IllegalArgumentException("Unknown URL");
633 }
634
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100635 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800636 String suggestSelection;
637 String [] myArgs;
638 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
639 suggestSelection = null;
640 myArgs = null;
641 } else {
642 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700643 if (selectionArgs[0].startsWith("http")
644 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800645 myArgs = new String[1];
646 myArgs[0] = like;
647 suggestSelection = selection;
648 } else {
649 SUGGEST_ARGS[0] = "http://" + like;
650 SUGGEST_ARGS[1] = "http://www." + like;
651 SUGGEST_ARGS[2] = "https://" + like;
652 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400653 // To match against titles.
654 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800655 myArgs = SUGGEST_ARGS;
656 suggestSelection = SUGGEST_SELECTION;
657 }
658 }
659
660 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
661 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Leon Scroggins31887fd2009-05-18 16:58:08 -0400662 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800663
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100664 if (match == URI_MATCH_BOOKMARKS_SUGGEST
665 || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800666 return new MySuggestionCursor(c, null, "");
667 } else {
668 // get Google suggest if there is still space in the list
669 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400670 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800671 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Leon Scroggins62b71f72009-06-12 17:51:22 -0400672 Cursor sc = SearchManager.getSuggestions(
673 getContext(), mSearchableInfo, selectionArgs[0]);
674 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800675 }
676 return new MySuggestionCursor(c, null, selectionArgs[0]);
677 }
678 }
679
680 String[] projection = null;
681 if (projectionIn != null && projectionIn.length > 0) {
682 projection = new String[projectionIn.length + 1];
683 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
684 projection[projectionIn.length] = "_id AS _id";
685 }
686
687 StringBuilder whereClause = new StringBuilder(256);
688 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
689 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
690 .append(")");
691 }
692
693 // Tack on the user's selection, if present
694 if (selection != null && selection.length() > 0) {
695 if (whereClause.length() > 0) {
696 whereClause.append(" AND ");
697 }
698
699 whereClause.append('(');
700 whereClause.append(selection);
701 whereClause.append(')');
702 }
703 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
704 whereClause.toString(), selectionArgs, null, null, sortOrder,
705 null);
706 c.setNotificationUri(getContext().getContentResolver(), url);
707 return c;
708 }
709
710 @Override
711 public String getType(Uri url) {
712 int match = URI_MATCHER.match(url);
713 switch (match) {
714 case URI_MATCH_BOOKMARKS:
715 return "vnd.android.cursor.dir/bookmark";
716
717 case URI_MATCH_BOOKMARKS_ID:
718 return "vnd.android.cursor.item/bookmark";
719
720 case URI_MATCH_SEARCHES:
721 return "vnd.android.cursor.dir/searches";
722
723 case URI_MATCH_SEARCHES_ID:
724 return "vnd.android.cursor.item/searches";
725
726 case URI_MATCH_SUGGEST:
727 return SearchManager.SUGGEST_MIME_TYPE;
728
729 default:
730 throw new IllegalArgumentException("Unknown URL");
731 }
732 }
733
734 @Override
735 public Uri insert(Uri url, ContentValues initialValues) {
736 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
737
738 int match = URI_MATCHER.match(url);
739 Uri uri = null;
740 switch (match) {
741 case URI_MATCH_BOOKMARKS: {
742 // Insert into the bookmarks table
743 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
744 initialValues);
745 if (rowID > 0) {
746 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
747 rowID);
748 }
749 break;
750 }
751
752 case URI_MATCH_SEARCHES: {
753 // Insert into the searches table
754 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
755 initialValues);
756 if (rowID > 0) {
757 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
758 rowID);
759 }
760 break;
761 }
762
763 default:
764 throw new IllegalArgumentException("Unknown URL");
765 }
766
767 if (uri == null) {
768 throw new IllegalArgumentException("Unknown URL");
769 }
770 getContext().getContentResolver().notifyChange(uri, null);
771 return uri;
772 }
773
774 @Override
775 public int delete(Uri url, String where, String[] whereArgs) {
776 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
777
778 int match = URI_MATCHER.match(url);
779 if (match == -1 || match == URI_MATCH_SUGGEST) {
780 throw new IllegalArgumentException("Unknown URL");
781 }
782
783 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
784 StringBuilder sb = new StringBuilder();
785 if (where != null && where.length() > 0) {
786 sb.append("( ");
787 sb.append(where);
788 sb.append(" ) AND ");
789 }
790 sb.append("_id = ");
791 sb.append(url.getPathSegments().get(1));
792 where = sb.toString();
793 }
794
795 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
796 getContext().getContentResolver().notifyChange(url, null);
797 return count;
798 }
799
800 @Override
801 public int update(Uri url, ContentValues values, String where,
802 String[] whereArgs) {
803 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
804
805 int match = URI_MATCHER.match(url);
806 if (match == -1 || match == URI_MATCH_SUGGEST) {
807 throw new IllegalArgumentException("Unknown URL");
808 }
809
810 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
811 StringBuilder sb = new StringBuilder();
812 if (where != null && where.length() > 0) {
813 sb.append("( ");
814 sb.append(where);
815 sb.append(" ) AND ");
816 }
817 sb.append("_id = ");
818 sb.append(url.getPathSegments().get(1));
819 where = sb.toString();
820 }
821
822 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
823 getContext().getContentResolver().notifyChange(url, null);
824 return ret;
825 }
Satish Sampath565505b2009-05-29 15:37:27 +0100826
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700827 /**
828 * Strips the provided url of preceding "http://" and any trailing "/". Does not
829 * strip "https://". If the provided string cannot be stripped, the original string
830 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +0100831 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700832 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +0100833 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700834 * @param url a url to strip, like "http://www.google.com/"
835 * @return a stripped url like "www.google.com", or the original string if it could
836 * not be stripped
837 */
838 private static String stripUrl(String url) {
839 if (url == null) return null;
840 Matcher m = STRIP_URL_PATTERN.matcher(url);
841 if (m.matches() && m.groupCount() == 3) {
842 return m.group(2);
843 } else {
844 return url;
845 }
846 }
847
The Android Open Source Project0c908882009-03-03 19:32:16 -0800848}