1 /* 2 * Copyright (C) 2008 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 17 package com.android.quicksearchbox.google; 18 19 import com.android.common.Search; 20 import com.android.quicksearchbox.R; 21 22 import android.app.Activity; 23 import android.app.SearchManager; 24 import android.content.Intent; 25 import android.net.Uri; 26 import android.os.Bundle; 27 import android.provider.Browser; 28 import android.text.TextUtils; 29 import android.util.Log; 30 31 import java.io.UnsupportedEncodingException; 32 import java.net.URLEncoder; 33 import java.util.Locale; 34 35 /** 36 * This class is purely here to get search queries and route them to 37 * the global {@link Intent#ACTION_WEB_SEARCH}. 38 */ 39 public class GoogleSearch extends Activity { 40 private static final String TAG = "GoogleSearch"; 41 42 // The template URL we should use to format google search requests. 43 private String googleSearchUrlBase = null; 44 45 // "source" parameter for Google search requests from unknown sources (e.g. apps). This will get 46 // prefixed with the string 'android-' before being sent on the wire. 47 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown"; 48 49 @Override 50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 Intent intent = getIntent(); 53 String action = intent != null ? intent.getAction() : null; 54 if (Intent.ACTION_WEB_SEARCH.equals(action) || Intent.ACTION_SEARCH.equals(action)) { 55 handleWebSearchIntent(intent); 56 } 57 finish(); 58 } 59 60 /** 61 * NOTE: This function is similar to the one found in 62 * com.google.android.providers.enhancedgooglesearch.Launcher. If you are changing this 63 * make sure you change both. 64 */ 65 private void handleWebSearchIntent(Intent intent) { 66 String query = intent.getStringExtra(SearchManager.QUERY); 67 if (TextUtils.isEmpty(query)) { 68 Log.w(TAG, "Got search intent with no query."); 69 return; 70 } 71 72 if (googleSearchUrlBase == null) { 73 Locale l = Locale.getDefault(); 74 String language = l.getLanguage(); 75 String country = l.getCountry().toLowerCase(); 76 // Chinese and Portuguese have two langauge variants. 77 if ("zh".equals(language)) { 78 if ("cn".equals(country)) { 79 language = "zh-CN"; 80 } else if ("tw".equals(country)) { 81 language = "zh-TW"; 82 } 83 } else if ("pt".equals(language)) { 84 if ("br".equals(country)) { 85 language = "pt-BR"; 86 } else if ("pt".equals(country)) { 87 language = "pt-PT"; 88 } 89 } 90 googleSearchUrlBase = getResources().getString( 91 R.string.google_search_base, language, country); 92 } 93 94 // If the caller specified a 'source' url parameter, use that and if not use default. 95 Bundle appSearchData = intent.getBundleExtra(SearchManager.APP_DATA); 96 String source = GOOGLE_SEARCH_SOURCE_UNKNOWN; 97 if (appSearchData != null) { 98 source = appSearchData.getString(Search.SOURCE); 99 } 100 101 // The browser can pass along an application id which it uses to figure out which 102 // window to place a new search into. So if this exists, we'll pass it back to 103 // the browser. Otherwise, add our own package name as the application id, so that 104 // the browser can organize all searches launched from this provider together. 105 String applicationId = intent.getStringExtra(Browser.EXTRA_APPLICATION_ID); 106 if (applicationId == null) { 107 applicationId = getPackageName(); 108 } 109 110 try { 111 String searchUri = googleSearchUrlBase 112 + "&source=android-" + source 113 + "&q=" + URLEncoder.encode(query, "UTF-8"); 114 Intent launchUriIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(searchUri)); 115 launchUriIntent.putExtra(Browser.EXTRA_APPLICATION_ID, applicationId); 116 launchUriIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 117 startActivity(launchUriIntent); 118 } catch (UnsupportedEncodingException e) { 119 Log.w(TAG, "Error", e); 120 } 121 } 122 123 } 124