blob: 9b723644d8a5e5aa6034f199b4e5c6ca1b9ce761 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -070019import com.google.android.providers.GoogleSettings.Partner;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070020
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.app.SearchManager;
22import android.content.ComponentName;
23import android.content.ContentProvider;
24import android.content.ContentUris;
25import android.content.ContentValues;
26import android.content.Context;
27import android.content.Intent;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070028import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.content.UriMatcher;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070030import android.content.SharedPreferences.Editor;
Satish Sampath565505b2009-05-29 15:37:27 +010031import android.content.pm.PackageManager;
32import android.content.pm.ResolveInfo;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.database.AbstractCursor;
Leon Scroggins62b71f72009-06-12 17:51:22 -040034import android.database.ContentObserver;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.database.sqlite.SQLiteDatabase;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010037import android.database.sqlite.SQLiteOpenHelper;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.net.Uri;
Leon Scroggins62b71f72009-06-12 17:51:22 -040039import android.os.Handler;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070040import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080041import android.provider.Browser;
Leon Scroggins62b71f72009-06-12 17:51:22 -040042import android.provider.Settings;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043import android.server.search.SearchableInfo;
Mike LeBeau21beb132009-05-13 14:57:50 -070044import android.text.TextUtils;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.text.util.Regex;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010046import android.util.Log;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070047import android.util.TypedValue;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010048
49import java.util.Date;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070050import java.util.regex.Matcher;
51import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -080052
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -070053
The Android Open Source Project0c908882009-03-03 19:32:16 -080054public class BrowserProvider extends ContentProvider {
55
56 private SQLiteOpenHelper mOpenHelper;
57 private static final String sDatabaseName = "browser.db";
58 private static final String TAG = "BrowserProvider";
59 private static final String ORDER_BY = "visits DESC, date DESC";
60
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070061 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
62 "viewer?source=androidclient";
63
The Android Open Source Project0c908882009-03-03 19:32:16 -080064 private static final String[] TABLE_NAMES = new String[] {
65 "bookmarks", "searches"
66 };
67 private static final String[] SUGGEST_PROJECTION = new String[] {
68 "_id", "url", "title", "bookmark"
69 };
Satish Sampath565505b2009-05-29 15:37:27 +010070 private static final String SUGGEST_SELECTION =
Leon Scroggins740eadf2009-06-15 12:34:39 -040071 "url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?"
72 + " OR title LIKE ?";
73 private String[] SUGGEST_ARGS = new String[5];
The Android Open Source Project0c908882009-03-03 19:32:16 -080074
75 // shared suggestion array index, make sure to match COLUMNS
76 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
77 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
78 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
79 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
80 private static final int SUGGEST_COLUMN_ICON_1_ID = 5;
81 private static final int SUGGEST_COLUMN_ICON_2_ID = 6;
82 private static final int SUGGEST_COLUMN_QUERY_ID = 7;
Mike LeBeau1ef26a32009-05-13 20:11:00 -070083 private static final int SUGGEST_COLUMN_FORMAT = 8;
The Android Open Source Project0c908882009-03-03 19:32:16 -080084
85 // shared suggestion columns
86 private static final String[] COLUMNS = new String[] {
87 "_id",
88 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
89 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
90 SearchManager.SUGGEST_COLUMN_TEXT_1,
91 SearchManager.SUGGEST_COLUMN_TEXT_2,
92 SearchManager.SUGGEST_COLUMN_ICON_1,
93 SearchManager.SUGGEST_COLUMN_ICON_2,
Mike LeBeau1ef26a32009-05-13 20:11:00 -070094 SearchManager.SUGGEST_COLUMN_QUERY,
95 SearchManager.SUGGEST_COLUMN_FORMAT};
The Android Open Source Project0c908882009-03-03 19:32:16 -080096
97 private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3;
98 private static final int MAX_SUGGESTION_LONG_ENTRIES = 6;
99
100 // make sure that these match the index of TABLE_NAMES
101 private static final int URI_MATCH_BOOKMARKS = 0;
102 private static final int URI_MATCH_SEARCHES = 1;
103 // (id % 10) should match the table name index
104 private static final int URI_MATCH_BOOKMARKS_ID = 10;
105 private static final int URI_MATCH_SEARCHES_ID = 11;
106 //
107 private static final int URI_MATCH_SUGGEST = 20;
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100108 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109
110 private static final UriMatcher URI_MATCHER;
111
112 static {
113 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
114 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
115 URI_MATCH_BOOKMARKS);
116 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
117 URI_MATCH_BOOKMARKS_ID);
118 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
119 URI_MATCH_SEARCHES);
120 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
121 URI_MATCH_SEARCHES_ID);
122 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
123 URI_MATCH_SUGGEST);
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100124 URI_MATCHER.addURI("browser",
125 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
126 URI_MATCH_BOOKMARKS_SUGGEST);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800127 }
128
129 // 1 -> 2 add cache table
130 // 2 -> 3 update history table
131 // 3 -> 4 add passwords table
132 // 4 -> 5 add settings table
133 // 5 -> 6 ?
134 // 6 -> 7 ?
135 // 7 -> 8 drop proxy table
136 // 8 -> 9 drop settings table
137 // 9 -> 10 add form_urls and form_data
138 // 10 -> 11 add searches table
139 // 11 -> 12 modify cache table
140 // 12 -> 13 modify cache table
141 // 13 -> 14 correspond with Google Bookmarks schema
142 // 14 -> 15 move couple of tables to either browser private database or webview database
143 // 15 -> 17 Set it up for the SearchManager
144 // 17 -> 18 Added favicon in bookmarks table for Home shortcuts
145 // 18 -> 19 Remove labels table
146 private static final int DATABASE_VERSION = 19;
Satish Sampath565505b2009-05-29 15:37:27 +0100147
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700148 // Regular expression which matches http://, followed by some stuff, followed by
149 // optionally a trailing slash, all matched as separate groups.
150 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
Satish Sampath565505b2009-05-29 15:37:27 +0100151
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100152 private SearchManager mSearchManager;
153
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700154 // The hex color string to be applied to urls of website suggestions, as derived from
155 // the current theme. This is not set until/unless beautifyUrl is called, at which point
156 // this variable caches the color value.
157 private static String mSearchUrlColorHex;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158
159 public BrowserProvider() {
160 }
Satish Sampath565505b2009-05-29 15:37:27 +0100161
The Android Open Source Project0c908882009-03-03 19:32:16 -0800162
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700163 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164 StringBuffer sb = new StringBuffer();
165 int lastCharLoc = 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100166
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700167 final String client_id = Partner.getString(context.getContentResolver(), Partner.CLIENT_ID);
168
The Android Open Source Project0c908882009-03-03 19:32:16 -0800169 for (int i = 0; i < srcString.length(); ++i) {
170 char c = srcString.charAt(i);
171 if (c == '{') {
172 sb.append(srcString.subSequence(lastCharLoc, i));
173 lastCharLoc = i;
174 inner:
175 for (int j = i; j < srcString.length(); ++j) {
176 char k = srcString.charAt(j);
177 if (k == '}') {
178 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700179 if (propertyKeyValue.equals("CLIENT_ID")) {
180 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800181 } else {
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700182 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800183 }
184 lastCharLoc = j + 1;
185 i = j;
186 break inner;
187 }
188 }
189 }
190 }
191 if (srcString.length() - lastCharLoc > 0) {
192 // Put on the tail, if there is one
193 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
194 }
195 return sb;
196 }
197
198 private static class DatabaseHelper extends SQLiteOpenHelper {
199 private Context mContext;
200
201 public DatabaseHelper(Context context) {
202 super(context, sDatabaseName, null, DATABASE_VERSION);
203 mContext = context;
204 }
205
206 @Override
207 public void onCreate(SQLiteDatabase db) {
208 db.execSQL("CREATE TABLE bookmarks (" +
209 "_id INTEGER PRIMARY KEY," +
210 "title TEXT," +
211 "url TEXT," +
212 "visits INTEGER," +
213 "date LONG," +
214 "created LONG," +
215 "description TEXT," +
216 "bookmark INTEGER," +
217 "favicon BLOB DEFAULT NULL" +
218 ");");
219
220 final CharSequence[] bookmarks = mContext.getResources()
221 .getTextArray(R.array.bookmarks);
222 int size = bookmarks.length;
223 try {
224 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700225 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800226 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
227 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100228 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800229 "', 0, 0, 0, 1);");
230 }
231 } catch (ArrayIndexOutOfBoundsException e) {
232 }
233
234 db.execSQL("CREATE TABLE searches (" +
235 "_id INTEGER PRIMARY KEY," +
236 "search TEXT," +
237 "date LONG" +
238 ");");
239 }
240
241 @Override
242 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
243 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
244 + newVersion + ", which will destroy all old data");
245 if (oldVersion == 18) {
246 db.execSQL("DROP TABLE IF EXISTS labels");
247 } else {
248 db.execSQL("DROP TABLE IF EXISTS bookmarks");
249 db.execSQL("DROP TABLE IF EXISTS searches");
250 onCreate(db);
251 }
252 }
253 }
254
255 @Override
256 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700257 final Context context = getContext();
258 mOpenHelper = new DatabaseHelper(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100259 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700260 // To avoid erasing the bookmark table, we added it explicitly for
261 // version 18 and 19 as in the other cases, we will erase the table.
262 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
263 SharedPreferences p = PreferenceManager
264 .getDefaultSharedPreferences(context);
265 boolean fix = p.getBoolean("fix_picasa", true);
266 if (fix) {
267 fixPicasaBookmark();
268 Editor ed = p.edit();
269 ed.putBoolean("fix_picasa", false);
270 ed.commit();
271 }
272 }
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100273 mSearchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400274 mShowWebSuggestionsSettingChangeObserver
275 = new ShowWebSuggestionsSettingChangeObserver();
276 context.getContentResolver().registerContentObserver(
277 Settings.System.getUriFor(
278 Settings.System.SHOW_WEB_SUGGESTIONS),
279 true, mShowWebSuggestionsSettingChangeObserver);
280 updateShowWebSuggestions();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800281 return true;
282 }
283
Leon Scroggins62b71f72009-06-12 17:51:22 -0400284 /**
285 * This Observer will ensure that if the user changes the system
286 * setting of whether to display web suggestions, we will
287 * change accordingly.
288 */
289 /* package */ class ShowWebSuggestionsSettingChangeObserver
290 extends ContentObserver {
291 public ShowWebSuggestionsSettingChangeObserver() {
292 super(new Handler());
293 }
294
295 @Override
296 public void onChange(boolean selfChange) {
297 updateShowWebSuggestions();
298 }
299 }
300
301 private ShowWebSuggestionsSettingChangeObserver
302 mShowWebSuggestionsSettingChangeObserver;
303
304 // If non-null, then the system is set to show web suggestions,
305 // and this is the SearchableInfo to use to get them.
306 private SearchableInfo mSearchableInfo;
307
308 /**
309 * Check the system settings to see whether web suggestions are
310 * allowed. If so, store the SearchableInfo to grab suggestions
311 * while the user is typing.
312 */
313 private void updateShowWebSuggestions() {
314 mSearchableInfo = null;
315 Context context = getContext();
316 if (Settings.System.getInt(context.getContentResolver(),
317 Settings.System.SHOW_WEB_SUGGESTIONS,
318 1 /* default on */) == 1) {
319 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
320 intent.addCategory(Intent.CATEGORY_DEFAULT);
321 ResolveInfo info = context.getPackageManager().resolveActivity(
322 intent, PackageManager.MATCH_DEFAULT_ONLY);
323 if (info != null) {
324 ComponentName googleSearchComponent =
325 new ComponentName(info.activityInfo.packageName,
326 info.activityInfo.name);
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100327 mSearchableInfo = mSearchManager.getSearchableInfo(
Leon Scroggins62b71f72009-06-12 17:51:22 -0400328 googleSearchComponent, false);
329 }
330 }
331 }
332
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700333 private void fixPicasaBookmark() {
334 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
335 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
336 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
337 try {
338 if (!cursor.moveToFirst()) {
339 // set "created" so that it will be on the top of the list
340 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
341 "date, created, bookmark)" + " VALUES('" +
342 getContext().getString(R.string.picasa) + "', '"
343 + PICASA_URL + "', 0, 0, " + new Date().getTime()
344 + ", 1);");
345 }
346 } finally {
347 if (cursor != null) {
348 cursor.close();
349 }
350 }
351 }
352
The Android Open Source Project0c908882009-03-03 19:32:16 -0800353 /*
354 * Subclass AbstractCursor so we can combine multiple Cursors and add
355 * "Google Search".
356 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100357 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
The Android Open Source Project0c908882009-03-03 19:32:16 -0800358 * "Google Search";
Satish Sampath565505b2009-05-29 15:37:27 +0100359 * 2. If bookmark/history entries are less than
The Android Open Source Project0c908882009-03-03 19:32:16 -0800360 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
361 */
362 private class MySuggestionCursor extends AbstractCursor {
363 private Cursor mHistoryCursor;
364 private Cursor mSuggestCursor;
365 private int mHistoryCount;
366 private int mSuggestionCount;
367 private boolean mBeyondCursor;
368 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100369 private int mSuggestText1Id;
370 private int mSuggestText2Id;
371 private int mSuggestQueryId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800372
373 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
374 mHistoryCursor = hc;
375 mSuggestCursor = sc;
376 mHistoryCount = hc.getCount();
377 mSuggestionCount = sc != null ? sc.getCount() : 0;
378 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
379 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
380 }
381 mString = string;
382 mBeyondCursor = false;
Satish Sampath565505b2009-05-29 15:37:27 +0100383
384 // Some web suggest providers only give suggestions and have no description string for
385 // items. The order of the result columns may be different as well. So retrieve the
386 // column indices for the fields we need now and check before using below.
387 if (mSuggestCursor == null) {
388 mSuggestText1Id = -1;
389 mSuggestText2Id = -1;
390 mSuggestQueryId = -1;
391 } else {
392 mSuggestText1Id = mSuggestCursor.getColumnIndex(
393 SearchManager.SUGGEST_COLUMN_TEXT_1);
394 mSuggestText2Id = mSuggestCursor.getColumnIndex(
395 SearchManager.SUGGEST_COLUMN_TEXT_2);
396 mSuggestQueryId = mSuggestCursor.getColumnIndex(
397 SearchManager.SUGGEST_COLUMN_QUERY);
398 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800399 }
400
401 @Override
402 public boolean onMove(int oldPosition, int newPosition) {
403 if (mHistoryCursor == null) {
404 return false;
405 }
406 if (mHistoryCount > newPosition) {
407 mHistoryCursor.moveToPosition(newPosition);
408 mBeyondCursor = false;
409 } else if (mHistoryCount + mSuggestionCount > newPosition) {
410 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
411 mBeyondCursor = false;
412 } else {
413 mBeyondCursor = true;
414 }
415 return true;
416 }
417
418 @Override
419 public int getCount() {
420 if (mString.length() > 0) {
421 return mHistoryCount + mSuggestionCount + 1;
422 } else {
423 return mHistoryCount + mSuggestionCount;
424 }
425 }
426
427 @Override
428 public String[] getColumnNames() {
429 return COLUMNS;
430 }
Satish Sampath565505b2009-05-29 15:37:27 +0100431
The Android Open Source Project0c908882009-03-03 19:32:16 -0800432 @Override
433 public String getString(int columnIndex) {
434 if ((mPos != -1 && mHistoryCursor != null)) {
435 switch(columnIndex) {
436 case SUGGEST_COLUMN_INTENT_ACTION_ID:
437 if (mHistoryCount > mPos) {
438 return Intent.ACTION_VIEW;
439 } else {
440 return Intent.ACTION_SEARCH;
441 }
442
443 case SUGGEST_COLUMN_INTENT_DATA_ID:
444 if (mHistoryCount > mPos) {
445 return mHistoryCursor.getString(1);
446 } else {
447 return null;
448 }
449
450 case SUGGEST_COLUMN_TEXT_1_ID:
451 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700452 return getHistoryTitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800453 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100454 if (mSuggestText1Id == -1) return null;
455 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800456 } else {
457 return mString;
458 }
459
460 case SUGGEST_COLUMN_TEXT_2_ID:
461 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700462 return getHistorySubtitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800463 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100464 if (mSuggestText2Id == -1) return null;
465 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800466 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700467 return getContext().getString(R.string.search_the_web);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800468 }
469
470 case SUGGEST_COLUMN_ICON_1_ID:
471 if (mHistoryCount > mPos) {
472 if (mHistoryCursor.getInt(3) == 1) {
473 return new Integer(
474 R.drawable.ic_search_category_bookmark)
475 .toString();
476 } else {
477 return new Integer(
478 R.drawable.ic_search_category_history)
479 .toString();
480 }
481 } else {
482 return new Integer(
483 R.drawable.ic_search_category_suggest)
484 .toString();
485 }
486
487 case SUGGEST_COLUMN_ICON_2_ID:
488 return new String("0");
489
490 case SUGGEST_COLUMN_QUERY_ID:
491 if (mHistoryCount > mPos) {
492 return null;
493 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100494 if (mSuggestQueryId == -1) return null;
495 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800496 } else {
497 return mString;
498 }
Satish Sampath565505b2009-05-29 15:37:27 +0100499
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700500 case SUGGEST_COLUMN_FORMAT:
501 return "html";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800502 }
503 }
504 return null;
505 }
506
507 @Override
508 public double getDouble(int column) {
509 throw new UnsupportedOperationException();
510 }
511
512 @Override
513 public float getFloat(int column) {
514 throw new UnsupportedOperationException();
515 }
516
517 @Override
518 public int getInt(int column) {
519 throw new UnsupportedOperationException();
520 }
521
522 @Override
523 public long getLong(int column) {
524 if ((mPos != -1) && column == 0) {
525 return mPos; // use row# as the _Id
526 }
527 throw new UnsupportedOperationException();
528 }
529
530 @Override
531 public short getShort(int column) {
532 throw new UnsupportedOperationException();
533 }
534
535 @Override
536 public boolean isNull(int column) {
537 throw new UnsupportedOperationException();
538 }
539
540 // TODO Temporary change, finalize after jq's changes go in
541 public void deactivate() {
542 if (mHistoryCursor != null) {
543 mHistoryCursor.deactivate();
544 }
545 if (mSuggestCursor != null) {
546 mSuggestCursor.deactivate();
547 }
548 super.deactivate();
549 }
550
551 public boolean requery() {
552 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
553 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
554 }
555
556 // TODO Temporary change, finalize after jq's changes go in
557 public void close() {
558 super.close();
559 if (mHistoryCursor != null) {
560 mHistoryCursor.close();
561 mHistoryCursor = null;
562 }
563 if (mSuggestCursor != null) {
564 mSuggestCursor.close();
565 mSuggestCursor = null;
566 }
567 }
Satish Sampath565505b2009-05-29 15:37:27 +0100568
Mike LeBeau21beb132009-05-13 14:57:50 -0700569 /**
570 * Provides the title (text line 1) for a browser suggestion, which should be the
571 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100572 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700573 * @return the title string to use
574 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700575 private String getHistoryTitle() {
576 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700577 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700578 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700579 }
580 return title;
581 }
Satish Sampath565505b2009-05-29 15:37:27 +0100582
Mike LeBeau21beb132009-05-13 14:57:50 -0700583 /**
584 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
585 * webpage url. If the webpage title is empty, then the url should go in the title
586 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100587 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700588 * @return the subtitle string to use, or null if none
589 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700590 private String getHistorySubtitle() {
591 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700592 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
593 return null;
594 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700595 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700596 }
597 }
Satish Sampath565505b2009-05-29 15:37:27 +0100598
Mike LeBeau21beb132009-05-13 14:57:50 -0700599 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700600 * Strips "http://" from the beginning of a url and "/" from the end,
601 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700602 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700603 private String beautifyUrl(String url) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700604 if (mSearchUrlColorHex == null) {
605 // Get the color used for this purpose from the current theme.
606 TypedValue colorValue = new TypedValue();
607 getContext().getTheme().resolveAttribute(
608 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
609 int color = getContext().getResources().getColor(colorValue.resourceId);
Satish Sampath565505b2009-05-29 15:37:27 +0100610
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700611 // Convert the int color value into a hex string, and strip the first two
612 // characters which will be the alpha transparency (html doesn't want this).
613 mSearchUrlColorHex = Integer.toHexString(color).substring(2);
Mike LeBeau21beb132009-05-13 14:57:50 -0700614 }
Satish Sampath565505b2009-05-29 15:37:27 +0100615
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700616 return "<font color=\"#" + mSearchUrlColorHex + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700617 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800618 }
619
620 @Override
621 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100622 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800623 throws IllegalStateException {
624 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
625
626 int match = URI_MATCHER.match(url);
627 if (match == -1) {
628 throw new IllegalArgumentException("Unknown URL");
629 }
630
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100631 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800632 String suggestSelection;
633 String [] myArgs;
634 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
635 suggestSelection = null;
636 myArgs = null;
637 } else {
638 String like = selectionArgs[0] + "%";
639 if (selectionArgs[0].startsWith("http")) {
640 myArgs = new String[1];
641 myArgs[0] = like;
642 suggestSelection = selection;
643 } else {
644 SUGGEST_ARGS[0] = "http://" + like;
645 SUGGEST_ARGS[1] = "http://www." + like;
646 SUGGEST_ARGS[2] = "https://" + like;
647 SUGGEST_ARGS[3] = "https://www." + like;
Leon Scroggins740eadf2009-06-15 12:34:39 -0400648 // To match against titles.
649 SUGGEST_ARGS[4] = like;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800650 myArgs = SUGGEST_ARGS;
651 suggestSelection = SUGGEST_SELECTION;
652 }
653 }
654
655 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
656 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
657 ORDER_BY,
658 (new Integer(MAX_SUGGESTION_LONG_ENTRIES)).toString());
659
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100660 if (match == URI_MATCH_BOOKMARKS_SUGGEST
661 || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800662 return new MySuggestionCursor(c, null, "");
663 } else {
664 // get Google suggest if there is still space in the list
665 if (myArgs != null && myArgs.length > 1
Leon Scroggins62b71f72009-06-12 17:51:22 -0400666 && mSearchableInfo != null
The Android Open Source Project0c908882009-03-03 19:32:16 -0800667 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringertd8b0ad22009-06-22 10:36:29 +0100668 Cursor sc = mSearchManager.getSuggestions(mSearchableInfo, selectionArgs[0]);
Leon Scroggins62b71f72009-06-12 17:51:22 -0400669 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800670 }
671 return new MySuggestionCursor(c, null, selectionArgs[0]);
672 }
673 }
674
675 String[] projection = null;
676 if (projectionIn != null && projectionIn.length > 0) {
677 projection = new String[projectionIn.length + 1];
678 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
679 projection[projectionIn.length] = "_id AS _id";
680 }
681
682 StringBuilder whereClause = new StringBuilder(256);
683 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
684 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
685 .append(")");
686 }
687
688 // Tack on the user's selection, if present
689 if (selection != null && selection.length() > 0) {
690 if (whereClause.length() > 0) {
691 whereClause.append(" AND ");
692 }
693
694 whereClause.append('(');
695 whereClause.append(selection);
696 whereClause.append(')');
697 }
698 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
699 whereClause.toString(), selectionArgs, null, null, sortOrder,
700 null);
701 c.setNotificationUri(getContext().getContentResolver(), url);
702 return c;
703 }
704
705 @Override
706 public String getType(Uri url) {
707 int match = URI_MATCHER.match(url);
708 switch (match) {
709 case URI_MATCH_BOOKMARKS:
710 return "vnd.android.cursor.dir/bookmark";
711
712 case URI_MATCH_BOOKMARKS_ID:
713 return "vnd.android.cursor.item/bookmark";
714
715 case URI_MATCH_SEARCHES:
716 return "vnd.android.cursor.dir/searches";
717
718 case URI_MATCH_SEARCHES_ID:
719 return "vnd.android.cursor.item/searches";
720
721 case URI_MATCH_SUGGEST:
722 return SearchManager.SUGGEST_MIME_TYPE;
723
724 default:
725 throw new IllegalArgumentException("Unknown URL");
726 }
727 }
728
729 @Override
730 public Uri insert(Uri url, ContentValues initialValues) {
731 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
732
733 int match = URI_MATCHER.match(url);
734 Uri uri = null;
735 switch (match) {
736 case URI_MATCH_BOOKMARKS: {
737 // Insert into the bookmarks table
738 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
739 initialValues);
740 if (rowID > 0) {
741 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
742 rowID);
743 }
744 break;
745 }
746
747 case URI_MATCH_SEARCHES: {
748 // Insert into the searches table
749 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
750 initialValues);
751 if (rowID > 0) {
752 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
753 rowID);
754 }
755 break;
756 }
757
758 default:
759 throw new IllegalArgumentException("Unknown URL");
760 }
761
762 if (uri == null) {
763 throw new IllegalArgumentException("Unknown URL");
764 }
765 getContext().getContentResolver().notifyChange(uri, null);
766 return uri;
767 }
768
769 @Override
770 public int delete(Uri url, String where, String[] whereArgs) {
771 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
772
773 int match = URI_MATCHER.match(url);
774 if (match == -1 || match == URI_MATCH_SUGGEST) {
775 throw new IllegalArgumentException("Unknown URL");
776 }
777
778 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
779 StringBuilder sb = new StringBuilder();
780 if (where != null && where.length() > 0) {
781 sb.append("( ");
782 sb.append(where);
783 sb.append(" ) AND ");
784 }
785 sb.append("_id = ");
786 sb.append(url.getPathSegments().get(1));
787 where = sb.toString();
788 }
789
790 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
791 getContext().getContentResolver().notifyChange(url, null);
792 return count;
793 }
794
795 @Override
796 public int update(Uri url, ContentValues values, String where,
797 String[] whereArgs) {
798 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
799
800 int match = URI_MATCHER.match(url);
801 if (match == -1 || match == URI_MATCH_SUGGEST) {
802 throw new IllegalArgumentException("Unknown URL");
803 }
804
805 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
806 StringBuilder sb = new StringBuilder();
807 if (where != null && where.length() > 0) {
808 sb.append("( ");
809 sb.append(where);
810 sb.append(" ) AND ");
811 }
812 sb.append("_id = ");
813 sb.append(url.getPathSegments().get(1));
814 where = sb.toString();
815 }
816
817 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
818 getContext().getContentResolver().notifyChange(url, null);
819 return ret;
820 }
Satish Sampath565505b2009-05-29 15:37:27 +0100821
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700822 /**
823 * Strips the provided url of preceding "http://" and any trailing "/". Does not
824 * strip "https://". If the provided string cannot be stripped, the original string
825 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +0100826 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700827 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +0100828 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700829 * @param url a url to strip, like "http://www.google.com/"
830 * @return a stripped url like "www.google.com", or the original string if it could
831 * not be stripped
832 */
833 private static String stripUrl(String url) {
834 if (url == null) return null;
835 Matcher m = STRIP_URL_PATTERN.matcher(url);
836 if (m.matches() && m.groupCount() == 3) {
837 return m.group(2);
838 } else {
839 return url;
840 }
841 }
842
The Android Open Source Project0c908882009-03-03 19:32:16 -0800843}