blob: 8743254bdd3d451528997e084c6ec25b4f334ce4 [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
148 private static final int DATABASE_VERSION = 19;
Satish Sampath565505b2009-05-29 15:37:27 +0100149
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700150 // Regular expression which matches http://, followed by some stuff, followed by
151 // optionally a trailing slash, all matched as separate groups.
152 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
Satish Sampath565505b2009-05-29 15:37:27 +0100153
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700154 // The hex color string to be applied to urls of website suggestions, as derived from
155 // the current theme. This is not set until/unless beautifyUrl is called, at which point
156 // this variable caches the color value.
157 private static String mSearchUrlColorHex;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158
159 public BrowserProvider() {
160 }
Satish Sampath565505b2009-05-29 15:37:27 +0100161
The Android Open Source Project0c908882009-03-03 19:32:16 -0800162
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700163 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164 StringBuffer sb = new StringBuffer();
165 int lastCharLoc = 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100166
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700167 final String client_id = Partner.getString(context.getContentResolver(), Partner.CLIENT_ID);
168
The Android Open Source Project0c908882009-03-03 19:32:16 -0800169 for (int i = 0; i < srcString.length(); ++i) {
170 char c = srcString.charAt(i);
171 if (c == '{') {
172 sb.append(srcString.subSequence(lastCharLoc, i));
173 lastCharLoc = i;
174 inner:
175 for (int j = i; j < srcString.length(); ++j) {
176 char k = srcString.charAt(j);
177 if (k == '}') {
178 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700179 if (propertyKeyValue.equals("CLIENT_ID")) {
180 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800181 } else {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700182 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183 }
184 lastCharLoc = j + 1;
185 i = j;
186 break inner;
187 }
188 }
189 }
190 }
191 if (srcString.length() - lastCharLoc > 0) {
192 // Put on the tail, if there is one
193 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
194 }
195 return sb;
196 }
197
198 private static class DatabaseHelper extends SQLiteOpenHelper {
199 private Context mContext;
200
201 public DatabaseHelper(Context context) {
202 super(context, sDatabaseName, null, DATABASE_VERSION);
203 mContext = context;
204 }
205
206 @Override
207 public void onCreate(SQLiteDatabase db) {
208 db.execSQL("CREATE TABLE bookmarks (" +
209 "_id INTEGER PRIMARY KEY," +
210 "title TEXT," +
211 "url TEXT," +
212 "visits INTEGER," +
213 "date LONG," +
214 "created LONG," +
215 "description TEXT," +
216 "bookmark INTEGER," +
217 "favicon BLOB DEFAULT NULL" +
218 ");");
219
220 final CharSequence[] bookmarks = mContext.getResources()
221 .getTextArray(R.array.bookmarks);
222 int size = bookmarks.length;
223 try {
224 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700225 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800226 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
227 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100228 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800229 "', 0, 0, 0, 1);");
230 }
231 } catch (ArrayIndexOutOfBoundsException e) {
232 }
233
234 db.execSQL("CREATE TABLE searches (" +
235 "_id INTEGER PRIMARY KEY," +
236 "search TEXT," +
237 "date LONG" +
238 ");");
239 }
240
241 @Override
242 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
243 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
244 + newVersion + ", which will destroy all old data");
245 if (oldVersion == 18) {
246 db.execSQL("DROP TABLE IF EXISTS labels");
247 } else {
248 db.execSQL("DROP TABLE IF EXISTS bookmarks");
249 db.execSQL("DROP TABLE IF EXISTS searches");
250 onCreate(db);
251 }
252 }
253 }
254
255 @Override
256 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700257 final Context context = getContext();
258 mOpenHelper = new DatabaseHelper(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100259 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700260 // To avoid erasing the bookmark table, we added it explicitly for
261 // version 18 and 19 as in the other cases, we will erase the table.
262 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
263 SharedPreferences p = PreferenceManager
264 .getDefaultSharedPreferences(context);
265 boolean fix = p.getBoolean("fix_picasa", true);
266 if (fix) {
267 fixPicasaBookmark();
268 Editor ed = p.edit();
269 ed.putBoolean("fix_picasa", false);
270 ed.commit();
271 }
272 }
Leon Scroggins62b71f72009-06-12 17:51:22 -0400273 mShowWebSuggestionsSettingChangeObserver
274 = new ShowWebSuggestionsSettingChangeObserver();
275 context.getContentResolver().registerContentObserver(
276 Settings.System.getUriFor(
277 Settings.System.SHOW_WEB_SUGGESTIONS),
278 true, mShowWebSuggestionsSettingChangeObserver);
279 updateShowWebSuggestions();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800280 return true;
281 }
282
Leon Scroggins62b71f72009-06-12 17:51:22 -0400283 /**
284 * This Observer will ensure that if the user changes the system
285 * setting of whether to display web suggestions, we will
286 * change accordingly.
287 */
288 /* package */ class ShowWebSuggestionsSettingChangeObserver
289 extends ContentObserver {
290 public ShowWebSuggestionsSettingChangeObserver() {
291 super(new Handler());
292 }
293
294 @Override
295 public void onChange(boolean selfChange) {
296 updateShowWebSuggestions();
297 }
298 }
299
300 private ShowWebSuggestionsSettingChangeObserver
301 mShowWebSuggestionsSettingChangeObserver;
302
303 // If non-null, then the system is set to show web suggestions,
304 // and this is the SearchableInfo to use to get them.
305 private SearchableInfo mSearchableInfo;
306
307 /**
308 * Check the system settings to see whether web suggestions are
309 * allowed. If so, store the SearchableInfo to grab suggestions
310 * while the user is typing.
311 */
312 private void updateShowWebSuggestions() {
313 mSearchableInfo = null;
314 Context context = getContext();
315 if (Settings.System.getInt(context.getContentResolver(),
316 Settings.System.SHOW_WEB_SUGGESTIONS,
317 1 /* default on */) == 1) {
318 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
319 intent.addCategory(Intent.CATEGORY_DEFAULT);
320 ResolveInfo info = context.getPackageManager().resolveActivity(
321 intent, PackageManager.MATCH_DEFAULT_ONLY);
322 if (info != null) {
323 ComponentName googleSearchComponent =
324 new ComponentName(info.activityInfo.packageName,
325 info.activityInfo.name);
326 mSearchableInfo = SearchManager.getSearchableInfo(
327 googleSearchComponent, false);
328 }
329 }
330 }
331
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700332 private void fixPicasaBookmark() {
333 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
334 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
335 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
336 try {
337 if (!cursor.moveToFirst()) {
338 // set "created" so that it will be on the top of the list
339 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
340 "date, created, bookmark)" + " VALUES('" +
341 getContext().getString(R.string.picasa) + "', '"
342 + PICASA_URL + "', 0, 0, " + new Date().getTime()
343 + ", 1);");
344 }
345 } finally {
346 if (cursor != null) {
347 cursor.close();
348 }
349 }
350 }
351
The Android Open Source Project0c908882009-03-03 19:32:16 -0800352 /*
353 * Subclass AbstractCursor so we can combine multiple Cursors and add
354 * "Google Search".
355 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100356 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
The Android Open Source Project0c908882009-03-03 19:32:16 -0800357 * "Google Search";
Satish Sampath565505b2009-05-29 15:37:27 +0100358 * 2. If bookmark/history entries are less than
The Android Open Source Project0c908882009-03-03 19:32:16 -0800359 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
360 */
361 private class MySuggestionCursor extends AbstractCursor {
362 private Cursor mHistoryCursor;
363 private Cursor mSuggestCursor;
364 private int mHistoryCount;
365 private int mSuggestionCount;
366 private boolean mBeyondCursor;
367 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100368 private int mSuggestText1Id;
369 private int mSuggestText2Id;
370 private int mSuggestQueryId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800371
372 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
373 mHistoryCursor = hc;
374 mSuggestCursor = sc;
375 mHistoryCount = hc.getCount();
376 mSuggestionCount = sc != null ? sc.getCount() : 0;
377 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
378 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
379 }
380 mString = string;
381 mBeyondCursor = false;
Satish Sampath565505b2009-05-29 15:37:27 +0100382
383 // Some web suggest providers only give suggestions and have no description string for
384 // items. The order of the result columns may be different as well. So retrieve the
385 // column indices for the fields we need now and check before using below.
386 if (mSuggestCursor == null) {
387 mSuggestText1Id = -1;
388 mSuggestText2Id = -1;
389 mSuggestQueryId = -1;
390 } else {
391 mSuggestText1Id = mSuggestCursor.getColumnIndex(
392 SearchManager.SUGGEST_COLUMN_TEXT_1);
393 mSuggestText2Id = mSuggestCursor.getColumnIndex(
394 SearchManager.SUGGEST_COLUMN_TEXT_2);
395 mSuggestQueryId = mSuggestCursor.getColumnIndex(
396 SearchManager.SUGGEST_COLUMN_QUERY);
397 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800398 }
399
400 @Override
401 public boolean onMove(int oldPosition, int newPosition) {
402 if (mHistoryCursor == null) {
403 return false;
404 }
405 if (mHistoryCount > newPosition) {
406 mHistoryCursor.moveToPosition(newPosition);
407 mBeyondCursor = false;
408 } else if (mHistoryCount + mSuggestionCount > newPosition) {
409 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
410 mBeyondCursor = false;
411 } else {
412 mBeyondCursor = true;
413 }
414 return true;
415 }
416
417 @Override
418 public int getCount() {
419 if (mString.length() > 0) {
420 return mHistoryCount + mSuggestionCount + 1;
421 } else {
422 return mHistoryCount + mSuggestionCount;
423 }
424 }
425
426 @Override
427 public String[] getColumnNames() {
428 return COLUMNS;
429 }
Satish Sampath565505b2009-05-29 15:37:27 +0100430
The Android Open Source Project0c908882009-03-03 19:32:16 -0800431 @Override
432 public String getString(int columnIndex) {
433 if ((mPos != -1 && mHistoryCursor != null)) {
434 switch(columnIndex) {
435 case SUGGEST_COLUMN_INTENT_ACTION_ID:
436 if (mHistoryCount > mPos) {
437 return Intent.ACTION_VIEW;
438 } else {
439 return Intent.ACTION_SEARCH;
440 }
441
442 case SUGGEST_COLUMN_INTENT_DATA_ID:
443 if (mHistoryCount > mPos) {
444 return mHistoryCursor.getString(1);
445 } else {
446 return null;
447 }
448
449 case SUGGEST_COLUMN_TEXT_1_ID:
450 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700451 return getHistoryTitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800452 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100453 if (mSuggestText1Id == -1) return null;
454 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800455 } else {
456 return mString;
457 }
458
459 case SUGGEST_COLUMN_TEXT_2_ID:
460 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700461 return getHistorySubtitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800462 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100463 if (mSuggestText2Id == -1) return null;
464 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800465 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700466 return getContext().getString(R.string.search_the_web);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 }
468
469 case SUGGEST_COLUMN_ICON_1_ID:
470 if (mHistoryCount > mPos) {
471 if (mHistoryCursor.getInt(3) == 1) {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400472 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800473 R.drawable.ic_search_category_bookmark)
474 .toString();
475 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400476 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800477 R.drawable.ic_search_category_history)
478 .toString();
479 }
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_suggest)
483 .toString();
484 }
485
486 case SUGGEST_COLUMN_ICON_2_ID:
Leon Scroggins31887fd2009-05-18 16:58:08 -0400487 return "0";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800488
489 case SUGGEST_COLUMN_QUERY_ID:
490 if (mHistoryCount > mPos) {
491 return null;
492 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100493 if (mSuggestQueryId == -1) return null;
494 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800495 } else {
496 return mString;
497 }
Satish Sampath565505b2009-05-29 15:37:27 +0100498
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700499 case SUGGEST_COLUMN_FORMAT:
500 return "html";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800501 }
502 }
503 return null;
504 }
505
506 @Override
507 public double getDouble(int column) {
508 throw new UnsupportedOperationException();
509 }
510
511 @Override
512 public float getFloat(int column) {
513 throw new UnsupportedOperationException();
514 }
515
516 @Override
517 public int getInt(int column) {
518 throw new UnsupportedOperationException();
519 }
520
521 @Override
522 public long getLong(int column) {
523 if ((mPos != -1) && column == 0) {
524 return mPos; // use row# as the _Id
525 }
526 throw new UnsupportedOperationException();
527 }
528
529 @Override
530 public short getShort(int column) {
531 throw new UnsupportedOperationException();
532 }
533
534 @Override
535 public boolean isNull(int column) {
536 throw new UnsupportedOperationException();
537 }
538
539 // TODO Temporary change, finalize after jq's changes go in
540 public void deactivate() {
541 if (mHistoryCursor != null) {
542 mHistoryCursor.deactivate();
543 }
544 if (mSuggestCursor != null) {
545 mSuggestCursor.deactivate();
546 }
547 super.deactivate();
548 }
549
550 public boolean requery() {
551 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
552 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
553 }
554
555 // TODO Temporary change, finalize after jq's changes go in
556 public void close() {
557 super.close();
558 if (mHistoryCursor != null) {
559 mHistoryCursor.close();
560 mHistoryCursor = null;
561 }
562 if (mSuggestCursor != null) {
563 mSuggestCursor.close();
564 mSuggestCursor = null;
565 }
566 }
Satish Sampath565505b2009-05-29 15:37:27 +0100567
Mike LeBeau21beb132009-05-13 14:57:50 -0700568 /**
569 * Provides the title (text line 1) for a browser suggestion, which should be the
570 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100571 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700572 * @return the title string to use
573 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700574 private String getHistoryTitle() {
575 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700576 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700577 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700578 }
579 return title;
580 }
Satish Sampath565505b2009-05-29 15:37:27 +0100581
Mike LeBeau21beb132009-05-13 14:57:50 -0700582 /**
583 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
584 * webpage url. If the webpage title is empty, then the url should go in the title
585 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100586 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700587 * @return the subtitle string to use, or null if none
588 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700589 private String getHistorySubtitle() {
590 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700591 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
592 return null;
593 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700594 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700595 }
596 }
Satish Sampath565505b2009-05-29 15:37:27 +0100597
Mike LeBeau21beb132009-05-13 14:57:50 -0700598 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700599 * Strips "http://" from the beginning of a url and "/" from the end,
600 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700601 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700602 private String beautifyUrl(String url) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700603 if (mSearchUrlColorHex == null) {
604 // Get the color used for this purpose from the current theme.
605 TypedValue colorValue = new TypedValue();
606 getContext().getTheme().resolveAttribute(
607 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
608 int color = getContext().getResources().getColor(colorValue.resourceId);
Satish Sampath565505b2009-05-29 15:37:27 +0100609
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700610 // Convert the int color value into a hex string, and strip the first two
611 // characters which will be the alpha transparency (html doesn't want this).
612 mSearchUrlColorHex = Integer.toHexString(color).substring(2);
Mike LeBeau21beb132009-05-13 14:57:50 -0700613 }
Satish Sampath565505b2009-05-29 15:37:27 +0100614
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700615 return "<font color=\"#" + mSearchUrlColorHex + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700616 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800617 }
618
619 @Override
620 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100621 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800622 throws IllegalStateException {
623 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
624
625 int match = URI_MATCHER.match(url);
626 if (match == -1) {
627 throw new IllegalArgumentException("Unknown URL");
628 }
629
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100630 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800631 String suggestSelection;
632 String [] myArgs;
633 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
634 suggestSelection = null;
635 myArgs = null;
636 } else {
637 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700638 if (selectionArgs[0].startsWith("http")
639 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800640 myArgs = new String[1];
641 myArgs[0] = like;
642 suggestSelection = selection;
643 } else {
644 SUGGEST_ARGS[0] = "http://" + like;
645 SUGGEST_ARGS[1] = "http://www." + like;
646 SUGGEST_ARGS[2] = "https://" + like;
647 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400648 // To match against titles.
649 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800650 myArgs = SUGGEST_ARGS;
651 suggestSelection = SUGGEST_SELECTION;
652 }
653 }
654
655 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
656 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Leon Scroggins31887fd2009-05-18 16:58:08 -0400657 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800658
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100659 if (match == URI_MATCH_BOOKMARKS_SUGGEST
660 || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800661 return new MySuggestionCursor(c, null, "");
662 } else {
663 // get Google suggest if there is still space in the list
664 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400665 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800666 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Leon Scroggins62b71f72009-06-12 17:51:22 -0400667 Cursor sc = SearchManager.getSuggestions(
668 getContext(), mSearchableInfo, selectionArgs[0]);
669 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800670 }
671 return new MySuggestionCursor(c, null, selectionArgs[0]);
672 }
673 }
674
675 String[] projection = null;
676 if (projectionIn != null && projectionIn.length > 0) {
677 projection = new String[projectionIn.length + 1];
678 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
679 projection[projectionIn.length] = "_id AS _id";
680 }
681
682 StringBuilder whereClause = new StringBuilder(256);
683 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
684 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
685 .append(")");
686 }
687
688 // Tack on the user's selection, if present
689 if (selection != null && selection.length() > 0) {
690 if (whereClause.length() > 0) {
691 whereClause.append(" AND ");
692 }
693
694 whereClause.append('(');
695 whereClause.append(selection);
696 whereClause.append(')');
697 }
698 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
699 whereClause.toString(), selectionArgs, null, null, sortOrder,
700 null);
701 c.setNotificationUri(getContext().getContentResolver(), url);
702 return c;
703 }
704
705 @Override
706 public String getType(Uri url) {
707 int match = URI_MATCHER.match(url);
708 switch (match) {
709 case URI_MATCH_BOOKMARKS:
710 return "vnd.android.cursor.dir/bookmark";
711
712 case URI_MATCH_BOOKMARKS_ID:
713 return "vnd.android.cursor.item/bookmark";
714
715 case URI_MATCH_SEARCHES:
716 return "vnd.android.cursor.dir/searches";
717
718 case URI_MATCH_SEARCHES_ID:
719 return "vnd.android.cursor.item/searches";
720
721 case URI_MATCH_SUGGEST:
722 return SearchManager.SUGGEST_MIME_TYPE;
723
724 default:
725 throw new IllegalArgumentException("Unknown URL");
726 }
727 }
728
729 @Override
730 public Uri insert(Uri url, ContentValues initialValues) {
731 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
732
733 int match = URI_MATCHER.match(url);
734 Uri uri = null;
735 switch (match) {
736 case URI_MATCH_BOOKMARKS: {
737 // Insert into the bookmarks table
738 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
739 initialValues);
740 if (rowID > 0) {
741 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
742 rowID);
743 }
744 break;
745 }
746
747 case URI_MATCH_SEARCHES: {
748 // Insert into the searches table
749 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
750 initialValues);
751 if (rowID > 0) {
752 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
753 rowID);
754 }
755 break;
756 }
757
758 default:
759 throw new IllegalArgumentException("Unknown URL");
760 }
761
762 if (uri == null) {
763 throw new IllegalArgumentException("Unknown URL");
764 }
765 getContext().getContentResolver().notifyChange(uri, null);
766 return uri;
767 }
768
769 @Override
770 public int delete(Uri url, String where, String[] whereArgs) {
771 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
772
773 int match = URI_MATCHER.match(url);
774 if (match == -1 || match == URI_MATCH_SUGGEST) {
775 throw new IllegalArgumentException("Unknown URL");
776 }
777
778 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
779 StringBuilder sb = new StringBuilder();
780 if (where != null && where.length() > 0) {
781 sb.append("( ");
782 sb.append(where);
783 sb.append(" ) AND ");
784 }
785 sb.append("_id = ");
786 sb.append(url.getPathSegments().get(1));
787 where = sb.toString();
788 }
789
790 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
791 getContext().getContentResolver().notifyChange(url, null);
792 return count;
793 }
794
795 @Override
796 public int update(Uri url, ContentValues values, String where,
797 String[] whereArgs) {
798 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
799
800 int match = URI_MATCHER.match(url);
801 if (match == -1 || match == URI_MATCH_SUGGEST) {
802 throw new IllegalArgumentException("Unknown URL");
803 }
804
805 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
806 StringBuilder sb = new StringBuilder();
807 if (where != null && where.length() > 0) {
808 sb.append("( ");
809 sb.append(where);
810 sb.append(" ) AND ");
811 }
812 sb.append("_id = ");
813 sb.append(url.getPathSegments().get(1));
814 where = sb.toString();
815 }
816
817 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
818 getContext().getContentResolver().notifyChange(url, null);
819 return ret;
820 }
Satish Sampath565505b2009-05-29 15:37:27 +0100821
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700822 /**
823 * Strips the provided url of preceding "http://" and any trailing "/". Does not
824 * strip "https://". If the provided string cannot be stripped, the original string
825 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +0100826 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700827 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +0100828 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700829 * @param url a url to strip, like "http://www.google.com/"
830 * @return a stripped url like "www.google.com", or the original string if it could
831 * not be stripped
832 */
833 private static String stripUrl(String url) {
834 if (url == null) return null;
835 Matcher m = STRIP_URL_PATTERN.matcher(url);
836 if (m.matches() && m.groupCount() == 3) {
837 return m.group(2);
838 } else {
839 return url;
840 }
841 }
842
The Android Open Source Project0c908882009-03-03 19:32:16 -0800843}