blob: d2f4a0a066143ba5d6492f12ceba5c2d2fcae799 [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;
34import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.database.sqlite.SQLiteDatabase;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010036import android.database.sqlite.SQLiteOpenHelper;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.net.Uri;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070038import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080039import android.provider.Browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040import android.server.search.SearchableInfo;
Mike LeBeau21beb132009-05-13 14:57:50 -070041import android.text.TextUtils;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042import android.text.util.Regex;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010043import android.util.Log;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070044import android.util.TypedValue;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010045
46import java.util.Date;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070047import java.util.regex.Matcher;
48import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -080049
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -070050
The Android Open Source Project0c908882009-03-03 19:32:16 -080051public class BrowserProvider extends ContentProvider {
52
53 private SQLiteOpenHelper mOpenHelper;
54 private static final String sDatabaseName = "browser.db";
55 private static final String TAG = "BrowserProvider";
56 private static final String ORDER_BY = "visits DESC, date DESC";
57
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070058 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
59 "viewer?source=androidclient";
60
The Android Open Source Project0c908882009-03-03 19:32:16 -080061 private static final String[] TABLE_NAMES = new String[] {
62 "bookmarks", "searches"
63 };
64 private static final String[] SUGGEST_PROJECTION = new String[] {
65 "_id", "url", "title", "bookmark"
66 };
Satish Sampath565505b2009-05-29 15:37:27 +010067 private static final String SUGGEST_SELECTION =
The Android Open Source Project0c908882009-03-03 19:32:16 -080068 "url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?";
69 private String[] SUGGEST_ARGS = new String[4];
70
71 // shared suggestion array index, make sure to match COLUMNS
72 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
73 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
74 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
75 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
76 private static final int SUGGEST_COLUMN_ICON_1_ID = 5;
77 private static final int SUGGEST_COLUMN_ICON_2_ID = 6;
78 private static final int SUGGEST_COLUMN_QUERY_ID = 7;
Mike LeBeau1ef26a32009-05-13 20:11:00 -070079 private static final int SUGGEST_COLUMN_FORMAT = 8;
The Android Open Source Project0c908882009-03-03 19:32:16 -080080
81 // shared suggestion columns
82 private static final String[] COLUMNS = new String[] {
83 "_id",
84 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
85 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
86 SearchManager.SUGGEST_COLUMN_TEXT_1,
87 SearchManager.SUGGEST_COLUMN_TEXT_2,
88 SearchManager.SUGGEST_COLUMN_ICON_1,
89 SearchManager.SUGGEST_COLUMN_ICON_2,
Mike LeBeau1ef26a32009-05-13 20:11:00 -070090 SearchManager.SUGGEST_COLUMN_QUERY,
91 SearchManager.SUGGEST_COLUMN_FORMAT};
The Android Open Source Project0c908882009-03-03 19:32:16 -080092
93 private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3;
94 private static final int MAX_SUGGESTION_LONG_ENTRIES = 6;
95
96 // make sure that these match the index of TABLE_NAMES
97 private static final int URI_MATCH_BOOKMARKS = 0;
98 private static final int URI_MATCH_SEARCHES = 1;
99 // (id % 10) should match the table name index
100 private static final int URI_MATCH_BOOKMARKS_ID = 10;
101 private static final int URI_MATCH_SEARCHES_ID = 11;
102 //
103 private static final int URI_MATCH_SUGGEST = 20;
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100104 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800105
106 private static final UriMatcher URI_MATCHER;
107
108 static {
109 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
110 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
111 URI_MATCH_BOOKMARKS);
112 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
113 URI_MATCH_BOOKMARKS_ID);
114 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
115 URI_MATCH_SEARCHES);
116 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
117 URI_MATCH_SEARCHES_ID);
118 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
119 URI_MATCH_SUGGEST);
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100120 URI_MATCHER.addURI("browser",
121 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
122 URI_MATCH_BOOKMARKS_SUGGEST);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123 }
124
125 // 1 -> 2 add cache table
126 // 2 -> 3 update history table
127 // 3 -> 4 add passwords table
128 // 4 -> 5 add settings table
129 // 5 -> 6 ?
130 // 6 -> 7 ?
131 // 7 -> 8 drop proxy table
132 // 8 -> 9 drop settings table
133 // 9 -> 10 add form_urls and form_data
134 // 10 -> 11 add searches table
135 // 11 -> 12 modify cache table
136 // 12 -> 13 modify cache table
137 // 13 -> 14 correspond with Google Bookmarks schema
138 // 14 -> 15 move couple of tables to either browser private database or webview database
139 // 15 -> 17 Set it up for the SearchManager
140 // 17 -> 18 Added favicon in bookmarks table for Home shortcuts
141 // 18 -> 19 Remove labels table
142 private static final int DATABASE_VERSION = 19;
Satish Sampath565505b2009-05-29 15:37:27 +0100143
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700144 // Regular expression which matches http://, followed by some stuff, followed by
145 // optionally a trailing slash, all matched as separate groups.
146 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
Satish Sampath565505b2009-05-29 15:37:27 +0100147
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700148 // The hex color string to be applied to urls of website suggestions, as derived from
149 // the current theme. This is not set until/unless beautifyUrl is called, at which point
150 // this variable caches the color value.
151 private static String mSearchUrlColorHex;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800152
153 public BrowserProvider() {
154 }
Satish Sampath565505b2009-05-29 15:37:27 +0100155
The Android Open Source Project0c908882009-03-03 19:32:16 -0800156
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700157 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800158 StringBuffer sb = new StringBuffer();
159 int lastCharLoc = 0;
Satish Sampath565505b2009-05-29 15:37:27 +0100160
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700161 final String client_id = Partner.getString(context.getContentResolver(), Partner.CLIENT_ID);
162
The Android Open Source Project0c908882009-03-03 19:32:16 -0800163 for (int i = 0; i < srcString.length(); ++i) {
164 char c = srcString.charAt(i);
165 if (c == '{') {
166 sb.append(srcString.subSequence(lastCharLoc, i));
167 lastCharLoc = i;
168 inner:
169 for (int j = i; j < srcString.length(); ++j) {
170 char k = srcString.charAt(j);
171 if (k == '}') {
172 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700173 if (propertyKeyValue.equals("CLIENT_ID")) {
174 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800175 } else {
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700176 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800177 }
178 lastCharLoc = j + 1;
179 i = j;
180 break inner;
181 }
182 }
183 }
184 }
185 if (srcString.length() - lastCharLoc > 0) {
186 // Put on the tail, if there is one
187 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
188 }
189 return sb;
190 }
191
192 private static class DatabaseHelper extends SQLiteOpenHelper {
193 private Context mContext;
194
195 public DatabaseHelper(Context context) {
196 super(context, sDatabaseName, null, DATABASE_VERSION);
197 mContext = context;
198 }
199
200 @Override
201 public void onCreate(SQLiteDatabase db) {
202 db.execSQL("CREATE TABLE bookmarks (" +
203 "_id INTEGER PRIMARY KEY," +
204 "title TEXT," +
205 "url TEXT," +
206 "visits INTEGER," +
207 "date LONG," +
208 "created LONG," +
209 "description TEXT," +
210 "bookmark INTEGER," +
211 "favicon BLOB DEFAULT NULL" +
212 ");");
213
214 final CharSequence[] bookmarks = mContext.getResources()
215 .getTextArray(R.array.bookmarks);
216 int size = bookmarks.length;
217 try {
218 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -0700219 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800220 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
221 "date, created, bookmark)" + " VALUES('" +
Satish Sampath565505b2009-05-29 15:37:27 +0100222 bookmarks[i] + "', '" + bookmarkDestination +
The Android Open Source Project0c908882009-03-03 19:32:16 -0800223 "', 0, 0, 0, 1);");
224 }
225 } catch (ArrayIndexOutOfBoundsException e) {
226 }
227
228 db.execSQL("CREATE TABLE searches (" +
229 "_id INTEGER PRIMARY KEY," +
230 "search TEXT," +
231 "date LONG" +
232 ");");
233 }
234
235 @Override
236 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
237 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
238 + newVersion + ", which will destroy all old data");
239 if (oldVersion == 18) {
240 db.execSQL("DROP TABLE IF EXISTS labels");
241 } else {
242 db.execSQL("DROP TABLE IF EXISTS bookmarks");
243 db.execSQL("DROP TABLE IF EXISTS searches");
244 onCreate(db);
245 }
246 }
247 }
248
249 @Override
250 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700251 final Context context = getContext();
252 mOpenHelper = new DatabaseHelper(context);
Satish Sampath565505b2009-05-29 15:37:27 +0100253 // we added "picasa web album" into default bookmarks for version 19.
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700254 // To avoid erasing the bookmark table, we added it explicitly for
255 // version 18 and 19 as in the other cases, we will erase the table.
256 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
257 SharedPreferences p = PreferenceManager
258 .getDefaultSharedPreferences(context);
259 boolean fix = p.getBoolean("fix_picasa", true);
260 if (fix) {
261 fixPicasaBookmark();
262 Editor ed = p.edit();
263 ed.putBoolean("fix_picasa", false);
264 ed.commit();
265 }
266 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800267 return true;
268 }
269
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700270 private void fixPicasaBookmark() {
271 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
272 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
273 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
274 try {
275 if (!cursor.moveToFirst()) {
276 // set "created" so that it will be on the top of the list
277 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
278 "date, created, bookmark)" + " VALUES('" +
279 getContext().getString(R.string.picasa) + "', '"
280 + PICASA_URL + "', 0, 0, " + new Date().getTime()
281 + ", 1);");
282 }
283 } finally {
284 if (cursor != null) {
285 cursor.close();
286 }
287 }
288 }
289
The Android Open Source Project0c908882009-03-03 19:32:16 -0800290 /*
291 * Subclass AbstractCursor so we can combine multiple Cursors and add
292 * "Google Search".
293 * Here are the rules.
Satish Sampath565505b2009-05-29 15:37:27 +0100294 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
The Android Open Source Project0c908882009-03-03 19:32:16 -0800295 * "Google Search";
Satish Sampath565505b2009-05-29 15:37:27 +0100296 * 2. If bookmark/history entries are less than
The Android Open Source Project0c908882009-03-03 19:32:16 -0800297 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
298 */
299 private class MySuggestionCursor extends AbstractCursor {
300 private Cursor mHistoryCursor;
301 private Cursor mSuggestCursor;
302 private int mHistoryCount;
303 private int mSuggestionCount;
304 private boolean mBeyondCursor;
305 private String mString;
Satish Sampath565505b2009-05-29 15:37:27 +0100306 private int mSuggestText1Id;
307 private int mSuggestText2Id;
308 private int mSuggestQueryId;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800309
310 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
311 mHistoryCursor = hc;
312 mSuggestCursor = sc;
313 mHistoryCount = hc.getCount();
314 mSuggestionCount = sc != null ? sc.getCount() : 0;
315 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
316 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
317 }
318 mString = string;
319 mBeyondCursor = false;
Satish Sampath565505b2009-05-29 15:37:27 +0100320
321 // Some web suggest providers only give suggestions and have no description string for
322 // items. The order of the result columns may be different as well. So retrieve the
323 // column indices for the fields we need now and check before using below.
324 if (mSuggestCursor == null) {
325 mSuggestText1Id = -1;
326 mSuggestText2Id = -1;
327 mSuggestQueryId = -1;
328 } else {
329 mSuggestText1Id = mSuggestCursor.getColumnIndex(
330 SearchManager.SUGGEST_COLUMN_TEXT_1);
331 mSuggestText2Id = mSuggestCursor.getColumnIndex(
332 SearchManager.SUGGEST_COLUMN_TEXT_2);
333 mSuggestQueryId = mSuggestCursor.getColumnIndex(
334 SearchManager.SUGGEST_COLUMN_QUERY);
335 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800336 }
337
338 @Override
339 public boolean onMove(int oldPosition, int newPosition) {
340 if (mHistoryCursor == null) {
341 return false;
342 }
343 if (mHistoryCount > newPosition) {
344 mHistoryCursor.moveToPosition(newPosition);
345 mBeyondCursor = false;
346 } else if (mHistoryCount + mSuggestionCount > newPosition) {
347 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
348 mBeyondCursor = false;
349 } else {
350 mBeyondCursor = true;
351 }
352 return true;
353 }
354
355 @Override
356 public int getCount() {
357 if (mString.length() > 0) {
358 return mHistoryCount + mSuggestionCount + 1;
359 } else {
360 return mHistoryCount + mSuggestionCount;
361 }
362 }
363
364 @Override
365 public String[] getColumnNames() {
366 return COLUMNS;
367 }
Satish Sampath565505b2009-05-29 15:37:27 +0100368
The Android Open Source Project0c908882009-03-03 19:32:16 -0800369 @Override
370 public String getString(int columnIndex) {
371 if ((mPos != -1 && mHistoryCursor != null)) {
372 switch(columnIndex) {
373 case SUGGEST_COLUMN_INTENT_ACTION_ID:
374 if (mHistoryCount > mPos) {
375 return Intent.ACTION_VIEW;
376 } else {
377 return Intent.ACTION_SEARCH;
378 }
379
380 case SUGGEST_COLUMN_INTENT_DATA_ID:
381 if (mHistoryCount > mPos) {
382 return mHistoryCursor.getString(1);
383 } else {
384 return null;
385 }
386
387 case SUGGEST_COLUMN_TEXT_1_ID:
388 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700389 return getHistoryTitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800390 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100391 if (mSuggestText1Id == -1) return null;
392 return mSuggestCursor.getString(mSuggestText1Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800393 } else {
394 return mString;
395 }
396
397 case SUGGEST_COLUMN_TEXT_2_ID:
398 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700399 return getHistorySubtitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800400 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100401 if (mSuggestText2Id == -1) return null;
402 return mSuggestCursor.getString(mSuggestText2Id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800403 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700404 return getContext().getString(R.string.search_the_web);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800405 }
406
407 case SUGGEST_COLUMN_ICON_1_ID:
408 if (mHistoryCount > mPos) {
409 if (mHistoryCursor.getInt(3) == 1) {
410 return new Integer(
411 R.drawable.ic_search_category_bookmark)
412 .toString();
413 } else {
414 return new Integer(
415 R.drawable.ic_search_category_history)
416 .toString();
417 }
418 } else {
419 return new Integer(
420 R.drawable.ic_search_category_suggest)
421 .toString();
422 }
423
424 case SUGGEST_COLUMN_ICON_2_ID:
425 return new String("0");
426
427 case SUGGEST_COLUMN_QUERY_ID:
428 if (mHistoryCount > mPos) {
429 return null;
430 } else if (!mBeyondCursor) {
Satish Sampath565505b2009-05-29 15:37:27 +0100431 if (mSuggestQueryId == -1) return null;
432 return mSuggestCursor.getString(mSuggestQueryId);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800433 } else {
434 return mString;
435 }
Satish Sampath565505b2009-05-29 15:37:27 +0100436
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700437 case SUGGEST_COLUMN_FORMAT:
438 return "html";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800439 }
440 }
441 return null;
442 }
443
444 @Override
445 public double getDouble(int column) {
446 throw new UnsupportedOperationException();
447 }
448
449 @Override
450 public float getFloat(int column) {
451 throw new UnsupportedOperationException();
452 }
453
454 @Override
455 public int getInt(int column) {
456 throw new UnsupportedOperationException();
457 }
458
459 @Override
460 public long getLong(int column) {
461 if ((mPos != -1) && column == 0) {
462 return mPos; // use row# as the _Id
463 }
464 throw new UnsupportedOperationException();
465 }
466
467 @Override
468 public short getShort(int column) {
469 throw new UnsupportedOperationException();
470 }
471
472 @Override
473 public boolean isNull(int column) {
474 throw new UnsupportedOperationException();
475 }
476
477 // TODO Temporary change, finalize after jq's changes go in
478 public void deactivate() {
479 if (mHistoryCursor != null) {
480 mHistoryCursor.deactivate();
481 }
482 if (mSuggestCursor != null) {
483 mSuggestCursor.deactivate();
484 }
485 super.deactivate();
486 }
487
488 public boolean requery() {
489 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
490 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
491 }
492
493 // TODO Temporary change, finalize after jq's changes go in
494 public void close() {
495 super.close();
496 if (mHistoryCursor != null) {
497 mHistoryCursor.close();
498 mHistoryCursor = null;
499 }
500 if (mSuggestCursor != null) {
501 mSuggestCursor.close();
502 mSuggestCursor = null;
503 }
504 }
Satish Sampath565505b2009-05-29 15:37:27 +0100505
Mike LeBeau21beb132009-05-13 14:57:50 -0700506 /**
507 * Provides the title (text line 1) for a browser suggestion, which should be the
508 * webpage title. If the webpage title is empty, returns the stripped url instead.
Satish Sampath565505b2009-05-29 15:37:27 +0100509 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700510 * @return the title string to use
511 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700512 private String getHistoryTitle() {
513 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700514 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700515 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700516 }
517 return title;
518 }
Satish Sampath565505b2009-05-29 15:37:27 +0100519
Mike LeBeau21beb132009-05-13 14:57:50 -0700520 /**
521 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
522 * webpage url. If the webpage title is empty, then the url should go in the title
523 * instead, and the subtitle should be empty, so this would return null.
Satish Sampath565505b2009-05-29 15:37:27 +0100524 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700525 * @return the subtitle string to use, or null if none
526 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700527 private String getHistorySubtitle() {
528 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700529 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
530 return null;
531 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700532 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700533 }
534 }
Satish Sampath565505b2009-05-29 15:37:27 +0100535
Mike LeBeau21beb132009-05-13 14:57:50 -0700536 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700537 * Strips "http://" from the beginning of a url and "/" from the end,
538 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700539 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700540 private String beautifyUrl(String url) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700541 if (mSearchUrlColorHex == null) {
542 // Get the color used for this purpose from the current theme.
543 TypedValue colorValue = new TypedValue();
544 getContext().getTheme().resolveAttribute(
545 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
546 int color = getContext().getResources().getColor(colorValue.resourceId);
Satish Sampath565505b2009-05-29 15:37:27 +0100547
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700548 // Convert the int color value into a hex string, and strip the first two
549 // characters which will be the alpha transparency (html doesn't want this).
550 mSearchUrlColorHex = Integer.toHexString(color).substring(2);
Mike LeBeau21beb132009-05-13 14:57:50 -0700551 }
Satish Sampath565505b2009-05-29 15:37:27 +0100552
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700553 return "<font color=\"#" + mSearchUrlColorHex + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700554 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800555 }
556
557 @Override
558 public Cursor query(Uri url, String[] projectionIn, String selection,
Satish Sampath565505b2009-05-29 15:37:27 +0100559 String[] selectionArgs, String sortOrder)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800560 throws IllegalStateException {
561 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
562
563 int match = URI_MATCHER.match(url);
564 if (match == -1) {
565 throw new IllegalArgumentException("Unknown URL");
566 }
567
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100568 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800569 String suggestSelection;
570 String [] myArgs;
571 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
572 suggestSelection = null;
573 myArgs = null;
574 } else {
575 String like = selectionArgs[0] + "%";
576 if (selectionArgs[0].startsWith("http")) {
577 myArgs = new String[1];
578 myArgs[0] = like;
579 suggestSelection = selection;
580 } else {
581 SUGGEST_ARGS[0] = "http://" + like;
582 SUGGEST_ARGS[1] = "http://www." + like;
583 SUGGEST_ARGS[2] = "https://" + like;
584 SUGGEST_ARGS[3] = "https://www." + like;
585 myArgs = SUGGEST_ARGS;
586 suggestSelection = SUGGEST_SELECTION;
587 }
588 }
589
590 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
591 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
592 ORDER_BY,
593 (new Integer(MAX_SUGGESTION_LONG_ENTRIES)).toString());
594
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100595 if (match == URI_MATCH_BOOKMARKS_SUGGEST
596 || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800597 return new MySuggestionCursor(c, null, "");
598 } else {
599 // get Google suggest if there is still space in the list
600 if (myArgs != null && myArgs.length > 1
601 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Satish Sampath565505b2009-05-29 15:37:27 +0100602 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
603 intent.addCategory(Intent.CATEGORY_DEFAULT);
604 ResolveInfo info = getContext().getPackageManager().resolveActivity(
605 intent, PackageManager.MATCH_DEFAULT_ONLY);
606 if (info != null) {
607 ComponentName googleSearchComponent =
608 new ComponentName(info.activityInfo.packageName,
609 info.activityInfo.name);
610 SearchableInfo si =
611 SearchManager.getSearchableInfo(googleSearchComponent, false);
612 Cursor sc = SearchManager.getSuggestions(
613 getContext(), si, selectionArgs[0]);
614 return new MySuggestionCursor(c, sc, selectionArgs[0]);
615 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800616 }
617 return new MySuggestionCursor(c, null, selectionArgs[0]);
618 }
619 }
620
621 String[] projection = null;
622 if (projectionIn != null && projectionIn.length > 0) {
623 projection = new String[projectionIn.length + 1];
624 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
625 projection[projectionIn.length] = "_id AS _id";
626 }
627
628 StringBuilder whereClause = new StringBuilder(256);
629 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
630 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
631 .append(")");
632 }
633
634 // Tack on the user's selection, if present
635 if (selection != null && selection.length() > 0) {
636 if (whereClause.length() > 0) {
637 whereClause.append(" AND ");
638 }
639
640 whereClause.append('(');
641 whereClause.append(selection);
642 whereClause.append(')');
643 }
644 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
645 whereClause.toString(), selectionArgs, null, null, sortOrder,
646 null);
647 c.setNotificationUri(getContext().getContentResolver(), url);
648 return c;
649 }
650
651 @Override
652 public String getType(Uri url) {
653 int match = URI_MATCHER.match(url);
654 switch (match) {
655 case URI_MATCH_BOOKMARKS:
656 return "vnd.android.cursor.dir/bookmark";
657
658 case URI_MATCH_BOOKMARKS_ID:
659 return "vnd.android.cursor.item/bookmark";
660
661 case URI_MATCH_SEARCHES:
662 return "vnd.android.cursor.dir/searches";
663
664 case URI_MATCH_SEARCHES_ID:
665 return "vnd.android.cursor.item/searches";
666
667 case URI_MATCH_SUGGEST:
668 return SearchManager.SUGGEST_MIME_TYPE;
669
670 default:
671 throw new IllegalArgumentException("Unknown URL");
672 }
673 }
674
675 @Override
676 public Uri insert(Uri url, ContentValues initialValues) {
677 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
678
679 int match = URI_MATCHER.match(url);
680 Uri uri = null;
681 switch (match) {
682 case URI_MATCH_BOOKMARKS: {
683 // Insert into the bookmarks table
684 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
685 initialValues);
686 if (rowID > 0) {
687 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
688 rowID);
689 }
690 break;
691 }
692
693 case URI_MATCH_SEARCHES: {
694 // Insert into the searches table
695 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
696 initialValues);
697 if (rowID > 0) {
698 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
699 rowID);
700 }
701 break;
702 }
703
704 default:
705 throw new IllegalArgumentException("Unknown URL");
706 }
707
708 if (uri == null) {
709 throw new IllegalArgumentException("Unknown URL");
710 }
711 getContext().getContentResolver().notifyChange(uri, null);
712 return uri;
713 }
714
715 @Override
716 public int delete(Uri url, String where, String[] whereArgs) {
717 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
718
719 int match = URI_MATCHER.match(url);
720 if (match == -1 || match == URI_MATCH_SUGGEST) {
721 throw new IllegalArgumentException("Unknown URL");
722 }
723
724 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
725 StringBuilder sb = new StringBuilder();
726 if (where != null && where.length() > 0) {
727 sb.append("( ");
728 sb.append(where);
729 sb.append(" ) AND ");
730 }
731 sb.append("_id = ");
732 sb.append(url.getPathSegments().get(1));
733 where = sb.toString();
734 }
735
736 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
737 getContext().getContentResolver().notifyChange(url, null);
738 return count;
739 }
740
741 @Override
742 public int update(Uri url, ContentValues values, String where,
743 String[] whereArgs) {
744 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
745
746 int match = URI_MATCHER.match(url);
747 if (match == -1 || match == URI_MATCH_SUGGEST) {
748 throw new IllegalArgumentException("Unknown URL");
749 }
750
751 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
752 StringBuilder sb = new StringBuilder();
753 if (where != null && where.length() > 0) {
754 sb.append("( ");
755 sb.append(where);
756 sb.append(" ) AND ");
757 }
758 sb.append("_id = ");
759 sb.append(url.getPathSegments().get(1));
760 where = sb.toString();
761 }
762
763 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
764 getContext().getContentResolver().notifyChange(url, null);
765 return ret;
766 }
Satish Sampath565505b2009-05-29 15:37:27 +0100767
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700768 /**
769 * Strips the provided url of preceding "http://" and any trailing "/". Does not
770 * strip "https://". If the provided string cannot be stripped, the original string
771 * is returned.
Satish Sampath565505b2009-05-29 15:37:27 +0100772 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700773 * TODO: Put this in TextUtils to be used by other packages doing something similar.
Satish Sampath565505b2009-05-29 15:37:27 +0100774 *
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700775 * @param url a url to strip, like "http://www.google.com/"
776 * @return a stripped url like "www.google.com", or the original string if it could
777 * not be stripped
778 */
779 private static String stripUrl(String url) {
780 if (url == null) return null;
781 Matcher m = STRIP_URL_PATTERN.matcher(url);
782 if (m.matches() && m.groupCount() == 3) {
783 return m.group(2);
784 } else {
785 return url;
786 }
787 }
788
The Android Open Source Project0c908882009-03-03 19:32:16 -0800789}