blob: aa8df3133af55dd4f5218a1099941b4c4c23c865 [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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.database.AbstractCursor;
32import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080033import android.database.sqlite.SQLiteDatabase;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010034import android.database.sqlite.SQLiteOpenHelper;
The Android Open Source Project0c908882009-03-03 19:32:16 -080035import android.net.Uri;
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070036import android.preference.PreferenceManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080037import android.provider.Browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080038import android.server.search.SearchableInfo;
Mike LeBeau21beb132009-05-13 14:57:50 -070039import android.text.TextUtils;
The Android Open Source Project0c908882009-03-03 19:32:16 -080040import android.text.util.Regex;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010041import android.util.Log;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070042import android.util.TypedValue;
Bjorn Bringertbcd20b32009-04-29 21:52:09 +010043
44import java.util.Date;
Mike LeBeauc42f81b2009-05-14 15:04:19 -070045import java.util.regex.Matcher;
46import java.util.regex.Pattern;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -070048
The Android Open Source Project0c908882009-03-03 19:32:16 -080049public class BrowserProvider extends ContentProvider {
50
51 private SQLiteOpenHelper mOpenHelper;
52 private static final String sDatabaseName = "browser.db";
53 private static final String TAG = "BrowserProvider";
54 private static final String ORDER_BY = "visits DESC, date DESC";
55
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -070056 private static final String PICASA_URL = "http://picasaweb.google.com/m/" +
57 "viewer?source=androidclient";
58
The Android Open Source Project0c908882009-03-03 19:32:16 -080059 private static final String[] TABLE_NAMES = new String[] {
60 "bookmarks", "searches"
61 };
62 private static final String[] SUGGEST_PROJECTION = new String[] {
63 "_id", "url", "title", "bookmark"
64 };
65 private static final String SUGGEST_SELECTION =
66 "url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?";
67 private String[] SUGGEST_ARGS = new String[4];
68
69 // shared suggestion array index, make sure to match COLUMNS
70 private static final int SUGGEST_COLUMN_INTENT_ACTION_ID = 1;
71 private static final int SUGGEST_COLUMN_INTENT_DATA_ID = 2;
72 private static final int SUGGEST_COLUMN_TEXT_1_ID = 3;
73 private static final int SUGGEST_COLUMN_TEXT_2_ID = 4;
74 private static final int SUGGEST_COLUMN_ICON_1_ID = 5;
75 private static final int SUGGEST_COLUMN_ICON_2_ID = 6;
76 private static final int SUGGEST_COLUMN_QUERY_ID = 7;
Mike LeBeau1ef26a32009-05-13 20:11:00 -070077 private static final int SUGGEST_COLUMN_FORMAT = 8;
The Android Open Source Project0c908882009-03-03 19:32:16 -080078
79 // shared suggestion columns
80 private static final String[] COLUMNS = new String[] {
81 "_id",
82 SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
83 SearchManager.SUGGEST_COLUMN_INTENT_DATA,
84 SearchManager.SUGGEST_COLUMN_TEXT_1,
85 SearchManager.SUGGEST_COLUMN_TEXT_2,
86 SearchManager.SUGGEST_COLUMN_ICON_1,
87 SearchManager.SUGGEST_COLUMN_ICON_2,
Mike LeBeau1ef26a32009-05-13 20:11:00 -070088 SearchManager.SUGGEST_COLUMN_QUERY,
89 SearchManager.SUGGEST_COLUMN_FORMAT};
The Android Open Source Project0c908882009-03-03 19:32:16 -080090
91 private static final int MAX_SUGGESTION_SHORT_ENTRIES = 3;
92 private static final int MAX_SUGGESTION_LONG_ENTRIES = 6;
93
94 // make sure that these match the index of TABLE_NAMES
95 private static final int URI_MATCH_BOOKMARKS = 0;
96 private static final int URI_MATCH_SEARCHES = 1;
97 // (id % 10) should match the table name index
98 private static final int URI_MATCH_BOOKMARKS_ID = 10;
99 private static final int URI_MATCH_SEARCHES_ID = 11;
100 //
101 private static final int URI_MATCH_SUGGEST = 20;
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100102 private static final int URI_MATCH_BOOKMARKS_SUGGEST = 21;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800103
104 private static final UriMatcher URI_MATCHER;
105
106 static {
107 URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
108 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS],
109 URI_MATCH_BOOKMARKS);
110 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/#",
111 URI_MATCH_BOOKMARKS_ID);
112 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES],
113 URI_MATCH_SEARCHES);
114 URI_MATCHER.addURI("browser", TABLE_NAMES[URI_MATCH_SEARCHES] + "/#",
115 URI_MATCH_SEARCHES_ID);
116 URI_MATCHER.addURI("browser", SearchManager.SUGGEST_URI_PATH_QUERY,
117 URI_MATCH_SUGGEST);
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100118 URI_MATCHER.addURI("browser",
119 TABLE_NAMES[URI_MATCH_BOOKMARKS] + "/" + SearchManager.SUGGEST_URI_PATH_QUERY,
120 URI_MATCH_BOOKMARKS_SUGGEST);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121 }
122
123 // 1 -> 2 add cache table
124 // 2 -> 3 update history table
125 // 3 -> 4 add passwords table
126 // 4 -> 5 add settings table
127 // 5 -> 6 ?
128 // 6 -> 7 ?
129 // 7 -> 8 drop proxy table
130 // 8 -> 9 drop settings table
131 // 9 -> 10 add form_urls and form_data
132 // 10 -> 11 add searches table
133 // 11 -> 12 modify cache table
134 // 12 -> 13 modify cache table
135 // 13 -> 14 correspond with Google Bookmarks schema
136 // 14 -> 15 move couple of tables to either browser private database or webview database
137 // 15 -> 17 Set it up for the SearchManager
138 // 17 -> 18 Added favicon in bookmarks table for Home shortcuts
139 // 18 -> 19 Remove labels table
140 private static final int DATABASE_VERSION = 19;
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700141
142 // Regular expression which matches http://, followed by some stuff, followed by
143 // optionally a trailing slash, all matched as separate groups.
144 private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
145
146 // The hex color string to be applied to urls of website suggestions, as derived from
147 // the current theme. This is not set until/unless beautifyUrl is called, at which point
148 // this variable caches the color value.
149 private static String mSearchUrlColorHex;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800150
151 public BrowserProvider() {
152 }
153
154
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700155 private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800156 StringBuffer sb = new StringBuffer();
157 int lastCharLoc = 0;
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700158
159 final String client_id = Partner.getString(context.getContentResolver(), Partner.CLIENT_ID);
160
The Android Open Source Project0c908882009-03-03 19:32:16 -0800161 for (int i = 0; i < srcString.length(); ++i) {
162 char c = srcString.charAt(i);
163 if (c == '{') {
164 sb.append(srcString.subSequence(lastCharLoc, i));
165 lastCharLoc = i;
166 inner:
167 for (int j = i; j < srcString.length(); ++j) {
168 char k = srcString.charAt(j);
169 if (k == '}') {
170 String propertyKeyValue = srcString.subSequence(i + 1, j).toString();
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700171 if (propertyKeyValue.equals("CLIENT_ID")) {
172 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800173 } else {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700174 sb.append("unknown");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800175 }
176 lastCharLoc = j + 1;
177 i = j;
178 break inner;
179 }
180 }
181 }
182 }
183 if (srcString.length() - lastCharLoc > 0) {
184 // Put on the tail, if there is one
185 sb.append(srcString.subSequence(lastCharLoc, srcString.length()));
186 }
187 return sb;
188 }
189
190 private static class DatabaseHelper extends SQLiteOpenHelper {
191 private Context mContext;
192
193 public DatabaseHelper(Context context) {
194 super(context, sDatabaseName, null, DATABASE_VERSION);
195 mContext = context;
196 }
197
198 @Override
199 public void onCreate(SQLiteDatabase db) {
200 db.execSQL("CREATE TABLE bookmarks (" +
201 "_id INTEGER PRIMARY KEY," +
202 "title TEXT," +
203 "url TEXT," +
204 "visits INTEGER," +
205 "date LONG," +
206 "created LONG," +
207 "description TEXT," +
208 "bookmark INTEGER," +
209 "favicon BLOB DEFAULT NULL" +
210 ");");
211
212 final CharSequence[] bookmarks = mContext.getResources()
213 .getTextArray(R.array.bookmarks);
214 int size = bookmarks.length;
215 try {
216 for (int i = 0; i < size; i = i + 2) {
Ramanan Rajeswarandd4f4292009-03-24 20:41:19 -0700217 CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800218 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
219 "date, created, bookmark)" + " VALUES('" +
220 bookmarks[i] + "', '" + bookmarkDestination +
221 "', 0, 0, 0, 1);");
222 }
223 } catch (ArrayIndexOutOfBoundsException e) {
224 }
225
226 db.execSQL("CREATE TABLE searches (" +
227 "_id INTEGER PRIMARY KEY," +
228 "search TEXT," +
229 "date LONG" +
230 ");");
231 }
232
233 @Override
234 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
235 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
236 + newVersion + ", which will destroy all old data");
237 if (oldVersion == 18) {
238 db.execSQL("DROP TABLE IF EXISTS labels");
239 } else {
240 db.execSQL("DROP TABLE IF EXISTS bookmarks");
241 db.execSQL("DROP TABLE IF EXISTS searches");
242 onCreate(db);
243 }
244 }
245 }
246
247 @Override
248 public boolean onCreate() {
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700249 final Context context = getContext();
250 mOpenHelper = new DatabaseHelper(context);
251 // we added "picasa web album" into default bookmarks for version 19.
252 // To avoid erasing the bookmark table, we added it explicitly for
253 // version 18 and 19 as in the other cases, we will erase the table.
254 if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
255 SharedPreferences p = PreferenceManager
256 .getDefaultSharedPreferences(context);
257 boolean fix = p.getBoolean("fix_picasa", true);
258 if (fix) {
259 fixPicasaBookmark();
260 Editor ed = p.edit();
261 ed.putBoolean("fix_picasa", false);
262 ed.commit();
263 }
264 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800265 return true;
266 }
267
The Android Open Source Projecta3c0aab2009-03-18 17:39:48 -0700268 private void fixPicasaBookmark() {
269 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
270 Cursor cursor = db.rawQuery("SELECT _id FROM bookmarks WHERE " +
271 "bookmark = 1 AND url = ?", new String[] { PICASA_URL });
272 try {
273 if (!cursor.moveToFirst()) {
274 // set "created" so that it will be on the top of the list
275 db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
276 "date, created, bookmark)" + " VALUES('" +
277 getContext().getString(R.string.picasa) + "', '"
278 + PICASA_URL + "', 0, 0, " + new Date().getTime()
279 + ", 1);");
280 }
281 } finally {
282 if (cursor != null) {
283 cursor.close();
284 }
285 }
286 }
287
The Android Open Source Project0c908882009-03-03 19:32:16 -0800288 /*
289 * Subclass AbstractCursor so we can combine multiple Cursors and add
290 * "Google Search".
291 * Here are the rules.
292 * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
293 * "Google Search";
294 * 2. If bookmark/history entries are less than
295 * (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
296 */
297 private class MySuggestionCursor extends AbstractCursor {
298 private Cursor mHistoryCursor;
299 private Cursor mSuggestCursor;
300 private int mHistoryCount;
301 private int mSuggestionCount;
302 private boolean mBeyondCursor;
303 private String mString;
304
305 public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
306 mHistoryCursor = hc;
307 mSuggestCursor = sc;
308 mHistoryCount = hc.getCount();
309 mSuggestionCount = sc != null ? sc.getCount() : 0;
310 if (mSuggestionCount > (MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount)) {
311 mSuggestionCount = MAX_SUGGESTION_LONG_ENTRIES - mHistoryCount;
312 }
313 mString = string;
314 mBeyondCursor = false;
315 }
316
317 @Override
318 public boolean onMove(int oldPosition, int newPosition) {
319 if (mHistoryCursor == null) {
320 return false;
321 }
322 if (mHistoryCount > newPosition) {
323 mHistoryCursor.moveToPosition(newPosition);
324 mBeyondCursor = false;
325 } else if (mHistoryCount + mSuggestionCount > newPosition) {
326 mSuggestCursor.moveToPosition(newPosition - mHistoryCount);
327 mBeyondCursor = false;
328 } else {
329 mBeyondCursor = true;
330 }
331 return true;
332 }
333
334 @Override
335 public int getCount() {
336 if (mString.length() > 0) {
337 return mHistoryCount + mSuggestionCount + 1;
338 } else {
339 return mHistoryCount + mSuggestionCount;
340 }
341 }
342
343 @Override
344 public String[] getColumnNames() {
345 return COLUMNS;
346 }
Mike LeBeau21beb132009-05-13 14:57:50 -0700347
The Android Open Source Project0c908882009-03-03 19:32:16 -0800348 @Override
349 public String getString(int columnIndex) {
350 if ((mPos != -1 && mHistoryCursor != null)) {
351 switch(columnIndex) {
352 case SUGGEST_COLUMN_INTENT_ACTION_ID:
353 if (mHistoryCount > mPos) {
354 return Intent.ACTION_VIEW;
355 } else {
356 return Intent.ACTION_SEARCH;
357 }
358
359 case SUGGEST_COLUMN_INTENT_DATA_ID:
360 if (mHistoryCount > mPos) {
361 return mHistoryCursor.getString(1);
362 } else {
363 return null;
364 }
365
366 case SUGGEST_COLUMN_TEXT_1_ID:
367 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700368 return getHistoryTitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800369 } else if (!mBeyondCursor) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700370 return mSuggestCursor.getString(1);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800371 } else {
372 return mString;
373 }
374
375 case SUGGEST_COLUMN_TEXT_2_ID:
376 if (mHistoryCount > mPos) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700377 return getHistorySubtitle();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800378 } else if (!mBeyondCursor) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700379 return mSuggestCursor.getString(2);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800380 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700381 return getContext().getString(R.string.search_the_web);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800382 }
383
384 case SUGGEST_COLUMN_ICON_1_ID:
385 if (mHistoryCount > mPos) {
386 if (mHistoryCursor.getInt(3) == 1) {
387 return new Integer(
388 R.drawable.ic_search_category_bookmark)
389 .toString();
390 } else {
391 return new Integer(
392 R.drawable.ic_search_category_history)
393 .toString();
394 }
395 } else {
396 return new Integer(
397 R.drawable.ic_search_category_suggest)
398 .toString();
399 }
400
401 case SUGGEST_COLUMN_ICON_2_ID:
402 return new String("0");
403
404 case SUGGEST_COLUMN_QUERY_ID:
405 if (mHistoryCount > mPos) {
406 return null;
407 } else if (!mBeyondCursor) {
408 return mSuggestCursor.getString(3);
409 } else {
410 return mString;
411 }
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700412
413 case SUGGEST_COLUMN_FORMAT:
414 return "html";
The Android Open Source Project0c908882009-03-03 19:32:16 -0800415 }
416 }
417 return null;
418 }
419
420 @Override
421 public double getDouble(int column) {
422 throw new UnsupportedOperationException();
423 }
424
425 @Override
426 public float getFloat(int column) {
427 throw new UnsupportedOperationException();
428 }
429
430 @Override
431 public int getInt(int column) {
432 throw new UnsupportedOperationException();
433 }
434
435 @Override
436 public long getLong(int column) {
437 if ((mPos != -1) && column == 0) {
438 return mPos; // use row# as the _Id
439 }
440 throw new UnsupportedOperationException();
441 }
442
443 @Override
444 public short getShort(int column) {
445 throw new UnsupportedOperationException();
446 }
447
448 @Override
449 public boolean isNull(int column) {
450 throw new UnsupportedOperationException();
451 }
452
453 // TODO Temporary change, finalize after jq's changes go in
454 public void deactivate() {
455 if (mHistoryCursor != null) {
456 mHistoryCursor.deactivate();
457 }
458 if (mSuggestCursor != null) {
459 mSuggestCursor.deactivate();
460 }
461 super.deactivate();
462 }
463
464 public boolean requery() {
465 return (mHistoryCursor != null ? mHistoryCursor.requery() : false) |
466 (mSuggestCursor != null ? mSuggestCursor.requery() : false);
467 }
468
469 // TODO Temporary change, finalize after jq's changes go in
470 public void close() {
471 super.close();
472 if (mHistoryCursor != null) {
473 mHistoryCursor.close();
474 mHistoryCursor = null;
475 }
476 if (mSuggestCursor != null) {
477 mSuggestCursor.close();
478 mSuggestCursor = null;
479 }
480 }
Mike LeBeau21beb132009-05-13 14:57:50 -0700481
482 /**
483 * Provides the title (text line 1) for a browser suggestion, which should be the
484 * webpage title. If the webpage title is empty, returns the stripped url instead.
485 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700486 * @return the title string to use
487 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700488 private String getHistoryTitle() {
489 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700490 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700491 title = beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700492 }
493 return title;
494 }
495
496 /**
497 * Provides the subtitle (text line 2) for a browser suggestion, which should be the
498 * webpage url. If the webpage title is empty, then the url should go in the title
499 * instead, and the subtitle should be empty, so this would return null.
500 *
Mike LeBeau21beb132009-05-13 14:57:50 -0700501 * @return the subtitle string to use, or null if none
502 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700503 private String getHistorySubtitle() {
504 String title = mHistoryCursor.getString(2 /* webpage title */);
Mike LeBeau21beb132009-05-13 14:57:50 -0700505 if (TextUtils.isEmpty(title) || TextUtils.getTrimmedLength(title) == 0) {
506 return null;
507 } else {
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700508 return beautifyUrl(mHistoryCursor.getString(1 /* url */));
Mike LeBeau21beb132009-05-13 14:57:50 -0700509 }
510 }
511
512 /**
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700513 * Strips "http://" from the beginning of a url and "/" from the end,
514 * and adds html formatting to make it green.
Mike LeBeau21beb132009-05-13 14:57:50 -0700515 */
Mike LeBeau1ef26a32009-05-13 20:11:00 -0700516 private String beautifyUrl(String url) {
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700517 if (mSearchUrlColorHex == null) {
518 // Get the color used for this purpose from the current theme.
519 TypedValue colorValue = new TypedValue();
520 getContext().getTheme().resolveAttribute(
521 com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
522 int color = getContext().getResources().getColor(colorValue.resourceId);
523
524 // Convert the int color value into a hex string, and strip the first two
525 // characters which will be the alpha transparency (html doesn't want this).
526 mSearchUrlColorHex = Integer.toHexString(color).substring(2);
Mike LeBeau21beb132009-05-13 14:57:50 -0700527 }
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700528
529 return "<font color=\"#" + mSearchUrlColorHex + "\">" + stripUrl(url) + "</font>";
Mike LeBeau21beb132009-05-13 14:57:50 -0700530 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800531 }
532
533 @Override
534 public Cursor query(Uri url, String[] projectionIn, String selection,
535 String[] selectionArgs, String sortOrder)
536 throws IllegalStateException {
537 SQLiteDatabase db = mOpenHelper.getReadableDatabase();
538
539 int match = URI_MATCHER.match(url);
540 if (match == -1) {
541 throw new IllegalArgumentException("Unknown URL");
542 }
543
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100544 if (match == URI_MATCH_SUGGEST || match == URI_MATCH_BOOKMARKS_SUGGEST) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800545 String suggestSelection;
546 String [] myArgs;
547 if (selectionArgs[0] == null || selectionArgs[0].equals("")) {
548 suggestSelection = null;
549 myArgs = null;
550 } else {
551 String like = selectionArgs[0] + "%";
Leon Scrogginsfaa15db2009-04-03 10:16:06 -0700552 if (selectionArgs[0].startsWith("http")
553 || selectionArgs[0].startsWith("file")) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800554 myArgs = new String[1];
555 myArgs[0] = like;
556 suggestSelection = selection;
557 } else {
558 SUGGEST_ARGS[0] = "http://" + like;
559 SUGGEST_ARGS[1] = "http://www." + like;
560 SUGGEST_ARGS[2] = "https://" + like;
561 SUGGEST_ARGS[3] = "https://www." + like;
562 myArgs = SUGGEST_ARGS;
563 suggestSelection = SUGGEST_SELECTION;
564 }
565 }
566
567 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
568 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
569 ORDER_BY,
570 (new Integer(MAX_SUGGESTION_LONG_ENTRIES)).toString());
571
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100572 if (match == URI_MATCH_BOOKMARKS_SUGGEST
573 || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800574 return new MySuggestionCursor(c, null, "");
575 } else {
576 // get Google suggest if there is still space in the list
577 if (myArgs != null && myArgs.length > 1
578 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringert24e1ec62009-04-29 16:10:43 +0100579 // TODO: This shouldn't be hard-coded. Instead, it should use the
580 // default web search provider. But the API for that is not implemented yet.
581 ComponentName googleSearchComponent =
582 new ComponentName("com.android.googlesearch",
583 "com.android.googlesearch.GoogleSearch");
584 SearchableInfo si =
585 SearchManager.getSearchableInfo(googleSearchComponent, false);
586 Cursor sc = SearchManager.getSuggestions(getContext(), si, selectionArgs[0]);
587 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800588 }
589 return new MySuggestionCursor(c, null, selectionArgs[0]);
590 }
591 }
592
593 String[] projection = null;
594 if (projectionIn != null && projectionIn.length > 0) {
595 projection = new String[projectionIn.length + 1];
596 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
597 projection[projectionIn.length] = "_id AS _id";
598 }
599
600 StringBuilder whereClause = new StringBuilder(256);
601 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
602 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
603 .append(")");
604 }
605
606 // Tack on the user's selection, if present
607 if (selection != null && selection.length() > 0) {
608 if (whereClause.length() > 0) {
609 whereClause.append(" AND ");
610 }
611
612 whereClause.append('(');
613 whereClause.append(selection);
614 whereClause.append(')');
615 }
616 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
617 whereClause.toString(), selectionArgs, null, null, sortOrder,
618 null);
619 c.setNotificationUri(getContext().getContentResolver(), url);
620 return c;
621 }
622
623 @Override
624 public String getType(Uri url) {
625 int match = URI_MATCHER.match(url);
626 switch (match) {
627 case URI_MATCH_BOOKMARKS:
628 return "vnd.android.cursor.dir/bookmark";
629
630 case URI_MATCH_BOOKMARKS_ID:
631 return "vnd.android.cursor.item/bookmark";
632
633 case URI_MATCH_SEARCHES:
634 return "vnd.android.cursor.dir/searches";
635
636 case URI_MATCH_SEARCHES_ID:
637 return "vnd.android.cursor.item/searches";
638
639 case URI_MATCH_SUGGEST:
640 return SearchManager.SUGGEST_MIME_TYPE;
641
642 default:
643 throw new IllegalArgumentException("Unknown URL");
644 }
645 }
646
647 @Override
648 public Uri insert(Uri url, ContentValues initialValues) {
649 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
650
651 int match = URI_MATCHER.match(url);
652 Uri uri = null;
653 switch (match) {
654 case URI_MATCH_BOOKMARKS: {
655 // Insert into the bookmarks table
656 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
657 initialValues);
658 if (rowID > 0) {
659 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
660 rowID);
661 }
662 break;
663 }
664
665 case URI_MATCH_SEARCHES: {
666 // Insert into the searches table
667 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
668 initialValues);
669 if (rowID > 0) {
670 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
671 rowID);
672 }
673 break;
674 }
675
676 default:
677 throw new IllegalArgumentException("Unknown URL");
678 }
679
680 if (uri == null) {
681 throw new IllegalArgumentException("Unknown URL");
682 }
683 getContext().getContentResolver().notifyChange(uri, null);
684 return uri;
685 }
686
687 @Override
688 public int delete(Uri url, String where, String[] whereArgs) {
689 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
690
691 int match = URI_MATCHER.match(url);
692 if (match == -1 || match == URI_MATCH_SUGGEST) {
693 throw new IllegalArgumentException("Unknown URL");
694 }
695
696 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
697 StringBuilder sb = new StringBuilder();
698 if (where != null && where.length() > 0) {
699 sb.append("( ");
700 sb.append(where);
701 sb.append(" ) AND ");
702 }
703 sb.append("_id = ");
704 sb.append(url.getPathSegments().get(1));
705 where = sb.toString();
706 }
707
708 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
709 getContext().getContentResolver().notifyChange(url, null);
710 return count;
711 }
712
713 @Override
714 public int update(Uri url, ContentValues values, String where,
715 String[] whereArgs) {
716 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
717
718 int match = URI_MATCHER.match(url);
719 if (match == -1 || match == URI_MATCH_SUGGEST) {
720 throw new IllegalArgumentException("Unknown URL");
721 }
722
723 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
724 StringBuilder sb = new StringBuilder();
725 if (where != null && where.length() > 0) {
726 sb.append("( ");
727 sb.append(where);
728 sb.append(" ) AND ");
729 }
730 sb.append("_id = ");
731 sb.append(url.getPathSegments().get(1));
732 where = sb.toString();
733 }
734
735 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
736 getContext().getContentResolver().notifyChange(url, null);
737 return ret;
738 }
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700739
740 /**
741 * Strips the provided url of preceding "http://" and any trailing "/". Does not
742 * strip "https://". If the provided string cannot be stripped, the original string
743 * is returned.
744 *
745 * TODO: Put this in TextUtils to be used by other packages doing something similar.
746 *
747 * @param url a url to strip, like "http://www.google.com/"
748 * @return a stripped url like "www.google.com", or the original string if it could
749 * not be stripped
750 */
751 private static String stripUrl(String url) {
752 if (url == null) return null;
753 Matcher m = STRIP_URL_PATTERN.matcher(url);
754 if (m.matches() && m.groupCount() == 3) {
755 return m.group(2);
756 } else {
757 return url;
758 }
759 }
760
The Android Open Source Project0c908882009-03-03 19:32:16 -0800761}