GoogleSearch.java revision 4a1c1325ac4b141b1610faeba49f2f3e54772eb6
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.quicksearchbox.R;
20import com.google.android.providers.GoogleSettings.Partner;
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
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                    + "client=ms-"
93                    + Partner.getString(this.getContentResolver(), Partner.CLIENT_ID);
94        }
95
96        // If the caller specified a 'source' url parameter, use that and if not use default.
97        Bundle appSearchData = intent.getBundleExtra(SearchManager.APP_DATA);
98        String source = GOOGLE_SEARCH_SOURCE_UNKNOWN;
99        if (appSearchData != null) {
100            source = appSearchData.getString(SearchManager.SOURCE);
101        }
102
103        // The browser can pass along an application id which it uses to figure out which
104        // window to place a new search into. So if this exists, we'll pass it back to
105        // the browser. Otherwise, add our own package name as the application id, so that
106        // the browser can organize all searches launched from this provider together.
107        String applicationId = intent.getStringExtra(Browser.EXTRA_APPLICATION_ID);
108        if (applicationId == null) {
109            applicationId = getPackageName();
110        }
111
112        try {
113            String searchUri = googleSearchUrlBase
114                    + "&source=android-" + source
115                    + "&q=" + URLEncoder.encode(query, "UTF-8");
116            Intent launchUriIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(searchUri));
117            launchUriIntent.putExtra(Browser.EXTRA_APPLICATION_ID, applicationId);
118            launchUriIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
119            startActivity(launchUriIntent);
120        } catch (UnsupportedEncodingException e) {
121            Log.w(TAG, "Error", e);
122        }
123    }
124
125}
126