blob: 474719001dfe338e49211323b8f5247f1dd2fcaa [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
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100152 private SearchManager mSearchManager;
153
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 Rajeswaranf447f262009-03-24 20:40:12 -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 Rajeswaranf447f262009-03-24 20:40:12 -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 Rajeswaranf447f262009-03-24 20:40:12 -0700179 if (propertyKeyValue.equals("CLIENT_ID")) {
180 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800181 } else {
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -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 Rajeswaranf447f262009-03-24 20:40:12 -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 }
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100273 mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400274 mShowWebSuggestionsSettingChangeObserver
275 = new ShowWebSuggestionsSettingChangeObserver();
276 context.getContentResolver().registerContentObserver(
277 Settings.System.getUriFor(
278 Settings.System.SHOW_WEB_SUGGESTIONS),
279 true, mShowWebSuggestionsSettingChangeObserver);
280 updateShowWebSuggestions();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800281 return true;
282 }
283
Leon Scroggins62b71f72009-06-12 17:51:22 -0400284 /**
285 * This Observer will ensure that if the user changes the system
286 * setting of whether to display web suggestions, we will
287 * change accordingly.
288 */
289 /* package */ class ShowWebSuggestionsSettingChangeObserver
290 extends ContentObserver {
291 public ShowWebSuggestionsSettingChangeObserver() {
292 super(new Handler());
293 }
294
295 @Override
296 public void onChange(boolean selfChange) {
297 updateShowWebSuggestions();
298 }
299 }
300
301 private ShowWebSuggestionsSettingChangeObserver
302 mShowWebSuggestionsSettingChangeObserver;
303
304 // If non-null, then the system is set to show web suggestions,
305 // and this is the SearchableInfo to use to get them.
306 private SearchableInfo mSearchableInfo;
307
308 /**
309 * Check the system settings to see whether web suggestions are
310 * allowed. If so, store the SearchableInfo to grab suggestions
311 * while the user is typing.
312 */
313 private void updateShowWebSuggestions() {
314 mSearchableInfo = null;
315 Context context = getContext();
316 if (Settings.System.getInt(context.getContentResolver(),
317 Settings.System.SHOW_WEB_SUGGESTIONS,
318 1 /* default on */) == 1) {
319 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
320 intent.addCategory(Intent.CATEGORY_DEFAULT);
321 ResolveInfo info = context.getPackageManager().resolveActivity(
322 intent, PackageManager.MATCH_DEFAULT_ONLY);
323 if (info != null) {
324 ComponentName googleSearchComponent =
325 new ComponentName(info.activityInfo.packageName,
326 info.activityInfo.name);
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100327 mSearchableInfo = mSearchManager.getSearchableInfo(
Leon Scroggins62b71f72009-06-12 17:51:22 -0400328 googleSearchComponent, false);
329 }
330 }
331 }
332
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700333 private void fixPicasaBookmark() {
334 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
335 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
336 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
337 try {
338 if (!cursor.moveToFirst()) {
339 // set "created" so that it will be on the top of the list
340 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
341 "date, created, bookmark)" + " VALUES('" +
342 getContext().getString(R.string.picasa) + "', '"
343 + PICASA_URL + "', 0, 0, " + new Date().getTime()
344 + ", 1);");
345 }
346 } finally {
347 if (cursor != null) {
348 cursor.close();
349 }
350 }
351 }
352
The Android Open Source Project0c908882009-03-03 19:32:16 -0800353 /*
354 * Subclass AbstractCursor so we can combine multiple Cursors and add
355 * "Google Search".
356 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100357 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
The Android Open Source Project0c908882009-03-03 19:32:16 -0800358 * "Google Search";
Satish Sampath565505b2009-05-29 15:37:27 +0100359 * 2. If bookmark/history entries are less than
The Android Open Source Project0c908882009-03-03 19:32:16 -0800360 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
361 */
362 private class MySuggestionCursor extends AbstractCursor {
363 private Cursor mHistoryCursor;
364 private Cursor mSuggestCursor;
365 private int mHistoryCount;
366 private int mSuggestionCount;
367 private boolean mBeyondCursor;
368 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100369 private int mSuggestText1Id;
370 private int mSuggestText2Id;
371 private int mSuggestQueryId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800372
373 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
374 mHistoryCursor = hc;
375 mSuggestCursor = sc;
376 mHistoryCount = hc.getCount();
377 mSuggestionCount = sc != null ? sc.getCount() : 0;
378 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
379 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
380 }
381 mString = string;
382 mBeyondCursor = false;
Satish Sampath565505b2009-05-29 15:37:27 +0100383
384 // Some web suggest providers only give suggestions and have no description string for
385 // items. The order of the result columns may be different as well. So retrieve the
386 // column indices for the fields we need now and check before using below.
387 if (mSuggestCursor == null) {
388 mSuggestText1Id = -1;
389 mSuggestText2Id = -1;
390 mSuggestQueryId = -1;
391 } else {
392 mSuggestText1Id = mSuggestCursor.getColumnIndex(
393 SearchManager.SUGGEST_COLUMN_TEXT_1);
394 mSuggestText2Id = mSuggestCursor.getColumnIndex(
395 SearchManager.SUGGEST_COLUMN_TEXT_2);
396 mSuggestQueryId = mSuggestCursor.getColumnIndex(
397 SearchManager.SUGGEST_COLUMN_QUERY);
398 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800399 }
400
401 @Override
402 public boolean onMove(int oldPosition, int newPosition) {
403 if (mHistoryCursor == null) {
404 return false;
405 }
406 if (mHistoryCount > newPosition) {
407 mHistoryCursor.moveToPosition(newPosition);
408 mBeyondCursor = false;
409 } else if (mHistoryCount + mSuggestionCount > newPosition) {
410 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
411 mBeyondCursor = false;
412 } else {
413 mBeyondCursor = true;
414 }
415 return true;
416 }
417
418 @Override
419 public int getCount() {
420 if (mString.length() > 0) {
421 return mHistoryCount + mSuggestionCount + 1;
422 } else {
423 return mHistoryCount + mSuggestionCount;
424 }
425 }
426
427 @Override
428 public String[] getColumnNames() {
429 return COLUMNS;
430 }
Satish Sampath565505b2009-05-29 15:37:27 +0100431
The Android Open Source Project0c908882009-03-03 19:32:16 -0800432 @Override
433 public String getString(int columnIndex) {
434 if ((mPos != -1 && mHistoryCursor != null)) {
435 switch(columnIndex) {
436 case SUGGEST_COLUMN_INTENT_ACTION_ID:
437 if (mHistoryCount > mPos) {
438 return Intent.ACTION_VIEW;
439 } else {
440 return Intent.ACTION_SEARCH;
441 }
442
443 case SUGGEST_COLUMN_INTENT_DATA_ID:
444 if (mHistoryCount > mPos) {
445 return mHistoryCursor.getString(1);
446 } else {
447 return null;
448 }
449
450 case SUGGEST_COLUMN_TEXT_1_ID:
451 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700452 return getHistoryTitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800453 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100454 if (mSuggestText1Id == -1) return null;
455 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800456 } else {
457 return mString;
458 }
459
460 case SUGGEST_COLUMN_TEXT_2_ID:
461 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700462 return getHistorySubtitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800463 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100464 if (mSuggestText2Id == -1) return null;
465 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800466 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700467 return getContext().getString(R.string.search_the_web);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800468 }
469
470 case SUGGEST_COLUMN_ICON_1_ID:
471 if (mHistoryCount > mPos) {
472 if (mHistoryCursor.getInt(3) == 1) {
473 return new Integer(
474 R.drawable.ic_search_category_bookmark)
475 .toString();
476 } else {
477 return new Integer(
478 R.drawable.ic_search_category_history)
479 .toString();
480 }
481 } else {
482 return new Integer(
483 R.drawable.ic_search_category_suggest)
484 .toString();
485 }
486
487 case SUGGEST_COLUMN_ICON_2_ID:
488 return new String("0");
489
490 case SUGGEST_COLUMN_QUERY_ID:
491 if (mHistoryCount > mPos) {
Mike LeBeau2af73052009-06-23 17:36:59 -0700492 // Return the url in the intent query column. This is ignored
493 // within the browser because our searchable is set to
494 // android:searchMode="queryRewriteFromData", but it is used by
495 // global search for query rewriting.
496 return mHistoryCursor.getString(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800497 } 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] + "%";
643 if (selectionArgs[0].startsWith("http")) {
644 myArgs = new String[1];
645 myArgs[0] = like;
646 suggestSelection = selection;
647 } else {
648 SUGGEST_ARGS[0] = "http://" + like;
649 SUGGEST_ARGS[1] = "http://www." + like;
650 SUGGEST_ARGS[2] = "https://" + like;
651 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scroggins740eadf2009-06-15 12:34:39 -0400652 // To match against titles.
653 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800654 myArgs = SUGGEST_ARGS;
655 suggestSelection = SUGGEST_SELECTION;
656 }
657 }
658
659 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
660 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
661 ORDER_BY,
662 (new Integer(MAX_SUGGESTION_LONG_ENTRIES)).toString());
663
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)) {
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100672 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400673 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674 }
675 return new MySuggestionCursor(c, null, selectionArgs[0]);
676 }
677 }
678
679 String[] projection = null;
680 if (projectionIn != null && projectionIn.length > 0) {
681 projection = new String[projectionIn.length + 1];
682 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
683 projection[projectionIn.length] = "_id AS _id";
684 }
685
686 StringBuilder whereClause = new StringBuilder(256);
687 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
688 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
689 .append(")");
690 }
691
692 // Tack on the user's selection, if present
693 if (selection != null && selection.length() > 0) {
694 if (whereClause.length() > 0) {
695 whereClause.append(" AND ");
696 }
697
698 whereClause.append('(');
699 whereClause.append(selection);
700 whereClause.append(')');
701 }
702 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
703 whereClause.toString(), selectionArgs, null, null, sortOrder,
704 null);
705 c.setNotificationUri(getContext().getContentResolver(), url);
706 return c;
707 }
708
709 @Override
710 public String getType(Uri url) {
711 int match = URI_MATCHER.match(url);
712 switch (match) {
713 case URI_MATCH_BOOKMARKS:
714 return "vnd.android.cursor.dir/bookmark";
715
716 case URI_MATCH_BOOKMARKS_ID:
717 return "vnd.android.cursor.item/bookmark";
718
719 case URI_MATCH_SEARCHES:
720 return "vnd.android.cursor.dir/searches";
721
722 case URI_MATCH_SEARCHES_ID:
723 return "vnd.android.cursor.item/searches";
724
725 case URI_MATCH_SUGGEST:
726 return SearchManager.SUGGEST_MIME_TYPE;
727
728 default:
729 throw new IllegalArgumentException("Unknown URL");
730 }
731 }
732
733 @Override
734 public Uri insert(Uri url, ContentValues initialValues) {
735 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
736
737 int match = URI_MATCHER.match(url);
738 Uri uri = null;
739 switch (match) {
740 case URI_MATCH_BOOKMARKS: {
741 // Insert into the bookmarks table
742 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
743 initialValues);
744 if (rowID > 0) {
745 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
746 rowID);
747 }
748 break;
749 }
750
751 case URI_MATCH_SEARCHES: {
752 // Insert into the searches table
753 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
754 initialValues);
755 if (rowID > 0) {
756 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
757 rowID);
758 }
759 break;
760 }
761
762 default:
763 throw new IllegalArgumentException("Unknown URL");
764 }
765
766 if (uri == null) {
767 throw new IllegalArgumentException("Unknown URL");
768 }
769 getContext().getContentResolver().notifyChange(uri, null);
770 return uri;
771 }
772
773 @Override
774 public int delete(Uri url, String where, String[] whereArgs) {
775 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
776
777 int match = URI_MATCHER.match(url);
778 if (match == -1 || match == URI_MATCH_SUGGEST) {
779 throw new IllegalArgumentException("Unknown URL");
780 }
781
782 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
783 StringBuilder sb = new StringBuilder();
784 if (where != null && where.length() > 0) {
785 sb.append("( ");
786 sb.append(where);
787 sb.append(" ) AND ");
788 }
789 sb.append("_id = ");
790 sb.append(url.getPathSegments().get(1));
791 where = sb.toString();
792 }
793
794 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
795 getContext().getContentResolver().notifyChange(url, null);
796 return count;
797 }
798
799 @Override
800 public int update(Uri url, ContentValues values, String where,
801 String[] whereArgs) {
802 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
803
804 int match = URI_MATCHER.match(url);
805 if (match == -1 || match == URI_MATCH_SUGGEST) {
806 throw new IllegalArgumentException("Unknown URL");
807 }
808
809 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
810 StringBuilder sb = new StringBuilder();
811 if (where != null && where.length() > 0) {
812 sb.append("( ");
813 sb.append(where);
814 sb.append(" ) AND ");
815 }
816 sb.append("_id = ");
817 sb.append(url.getPathSegments().get(1));
818 where = sb.toString();
819 }
820
821 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
822 getContext().getContentResolver().notifyChange(url, null);
823 return ret;
824 }
Satish Sampath565505b2009-05-29 15:37:27 +0100825
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700826 /**
827 * Strips the provided url of preceding "http://" and any trailing "/". Does not
828 * strip "https://". If the provided string cannot be stripped, the original string
829 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +0100830 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700831 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +0100832 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700833 * @param url a url to strip, like "http://www.google.com/"
834 * @return a stripped url like "www.google.com", or the original string if it could
835 * not be stripped
836 */
837 private static String stripUrl(String url) {
838 if (url == null) return null;
839 Matcher m = STRIP_URL_PATTERN.matcher(url);
840 if (m.matches() && m.groupCount() == 3) {
841 return m.group(2);
842 } else {
843 return url;
844 }
845 }
846
The Android Open Source Project0c908882009-03-03 19:32:16 -0800847}