blob: 7a1e5cba3469630d13491b5150f42a7b26874b2c [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070019import com.google.android.providers.GoogleSettings.Partner;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070020
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.app.SearchManager;
22import android.content.ComponentName;
23import android.content.ContentProvider;
24import android.content.ContentUris;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.Intent;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070028import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.content.UriMatcher;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070030import android.content.SharedPreferences.Editor;
Satish Sampath565505b2009-05-29 15:37:27 +010031import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.database.AbstractCursor;
Leon Scroggins62b71f72009-06-12 17:51:22 -040034import android.database.ContentObserver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.database.sqlite.SQLiteDatabase;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010037import android.database.sqlite.SQLiteOpenHelper;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.net.Uri;
Leon Scroggins62b71f72009-06-12 17:51:22 -040039import android.os.Handler;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070040import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041import android.provider.Browser;
Leon Scroggins62b71f72009-06-12 17:51:22 -040042import android.provider.Settings;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.server.search.SearchableInfo;
Mike LeBeau21beb132009-05-13 14:57:50 -070044import android.text.TextUtils;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.text.util.Regex;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010046import android.util.Log;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070047import android.util.TypedValue;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010048
49import java.util.Date;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070050import java.util.regex.Matcher;
51import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -080052
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070053
The Android Open Source Project0c908882009-03-03 19:32:16 -080054public class BrowserProvider extends ContentProvider {
55
56 private SQLiteOpenHelper mOpenHelper;
57 private static final String sDatabaseName = "browser.db";
58 private static final String TAG = "BrowserProvider";
59 private static final String ORDER_BY = "visits DESC, date DESC";
60
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070061 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
62 "viewer?source=androidclient";
63
The Android Open Source Project0c908882009-03-03 19:32:16 -080064 private static final String[] TABLE_NAMES = new String[] {
65 "bookmarks", "searches"
66 };
67 private static final String[] SUGGEST_PROJECTION = new String[] {
68 "_id", "url", "title", "bookmark"
69 };
Satish Sampath565505b2009-05-29 15:37:27 +010070 private static final String SUGGEST_SELECTION =
Leon Scrogginsbd359cc2009-05-26 15:57:35 -040071 "url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?"
72 + " OR title LIKE ?";
73 private String[] SUGGEST_ARGS = new String[5];
The Android Open Source Project0c908882009-03-03 19:32:16 -080074
75 // shared suggestion array index, make sure to match COLUMNS
76 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
77 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
78 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
79 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
80 private static final int SUGGEST_COLUMN_ICON_1_ID = 5;
81 private static final int SUGGEST_COLUMN_ICON_2_ID = 6;
82 private static final int SUGGEST_COLUMN_QUERY_ID = 7;
Mike LeBeau1ef26a32009-05-13 20:11:00 -070083 private static final int SUGGEST_COLUMN_FORMAT = 8;
The Android Open Source Project0c908882009-03-03 19:32:16 -080084
85 // shared suggestion columns
86 private static final String[] COLUMNS = new String[] {
87 "_id",
88 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
89 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
90 SearchManager.SUGGEST_COLUMN_TEXT_1,
91 SearchManager.SUGGEST_COLUMN_TEXT_2,
92 SearchManager.SUGGEST_COLUMN_ICON_1,
93 SearchManager.SUGGEST_COLUMN_ICON_2,
Mike LeBeau1ef26a32009-05-13 20:11:00 -070094 SearchManager.SUGGEST_COLUMN_QUERY,
95 SearchManager.SUGGEST_COLUMN_FORMAT};
The Android Open Source Project0c908882009-03-03 19:32:16 -080096
97 private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3;
98 private static final int MAX_SUGGESTION_LONG_ENTRIES = 6;
Leon Scroggins31887fd2009-05-18 16:58:08 -040099 private static final String MAX_SUGGESTION_LONG_ENTRIES_STRING =
100 Integer.valueOf(MAX_SUGGESTION_LONG_ENTRIES).toString();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800101
102 // make sure that these match the index of TABLE_NAMES
103 private static final int URI_MATCH_BOOKMARKS = 0;
104 private static final int URI_MATCH_SEARCHES = 1;
105 // (id % 10) should match the table name index
106 private static final int URI_MATCH_BOOKMARKS_ID = 10;
107 private static final int URI_MATCH_SEARCHES_ID = 11;
108 //
109 private static final int URI_MATCH_SUGGEST = 20;
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100110 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800111
112 private static final UriMatcher URI_MATCHER;
113
114 static {
115 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
116 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
117 URI_MATCH_BOOKMARKS);
118 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
119 URI_MATCH_BOOKMARKS_ID);
120 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
121 URI_MATCH_SEARCHES);
122 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
123 URI_MATCH_SEARCHES_ID);
124 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
125 URI_MATCH_SUGGEST);
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100126 URI_MATCHER.addURI("browser",
127 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
128 URI_MATCH_BOOKMARKS_SUGGEST);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800129 }
130
131 // 1 -> 2 add cache table
132 // 2 -> 3 update history table
133 // 3 -> 4 add passwords table
134 // 4 -> 5 add settings table
135 // 5 -> 6 ?
136 // 6 -> 7 ?
137 // 7 -> 8 drop proxy table
138 // 8 -> 9 drop settings table
139 // 9 -> 10 add form_urls and form_data
140 // 10 -> 11 add searches table
141 // 11 -> 12 modify cache table
142 // 12 -> 13 modify cache table
143 // 13 -> 14 correspond with Google Bookmarks schema
144 // 14 -> 15 move couple of tables to either browser private database or webview database
145 // 15 -> 17 Set it up for the SearchManager
146 // 17 -> 18 Added favicon in bookmarks table for Home shortcuts
147 // 18 -> 19 Remove labels table
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400148 // 19 -> 20 Added thumbnail
149 private static final int DATABASE_VERSION = 20;
Satish Sampath565505b2009-05-29 15:37:27 +0100150
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700151 // Regular expression which matches http://, followed by some stuff, followed by
152 // optionally a trailing slash, all matched as separate groups.
153 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
Satish Sampath565505b2009-05-29 15:37:27 +0100154
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100155 private SearchManager mSearchManager;
156
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700157 // The hex color string to be applied to urls of website suggestions, as derived from
158 // the current theme. This is not set until/unless beautifyUrl is called, at which point
159 // this variable caches the color value.
160 private static String mSearchUrlColorHex;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800161
162 public BrowserProvider() {
163 }
Satish Sampath565505b2009-05-29 15:37:27 +0100164
The Android Open Source Project0c908882009-03-03 19:32:16 -0800165
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700166 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800167 StringBuffer sb = new StringBuffer();
168 int lastCharLoc = 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100169
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700170 final String client_id = Partner.getString(context.getContentResolver(), Partner.CLIENT_ID);
171
The Android Open Source Project0c908882009-03-03 19:32:16 -0800172 for (int i = 0; i < srcString.length(); ++i) {
173 char c = srcString.charAt(i);
174 if (c == '{') {
175 sb.append(srcString.subSequence(lastCharLoc, i));
176 lastCharLoc = i;
177 inner:
178 for (int j = i; j < srcString.length(); ++j) {
179 char k = srcString.charAt(j);
180 if (k == '}') {
181 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700182 if (propertyKeyValue.equals("CLIENT_ID")) {
183 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800184 } else {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700185 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 }
187 lastCharLoc = j + 1;
188 i = j;
189 break inner;
190 }
191 }
192 }
193 }
194 if (srcString.length() - lastCharLoc > 0) {
195 // Put on the tail, if there is one
196 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
197 }
198 return sb;
199 }
200
201 private static class DatabaseHelper extends SQLiteOpenHelper {
202 private Context mContext;
203
204 public DatabaseHelper(Context context) {
205 super(context, sDatabaseName, null, DATABASE_VERSION);
206 mContext = context;
207 }
208
209 @Override
210 public void onCreate(SQLiteDatabase db) {
211 db.execSQL("CREATE TABLE bookmarks (" +
212 "_id INTEGER PRIMARY KEY," +
213 "title TEXT," +
214 "url TEXT," +
215 "visits INTEGER," +
216 "date LONG," +
217 "created LONG," +
218 "description TEXT," +
219 "bookmark INTEGER," +
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400220 "favicon BLOB DEFAULT NULL," +
221 "thumbnail BLOB DEFAULT NULL" +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800222 ");");
223
224 final CharSequence[] bookmarks = mContext.getResources()
225 .getTextArray(R.array.bookmarks);
226 int size = bookmarks.length;
227 try {
228 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700229 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800230 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
231 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100232 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800233 "', 0, 0, 0, 1);");
234 }
235 } catch (ArrayIndexOutOfBoundsException e) {
236 }
237
238 db.execSQL("CREATE TABLE searches (" +
239 "_id INTEGER PRIMARY KEY," +
240 "search TEXT," +
241 "date LONG" +
242 ");");
243 }
244
245 @Override
246 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
247 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400248 + newVersion);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800249 if (oldVersion == 18) {
250 db.execSQL("DROP TABLE IF EXISTS labels");
Leon Scrogginsb6b7f9e2009-06-18 12:05:28 -0400251 }
252 if (oldVersion <= 19) {
253 db.execSQL("ALTER TABLE bookmarks ADD COLUMN thumbnail BLOB DEFAULT NULL;");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800254 } else {
255 db.execSQL("DROP TABLE IF EXISTS bookmarks");
256 db.execSQL("DROP TABLE IF EXISTS searches");
257 onCreate(db);
258 }
259 }
260 }
261
262 @Override
263 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700264 final Context context = getContext();
265 mOpenHelper = new DatabaseHelper(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100266 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700267 // To avoid erasing the bookmark table, we added it explicitly for
268 // version 18 and 19 as in the other cases, we will erase the table.
269 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
270 SharedPreferences p = PreferenceManager
271 .getDefaultSharedPreferences(context);
272 boolean fix = p.getBoolean("fix_picasa", true);
273 if (fix) {
274 fixPicasaBookmark();
275 Editor ed = p.edit();
276 ed.putBoolean("fix_picasa", false);
277 ed.commit();
278 }
279 }
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100280 mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400281 mShowWebSuggestionsSettingChangeObserver
282 = new ShowWebSuggestionsSettingChangeObserver();
283 context.getContentResolver().registerContentObserver(
284 Settings.System.getUriFor(
285 Settings.System.SHOW_WEB_SUGGESTIONS),
286 true, mShowWebSuggestionsSettingChangeObserver);
287 updateShowWebSuggestions();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800288 return true;
289 }
290
Leon Scroggins62b71f72009-06-12 17:51:22 -0400291 /**
292 * This Observer will ensure that if the user changes the system
293 * setting of whether to display web suggestions, we will
294 * change accordingly.
295 */
296 /* package */ class ShowWebSuggestionsSettingChangeObserver
297 extends ContentObserver {
298 public ShowWebSuggestionsSettingChangeObserver() {
299 super(new Handler());
300 }
301
302 @Override
303 public void onChange(boolean selfChange) {
304 updateShowWebSuggestions();
305 }
306 }
307
308 private ShowWebSuggestionsSettingChangeObserver
309 mShowWebSuggestionsSettingChangeObserver;
310
311 // If non-null, then the system is set to show web suggestions,
312 // and this is the SearchableInfo to use to get them.
313 private SearchableInfo mSearchableInfo;
314
315 /**
316 * Check the system settings to see whether web suggestions are
317 * allowed. If so, store the SearchableInfo to grab suggestions
318 * while the user is typing.
319 */
320 private void updateShowWebSuggestions() {
321 mSearchableInfo = null;
322 Context context = getContext();
323 if (Settings.System.getInt(context.getContentResolver(),
324 Settings.System.SHOW_WEB_SUGGESTIONS,
325 1 /* default on */) == 1) {
326 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
327 intent.addCategory(Intent.CATEGORY_DEFAULT);
328 ResolveInfo info = context.getPackageManager().resolveActivity(
329 intent, PackageManager.MATCH_DEFAULT_ONLY);
330 if (info != null) {
331 ComponentName googleSearchComponent =
332 new ComponentName(info.activityInfo.packageName,
333 info.activityInfo.name);
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100334 mSearchableInfo = mSearchManager.getSearchableInfo(
Leon Scroggins62b71f72009-06-12 17:51:22 -0400335 googleSearchComponent, false);
336 }
337 }
338 }
339
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700340 private void fixPicasaBookmark() {
341 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
342 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
343 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
344 try {
345 if (!cursor.moveToFirst()) {
346 // set "created" so that it will be on the top of the list
347 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
348 "date, created, bookmark)" + " VALUES('" +
349 getContext().getString(R.string.picasa) + "', '"
350 + PICASA_URL + "', 0, 0, " + new Date().getTime()
351 + ", 1);");
352 }
353 } finally {
354 if (cursor != null) {
355 cursor.close();
356 }
357 }
358 }
359
The Android Open Source Project0c908882009-03-03 19:32:16 -0800360 /*
361 * Subclass AbstractCursor so we can combine multiple Cursors and add
362 * "Google Search".
363 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100364 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
The Android Open Source Project0c908882009-03-03 19:32:16 -0800365 * "Google Search";
Satish Sampath565505b2009-05-29 15:37:27 +0100366 * 2. If bookmark/history entries are less than
The Android Open Source Project0c908882009-03-03 19:32:16 -0800367 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
368 */
369 private class MySuggestionCursor extends AbstractCursor {
370 private Cursor mHistoryCursor;
371 private Cursor mSuggestCursor;
372 private int mHistoryCount;
373 private int mSuggestionCount;
374 private boolean mBeyondCursor;
375 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100376 private int mSuggestText1Id;
377 private int mSuggestText2Id;
378 private int mSuggestQueryId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800379
380 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
381 mHistoryCursor = hc;
382 mSuggestCursor = sc;
383 mHistoryCount = hc.getCount();
384 mSuggestionCount = sc != null ? sc.getCount() : 0;
385 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
386 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
387 }
388 mString = string;
389 mBeyondCursor = false;
Satish Sampath565505b2009-05-29 15:37:27 +0100390
391 // Some web suggest providers only give suggestions and have no description string for
392 // items. The order of the result columns may be different as well. So retrieve the
393 // column indices for the fields we need now and check before using below.
394 if (mSuggestCursor == null) {
395 mSuggestText1Id = -1;
396 mSuggestText2Id = -1;
397 mSuggestQueryId = -1;
398 } else {
399 mSuggestText1Id = mSuggestCursor.getColumnIndex(
400 SearchManager.SUGGEST_COLUMN_TEXT_1);
401 mSuggestText2Id = mSuggestCursor.getColumnIndex(
402 SearchManager.SUGGEST_COLUMN_TEXT_2);
403 mSuggestQueryId = mSuggestCursor.getColumnIndex(
404 SearchManager.SUGGEST_COLUMN_QUERY);
405 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800406 }
407
408 @Override
409 public boolean onMove(int oldPosition, int newPosition) {
410 if (mHistoryCursor == null) {
411 return false;
412 }
413 if (mHistoryCount > newPosition) {
414 mHistoryCursor.moveToPosition(newPosition);
415 mBeyondCursor = false;
416 } else if (mHistoryCount + mSuggestionCount > newPosition) {
417 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
418 mBeyondCursor = false;
419 } else {
420 mBeyondCursor = true;
421 }
422 return true;
423 }
424
425 @Override
426 public int getCount() {
427 if (mString.length() > 0) {
428 return mHistoryCount + mSuggestionCount + 1;
429 } else {
430 return mHistoryCount + mSuggestionCount;
431 }
432 }
433
434 @Override
435 public String[] getColumnNames() {
436 return COLUMNS;
437 }
Satish Sampath565505b2009-05-29 15:37:27 +0100438
The Android Open Source Project0c908882009-03-03 19:32:16 -0800439 @Override
440 public String getString(int columnIndex) {
441 if ((mPos != -1 && mHistoryCursor != null)) {
442 switch(columnIndex) {
443 case SUGGEST_COLUMN_INTENT_ACTION_ID:
444 if (mHistoryCount > mPos) {
445 return Intent.ACTION_VIEW;
446 } else {
447 return Intent.ACTION_SEARCH;
448 }
449
450 case SUGGEST_COLUMN_INTENT_DATA_ID:
451 if (mHistoryCount > mPos) {
452 return mHistoryCursor.getString(1);
453 } else {
454 return null;
455 }
456
457 case SUGGEST_COLUMN_TEXT_1_ID:
458 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700459 return getHistoryTitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800460 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100461 if (mSuggestText1Id == -1) return null;
462 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800463 } else {
464 return mString;
465 }
466
467 case SUGGEST_COLUMN_TEXT_2_ID:
468 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700469 return getHistorySubtitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800470 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100471 if (mSuggestText2Id == -1) return null;
472 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800473 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700474 return getContext().getString(R.string.search_the_web);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800475 }
476
477 case SUGGEST_COLUMN_ICON_1_ID:
478 if (mHistoryCount > mPos) {
479 if (mHistoryCursor.getInt(3) == 1) {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400480 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800481 R.drawable.ic_search_category_bookmark)
482 .toString();
483 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400484 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800485 R.drawable.ic_search_category_history)
486 .toString();
487 }
488 } else {
Leon Scroggins31887fd2009-05-18 16:58:08 -0400489 return Integer.valueOf(
The Android Open Source Project0c908882009-03-03 19:32:16 -0800490 R.drawable.ic_search_category_suggest)
491 .toString();
492 }
493
494 case SUGGEST_COLUMN_ICON_2_ID:
Leon Scroggins31887fd2009-05-18 16:58:08 -0400495 return "0";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800496
497 case SUGGEST_COLUMN_QUERY_ID:
498 if (mHistoryCount > mPos) {
Mike LeBeau2af73052009-06-23 17:36:59 -0700499 // Return the url in the intent query column. This is ignored
500 // within the browser because our searchable is set to
501 // android:searchMode="queryRewriteFromData", but it is used by
502 // global search for query rewriting.
503 return mHistoryCursor.getString(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800504 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100505 if (mSuggestQueryId == -1) return null;
506 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800507 } else {
508 return mString;
509 }
Satish Sampath565505b2009-05-29 15:37:27 +0100510
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700511 case SUGGEST_COLUMN_FORMAT:
512 return "html";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800513 }
514 }
515 return null;
516 }
517
518 @Override
519 public double getDouble(int column) {
520 throw new UnsupportedOperationException();
521 }
522
523 @Override
524 public float getFloat(int column) {
525 throw new UnsupportedOperationException();
526 }
527
528 @Override
529 public int getInt(int column) {
530 throw new UnsupportedOperationException();
531 }
532
533 @Override
534 public long getLong(int column) {
535 if ((mPos != -1) && column == 0) {
536 return mPos; // use row# as the _Id
537 }
538 throw new UnsupportedOperationException();
539 }
540
541 @Override
542 public short getShort(int column) {
543 throw new UnsupportedOperationException();
544 }
545
546 @Override
547 public boolean isNull(int column) {
548 throw new UnsupportedOperationException();
549 }
550
551 // TODO Temporary change, finalize after jq's changes go in
552 public void deactivate() {
553 if (mHistoryCursor != null) {
554 mHistoryCursor.deactivate();
555 }
556 if (mSuggestCursor != null) {
557 mSuggestCursor.deactivate();
558 }
559 super.deactivate();
560 }
561
562 public boolean requery() {
563 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
564 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
565 }
566
567 // TODO Temporary change, finalize after jq's changes go in
568 public void close() {
569 super.close();
570 if (mHistoryCursor != null) {
571 mHistoryCursor.close();
572 mHistoryCursor = null;
573 }
574 if (mSuggestCursor != null) {
575 mSuggestCursor.close();
576 mSuggestCursor = null;
577 }
578 }
Satish Sampath565505b2009-05-29 15:37:27 +0100579
Mike LeBeau21beb132009-05-13 14:57:50 -0700580 /**
581 * Provides the title (text line 1) for a browser suggestion, which should be the
582 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100583 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700584 * @return the title string to use
585 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700586 private String getHistoryTitle() {
587 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700588 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700589 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700590 }
591 return title;
592 }
Satish Sampath565505b2009-05-29 15:37:27 +0100593
Mike LeBeau21beb132009-05-13 14:57:50 -0700594 /**
595 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
596 * webpage url. If the webpage title is empty, then the url should go in the title
597 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100598 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700599 * @return the subtitle string to use, or null if none
600 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700601 private String getHistorySubtitle() {
602 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700603 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
604 return null;
605 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700606 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700607 }
608 }
Satish Sampath565505b2009-05-29 15:37:27 +0100609
Mike LeBeau21beb132009-05-13 14:57:50 -0700610 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700611 * Strips "http://" from the beginning of a url and "/" from the end,
612 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700613 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700614 private String beautifyUrl(String url) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700615 if (mSearchUrlColorHex == null) {
616 // Get the color used for this purpose from the current theme.
617 TypedValue colorValue = new TypedValue();
618 getContext().getTheme().resolveAttribute(
619 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
620 int color = getContext().getResources().getColor(colorValue.resourceId);
Satish Sampath565505b2009-05-29 15:37:27 +0100621
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700622 // Convert the int color value into a hex string, and strip the first two
623 // characters which will be the alpha transparency (html doesn't want this).
624 mSearchUrlColorHex = Integer.toHexString(color).substring(2);
Mike LeBeau21beb132009-05-13 14:57:50 -0700625 }
Satish Sampath565505b2009-05-29 15:37:27 +0100626
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700627 return "<font color=\"#" + mSearchUrlColorHex + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700628 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800629 }
630
631 @Override
632 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100633 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800634 throws IllegalStateException {
635 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
636
637 int match = URI_MATCHER.match(url);
638 if (match == -1) {
639 throw new IllegalArgumentException("Unknown URL");
640 }
641
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100642 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800643 String suggestSelection;
644 String [] myArgs;
645 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
646 suggestSelection = null;
647 myArgs = null;
648 } else {
649 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700650 if (selectionArgs[0].startsWith("http")
651 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800652 myArgs = new String[1];
653 myArgs[0] = like;
654 suggestSelection = selection;
655 } else {
656 SUGGEST_ARGS[0] = "http://" + like;
657 SUGGEST_ARGS[1] = "http://www." + like;
658 SUGGEST_ARGS[2] = "https://" + like;
659 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400660 // To match against titles.
661 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800662 myArgs = SUGGEST_ARGS;
663 suggestSelection = SUGGEST_SELECTION;
664 }
665 }
666
667 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
668 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Leon Scroggins31887fd2009-05-18 16:58:08 -0400669 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800670
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100671 if (match == URI_MATCH_BOOKMARKS_SUGGEST
672 || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800673 return new MySuggestionCursor(c, null, "");
674 } else {
675 // get Google suggest if there is still space in the list
676 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400677 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800678 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100679 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400680 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800681 }
682 return new MySuggestionCursor(c, null, selectionArgs[0]);
683 }
684 }
685
686 String[] projection = null;
687 if (projectionIn != null && projectionIn.length > 0) {
688 projection = new String[projectionIn.length + 1];
689 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
690 projection[projectionIn.length] = "_id AS _id";
691 }
692
693 StringBuilder whereClause = new StringBuilder(256);
694 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
695 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
696 .append(")");
697 }
698
699 // Tack on the user's selection, if present
700 if (selection != null && selection.length() > 0) {
701 if (whereClause.length() > 0) {
702 whereClause.append(" AND ");
703 }
704
705 whereClause.append('(');
706 whereClause.append(selection);
707 whereClause.append(')');
708 }
709 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
710 whereClause.toString(), selectionArgs, null, null, sortOrder,
711 null);
712 c.setNotificationUri(getContext().getContentResolver(), url);
713 return c;
714 }
715
716 @Override
717 public String getType(Uri url) {
718 int match = URI_MATCHER.match(url);
719 switch (match) {
720 case URI_MATCH_BOOKMARKS:
721 return "vnd.android.cursor.dir/bookmark";
722
723 case URI_MATCH_BOOKMARKS_ID:
724 return "vnd.android.cursor.item/bookmark";
725
726 case URI_MATCH_SEARCHES:
727 return "vnd.android.cursor.dir/searches";
728
729 case URI_MATCH_SEARCHES_ID:
730 return "vnd.android.cursor.item/searches";
731
732 case URI_MATCH_SUGGEST:
733 return SearchManager.SUGGEST_MIME_TYPE;
734
735 default:
736 throw new IllegalArgumentException("Unknown URL");
737 }
738 }
739
740 @Override
741 public Uri insert(Uri url, ContentValues initialValues) {
742 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
743
744 int match = URI_MATCHER.match(url);
745 Uri uri = null;
746 switch (match) {
747 case URI_MATCH_BOOKMARKS: {
748 // Insert into the bookmarks table
749 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
750 initialValues);
751 if (rowID > 0) {
752 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
753 rowID);
754 }
755 break;
756 }
757
758 case URI_MATCH_SEARCHES: {
759 // Insert into the searches table
760 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
761 initialValues);
762 if (rowID > 0) {
763 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
764 rowID);
765 }
766 break;
767 }
768
769 default:
770 throw new IllegalArgumentException("Unknown URL");
771 }
772
773 if (uri == null) {
774 throw new IllegalArgumentException("Unknown URL");
775 }
776 getContext().getContentResolver().notifyChange(uri, null);
777 return uri;
778 }
779
780 @Override
781 public int delete(Uri url, String where, String[] whereArgs) {
782 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
783
784 int match = URI_MATCHER.match(url);
785 if (match == -1 || match == URI_MATCH_SUGGEST) {
786 throw new IllegalArgumentException("Unknown URL");
787 }
788
789 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
790 StringBuilder sb = new StringBuilder();
791 if (where != null && where.length() > 0) {
792 sb.append("( ");
793 sb.append(where);
794 sb.append(" ) AND ");
795 }
796 sb.append("_id = ");
797 sb.append(url.getPathSegments().get(1));
798 where = sb.toString();
799 }
800
801 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
802 getContext().getContentResolver().notifyChange(url, null);
803 return count;
804 }
805
806 @Override
807 public int update(Uri url, ContentValues values, String where,
808 String[] whereArgs) {
809 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
810
811 int match = URI_MATCHER.match(url);
812 if (match == -1 || match == URI_MATCH_SUGGEST) {
813 throw new IllegalArgumentException("Unknown URL");
814 }
815
816 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
817 StringBuilder sb = new StringBuilder();
818 if (where != null && where.length() > 0) {
819 sb.append("( ");
820 sb.append(where);
821 sb.append(" ) AND ");
822 }
823 sb.append("_id = ");
824 sb.append(url.getPathSegments().get(1));
825 where = sb.toString();
826 }
827
828 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
829 getContext().getContentResolver().notifyChange(url, null);
830 return ret;
831 }
Satish Sampath565505b2009-05-29 15:37:27 +0100832
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700833 /**
834 * Strips the provided url of preceding "http://" and any trailing "/". Does not
835 * strip "https://". If the provided string cannot be stripped, the original string
836 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +0100837 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700838 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +0100839 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700840 * @param url a url to strip, like "http://www.google.com/"
841 * @return a stripped url like "www.google.com", or the original string if it could
842 * not be stripped
843 */
844 private static String stripUrl(String url) {
845 if (url == null) return null;
846 Matcher m = STRIP_URL_PATTERN.matcher(url);
847 if (m.matches() && m.groupCount() == 3) {
848 return m.group(2);
849 } else {
850 return url;
851 }
852 }
853
The Android Open Source Project0c908882009-03-03 19:32:16 -0800854}