SearchWidgetProvider.java revision a576b1abd602c08dcf0d470facc43fac0243b07c
1/*
2 * Copyright (C) 2009 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;
18
19import com.android.quicksearchbox.ui.CorpusViewFactory;
20
21import android.app.PendingIntent;
22import android.app.SearchManager;
23import android.appwidget.AppWidgetManager;
24import android.appwidget.AppWidgetProvider;
25import android.content.Context;
26import android.content.Intent;
27import android.net.Uri;
28import android.os.Bundle;
29import android.speech.RecognizerIntent;
30import android.util.Log;
31import android.view.View;
32import android.widget.RemoteViews;
33
34/**
35 * Search widget provider.
36 *
37 */
38public class SearchWidgetProvider extends AppWidgetProvider {
39
40    private static final boolean DBG = true;
41    private static final String TAG = "QSB.SearchWidgetProvider";
42
43    private static final String WIDGET_SEARCH_SOURCE = "launcher-search";
44
45    // TODO: Expose SearchManager.SOURCE instead.
46    private static final String SOURCE = "source";
47
48    @Override
49    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
50        final int count = appWidgetIds.length;
51        for (int i = 0; i < count; i++) {
52            updateSearchWidget(context, appWidgetManager, appWidgetIds[i]);
53        }
54    }
55
56    private void updateSearchWidget(Context context, AppWidgetManager appWidgetManager,
57            int appWidgetId) {
58        String corpusName = SearchWidgetConfigActivity.readWidgetCorpusPref(context, appWidgetId);
59        Corpus corpus = getCorpora(context).getCorpus(corpusName);
60        setupSearchWidget(context, appWidgetManager, appWidgetId, corpus);
61    }
62
63    public static void setupSearchWidget(Context context, AppWidgetManager appWidgetManager,
64            int appWidgetId, Corpus corpus) {
65        if (DBG) Log.d(TAG, "setupSearchWidget()");
66        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.search_widget);
67
68        Bundle widgetAppData = new Bundle();
69        widgetAppData.putString(SOURCE, WIDGET_SEARCH_SOURCE);
70
71        // Corpus indicator
72        bindCorpusIndicator(context, views, widgetAppData, corpus);
73
74        // Text field
75        Intent qsbIntent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
76        qsbIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
77                | Intent.FLAG_ACTIVITY_CLEAR_TOP
78                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
79        qsbIntent.putExtra(SearchManager.APP_DATA, widgetAppData);
80        qsbIntent.setData(SearchActivity.getCorpusUri(corpus));
81        setOnClickIntent(context, views, R.id.search_widget_text, qsbIntent);
82
83        // Voice search button. Only shown if voice search is available.
84        // TODO: This should be Voice Search for the selected source,
85        // and only show if available for that source
86        Intent voiceSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
87        voiceSearchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
88                | Intent.FLAG_ACTIVITY_CLEAR_TOP
89                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
90        voiceSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
91                RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
92        // TODO: Does VoiceSearch actually look at APP_DATA?
93        voiceSearchIntent.putExtra(SearchManager.APP_DATA, widgetAppData);
94        if (voiceSearchIntent.resolveActivity(context.getPackageManager()) != null) {
95            setOnClickIntent(context, views, R.id.search_widget_voice_btn, voiceSearchIntent);
96            views.setViewVisibility(R.id.search_widget_voice_btn, View.VISIBLE);
97        } else {
98            views.setViewVisibility(R.id.search_widget_voice_btn, View.GONE);
99        }
100
101        appWidgetManager.updateAppWidget(appWidgetId, views);
102    }
103
104    private static void bindCorpusIndicator(Context context, RemoteViews views,
105            Bundle widgetAppData, Corpus corpus) {
106        Uri sourceIconUri = getCorpusIconUri(context, corpus);
107        views.setImageViewUri(R.id.corpus_indicator, sourceIconUri);
108
109        Intent intent = new Intent(SearchActivity.INTENT_ACTION_QSB_AND_SELECT_CORPUS);
110        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
111                | Intent.FLAG_ACTIVITY_CLEAR_TOP
112                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
113        intent.putExtra(SearchManager.APP_DATA, widgetAppData);
114        intent.setData(SearchActivity.getCorpusUri(corpus));
115        setOnClickIntent(context, views, R.id.corpus_indicator, intent);
116    }
117
118    private static void setOnClickIntent(Context context, RemoteViews views,
119            int viewId, Intent intent) {
120        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
121        views.setOnClickPendingIntent(viewId, pendingIntent);
122    }
123
124    private static Uri getCorpusIconUri(Context context, Corpus corpus) {
125        if (corpus == null) {
126            return getCorpusViewFactory(context).getGlobalSearchIconUri();
127        }
128        return corpus.getCorpusIconUri();
129    }
130
131    private static QsbApplication getQsbApplication(Context context) {
132        return (QsbApplication) context.getApplicationContext();
133    }
134
135    private static Corpora getCorpora(Context context) {
136        return getQsbApplication(context).getCorpora();
137    }
138
139    private static CorpusViewFactory getCorpusViewFactory(Context context) {
140        return getQsbApplication(context).getCorpusViewFactory();
141    }
142
143}
144