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