GoogleSearch.java revision c953ef06f0fc1fb4157fe67aa145cf702ee204d0
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 17package com.android.quicksearchbox.google; 18 19import com.android.common.Search; 20import com.android.quicksearchbox.QsbApplication; 21 22import android.app.Activity; 23import android.app.SearchManager; 24import android.content.Intent; 25import android.net.Uri; 26import android.os.Bundle; 27import android.provider.Browser; 28import android.text.TextUtils; 29import android.util.Log; 30 31import java.io.UnsupportedEncodingException; 32import java.net.URLEncoder; 33import 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 */ 39public class GoogleSearch extends Activity { 40 private static final String TAG = "GoogleSearch"; 41 private static final boolean DBG = false; 42 43 // Used to figure out which domain to base search requests 44 // on. 45 private SearchBaseUrlHelper mSearchDomainHelper; 46 47 // "source" parameter for Google search requests from unknown sources (e.g. apps). This will get 48 // prefixed with the string 'android-' before being sent on the wire. 49 final static String GOOGLE_SEARCH_SOURCE_UNKNOWN = "unknown"; 50 51 @Override 52 protected void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 Intent intent = getIntent(); 55 String action = intent != null ? intent.getAction() : null; 56 57 // This should probably be moved so as to 58 // send out the request to /checksearchdomain as early as possible. 59 mSearchDomainHelper = QsbApplication.get(this).getSearchBaseUrlHelper(); 60 61 if (Intent.ACTION_WEB_SEARCH.equals(action) || Intent.ACTION_SEARCH.equals(action)) { 62 handleWebSearchIntent(intent); 63 } 64 65 finish(); 66 } 67 68 /** 69 * Construct the language code (hl= paramater) for the given locale. 70 */ 71 public static String getLanguage(Locale locale) { 72 String language = locale.getLanguage(); 73 StringBuilder hl = new StringBuilder(language); 74 String country = locale.getCountry(); 75 if (!TextUtils.isEmpty(country)) { 76 hl.append('-'); 77 hl.append(country); 78 } 79 80 if (DBG) Log.d(TAG, "language " + language + ", country " + country + " -> hl=" + hl); 81 return hl.toString(); 82 } 83 84 private void handleWebSearchIntent(Intent intent) { 85 String query = intent.getStringExtra(SearchManager.QUERY); 86 if (TextUtils.isEmpty(query)) { 87 Log.w(TAG, "Got search intent with no query."); 88 return; 89 } 90 91 // If the caller specified a 'source' url parameter, use that and if not use default. 92 Bundle appSearchData = intent.getBundleExtra(SearchManager.APP_DATA); 93 String source = GOOGLE_SEARCH_SOURCE_UNKNOWN; 94 if (appSearchData != null) { 95 source = appSearchData.getString(Search.SOURCE); 96 } 97 98 // The browser can pass along an application id which it uses to figure out which 99 // window to place a new search into. So if this exists, we'll pass it back to 100 // the browser. Otherwise, add our own package name as the application id, so that 101 // the browser can organize all searches launched from this provider together. 102 String applicationId = intent.getStringExtra(Browser.EXTRA_APPLICATION_ID); 103 if (applicationId == null) { 104 applicationId = getPackageName(); 105 } 106 107 try { 108 String searchUri = mSearchDomainHelper.getSearchBaseUrl() 109 + "&source=android-" + source 110 + "&q=" + URLEncoder.encode(query, "UTF-8"); 111 Intent launchUriIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(searchUri)); 112 launchUriIntent.putExtra(Browser.EXTRA_APPLICATION_ID, applicationId); 113 launchUriIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 114 startActivity(launchUriIntent); 115 } catch (UnsupportedEncodingException e) { 116 Log.w(TAG, "Error", e); 117 } 118 } 119 120} 121