SearchWidgetProvider.java revision b5fc08b7f16a32d3865f44b7f26d8aaa5304a2ad
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 = 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 Launcher launcher = new Launcher(context); 95 Corpus voiceSearchCorpus = launcher.getSearchCorpus(getCorpora(context), corpus); 96 if (voiceSearchCorpus != null && launcher.shouldShowVoiceSearch(voiceSearchCorpus)) { 97 Intent voiceSearchIntent = voiceSearchCorpus.createVoiceSearchIntent(widgetAppData); 98 setOnClickIntent(context, views, R.id.search_widget_voice_btn, voiceSearchIntent); 99 views.setViewVisibility(R.id.search_widget_voice_btn, View.VISIBLE); 100 } else { 101 views.setViewVisibility(R.id.search_widget_voice_btn, View.GONE); 102 } 103 104 appWidgetManager.updateAppWidget(appWidgetId, views); 105 } 106 107 private static void bindCorpusIndicator(Context context, RemoteViews views, 108 Bundle widgetAppData, Corpus corpus) { 109 Uri sourceIconUri = getCorpusIconUri(context, corpus); 110 views.setImageViewUri(R.id.corpus_indicator, sourceIconUri); 111 112 Intent intent = new Intent(SearchActivity.INTENT_ACTION_QSB_AND_SELECT_CORPUS); 113 intent.setPackage(context.getPackageName()); 114 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 115 | Intent.FLAG_ACTIVITY_CLEAR_TOP 116 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 117 intent.putExtra(SearchManager.APP_DATA, widgetAppData); 118 intent.setData(SearchActivity.getCorpusUri(corpus)); 119 setOnClickIntent(context, views, R.id.corpus_indicator, intent); 120 } 121 122 private static void setOnClickIntent(Context context, RemoteViews views, 123 int viewId, Intent intent) { 124 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 125 views.setOnClickPendingIntent(viewId, pendingIntent); 126 } 127 128 private static Uri getCorpusIconUri(Context context, Corpus corpus) { 129 if (corpus == null) { 130 return getCorpusViewFactory(context).getGlobalSearchIconUri(); 131 } 132 return corpus.getCorpusIconUri(); 133 } 134 135 private static QsbApplication getQsbApplication(Context context) { 136 return (QsbApplication) context.getApplicationContext(); 137 } 138 139 private static Corpora getCorpora(Context context) { 140 return getQsbApplication(context).getCorpora(); 141 } 142 143 private static CorpusViewFactory getCorpusViewFactory(Context context) { 144 return getQsbApplication(context).getCorpusViewFactory(); 145 } 146 147} 148