blob: a3ccf04986207e46320c0273f62565997579a62e [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
Satish Sampath60d24e22009-07-09 16:46:08 +0100154 // The ID of the ColorStateList to be applied to urls of website suggestions, as derived from
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700155 // the current theme. This is not set until/unless beautifyUrl is called, at which point
156 // this variable caches the color value.
Satish Sampath60d24e22009-07-09 16:46:08 +0100157 private static String mSearchUrlColorId;
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 Rajeswaran378e5722009-07-01 08:46:58 -0700167 final String client_id = Partner.getString(context.getContentResolver(),
168 Partner.CLIENT_ID, "android-google");
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700169
The Android Open Source Project0c908882009-03-03 19:32:16 -0800170 for (int i = 0; i < srcString.length(); ++i) {
171 char c = srcString.charAt(i);
172 if (c == '{') {
173 sb.append(srcString.subSequence(lastCharLoc, i));
174 lastCharLoc = i;
175 inner:
176 for (int j = i; j < srcString.length(); ++j) {
177 char k = srcString.charAt(j);
178 if (k == '}') {
179 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700180 if (propertyKeyValue.equals("CLIENT_ID")) {
181 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800182 } else {
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700183 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800184 }
185 lastCharLoc = j + 1;
186 i = j;
187 break inner;
188 }
189 }
190 }
191 }
192 if (srcString.length() - lastCharLoc > 0) {
193 // Put on the tail, if there is one
194 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
195 }
196 return sb;
197 }
198
199 private static class DatabaseHelper extends SQLiteOpenHelper {
200 private Context mContext;
201
202 public DatabaseHelper(Context context) {
203 super(context, sDatabaseName, null, DATABASE_VERSION);
204 mContext = context;
205 }
206
207 @Override
208 public void onCreate(SQLiteDatabase db) {
209 db.execSQL("CREATE TABLE bookmarks (" +
210 "_id INTEGER PRIMARY KEY," +
211 "title TEXT," +
212 "url TEXT," +
213 "visits INTEGER," +
214 "date LONG," +
215 "created LONG," +
216 "description TEXT," +
217 "bookmark INTEGER," +
218 "favicon BLOB DEFAULT NULL" +
219 ");");
220
221 final CharSequence[] bookmarks = mContext.getResources()
222 .getTextArray(R.array.bookmarks);
223 int size = bookmarks.length;
224 try {
225 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700226 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800227 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
228 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100229 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230 "', 0, 0, 0, 1);");
231 }
232 } catch (ArrayIndexOutOfBoundsException e) {
233 }
234
235 db.execSQL("CREATE TABLE searches (" +
236 "_id INTEGER PRIMARY KEY," +
237 "search TEXT," +
238 "date LONG" +
239 ");");
240 }
241
242 @Override
243 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
244 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
245 + newVersion + ", which will destroy all old data");
246 if (oldVersion == 18) {
247 db.execSQL("DROP TABLE IF EXISTS labels");
248 } else {
249 db.execSQL("DROP TABLE IF EXISTS bookmarks");
250 db.execSQL("DROP TABLE IF EXISTS searches");
251 onCreate(db);
252 }
253 }
254 }
255
256 @Override
257 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700258 final Context context = getContext();
259 mOpenHelper = new DatabaseHelper(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100260 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700261 // To avoid erasing the bookmark table, we added it explicitly for
262 // version 18 and 19 as in the other cases, we will erase the table.
263 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
264 SharedPreferences p = PreferenceManager
265 .getDefaultSharedPreferences(context);
266 boolean fix = p.getBoolean("fix_picasa", true);
267 if (fix) {
268 fixPicasaBookmark();
269 Editor ed = p.edit();
270 ed.putBoolean("fix_picasa", false);
271 ed.commit();
272 }
273 }
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100274 mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400275 mShowWebSuggestionsSettingChangeObserver
276 = new ShowWebSuggestionsSettingChangeObserver();
277 context.getContentResolver().registerContentObserver(
278 Settings.System.getUriFor(
279 Settings.System.SHOW_WEB_SUGGESTIONS),
280 true, mShowWebSuggestionsSettingChangeObserver);
281 updateShowWebSuggestions();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800282 return true;
283 }
284
Leon Scroggins62b71f72009-06-12 17:51:22 -0400285 /**
286 * This Observer will ensure that if the user changes the system
287 * setting of whether to display web suggestions, we will
288 * change accordingly.
289 */
290 /* package */ class ShowWebSuggestionsSettingChangeObserver
291 extends ContentObserver {
292 public ShowWebSuggestionsSettingChangeObserver() {
293 super(new Handler());
294 }
295
296 @Override
297 public void onChange(boolean selfChange) {
298 updateShowWebSuggestions();
299 }
300 }
301
302 private ShowWebSuggestionsSettingChangeObserver
303 mShowWebSuggestionsSettingChangeObserver;
304
305 // If non-null, then the system is set to show web suggestions,
306 // and this is the SearchableInfo to use to get them.
307 private SearchableInfo mSearchableInfo;
308
309 /**
310 * Check the system settings to see whether web suggestions are
311 * allowed. If so, store the SearchableInfo to grab suggestions
312 * while the user is typing.
313 */
314 private void updateShowWebSuggestions() {
315 mSearchableInfo = null;
316 Context context = getContext();
317 if (Settings.System.getInt(context.getContentResolver(),
318 Settings.System.SHOW_WEB_SUGGESTIONS,
319 1 /* default on */) == 1) {
320 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
321 intent.addCategory(Intent.CATEGORY_DEFAULT);
322 ResolveInfo info = context.getPackageManager().resolveActivity(
323 intent, PackageManager.MATCH_DEFAULT_ONLY);
324 if (info != null) {
325 ComponentName googleSearchComponent =
326 new ComponentName(info.activityInfo.packageName,
327 info.activityInfo.name);
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100328 mSearchableInfo = mSearchManager.getSearchableInfo(
Leon Scroggins62b71f72009-06-12 17:51:22 -0400329 googleSearchComponent, false);
330 }
331 }
332 }
333
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700334 private void fixPicasaBookmark() {
335 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
336 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
337 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
338 try {
339 if (!cursor.moveToFirst()) {
340 // set "created" so that it will be on the top of the list
341 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
342 "date, created, bookmark)" + " VALUES('" +
343 getContext().getString(R.string.picasa) + "', '"
344 + PICASA_URL + "', 0, 0, " + new Date().getTime()
345 + ", 1);");
346 }
347 } finally {
348 if (cursor != null) {
349 cursor.close();
350 }
351 }
352 }
353
The Android Open Source Project0c908882009-03-03 19:32:16 -0800354 /*
355 * Subclass AbstractCursor so we can combine multiple Cursors and add
356 * "Google Search".
357 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100358 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
The Android Open Source Project0c908882009-03-03 19:32:16 -0800359 * "Google Search";
Satish Sampath565505b2009-05-29 15:37:27 +0100360 * 2. If bookmark/history entries are less than
The Android Open Source Project0c908882009-03-03 19:32:16 -0800361 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
362 */
363 private class MySuggestionCursor extends AbstractCursor {
364 private Cursor mHistoryCursor;
365 private Cursor mSuggestCursor;
366 private int mHistoryCount;
367 private int mSuggestionCount;
368 private boolean mBeyondCursor;
369 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100370 private int mSuggestText1Id;
371 private int mSuggestText2Id;
372 private int mSuggestQueryId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800373
374 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
375 mHistoryCursor = hc;
376 mSuggestCursor = sc;
377 mHistoryCount = hc.getCount();
378 mSuggestionCount = sc != null ? sc.getCount() : 0;
379 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
380 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
381 }
382 mString = string;
383 mBeyondCursor = false;
Satish Sampath565505b2009-05-29 15:37:27 +0100384
385 // Some web suggest providers only give suggestions and have no description string for
386 // items. The order of the result columns may be different as well. So retrieve the
387 // column indices for the fields we need now and check before using below.
388 if (mSuggestCursor == null) {
389 mSuggestText1Id = -1;
390 mSuggestText2Id = -1;
391 mSuggestQueryId = -1;
392 } else {
393 mSuggestText1Id = mSuggestCursor.getColumnIndex(
394 SearchManager.SUGGEST_COLUMN_TEXT_1);
395 mSuggestText2Id = mSuggestCursor.getColumnIndex(
396 SearchManager.SUGGEST_COLUMN_TEXT_2);
397 mSuggestQueryId = mSuggestCursor.getColumnIndex(
398 SearchManager.SUGGEST_COLUMN_QUERY);
399 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800400 }
401
402 @Override
403 public boolean onMove(int oldPosition, int newPosition) {
404 if (mHistoryCursor == null) {
405 return false;
406 }
407 if (mHistoryCount > newPosition) {
408 mHistoryCursor.moveToPosition(newPosition);
409 mBeyondCursor = false;
410 } else if (mHistoryCount + mSuggestionCount > newPosition) {
411 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
412 mBeyondCursor = false;
413 } else {
414 mBeyondCursor = true;
415 }
416 return true;
417 }
418
419 @Override
420 public int getCount() {
421 if (mString.length() > 0) {
422 return mHistoryCount + mSuggestionCount + 1;
423 } else {
424 return mHistoryCount + mSuggestionCount;
425 }
426 }
427
428 @Override
429 public String[] getColumnNames() {
430 return COLUMNS;
431 }
Satish Sampath565505b2009-05-29 15:37:27 +0100432
The Android Open Source Project0c908882009-03-03 19:32:16 -0800433 @Override
434 public String getString(int columnIndex) {
435 if ((mPos != -1 && mHistoryCursor != null)) {
436 switch(columnIndex) {
437 case SUGGEST_COLUMN_INTENT_ACTION_ID:
438 if (mHistoryCount > mPos) {
439 return Intent.ACTION_VIEW;
440 } else {
441 return Intent.ACTION_SEARCH;
442 }
443
444 case SUGGEST_COLUMN_INTENT_DATA_ID:
445 if (mHistoryCount > mPos) {
446 return mHistoryCursor.getString(1);
447 } else {
448 return null;
449 }
450
451 case SUGGEST_COLUMN_TEXT_1_ID:
452 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700453 return getHistoryTitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800454 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100455 if (mSuggestText1Id == -1) return null;
456 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800457 } else {
458 return mString;
459 }
460
461 case SUGGEST_COLUMN_TEXT_2_ID:
462 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700463 return getHistorySubtitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800464 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100465 if (mSuggestText2Id == -1) return null;
466 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800467 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700468 return getContext().getString(R.string.search_the_web);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800469 }
470
471 case SUGGEST_COLUMN_ICON_1_ID:
472 if (mHistoryCount > mPos) {
473 if (mHistoryCursor.getInt(3) == 1) {
474 return new Integer(
475 R.drawable.ic_search_category_bookmark)
476 .toString();
477 } else {
478 return new Integer(
479 R.drawable.ic_search_category_history)
480 .toString();
481 }
482 } else {
483 return new Integer(
484 R.drawable.ic_search_category_suggest)
485 .toString();
486 }
487
488 case SUGGEST_COLUMN_ICON_2_ID:
489 return new String("0");
490
491 case SUGGEST_COLUMN_QUERY_ID:
492 if (mHistoryCount > mPos) {
Mike LeBeau2af73052009-06-23 17:36:59 -0700493 // Return the url in the intent query column. This is ignored
494 // within the browser because our searchable is set to
495 // android:searchMode="queryRewriteFromData", but it is used by
496 // global search for query rewriting.
497 return mHistoryCursor.getString(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800498 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100499 if (mSuggestQueryId == -1) return null;
500 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800501 } else {
502 return mString;
503 }
Satish Sampath565505b2009-05-29 15:37:27 +0100504
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700505 case SUGGEST_COLUMN_FORMAT:
506 return "html";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800507 }
508 }
509 return null;
510 }
511
512 @Override
513 public double getDouble(int column) {
514 throw new UnsupportedOperationException();
515 }
516
517 @Override
518 public float getFloat(int column) {
519 throw new UnsupportedOperationException();
520 }
521
522 @Override
523 public int getInt(int column) {
524 throw new UnsupportedOperationException();
525 }
526
527 @Override
528 public long getLong(int column) {
529 if ((mPos != -1) && column == 0) {
530 return mPos; // use row# as the _Id
531 }
532 throw new UnsupportedOperationException();
533 }
534
535 @Override
536 public short getShort(int column) {
537 throw new UnsupportedOperationException();
538 }
539
540 @Override
541 public boolean isNull(int column) {
542 throw new UnsupportedOperationException();
543 }
544
545 // TODO Temporary change, finalize after jq's changes go in
546 public void deactivate() {
547 if (mHistoryCursor != null) {
548 mHistoryCursor.deactivate();
549 }
550 if (mSuggestCursor != null) {
551 mSuggestCursor.deactivate();
552 }
553 super.deactivate();
554 }
555
556 public boolean requery() {
557 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
558 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
559 }
560
561 // TODO Temporary change, finalize after jq's changes go in
562 public void close() {
563 super.close();
564 if (mHistoryCursor != null) {
565 mHistoryCursor.close();
566 mHistoryCursor = null;
567 }
568 if (mSuggestCursor != null) {
569 mSuggestCursor.close();
570 mSuggestCursor = null;
571 }
572 }
Satish Sampath565505b2009-05-29 15:37:27 +0100573
Mike LeBeau21beb132009-05-13 14:57:50 -0700574 /**
575 * Provides the title (text line 1) for a browser suggestion, which should be the
576 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100577 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700578 * @return the title string to use
579 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700580 private String getHistoryTitle() {
581 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700582 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700583 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700584 }
585 return title;
586 }
Satish Sampath565505b2009-05-29 15:37:27 +0100587
Mike LeBeau21beb132009-05-13 14:57:50 -0700588 /**
589 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
590 * webpage url. If the webpage title is empty, then the url should go in the title
591 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100592 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700593 * @return the subtitle string to use, or null if none
594 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700595 private String getHistorySubtitle() {
596 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700597 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
598 return null;
599 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700600 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700601 }
602 }
Satish Sampath565505b2009-05-29 15:37:27 +0100603
Mike LeBeau21beb132009-05-13 14:57:50 -0700604 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700605 * Strips "http://" from the beginning of a url and "/" from the end,
606 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700607 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700608 private String beautifyUrl(String url) {
Satish Sampath60d24e22009-07-09 16:46:08 +0100609 if (mSearchUrlColorId == null) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700610 // Get the color used for this purpose from the current theme.
611 TypedValue colorValue = new TypedValue();
612 getContext().getTheme().resolveAttribute(
613 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
Satish Sampath60d24e22009-07-09 16:46:08 +0100614 mSearchUrlColorId = Integer.toString(colorValue.resourceId);
Mike LeBeau21beb132009-05-13 14:57:50 -0700615 }
Satish Sampath565505b2009-05-29 15:37:27 +0100616
Satish Sampath60d24e22009-07-09 16:46:08 +0100617 return "<font color=\"@" + mSearchUrlColorId + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700618 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800619 }
620
621 @Override
622 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100623 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800624 throws IllegalStateException {
625 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
626
627 int match = URI_MATCHER.match(url);
628 if (match == -1) {
629 throw new IllegalArgumentException("Unknown URL");
630 }
631
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100632 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800633 String suggestSelection;
634 String [] myArgs;
635 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
636 suggestSelection = null;
637 myArgs = null;
638 } else {
639 String like = selectionArgs[0] + "%";
640 if (selectionArgs[0].startsWith("http")) {
641 myArgs = new String[1];
642 myArgs[0] = like;
643 suggestSelection = selection;
644 } else {
645 SUGGEST_ARGS[0] = "http://" + like;
646 SUGGEST_ARGS[1] = "http://www." + like;
647 SUGGEST_ARGS[2] = "https://" + like;
648 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scroggins740eadf2009-06-15 12:34:39 -0400649 // To match against titles.
650 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800651 myArgs = SUGGEST_ARGS;
652 suggestSelection = SUGGEST_SELECTION;
653 }
654 }
655
656 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
657 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
658 ORDER_BY,
659 (new Integer(MAX_SUGGESTION_LONG_ENTRIES)).toString());
660
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100661 if (match == URI_MATCH_BOOKMARKS_SUGGEST
662 || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800663 return new MySuggestionCursor(c, null, "");
664 } else {
665 // get Google suggest if there is still space in the list
666 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400667 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800668 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100669 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400670 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800671 }
672 return new MySuggestionCursor(c, null, selectionArgs[0]);
673 }
674 }
675
676 String[] projection = null;
677 if (projectionIn != null && projectionIn.length > 0) {
678 projection = new String[projectionIn.length + 1];
679 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
680 projection[projectionIn.length] = "_id AS _id";
681 }
682
683 StringBuilder whereClause = new StringBuilder(256);
684 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
685 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
686 .append(")");
687 }
688
689 // Tack on the user's selection, if present
690 if (selection != null && selection.length() > 0) {
691 if (whereClause.length() > 0) {
692 whereClause.append(" AND ");
693 }
694
695 whereClause.append('(');
696 whereClause.append(selection);
697 whereClause.append(')');
698 }
699 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
700 whereClause.toString(), selectionArgs, null, null, sortOrder,
701 null);
702 c.setNotificationUri(getContext().getContentResolver(), url);
703 return c;
704 }
705
706 @Override
707 public String getType(Uri url) {
708 int match = URI_MATCHER.match(url);
709 switch (match) {
710 case URI_MATCH_BOOKMARKS:
711 return "vnd.android.cursor.dir/bookmark";
712
713 case URI_MATCH_BOOKMARKS_ID:
714 return "vnd.android.cursor.item/bookmark";
715
716 case URI_MATCH_SEARCHES:
717 return "vnd.android.cursor.dir/searches";
718
719 case URI_MATCH_SEARCHES_ID:
720 return "vnd.android.cursor.item/searches";
721
722 case URI_MATCH_SUGGEST:
723 return SearchManager.SUGGEST_MIME_TYPE;
724
725 default:
726 throw new IllegalArgumentException("Unknown URL");
727 }
728 }
729
730 @Override
731 public Uri insert(Uri url, ContentValues initialValues) {
732 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
733
734 int match = URI_MATCHER.match(url);
735 Uri uri = null;
736 switch (match) {
737 case URI_MATCH_BOOKMARKS: {
738 // Insert into the bookmarks table
739 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
740 initialValues);
741 if (rowID > 0) {
742 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
743 rowID);
744 }
745 break;
746 }
747
748 case URI_MATCH_SEARCHES: {
749 // Insert into the searches table
750 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
751 initialValues);
752 if (rowID > 0) {
753 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
754 rowID);
755 }
756 break;
757 }
758
759 default:
760 throw new IllegalArgumentException("Unknown URL");
761 }
762
763 if (uri == null) {
764 throw new IllegalArgumentException("Unknown URL");
765 }
766 getContext().getContentResolver().notifyChange(uri, null);
767 return uri;
768 }
769
770 @Override
771 public int delete(Uri url, String where, String[] whereArgs) {
772 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
773
774 int match = URI_MATCHER.match(url);
775 if (match == -1 || match == URI_MATCH_SUGGEST) {
776 throw new IllegalArgumentException("Unknown URL");
777 }
778
779 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
780 StringBuilder sb = new StringBuilder();
781 if (where != null && where.length() > 0) {
782 sb.append("( ");
783 sb.append(where);
784 sb.append(" ) AND ");
785 }
786 sb.append("_id = ");
787 sb.append(url.getPathSegments().get(1));
788 where = sb.toString();
789 }
790
791 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
792 getContext().getContentResolver().notifyChange(url, null);
793 return count;
794 }
795
796 @Override
797 public int update(Uri url, ContentValues values, String where,
798 String[] whereArgs) {
799 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
800
801 int match = URI_MATCHER.match(url);
802 if (match == -1 || match == URI_MATCH_SUGGEST) {
803 throw new IllegalArgumentException("Unknown URL");
804 }
805
806 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
807 StringBuilder sb = new StringBuilder();
808 if (where != null && where.length() > 0) {
809 sb.append("( ");
810 sb.append(where);
811 sb.append(" ) AND ");
812 }
813 sb.append("_id = ");
814 sb.append(url.getPathSegments().get(1));
815 where = sb.toString();
816 }
817
818 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
819 getContext().getContentResolver().notifyChange(url, null);
820 return ret;
821 }
Satish Sampath565505b2009-05-29 15:37:27 +0100822
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700823 /**
824 * Strips the provided url of preceding "http://" and any trailing "/". Does not
825 * strip "https://". If the provided string cannot be stripped, the original string
826 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +0100827 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700828 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +0100829 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700830 * @param url a url to strip, like "http://www.google.com/"
831 * @return a stripped url like "www.google.com", or the original string if it could
832 * not be stripped
833 */
834 private static String stripUrl(String url) {
835 if (url == null) return null;
836 Matcher m = STRIP_URL_PATTERN.matcher(url);
837 if (m.matches() && m.groupCount() == 3) {
838 return m.group(2);
839 } else {
840 return url;
841 }
842 }
843
The Android Open Source Project0c908882009-03-03 19:32:16 -0800844}