blob: c97c0572175b2c169c0c60048d6af23445c73f8a [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 Rajeswaranf447f262009-03-24 20:40:12 -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 Rajeswaranf447f262009-03-24 20:40:12 -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 Scroggins740eadf2009-06-15 12:34:39 -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;
99
100 // make sure that these match the index of TABLE_NAMES
101 private static final int URI_MATCH_BOOKMARKS = 0;
102 private static final int URI_MATCH_SEARCHES = 1;
103 // (id % 10) should match the table name index
104 private static final int URI_MATCH_BOOKMARKS_ID = 10;
105 private static final int URI_MATCH_SEARCHES_ID = 11;
106 //
107 private static final int URI_MATCH_SUGGEST = 20;
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100108 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109
110 private static final UriMatcher URI_MATCHER;
111
112 static {
113 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
114 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
115 URI_MATCH_BOOKMARKS);
116 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
117 URI_MATCH_BOOKMARKS_ID);
118 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
119 URI_MATCH_SEARCHES);
120 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
121 URI_MATCH_SEARCHES_ID);
122 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
123 URI_MATCH_SUGGEST);
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100124 URI_MATCHER.addURI("browser",
125 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
126 URI_MATCH_BOOKMARKS_SUGGEST);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800127 }
128
129 // 1 -> 2 add cache table
130 // 2 -> 3 update history table
131 // 3 -> 4 add passwords table
132 // 4 -> 5 add settings table
133 // 5 -> 6 ?
134 // 6 -> 7 ?
135 // 7 -> 8 drop proxy table
136 // 8 -> 9 drop settings table
137 // 9 -> 10 add form_urls and form_data
138 // 10 -> 11 add searches table
139 // 11 -> 12 modify cache table
140 // 12 -> 13 modify cache table
141 // 13 -> 14 correspond with Google Bookmarks schema
142 // 14 -> 15 move couple of tables to either browser private database or webview database
143 // 15 -> 17 Set it up for the SearchManager
144 // 17 -> 18 Added favicon in bookmarks table for Home shortcuts
145 // 18 -> 19 Remove labels table
146 private static final int DATABASE_VERSION = 19;
Satish Sampath565505b2009-05-29 15:37:27 +0100147
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700148 // Regular expression which matches http://, followed by some stuff, followed by
149 // optionally a trailing slash, all matched as separate groups.
150 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
Satish Sampath565505b2009-05-29 15:37:27 +0100151
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700152 // The hex color string to be applied to urls of website suggestions, as derived from
153 // the current theme. This is not set until/unless beautifyUrl is called, at which point
154 // this variable caches the color value.
155 private static String mSearchUrlColorHex;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800156
157 public BrowserProvider() {
158 }
Satish Sampath565505b2009-05-29 15:37:27 +0100159
The Android Open Source Project0c908882009-03-03 19:32:16 -0800160
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700161 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800162 StringBuffer sb = new StringBuffer();
163 int lastCharLoc = 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100164
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700165 final String client_id = Partner.getString(context.getContentResolver(), Partner.CLIENT_ID);
166
The Android Open Source Project0c908882009-03-03 19:32:16 -0800167 for (int i = 0; i < srcString.length(); ++i) {
168 char c = srcString.charAt(i);
169 if (c == '{') {
170 sb.append(srcString.subSequence(lastCharLoc, i));
171 lastCharLoc = i;
172 inner:
173 for (int j = i; j < srcString.length(); ++j) {
174 char k = srcString.charAt(j);
175 if (k == '}') {
176 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700177 if (propertyKeyValue.equals("CLIENT_ID")) {
178 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800179 } else {
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700180 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800181 }
182 lastCharLoc = j + 1;
183 i = j;
184 break inner;
185 }
186 }
187 }
188 }
189 if (srcString.length() - lastCharLoc > 0) {
190 // Put on the tail, if there is one
191 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
192 }
193 return sb;
194 }
195
196 private static class DatabaseHelper extends SQLiteOpenHelper {
197 private Context mContext;
198
199 public DatabaseHelper(Context context) {
200 super(context, sDatabaseName, null, DATABASE_VERSION);
201 mContext = context;
202 }
203
204 @Override
205 public void onCreate(SQLiteDatabase db) {
206 db.execSQL("CREATE TABLE bookmarks (" +
207 "_id INTEGER PRIMARY KEY," +
208 "title TEXT," +
209 "url TEXT," +
210 "visits INTEGER," +
211 "date LONG," +
212 "created LONG," +
213 "description TEXT," +
214 "bookmark INTEGER," +
215 "favicon BLOB DEFAULT NULL" +
216 ");");
217
218 final CharSequence[] bookmarks = mContext.getResources()
219 .getTextArray(R.array.bookmarks);
220 int size = bookmarks.length;
221 try {
222 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700223 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800224 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
225 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100226 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800227 "', 0, 0, 0, 1);");
228 }
229 } catch (ArrayIndexOutOfBoundsException e) {
230 }
231
232 db.execSQL("CREATE TABLE searches (" +
233 "_id INTEGER PRIMARY KEY," +
234 "search TEXT," +
235 "date LONG" +
236 ");");
237 }
238
239 @Override
240 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
241 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
242 + newVersion + ", which will destroy all old data");
243 if (oldVersion == 18) {
244 db.execSQL("DROP TABLE IF EXISTS labels");
245 } else {
246 db.execSQL("DROP TABLE IF EXISTS bookmarks");
247 db.execSQL("DROP TABLE IF EXISTS searches");
248 onCreate(db);
249 }
250 }
251 }
252
253 @Override
254 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700255 final Context context = getContext();
256 mOpenHelper = new DatabaseHelper(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100257 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700258 // To avoid erasing the bookmark table, we added it explicitly for
259 // version 18 and 19 as in the other cases, we will erase the table.
260 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
261 SharedPreferences p = PreferenceManager
262 .getDefaultSharedPreferences(context);
263 boolean fix = p.getBoolean("fix_picasa", true);
264 if (fix) {
265 fixPicasaBookmark();
266 Editor ed = p.edit();
267 ed.putBoolean("fix_picasa", false);
268 ed.commit();
269 }
270 }
Leon Scroggins62b71f72009-06-12 17:51:22 -0400271 mShowWebSuggestionsSettingChangeObserver
272 = new ShowWebSuggestionsSettingChangeObserver();
273 context.getContentResolver().registerContentObserver(
274 Settings.System.getUriFor(
275 Settings.System.SHOW_WEB_SUGGESTIONS),
276 true, mShowWebSuggestionsSettingChangeObserver);
277 updateShowWebSuggestions();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800278 return true;
279 }
280
Leon Scroggins62b71f72009-06-12 17:51:22 -0400281 /**
282 * This Observer will ensure that if the user changes the system
283 * setting of whether to display web suggestions, we will
284 * change accordingly.
285 */
286 /* package */ class ShowWebSuggestionsSettingChangeObserver
287 extends ContentObserver {
288 public ShowWebSuggestionsSettingChangeObserver() {
289 super(new Handler());
290 }
291
292 @Override
293 public void onChange(boolean selfChange) {
294 updateShowWebSuggestions();
295 }
296 }
297
298 private ShowWebSuggestionsSettingChangeObserver
299 mShowWebSuggestionsSettingChangeObserver;
300
301 // If non-null, then the system is set to show web suggestions,
302 // and this is the SearchableInfo to use to get them.
303 private SearchableInfo mSearchableInfo;
304
305 /**
306 * Check the system settings to see whether web suggestions are
307 * allowed. If so, store the SearchableInfo to grab suggestions
308 * while the user is typing.
309 */
310 private void updateShowWebSuggestions() {
311 mSearchableInfo = null;
312 Context context = getContext();
313 if (Settings.System.getInt(context.getContentResolver(),
314 Settings.System.SHOW_WEB_SUGGESTIONS,
315 1 /* default on */) == 1) {
316 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
317 intent.addCategory(Intent.CATEGORY_DEFAULT);
318 ResolveInfo info = context.getPackageManager().resolveActivity(
319 intent, PackageManager.MATCH_DEFAULT_ONLY);
320 if (info != null) {
321 ComponentName googleSearchComponent =
322 new ComponentName(info.activityInfo.packageName,
323 info.activityInfo.name);
324 mSearchableInfo = SearchManager.getSearchableInfo(
325 googleSearchComponent, false);
326 }
327 }
328 }
329
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700330 private void fixPicasaBookmark() {
331 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
332 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
333 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
334 try {
335 if (!cursor.moveToFirst()) {
336 // set "created" so that it will be on the top of the list
337 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
338 "date, created, bookmark)" + " VALUES('" +
339 getContext().getString(R.string.picasa) + "', '"
340 + PICASA_URL + "', 0, 0, " + new Date().getTime()
341 + ", 1);");
342 }
343 } finally {
344 if (cursor != null) {
345 cursor.close();
346 }
347 }
348 }
349
The Android Open Source Project0c908882009-03-03 19:32:16 -0800350 /*
351 * Subclass AbstractCursor so we can combine multiple Cursors and add
352 * "Google Search".
353 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100354 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
The Android Open Source Project0c908882009-03-03 19:32:16 -0800355 * "Google Search";
Satish Sampath565505b2009-05-29 15:37:27 +0100356 * 2. If bookmark/history entries are less than
The Android Open Source Project0c908882009-03-03 19:32:16 -0800357 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
358 */
359 private class MySuggestionCursor extends AbstractCursor {
360 private Cursor mHistoryCursor;
361 private Cursor mSuggestCursor;
362 private int mHistoryCount;
363 private int mSuggestionCount;
364 private boolean mBeyondCursor;
365 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100366 private int mSuggestText1Id;
367 private int mSuggestText2Id;
368 private int mSuggestQueryId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800369
370 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
371 mHistoryCursor = hc;
372 mSuggestCursor = sc;
373 mHistoryCount = hc.getCount();
374 mSuggestionCount = sc != null ? sc.getCount() : 0;
375 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
376 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
377 }
378 mString = string;
379 mBeyondCursor = false;
Satish Sampath565505b2009-05-29 15:37:27 +0100380
381 // Some web suggest providers only give suggestions and have no description string for
382 // items. The order of the result columns may be different as well. So retrieve the
383 // column indices for the fields we need now and check before using below.
384 if (mSuggestCursor == null) {
385 mSuggestText1Id = -1;
386 mSuggestText2Id = -1;
387 mSuggestQueryId = -1;
388 } else {
389 mSuggestText1Id = mSuggestCursor.getColumnIndex(
390 SearchManager.SUGGEST_COLUMN_TEXT_1);
391 mSuggestText2Id = mSuggestCursor.getColumnIndex(
392 SearchManager.SUGGEST_COLUMN_TEXT_2);
393 mSuggestQueryId = mSuggestCursor.getColumnIndex(
394 SearchManager.SUGGEST_COLUMN_QUERY);
395 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800396 }
397
398 @Override
399 public boolean onMove(int oldPosition, int newPosition) {
400 if (mHistoryCursor == null) {
401 return false;
402 }
403 if (mHistoryCount > newPosition) {
404 mHistoryCursor.moveToPosition(newPosition);
405 mBeyondCursor = false;
406 } else if (mHistoryCount + mSuggestionCount > newPosition) {
407 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
408 mBeyondCursor = false;
409 } else {
410 mBeyondCursor = true;
411 }
412 return true;
413 }
414
415 @Override
416 public int getCount() {
417 if (mString.length() > 0) {
418 return mHistoryCount + mSuggestionCount + 1;
419 } else {
420 return mHistoryCount + mSuggestionCount;
421 }
422 }
423
424 @Override
425 public String[] getColumnNames() {
426 return COLUMNS;
427 }
Satish Sampath565505b2009-05-29 15:37:27 +0100428
The Android Open Source Project0c908882009-03-03 19:32:16 -0800429 @Override
430 public String getString(int columnIndex) {
431 if ((mPos != -1 && mHistoryCursor != null)) {
432 switch(columnIndex) {
433 case SUGGEST_COLUMN_INTENT_ACTION_ID:
434 if (mHistoryCount > mPos) {
435 return Intent.ACTION_VIEW;
436 } else {
437 return Intent.ACTION_SEARCH;
438 }
439
440 case SUGGEST_COLUMN_INTENT_DATA_ID:
441 if (mHistoryCount > mPos) {
442 return mHistoryCursor.getString(1);
443 } else {
444 return null;
445 }
446
447 case SUGGEST_COLUMN_TEXT_1_ID:
448 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700449 return getHistoryTitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800450 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100451 if (mSuggestText1Id == -1) return null;
452 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800453 } else {
454 return mString;
455 }
456
457 case SUGGEST_COLUMN_TEXT_2_ID:
458 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700459 return getHistorySubtitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800460 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100461 if (mSuggestText2Id == -1) return null;
462 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800463 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700464 return getContext().getString(R.string.search_the_web);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800465 }
466
467 case SUGGEST_COLUMN_ICON_1_ID:
468 if (mHistoryCount > mPos) {
469 if (mHistoryCursor.getInt(3) == 1) {
470 return new Integer(
471 R.drawable.ic_search_category_bookmark)
472 .toString();
473 } else {
474 return new Integer(
475 R.drawable.ic_search_category_history)
476 .toString();
477 }
478 } else {
479 return new Integer(
480 R.drawable.ic_search_category_suggest)
481 .toString();
482 }
483
484 case SUGGEST_COLUMN_ICON_2_ID:
485 return new String("0");
486
487 case SUGGEST_COLUMN_QUERY_ID:
488 if (mHistoryCount > mPos) {
489 return null;
490 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100491 if (mSuggestQueryId == -1) return null;
492 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800493 } else {
494 return mString;
495 }
Satish Sampath565505b2009-05-29 15:37:27 +0100496
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700497 case SUGGEST_COLUMN_FORMAT:
498 return "html";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800499 }
500 }
501 return null;
502 }
503
504 @Override
505 public double getDouble(int column) {
506 throw new UnsupportedOperationException();
507 }
508
509 @Override
510 public float getFloat(int column) {
511 throw new UnsupportedOperationException();
512 }
513
514 @Override
515 public int getInt(int column) {
516 throw new UnsupportedOperationException();
517 }
518
519 @Override
520 public long getLong(int column) {
521 if ((mPos != -1) && column == 0) {
522 return mPos; // use row# as the _Id
523 }
524 throw new UnsupportedOperationException();
525 }
526
527 @Override
528 public short getShort(int column) {
529 throw new UnsupportedOperationException();
530 }
531
532 @Override
533 public boolean isNull(int column) {
534 throw new UnsupportedOperationException();
535 }
536
537 // TODO Temporary change, finalize after jq's changes go in
538 public void deactivate() {
539 if (mHistoryCursor != null) {
540 mHistoryCursor.deactivate();
541 }
542 if (mSuggestCursor != null) {
543 mSuggestCursor.deactivate();
544 }
545 super.deactivate();
546 }
547
548 public boolean requery() {
549 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
550 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
551 }
552
553 // TODO Temporary change, finalize after jq's changes go in
554 public void close() {
555 super.close();
556 if (mHistoryCursor != null) {
557 mHistoryCursor.close();
558 mHistoryCursor = null;
559 }
560 if (mSuggestCursor != null) {
561 mSuggestCursor.close();
562 mSuggestCursor = null;
563 }
564 }
Satish Sampath565505b2009-05-29 15:37:27 +0100565
Mike LeBeau21beb132009-05-13 14:57:50 -0700566 /**
567 * Provides the title (text line 1) for a browser suggestion, which should be the
568 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100569 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700570 * @return the title string to use
571 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700572 private String getHistoryTitle() {
573 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700574 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700575 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700576 }
577 return title;
578 }
Satish Sampath565505b2009-05-29 15:37:27 +0100579
Mike LeBeau21beb132009-05-13 14:57:50 -0700580 /**
581 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
582 * webpage url. If the webpage title is empty, then the url should go in the title
583 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100584 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700585 * @return the subtitle string to use, or null if none
586 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700587 private String getHistorySubtitle() {
588 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700589 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
590 return null;
591 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700592 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700593 }
594 }
Satish Sampath565505b2009-05-29 15:37:27 +0100595
Mike LeBeau21beb132009-05-13 14:57:50 -0700596 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700597 * Strips "http://" from the beginning of a url and "/" from the end,
598 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700599 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700600 private String beautifyUrl(String url) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700601 if (mSearchUrlColorHex == null) {
602 // Get the color used for this purpose from the current theme.
603 TypedValue colorValue = new TypedValue();
604 getContext().getTheme().resolveAttribute(
605 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
606 int color = getContext().getResources().getColor(colorValue.resourceId);
Satish Sampath565505b2009-05-29 15:37:27 +0100607
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700608 // Convert the int color value into a hex string, and strip the first two
609 // characters which will be the alpha transparency (html doesn't want this).
610 mSearchUrlColorHex = Integer.toHexString(color).substring(2);
Mike LeBeau21beb132009-05-13 14:57:50 -0700611 }
Satish Sampath565505b2009-05-29 15:37:27 +0100612
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700613 return "<font color=\"#" + mSearchUrlColorHex + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700614 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800615 }
616
617 @Override
618 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100619 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800620 throws IllegalStateException {
621 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
622
623 int match = URI_MATCHER.match(url);
624 if (match == -1) {
625 throw new IllegalArgumentException("Unknown URL");
626 }
627
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100628 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800629 String suggestSelection;
630 String [] myArgs;
631 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
632 suggestSelection = null;
633 myArgs = null;
634 } else {
635 String like = selectionArgs[0] + "%";
636 if (selectionArgs[0].startsWith("http")) {
637 myArgs = new String[1];
638 myArgs[0] = like;
639 suggestSelection = selection;
640 } else {
641 SUGGEST_ARGS[0] = "http://" + like;
642 SUGGEST_ARGS[1] = "http://www." + like;
643 SUGGEST_ARGS[2] = "https://" + like;
644 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scroggins740eadf2009-06-15 12:34:39 -0400645 // To match against titles.
646 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800647 myArgs = SUGGEST_ARGS;
648 suggestSelection = SUGGEST_SELECTION;
649 }
650 }
651
652 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
653 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
654 ORDER_BY,
655 (new Integer(MAX_SUGGESTION_LONG_ENTRIES)).toString());
656
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100657 if (match == URI_MATCH_BOOKMARKS_SUGGEST
658 || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800659 return new MySuggestionCursor(c, null, "");
660 } else {
661 // get Google suggest if there is still space in the list
662 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400663 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800664 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Leon Scroggins62b71f72009-06-12 17:51:22 -0400665 Cursor sc = SearchManager.getSuggestions(
666 getContext(), mSearchableInfo, selectionArgs[0]);
667 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800668 }
669 return new MySuggestionCursor(c, null, selectionArgs[0]);
670 }
671 }
672
673 String[] projection = null;
674 if (projectionIn != null && projectionIn.length > 0) {
675 projection = new String[projectionIn.length + 1];
676 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
677 projection[projectionIn.length] = "_id AS _id";
678 }
679
680 StringBuilder whereClause = new StringBuilder(256);
681 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
682 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
683 .append(")");
684 }
685
686 // Tack on the user's selection, if present
687 if (selection != null && selection.length() > 0) {
688 if (whereClause.length() > 0) {
689 whereClause.append(" AND ");
690 }
691
692 whereClause.append('(');
693 whereClause.append(selection);
694 whereClause.append(')');
695 }
696 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
697 whereClause.toString(), selectionArgs, null, null, sortOrder,
698 null);
699 c.setNotificationUri(getContext().getContentResolver(), url);
700 return c;
701 }
702
703 @Override
704 public String getType(Uri url) {
705 int match = URI_MATCHER.match(url);
706 switch (match) {
707 case URI_MATCH_BOOKMARKS:
708 return "vnd.android.cursor.dir/bookmark";
709
710 case URI_MATCH_BOOKMARKS_ID:
711 return "vnd.android.cursor.item/bookmark";
712
713 case URI_MATCH_SEARCHES:
714 return "vnd.android.cursor.dir/searches";
715
716 case URI_MATCH_SEARCHES_ID:
717 return "vnd.android.cursor.item/searches";
718
719 case URI_MATCH_SUGGEST:
720 return SearchManager.SUGGEST_MIME_TYPE;
721
722 default:
723 throw new IllegalArgumentException("Unknown URL");
724 }
725 }
726
727 @Override
728 public Uri insert(Uri url, ContentValues initialValues) {
729 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
730
731 int match = URI_MATCHER.match(url);
732 Uri uri = null;
733 switch (match) {
734 case URI_MATCH_BOOKMARKS: {
735 // Insert into the bookmarks table
736 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
737 initialValues);
738 if (rowID > 0) {
739 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
740 rowID);
741 }
742 break;
743 }
744
745 case URI_MATCH_SEARCHES: {
746 // Insert into the searches table
747 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
748 initialValues);
749 if (rowID > 0) {
750 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
751 rowID);
752 }
753 break;
754 }
755
756 default:
757 throw new IllegalArgumentException("Unknown URL");
758 }
759
760 if (uri == null) {
761 throw new IllegalArgumentException("Unknown URL");
762 }
763 getContext().getContentResolver().notifyChange(uri, null);
764 return uri;
765 }
766
767 @Override
768 public int delete(Uri url, String where, String[] whereArgs) {
769 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
770
771 int match = URI_MATCHER.match(url);
772 if (match == -1 || match == URI_MATCH_SUGGEST) {
773 throw new IllegalArgumentException("Unknown URL");
774 }
775
776 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
777 StringBuilder sb = new StringBuilder();
778 if (where != null && where.length() > 0) {
779 sb.append("( ");
780 sb.append(where);
781 sb.append(" ) AND ");
782 }
783 sb.append("_id = ");
784 sb.append(url.getPathSegments().get(1));
785 where = sb.toString();
786 }
787
788 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
789 getContext().getContentResolver().notifyChange(url, null);
790 return count;
791 }
792
793 @Override
794 public int update(Uri url, ContentValues values, String where,
795 String[] whereArgs) {
796 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
797
798 int match = URI_MATCHER.match(url);
799 if (match == -1 || match == URI_MATCH_SUGGEST) {
800 throw new IllegalArgumentException("Unknown URL");
801 }
802
803 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
804 StringBuilder sb = new StringBuilder();
805 if (where != null && where.length() > 0) {
806 sb.append("( ");
807 sb.append(where);
808 sb.append(" ) AND ");
809 }
810 sb.append("_id = ");
811 sb.append(url.getPathSegments().get(1));
812 where = sb.toString();
813 }
814
815 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
816 getContext().getContentResolver().notifyChange(url, null);
817 return ret;
818 }
Satish Sampath565505b2009-05-29 15:37:27 +0100819
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700820 /**
821 * Strips the provided url of preceding "http://" and any trailing "/". Does not
822 * strip "https://". If the provided string cannot be stripped, the original string
823 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +0100824 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700825 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +0100826 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700827 * @param url a url to strip, like "http://www.google.com/"
828 * @return a stripped url like "www.google.com", or the original string if it could
829 * not be stripped
830 */
831 private static String stripUrl(String url) {
832 if (url == null) return null;
833 Matcher m = STRIP_URL_PATTERN.matcher(url);
834 if (m.matches() && m.groupCount() == 3) {
835 return m.group(2);
836 } else {
837 return url;
838 }
839 }
840
The Android Open Source Project0c908882009-03-03 19:32:16 -0800841}