SearchWidgetProvider.java revision f36e998667852333addf155fb4bd22132e6a916e
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.common.Search;
20import com.android.quicksearchbox.ui.CorpusViewFactory;
21
22import android.app.PendingIntent;
23import android.app.SearchManager;
24import android.appwidget.AppWidgetManager;
25import android.appwidget.AppWidgetProvider;
26import android.content.Context;
27import android.content.Intent;
28import android.net.Uri;
29import android.os.Bundle;
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 = false;
41    private static final String TAG = "QSB.SearchWidgetProvider";
42
43    private static final String WIDGET_SEARCH_SOURCE = "launcher-widget";
44
45    @Override
46    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
47        final int count = appWidgetIds.length;
48        for (int i = 0; i < count; i++) {
49            updateSearchWidget(context, appWidgetManager, appWidgetIds[i]);
50        }
51    }
52
53    private void updateSearchWidget(Context context, AppWidgetManager appWidgetManager,
54            int appWidgetId) {
55        String corpusName = SearchWidgetConfigActivity.readWidgetCorpusPref(context, appWidgetId);
56        Corpus corpus = corpusName == null ? null : getCorpora(context).getCorpus(corpusName);
57        setupSearchWidget(context, appWidgetManager, appWidgetId, corpus);
58    }
59
60    public static void setupSearchWidget(Context context, AppWidgetManager appWidgetManager,
61            int appWidgetId, Corpus corpus) {
62        if (DBG) Log.d(TAG, "setupSearchWidget()");
63        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.search_widget);
64
65        Bundle widgetAppData = new Bundle();
66        widgetAppData.putString(Search.SOURCE, WIDGET_SEARCH_SOURCE);
67
68        // Corpus indicator
69        bindCorpusIndicator(context, views, widgetAppData, corpus);
70
71        // Hint
72        CharSequence hint;
73        int backgroundId;
74        if (corpus == null || corpus.isWebCorpus()) {
75            hint = null;
76            backgroundId = R.drawable.textfield_search_empty_google;
77        } else {
78            hint = corpus.getHint();
79            backgroundId = R.drawable.textfield_search_empty;
80        }
81        views.setCharSequence(R.id.search_widget_text, "setHint", hint);
82        views.setInt(R.id.search_widget_text, "setBackgroundResource", backgroundId);
83
84        // Text field click
85        Intent qsbIntent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
86        qsbIntent.setPackage(context.getPackageName());
87        qsbIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
88                | Intent.FLAG_ACTIVITY_CLEAR_TOP
89                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
90        qsbIntent.putExtra(SearchManager.APP_DATA, widgetAppData);
91        qsbIntent.setData(SearchActivity.getCorpusUri(corpus));
92        setOnClickIntent(context, views, R.id.search_widget_text, qsbIntent);
93
94        Intent voiceSearchIntent = getVoiceSearchIntent(context, corpus, widgetAppData);
95        if (voiceSearchIntent != null) {
96            setOnClickIntent(context, views, R.id.search_widget_voice_btn, voiceSearchIntent);
97            views.setViewVisibility(R.id.search_widget_voice_btn, View.VISIBLE);
98        } else {
99            views.setViewVisibility(R.id.search_widget_voice_btn, View.GONE);
100        }
101
102        appWidgetManager.updateAppWidget(appWidgetId, views);
103    }
104
105    private static void bindCorpusIndicator(Context context, RemoteViews views,
106            Bundle widgetAppData, Corpus corpus) {
107        Uri sourceIconUri = getCorpusIconUri(context, corpus);
108        views.setImageViewUri(R.id.corpus_indicator, sourceIconUri);
109
110        Intent intent = new Intent(SearchActivity.INTENT_ACTION_QSB_AND_SELECT_CORPUS);
111        intent.setPackage(context.getPackageName());
112        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
113                | Intent.FLAG_ACTIVITY_CLEAR_TOP
114                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
115        intent.putExtra(SearchManager.APP_DATA, widgetAppData);
116        intent.setData(SearchActivity.getCorpusUri(corpus));
117        setOnClickIntent(context, views, R.id.corpus_indicator, intent);
118    }
119
120    private static Intent getVoiceSearchIntent(Context context, Corpus corpus,
121            Bundle widgetAppData) {
122        Launcher launcher = new Launcher(context);
123        if (!launcher.shouldShowVoiceSearch(corpus)) return null;
124        if (corpus == null) {
125            return WebCorpus.createVoiceWebSearchIntent(widgetAppData);
126        } else {
127            return corpus.createVoiceSearchIntent(widgetAppData);
128        }
129    }
130
131    private static void setOnClickIntent(Context context, RemoteViews views,
132            int viewId, Intent intent) {
133        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
134        views.setOnClickPendingIntent(viewId, pendingIntent);
135    }
136
137    private static Uri getCorpusIconUri(Context context, Corpus corpus) {
138        if (corpus == null) {
139            return getCorpusViewFactory(context).getGlobalSearchIconUri();
140        }
141        return corpus.getCorpusIconUri();
142    }
143
144    private static QsbApplication getQsbApplication(Context context) {
145        return (QsbApplication) context.getApplicationContext();
146    }
147
148    private static Corpora getCorpora(Context context) {
149        return getQsbApplication(context).getCorpora();
150    }
151
152    private static CorpusViewFactory getCorpusViewFactory(Context context) {
153        return getQsbApplication(context).getCorpusViewFactory();
154    }
155
156}
157