blob: cf3ed081e0405fd7950f76dba1c6176c9164ba3d [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) {
499 return null;
500 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100501 if (mSuggestQueryId == -1) return null;
502 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800503 } else {
504 return mString;
505 }
Satish Sampath565505b2009-05-29 15:37:27 +0100506
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700507 case SUGGEST_COLUMN_FORMAT:
508 return "html";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800509 }
510 }
511 return null;
512 }
513
514 @Override
515 public double getDouble(int column) {
516 throw new UnsupportedOperationException();
517 }
518
519 @Override
520 public float getFloat(int column) {
521 throw new UnsupportedOperationException();
522 }
523
524 @Override
525 public int getInt(int column) {
526 throw new UnsupportedOperationException();
527 }
528
529 @Override
530 public long getLong(int column) {
531 if ((mPos != -1) && column == 0) {
532 return mPos; // use row# as the _Id
533 }
534 throw new UnsupportedOperationException();
535 }
536
537 @Override
538 public short getShort(int column) {
539 throw new UnsupportedOperationException();
540 }
541
542 @Override
543 public boolean isNull(int column) {
544 throw new UnsupportedOperationException();
545 }
546
547 // TODO Temporary change, finalize after jq's changes go in
548 public void deactivate() {
549 if (mHistoryCursor != null) {
550 mHistoryCursor.deactivate();
551 }
552 if (mSuggestCursor != null) {
553 mSuggestCursor.deactivate();
554 }
555 super.deactivate();
556 }
557
558 public boolean requery() {
559 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
560 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
561 }
562
563 // TODO Temporary change, finalize after jq's changes go in
564 public void close() {
565 super.close();
566 if (mHistoryCursor != null) {
567 mHistoryCursor.close();
568 mHistoryCursor = null;
569 }
570 if (mSuggestCursor != null) {
571 mSuggestCursor.close();
572 mSuggestCursor = null;
573 }
574 }
Satish Sampath565505b2009-05-29 15:37:27 +0100575
Mike LeBeau21beb132009-05-13 14:57:50 -0700576 /**
577 * Provides the title (text line 1) for a browser suggestion, which should be the
578 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100579 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700580 * @return the title string to use
581 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700582 private String getHistoryTitle() {
583 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700584 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700585 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700586 }
587 return title;
588 }
Satish Sampath565505b2009-05-29 15:37:27 +0100589
Mike LeBeau21beb132009-05-13 14:57:50 -0700590 /**
591 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
592 * webpage url. If the webpage title is empty, then the url should go in the title
593 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100594 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700595 * @return the subtitle string to use, or null if none
596 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700597 private String getHistorySubtitle() {
598 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700599 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
600 return null;
601 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700602 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700603 }
604 }
Satish Sampath565505b2009-05-29 15:37:27 +0100605
Mike LeBeau21beb132009-05-13 14:57:50 -0700606 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700607 * Strips "http://" from the beginning of a url and "/" from the end,
608 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700609 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700610 private String beautifyUrl(String url) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700611 if (mSearchUrlColorHex == null) {
612 // Get the color used for this purpose from the current theme.
613 TypedValue colorValue = new TypedValue();
614 getContext().getTheme().resolveAttribute(
615 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
616 int color = getContext().getResources().getColor(colorValue.resourceId);
Satish Sampath565505b2009-05-29 15:37:27 +0100617
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700618 // Convert the int color value into a hex string, and strip the first two
619 // characters which will be the alpha transparency (html doesn't want this).
620 mSearchUrlColorHex = Integer.toHexString(color).substring(2);
Mike LeBeau21beb132009-05-13 14:57:50 -0700621 }
Satish Sampath565505b2009-05-29 15:37:27 +0100622
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700623 return "<font color=\"#" + mSearchUrlColorHex + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700624 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800625 }
626
627 @Override
628 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100629 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800630 throws IllegalStateException {
631 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
632
633 int match = URI_MATCHER.match(url);
634 if (match == -1) {
635 throw new IllegalArgumentException("Unknown URL");
636 }
637
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100638 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800639 String suggestSelection;
640 String [] myArgs;
641 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
642 suggestSelection = null;
643 myArgs = null;
644 } else {
645 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700646 if (selectionArgs[0].startsWith("http")
647 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800648 myArgs = new String[1];
649 myArgs[0] = like;
650 suggestSelection = selection;
651 } else {
652 SUGGEST_ARGS[0] = "http://" + like;
653 SUGGEST_ARGS[1] = "http://www." + like;
654 SUGGEST_ARGS[2] = "https://" + like;
655 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scrogginsbd359cc2009-05-26 15:57:35 -0400656 // To match against titles.
657 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800658 myArgs = SUGGEST_ARGS;
659 suggestSelection = SUGGEST_SELECTION;
660 }
661 }
662
663 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
664 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
Leon Scroggins31887fd2009-05-18 16:58:08 -0400665 ORDER_BY, MAX_SUGGESTION_LONG_ENTRIES_STRING);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800666
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100667 if (match == URI_MATCH_BOOKMARKS_SUGGEST
668 || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800669 return new MySuggestionCursor(c, null, "");
670 } else {
671 // get Google suggest if there is still space in the list
672 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400673 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800674 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100675 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400676 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800677 }
678 return new MySuggestionCursor(c, null, selectionArgs[0]);
679 }
680 }
681
682 String[] projection = null;
683 if (projectionIn != null && projectionIn.length > 0) {
684 projection = new String[projectionIn.length + 1];
685 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
686 projection[projectionIn.length] = "_id AS _id";
687 }
688
689 StringBuilder whereClause = new StringBuilder(256);
690 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
691 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
692 .append(")");
693 }
694
695 // Tack on the user's selection, if present
696 if (selection != null && selection.length() > 0) {
697 if (whereClause.length() > 0) {
698 whereClause.append(" AND ");
699 }
700
701 whereClause.append('(');
702 whereClause.append(selection);
703 whereClause.append(')');
704 }
705 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
706 whereClause.toString(), selectionArgs, null, null, sortOrder,
707 null);
708 c.setNotificationUri(getContext().getContentResolver(), url);
709 return c;
710 }
711
712 @Override
713 public String getType(Uri url) {
714 int match = URI_MATCHER.match(url);
715 switch (match) {
716 case URI_MATCH_BOOKMARKS:
717 return "vnd.android.cursor.dir/bookmark";
718
719 case URI_MATCH_BOOKMARKS_ID:
720 return "vnd.android.cursor.item/bookmark";
721
722 case URI_MATCH_SEARCHES:
723 return "vnd.android.cursor.dir/searches";
724
725 case URI_MATCH_SEARCHES_ID:
726 return "vnd.android.cursor.item/searches";
727
728 case URI_MATCH_SUGGEST:
729 return SearchManager.SUGGEST_MIME_TYPE;
730
731 default:
732 throw new IllegalArgumentException("Unknown URL");
733 }
734 }
735
736 @Override
737 public Uri insert(Uri url, ContentValues initialValues) {
738 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
739
740 int match = URI_MATCHER.match(url);
741 Uri uri = null;
742 switch (match) {
743 case URI_MATCH_BOOKMARKS: {
744 // Insert into the bookmarks table
745 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
746 initialValues);
747 if (rowID > 0) {
748 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
749 rowID);
750 }
751 break;
752 }
753
754 case URI_MATCH_SEARCHES: {
755 // Insert into the searches table
756 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
757 initialValues);
758 if (rowID > 0) {
759 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
760 rowID);
761 }
762 break;
763 }
764
765 default:
766 throw new IllegalArgumentException("Unknown URL");
767 }
768
769 if (uri == null) {
770 throw new IllegalArgumentException("Unknown URL");
771 }
772 getContext().getContentResolver().notifyChange(uri, null);
773 return uri;
774 }
775
776 @Override
777 public int delete(Uri url, String where, String[] whereArgs) {
778 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
779
780 int match = URI_MATCHER.match(url);
781 if (match == -1 || match == URI_MATCH_SUGGEST) {
782 throw new IllegalArgumentException("Unknown URL");
783 }
784
785 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
786 StringBuilder sb = new StringBuilder();
787 if (where != null && where.length() > 0) {
788 sb.append("( ");
789 sb.append(where);
790 sb.append(" ) AND ");
791 }
792 sb.append("_id = ");
793 sb.append(url.getPathSegments().get(1));
794 where = sb.toString();
795 }
796
797 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
798 getContext().getContentResolver().notifyChange(url, null);
799 return count;
800 }
801
802 @Override
803 public int update(Uri url, ContentValues values, String where,
804 String[] whereArgs) {
805 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
806
807 int match = URI_MATCHER.match(url);
808 if (match == -1 || match == URI_MATCH_SUGGEST) {
809 throw new IllegalArgumentException("Unknown URL");
810 }
811
812 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
813 StringBuilder sb = new StringBuilder();
814 if (where != null && where.length() > 0) {
815 sb.append("( ");
816 sb.append(where);
817 sb.append(" ) AND ");
818 }
819 sb.append("_id = ");
820 sb.append(url.getPathSegments().get(1));
821 where = sb.toString();
822 }
823
824 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
825 getContext().getContentResolver().notifyChange(url, null);
826 return ret;
827 }
Satish Sampath565505b2009-05-29 15:37:27 +0100828
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700829 /**
830 * Strips the provided url of preceding "http://" and any trailing "/". Does not
831 * strip "https://". If the provided string cannot be stripped, the original string
832 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +0100833 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700834 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +0100835 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700836 * @param url a url to strip, like "http://www.google.com/"
837 * @return a stripped url like "www.google.com", or the original string if it could
838 * not be stripped
839 */
840 private static String stripUrl(String url) {
841 if (url == null) return null;
842 Matcher m = STRIP_URL_PATTERN.matcher(url);
843 if (m.matches() && m.groupCount() == 3) {
844 return m.group(2);
845 } else {
846 return url;
847 }
848 }
849
The Android Open Source Project0c908882009-03-03 19:32:16 -0800850}