blob: 9c18df8c6c2abffc96435766a55344c05db22d05 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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 android.app;
18
Amith Yamasani479ae0a2010-02-04 14:52:32 -080019
Amith Yamasanid25eb352010-03-10 21:09:28 -080020import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.ComponentName;
Bjorn Bringert444c7272009-07-06 21:32:50 +010022import android.content.Context;
23import android.content.Intent;
Amith Yamasanid25eb352010-03-10 21:09:28 -080024import android.content.IntentFilter;
Karl Rosaen875d50a2009-04-23 19:00:21 -070025import android.content.pm.ActivityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.pm.PackageManager;
Bjorn Bringertc1f40962009-04-29 13:08:39 +010027import android.content.pm.PackageManager.NameNotFoundException;
Amith Yamasanid25eb352010-03-10 21:09:28 -080028import android.content.res.Configuration;
Bjorn Bringert444c7272009-07-06 21:32:50 +010029import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.net.Uri;
31import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.speech.RecognizerIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.text.InputType;
34import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.util.AttributeSet;
36import android.util.Log;
Adam Powell8abebcd2011-05-19 18:46:47 -070037import android.util.TypedValue;
38import android.view.ActionMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.view.Gravity;
40import android.view.KeyEvent;
Karl Rosaen875d50a2009-04-23 19:00:21 -070041import android.view.MotionEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.view.View;
Karl Rosaen875d50a2009-04-23 19:00:21 -070043import android.view.ViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.view.ViewGroup;
45import android.view.Window;
46import android.view.WindowManager;
Dianne Hackborna8f556e2009-03-24 20:47:50 -070047import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.widget.AutoCompleteTextView;
Bjorn Bringertda5c1bd2010-02-04 15:11:25 +000049import android.widget.ImageView;
Bjorn Bringert077357c2009-09-23 13:57:24 +010050import android.widget.LinearLayout;
Amith Yamasani968ec932010-12-02 14:00:47 -080051import android.widget.SearchView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.widget.TextView;
Amith Yamasani968ec932010-12-02 14:00:47 -080053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054/**
Amith Yamasanie9ce3f02010-01-25 09:15:50 -080055 * Search dialog. This is controlled by the
56 * SearchManager and runs in the current foreground process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 *
58 * @hide
59 */
Amith Yamasani968ec932010-12-02 14:00:47 -080060public class SearchDialog extends Dialog {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
62 // Debugging support
Karl Rosaen875d50a2009-04-23 19:00:21 -070063 private static final boolean DBG = false;
64 private static final String LOG_TAG = "SearchDialog";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 private static final String INSTANCE_KEY_COMPONENT = "comp";
67 private static final String INSTANCE_KEY_APPDATA = "data";
Bjorn Bringertb0ae27f2009-06-23 13:47:31 +010068 private static final String INSTANCE_KEY_USER_QUERY = "uQry";
Amith Yamasani968ec932010-12-02 14:00:47 -080069
Mike LeBeau7df84612009-10-30 16:11:40 -070070 // The string used for privateImeOptions to identify to the IME that it should not show
71 // a microphone button since one already exists in the search dialog.
72 private static final String IME_OPTION_NO_MICROPHONE = "nm";
Bjorn Bringertb0ae27f2009-06-23 13:47:31 +010073
Mike LeBeau1fd73232009-04-27 19:12:05 -070074 private static final int SEARCH_PLATE_LEFT_PADDING_NON_GLOBAL = 7;
Bjorn Bringert444c7272009-07-06 21:32:50 +010075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 // views & widgets
77 private TextView mBadgeLabel;
Bjorn Bringertda5c1bd2010-02-04 15:11:25 +000078 private ImageView mAppIcon;
Amith Yamasani968ec932010-12-02 14:00:47 -080079 private AutoCompleteTextView mSearchAutoComplete;
Karl Rosaen875d50a2009-04-23 19:00:21 -070080 private View mSearchPlate;
Amith Yamasani968ec932010-12-02 14:00:47 -080081 private SearchView mSearchView;
Romain Guyf4f70462009-06-26 16:55:54 -070082 private Drawable mWorkingSpinner;
Amith Yamasani24652982011-06-23 16:16:05 -070083 private View mCloseSearch;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
85 // interaction with searchable application
Karl Rosaen875d50a2009-04-23 19:00:21 -070086 private SearchableInfo mSearchable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 private ComponentName mLaunchComponent;
88 private Bundle mAppSearchData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 private Context mActivityContext;
90
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 // For voice searching
Bryan Mawhinneyf36740b2009-09-30 15:20:22 +010092 private final Intent mVoiceWebSearchIntent;
93 private final Intent mVoiceAppSearchIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
Karl Rosaen875d50a2009-04-23 19:00:21 -070095 // The query entered by the user. This is not changed when selecting a suggestion
96 // that modifies the contents of the text field. But if the user then edits
97 // the suggestion, the resulting string is saved.
98 private String mUserQuery;
Satish Sampath662df0b2009-06-22 23:16:07 +010099
100 // Last known IME options value for the search edit text.
101 private int mSearchAutoCompleteImeOptions;
102
Amith Yamasani49860442010-03-17 20:54:10 -0700103 private BroadcastReceiver mConfChangeListener = new BroadcastReceiver() {
104 @Override
105 public void onReceive(Context context, Intent intent) {
106 if (intent.getAction().equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
107 onConfigurationChanged();
108 }
109 }
110 };
111
Adam Powell8abebcd2011-05-19 18:46:47 -0700112 static int resolveDialogTheme(Context context) {
113 TypedValue outValue = new TypedValue();
114 context.getTheme().resolveAttribute(com.android.internal.R.attr.searchDialogTheme,
115 outValue, true);
116 return outValue.resourceId;
117 }
118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 /**
120 * Constructor - fires it up and makes it look like the search UI.
121 *
122 * @param context Application Context we can use for system acess
123 */
Amith Yamasanie9ce3f02010-01-25 09:15:50 -0800124 public SearchDialog(Context context, SearchManager searchManager) {
Adam Powell8abebcd2011-05-19 18:46:47 -0700125 super(context, resolveDialogTheme(context));
Bryan Mawhinneyf36740b2009-09-30 15:20:22 +0100126
127 // Save voice intent for later queries/launching
128 mVoiceWebSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
129 mVoiceWebSearchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
130 mVoiceWebSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
131 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
132
133 mVoiceAppSearchIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
134 mVoiceAppSearchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 }
136
137 /**
Bryan Mawhinneyf36740b2009-09-30 15:20:22 +0100138 * Create the search dialog and any resources that are used for the
139 * entire lifetime of the dialog.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 */
141 @Override
142 protected void onCreate(Bundle savedInstanceState) {
143 super.onCreate(savedInstanceState);
144
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100145 Window theWindow = getWindow();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 WindowManager.LayoutParams lp = theWindow.getAttributes();
Romain Guy980a9382010-01-08 15:06:28 -0800147 lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100148 // taking up the whole window (even when transparent) is less than ideal,
149 // but necessary to show the popup window until the window manager supports
150 // having windows anchored by their parent but not clipped by them.
Romain Guy980a9382010-01-08 15:06:28 -0800151 lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100152 lp.gravity = Gravity.TOP | Gravity.FILL_HORIZONTAL;
Mike LeBeau98acd542009-05-07 19:04:39 -0700153 lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 theWindow.setAttributes(lp);
155
Bryan Mawhinneyf36740b2009-09-30 15:20:22 +0100156 // Touching outside of the search dialog will dismiss it
157 setCanceledOnTouchOutside(true);
158 }
159
160 /**
161 * We recreate the dialog view each time it becomes visible so as to limit
162 * the scope of any problems with the contained resources.
163 */
164 private void createContentView() {
165 setContentView(com.android.internal.R.layout.search_bar);
166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 // get the view elements for local access
Amith Yamasani968ec932010-12-02 14:00:47 -0800168 mSearchView = (SearchView) findViewById(com.android.internal.R.id.search_view);
Amith Yamasanib4569fb2011-07-08 15:25:39 -0700169 mSearchView.setIconified(false);
Amith Yamasani968ec932010-12-02 14:00:47 -0800170 mSearchView.setOnCloseListener(mOnCloseListener);
Adam Powell01f21352011-01-20 18:30:10 -0800171 mSearchView.setOnQueryTextListener(mOnQueryChangeListener);
172 mSearchView.setOnSuggestionListener(mOnSuggestionSelectionListener);
Amith Yamasani6a7421b2011-07-27 11:55:53 -0700173 mSearchView.onActionViewExpanded();
Bjorn Bringert077357c2009-09-23 13:57:24 +0100174
Amith Yamasani24652982011-06-23 16:16:05 -0700175 mCloseSearch = findViewById(com.android.internal.R.id.closeButton);
176 mCloseSearch.setOnClickListener(new View.OnClickListener() {
177 @Override
178 public void onClick(View v) {
179 dismiss();
180 }
181 });
182
Amith Yamasani968ec932010-12-02 14:00:47 -0800183 // TODO: Move the badge logic to SearchView or move the badge to search_bar.xml
184 mBadgeLabel = (TextView) mSearchView.findViewById(com.android.internal.R.id.search_badge);
185 mSearchAutoComplete = (AutoCompleteTextView)
186 mSearchView.findViewById(com.android.internal.R.id.search_src_text);
Bjorn Bringertda5c1bd2010-02-04 15:11:25 +0000187 mAppIcon = (ImageView) findViewById(com.android.internal.R.id.search_app_icon);
Amith Yamasani968ec932010-12-02 14:00:47 -0800188 mSearchPlate = mSearchView.findViewById(com.android.internal.R.id.search_plate);
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800189 mWorkingSpinner = getContext().getDrawable(com.android.internal.R.drawable.search_spinner);
Amith Yamasania40d7ec2011-07-01 13:50:04 -0700190 // TODO: Restore the spinner for slow suggestion lookups
191 // mSearchAutoComplete.setCompoundDrawablesWithIntrinsicBounds(
192 // null, null, mWorkingSpinner, null);
Bryan Mawhinneydfcbc042009-09-17 21:31:16 +0100193 setWorking(false);
194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 // pre-hide all the extraneous elements
196 mBadgeLabel.setVisibility(View.GONE);
197
198 // Additional adjustments to make Dialog work for Search
Satish Sampath662df0b2009-06-22 23:16:07 +0100199 mSearchAutoCompleteImeOptions = mSearchAutoComplete.getImeOptions();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 }
201
202 /**
203 * Set up the search dialog
204 *
Karl Rosaen875d50a2009-04-23 19:00:21 -0700205 * @return true if search dialog launched, false if not
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 */
207 public boolean show(String initialQuery, boolean selectInitialQuery,
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800208 ComponentName componentName, Bundle appSearchData) {
209 boolean success = doShow(initialQuery, selectInitialQuery, componentName, appSearchData);
Satish Sampathfef8d3e2009-07-01 17:48:42 +0100210 if (success) {
211 // Display the drop down as soon as possible instead of waiting for the rest of the
212 // pending UI stuff to get done, so that things appear faster to the user.
213 mSearchAutoComplete.showDropDownAfterLayout();
214 }
215 return success;
Mike LeBeaub3aab692009-04-30 02:09:09 -0700216 }
Bjorn Bringertda1f4b62009-07-28 15:15:26 +0100217
Mike LeBeaub3aab692009-04-30 02:09:09 -0700218 /**
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800219 * Does the rest of the work required to show the search dialog. Called by
220 * {@link #show(String, boolean, ComponentName, Bundle)} and
221 *
Mike LeBeaub3aab692009-04-30 02:09:09 -0700222 * @return true if search dialog showed, false if not
223 */
224 private boolean doShow(String initialQuery, boolean selectInitialQuery,
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800225 ComponentName componentName, Bundle appSearchData) {
Karl Rosaen875d50a2009-04-23 19:00:21 -0700226 // set up the searchable and show the dialog
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800227 if (!show(componentName, appSearchData)) {
Karl Rosaen875d50a2009-04-23 19:00:21 -0700228 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 }
Mike LeBeaub3aab692009-04-30 02:09:09 -0700230
Karl Rosaen875d50a2009-04-23 19:00:21 -0700231 // finally, load the user's initial text (which may trigger suggestions)
232 setUserQuery(initialQuery);
233 if (selectInitialQuery) {
234 mSearchAutoComplete.selectAll();
235 }
Mike LeBeaub3aab692009-04-30 02:09:09 -0700236
Karl Rosaen875d50a2009-04-23 19:00:21 -0700237 return true;
238 }
Mike LeBeaub3aab692009-04-30 02:09:09 -0700239
Karl Rosaen875d50a2009-04-23 19:00:21 -0700240 /**
241 * Sets up the search dialog and shows it.
242 *
243 * @return <code>true</code> if search dialog launched
244 */
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800245 private boolean show(ComponentName componentName, Bundle appSearchData) {
Karl Rosaen875d50a2009-04-23 19:00:21 -0700246
247 if (DBG) {
248 Log.d(LOG_TAG, "show(" + componentName + ", "
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800249 + appSearchData + ")");
Karl Rosaen875d50a2009-04-23 19:00:21 -0700250 }
251
Bjorn Bringert8d153822009-06-22 10:31:44 +0100252 SearchManager searchManager = (SearchManager)
253 mContext.getSystemService(Context.SEARCH_SERVICE);
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800254 // Try to get the searchable info for the provided component.
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000255 mSearchable = searchManager.getSearchableInfo(componentName);
Bjorn Bringertee716fa2009-07-16 09:15:37 +0100256
Bjorn Bringertee716fa2009-07-16 09:15:37 +0100257 if (mSearchable == null) {
Bjorn Bringertee716fa2009-07-16 09:15:37 +0100258 return false;
259 }
260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 mLaunchComponent = componentName;
262 mAppSearchData = appSearchData;
Karl Rosaen875d50a2009-04-23 19:00:21 -0700263 mActivityContext = mSearchable.getActivityContext(getContext());
Bryan Mawhinneyf36740b2009-09-30 15:20:22 +0100264
Karl Rosaen875d50a2009-04-23 19:00:21 -0700265 // show the dialog. this will call onStart().
Bjorn Bringert11f09bb2009-10-05 21:00:53 +0100266 if (!isShowing()) {
267 // Recreate the search bar view every time the dialog is shown, to get rid
268 // of any bad state in the AutoCompleteTextView etc
269 createContentView();
Amith Yamasani968ec932010-12-02 14:00:47 -0800270 mSearchView.setSearchableInfo(mSearchable);
Amith Yamasani940ef382011-03-02 18:43:23 -0800271 mSearchView.setAppSearchData(mAppSearchData);
Bjorn Bringert11f09bb2009-10-05 21:00:53 +0100272
Karl Rosaen875d50a2009-04-23 19:00:21 -0700273 show();
274 }
Karl Rosaen875d50a2009-04-23 19:00:21 -0700275 updateUI();
Amith Yamasani49860442010-03-17 20:54:10 -0700276
Karl Rosaen875d50a2009-04-23 19:00:21 -0700277 return true;
278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279
Amith Yamasani49860442010-03-17 20:54:10 -0700280 @Override
281 public void onStart() {
282 super.onStart();
283
284 // Register a listener for configuration change events.
285 IntentFilter filter = new IntentFilter();
286 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
287 getContext().registerReceiver(mConfChangeListener, filter);
288 }
289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 /**
291 * The search dialog is being dismissed, so handle all of the local shutdown operations.
292 *
293 * This function is designed to be idempotent so that dismiss() can be safely called at any time
294 * (even if already closed) and more likely to really dump any memory. No leaks!
295 */
296 @Override
297 public void onStop() {
298 super.onStop();
Bjorn Bringert444c7272009-07-06 21:32:50 +0100299
Amith Yamasani49860442010-03-17 20:54:10 -0700300 getContext().unregisterReceiver(mConfChangeListener);
301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 // dump extra memory we're hanging on to
303 mLaunchComponent = null;
304 mAppSearchData = null;
305 mSearchable = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 mUserQuery = null;
Karl Rosaen875d50a2009-04-23 19:00:21 -0700307 }
Mike LeBeau1c690752009-05-20 20:20:26 -0700308
Mike LeBeau1c690752009-05-20 20:20:26 -0700309 /**
Mike LeBeau1480eb22009-05-20 17:22:13 -0700310 * Sets the search dialog to the 'working' state, which shows a working spinner in the
311 * right hand size of the text field.
312 *
313 * @param working true to show spinner, false to hide spinner
314 */
315 public void setWorking(boolean working) {
Bryan Mawhinneydfcbc042009-09-17 21:31:16 +0100316 mWorkingSpinner.setAlpha(working ? 255 : 0);
317 mWorkingSpinner.setVisible(working, false);
318 mWorkingSpinner.invalidateSelf();
Mike LeBeau1480eb22009-05-20 17:22:13 -0700319 }
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800320
Mike LeBeau1480eb22009-05-20 17:22:13 -0700321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 * Save the minimal set of data necessary to recreate the search
323 *
Bjorn Bringert444c7272009-07-06 21:32:50 +0100324 * @return A bundle with the state of the dialog, or {@code null} if the search
325 * dialog is not showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 */
327 @Override
328 public Bundle onSaveInstanceState() {
Bjorn Bringert444c7272009-07-06 21:32:50 +0100329 if (!isShowing()) return null;
330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 Bundle bundle = new Bundle();
Bjorn Bringert444c7272009-07-06 21:32:50 +0100332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 // setup info so I can recreate this particular search
334 bundle.putParcelable(INSTANCE_KEY_COMPONENT, mLaunchComponent);
335 bundle.putBundle(INSTANCE_KEY_APPDATA, mAppSearchData);
Bjorn Bringertb0ae27f2009-06-23 13:47:31 +0100336 bundle.putString(INSTANCE_KEY_USER_QUERY, mUserQuery);
337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 return bundle;
339 }
340
341 /**
342 * Restore the state of the dialog from a previously saved bundle.
Karl Rosaen875d50a2009-04-23 19:00:21 -0700343 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 * @param savedInstanceState The state of the dialog previously saved by
345 * {@link #onSaveInstanceState()}.
346 */
347 @Override
348 public void onRestoreInstanceState(Bundle savedInstanceState) {
Bjorn Bringert444c7272009-07-06 21:32:50 +0100349 if (savedInstanceState == null) return;
350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 ComponentName launchComponent = savedInstanceState.getParcelable(INSTANCE_KEY_COMPONENT);
352 Bundle appSearchData = savedInstanceState.getBundle(INSTANCE_KEY_APPDATA);
Bjorn Bringertb0ae27f2009-06-23 13:47:31 +0100353 String userQuery = savedInstanceState.getString(INSTANCE_KEY_USER_QUERY);
354
Bjorn Bringertb0ae27f2009-06-23 13:47:31 +0100355 // show the dialog.
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800356 if (!doShow(userQuery, false, launchComponent, appSearchData)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 // for some reason, we couldn't re-instantiate
358 return;
359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 }
Amith Yamasani968ec932010-12-02 14:00:47 -0800361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 /**
Karl Rosaen875d50a2009-04-23 19:00:21 -0700363 * Called after resources have changed, e.g. after screen rotation or locale change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 */
Bjorn Bringert444c7272009-07-06 21:32:50 +0100365 public void onConfigurationChanged() {
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800366 if (mSearchable != null && isShowing()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 // Redraw (resources may have changed)
Mike LeBeau1fd73232009-04-27 19:12:05 -0700368 updateSearchAppIcon();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 updateSearchBadge();
Amith Yamasanid25eb352010-03-10 21:09:28 -0800370 if (isLandscapeMode(getContext())) {
371 mSearchAutoComplete.ensureImeVisible(true);
372 }
Amith Yamasanid25eb352010-03-10 21:09:28 -0800373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
Amith Yamasanid25eb352010-03-10 21:09:28 -0800375
376 static boolean isLandscapeMode(Context context) {
377 return context.getResources().getConfiguration().orientation
378 == Configuration.ORIENTATION_LANDSCAPE;
379 }
380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 /**
Karl Rosaen875d50a2009-04-23 19:00:21 -0700382 * Update the UI according to the info in the current value of {@link #mSearchable}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 */
Karl Rosaen875d50a2009-04-23 19:00:21 -0700384 private void updateUI() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 if (mSearchable != null) {
Karl Rosaenea52d292009-07-20 09:26:10 -0700386 mDecor.setVisibility(View.VISIBLE);
Karl Rosaen875d50a2009-04-23 19:00:21 -0700387 updateSearchAutoComplete();
Mike LeBeau1fd73232009-04-27 19:12:05 -0700388 updateSearchAppIcon();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 updateSearchBadge();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390
391 // In order to properly configure the input method (if one is being used), we
392 // need to let it know if we'll be providing suggestions. Although it would be
393 // difficult/expensive to know if every last detail has been configured properly, we
394 // can at least see if a suggestions provider has been configured, and use that
395 // as our trigger.
396 int inputType = mSearchable.getInputType();
397 // We only touch this if the input type is set up for text (which it almost certainly
398 // should be, in the case of search!)
399 if ((inputType & InputType.TYPE_MASK_CLASS) == InputType.TYPE_CLASS_TEXT) {
400 // The existence of a suggestions authority is the proxy for "suggestions
401 // are available here"
402 inputType &= ~InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE;
403 if (mSearchable.getSuggestAuthority() != null) {
404 inputType |= InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE;
405 }
406 }
Karl Rosaen875d50a2009-04-23 19:00:21 -0700407 mSearchAutoComplete.setInputType(inputType);
Satish Sampath662df0b2009-06-22 23:16:07 +0100408 mSearchAutoCompleteImeOptions = mSearchable.getImeOptions();
409 mSearchAutoComplete.setImeOptions(mSearchAutoCompleteImeOptions);
Mike LeBeau7df84612009-10-30 16:11:40 -0700410
411 // If the search dialog is going to show a voice search button, then don't let
412 // the soft keyboard display a microphone button if it would have otherwise.
413 if (mSearchable.getVoiceSearchEnabled()) {
414 mSearchAutoComplete.setPrivateImeOptions(IME_OPTION_NO_MICROPHONE);
415 } else {
416 mSearchAutoComplete.setPrivateImeOptions(null);
417 }
Karl Rosaen875d50a2009-04-23 19:00:21 -0700418 }
419 }
420
421 /**
422 * Updates the auto-complete text view.
423 */
424 private void updateSearchAutoComplete() {
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100425 // we dismiss the entire dialog instead
426 mSearchAutoComplete.setDropDownDismissedOnCompletion(false);
Amith Yamasani968ec932010-12-02 14:00:47 -0800427 mSearchAutoComplete.setForceIgnoreOutsideTouch(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429
Mike LeBeau1fd73232009-04-27 19:12:05 -0700430 private void updateSearchAppIcon() {
Amith Yamasani968ec932010-12-02 14:00:47 -0800431 PackageManager pm = getContext().getPackageManager();
432 Drawable icon;
433 try {
434 ActivityInfo info = pm.getActivityInfo(mLaunchComponent, 0);
435 icon = pm.getApplicationIcon(info.applicationInfo);
436 if (DBG)
437 Log.d(LOG_TAG, "Using app-specific icon");
438 } catch (NameNotFoundException e) {
439 icon = pm.getDefaultActivityIcon();
440 Log.w(LOG_TAG, mLaunchComponent + " not found, using generic app icon");
Mike LeBeau1fd73232009-04-27 19:12:05 -0700441 }
Amith Yamasani968ec932010-12-02 14:00:47 -0800442 mAppIcon.setImageDrawable(icon);
443 mAppIcon.setVisibility(View.VISIBLE);
444 mSearchPlate.setPadding(SEARCH_PLATE_LEFT_PADDING_NON_GLOBAL, mSearchPlate.getPaddingTop(), mSearchPlate.getPaddingRight(), mSearchPlate.getPaddingBottom());
Mike LeBeau1fd73232009-04-27 19:12:05 -0700445 }
446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 /**
Karl Rosaen875d50a2009-04-23 19:00:21 -0700448 * Setup the search "Badge" if requested by mode flags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 */
450 private void updateSearchBadge() {
451 // assume both hidden
452 int visibility = View.GONE;
453 Drawable icon = null;
Karl Rosaen875d50a2009-04-23 19:00:21 -0700454 CharSequence text = null;
Amith Yamasani968ec932010-12-02 14:00:47 -0800455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 // optionally show one or the other.
Bjorn Bringerta9204132009-05-05 14:06:35 +0100457 if (mSearchable.useBadgeIcon()) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800458 icon = mActivityContext.getDrawable(mSearchable.getIconId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 visibility = View.VISIBLE;
Karl Rosaen875d50a2009-04-23 19:00:21 -0700460 if (DBG) Log.d(LOG_TAG, "Using badge icon: " + mSearchable.getIconId());
Bjorn Bringerta9204132009-05-05 14:06:35 +0100461 } else if (mSearchable.useBadgeLabel()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 text = mActivityContext.getResources().getText(mSearchable.getLabelId()).toString();
463 visibility = View.VISIBLE;
Karl Rosaen875d50a2009-04-23 19:00:21 -0700464 if (DBG) Log.d(LOG_TAG, "Using badge label: " + mSearchable.getLabelId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
Amith Yamasani968ec932010-12-02 14:00:47 -0800466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 mBadgeLabel.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
468 mBadgeLabel.setText(text);
469 mBadgeLabel.setVisibility(visibility);
470 }
471
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800472 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 * Listeners of various types
474 */
475
476 /**
Karl Rosaen875d50a2009-04-23 19:00:21 -0700477 * {@link Dialog#onTouchEvent(MotionEvent)} will cancel the dialog only when the
478 * touch is outside the window. But the window includes space for the drop-down,
479 * so we also cancel on taps outside the search bar when the drop-down is not showing.
480 */
481 @Override
482 public boolean onTouchEvent(MotionEvent event) {
483 // cancel if the drop-down is not showing and the touch event was outside the search plate
484 if (!mSearchAutoComplete.isPopupShowing() && isOutOfBounds(mSearchPlate, event)) {
485 if (DBG) Log.d(LOG_TAG, "Pop-up not showing and outside of search plate.");
486 cancel();
487 return true;
488 }
489 // Let Dialog handle events outside the window while the pop-up is showing.
490 return super.onTouchEvent(event);
491 }
Amith Yamasani968ec932010-12-02 14:00:47 -0800492
Karl Rosaen875d50a2009-04-23 19:00:21 -0700493 private boolean isOutOfBounds(View v, MotionEvent event) {
494 final int x = (int) event.getX();
495 final int y = (int) event.getY();
496 final int slop = ViewConfiguration.get(mContext).getScaledWindowTouchSlop();
497 return (x < -slop) || (y < -slop)
498 || (x > (v.getWidth()+slop))
499 || (y > (v.getHeight()+slop));
500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501
Dianne Hackborna8f556e2009-03-24 20:47:50 -0700502 @Override
Karl Rosaenea52d292009-07-20 09:26:10 -0700503 public void hide() {
Bjorn Bringert444c7272009-07-06 21:32:50 +0100504 if (!isShowing()) return;
505
Dianne Hackborna8f556e2009-03-24 20:47:50 -0700506 // We made sure the IME was displayed, so also make sure it is closed
507 // when we go away.
Yohei Yukawa777ef952015-11-25 20:32:24 -0800508 InputMethodManager imm = getContext().getSystemService(InputMethodManager.class);
Dianne Hackborna8f556e2009-03-24 20:47:50 -0700509 if (imm != null) {
510 imm.hideSoftInputFromWindow(
511 getWindow().getDecorView().getWindowToken(), 0);
512 }
Karl Rosaenea52d292009-07-20 09:26:10 -0700513
514 super.hide();
Dianne Hackborna8f556e2009-03-24 20:47:50 -0700515 }
Karl Rosaen875d50a2009-04-23 19:00:21 -0700516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 /**
Karl Rosaen875d50a2009-04-23 19:00:21 -0700518 * Launch a search for the text in the query text field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 */
Amith Yamasani968ec932010-12-02 14:00:47 -0800520 public void launchQuerySearch() {
Karl Rosaen875d50a2009-04-23 19:00:21 -0700521 launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 }
523
524 /**
Karl Rosaen875d50a2009-04-23 19:00:21 -0700525 * Launch a search for the text in the query text field.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 *
Karl Rosaen875d50a2009-04-23 19:00:21 -0700527 * @param actionKey The key code of the action key that was pressed,
528 * or {@link KeyEvent#KEYCODE_UNKNOWN} if none.
529 * @param actionMsg The message for the action key that was pressed,
530 * or <code>null</code> if none.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 */
Amith Yamasani968ec932010-12-02 14:00:47 -0800532 protected void launchQuerySearch(int actionKey, String actionMsg) {
Karl Rosaen875d50a2009-04-23 19:00:21 -0700533 String query = mSearchAutoComplete.getText().toString();
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800534 String action = Intent.ACTION_SEARCH;
Amith Yamasani968ec932010-12-02 14:00:47 -0800535 Intent intent = createIntent(action, null, null, query, actionKey, actionMsg);
Karl Rosaen875d50a2009-04-23 19:00:21 -0700536 launchIntent(intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 }
Karl Rosaena058f022009-06-01 23:11:44 +0100538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 /**
Amith Yamasanie9ce3f02010-01-25 09:15:50 -0800540 * Launches an intent, including any special intent handling.
Karl Rosaen875d50a2009-04-23 19:00:21 -0700541 */
542 private void launchIntent(Intent intent) {
543 if (intent == null) {
544 return;
545 }
Karl Rosaenea52d292009-07-20 09:26:10 -0700546 Log.d(LOG_TAG, "launching " + intent);
Bjorn Bringert4899e382009-07-22 10:25:25 +0100547 try {
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800548 // If the intent was created from a suggestion, it will always have an explicit
549 // component here.
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800550 getContext().startActivity(intent);
551 // If the search switches to a different activity,
552 // SearchDialogWrapper#performActivityResuming
553 // will handle hiding the dialog when the next activity starts, but for
554 // real in-app search, we still need to dismiss the dialog.
555 dismiss();
Bjorn Bringert4899e382009-07-22 10:25:25 +0100556 } catch (RuntimeException ex) {
557 Log.e(LOG_TAG, "Failed launch activity: " + intent, ex);
Karl Rosaen876627d2009-07-20 14:30:55 -0700558 }
Karl Rosaen875d50a2009-04-23 19:00:21 -0700559 }
Bjorn Bringert4899e382009-07-22 10:25:25 +0100560
Bjorn Bringert4899e382009-07-22 10:25:25 +0100561 /**
Mike LeBeauae9760b2009-06-01 21:53:09 +0100562 * Sets the list item selection in the AutoCompleteTextView's ListView.
563 */
564 public void setListSelection(int index) {
565 mSearchAutoComplete.setListSelection(index);
566 }
Karl Rosaena058f022009-06-01 23:11:44 +0100567
Mike LeBeauae9760b2009-06-01 21:53:09 +0100568 /**
Karl Rosaen875d50a2009-04-23 19:00:21 -0700569 * Constructs an intent from the given information and the search dialog state.
570 *
571 * @param action Intent action.
572 * @param data Intent data, or <code>null</code>.
Karl Rosaen875d50a2009-04-23 19:00:21 -0700573 * @param extraData Data for {@link SearchManager#EXTRA_DATA_KEY} or <code>null</code>.
Satish Sampathbf23fe02009-06-15 23:47:56 +0100574 * @param query Intent query, or <code>null</code>.
Karl Rosaen875d50a2009-04-23 19:00:21 -0700575 * @param actionKey The key code of the action key that was pressed,
576 * or {@link KeyEvent#KEYCODE_UNKNOWN} if none.
577 * @param actionMsg The message for the action key that was pressed,
578 * or <code>null</code> if none.
Bjorn Bringertbe5b73c2009-09-21 23:05:23 +0100579 * @param mode The search mode, one of the acceptable values for
580 * {@link SearchManager#SEARCH_MODE}, or {@code null}.
Karl Rosaen875d50a2009-04-23 19:00:21 -0700581 * @return The intent.
582 */
Satish Sampathbf23fe02009-06-15 23:47:56 +0100583 private Intent createIntent(String action, Uri data, String extraData, String query,
Amith Yamasanie678f462010-09-15 16:13:43 -0700584 int actionKey, String actionMsg) {
Karl Rosaen875d50a2009-04-23 19:00:21 -0700585 // Now build the Intent
586 Intent intent = new Intent(action);
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100587 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bjorn Bringert442da482009-09-22 13:15:33 +0100588 // We need CLEAR_TOP to avoid reusing an old task that has other activities
Evan Millardc1396c2009-11-06 11:59:39 -0800589 // on top of the one we want. We don't want to do this in in-app search though,
590 // as it can be destructive to the activity stack.
Karl Rosaen875d50a2009-04-23 19:00:21 -0700591 if (data != null) {
592 intent.setData(data);
593 }
Bjorn Bringert5f806052009-06-24 12:02:26 +0100594 intent.putExtra(SearchManager.USER_QUERY, mUserQuery);
Karl Rosaen875d50a2009-04-23 19:00:21 -0700595 if (query != null) {
596 intent.putExtra(SearchManager.QUERY, query);
597 }
598 if (extraData != null) {
599 intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
600 }
601 if (mAppSearchData != null) {
602 intent.putExtra(SearchManager.APP_DATA, mAppSearchData);
603 }
604 if (actionKey != KeyEvent.KEYCODE_UNKNOWN) {
605 intent.putExtra(SearchManager.ACTION_KEY, actionKey);
606 intent.putExtra(SearchManager.ACTION_MSG, actionMsg);
607 }
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800608 intent.setComponent(mSearchable.getSearchActivity());
Karl Rosaen875d50a2009-04-23 19:00:21 -0700609 return intent;
610 }
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800611
Karl Rosaen875d50a2009-04-23 19:00:21 -0700612 /**
Bjorn Bringert077357c2009-09-23 13:57:24 +0100613 * The root element in the search bar layout. This is a custom view just to override
614 * the handling of the back button.
615 */
616 public static class SearchBar extends LinearLayout {
617
Bjorn Bringert077357c2009-09-23 13:57:24 +0100618 public SearchBar(Context context, AttributeSet attrs) {
619 super(context, attrs);
620 }
621
622 public SearchBar(Context context) {
623 super(context);
624 }
625
Clara Bayarri4423d912015-03-02 19:42:48 +0000626 @Override
627 public ActionMode startActionModeForChild(
628 View child, ActionMode.Callback callback, int type) {
Clara Bayarri0433ae62015-04-14 15:51:38 +0100629 // Disable Primary Action Modes in the SearchBar, as they overlap.
630 if (type != ActionMode.TYPE_PRIMARY) {
631 return super.startActionModeForChild(child, callback, type);
632 }
Clara Bayarri4423d912015-03-02 19:42:48 +0000633 return null;
634 }
Bjorn Bringert077357c2009-09-23 13:57:24 +0100635 }
636
Amith Yamasani968ec932010-12-02 14:00:47 -0800637 private boolean isEmpty(AutoCompleteTextView actv) {
638 return TextUtils.getTrimmedLength(actv.getText()) == 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 }
Bjorn Bringert87d61f22009-09-16 23:17:16 +0100640
641 @Override
642 public void onBackPressed() {
Bjorn Bringerta6309c32009-10-01 10:30:25 +0100643 // If the input method is covering the search dialog completely,
644 // e.g. in landscape mode with no hard keyboard, dismiss just the input method
Yohei Yukawa777ef952015-11-25 20:32:24 -0800645 InputMethodManager imm = getContext().getSystemService(InputMethodManager.class);
Bjorn Bringerta6309c32009-10-01 10:30:25 +0100646 if (imm != null && imm.isFullscreenMode() &&
647 imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0)) {
648 return;
649 }
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800650 // Close search dialog
651 cancel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 }
Bjorn Bringert87d61f22009-09-16 23:17:16 +0100653
Amith Yamasani968ec932010-12-02 14:00:47 -0800654 private boolean onClosePressed() {
655 // Dismiss the dialog if close button is pressed when there's no query text
656 if (isEmpty(mSearchAutoComplete)) {
657 dismiss();
658 return true;
659 }
660
661 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 }
Karl Rosaen875d50a2009-04-23 19:00:21 -0700663
Amith Yamasani968ec932010-12-02 14:00:47 -0800664 private final SearchView.OnCloseListener mOnCloseListener = new SearchView.OnCloseListener() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665
Amith Yamasani968ec932010-12-02 14:00:47 -0800666 public boolean onClose() {
667 return onClosePressed();
668 }
669 };
Karl Rosaen875d50a2009-04-23 19:00:21 -0700670
Adam Powell01f21352011-01-20 18:30:10 -0800671 private final SearchView.OnQueryTextListener mOnQueryChangeListener =
672 new SearchView.OnQueryTextListener() {
Amith Yamasani968ec932010-12-02 14:00:47 -0800673
Adam Powell01f21352011-01-20 18:30:10 -0800674 public boolean onQueryTextSubmit(String query) {
Amith Yamasani968ec932010-12-02 14:00:47 -0800675 dismiss();
676 return false;
677 }
678
Adam Powell01f21352011-01-20 18:30:10 -0800679 public boolean onQueryTextChange(String newText) {
Amith Yamasani968ec932010-12-02 14:00:47 -0800680 return false;
681 }
682 };
683
Adam Powell01f21352011-01-20 18:30:10 -0800684 private final SearchView.OnSuggestionListener mOnSuggestionSelectionListener =
685 new SearchView.OnSuggestionListener() {
Amith Yamasani968ec932010-12-02 14:00:47 -0800686
Adam Powell01f21352011-01-20 18:30:10 -0800687 public boolean onSuggestionSelect(int position) {
Amith Yamasani968ec932010-12-02 14:00:47 -0800688 return false;
689 }
690
Adam Powell01f21352011-01-20 18:30:10 -0800691 public boolean onSuggestionClick(int position) {
Amith Yamasani968ec932010-12-02 14:00:47 -0800692 dismiss();
693 return false;
694 }
695 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696
697 /**
Amith Yamasani968ec932010-12-02 14:00:47 -0800698 * Sets the text in the query box, updating the suggestions.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 */
Amith Yamasani968ec932010-12-02 14:00:47 -0800700 private void setUserQuery(String query) {
701 if (query == null) {
702 query = "";
703 }
704 mUserQuery = query;
705 mSearchAutoComplete.setText(query);
706 mSearchAutoComplete.setSelection(query.length());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 }
708}