blob: f8051a4856f908f86a9010cb03416defd0bb95b4 [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;
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 Rajeswaranf447f262009-03-24 20:40:12 -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 Rajeswaranf447f262009-03-24 20:40:12 -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 Rajeswaranf447f262009-03-24 20:40:12 -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 Rajeswaranf447f262009-03-24 20:40:12 -0700171 if (propertyKeyValue.equals("CLIENT_ID")) {
172 sb.append(client_id);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800173 } else {
Ramanan Rajeswaranf447f262009-03-24 20:40:12 -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 Rajeswaranf447f262009-03-24 20:40:12 -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] + "%";
552 if (selectionArgs[0].startsWith("http")) {
553 myArgs = new String[1];
554 myArgs[0] = like;
555 suggestSelection = selection;
556 } else {
557 SUGGEST_ARGS[0] = "http://" + like;
558 SUGGEST_ARGS[1] = "http://www." + like;
559 SUGGEST_ARGS[2] = "https://" + like;
560 SUGGEST_ARGS[3] = "https://www." + like;
561 myArgs = SUGGEST_ARGS;
562 suggestSelection = SUGGEST_SELECTION;
563 }
564 }
565
566 Cursor c = db.query(TABLE_NAMES[URI_MATCH_BOOKMARKS],
567 SUGGEST_PROJECTION, suggestSelection, myArgs, null, null,
568 ORDER_BY,
569 (new Integer(MAX_SUGGESTION_LONG_ENTRIES)).toString());
570
Bjorn Bringert346dafb2009-04-29 21:41:47 +0100571 if (match == URI_MATCH_BOOKMARKS_SUGGEST
572 || Regex.WEB_URL_PATTERN.matcher(selectionArgs[0]).matches()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800573 return new MySuggestionCursor(c, null, "");
574 } else {
575 // get Google suggest if there is still space in the list
576 if (myArgs != null && myArgs.length > 1
577 && c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
Bjorn Bringert24e1ec62009-04-29 16:10:43 +0100578 // TODO: This shouldn't be hard-coded. Instead, it should use the
579 // default web search provider. But the API for that is not implemented yet.
580 ComponentName googleSearchComponent =
581 new ComponentName("com.android.googlesearch",
582 "com.android.googlesearch.GoogleSearch");
583 SearchableInfo si =
584 SearchManager.getSearchableInfo(googleSearchComponent, false);
585 Cursor sc = SearchManager.getSuggestions(getContext(), si, selectionArgs[0]);
586 return new MySuggestionCursor(c, sc, selectionArgs[0]);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800587 }
588 return new MySuggestionCursor(c, null, selectionArgs[0]);
589 }
590 }
591
592 String[] projection = null;
593 if (projectionIn != null && projectionIn.length > 0) {
594 projection = new String[projectionIn.length + 1];
595 System.arraycopy(projectionIn, 0, projection, 0, projectionIn.length);
596 projection[projectionIn.length] = "_id AS _id";
597 }
598
599 StringBuilder whereClause = new StringBuilder(256);
600 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
601 whereClause.append("(_id = ").append(url.getPathSegments().get(1))
602 .append(")");
603 }
604
605 // Tack on the user's selection, if present
606 if (selection != null && selection.length() > 0) {
607 if (whereClause.length() > 0) {
608 whereClause.append(" AND ");
609 }
610
611 whereClause.append('(');
612 whereClause.append(selection);
613 whereClause.append(')');
614 }
615 Cursor c = db.query(TABLE_NAMES[match % 10], projection,
616 whereClause.toString(), selectionArgs, null, null, sortOrder,
617 null);
618 c.setNotificationUri(getContext().getContentResolver(), url);
619 return c;
620 }
621
622 @Override
623 public String getType(Uri url) {
624 int match = URI_MATCHER.match(url);
625 switch (match) {
626 case URI_MATCH_BOOKMARKS:
627 return "vnd.android.cursor.dir/bookmark";
628
629 case URI_MATCH_BOOKMARKS_ID:
630 return "vnd.android.cursor.item/bookmark";
631
632 case URI_MATCH_SEARCHES:
633 return "vnd.android.cursor.dir/searches";
634
635 case URI_MATCH_SEARCHES_ID:
636 return "vnd.android.cursor.item/searches";
637
638 case URI_MATCH_SUGGEST:
639 return SearchManager.SUGGEST_MIME_TYPE;
640
641 default:
642 throw new IllegalArgumentException("Unknown URL");
643 }
644 }
645
646 @Override
647 public Uri insert(Uri url, ContentValues initialValues) {
648 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
649
650 int match = URI_MATCHER.match(url);
651 Uri uri = null;
652 switch (match) {
653 case URI_MATCH_BOOKMARKS: {
654 // Insert into the bookmarks table
655 long rowID = db.insert(TABLE_NAMES[URI_MATCH_BOOKMARKS], "url",
656 initialValues);
657 if (rowID > 0) {
658 uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
659 rowID);
660 }
661 break;
662 }
663
664 case URI_MATCH_SEARCHES: {
665 // Insert into the searches table
666 long rowID = db.insert(TABLE_NAMES[URI_MATCH_SEARCHES], "url",
667 initialValues);
668 if (rowID > 0) {
669 uri = ContentUris.withAppendedId(Browser.SEARCHES_URI,
670 rowID);
671 }
672 break;
673 }
674
675 default:
676 throw new IllegalArgumentException("Unknown URL");
677 }
678
679 if (uri == null) {
680 throw new IllegalArgumentException("Unknown URL");
681 }
682 getContext().getContentResolver().notifyChange(uri, null);
683 return uri;
684 }
685
686 @Override
687 public int delete(Uri url, String where, String[] whereArgs) {
688 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
689
690 int match = URI_MATCHER.match(url);
691 if (match == -1 || match == URI_MATCH_SUGGEST) {
692 throw new IllegalArgumentException("Unknown URL");
693 }
694
695 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
696 StringBuilder sb = new StringBuilder();
697 if (where != null && where.length() > 0) {
698 sb.append("( ");
699 sb.append(where);
700 sb.append(" ) AND ");
701 }
702 sb.append("_id = ");
703 sb.append(url.getPathSegments().get(1));
704 where = sb.toString();
705 }
706
707 int count = db.delete(TABLE_NAMES[match % 10], where, whereArgs);
708 getContext().getContentResolver().notifyChange(url, null);
709 return count;
710 }
711
712 @Override
713 public int update(Uri url, ContentValues values, String where,
714 String[] whereArgs) {
715 SQLiteDatabase db = mOpenHelper.getWritableDatabase();
716
717 int match = URI_MATCHER.match(url);
718 if (match == -1 || match == URI_MATCH_SUGGEST) {
719 throw new IllegalArgumentException("Unknown URL");
720 }
721
722 if (match == URI_MATCH_BOOKMARKS_ID || match == URI_MATCH_SEARCHES_ID) {
723 StringBuilder sb = new StringBuilder();
724 if (where != null && where.length() > 0) {
725 sb.append("( ");
726 sb.append(where);
727 sb.append(" ) AND ");
728 }
729 sb.append("_id = ");
730 sb.append(url.getPathSegments().get(1));
731 where = sb.toString();
732 }
733
734 int ret = db.update(TABLE_NAMES[match % 10], values, where, whereArgs);
735 getContext().getContentResolver().notifyChange(url, null);
736 return ret;
737 }
Mike LeBeauc42f81b2009-05-14 15:04:19 -0700738
739 /**
740 * Strips the provided url of preceding "http://" and any trailing "/". Does not
741 * strip "https://". If the provided string cannot be stripped, the original string
742 * is returned.
743 *
744 * TODO: Put this in TextUtils to be used by other packages doing something similar.
745 *
746 * @param url a url to strip, like "http://www.google.com/"
747 * @return a stripped url like "www.google.com", or the original string if it could
748 * not be stripped
749 */
750 private static String stripUrl(String url) {
751 if (url == null) return null;
752 Matcher m = STRIP_URL_PATTERN.matcher(url);
753 if (m.matches() && m.groupCount() == 3) {
754 return m.group(2);
755 } else {
756 return url;
757 }
758 }
759
The Android Open Source Project0c908882009-03-03 19:32:16 -0800760}