summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/BrowserProvider.java
diff options
context:
space:
mode:
authorSatish Sampath <satish@android.com>2009-05-29 15:37:27 +0100
committerSatish Sampath <satish@android.com>2009-06-03 17:08:31 +0100
commit565505b1a2c1d3099496af5910760087602e994a (patch)
tree45051646b0114ed243bec49b46a4379c332f69cd /src/com/android/browser/BrowserProvider.java
parent8dd422a6b60f82a950900fb12d3ba987edac3e86 (diff)
downloadpackages_apps_Browser-565505b1a2c1d3099496af5910760087602e994a.zip
packages_apps_Browser-565505b1a2c1d3099496af5910760087602e994a.tar.gz
packages_apps_Browser-565505b1a2c1d3099496af5910760087602e994a.tar.bz2
Use system default web search & suggestions providers.
- Web search was hard coded to Google and suggestions in the search dialog were hard coded to the GoogleSearch suggest provider package. Both now point to the system default web search/suggest provider which can include third party search engines. - I also removed the intent filter to handle action.WEB_SEARCH from the browser because it should no longer provide web search functionality for other apps, that feature is provided by the recently added WebSearchProvider package. Removing this intent filter also removes the browser from the list of web search providers in the system settings. - As part of this change I had to factor out the search shortcut code to a separate function, add a couple of new functions and modify the browser provider code to access cursor fields in a safe manner.
Diffstat (limited to 'src/com/android/browser/BrowserProvider.java')
-rw-r--r--src/com/android/browser/BrowserProvider.java97
1 files changed, 63 insertions, 34 deletions
diff --git a/src/com/android/browser/BrowserProvider.java b/src/com/android/browser/BrowserProvider.java
index f8051a4..d2f4a0a 100644
--- a/src/com/android/browser/BrowserProvider.java
+++ b/src/com/android/browser/BrowserProvider.java
@@ -28,6 +28,8 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.content.UriMatcher;
import android.content.SharedPreferences.Editor;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
import android.database.AbstractCursor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
@@ -62,7 +64,7 @@ public class BrowserProvider extends ContentProvider {
private static final String[] SUGGEST_PROJECTION = new String[] {
"_id", "url", "title", "bookmark"
};
- private static final String SUGGEST_SELECTION =
+ private static final String SUGGEST_SELECTION =
"url LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ?";
private String[] SUGGEST_ARGS = new String[4];
@@ -138,11 +140,11 @@ public class BrowserProvider extends ContentProvider {
// 17 -> 18 Added favicon in bookmarks table for Home shortcuts
// 18 -> 19 Remove labels table
private static final int DATABASE_VERSION = 19;
-
+
// Regular expression which matches http://, followed by some stuff, followed by
// optionally a trailing slash, all matched as separate groups.
private static final Pattern STRIP_URL_PATTERN = Pattern.compile("^(http://)(.*?)(/$)?");
-
+
// The hex color string to be applied to urls of website suggestions, as derived from
// the current theme. This is not set until/unless beautifyUrl is called, at which point
// this variable caches the color value.
@@ -150,12 +152,12 @@ public class BrowserProvider extends ContentProvider {
public BrowserProvider() {
}
-
+
private static CharSequence replaceSystemPropertyInString(Context context, CharSequence srcString) {
StringBuffer sb = new StringBuffer();
int lastCharLoc = 0;
-
+
final String client_id = Partner.getString(context.getContentResolver(), Partner.CLIENT_ID);
for (int i = 0; i < srcString.length(); ++i) {
@@ -217,7 +219,7 @@ public class BrowserProvider extends ContentProvider {
CharSequence bookmarkDestination = replaceSystemPropertyInString(mContext, bookmarks[i + 1]);
db.execSQL("INSERT INTO bookmarks (title, url, visits, " +
"date, created, bookmark)" + " VALUES('" +
- bookmarks[i] + "', '" + bookmarkDestination +
+ bookmarks[i] + "', '" + bookmarkDestination +
"', 0, 0, 0, 1);");
}
} catch (ArrayIndexOutOfBoundsException e) {
@@ -248,7 +250,7 @@ public class BrowserProvider extends ContentProvider {
public boolean onCreate() {
final Context context = getContext();
mOpenHelper = new DatabaseHelper(context);
- // we added "picasa web album" into default bookmarks for version 19.
+ // we added "picasa web album" into default bookmarks for version 19.
// To avoid erasing the bookmark table, we added it explicitly for
// version 18 and 19 as in the other cases, we will erase the table.
if (DATABASE_VERSION == 18 || DATABASE_VERSION == 19) {
@@ -289,9 +291,9 @@ public class BrowserProvider extends ContentProvider {
* Subclass AbstractCursor so we can combine multiple Cursors and add
* "Google Search".
* Here are the rules.
- * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
+ * 1. We only have MAX_SUGGESTION_LONG_ENTRIES in the list plus
* "Google Search";
- * 2. If bookmark/history entries are less than
+ * 2. If bookmark/history entries are less than
* (MAX_SUGGESTION_SHORT_ENTRIES -1), we include Google suggest.
*/
private class MySuggestionCursor extends AbstractCursor {
@@ -301,6 +303,9 @@ public class BrowserProvider extends ContentProvider {
private int mSuggestionCount;
private boolean mBeyondCursor;
private String mString;
+ private int mSuggestText1Id;
+ private int mSuggestText2Id;
+ private int mSuggestQueryId;
public MySuggestionCursor(Cursor hc, Cursor sc, String string) {
mHistoryCursor = hc;
@@ -312,6 +317,22 @@ public class BrowserProvider extends ContentProvider {
}
mString = string;
mBeyondCursor = false;
+
+ // Some web suggest providers only give suggestions and have no description string for
+ // items. The order of the result columns may be different as well. So retrieve the
+ // column indices for the fields we need now and check before using below.
+ if (mSuggestCursor == null) {
+ mSuggestText1Id = -1;
+ mSuggestText2Id = -1;
+ mSuggestQueryId = -1;
+ } else {
+ mSuggestText1Id = mSuggestCursor.getColumnIndex(
+ SearchManager.SUGGEST_COLUMN_TEXT_1);
+ mSuggestText2Id = mSuggestCursor.getColumnIndex(
+ SearchManager.SUGGEST_COLUMN_TEXT_2);
+ mSuggestQueryId = mSuggestCursor.getColumnIndex(
+ SearchManager.SUGGEST_COLUMN_QUERY);
+ }
}
@Override
@@ -344,7 +365,7 @@ public class BrowserProvider extends ContentProvider {
public String[] getColumnNames() {
return COLUMNS;
}
-
+
@Override
public String getString(int columnIndex) {
if ((mPos != -1 && mHistoryCursor != null)) {
@@ -367,7 +388,8 @@ public class BrowserProvider extends ContentProvider {
if (mHistoryCount > mPos) {
return getHistoryTitle();
} else if (!mBeyondCursor) {
- return mSuggestCursor.getString(1);
+ if (mSuggestText1Id == -1) return null;
+ return mSuggestCursor.getString(mSuggestText1Id);
} else {
return mString;
}
@@ -376,7 +398,8 @@ public class BrowserProvider extends ContentProvider {
if (mHistoryCount > mPos) {
return getHistorySubtitle();
} else if (!mBeyondCursor) {
- return mSuggestCursor.getString(2);
+ if (mSuggestText2Id == -1) return null;
+ return mSuggestCursor.getString(mSuggestText2Id);
} else {
return getContext().getString(R.string.search_the_web);
}
@@ -405,11 +428,12 @@ public class BrowserProvider extends ContentProvider {
if (mHistoryCount > mPos) {
return null;
} else if (!mBeyondCursor) {
- return mSuggestCursor.getString(3);
+ if (mSuggestQueryId == -1) return null;
+ return mSuggestCursor.getString(mSuggestQueryId);
} else {
return mString;
}
-
+
case SUGGEST_COLUMN_FORMAT:
return "html";
}
@@ -478,11 +502,11 @@ public class BrowserProvider extends ContentProvider {
mSuggestCursor = null;
}
}
-
+
/**
* Provides the title (text line 1) for a browser suggestion, which should be the
* webpage title. If the webpage title is empty, returns the stripped url instead.
- *
+ *
* @return the title string to use
*/
private String getHistoryTitle() {
@@ -492,12 +516,12 @@ public class BrowserProvider extends ContentProvider {
}
return title;
}
-
+
/**
* Provides the subtitle (text line 2) for a browser suggestion, which should be the
* webpage url. If the webpage title is empty, then the url should go in the title
* instead, and the subtitle should be empty, so this would return null.
- *
+ *
* @return the subtitle string to use, or null if none
*/
private String getHistorySubtitle() {
@@ -508,7 +532,7 @@ public class BrowserProvider extends ContentProvider {
return beautifyUrl(mHistoryCursor.getString(1 /* url */));
}
}
-
+
/**
* Strips "http://" from the beginning of a url and "/" from the end,
* and adds html formatting to make it green.
@@ -520,19 +544,19 @@ public class BrowserProvider extends ContentProvider {
getContext().getTheme().resolveAttribute(
com.android.internal.R.attr.textColorSearchUrl, colorValue, true);
int color = getContext().getResources().getColor(colorValue.resourceId);
-
+
// Convert the int color value into a hex string, and strip the first two
// characters which will be the alpha transparency (html doesn't want this).
mSearchUrlColorHex = Integer.toHexString(color).substring(2);
}
-
+
return "<font color=\"#" + mSearchUrlColorHex + "\">" + stripUrl(url) + "</font>";
}
}
@Override
public Cursor query(Uri url, String[] projectionIn, String selection,
- String[] selectionArgs, String sortOrder)
+ String[] selectionArgs, String sortOrder)
throws IllegalStateException {
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
@@ -575,15 +599,20 @@ public class BrowserProvider extends ContentProvider {
// get Google suggest if there is still space in the list
if (myArgs != null && myArgs.length > 1
&& c.getCount() < (MAX_SUGGESTION_SHORT_ENTRIES - 1)) {
- // TODO: This shouldn't be hard-coded. Instead, it should use the
- // default web search provider. But the API for that is not implemented yet.
- ComponentName googleSearchComponent =
- new ComponentName("com.android.googlesearch",
- "com.android.googlesearch.GoogleSearch");
- SearchableInfo si =
- SearchManager.getSearchableInfo(googleSearchComponent, false);
- Cursor sc = SearchManager.getSuggestions(getContext(), si, selectionArgs[0]);
- return new MySuggestionCursor(c, sc, selectionArgs[0]);
+ Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
+ intent.addCategory(Intent.CATEGORY_DEFAULT);
+ ResolveInfo info = getContext().getPackageManager().resolveActivity(
+ intent, PackageManager.MATCH_DEFAULT_ONLY);
+ if (info != null) {
+ ComponentName googleSearchComponent =
+ new ComponentName(info.activityInfo.packageName,
+ info.activityInfo.name);
+ SearchableInfo si =
+ SearchManager.getSearchableInfo(googleSearchComponent, false);
+ Cursor sc = SearchManager.getSuggestions(
+ getContext(), si, selectionArgs[0]);
+ return new MySuggestionCursor(c, sc, selectionArgs[0]);
+ }
}
return new MySuggestionCursor(c, null, selectionArgs[0]);
}
@@ -735,14 +764,14 @@ public class BrowserProvider extends ContentProvider {
getContext().getContentResolver().notifyChange(url, null);
return ret;
}
-
+
/**
* Strips the provided url of preceding "http://" and any trailing "/". Does not
* strip "https://". If the provided string cannot be stripped, the original string
* is returned.
- *
+ *
* TODO: Put this in TextUtils to be used by other packages doing something similar.
- *
+ *
* @param url a url to strip, like "http://www.google.com/"
* @return a stripped url like "www.google.com", or the original string if it could
* not be stripped