SearchWidgetProvider.java revision 2617a0177a6088d5aaf381263229bf5a62d2238d
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.SearchSourceSelector; 20import com.android.quicksearchbox.ui.SuggestionViewFactory; 21 22import android.app.PendingIntent; 23import android.app.SearchManager; 24import android.appwidget.AppWidgetManager; 25import android.appwidget.AppWidgetProvider; 26import android.content.ComponentName; 27import android.content.Context; 28import android.content.Intent; 29import android.net.Uri; 30import android.os.Bundle; 31import android.speech.RecognizerIntent; 32import android.util.Log; 33import android.view.View; 34import android.widget.RemoteViews; 35 36/** 37 * Search widget provider. 38 * 39 */ 40public class SearchWidgetProvider extends AppWidgetProvider { 41 42 private static final boolean DBG = true; 43 private static final String TAG = "QSB.SearchWidgetProvider"; 44 45 private static final String WIDGET_SEARCH_SOURCE = "launcher-search"; 46 47 // TODO: Expose SearchManager.SOURCE instead. 48 private static final String SOURCE = "source"; 49 50 @Override 51 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 52 final int count = appWidgetIds.length; 53 for (int i = 0; i < count; i++) { 54 updateSearchWidget(context, appWidgetManager, appWidgetIds[i]); 55 } 56 } 57 58 private void updateSearchWidget(Context context, AppWidgetManager appWidgetManager, 59 int appWidgetId) { 60 ComponentName sourceName = 61 SearchWidgetConfigActivity.readWidgetSourcePref(context, appWidgetId); 62 Source source = getSources(context).getSourceByComponentName(sourceName); 63 setupSearchWidget(context, appWidgetManager, appWidgetId, source); 64 } 65 66 public static void setupSearchWidget(Context context, AppWidgetManager appWidgetManager, 67 int appWidgetId, Source source) { 68 if (DBG) Log.d(TAG, "setupSearchWidget()"); 69 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.search_widget); 70 71 ComponentName sourceName = source == null ? null : source.getComponentName(); 72 73 Bundle widgetAppData = new Bundle(); 74 widgetAppData.putString(SOURCE, WIDGET_SEARCH_SOURCE); 75 76 // Source selector 77 bindSourceSelector(context, views, widgetAppData, source); 78 79 // Text field 80 Intent qsbIntent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH); 81 qsbIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 82 | Intent.FLAG_ACTIVITY_CLEAR_TOP 83 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 84 qsbIntent.putExtra(SearchManager.APP_DATA, widgetAppData); 85 SearchSourceSelector.setSource(qsbIntent, sourceName); 86 setOnClickIntent(context, views, R.id.search_widget_text, qsbIntent); 87 88 // Voice search button. Only shown if voice search is available. 89 // TODO: This should be Voice Search for the selected source, 90 // and only show if available for that source 91 Intent voiceSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH); 92 voiceSearchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 93 | Intent.FLAG_ACTIVITY_CLEAR_TOP 94 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 95 voiceSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 96 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); 97 // TODO: Does VoiceSearch actually look at APP_DATA? 98 voiceSearchIntent.putExtra(SearchManager.APP_DATA, widgetAppData); 99 if (voiceSearchIntent.resolveActivity(context.getPackageManager()) != null) { 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 bindSourceSelector(Context context, RemoteViews views, 110 Bundle widgetAppData, Source source) { 111 Uri sourceIconUri = getSourceIconUri(context, source); 112 views.setImageViewUri(SearchSourceSelector.ICON_VIEW_ID, sourceIconUri); 113 ComponentName sourceName = source == null ? null : source.getComponentName(); 114 115 Intent intent = new Intent(SearchActivity.INTENT_ACTION_QSB_AND_SELECT_SEARCH_SOURCE); 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 SearchSourceSelector.setSource(intent, sourceName); 121 setOnClickIntent(context, views, SearchSourceSelector.ICON_VIEW_ID, 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 getSourceIconUri(Context context, Source source) { 131 if (source == null) { 132 return getSuggestionViewFactory(context).getGlobalSearchIconUri(); 133 } 134 return source.getSourceIconUri(); 135 } 136 137 private static QsbApplication getQsbApplication(Context context) { 138 return (QsbApplication) context.getApplicationContext(); 139 } 140 141 private static SourceLookup getSources(Context context) { 142 return getQsbApplication(context).getSources(); 143 } 144 145 private static SuggestionViewFactory getSuggestionViewFactory(Context context) { 146 return getQsbApplication(context).getSuggestionViewFactory(); 147 } 148 149} 150