The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app; |
| 18 | |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 19 | |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 20 | import android.annotation.UnsupportedAppUsage; |
Amith Yamasani | d25eb35 | 2010-03-10 21:09:28 -0800 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.ComponentName; |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
Amith Yamasani | d25eb35 | 2010-03-10 21:09:28 -0800 | [diff] [blame] | 25 | import android.content.IntentFilter; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 26 | import android.content.pm.ActivityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
Bjorn Bringert | c1f4096 | 2009-04-29 13:08:39 +0100 | [diff] [blame] | 28 | import android.content.pm.PackageManager.NameNotFoundException; |
Amith Yamasani | d25eb35 | 2010-03-10 21:09:28 -0800 | [diff] [blame] | 29 | import android.content.res.Configuration; |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 30 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.net.Uri; |
| 32 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.speech.RecognizerIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.text.InputType; |
| 35 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.util.AttributeSet; |
| 37 | import android.util.Log; |
Adam Powell | 8abebcd | 2011-05-19 18:46:47 -0700 | [diff] [blame] | 38 | import android.util.TypedValue; |
| 39 | import android.view.ActionMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.view.Gravity; |
| 41 | import android.view.KeyEvent; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 42 | import android.view.MotionEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.view.View; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 44 | import android.view.ViewConfiguration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.view.ViewGroup; |
| 46 | import android.view.Window; |
| 47 | import android.view.WindowManager; |
Dianne Hackborn | a8f556e | 2009-03-24 20:47:50 -0700 | [diff] [blame] | 48 | import android.view.inputmethod.InputMethodManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.widget.AutoCompleteTextView; |
Ralston Da Silva | 13b2b97 | 2019-02-14 15:43:23 -0800 | [diff] [blame] | 50 | import android.widget.Filterable; |
Bjorn Bringert | da5c1bd | 2010-02-04 15:11:25 +0000 | [diff] [blame] | 51 | import android.widget.ImageView; |
Bjorn Bringert | 077357c | 2009-09-23 13:57:24 +0100 | [diff] [blame] | 52 | import android.widget.LinearLayout; |
Ralston Da Silva | 13b2b97 | 2019-02-14 15:43:23 -0800 | [diff] [blame] | 53 | import android.widget.ListPopupWindow; |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 54 | import android.widget.SearchView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.widget.TextView; |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 56 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | /** |
Amith Yamasani | e9ce3f0 | 2010-01-25 09:15:50 -0800 | [diff] [blame] | 58 | * Search dialog. This is controlled by the |
| 59 | * SearchManager and runs in the current foreground process. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | * |
| 61 | * @hide |
| 62 | */ |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 63 | public class SearchDialog extends Dialog { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | |
| 65 | // Debugging support |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 66 | private static final boolean DBG = false; |
| 67 | private static final String LOG_TAG = "SearchDialog"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | private static final String INSTANCE_KEY_COMPONENT = "comp"; |
| 70 | private static final String INSTANCE_KEY_APPDATA = "data"; |
Bjorn Bringert | b0ae27f | 2009-06-23 13:47:31 +0100 | [diff] [blame] | 71 | private static final String INSTANCE_KEY_USER_QUERY = "uQry"; |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 72 | |
Mike LeBeau | 7df8461 | 2009-10-30 16:11:40 -0700 | [diff] [blame] | 73 | // The string used for privateImeOptions to identify to the IME that it should not show |
| 74 | // a microphone button since one already exists in the search dialog. |
| 75 | private static final String IME_OPTION_NO_MICROPHONE = "nm"; |
Bjorn Bringert | b0ae27f | 2009-06-23 13:47:31 +0100 | [diff] [blame] | 76 | |
Mike LeBeau | 1fd7323 | 2009-04-27 19:12:05 -0700 | [diff] [blame] | 77 | private static final int SEARCH_PLATE_LEFT_PADDING_NON_GLOBAL = 7; |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 78 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | // views & widgets |
| 80 | private TextView mBadgeLabel; |
Bjorn Bringert | da5c1bd | 2010-02-04 15:11:25 +0000 | [diff] [blame] | 81 | private ImageView mAppIcon; |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 82 | private AutoCompleteTextView mSearchAutoComplete; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 83 | private View mSearchPlate; |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 84 | private SearchView mSearchView; |
Romain Guy | f4f7046 | 2009-06-26 16:55:54 -0700 | [diff] [blame] | 85 | private Drawable mWorkingSpinner; |
Amith Yamasani | 2465298 | 2011-06-23 16:16:05 -0700 | [diff] [blame] | 86 | private View mCloseSearch; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | |
| 88 | // interaction with searchable application |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 89 | private SearchableInfo mSearchable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | private ComponentName mLaunchComponent; |
| 91 | private Bundle mAppSearchData; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | private Context mActivityContext; |
| 93 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | // For voice searching |
Bryan Mawhinney | f36740b | 2009-09-30 15:20:22 +0100 | [diff] [blame] | 95 | private final Intent mVoiceWebSearchIntent; |
| 96 | private final Intent mVoiceAppSearchIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 98 | // The query entered by the user. This is not changed when selecting a suggestion |
| 99 | // that modifies the contents of the text field. But if the user then edits |
| 100 | // the suggestion, the resulting string is saved. |
| 101 | private String mUserQuery; |
Satish Sampath | 662df0b | 2009-06-22 23:16:07 +0100 | [diff] [blame] | 102 | |
| 103 | // Last known IME options value for the search edit text. |
| 104 | private int mSearchAutoCompleteImeOptions; |
| 105 | |
Amith Yamasani | 4986044 | 2010-03-17 20:54:10 -0700 | [diff] [blame] | 106 | private BroadcastReceiver mConfChangeListener = new BroadcastReceiver() { |
| 107 | @Override |
| 108 | public void onReceive(Context context, Intent intent) { |
| 109 | if (intent.getAction().equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
| 110 | onConfigurationChanged(); |
| 111 | } |
| 112 | } |
| 113 | }; |
| 114 | |
Adam Powell | 8abebcd | 2011-05-19 18:46:47 -0700 | [diff] [blame] | 115 | static int resolveDialogTheme(Context context) { |
| 116 | TypedValue outValue = new TypedValue(); |
| 117 | context.getTheme().resolveAttribute(com.android.internal.R.attr.searchDialogTheme, |
| 118 | outValue, true); |
| 119 | return outValue.resourceId; |
| 120 | } |
| 121 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | /** |
| 123 | * Constructor - fires it up and makes it look like the search UI. |
| 124 | * |
| 125 | * @param context Application Context we can use for system acess |
| 126 | */ |
Amith Yamasani | e9ce3f0 | 2010-01-25 09:15:50 -0800 | [diff] [blame] | 127 | public SearchDialog(Context context, SearchManager searchManager) { |
Adam Powell | 8abebcd | 2011-05-19 18:46:47 -0700 | [diff] [blame] | 128 | super(context, resolveDialogTheme(context)); |
Bryan Mawhinney | f36740b | 2009-09-30 15:20:22 +0100 | [diff] [blame] | 129 | |
| 130 | // Save voice intent for later queries/launching |
| 131 | mVoiceWebSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH); |
| 132 | mVoiceWebSearchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 133 | mVoiceWebSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, |
| 134 | RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); |
| 135 | |
| 136 | mVoiceAppSearchIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
| 137 | mVoiceAppSearchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
Bryan Mawhinney | f36740b | 2009-09-30 15:20:22 +0100 | [diff] [blame] | 141 | * Create the search dialog and any resources that are used for the |
| 142 | * entire lifetime of the dialog. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | */ |
| 144 | @Override |
| 145 | protected void onCreate(Bundle savedInstanceState) { |
| 146 | super.onCreate(savedInstanceState); |
| 147 | |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 148 | Window theWindow = getWindow(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | WindowManager.LayoutParams lp = theWindow.getAttributes(); |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 150 | lp.width = ViewGroup.LayoutParams.MATCH_PARENT; |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 151 | // taking up the whole window (even when transparent) is less than ideal, |
| 152 | // but necessary to show the popup window until the window manager supports |
| 153 | // having windows anchored by their parent but not clipped by them. |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 154 | lp.height = ViewGroup.LayoutParams.MATCH_PARENT; |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 155 | lp.gravity = Gravity.TOP | Gravity.FILL_HORIZONTAL; |
Mike LeBeau | 98acd54 | 2009-05-07 19:04:39 -0700 | [diff] [blame] | 156 | lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | theWindow.setAttributes(lp); |
| 158 | |
Bryan Mawhinney | f36740b | 2009-09-30 15:20:22 +0100 | [diff] [blame] | 159 | // Touching outside of the search dialog will dismiss it |
| 160 | setCanceledOnTouchOutside(true); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * We recreate the dialog view each time it becomes visible so as to limit |
| 165 | * the scope of any problems with the contained resources. |
| 166 | */ |
| 167 | private void createContentView() { |
| 168 | setContentView(com.android.internal.R.layout.search_bar); |
| 169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | // get the view elements for local access |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 171 | mSearchView = findViewById(com.android.internal.R.id.search_view); |
Amith Yamasani | b4569fb | 2011-07-08 15:25:39 -0700 | [diff] [blame] | 172 | mSearchView.setIconified(false); |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 173 | mSearchView.setOnCloseListener(mOnCloseListener); |
Adam Powell | 01f2135 | 2011-01-20 18:30:10 -0800 | [diff] [blame] | 174 | mSearchView.setOnQueryTextListener(mOnQueryChangeListener); |
| 175 | mSearchView.setOnSuggestionListener(mOnSuggestionSelectionListener); |
Amith Yamasani | 6a7421b | 2011-07-27 11:55:53 -0700 | [diff] [blame] | 176 | mSearchView.onActionViewExpanded(); |
Bjorn Bringert | 077357c | 2009-09-23 13:57:24 +0100 | [diff] [blame] | 177 | |
Amith Yamasani | 2465298 | 2011-06-23 16:16:05 -0700 | [diff] [blame] | 178 | mCloseSearch = findViewById(com.android.internal.R.id.closeButton); |
| 179 | mCloseSearch.setOnClickListener(new View.OnClickListener() { |
| 180 | @Override |
| 181 | public void onClick(View v) { |
| 182 | dismiss(); |
| 183 | } |
| 184 | }); |
| 185 | |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 186 | // TODO: Move the badge logic to SearchView or move the badge to search_bar.xml |
| 187 | mBadgeLabel = (TextView) mSearchView.findViewById(com.android.internal.R.id.search_badge); |
| 188 | mSearchAutoComplete = (AutoCompleteTextView) |
| 189 | mSearchView.findViewById(com.android.internal.R.id.search_src_text); |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 190 | mAppIcon = findViewById(com.android.internal.R.id.search_app_icon); |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 191 | mSearchPlate = mSearchView.findViewById(com.android.internal.R.id.search_plate); |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 192 | mWorkingSpinner = getContext().getDrawable(com.android.internal.R.drawable.search_spinner); |
Amith Yamasani | a40d7ec | 2011-07-01 13:50:04 -0700 | [diff] [blame] | 193 | // TODO: Restore the spinner for slow suggestion lookups |
| 194 | // mSearchAutoComplete.setCompoundDrawablesWithIntrinsicBounds( |
| 195 | // null, null, mWorkingSpinner, null); |
Bryan Mawhinney | dfcbc04 | 2009-09-17 21:31:16 +0100 | [diff] [blame] | 196 | setWorking(false); |
| 197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | // pre-hide all the extraneous elements |
| 199 | mBadgeLabel.setVisibility(View.GONE); |
| 200 | |
| 201 | // Additional adjustments to make Dialog work for Search |
Satish Sampath | 662df0b | 2009-06-22 23:16:07 +0100 | [diff] [blame] | 202 | mSearchAutoCompleteImeOptions = mSearchAutoComplete.getImeOptions(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Set up the search dialog |
| 207 | * |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 208 | * @return true if search dialog launched, false if not |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | */ |
| 210 | public boolean show(String initialQuery, boolean selectInitialQuery, |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 211 | ComponentName componentName, Bundle appSearchData) { |
| 212 | boolean success = doShow(initialQuery, selectInitialQuery, componentName, appSearchData); |
Satish Sampath | fef8d3e | 2009-07-01 17:48:42 +0100 | [diff] [blame] | 213 | if (success) { |
| 214 | // Display the drop down as soon as possible instead of waiting for the rest of the |
| 215 | // pending UI stuff to get done, so that things appear faster to the user. |
| 216 | mSearchAutoComplete.showDropDownAfterLayout(); |
| 217 | } |
| 218 | return success; |
Mike LeBeau | b3aab69 | 2009-04-30 02:09:09 -0700 | [diff] [blame] | 219 | } |
Bjorn Bringert | da1f4b6 | 2009-07-28 15:15:26 +0100 | [diff] [blame] | 220 | |
Mike LeBeau | b3aab69 | 2009-04-30 02:09:09 -0700 | [diff] [blame] | 221 | /** |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 222 | * Does the rest of the work required to show the search dialog. Called by |
| 223 | * {@link #show(String, boolean, ComponentName, Bundle)} and |
| 224 | * |
Mike LeBeau | b3aab69 | 2009-04-30 02:09:09 -0700 | [diff] [blame] | 225 | * @return true if search dialog showed, false if not |
| 226 | */ |
| 227 | private boolean doShow(String initialQuery, boolean selectInitialQuery, |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 228 | ComponentName componentName, Bundle appSearchData) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 229 | // set up the searchable and show the dialog |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 230 | if (!show(componentName, appSearchData)) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 231 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | } |
Mike LeBeau | b3aab69 | 2009-04-30 02:09:09 -0700 | [diff] [blame] | 233 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 234 | // finally, load the user's initial text (which may trigger suggestions) |
| 235 | setUserQuery(initialQuery); |
| 236 | if (selectInitialQuery) { |
| 237 | mSearchAutoComplete.selectAll(); |
| 238 | } |
Mike LeBeau | b3aab69 | 2009-04-30 02:09:09 -0700 | [diff] [blame] | 239 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 240 | return true; |
| 241 | } |
Mike LeBeau | b3aab69 | 2009-04-30 02:09:09 -0700 | [diff] [blame] | 242 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 243 | /** |
| 244 | * Sets up the search dialog and shows it. |
| 245 | * |
| 246 | * @return <code>true</code> if search dialog launched |
| 247 | */ |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 248 | private boolean show(ComponentName componentName, Bundle appSearchData) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 249 | |
| 250 | if (DBG) { |
| 251 | Log.d(LOG_TAG, "show(" + componentName + ", " |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 252 | + appSearchData + ")"); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Bjorn Bringert | 8d15382 | 2009-06-22 10:31:44 +0100 | [diff] [blame] | 255 | SearchManager searchManager = (SearchManager) |
| 256 | mContext.getSystemService(Context.SEARCH_SERVICE); |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 257 | // Try to get the searchable info for the provided component. |
Bjorn Bringert | 6cf7a32 | 2010-02-23 13:17:06 +0000 | [diff] [blame] | 258 | mSearchable = searchManager.getSearchableInfo(componentName); |
Bjorn Bringert | ee716fa | 2009-07-16 09:15:37 +0100 | [diff] [blame] | 259 | |
Bjorn Bringert | ee716fa | 2009-07-16 09:15:37 +0100 | [diff] [blame] | 260 | if (mSearchable == null) { |
Bjorn Bringert | ee716fa | 2009-07-16 09:15:37 +0100 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | mLaunchComponent = componentName; |
| 265 | mAppSearchData = appSearchData; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 266 | mActivityContext = mSearchable.getActivityContext(getContext()); |
Bryan Mawhinney | f36740b | 2009-09-30 15:20:22 +0100 | [diff] [blame] | 267 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 268 | // show the dialog. this will call onStart(). |
Bjorn Bringert | 11f09bb | 2009-10-05 21:00:53 +0100 | [diff] [blame] | 269 | if (!isShowing()) { |
| 270 | // Recreate the search bar view every time the dialog is shown, to get rid |
| 271 | // of any bad state in the AutoCompleteTextView etc |
| 272 | createContentView(); |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 273 | mSearchView.setSearchableInfo(mSearchable); |
Amith Yamasani | 940ef38 | 2011-03-02 18:43:23 -0800 | [diff] [blame] | 274 | mSearchView.setAppSearchData(mAppSearchData); |
Bjorn Bringert | 11f09bb | 2009-10-05 21:00:53 +0100 | [diff] [blame] | 275 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 276 | show(); |
| 277 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 278 | updateUI(); |
Amith Yamasani | 4986044 | 2010-03-17 20:54:10 -0700 | [diff] [blame] | 279 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 280 | return true; |
| 281 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | |
Amith Yamasani | 4986044 | 2010-03-17 20:54:10 -0700 | [diff] [blame] | 283 | @Override |
| 284 | public void onStart() { |
| 285 | super.onStart(); |
| 286 | |
| 287 | // Register a listener for configuration change events. |
| 288 | IntentFilter filter = new IntentFilter(); |
| 289 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
| 290 | getContext().registerReceiver(mConfChangeListener, filter); |
| 291 | } |
| 292 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | /** |
| 294 | * The search dialog is being dismissed, so handle all of the local shutdown operations. |
| 295 | * |
| 296 | * This function is designed to be idempotent so that dismiss() can be safely called at any time |
| 297 | * (even if already closed) and more likely to really dump any memory. No leaks! |
| 298 | */ |
| 299 | @Override |
| 300 | public void onStop() { |
| 301 | super.onStop(); |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 302 | |
Amith Yamasani | 4986044 | 2010-03-17 20:54:10 -0700 | [diff] [blame] | 303 | getContext().unregisterReceiver(mConfChangeListener); |
| 304 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | // dump extra memory we're hanging on to |
| 306 | mLaunchComponent = null; |
| 307 | mAppSearchData = null; |
| 308 | mSearchable = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | mUserQuery = null; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 310 | } |
Mike LeBeau | 1c69075 | 2009-05-20 20:20:26 -0700 | [diff] [blame] | 311 | |
Mike LeBeau | 1c69075 | 2009-05-20 20:20:26 -0700 | [diff] [blame] | 312 | /** |
Mike LeBeau | 1480eb2 | 2009-05-20 17:22:13 -0700 | [diff] [blame] | 313 | * Sets the search dialog to the 'working' state, which shows a working spinner in the |
| 314 | * right hand size of the text field. |
| 315 | * |
| 316 | * @param working true to show spinner, false to hide spinner |
| 317 | */ |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 318 | @UnsupportedAppUsage |
Mike LeBeau | 1480eb2 | 2009-05-20 17:22:13 -0700 | [diff] [blame] | 319 | public void setWorking(boolean working) { |
Bryan Mawhinney | dfcbc04 | 2009-09-17 21:31:16 +0100 | [diff] [blame] | 320 | mWorkingSpinner.setAlpha(working ? 255 : 0); |
| 321 | mWorkingSpinner.setVisible(working, false); |
| 322 | mWorkingSpinner.invalidateSelf(); |
Mike LeBeau | 1480eb2 | 2009-05-20 17:22:13 -0700 | [diff] [blame] | 323 | } |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 324 | |
Mike LeBeau | 1480eb2 | 2009-05-20 17:22:13 -0700 | [diff] [blame] | 325 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | * Save the minimal set of data necessary to recreate the search |
| 327 | * |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 328 | * @return A bundle with the state of the dialog, or {@code null} if the search |
| 329 | * dialog is not showing. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | */ |
| 331 | @Override |
| 332 | public Bundle onSaveInstanceState() { |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 333 | if (!isShowing()) return null; |
| 334 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | Bundle bundle = new Bundle(); |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 336 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | // setup info so I can recreate this particular search |
| 338 | bundle.putParcelable(INSTANCE_KEY_COMPONENT, mLaunchComponent); |
| 339 | bundle.putBundle(INSTANCE_KEY_APPDATA, mAppSearchData); |
Bjorn Bringert | b0ae27f | 2009-06-23 13:47:31 +0100 | [diff] [blame] | 340 | bundle.putString(INSTANCE_KEY_USER_QUERY, mUserQuery); |
| 341 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | return bundle; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Restore the state of the dialog from a previously saved bundle. |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 347 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | * @param savedInstanceState The state of the dialog previously saved by |
| 349 | * {@link #onSaveInstanceState()}. |
| 350 | */ |
| 351 | @Override |
| 352 | public void onRestoreInstanceState(Bundle savedInstanceState) { |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 353 | if (savedInstanceState == null) return; |
| 354 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | ComponentName launchComponent = savedInstanceState.getParcelable(INSTANCE_KEY_COMPONENT); |
| 356 | Bundle appSearchData = savedInstanceState.getBundle(INSTANCE_KEY_APPDATA); |
Bjorn Bringert | b0ae27f | 2009-06-23 13:47:31 +0100 | [diff] [blame] | 357 | String userQuery = savedInstanceState.getString(INSTANCE_KEY_USER_QUERY); |
| 358 | |
Bjorn Bringert | b0ae27f | 2009-06-23 13:47:31 +0100 | [diff] [blame] | 359 | // show the dialog. |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 360 | if (!doShow(userQuery, false, launchComponent, appSearchData)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | // for some reason, we couldn't re-instantiate |
| 362 | return; |
| 363 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | } |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 365 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 367 | * Called after resources have changed, e.g. after screen rotation or locale change. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | */ |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 369 | public void onConfigurationChanged() { |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 370 | if (mSearchable != null && isShowing()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | // Redraw (resources may have changed) |
Mike LeBeau | 1fd7323 | 2009-04-27 19:12:05 -0700 | [diff] [blame] | 372 | updateSearchAppIcon(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | updateSearchBadge(); |
Amith Yamasani | d25eb35 | 2010-03-10 21:09:28 -0800 | [diff] [blame] | 374 | if (isLandscapeMode(getContext())) { |
Ralston Da Silva | 13b2b97 | 2019-02-14 15:43:23 -0800 | [diff] [blame] | 375 | mSearchAutoComplete.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NEEDED); |
| 376 | if (mSearchAutoComplete.isDropDownAlwaysVisible() || enoughToFilter()) { |
| 377 | mSearchAutoComplete.showDropDown(); |
| 378 | } |
Amith Yamasani | d25eb35 | 2010-03-10 21:09:28 -0800 | [diff] [blame] | 379 | } |
Amith Yamasani | d25eb35 | 2010-03-10 21:09:28 -0800 | [diff] [blame] | 380 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | } |
Amith Yamasani | d25eb35 | 2010-03-10 21:09:28 -0800 | [diff] [blame] | 382 | |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 383 | @UnsupportedAppUsage |
Amith Yamasani | d25eb35 | 2010-03-10 21:09:28 -0800 | [diff] [blame] | 384 | static boolean isLandscapeMode(Context context) { |
| 385 | return context.getResources().getConfiguration().orientation |
| 386 | == Configuration.ORIENTATION_LANDSCAPE; |
| 387 | } |
| 388 | |
Ralston Da Silva | 13b2b97 | 2019-02-14 15:43:23 -0800 | [diff] [blame] | 389 | private boolean enoughToFilter() { |
| 390 | Filterable filterableAdapter = (Filterable) mSearchAutoComplete.getAdapter(); |
| 391 | if (filterableAdapter == null || filterableAdapter.getFilter() == null) { |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | return mSearchAutoComplete.enoughToFilter(); |
| 396 | } |
| 397 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 399 | * Update the UI according to the info in the current value of {@link #mSearchable}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | */ |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 401 | private void updateUI() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | if (mSearchable != null) { |
Karl Rosaen | ea52d29 | 2009-07-20 09:26:10 -0700 | [diff] [blame] | 403 | mDecor.setVisibility(View.VISIBLE); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 404 | updateSearchAutoComplete(); |
Mike LeBeau | 1fd7323 | 2009-04-27 19:12:05 -0700 | [diff] [blame] | 405 | updateSearchAppIcon(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | updateSearchBadge(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | |
| 408 | // In order to properly configure the input method (if one is being used), we |
| 409 | // need to let it know if we'll be providing suggestions. Although it would be |
| 410 | // difficult/expensive to know if every last detail has been configured properly, we |
| 411 | // can at least see if a suggestions provider has been configured, and use that |
| 412 | // as our trigger. |
| 413 | int inputType = mSearchable.getInputType(); |
| 414 | // We only touch this if the input type is set up for text (which it almost certainly |
| 415 | // should be, in the case of search!) |
| 416 | if ((inputType & InputType.TYPE_MASK_CLASS) == InputType.TYPE_CLASS_TEXT) { |
| 417 | // The existence of a suggestions authority is the proxy for "suggestions |
| 418 | // are available here" |
| 419 | inputType &= ~InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE; |
| 420 | if (mSearchable.getSuggestAuthority() != null) { |
| 421 | inputType |= InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE; |
| 422 | } |
| 423 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 424 | mSearchAutoComplete.setInputType(inputType); |
Satish Sampath | 662df0b | 2009-06-22 23:16:07 +0100 | [diff] [blame] | 425 | mSearchAutoCompleteImeOptions = mSearchable.getImeOptions(); |
| 426 | mSearchAutoComplete.setImeOptions(mSearchAutoCompleteImeOptions); |
Mike LeBeau | 7df8461 | 2009-10-30 16:11:40 -0700 | [diff] [blame] | 427 | |
| 428 | // If the search dialog is going to show a voice search button, then don't let |
| 429 | // the soft keyboard display a microphone button if it would have otherwise. |
| 430 | if (mSearchable.getVoiceSearchEnabled()) { |
| 431 | mSearchAutoComplete.setPrivateImeOptions(IME_OPTION_NO_MICROPHONE); |
| 432 | } else { |
| 433 | mSearchAutoComplete.setPrivateImeOptions(null); |
| 434 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Updates the auto-complete text view. |
| 440 | */ |
| 441 | private void updateSearchAutoComplete() { |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 442 | // we dismiss the entire dialog instead |
| 443 | mSearchAutoComplete.setDropDownDismissedOnCompletion(false); |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 444 | mSearchAutoComplete.setForceIgnoreOutsideTouch(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Mike LeBeau | 1fd7323 | 2009-04-27 19:12:05 -0700 | [diff] [blame] | 447 | private void updateSearchAppIcon() { |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 448 | PackageManager pm = getContext().getPackageManager(); |
| 449 | Drawable icon; |
| 450 | try { |
| 451 | ActivityInfo info = pm.getActivityInfo(mLaunchComponent, 0); |
| 452 | icon = pm.getApplicationIcon(info.applicationInfo); |
| 453 | if (DBG) |
| 454 | Log.d(LOG_TAG, "Using app-specific icon"); |
| 455 | } catch (NameNotFoundException e) { |
| 456 | icon = pm.getDefaultActivityIcon(); |
| 457 | Log.w(LOG_TAG, mLaunchComponent + " not found, using generic app icon"); |
Mike LeBeau | 1fd7323 | 2009-04-27 19:12:05 -0700 | [diff] [blame] | 458 | } |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 459 | mAppIcon.setImageDrawable(icon); |
| 460 | mAppIcon.setVisibility(View.VISIBLE); |
| 461 | mSearchPlate.setPadding(SEARCH_PLATE_LEFT_PADDING_NON_GLOBAL, mSearchPlate.getPaddingTop(), mSearchPlate.getPaddingRight(), mSearchPlate.getPaddingBottom()); |
Mike LeBeau | 1fd7323 | 2009-04-27 19:12:05 -0700 | [diff] [blame] | 462 | } |
| 463 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 465 | * Setup the search "Badge" if requested by mode flags. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | */ |
| 467 | private void updateSearchBadge() { |
| 468 | // assume both hidden |
| 469 | int visibility = View.GONE; |
| 470 | Drawable icon = null; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 471 | CharSequence text = null; |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 472 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | // optionally show one or the other. |
Bjorn Bringert | a920413 | 2009-05-05 14:06:35 +0100 | [diff] [blame] | 474 | if (mSearchable.useBadgeIcon()) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 475 | icon = mActivityContext.getDrawable(mSearchable.getIconId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | visibility = View.VISIBLE; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 477 | if (DBG) Log.d(LOG_TAG, "Using badge icon: " + mSearchable.getIconId()); |
Bjorn Bringert | a920413 | 2009-05-05 14:06:35 +0100 | [diff] [blame] | 478 | } else if (mSearchable.useBadgeLabel()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | text = mActivityContext.getResources().getText(mSearchable.getLabelId()).toString(); |
| 480 | visibility = View.VISIBLE; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 481 | if (DBG) Log.d(LOG_TAG, "Using badge label: " + mSearchable.getLabelId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | } |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 483 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | mBadgeLabel.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null); |
| 485 | mBadgeLabel.setText(text); |
| 486 | mBadgeLabel.setVisibility(visibility); |
| 487 | } |
| 488 | |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 489 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | * Listeners of various types |
| 491 | */ |
| 492 | |
| 493 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 494 | * {@link Dialog#onTouchEvent(MotionEvent)} will cancel the dialog only when the |
| 495 | * touch is outside the window. But the window includes space for the drop-down, |
| 496 | * so we also cancel on taps outside the search bar when the drop-down is not showing. |
| 497 | */ |
| 498 | @Override |
| 499 | public boolean onTouchEvent(MotionEvent event) { |
| 500 | // cancel if the drop-down is not showing and the touch event was outside the search plate |
| 501 | if (!mSearchAutoComplete.isPopupShowing() && isOutOfBounds(mSearchPlate, event)) { |
| 502 | if (DBG) Log.d(LOG_TAG, "Pop-up not showing and outside of search plate."); |
| 503 | cancel(); |
| 504 | return true; |
| 505 | } |
| 506 | // Let Dialog handle events outside the window while the pop-up is showing. |
| 507 | return super.onTouchEvent(event); |
| 508 | } |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 509 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 510 | private boolean isOutOfBounds(View v, MotionEvent event) { |
| 511 | final int x = (int) event.getX(); |
| 512 | final int y = (int) event.getY(); |
| 513 | final int slop = ViewConfiguration.get(mContext).getScaledWindowTouchSlop(); |
| 514 | return (x < -slop) || (y < -slop) |
| 515 | || (x > (v.getWidth()+slop)) |
| 516 | || (y > (v.getHeight()+slop)); |
| 517 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | |
Dianne Hackborn | a8f556e | 2009-03-24 20:47:50 -0700 | [diff] [blame] | 519 | @Override |
Karl Rosaen | ea52d29 | 2009-07-20 09:26:10 -0700 | [diff] [blame] | 520 | public void hide() { |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 521 | if (!isShowing()) return; |
| 522 | |
Dianne Hackborn | a8f556e | 2009-03-24 20:47:50 -0700 | [diff] [blame] | 523 | // We made sure the IME was displayed, so also make sure it is closed |
| 524 | // when we go away. |
Yohei Yukawa | 777ef95 | 2015-11-25 20:32:24 -0800 | [diff] [blame] | 525 | InputMethodManager imm = getContext().getSystemService(InputMethodManager.class); |
Dianne Hackborn | a8f556e | 2009-03-24 20:47:50 -0700 | [diff] [blame] | 526 | if (imm != null) { |
| 527 | imm.hideSoftInputFromWindow( |
| 528 | getWindow().getDecorView().getWindowToken(), 0); |
| 529 | } |
Karl Rosaen | ea52d29 | 2009-07-20 09:26:10 -0700 | [diff] [blame] | 530 | |
| 531 | super.hide(); |
Dianne Hackborn | a8f556e | 2009-03-24 20:47:50 -0700 | [diff] [blame] | 532 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 533 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 535 | * Launch a search for the text in the query text field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | */ |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 537 | @UnsupportedAppUsage |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 538 | public void launchQuerySearch() { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 539 | launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 543 | * Launch a search for the text in the query text field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | * |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 545 | * @param actionKey The key code of the action key that was pressed, |
| 546 | * or {@link KeyEvent#KEYCODE_UNKNOWN} if none. |
| 547 | * @param actionMsg The message for the action key that was pressed, |
| 548 | * or <code>null</code> if none. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | */ |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 550 | @UnsupportedAppUsage |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 551 | protected void launchQuerySearch(int actionKey, String actionMsg) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 552 | String query = mSearchAutoComplete.getText().toString(); |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 553 | String action = Intent.ACTION_SEARCH; |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 554 | Intent intent = createIntent(action, null, null, query, actionKey, actionMsg); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 555 | launchIntent(intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | } |
Karl Rosaen | a058f02 | 2009-06-01 23:11:44 +0100 | [diff] [blame] | 557 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | /** |
Amith Yamasani | e9ce3f0 | 2010-01-25 09:15:50 -0800 | [diff] [blame] | 559 | * Launches an intent, including any special intent handling. |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 560 | */ |
| 561 | private void launchIntent(Intent intent) { |
| 562 | if (intent == null) { |
| 563 | return; |
| 564 | } |
Karl Rosaen | ea52d29 | 2009-07-20 09:26:10 -0700 | [diff] [blame] | 565 | Log.d(LOG_TAG, "launching " + intent); |
Bjorn Bringert | 4899e38 | 2009-07-22 10:25:25 +0100 | [diff] [blame] | 566 | try { |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 567 | // If the intent was created from a suggestion, it will always have an explicit |
| 568 | // component here. |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 569 | getContext().startActivity(intent); |
| 570 | // If the search switches to a different activity, |
| 571 | // SearchDialogWrapper#performActivityResuming |
| 572 | // will handle hiding the dialog when the next activity starts, but for |
| 573 | // real in-app search, we still need to dismiss the dialog. |
| 574 | dismiss(); |
Bjorn Bringert | 4899e38 | 2009-07-22 10:25:25 +0100 | [diff] [blame] | 575 | } catch (RuntimeException ex) { |
| 576 | Log.e(LOG_TAG, "Failed launch activity: " + intent, ex); |
Karl Rosaen | 876627d | 2009-07-20 14:30:55 -0700 | [diff] [blame] | 577 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 578 | } |
Bjorn Bringert | 4899e38 | 2009-07-22 10:25:25 +0100 | [diff] [blame] | 579 | |
Bjorn Bringert | 4899e38 | 2009-07-22 10:25:25 +0100 | [diff] [blame] | 580 | /** |
Mike LeBeau | ae9760b | 2009-06-01 21:53:09 +0100 | [diff] [blame] | 581 | * Sets the list item selection in the AutoCompleteTextView's ListView. |
| 582 | */ |
| 583 | public void setListSelection(int index) { |
| 584 | mSearchAutoComplete.setListSelection(index); |
| 585 | } |
Karl Rosaen | a058f02 | 2009-06-01 23:11:44 +0100 | [diff] [blame] | 586 | |
Mike LeBeau | ae9760b | 2009-06-01 21:53:09 +0100 | [diff] [blame] | 587 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 588 | * Constructs an intent from the given information and the search dialog state. |
| 589 | * |
| 590 | * @param action Intent action. |
| 591 | * @param data Intent data, or <code>null</code>. |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 592 | * @param extraData Data for {@link SearchManager#EXTRA_DATA_KEY} or <code>null</code>. |
Satish Sampath | bf23fe0 | 2009-06-15 23:47:56 +0100 | [diff] [blame] | 593 | * @param query Intent query, or <code>null</code>. |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 594 | * @param actionKey The key code of the action key that was pressed, |
| 595 | * or {@link KeyEvent#KEYCODE_UNKNOWN} if none. |
| 596 | * @param actionMsg The message for the action key that was pressed, |
| 597 | * or <code>null</code> if none. |
Bjorn Bringert | be5b73c | 2009-09-21 23:05:23 +0100 | [diff] [blame] | 598 | * @param mode The search mode, one of the acceptable values for |
| 599 | * {@link SearchManager#SEARCH_MODE}, or {@code null}. |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 600 | * @return The intent. |
| 601 | */ |
Satish Sampath | bf23fe0 | 2009-06-15 23:47:56 +0100 | [diff] [blame] | 602 | private Intent createIntent(String action, Uri data, String extraData, String query, |
Amith Yamasani | e678f46 | 2010-09-15 16:13:43 -0700 | [diff] [blame] | 603 | int actionKey, String actionMsg) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 604 | // Now build the Intent |
| 605 | Intent intent = new Intent(action); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 606 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Bjorn Bringert | 442da48 | 2009-09-22 13:15:33 +0100 | [diff] [blame] | 607 | // We need CLEAR_TOP to avoid reusing an old task that has other activities |
Evan Millar | dc1396c | 2009-11-06 11:59:39 -0800 | [diff] [blame] | 608 | // on top of the one we want. We don't want to do this in in-app search though, |
| 609 | // as it can be destructive to the activity stack. |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 610 | if (data != null) { |
| 611 | intent.setData(data); |
| 612 | } |
Bjorn Bringert | 5f80605 | 2009-06-24 12:02:26 +0100 | [diff] [blame] | 613 | intent.putExtra(SearchManager.USER_QUERY, mUserQuery); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 614 | if (query != null) { |
| 615 | intent.putExtra(SearchManager.QUERY, query); |
| 616 | } |
| 617 | if (extraData != null) { |
| 618 | intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData); |
| 619 | } |
| 620 | if (mAppSearchData != null) { |
| 621 | intent.putExtra(SearchManager.APP_DATA, mAppSearchData); |
| 622 | } |
| 623 | if (actionKey != KeyEvent.KEYCODE_UNKNOWN) { |
| 624 | intent.putExtra(SearchManager.ACTION_KEY, actionKey); |
| 625 | intent.putExtra(SearchManager.ACTION_MSG, actionMsg); |
| 626 | } |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 627 | intent.setComponent(mSearchable.getSearchActivity()); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 628 | return intent; |
| 629 | } |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 630 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 631 | /** |
Bjorn Bringert | 077357c | 2009-09-23 13:57:24 +0100 | [diff] [blame] | 632 | * The root element in the search bar layout. This is a custom view just to override |
| 633 | * the handling of the back button. |
| 634 | */ |
| 635 | public static class SearchBar extends LinearLayout { |
| 636 | |
Bjorn Bringert | 077357c | 2009-09-23 13:57:24 +0100 | [diff] [blame] | 637 | public SearchBar(Context context, AttributeSet attrs) { |
| 638 | super(context, attrs); |
| 639 | } |
| 640 | |
| 641 | public SearchBar(Context context) { |
| 642 | super(context); |
| 643 | } |
| 644 | |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 645 | @Override |
| 646 | public ActionMode startActionModeForChild( |
| 647 | View child, ActionMode.Callback callback, int type) { |
Clara Bayarri | 0433ae6 | 2015-04-14 15:51:38 +0100 | [diff] [blame] | 648 | // Disable Primary Action Modes in the SearchBar, as they overlap. |
| 649 | if (type != ActionMode.TYPE_PRIMARY) { |
| 650 | return super.startActionModeForChild(child, callback, type); |
| 651 | } |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 652 | return null; |
| 653 | } |
Bjorn Bringert | 077357c | 2009-09-23 13:57:24 +0100 | [diff] [blame] | 654 | } |
| 655 | |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 656 | private boolean isEmpty(AutoCompleteTextView actv) { |
| 657 | return TextUtils.getTrimmedLength(actv.getText()) == 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | } |
Bjorn Bringert | 87d61f2 | 2009-09-16 23:17:16 +0100 | [diff] [blame] | 659 | |
| 660 | @Override |
| 661 | public void onBackPressed() { |
Bjorn Bringert | a6309c3 | 2009-10-01 10:30:25 +0100 | [diff] [blame] | 662 | // If the input method is covering the search dialog completely, |
| 663 | // e.g. in landscape mode with no hard keyboard, dismiss just the input method |
Yohei Yukawa | 777ef95 | 2015-11-25 20:32:24 -0800 | [diff] [blame] | 664 | InputMethodManager imm = getContext().getSystemService(InputMethodManager.class); |
Bjorn Bringert | a6309c3 | 2009-10-01 10:30:25 +0100 | [diff] [blame] | 665 | if (imm != null && imm.isFullscreenMode() && |
| 666 | imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0)) { |
| 667 | return; |
| 668 | } |
Amith Yamasani | 479ae0a | 2010-02-04 14:52:32 -0800 | [diff] [blame] | 669 | // Close search dialog |
| 670 | cancel(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 671 | } |
Bjorn Bringert | 87d61f2 | 2009-09-16 23:17:16 +0100 | [diff] [blame] | 672 | |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 673 | private boolean onClosePressed() { |
| 674 | // Dismiss the dialog if close button is pressed when there's no query text |
| 675 | if (isEmpty(mSearchAutoComplete)) { |
| 676 | dismiss(); |
| 677 | return true; |
| 678 | } |
| 679 | |
| 680 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 682 | |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 683 | private final SearchView.OnCloseListener mOnCloseListener = new SearchView.OnCloseListener() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 685 | public boolean onClose() { |
| 686 | return onClosePressed(); |
| 687 | } |
| 688 | }; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 689 | |
Adam Powell | 01f2135 | 2011-01-20 18:30:10 -0800 | [diff] [blame] | 690 | private final SearchView.OnQueryTextListener mOnQueryChangeListener = |
| 691 | new SearchView.OnQueryTextListener() { |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 692 | |
Adam Powell | 01f2135 | 2011-01-20 18:30:10 -0800 | [diff] [blame] | 693 | public boolean onQueryTextSubmit(String query) { |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 694 | dismiss(); |
| 695 | return false; |
| 696 | } |
| 697 | |
Adam Powell | 01f2135 | 2011-01-20 18:30:10 -0800 | [diff] [blame] | 698 | public boolean onQueryTextChange(String newText) { |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 699 | return false; |
| 700 | } |
| 701 | }; |
| 702 | |
Adam Powell | 01f2135 | 2011-01-20 18:30:10 -0800 | [diff] [blame] | 703 | private final SearchView.OnSuggestionListener mOnSuggestionSelectionListener = |
| 704 | new SearchView.OnSuggestionListener() { |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 705 | |
Adam Powell | 01f2135 | 2011-01-20 18:30:10 -0800 | [diff] [blame] | 706 | public boolean onSuggestionSelect(int position) { |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 707 | return false; |
| 708 | } |
| 709 | |
Adam Powell | 01f2135 | 2011-01-20 18:30:10 -0800 | [diff] [blame] | 710 | public boolean onSuggestionClick(int position) { |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 711 | dismiss(); |
| 712 | return false; |
| 713 | } |
| 714 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 | |
| 716 | /** |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 717 | * Sets the text in the query box, updating the suggestions. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | */ |
Amith Yamasani | 968ec93 | 2010-12-02 14:00:47 -0800 | [diff] [blame] | 719 | private void setUserQuery(String query) { |
| 720 | if (query == null) { |
| 721 | query = ""; |
| 722 | } |
| 723 | mUserQuery = query; |
| 724 | mSearchAutoComplete.setText(query); |
| 725 | mSearchAutoComplete.setSelection(query.length()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | } |
| 727 | } |