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 
17 package com.android.quicksearchbox;
18 
19 import com.android.common.Search;
20 import com.android.common.speech.Recognition;
21 import com.android.quicksearchbox.util.Util;
22 
23 import android.app.Activity;
24 import android.app.AlarmManager;
25 import android.app.PendingIntent;
26 import android.app.SearchManager;
27 import android.appwidget.AppWidgetManager;
28 import android.content.BroadcastReceiver;
29 import android.content.ComponentName;
30 import android.content.Context;
31 import android.content.Intent;
32 import android.graphics.Typeface;
33 import android.net.Uri;
34 import android.os.Bundle;
35 import android.os.SystemClock;
36 import android.speech.RecognizerIntent;
37 import android.text.Annotation;
38 import android.text.SpannableStringBuilder;
39 import android.text.TextUtils;
40 import android.text.style.StyleSpan;
41 import android.util.Log;
42 import android.view.View;
43 import android.widget.RemoteViews;
44 
45 import java.util.ArrayList;
46 import java.util.Random;
47 
48 /**
49  * Search widget provider.
50  *
51  */
52 public class SearchWidgetProvider extends BroadcastReceiver {
53 
54     private static final boolean DBG = false;
55     private static final String TAG = "QSB.SearchWidgetProvider";
56 
57     /**
58      * The {@link Search#SOURCE} value used when starting searches from the search widget.
59      */
60     private static final String WIDGET_SEARCH_SOURCE = "launcher-widget";
61 
62     @Override
onReceive(Context context, Intent intent)63     public void onReceive(Context context, Intent intent) {
64         if (DBG) Log.d(TAG, "onReceive(" + intent.toUri(0) + ")");
65         String action = intent.getAction();
66         if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {
67             // nothing needs doing
68         } else if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
69             updateSearchWidgets(context);
70         } else {
71             if (DBG) Log.d(TAG, "Unhandled intent action=" + action);
72         }
73     }
74 
getSearchWidgetStates(Context context)75     private static SearchWidgetState[] getSearchWidgetStates(Context context) {
76         AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
77         int[] appWidgetIds = appWidgetManager.getAppWidgetIds(myComponentName(context));
78         SearchWidgetState[] states = new SearchWidgetState[appWidgetIds.length];
79         for (int i = 0; i<appWidgetIds.length; ++i) {
80             states[i] = getSearchWidgetState(context, appWidgetIds[i]);
81         }
82         return states;
83     }
84 
85 
86     /**
87      * Updates all search widgets.
88      */
updateSearchWidgets(Context context)89     public static void updateSearchWidgets(Context context) {
90         if (DBG) Log.d(TAG, "updateSearchWidgets");
91         SearchWidgetState[] states = getSearchWidgetStates(context);
92 
93         for (SearchWidgetState state : states) {
94             state.updateWidget(context, AppWidgetManager.getInstance(context));
95         }
96     }
97 
98     /**
99      * Gets the component name of this search widget provider.
100      */
myComponentName(Context context)101     private static ComponentName myComponentName(Context context) {
102         String pkg = context.getPackageName();
103         String cls = pkg + ".SearchWidgetProvider";
104         return new ComponentName(pkg, cls);
105     }
106 
createQsbActivityIntent(Context context, String action, Bundle widgetAppData)107     private static Intent createQsbActivityIntent(Context context, String action,
108             Bundle widgetAppData) {
109         Intent qsbIntent = new Intent(action);
110         qsbIntent.setPackage(context.getPackageName());
111         qsbIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
112                 | Intent.FLAG_ACTIVITY_CLEAR_TOP
113                 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
114         qsbIntent.putExtra(SearchManager.APP_DATA, widgetAppData);
115         return qsbIntent;
116     }
117 
getSearchWidgetState(Context context, int appWidgetId)118     private static SearchWidgetState getSearchWidgetState(Context context, int appWidgetId) {
119         if (DBG) Log.d(TAG, "Creating appwidget state " + appWidgetId);
120         SearchWidgetState state = new SearchWidgetState(appWidgetId);
121 
122         Bundle widgetAppData = new Bundle();
123         widgetAppData.putString(Search.SOURCE, WIDGET_SEARCH_SOURCE);
124 
125         // Text field click
126         Intent qsbIntent = createQsbActivityIntent(
127                 context,
128                 SearchManager.INTENT_ACTION_GLOBAL_SEARCH,
129                 widgetAppData);
130         state.setQueryTextViewIntent(qsbIntent);
131 
132         // Voice search button
133         Intent voiceSearchIntent = getVoiceSearchIntent(context, widgetAppData);
134         state.setVoiceSearchIntent(voiceSearchIntent);
135 
136         return state;
137     }
138 
getVoiceSearchIntent(Context context, Bundle widgetAppData)139     private static Intent getVoiceSearchIntent(Context context, Bundle widgetAppData) {
140         VoiceSearch voiceSearch = QsbApplication.get(context).getVoiceSearch();
141         return voiceSearch.createVoiceWebSearchIntent(widgetAppData);
142     }
143 
144     private static class SearchWidgetState {
145         private final int mAppWidgetId;
146         private Intent mQueryTextViewIntent;
147         private Intent mVoiceSearchIntent;
148 
SearchWidgetState(int appWidgetId)149         public SearchWidgetState(int appWidgetId) {
150             mAppWidgetId = appWidgetId;
151         }
152 
setQueryTextViewIntent(Intent queryTextViewIntent)153         public void setQueryTextViewIntent(Intent queryTextViewIntent) {
154             mQueryTextViewIntent = queryTextViewIntent;
155         }
156 
setVoiceSearchIntent(Intent voiceSearchIntent)157         public void setVoiceSearchIntent(Intent voiceSearchIntent) {
158             mVoiceSearchIntent = voiceSearchIntent;
159         }
160 
updateWidget(Context context,AppWidgetManager appWidgetMgr)161         public void updateWidget(Context context,AppWidgetManager appWidgetMgr) {
162             if (DBG) Log.d(TAG, "Updating appwidget " + mAppWidgetId);
163             RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.search_widget);
164 
165             setOnClickActivityIntent(context, views, R.id.search_widget_text,
166                     mQueryTextViewIntent);
167             // Voice Search button
168             if (mVoiceSearchIntent != null) {
169                 setOnClickActivityIntent(context, views, R.id.search_widget_voice_btn,
170                         mVoiceSearchIntent);
171                 views.setViewVisibility(R.id.search_widget_voice_btn, View.VISIBLE);
172             } else {
173                 views.setViewVisibility(R.id.search_widget_voice_btn, View.GONE);
174             }
175 
176             appWidgetMgr.updateAppWidget(mAppWidgetId, views);
177         }
178 
setOnClickActivityIntent(Context context, RemoteViews views, int viewId, Intent intent)179         private void setOnClickActivityIntent(Context context, RemoteViews views, int viewId,
180                 Intent intent) {
181             PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
182             views.setOnClickPendingIntent(viewId, pendingIntent);
183         }
184     }
185 
186 }
187