summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSatish Sampath <satish@android.com>2009-06-05 12:11:05 +0100
committerSatish Sampath <satish@android.com>2009-06-05 12:13:27 +0100
commit84b1e2b3a3833c7e6910c176912efbe25b318d88 (patch)
tree13e8f4210e2b6d77f2033fa31bd62de72703655b
parent40add5db792311bdee1a2865c827e43d8a28d302 (diff)
downloadGoogleSearch-84b1e2b3a3833c7e6910c176912efbe25b318d88.tar.gz
Get web search to work with GoogleSearch provider.
- make the suggestion provider specify the intent action web-search - handle web search intent by launching the browser with the google search url Without these changes, GoogleSearch was passing intent web-search to the browser which was forwarding it back to GoogleSearch and nothing happened in the end.
-rw-r--r--Android.mk2
-rw-r--r--res/values/strings.xml17
-rw-r--r--src/com/android/googlesearch/GoogleSearch.java75
-rw-r--r--src/com/android/googlesearch/SuggestionProvider.java33
4 files changed, 97 insertions, 30 deletions
diff --git a/Android.mk b/Android.mk
index efe4ce7..5bd01cb 100644
--- a/Android.mk
+++ b/Android.mk
@@ -19,6 +19,8 @@ include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := user # should be optional, but launcher crashes without this
+LOCAL_STATIC_JAVA_LIBRARIES := google-framework
+
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_PACKAGE_NAME := GoogleSearch
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 4e26577..455eca2 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -4,9 +4,9 @@
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
-
+
http://www.apache.org/licenses/LICENSE-2.0
-
+
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -23,12 +23,21 @@
Home/res/values/strings.xml -->
<string name="search_hint">Google Search</string>
+ <!-- Note that this is the standard search url. It uses the current locale for language -->
+ <!-- (%1$s) and country (%2$s) and shouldn't need to be replaced by locale or mcc selected -->
+ <!-- resources. -->
+ <!-- Any changes to this (especially in terms of adding/removing the MCC-specific flavors) -->
+ <!-- should be mirrored by changes in GenieProvider. -->
+ <!-- Do not translate. This string is an internal URI, not seen in the UI. -->
+ <string name="google_search_base" translatable="false">
+ http://www.google.com/m?hl=%1$s&amp;gl=%2$s&amp;</string>
+
<!-- Note that this is the standard suggest url. It uses the current locale for language -->
<!-- (t%1$s) and country (%2$s) and should not need to be replaced by locale or mcc -->
<!-- selected resources. -->
<!-- Any changes to this (especially in terms of adding/removing the MCC-specific flavors) -->
- <!-- should be mirrored by changes in apps/Browser -->
+ <!-- should be mirrored by changes in GenieProvider -->
<!-- Do not translate. This string is an internal URI, not seen in the UI. -->
- <string name="google_search_base" translatable="false">
+ <string name="google_suggest_base" translatable="false">
http://www.google.com/complete/search?hl=%1$s&amp;gl=%2$s&amp;</string>
</resources>
diff --git a/src/com/android/googlesearch/GoogleSearch.java b/src/com/android/googlesearch/GoogleSearch.java
index cab6428..d429e3a 100644
--- a/src/com/android/googlesearch/GoogleSearch.java
+++ b/src/com/android/googlesearch/GoogleSearch.java
@@ -16,36 +16,87 @@
package com.android.googlesearch;
+import com.google.android.providers.GoogleSettings.Partner;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.Locale;
+
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
+import android.net.Uri;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.text.TextUtils;
+import android.util.Log;
/**
* This class is purely here to get search queries and route them to
* the global {@link Intent#ACTION_WEB_SEARCH}.
*/
public class GoogleSearch extends Activity {
+ private static final String TAG = "GoogleSearch";
+
+ // The template URL we should use to format google search requests.
+ private String googleSearchUrlBase = null;
+
+ // "source" parameter for Google search requests.
+ final static String GOOGLE_SEARCH_SOURCE_SUGGEST = "browser-suggest";
+
+ /**
+ * This function is the same exact one as found in
+ * com.google.android.providers.genie.GenieLauncher. If you are changing this make sure you
+ * change both.
+ */
+ private void handleWebSearchIntent(Intent intent) {
+ String query = intent.getStringExtra(SearchManager.QUERY);
+ if (TextUtils.isEmpty(query)) {
+ Log.w(TAG, "Got search intent with no query.");
+ return;
+ }
+
+ if (googleSearchUrlBase == null) {
+ Locale l = Locale.getDefault();
+ String language = l.getLanguage();
+ String country = l.getCountry().toLowerCase();
+ // Chinese and Portuguese have two langauge variants.
+ if ("zh".equals(language)) {
+ if ("cn".equals(country)) {
+ language = "zh-CN";
+ } else if ("tw".equals(country)) {
+ language = "zh-TW";
+ }
+ } else if ("pt".equals(language)) {
+ if ("br".equals(country)) {
+ language = "pt-BR";
+ } else if ("pt".equals(country)) {
+ language = "pt-PT";
+ }
+ }
+ googleSearchUrlBase = getResources().getString(
+ R.string.google_search_base, language, country)
+ + "client=ms-"
+ + Partner.getString(this.getContentResolver(), Partner.CLIENT_ID)
+ + "&source=android-" + GOOGLE_SEARCH_SOURCE_SUGGEST + "&q=";
+ }
+
+ try {
+ String searchUri = googleSearchUrlBase + URLEncoder.encode(query, "UTF-8");
+ Intent launchUriIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(searchUri));
+ launchUriIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ startActivity(launchUriIntent);
+ } catch (UnsupportedEncodingException e) {
+ Log.w(TAG, "Error", e);
+ }
+ }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if ((intent != null) && Intent.ACTION_WEB_SEARCH.equals(intent.getAction())) {
- String query = intent.getStringExtra(SearchManager.QUERY);
- if (!TextUtils.isEmpty(query)) {
- // forward query to browser for Google search
- Intent search = new Intent(Intent.ACTION_WEB_SEARCH);
- search.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- search.putExtra(SearchManager.QUERY, query);
- final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
- if (appData != null) {
- search.putExtra(SearchManager.APP_DATA, appData);
- }
- startActivity(search);
- }
+ handleWebSearchIntent(intent);
}
finish();
}
diff --git a/src/com/android/googlesearch/SuggestionProvider.java b/src/com/android/googlesearch/SuggestionProvider.java
index 141aaed..b1d458a 100644
--- a/src/com/android/googlesearch/SuggestionProvider.java
+++ b/src/com/android/googlesearch/SuggestionProvider.java
@@ -31,6 +31,7 @@ import org.json.JSONException;
import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentValues;
+import android.content.Intent;
import android.database.AbstractCursor;
import android.database.Cursor;
import android.net.Uri;
@@ -45,29 +46,31 @@ import java.util.Locale;
/**
* Use network-based Google Suggests to provide search suggestions.
- *
+ *
* Future: Merge live suggestions with saved recent queries
*/
public class SuggestionProvider extends ContentProvider {
-
+
public static final Uri CONTENT_URI = Uri.parse(
"content://com.android.googlesearch.SuggestionProvider");
private static final String USER_AGENT = "Android/1.0";
private String mSuggestUri;
private static final int HTTP_TIMEOUT_MS = 1000;
-
+
// TODO: this should be defined somewhere
private static final String HTTP_TIMEOUT = "http.connection-manager.timeout";
private static final String LOG_TAG = "GoogleSearch.SuggestionProvider";
-
+
/* The suggestion columns used */
private static final String[] COLUMNS = new String[] {
- "_id",
- SearchManager.SUGGEST_COLUMN_TEXT_1,
- SearchManager.SUGGEST_COLUMN_TEXT_2,
- SearchManager.SUGGEST_COLUMN_QUERY};
+ "_id",
+ SearchManager.SUGGEST_COLUMN_TEXT_1,
+ SearchManager.SUGGEST_COLUMN_TEXT_2,
+ SearchManager.SUGGEST_COLUMN_QUERY,
+ SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
+ };
private HttpClient mHttpClient;
@@ -106,7 +109,7 @@ public class SuggestionProvider extends ContentProvider {
String[] selectionArgs, String sortOrder) {
String query = selectionArgs[0];
if (TextUtils.isEmpty(query)) {
-
+
/* Can't pass back null, things blow up */
return makeEmptyCursor();
}
@@ -115,7 +118,7 @@ public class SuggestionProvider extends ContentProvider {
// NOTE: This code uses resources to optionally select the search Uri, based on the
// MCC value from the SIM. iThe default string will most likely be fine. It is
// paramerterized to accept info from the Locale, the language code is the first
- // parameter (%1$s) and the country code is the second (%2$s). This code *must*
+ // parameter (%1$s) and the country code is the second (%2$s). This code *must*
// function in the same way as a similar lookup in
// com.android.browser.BrowserActivity#onCreate(). If you change
// either of these functions, change them both. (The same is true for the underlying
@@ -138,7 +141,7 @@ public class SuggestionProvider extends ContentProvider {
language = "pt-PT";
}
}
- mSuggestUri = getContext().getResources().getString(R.string.google_search_base,
+ mSuggestUri = getContext().getResources().getString(R.string.google_suggest_base,
language,
country)
+ "json=true&q=";
@@ -149,7 +152,7 @@ public class SuggestionProvider extends ContentProvider {
method.setEntity(content);
HttpResponse response = mHttpClient.execute(method);
if (response.getStatusLine().getStatusCode() == 200) {
-
+
/* Goto http://www.google.com/complete/search?json=true&q=foo
* to see what the data format looks like. It's basically a json
* array containing 4 other arrays. We only care about the middle
@@ -174,7 +177,7 @@ public class SuggestionProvider extends ContentProvider {
/* Contains the actual suggestions */
final JSONArray mSuggestions;
-
+
/* This contains the popularity of each suggestion
* i.e. 165,000 results. It's not related to sorting.
*/
@@ -209,6 +212,8 @@ public class SuggestionProvider extends ContentProvider {
} catch (JSONException e) {
Log.w(LOG_TAG, "Error", e);
}
+ } else if (column == 4) {
+ return Intent.ACTION_WEB_SEARCH;
}
}
return null;
@@ -247,7 +252,7 @@ public class SuggestionProvider extends ContentProvider {
throw new UnsupportedOperationException();
}
}
-
+
@Override
public Uri insert(Uri uri, ContentValues values) {
throw new UnsupportedOperationException();