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 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 19 | import static android.app.SuggestionsAdapter.getColumnString; |
Bjorn Bringert | c1f4096 | 2009-04-29 13:08:39 +0100 | [diff] [blame] | 20 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.ActivityNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.ComponentName; |
Karl Rosaen | d4c98c4 | 2009-06-09 17:05:54 +0100 | [diff] [blame] | 23 | import android.content.ContentResolver; |
| 24 | import android.content.ContentValues; |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 27 | import android.content.pm.ActivityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
Karl Rosaen | 98e333f | 2009-04-28 10:39:09 -0700 | [diff] [blame] | 29 | import android.content.pm.ResolveInfo; |
Bjorn Bringert | c1f4096 | 2009-04-29 13:08:39 +0100 | [diff] [blame] | 30 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.content.res.Resources; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.database.Cursor; |
Romain Guy | b5537c4 | 2009-06-30 12:39:18 -0700 | [diff] [blame] | 33 | import android.graphics.drawable.Animatable; |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 34 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.net.Uri; |
| 36 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.os.SystemClock; |
| 38 | import android.server.search.SearchableInfo; |
| 39 | import android.speech.RecognizerIntent; |
| 40 | import android.text.Editable; |
| 41 | import android.text.InputType; |
| 42 | import android.text.TextUtils; |
| 43 | import android.text.TextWatcher; |
Satish Sampath | 662df0b | 2009-06-22 23:16:07 +0100 | [diff] [blame] | 44 | import android.text.util.Regex; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.util.AttributeSet; |
| 46 | import android.util.Log; |
Mike LeBeau | a97f4a1 | 2009-05-23 01:19:36 -0500 | [diff] [blame] | 47 | import android.view.ContextThemeWrapper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.view.Gravity; |
| 49 | import android.view.KeyEvent; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 50 | import android.view.MotionEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.view.View; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 52 | import android.view.ViewConfiguration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.view.ViewGroup; |
| 54 | import android.view.Window; |
| 55 | import android.view.WindowManager; |
Satish Sampath | 662df0b | 2009-06-22 23:16:07 +0100 | [diff] [blame] | 56 | import android.view.inputmethod.EditorInfo; |
Dianne Hackborn | a8f556e | 2009-03-24 20:47:50 -0700 | [diff] [blame] | 57 | import android.view.inputmethod.InputMethodManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.widget.AdapterView; |
| 59 | import android.widget.AutoCompleteTextView; |
| 60 | import android.widget.Button; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import android.widget.ImageButton; |
Mike LeBeau | 1fd7323 | 2009-04-27 19:12:05 -0700 | [diff] [blame] | 62 | import android.widget.ImageView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.widget.ListView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.widget.TextView; |
Bjorn Bringert | c1f4096 | 2009-04-29 13:08:39 +0100 | [diff] [blame] | 65 | import android.widget.AdapterView.OnItemClickListener; |
| 66 | import android.widget.AdapterView.OnItemSelectedListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 68 | import java.util.ArrayList; |
| 69 | import java.util.WeakHashMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | import java.util.concurrent.atomic.AtomicLong; |
| 71 | |
| 72 | /** |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 73 | * System search dialog. This is controlled by the |
| 74 | * SearchManagerService and runs in the system process. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | * |
| 76 | * @hide |
| 77 | */ |
| 78 | public class SearchDialog extends Dialog implements OnItemClickListener, OnItemSelectedListener { |
| 79 | |
| 80 | // Debugging support |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 81 | private static final boolean DBG = false; |
| 82 | private static final String LOG_TAG = "SearchDialog"; |
| 83 | private static final boolean DBG_LOG_TIMING = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | private static final String INSTANCE_KEY_COMPONENT = "comp"; |
| 86 | private static final String INSTANCE_KEY_APPDATA = "data"; |
| 87 | private static final String INSTANCE_KEY_GLOBALSEARCH = "glob"; |
Bjorn Bringert | b0ae27f | 2009-06-23 13:47:31 +0100 | [diff] [blame] | 88 | private static final String INSTANCE_KEY_STORED_COMPONENT = "sComp"; |
| 89 | private static final String INSTANCE_KEY_STORED_APPDATA = "sData"; |
| 90 | private static final String INSTANCE_KEY_PREVIOUS_COMPONENTS = "sPrev"; |
| 91 | private static final String INSTANCE_KEY_USER_QUERY = "uQry"; |
Mike LeBeau | 260dfb5 | 2009-07-15 15:20:14 -0700 | [diff] [blame] | 92 | |
| 93 | // The extra key used in an intent to the speech recognizer for in-app voice search. |
| 94 | private static final String EXTRA_CALLING_PACKAGE = "calling_package"; |
Bjorn Bringert | b0ae27f | 2009-06-23 13:47:31 +0100 | [diff] [blame] | 95 | |
Mike LeBeau | 1fd7323 | 2009-04-27 19:12:05 -0700 | [diff] [blame] | 96 | private static final int SEARCH_PLATE_LEFT_PADDING_GLOBAL = 12; |
| 97 | private static final int SEARCH_PLATE_LEFT_PADDING_NON_GLOBAL = 7; |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 98 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | // views & widgets |
| 100 | private TextView mBadgeLabel; |
Mike LeBeau | 1fd7323 | 2009-04-27 19:12:05 -0700 | [diff] [blame] | 101 | private ImageView mAppIcon; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 102 | private SearchAutoComplete mSearchAutoComplete; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | private Button mGoButton; |
| 104 | private ImageButton mVoiceButton; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 105 | private View mSearchPlate; |
Romain Guy | f4f7046 | 2009-06-26 16:55:54 -0700 | [diff] [blame] | 106 | private Drawable mWorkingSpinner; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | |
| 108 | // interaction with searchable application |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 109 | private SearchableInfo mSearchable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | private ComponentName mLaunchComponent; |
| 111 | private Bundle mAppSearchData; |
| 112 | private boolean mGlobalSearchMode; |
| 113 | private Context mActivityContext; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 114 | |
Mike LeBeau | b3aab69 | 2009-04-30 02:09:09 -0700 | [diff] [blame] | 115 | // Values we store to allow user to toggle between in-app search and global search. |
| 116 | private ComponentName mStoredComponentName; |
| 117 | private Bundle mStoredAppSearchData; |
| 118 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 119 | // stack of previous searchables, to support the BACK key after |
| 120 | // SearchManager.INTENT_ACTION_CHANGE_SEARCH_SOURCE. |
| 121 | // The top of the stack (= previous searchable) is the last element of the list, |
| 122 | // since adding and removing is efficient at the end of an ArrayList. |
| 123 | private ArrayList<ComponentName> mPreviousComponents; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | // For voice searching |
| 126 | private Intent mVoiceWebSearchIntent; |
| 127 | private Intent mVoiceAppSearchIntent; |
| 128 | |
| 129 | // support for AutoCompleteTextView suggestions display |
| 130 | private SuggestionsAdapter mSuggestionsAdapter; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 131 | |
| 132 | // Whether to rewrite queries when selecting suggestions |
Mike LeBeau | cce7dbc | 2009-06-18 09:25:18 -0700 | [diff] [blame] | 133 | private static final boolean REWRITE_QUERIES = true; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 134 | |
| 135 | // The query entered by the user. This is not changed when selecting a suggestion |
| 136 | // that modifies the contents of the text field. But if the user then edits |
| 137 | // the suggestion, the resulting string is saved. |
| 138 | private String mUserQuery; |
| 139 | |
| 140 | // A weak map of drawables we've gotten from other packages, so we don't load them |
| 141 | // more than once. |
| 142 | private final WeakHashMap<String, Drawable> mOutsideDrawablesCache = |
| 143 | new WeakHashMap<String, Drawable>(); |
Satish Sampath | 662df0b | 2009-06-22 23:16:07 +0100 | [diff] [blame] | 144 | |
| 145 | // Last known IME options value for the search edit text. |
| 146 | private int mSearchAutoCompleteImeOptions; |
| 147 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | /** |
| 149 | * Constructor - fires it up and makes it look like the search UI. |
| 150 | * |
| 151 | * @param context Application Context we can use for system acess |
| 152 | */ |
| 153 | public SearchDialog(Context context) { |
Mike LeBeau | a97f4a1 | 2009-05-23 01:19:36 -0500 | [diff] [blame] | 154 | super(context, com.android.internal.R.style.Theme_GlobalSearchBar); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
| 158 | * We create the search dialog just once, and it stays around (hidden) |
| 159 | * until activated by the user. |
| 160 | */ |
| 161 | @Override |
| 162 | protected void onCreate(Bundle savedInstanceState) { |
| 163 | super.onCreate(savedInstanceState); |
| 164 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | setContentView(com.android.internal.R.layout.search_bar); |
| 166 | |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 167 | Window theWindow = getWindow(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | WindowManager.LayoutParams lp = theWindow.getAttributes(); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 169 | lp.type = WindowManager.LayoutParams.TYPE_SEARCH_BAR; |
| 170 | lp.width = ViewGroup.LayoutParams.FILL_PARENT; |
| 171 | // taking up the whole window (even when transparent) is less than ideal, |
| 172 | // but necessary to show the popup window until the window manager supports |
| 173 | // having windows anchored by their parent but not clipped by them. |
| 174 | lp.height = ViewGroup.LayoutParams.FILL_PARENT; |
| 175 | lp.gravity = Gravity.TOP | Gravity.FILL_HORIZONTAL; |
Mike LeBeau | 98acd54 | 2009-05-07 19:04:39 -0700 | [diff] [blame] | 176 | lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | theWindow.setAttributes(lp); |
| 178 | |
| 179 | // get the view elements for local access |
| 180 | mBadgeLabel = (TextView) findViewById(com.android.internal.R.id.search_badge); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 181 | mSearchAutoComplete = (SearchAutoComplete) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | findViewById(com.android.internal.R.id.search_src_text); |
Mike LeBeau | 1fd7323 | 2009-04-27 19:12:05 -0700 | [diff] [blame] | 183 | mAppIcon = (ImageView) findViewById(com.android.internal.R.id.search_app_icon); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | mGoButton = (Button) findViewById(com.android.internal.R.id.search_go_btn); |
| 185 | mVoiceButton = (ImageButton) findViewById(com.android.internal.R.id.search_voice_btn); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 186 | mSearchPlate = findViewById(com.android.internal.R.id.search_plate); |
Romain Guy | f4f7046 | 2009-06-26 16:55:54 -0700 | [diff] [blame] | 187 | mWorkingSpinner = getContext().getResources(). |
Mike LeBeau | 1480eb2 | 2009-05-20 17:22:13 -0700 | [diff] [blame] | 188 | getDrawable(com.android.internal.R.drawable.search_spinner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | |
| 190 | // attach listeners |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 191 | mSearchAutoComplete.addTextChangedListener(mTextWatcher); |
| 192 | mSearchAutoComplete.setOnKeyListener(mTextKeyListener); |
| 193 | mSearchAutoComplete.setOnItemClickListener(this); |
| 194 | mSearchAutoComplete.setOnItemSelectedListener(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | mGoButton.setOnClickListener(mGoButtonClickListener); |
| 196 | mGoButton.setOnKeyListener(mButtonsKeyListener); |
| 197 | mVoiceButton.setOnClickListener(mVoiceButtonClickListener); |
| 198 | mVoiceButton.setOnKeyListener(mButtonsKeyListener); |
| 199 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 200 | mSearchAutoComplete.setSearchDialog(this); |
| 201 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | // pre-hide all the extraneous elements |
| 203 | mBadgeLabel.setVisibility(View.GONE); |
| 204 | |
| 205 | // Additional adjustments to make Dialog work for Search |
| 206 | |
| 207 | // Touching outside of the search dialog will dismiss it |
| 208 | setCanceledOnTouchOutside(true); |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | // Save voice intent for later queries/launching |
| 211 | mVoiceWebSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 212 | mVoiceWebSearchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | mVoiceWebSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, |
| 214 | RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); |
| 215 | |
| 216 | mVoiceAppSearchIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 217 | mVoiceAppSearchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Satish Sampath | 662df0b | 2009-06-22 23:16:07 +0100 | [diff] [blame] | 218 | |
| 219 | mSearchAutoCompleteImeOptions = mSearchAutoComplete.getImeOptions(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Set up the search dialog |
| 224 | * |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 225 | * @return true if search dialog launched, false if not |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | */ |
| 227 | public boolean show(String initialQuery, boolean selectInitialQuery, |
| 228 | ComponentName componentName, Bundle appSearchData, boolean globalSearch) { |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 229 | |
Mike LeBeau | b3aab69 | 2009-04-30 02:09:09 -0700 | [diff] [blame] | 230 | // Reset any stored values from last time dialog was shown. |
| 231 | mStoredComponentName = null; |
| 232 | mStoredAppSearchData = null; |
Satish Sampath | fef8d3e | 2009-07-01 17:48:42 +0100 | [diff] [blame] | 233 | |
| 234 | boolean success = doShow(initialQuery, selectInitialQuery, componentName, appSearchData, |
| 235 | globalSearch); |
| 236 | if (success) { |
| 237 | // Display the drop down as soon as possible instead of waiting for the rest of the |
| 238 | // pending UI stuff to get done, so that things appear faster to the user. |
| 239 | mSearchAutoComplete.showDropDownAfterLayout(); |
| 240 | } |
| 241 | return success; |
Mike LeBeau | b3aab69 | 2009-04-30 02:09:09 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Mike LeBeau | b3aab69 | 2009-04-30 02:09:09 -0700 | [diff] [blame] | 244 | /** |
| 245 | * Called in response to a press of the hard search button in |
| 246 | * {@link #onKeyDown(int, KeyEvent)}, this method toggles between in-app |
| 247 | * search and global search when relevant. |
| 248 | * |
| 249 | * If pressed within an in-app search context, this switches the search dialog out to |
| 250 | * global search. If pressed within a global search context that was originally an in-app |
| 251 | * search context, this switches back to the in-app search context. If pressed within a |
| 252 | * global search context that has no original in-app search context (e.g., global search |
| 253 | * from Home), this does nothing. |
| 254 | * |
| 255 | * @return false if we wanted to toggle context but could not do so successfully, true |
| 256 | * in all other cases |
| 257 | */ |
| 258 | private boolean toggleGlobalSearch() { |
| 259 | String currentSearchText = mSearchAutoComplete.getText().toString(); |
| 260 | if (!mGlobalSearchMode) { |
| 261 | mStoredComponentName = mLaunchComponent; |
| 262 | mStoredAppSearchData = mAppSearchData; |
| 263 | return doShow(currentSearchText, false, null, mAppSearchData, true); |
| 264 | } else { |
| 265 | if (mStoredComponentName != null) { |
| 266 | // This means we should toggle *back* to an in-app search context from |
| 267 | // global search. |
| 268 | return doShow(currentSearchText, false, mStoredComponentName, |
| 269 | mStoredAppSearchData, false); |
| 270 | } else { |
| 271 | return true; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Does the rest of the work required to show the search dialog. Called by both |
| 278 | * {@link #show(String, boolean, ComponentName, Bundle, boolean)} and |
| 279 | * {@link #toggleGlobalSearch()}. |
| 280 | * |
| 281 | * @return true if search dialog showed, false if not |
| 282 | */ |
| 283 | private boolean doShow(String initialQuery, boolean selectInitialQuery, |
| 284 | ComponentName componentName, Bundle appSearchData, |
| 285 | boolean globalSearch) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 286 | // set up the searchable and show the dialog |
| 287 | if (!show(componentName, appSearchData, globalSearch)) { |
| 288 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | } |
Mike LeBeau | b3aab69 | 2009-04-30 02:09:09 -0700 | [diff] [blame] | 290 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 291 | // finally, load the user's initial text (which may trigger suggestions) |
| 292 | setUserQuery(initialQuery); |
| 293 | if (selectInitialQuery) { |
| 294 | mSearchAutoComplete.selectAll(); |
| 295 | } |
Mike LeBeau | b3aab69 | 2009-04-30 02:09:09 -0700 | [diff] [blame] | 296 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 297 | return true; |
| 298 | } |
Mike LeBeau | b3aab69 | 2009-04-30 02:09:09 -0700 | [diff] [blame] | 299 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 300 | /** |
| 301 | * Sets up the search dialog and shows it. |
| 302 | * |
| 303 | * @return <code>true</code> if search dialog launched |
| 304 | */ |
| 305 | private boolean show(ComponentName componentName, Bundle appSearchData, |
| 306 | boolean globalSearch) { |
| 307 | |
| 308 | if (DBG) { |
| 309 | Log.d(LOG_TAG, "show(" + componentName + ", " |
| 310 | + appSearchData + ", " + globalSearch + ")"); |
| 311 | } |
| 312 | |
Bjorn Bringert | 8d15382 | 2009-06-22 10:31:44 +0100 | [diff] [blame] | 313 | SearchManager searchManager = (SearchManager) |
| 314 | mContext.getSystemService(Context.SEARCH_SERVICE); |
Mike LeBeau | a59d7b0 | 2009-05-12 15:30:37 -0700 | [diff] [blame] | 315 | // Try to get the searchable info for the provided component (or for global search, |
| 316 | // if globalSearch == true). |
Bjorn Bringert | 8d15382 | 2009-06-22 10:31:44 +0100 | [diff] [blame] | 317 | mSearchable = searchManager.getSearchableInfo(componentName, globalSearch); |
Mike LeBeau | a59d7b0 | 2009-05-12 15:30:37 -0700 | [diff] [blame] | 318 | |
| 319 | // If we got back nothing, and it wasn't a request for global search, then try again |
| 320 | // for global search, as we'll try to launch that in lieu of any component-specific search. |
| 321 | if (!globalSearch && mSearchable == null) { |
| 322 | globalSearch = true; |
Bjorn Bringert | 8d15382 | 2009-06-22 10:31:44 +0100 | [diff] [blame] | 323 | mSearchable = searchManager.getSearchableInfo(componentName, globalSearch); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | } |
Bjorn Bringert | ee716fa | 2009-07-16 09:15:37 +0100 | [diff] [blame^] | 325 | |
| 326 | // If there's not even a searchable info available for global search, then really give up. |
| 327 | if (mSearchable == null) { |
| 328 | Log.w(LOG_TAG, "No global search provider."); |
| 329 | return false; |
| 330 | } |
| 331 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | mLaunchComponent = componentName; |
| 333 | mAppSearchData = appSearchData; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 334 | // Using globalSearch here is just an optimization, just calling |
| 335 | // isDefaultSearchable() should always give the same result. |
Bjorn Bringert | 8d15382 | 2009-06-22 10:31:44 +0100 | [diff] [blame] | 336 | mGlobalSearchMode = globalSearch || searchManager.isDefaultSearchable(mSearchable); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 337 | mActivityContext = mSearchable.getActivityContext(getContext()); |
| 338 | |
| 339 | // show the dialog. this will call onStart(). |
Mike LeBeau | 1fffbd9 | 2009-07-14 15:57:41 -0700 | [diff] [blame] | 340 | if (!isShowing()) { |
Mike LeBeau | a97f4a1 | 2009-05-23 01:19:36 -0500 | [diff] [blame] | 341 | // The Dialog uses a ContextThemeWrapper for the context; use this to change the |
| 342 | // theme out from underneath us, between the global search theme and the in-app |
| 343 | // search theme. They are identical except that the global search theme does not |
| 344 | // dim the background of the window (because global search is full screen so it's |
| 345 | // not needed and this should save a little bit of time on global search invocation). |
| 346 | Object context = getContext(); |
| 347 | if (context instanceof ContextThemeWrapper) { |
| 348 | ContextThemeWrapper wrapper = (ContextThemeWrapper) context; |
| 349 | if (globalSearch) { |
| 350 | wrapper.setTheme(com.android.internal.R.style.Theme_GlobalSearchBar); |
| 351 | } else { |
| 352 | wrapper.setTheme(com.android.internal.R.style.Theme_SearchBar); |
| 353 | } |
| 354 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 355 | show(); |
| 356 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 358 | updateUI(); |
| 359 | |
| 360 | return true; |
| 361 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | |
| 363 | /** |
| 364 | * The search dialog is being dismissed, so handle all of the local shutdown operations. |
| 365 | * |
| 366 | * This function is designed to be idempotent so that dismiss() can be safely called at any time |
| 367 | * (even if already closed) and more likely to really dump any memory. No leaks! |
| 368 | */ |
| 369 | @Override |
| 370 | public void onStop() { |
| 371 | super.onStop(); |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 372 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 373 | closeSuggestionsAdapter(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | |
| 375 | // dump extra memory we're hanging on to |
| 376 | mLaunchComponent = null; |
| 377 | mAppSearchData = null; |
| 378 | mSearchable = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | mActivityContext = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | mUserQuery = null; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 381 | mPreviousComponents = null; |
| 382 | } |
Mike LeBeau | 1c69075 | 2009-05-20 20:20:26 -0700 | [diff] [blame] | 383 | |
Mike LeBeau | 1c69075 | 2009-05-20 20:20:26 -0700 | [diff] [blame] | 384 | /** |
Mike LeBeau | 1480eb2 | 2009-05-20 17:22:13 -0700 | [diff] [blame] | 385 | * Sets the search dialog to the 'working' state, which shows a working spinner in the |
| 386 | * right hand size of the text field. |
| 387 | * |
| 388 | * @param working true to show spinner, false to hide spinner |
| 389 | */ |
| 390 | public void setWorking(boolean working) { |
| 391 | if (working) { |
| 392 | mSearchAutoComplete.setCompoundDrawablesWithIntrinsicBounds( |
| 393 | null, null, mWorkingSpinner, null); |
Romain Guy | b5537c4 | 2009-06-30 12:39:18 -0700 | [diff] [blame] | 394 | ((Animatable) mWorkingSpinner).start(); |
Mike LeBeau | 1480eb2 | 2009-05-20 17:22:13 -0700 | [diff] [blame] | 395 | } else { |
| 396 | mSearchAutoComplete.setCompoundDrawablesWithIntrinsicBounds( |
| 397 | null, null, null, null); |
Romain Guy | b5537c4 | 2009-06-30 12:39:18 -0700 | [diff] [blame] | 398 | ((Animatable) mWorkingSpinner).stop(); |
Mike LeBeau | 1480eb2 | 2009-05-20 17:22:13 -0700 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
| 402 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 403 | * Closes and gets rid of the suggestions adapter. |
| 404 | */ |
| 405 | private void closeSuggestionsAdapter() { |
| 406 | // remove the adapter from the autocomplete first, to avoid any updates |
| 407 | // when we drop the cursor |
| 408 | mSearchAutoComplete.setAdapter((SuggestionsAdapter)null); |
| 409 | // close any leftover cursor |
| 410 | if (mSuggestionsAdapter != null) { |
| 411 | mSuggestionsAdapter.changeCursor(null); |
| 412 | } |
| 413 | mSuggestionsAdapter = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Save the minimal set of data necessary to recreate the search |
| 418 | * |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 419 | * @return A bundle with the state of the dialog, or {@code null} if the search |
| 420 | * dialog is not showing. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | */ |
| 422 | @Override |
| 423 | public Bundle onSaveInstanceState() { |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 424 | if (!isShowing()) return null; |
| 425 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | Bundle bundle = new Bundle(); |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 427 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | // setup info so I can recreate this particular search |
| 429 | bundle.putParcelable(INSTANCE_KEY_COMPONENT, mLaunchComponent); |
| 430 | bundle.putBundle(INSTANCE_KEY_APPDATA, mAppSearchData); |
| 431 | bundle.putBoolean(INSTANCE_KEY_GLOBALSEARCH, mGlobalSearchMode); |
Bjorn Bringert | b0ae27f | 2009-06-23 13:47:31 +0100 | [diff] [blame] | 432 | bundle.putParcelable(INSTANCE_KEY_STORED_COMPONENT, mStoredComponentName); |
| 433 | bundle.putBundle(INSTANCE_KEY_STORED_APPDATA, mStoredAppSearchData); |
| 434 | bundle.putParcelableArrayList(INSTANCE_KEY_PREVIOUS_COMPONENTS, mPreviousComponents); |
| 435 | bundle.putString(INSTANCE_KEY_USER_QUERY, mUserQuery); |
| 436 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | return bundle; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Restore the state of the dialog from a previously saved bundle. |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 442 | * |
| 443 | * TODO: go through this and make sure that it saves everything that is saved |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | * |
| 445 | * @param savedInstanceState The state of the dialog previously saved by |
| 446 | * {@link #onSaveInstanceState()}. |
| 447 | */ |
| 448 | @Override |
| 449 | public void onRestoreInstanceState(Bundle savedInstanceState) { |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 450 | if (savedInstanceState == null) return; |
| 451 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | ComponentName launchComponent = savedInstanceState.getParcelable(INSTANCE_KEY_COMPONENT); |
| 453 | Bundle appSearchData = savedInstanceState.getBundle(INSTANCE_KEY_APPDATA); |
| 454 | boolean globalSearch = savedInstanceState.getBoolean(INSTANCE_KEY_GLOBALSEARCH); |
Bjorn Bringert | b0ae27f | 2009-06-23 13:47:31 +0100 | [diff] [blame] | 455 | ComponentName storedComponentName = |
| 456 | savedInstanceState.getParcelable(INSTANCE_KEY_STORED_COMPONENT); |
| 457 | Bundle storedAppSearchData = |
| 458 | savedInstanceState.getBundle(INSTANCE_KEY_STORED_APPDATA); |
| 459 | ArrayList<ComponentName> previousComponents = |
| 460 | savedInstanceState.getParcelableArrayList(INSTANCE_KEY_PREVIOUS_COMPONENTS); |
| 461 | String userQuery = savedInstanceState.getString(INSTANCE_KEY_USER_QUERY); |
| 462 | |
| 463 | // Set stored state |
| 464 | mStoredComponentName = storedComponentName; |
| 465 | mStoredAppSearchData = storedAppSearchData; |
| 466 | mPreviousComponents = previousComponents; |
| 467 | |
| 468 | // show the dialog. |
| 469 | if (!doShow(userQuery, false, launchComponent, appSearchData, globalSearch)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | // for some reason, we couldn't re-instantiate |
| 471 | return; |
| 472 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 476 | * 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] | 477 | */ |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 478 | public void onConfigurationChanged() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | if (isShowing()) { |
| 480 | // Redraw (resources may have changed) |
| 481 | updateSearchButton(); |
Mike LeBeau | 1fd7323 | 2009-04-27 19:12:05 -0700 | [diff] [blame] | 482 | updateSearchAppIcon(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | updateSearchBadge(); |
| 484 | updateQueryHint(); |
| 485 | } |
| 486 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 487 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 489 | * 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] | 490 | */ |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 491 | private void updateUI() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | if (mSearchable != null) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 493 | updateSearchAutoComplete(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | updateSearchButton(); |
Mike LeBeau | 1fd7323 | 2009-04-27 19:12:05 -0700 | [diff] [blame] | 495 | updateSearchAppIcon(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 496 | updateSearchBadge(); |
| 497 | updateQueryHint(); |
| 498 | updateVoiceButton(); |
| 499 | |
| 500 | // In order to properly configure the input method (if one is being used), we |
| 501 | // need to let it know if we'll be providing suggestions. Although it would be |
| 502 | // difficult/expensive to know if every last detail has been configured properly, we |
| 503 | // can at least see if a suggestions provider has been configured, and use that |
| 504 | // as our trigger. |
| 505 | int inputType = mSearchable.getInputType(); |
| 506 | // We only touch this if the input type is set up for text (which it almost certainly |
| 507 | // should be, in the case of search!) |
| 508 | if ((inputType & InputType.TYPE_MASK_CLASS) == InputType.TYPE_CLASS_TEXT) { |
| 509 | // The existence of a suggestions authority is the proxy for "suggestions |
| 510 | // are available here" |
| 511 | inputType &= ~InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE; |
| 512 | if (mSearchable.getSuggestAuthority() != null) { |
| 513 | inputType |= InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE; |
| 514 | } |
| 515 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 516 | mSearchAutoComplete.setInputType(inputType); |
Satish Sampath | 662df0b | 2009-06-22 23:16:07 +0100 | [diff] [blame] | 517 | mSearchAutoCompleteImeOptions = mSearchable.getImeOptions(); |
| 518 | mSearchAutoComplete.setImeOptions(mSearchAutoCompleteImeOptions); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Updates the auto-complete text view. |
| 524 | */ |
| 525 | private void updateSearchAutoComplete() { |
| 526 | // close any existing suggestions adapter |
| 527 | closeSuggestionsAdapter(); |
| 528 | |
| 529 | mSearchAutoComplete.setDropDownAnimationStyle(0); // no animation |
Bjorn Bringert | 203464a | 2009-04-27 17:08:11 +0100 | [diff] [blame] | 530 | mSearchAutoComplete.setThreshold(mSearchable.getSuggestThreshold()); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 531 | // we dismiss the entire dialog instead |
| 532 | mSearchAutoComplete.setDropDownDismissedOnCompletion(false); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 533 | |
| 534 | if (mGlobalSearchMode) { |
| 535 | mSearchAutoComplete.setDropDownAlwaysVisible(true); // fill space until results come in |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 536 | } else { |
| 537 | mSearchAutoComplete.setDropDownAlwaysVisible(false); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | // attach the suggestions adapter, if suggestions are available |
| 541 | // The existence of a suggestions authority is the proxy for "suggestions available here" |
| 542 | if (mSearchable.getSuggestAuthority() != null) { |
Mike LeBeau | 1480eb2 | 2009-05-20 17:22:13 -0700 | [diff] [blame] | 543 | mSuggestionsAdapter = new SuggestionsAdapter(getContext(), this, mSearchable, |
| 544 | mOutsideDrawablesCache, mGlobalSearchMode); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 545 | mSearchAutoComplete.setAdapter(mSuggestionsAdapter); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | /** |
| 550 | * Update the text in the search button. Note: This is deprecated functionality, for |
| 551 | * 1.0 compatibility only. |
| 552 | */ |
| 553 | private void updateSearchButton() { |
| 554 | String textLabel = null; |
| 555 | Drawable iconLabel = null; |
| 556 | int textId = mSearchable.getSearchButtonText(); |
| 557 | if (textId != 0) { |
| 558 | textLabel = mActivityContext.getResources().getString(textId); |
| 559 | } else { |
| 560 | iconLabel = getContext().getResources(). |
| 561 | getDrawable(com.android.internal.R.drawable.ic_btn_search); |
| 562 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 563 | mGoButton.setText(textLabel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | mGoButton.setCompoundDrawablesWithIntrinsicBounds(iconLabel, null, null, null); |
| 565 | } |
| 566 | |
Mike LeBeau | 1fd7323 | 2009-04-27 19:12:05 -0700 | [diff] [blame] | 567 | private void updateSearchAppIcon() { |
| 568 | if (mGlobalSearchMode) { |
| 569 | mAppIcon.setImageResource(0); |
| 570 | mAppIcon.setVisibility(View.GONE); |
| 571 | mSearchPlate.setPadding(SEARCH_PLATE_LEFT_PADDING_GLOBAL, |
| 572 | mSearchPlate.getPaddingTop(), |
| 573 | mSearchPlate.getPaddingRight(), |
| 574 | mSearchPlate.getPaddingBottom()); |
| 575 | } else { |
| 576 | PackageManager pm = getContext().getPackageManager(); |
Romain Guy | f4f7046 | 2009-06-26 16:55:54 -0700 | [diff] [blame] | 577 | Drawable icon; |
Mike LeBeau | 1fd7323 | 2009-04-27 19:12:05 -0700 | [diff] [blame] | 578 | try { |
| 579 | ActivityInfo info = pm.getActivityInfo(mLaunchComponent, 0); |
| 580 | icon = pm.getApplicationIcon(info.applicationInfo); |
| 581 | if (DBG) Log.d(LOG_TAG, "Using app-specific icon"); |
| 582 | } catch (NameNotFoundException e) { |
| 583 | icon = pm.getDefaultActivityIcon(); |
| 584 | Log.w(LOG_TAG, mLaunchComponent + " not found, using generic app icon"); |
| 585 | } |
| 586 | mAppIcon.setImageDrawable(icon); |
| 587 | mAppIcon.setVisibility(View.VISIBLE); |
| 588 | mSearchPlate.setPadding(SEARCH_PLATE_LEFT_PADDING_NON_GLOBAL, |
| 589 | mSearchPlate.getPaddingTop(), |
| 590 | mSearchPlate.getPaddingRight(), |
| 591 | mSearchPlate.getPaddingBottom()); |
| 592 | } |
| 593 | } |
| 594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 596 | * Setup the search "Badge" if requested by mode flags. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 | */ |
| 598 | private void updateSearchBadge() { |
| 599 | // assume both hidden |
| 600 | int visibility = View.GONE; |
| 601 | Drawable icon = null; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 602 | CharSequence text = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | |
| 604 | // optionally show one or the other. |
Bjorn Bringert | a920413 | 2009-05-05 14:06:35 +0100 | [diff] [blame] | 605 | if (mSearchable.useBadgeIcon()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | icon = mActivityContext.getResources().getDrawable(mSearchable.getIconId()); |
| 607 | visibility = View.VISIBLE; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 608 | if (DBG) Log.d(LOG_TAG, "Using badge icon: " + mSearchable.getIconId()); |
Bjorn Bringert | a920413 | 2009-05-05 14:06:35 +0100 | [diff] [blame] | 609 | } else if (mSearchable.useBadgeLabel()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | text = mActivityContext.getResources().getText(mSearchable.getLabelId()).toString(); |
| 611 | visibility = View.VISIBLE; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 612 | 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] | 613 | } |
| 614 | |
| 615 | mBadgeLabel.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null); |
| 616 | mBadgeLabel.setText(text); |
| 617 | mBadgeLabel.setVisibility(visibility); |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Update the hint in the query text field. |
| 622 | */ |
| 623 | private void updateQueryHint() { |
| 624 | if (isShowing()) { |
| 625 | String hint = null; |
| 626 | if (mSearchable != null) { |
| 627 | int hintId = mSearchable.getHintId(); |
| 628 | if (hintId != 0) { |
| 629 | hint = mActivityContext.getString(hintId); |
| 630 | } |
| 631 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 632 | mSearchAutoComplete.setHint(hint); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Update the visibility of the voice button. There are actually two voice search modes, |
| 638 | * either of which will activate the button. |
| 639 | */ |
| 640 | private void updateVoiceButton() { |
| 641 | int visibility = View.GONE; |
| 642 | if (mSearchable.getVoiceSearchEnabled()) { |
| 643 | Intent testIntent = null; |
| 644 | if (mSearchable.getVoiceSearchLaunchWebSearch()) { |
| 645 | testIntent = mVoiceWebSearchIntent; |
| 646 | } else if (mSearchable.getVoiceSearchLaunchRecognizer()) { |
| 647 | testIntent = mVoiceAppSearchIntent; |
| 648 | } |
| 649 | if (testIntent != null) { |
| 650 | ResolveInfo ri = getContext().getPackageManager(). |
| 651 | resolveActivity(testIntent, PackageManager.MATCH_DEFAULT_ONLY); |
| 652 | if (ri != null) { |
| 653 | visibility = View.VISIBLE; |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | mVoiceButton.setVisibility(visibility); |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * Listeners of various types |
| 662 | */ |
| 663 | |
| 664 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 665 | * {@link Dialog#onTouchEvent(MotionEvent)} will cancel the dialog only when the |
| 666 | * touch is outside the window. But the window includes space for the drop-down, |
| 667 | * so we also cancel on taps outside the search bar when the drop-down is not showing. |
| 668 | */ |
| 669 | @Override |
| 670 | public boolean onTouchEvent(MotionEvent event) { |
| 671 | // cancel if the drop-down is not showing and the touch event was outside the search plate |
| 672 | if (!mSearchAutoComplete.isPopupShowing() && isOutOfBounds(mSearchPlate, event)) { |
| 673 | if (DBG) Log.d(LOG_TAG, "Pop-up not showing and outside of search plate."); |
| 674 | cancel(); |
| 675 | return true; |
| 676 | } |
| 677 | // Let Dialog handle events outside the window while the pop-up is showing. |
| 678 | return super.onTouchEvent(event); |
| 679 | } |
| 680 | |
| 681 | private boolean isOutOfBounds(View v, MotionEvent event) { |
| 682 | final int x = (int) event.getX(); |
| 683 | final int y = (int) event.getY(); |
| 684 | final int slop = ViewConfiguration.get(mContext).getScaledWindowTouchSlop(); |
| 685 | return (x < -slop) || (y < -slop) |
| 686 | || (x > (v.getWidth()+slop)) |
| 687 | || (y > (v.getHeight()+slop)); |
| 688 | } |
| 689 | |
| 690 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | * Dialog's OnKeyListener implements various search-specific functionality |
| 692 | * |
| 693 | * @param keyCode This is the keycode of the typed key, and is the same value as |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 694 | * found in the KeyEvent parameter. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | * @param event The complete event record for the typed key |
| 696 | * |
| 697 | * @return Return true if the event was handled here, or false if not. |
| 698 | */ |
| 699 | @Override |
| 700 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 701 | if (DBG) Log.d(LOG_TAG, "onKeyDown(" + keyCode + "," + event + ")"); |
Bjorn Bringert | ee716fa | 2009-07-16 09:15:37 +0100 | [diff] [blame^] | 702 | if (mSearchable == null) { |
| 703 | return false; |
| 704 | } |
| 705 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 706 | // handle back key to go back to previous searchable, etc. |
| 707 | if (handleBackKey(keyCode, event)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 708 | return true; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 711 | if (keyCode == KeyEvent.KEYCODE_SEARCH) { |
Mike LeBeau | b3aab69 | 2009-04-30 02:09:09 -0700 | [diff] [blame] | 712 | // If the search key is pressed, toggle between global and in-app search. If we are |
| 713 | // currently doing global search and there is no in-app search context to toggle to, |
| 714 | // just don't do anything. |
| 715 | return toggleGlobalSearch(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 717 | |
| 718 | // if it's an action specified by the searchable activity, launch the |
| 719 | // entered query with the action key |
| 720 | SearchableInfo.ActionKeyInfo actionKey = mSearchable.findActionKey(keyCode); |
Bjorn Bringert | a920413 | 2009-05-05 14:06:35 +0100 | [diff] [blame] | 721 | if ((actionKey != null) && (actionKey.getQueryActionMsg() != null)) { |
| 722 | launchQuerySearch(keyCode, actionKey.getQueryActionMsg()); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 723 | return true; |
| 724 | } |
| 725 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | return false; |
| 727 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 728 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | /** |
| 730 | * Callback to watch the textedit field for empty/non-empty |
| 731 | */ |
| 732 | private TextWatcher mTextWatcher = new TextWatcher() { |
| 733 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 734 | public void beforeTextChanged(CharSequence s, int start, int before, int after) { } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | |
| 736 | public void onTextChanged(CharSequence s, int start, |
| 737 | int before, int after) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 738 | if (DBG_LOG_TIMING) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | dbgLogTiming("onTextChanged()"); |
| 740 | } |
Bjorn Bringert | ee716fa | 2009-07-16 09:15:37 +0100 | [diff] [blame^] | 741 | if (mSearchable == null) { |
| 742 | return; |
| 743 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 | updateWidgetState(); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 745 | if (!mSearchAutoComplete.isPerformingCompletion()) { |
| 746 | // The user changed the query, remember it. |
| 747 | mUserQuery = s == null ? "" : s.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
Satish Sampath | 662df0b | 2009-06-22 23:16:07 +0100 | [diff] [blame] | 751 | public void afterTextChanged(Editable s) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 752 | if (mSearchable == null) { |
| 753 | return; |
| 754 | } |
Satish Sampath | d21572c | 2009-07-08 14:54:11 +0100 | [diff] [blame] | 755 | if (mSearchable.autoUrlDetect() && !mSearchAutoComplete.isPerformingCompletion()) { |
Satish Sampath | 662df0b | 2009-06-22 23:16:07 +0100 | [diff] [blame] | 756 | // The user changed the query, check if it is a URL and if so change the search |
| 757 | // button in the soft keyboard to the 'Go' button. |
| 758 | int options = (mSearchAutoComplete.getImeOptions() & (~EditorInfo.IME_MASK_ACTION)); |
| 759 | if (Regex.WEB_URL_PATTERN.matcher(mUserQuery).matches()) { |
| 760 | options = options | EditorInfo.IME_ACTION_GO; |
| 761 | } else { |
| 762 | options = options | EditorInfo.IME_ACTION_SEARCH; |
| 763 | } |
| 764 | if (options != mSearchAutoCompleteImeOptions) { |
| 765 | mSearchAutoCompleteImeOptions = options; |
| 766 | mSearchAutoComplete.setImeOptions(options); |
| 767 | // This call is required to update the soft keyboard UI with latest IME flags. |
| 768 | mSearchAutoComplete.setInputType(mSearchAutoComplete.getInputType()); |
| 769 | } |
| 770 | } |
| 771 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | }; |
| 773 | |
| 774 | /** |
| 775 | * Enable/Disable the cancel button based on edit text state (any text?) |
| 776 | */ |
| 777 | private void updateWidgetState() { |
| 778 | // enable the button if we have one or more non-space characters |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 779 | boolean enabled = !mSearchAutoComplete.isEmpty(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | mGoButton.setEnabled(enabled); |
| 781 | mGoButton.setFocusable(enabled); |
| 782 | } |
| 783 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | /** |
| 785 | * React to typing in the GO search button by refocusing to EditText. |
| 786 | * Continue typing the query. |
| 787 | */ |
| 788 | View.OnKeyListener mButtonsKeyListener = new View.OnKeyListener() { |
| 789 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 790 | // guard against possible race conditions |
| 791 | if (mSearchable == null) { |
| 792 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 794 | |
| 795 | if (!event.isSystem() && |
| 796 | (keyCode != KeyEvent.KEYCODE_DPAD_UP) && |
| 797 | (keyCode != KeyEvent.KEYCODE_DPAD_DOWN) && |
| 798 | (keyCode != KeyEvent.KEYCODE_DPAD_LEFT) && |
| 799 | (keyCode != KeyEvent.KEYCODE_DPAD_RIGHT) && |
| 800 | (keyCode != KeyEvent.KEYCODE_DPAD_CENTER)) { |
| 801 | // restore focus and give key to EditText ... |
| 802 | if (mSearchAutoComplete.requestFocus()) { |
| 803 | return mSearchAutoComplete.dispatchKeyEvent(event); |
| 804 | } |
| 805 | } |
| 806 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | return false; |
| 808 | } |
| 809 | }; |
| 810 | |
| 811 | /** |
| 812 | * React to a click in the GO button by launching a search. |
| 813 | */ |
| 814 | View.OnClickListener mGoButtonClickListener = new View.OnClickListener() { |
| 815 | public void onClick(View v) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 816 | // guard against possible race conditions |
| 817 | if (mSearchable == null) { |
| 818 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 820 | launchQuerySearch(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | } |
| 822 | }; |
| 823 | |
| 824 | /** |
| 825 | * React to a click in the voice search button. |
| 826 | */ |
| 827 | View.OnClickListener mVoiceButtonClickListener = new View.OnClickListener() { |
| 828 | public void onClick(View v) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 829 | // guard against possible race conditions |
| 830 | if (mSearchable == null) { |
| 831 | return; |
| 832 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | try { |
| 834 | if (mSearchable.getVoiceSearchLaunchWebSearch()) { |
| 835 | getContext().startActivity(mVoiceWebSearchIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | } else if (mSearchable.getVoiceSearchLaunchRecognizer()) { |
| 837 | Intent appSearchIntent = createVoiceAppSearchIntent(mVoiceAppSearchIntent); |
| 838 | getContext().startActivity(appSearchIntent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 839 | } |
| 840 | } catch (ActivityNotFoundException e) { |
| 841 | // Should not happen, since we check the availability of |
| 842 | // voice search before showing the button. But just in case... |
| 843 | Log.w(LOG_TAG, "Could not find voice search activity"); |
| 844 | } |
| 845 | } |
| 846 | }; |
| 847 | |
| 848 | /** |
| 849 | * Create and return an Intent that can launch the voice search activity, perform a specific |
| 850 | * voice transcription, and forward the results to the searchable activity. |
| 851 | * |
| 852 | * @param baseIntent The voice app search intent to start from |
| 853 | * @return A completely-configured intent ready to send to the voice search activity |
| 854 | */ |
| 855 | private Intent createVoiceAppSearchIntent(Intent baseIntent) { |
Mike LeBeau | 260dfb5 | 2009-07-15 15:20:14 -0700 | [diff] [blame] | 856 | ComponentName searchActivity = mSearchable.getSearchActivity(); |
| 857 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | // create the necessary intent to set up a search-and-forward operation |
| 859 | // in the voice search system. We have to keep the bundle separate, |
| 860 | // because it becomes immutable once it enters the PendingIntent |
| 861 | Intent queryIntent = new Intent(Intent.ACTION_SEARCH); |
Mike LeBeau | 260dfb5 | 2009-07-15 15:20:14 -0700 | [diff] [blame] | 862 | queryIntent.setComponent(searchActivity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | PendingIntent pending = PendingIntent.getActivity( |
| 864 | getContext(), 0, queryIntent, PendingIntent.FLAG_ONE_SHOT); |
| 865 | |
| 866 | // Now set up the bundle that will be inserted into the pending intent |
| 867 | // when it's time to do the search. We always build it here (even if empty) |
| 868 | // because the voice search activity will always need to insert "QUERY" into |
| 869 | // it anyway. |
| 870 | Bundle queryExtras = new Bundle(); |
| 871 | if (mAppSearchData != null) { |
| 872 | queryExtras.putBundle(SearchManager.APP_DATA, mAppSearchData); |
| 873 | } |
| 874 | |
| 875 | // Now build the intent to launch the voice search. Add all necessary |
| 876 | // extras to launch the voice recognizer, and then all the necessary extras |
| 877 | // to forward the results to the searchable activity |
| 878 | Intent voiceIntent = new Intent(baseIntent); |
| 879 | |
| 880 | // Add all of the configuration options supplied by the searchable's metadata |
| 881 | String languageModel = RecognizerIntent.LANGUAGE_MODEL_FREE_FORM; |
| 882 | String prompt = null; |
| 883 | String language = null; |
| 884 | int maxResults = 1; |
| 885 | Resources resources = mActivityContext.getResources(); |
| 886 | if (mSearchable.getVoiceLanguageModeId() != 0) { |
| 887 | languageModel = resources.getString(mSearchable.getVoiceLanguageModeId()); |
| 888 | } |
| 889 | if (mSearchable.getVoicePromptTextId() != 0) { |
| 890 | prompt = resources.getString(mSearchable.getVoicePromptTextId()); |
| 891 | } |
| 892 | if (mSearchable.getVoiceLanguageId() != 0) { |
| 893 | language = resources.getString(mSearchable.getVoiceLanguageId()); |
| 894 | } |
| 895 | if (mSearchable.getVoiceMaxResults() != 0) { |
| 896 | maxResults = mSearchable.getVoiceMaxResults(); |
| 897 | } |
| 898 | voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, languageModel); |
| 899 | voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt); |
| 900 | voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language); |
| 901 | voiceIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxResults); |
Mike LeBeau | 260dfb5 | 2009-07-15 15:20:14 -0700 | [diff] [blame] | 902 | voiceIntent.putExtra(EXTRA_CALLING_PACKAGE, |
| 903 | searchActivity == null ? null : searchActivity.toShortString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | |
| 905 | // Add the values that configure forwarding the results |
| 906 | voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pending); |
| 907 | voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT_BUNDLE, queryExtras); |
| 908 | |
| 909 | return voiceIntent; |
| 910 | } |
| 911 | |
| 912 | /** |
Satish Sampath | 662df0b | 2009-06-22 23:16:07 +0100 | [diff] [blame] | 913 | * Corrects http/https typo errors in the given url string, and if the protocol specifier was |
| 914 | * not present defaults to http. |
| 915 | * |
| 916 | * @param inUrl URL to check and fix |
| 917 | * @return fixed URL string. |
| 918 | */ |
| 919 | private String fixUrl(String inUrl) { |
| 920 | if (inUrl.startsWith("http://") || inUrl.startsWith("https://")) |
| 921 | return inUrl; |
| 922 | |
| 923 | if (inUrl.startsWith("http:") || inUrl.startsWith("https:")) { |
| 924 | if (inUrl.startsWith("http:/") || inUrl.startsWith("https:/")) { |
| 925 | inUrl = inUrl.replaceFirst("/", "//"); |
| 926 | } else { |
| 927 | inUrl = inUrl.replaceFirst(":", "://"); |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | if (inUrl.indexOf("://") == -1) { |
| 932 | inUrl = "http://" + inUrl; |
| 933 | } |
| 934 | |
| 935 | return inUrl; |
| 936 | } |
| 937 | |
| 938 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | * React to the user typing "enter" or other hardwired keys while typing in the search box. |
| 940 | * This handles these special keys while the edit box has focus. |
| 941 | */ |
| 942 | View.OnKeyListener mTextKeyListener = new View.OnKeyListener() { |
| 943 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 944 | // guard against possible race conditions |
| 945 | if (mSearchable == null) { |
| 946 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 948 | |
| 949 | if (DBG_LOG_TIMING) dbgLogTiming("doTextKey()"); |
| 950 | if (DBG) { |
| 951 | Log.d(LOG_TAG, "mTextListener.onKey(" + keyCode + "," + event |
| 952 | + "), selection: " + mSearchAutoComplete.getListSelection()); |
| 953 | } |
| 954 | |
| 955 | // If a suggestion is selected, handle enter, search key, and action keys |
| 956 | // as presses on the selected suggestion |
| 957 | if (mSearchAutoComplete.isPopupShowing() && |
| 958 | mSearchAutoComplete.getListSelection() != ListView.INVALID_POSITION) { |
| 959 | return onSuggestionsKey(v, keyCode, event); |
| 960 | } |
| 961 | |
| 962 | // If there is text in the query box, handle enter, and action keys |
| 963 | // The search key is handled by the dialog's onKeyDown(). |
| 964 | if (!mSearchAutoComplete.isEmpty()) { |
| 965 | if (keyCode == KeyEvent.KEYCODE_ENTER |
| 966 | && event.getAction() == KeyEvent.ACTION_UP) { |
| 967 | v.cancelLongPress(); |
Satish Sampath | 662df0b | 2009-06-22 23:16:07 +0100 | [diff] [blame] | 968 | |
Satish Sampath | b1665f2 | 2009-07-10 15:43:38 +0100 | [diff] [blame] | 969 | // If this is a url entered by the user & we displayed the 'Go' button which |
| 970 | // the user clicked, launch the url instead of using it as a search query. |
| 971 | if (mSearchable.autoUrlDetect() && |
| 972 | (mSearchAutoCompleteImeOptions & EditorInfo.IME_MASK_ACTION) |
Satish Sampath | d21572c | 2009-07-08 14:54:11 +0100 | [diff] [blame] | 973 | == EditorInfo.IME_ACTION_GO) { |
Satish Sampath | b1665f2 | 2009-07-10 15:43:38 +0100 | [diff] [blame] | 974 | Uri uri = Uri.parse(fixUrl(mSearchAutoComplete.getText().toString())); |
| 975 | Intent intent = new Intent(Intent.ACTION_VIEW, uri); |
| 976 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 977 | launchIntent(intent); |
| 978 | } else { |
| 979 | // Launch as a regular search. |
| 980 | launchQuerySearch(); |
Satish Sampath | 662df0b | 2009-06-22 23:16:07 +0100 | [diff] [blame] | 981 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 982 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 984 | if (event.getAction() == KeyEvent.ACTION_DOWN) { |
| 985 | SearchableInfo.ActionKeyInfo actionKey = mSearchable.findActionKey(keyCode); |
Bjorn Bringert | a920413 | 2009-05-05 14:06:35 +0100 | [diff] [blame] | 986 | if ((actionKey != null) && (actionKey.getQueryActionMsg() != null)) { |
| 987 | launchQuerySearch(keyCode, actionKey.getQueryActionMsg()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | return true; |
| 989 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | return false; |
| 993 | } |
| 994 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 995 | |
Dianne Hackborn | a8f556e | 2009-03-24 20:47:50 -0700 | [diff] [blame] | 996 | @Override |
Bjorn Bringert | ec8ee34 | 2009-07-08 21:49:42 +0100 | [diff] [blame] | 997 | public void dismiss() { |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 998 | if (!isShowing()) return; |
| 999 | |
Dianne Hackborn | a8f556e | 2009-03-24 20:47:50 -0700 | [diff] [blame] | 1000 | // We made sure the IME was displayed, so also make sure it is closed |
| 1001 | // when we go away. |
| 1002 | InputMethodManager imm = (InputMethodManager)getContext() |
| 1003 | .getSystemService(Context.INPUT_METHOD_SERVICE); |
| 1004 | if (imm != null) { |
| 1005 | imm.hideSoftInputFromWindow( |
| 1006 | getWindow().getDecorView().getWindowToken(), 0); |
| 1007 | } |
| 1008 | |
Bjorn Bringert | ec8ee34 | 2009-07-08 21:49:42 +0100 | [diff] [blame] | 1009 | super.dismiss(); |
Dianne Hackborn | a8f556e | 2009-03-24 20:47:50 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1012 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1013 | * React to the user typing while in the suggestions list. First, check for action |
| 1014 | * keys. If not handled, try refocusing regular characters into the EditText. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 | */ |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1016 | private boolean onSuggestionsKey(View v, int keyCode, KeyEvent event) { |
| 1017 | // guard against possible race conditions (late arrival after dismiss) |
| 1018 | if (mSearchable == null) { |
| 1019 | return false; |
| 1020 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1021 | if (mSuggestionsAdapter == null) { |
| 1022 | return false; |
| 1023 | } |
| 1024 | if (event.getAction() == KeyEvent.ACTION_DOWN) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1025 | if (DBG_LOG_TIMING) { |
| 1026 | dbgLogTiming("onSuggestionsKey()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | // First, check for enter or search (both of which we'll treat as a "click") |
| 1030 | if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_SEARCH) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1031 | int position = mSearchAutoComplete.getListSelection(); |
| 1032 | return launchSuggestion(position); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | // Next, check for left/right moves, which we use to "return" the user to the edit view |
| 1036 | if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1037 | // give "focus" to text editor, with cursor at the beginning if |
| 1038 | // left key, at end if right key |
| 1039 | // TODO: Reverse left/right for right-to-left languages, e.g. Arabic |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | int selPoint = (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) ? |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1041 | 0 : mSearchAutoComplete.length(); |
| 1042 | mSearchAutoComplete.setSelection(selPoint); |
| 1043 | mSearchAutoComplete.setListSelection(0); |
| 1044 | mSearchAutoComplete.clearListSelection(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | return true; |
| 1046 | } |
| 1047 | |
| 1048 | // Next, check for an "up and out" move |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1049 | if (keyCode == KeyEvent.KEYCODE_DPAD_UP |
| 1050 | && 0 == mSearchAutoComplete.getListSelection()) { |
| 1051 | restoreUserQuery(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1052 | // let ACTV complete the move |
| 1053 | return false; |
| 1054 | } |
| 1055 | |
| 1056 | // Next, check for an "action key" |
| 1057 | SearchableInfo.ActionKeyInfo actionKey = mSearchable.findActionKey(keyCode); |
| 1058 | if ((actionKey != null) && |
Bjorn Bringert | a920413 | 2009-05-05 14:06:35 +0100 | [diff] [blame] | 1059 | ((actionKey.getSuggestActionMsg() != null) || |
| 1060 | (actionKey.getSuggestActionMsgColumn() != null))) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1061 | // launch suggestion using action key column |
| 1062 | int position = mSearchAutoComplete.getListSelection(); |
| 1063 | if (position != ListView.INVALID_POSITION) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | Cursor c = mSuggestionsAdapter.getCursor(); |
| 1065 | if (c.moveToPosition(position)) { |
| 1066 | final String actionMsg = getActionKeyMessage(c, actionKey); |
| 1067 | if (actionMsg != null && (actionMsg.length() > 0)) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1068 | return launchSuggestion(position, keyCode, actionMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | return false; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1078 | * 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] | 1079 | */ |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1080 | protected void launchQuerySearch() { |
| 1081 | launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1085 | * 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] | 1086 | * |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1087 | * @param actionKey The key code of the action key that was pressed, |
| 1088 | * or {@link KeyEvent#KEYCODE_UNKNOWN} if none. |
| 1089 | * @param actionMsg The message for the action key that was pressed, |
| 1090 | * or <code>null</code> if none. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | */ |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1092 | protected void launchQuerySearch(int actionKey, String actionMsg) { |
| 1093 | String query = mSearchAutoComplete.getText().toString(); |
Satish Sampath | bf23fe0 | 2009-06-15 23:47:56 +0100 | [diff] [blame] | 1094 | Intent intent = createIntent(Intent.ACTION_SEARCH, null, null, query, null, |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1095 | actionKey, actionMsg); |
| 1096 | launchIntent(intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1097 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1098 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1100 | * Launches an intent based on a suggestion. |
| 1101 | * |
| 1102 | * @param position The index of the suggestion to create the intent from. |
| 1103 | * @return true if a successful launch, false if could not (e.g. bad position). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1104 | */ |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1105 | protected boolean launchSuggestion(int position) { |
| 1106 | return launchSuggestion(position, KeyEvent.KEYCODE_UNKNOWN, null); |
| 1107 | } |
| 1108 | |
| 1109 | /** |
| 1110 | * Launches an intent based on a suggestion. |
| 1111 | * |
| 1112 | * @param position The index of the suggestion to create the intent from. |
| 1113 | * @param actionKey The key code of the action key that was pressed, |
| 1114 | * or {@link KeyEvent#KEYCODE_UNKNOWN} if none. |
| 1115 | * @param actionMsg The message for the action key that was pressed, |
| 1116 | * or <code>null</code> if none. |
| 1117 | * @return true if a successful launch, false if could not (e.g. bad position). |
| 1118 | */ |
| 1119 | protected boolean launchSuggestion(int position, int actionKey, String actionMsg) { |
| 1120 | Cursor c = mSuggestionsAdapter.getCursor(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1121 | if ((c != null) && c.moveToPosition(position)) { |
Karl Rosaen | d4c98c4 | 2009-06-09 17:05:54 +0100 | [diff] [blame] | 1122 | |
| 1123 | Intent intent = createIntentFromSuggestion(c, actionKey, actionMsg); |
| 1124 | |
| 1125 | // report back about the click |
| 1126 | if (mGlobalSearchMode) { |
| 1127 | // in global search mode, do it via cursor |
| 1128 | mSuggestionsAdapter.callCursorOnClick(c, position); |
| 1129 | } else if (intent != null |
| 1130 | && mPreviousComponents != null |
| 1131 | && !mPreviousComponents.isEmpty()) { |
| 1132 | // in-app search (and we have pivoted in as told by mPreviousComponents, |
| 1133 | // which is used for keeping track of what we pop back to when we are pivoting into |
| 1134 | // in app search.) |
| 1135 | reportInAppClickToGlobalSearch(c, intent); |
| 1136 | } |
Karl Rosaen | a058f02 | 2009-06-01 23:11:44 +0100 | [diff] [blame] | 1137 | |
| 1138 | // launch the intent |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1139 | launchIntent(intent); |
Karl Rosaen | a058f02 | 2009-06-01 23:11:44 +0100 | [diff] [blame] | 1140 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1141 | return true; |
| 1142 | } |
| 1143 | return false; |
| 1144 | } |
Karl Rosaen | a058f02 | 2009-06-01 23:11:44 +0100 | [diff] [blame] | 1145 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1146 | /** |
Karl Rosaen | d4c98c4 | 2009-06-09 17:05:54 +0100 | [diff] [blame] | 1147 | * Report a click from an in app search result back to global search for shortcutting porpoises. |
| 1148 | * |
| 1149 | * @param c The cursor that is pointing to the clicked position. |
| 1150 | * @param intent The intent that will be launched for the click. |
| 1151 | */ |
| 1152 | private void reportInAppClickToGlobalSearch(Cursor c, Intent intent) { |
| 1153 | // for in app search, still tell global search via content provider |
| 1154 | Uri uri = getClickReportingUri(); |
| 1155 | final ContentValues cv = new ContentValues(); |
| 1156 | cv.put(SearchManager.SEARCH_CLICK_REPORT_COLUMN_QUERY, mUserQuery); |
| 1157 | final ComponentName source = mSearchable.getSearchActivity(); |
| 1158 | cv.put(SearchManager.SEARCH_CLICK_REPORT_COLUMN_COMPONENT, source.flattenToShortString()); |
| 1159 | |
| 1160 | // grab the intent columns from the intent we created since it has additional |
| 1161 | // logic for falling back on the searchable default |
| 1162 | cv.put(SearchManager.SUGGEST_COLUMN_INTENT_ACTION, intent.getAction()); |
| 1163 | cv.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA, intent.getDataString()); |
Satish Sampath | bf23fe0 | 2009-06-15 23:47:56 +0100 | [diff] [blame] | 1164 | cv.put(SearchManager.SUGGEST_COLUMN_INTENT_COMPONENT_NAME, |
| 1165 | intent.getStringExtra(SearchManager.COMPONENT_NAME_KEY)); |
Karl Rosaen | d4c98c4 | 2009-06-09 17:05:54 +0100 | [diff] [blame] | 1166 | |
| 1167 | // ensure the icons will work for global search |
| 1168 | cv.put(SearchManager.SUGGEST_COLUMN_ICON_1, |
| 1169 | wrapIconForPackage( |
| 1170 | source, |
| 1171 | getColumnString(c, SearchManager.SUGGEST_COLUMN_ICON_1))); |
| 1172 | cv.put(SearchManager.SUGGEST_COLUMN_ICON_2, |
| 1173 | wrapIconForPackage( |
| 1174 | source, |
| 1175 | getColumnString(c, SearchManager.SUGGEST_COLUMN_ICON_2))); |
| 1176 | |
| 1177 | // the rest can be passed through directly |
| 1178 | cv.put(SearchManager.SUGGEST_COLUMN_FORMAT, |
| 1179 | getColumnString(c, SearchManager.SUGGEST_COLUMN_FORMAT)); |
| 1180 | cv.put(SearchManager.SUGGEST_COLUMN_TEXT_1, |
| 1181 | getColumnString(c, SearchManager.SUGGEST_COLUMN_TEXT_1)); |
| 1182 | cv.put(SearchManager.SUGGEST_COLUMN_TEXT_2, |
| 1183 | getColumnString(c, SearchManager.SUGGEST_COLUMN_TEXT_2)); |
| 1184 | cv.put(SearchManager.SUGGEST_COLUMN_QUERY, |
| 1185 | getColumnString(c, SearchManager.SUGGEST_COLUMN_QUERY)); |
| 1186 | cv.put(SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, |
| 1187 | getColumnString(c, SearchManager.SUGGEST_COLUMN_SHORTCUT_ID)); |
| 1188 | // note: deliberately omitting background color since it is only for global search |
| 1189 | // "more results" entries |
| 1190 | mContext.getContentResolver().insert(uri, cv); |
| 1191 | } |
| 1192 | |
| 1193 | /** |
| 1194 | * @return A URI appropriate for reporting a click. |
| 1195 | */ |
| 1196 | private Uri getClickReportingUri() { |
| 1197 | Uri.Builder uriBuilder = new Uri.Builder() |
| 1198 | .scheme(ContentResolver.SCHEME_CONTENT) |
| 1199 | .authority(SearchManager.SEARCH_CLICK_REPORT_AUTHORITY); |
| 1200 | |
| 1201 | uriBuilder.appendPath(SearchManager.SEARCH_CLICK_REPORT_URI_PATH); |
| 1202 | |
| 1203 | return uriBuilder |
| 1204 | .query("") // TODO: Remove, workaround for a bug in Uri.writeToParcel() |
| 1205 | .fragment("") // TODO: Remove, workaround for a bug in Uri.writeToParcel() |
| 1206 | .build(); |
| 1207 | } |
| 1208 | |
| 1209 | /** |
| 1210 | * Wraps an icon for a particular package. If the icon is a resource id, it is converted into |
| 1211 | * an android.resource:// URI. |
| 1212 | * |
| 1213 | * @param source The source of the icon |
| 1214 | * @param icon The icon retrieved from a suggestion column |
| 1215 | * @return An icon string appropriate for the package. |
| 1216 | */ |
| 1217 | private String wrapIconForPackage(ComponentName source, String icon) { |
| 1218 | if (icon == null || icon.length() == 0 || "0".equals(icon)) { |
| 1219 | // SearchManager specifies that null or zero can be returned to indicate |
| 1220 | // no icon. We also allow empty string. |
| 1221 | return null; |
| 1222 | } else if (!Character.isDigit(icon.charAt(0))){ |
| 1223 | return icon; |
| 1224 | } else { |
| 1225 | String packageName = source.getPackageName(); |
| 1226 | return new Uri.Builder() |
| 1227 | .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) |
| 1228 | .authority(packageName) |
| 1229 | .encodedPath(icon) |
| 1230 | .toString(); |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | /** |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 1235 | * Launches an intent and dismisses the search dialog (unless the intent |
| 1236 | * is one of the special intents that modifies the state of the search dialog). |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1237 | */ |
| 1238 | private void launchIntent(Intent intent) { |
| 1239 | if (intent == null) { |
| 1240 | return; |
| 1241 | } |
| 1242 | if (handleSpecialIntent(intent)){ |
| 1243 | return; |
| 1244 | } |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 1245 | dismiss(); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1246 | getContext().startActivity(intent); |
| 1247 | } |
| 1248 | |
| 1249 | /** |
| 1250 | * Handles the special intent actions declared in {@link SearchManager}. |
| 1251 | * |
| 1252 | * @return <code>true</code> if the intent was handled. |
| 1253 | */ |
| 1254 | private boolean handleSpecialIntent(Intent intent) { |
| 1255 | String action = intent.getAction(); |
| 1256 | if (SearchManager.INTENT_ACTION_CHANGE_SEARCH_SOURCE.equals(action)) { |
| 1257 | handleChangeSourceIntent(intent); |
| 1258 | return true; |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1259 | } |
| 1260 | return false; |
| 1261 | } |
| 1262 | |
| 1263 | /** |
Karl Rosaen | d4c98c4 | 2009-06-09 17:05:54 +0100 | [diff] [blame] | 1264 | * Handles {@link SearchManager#INTENT_ACTION_CHANGE_SEARCH_SOURCE}. |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1265 | */ |
| 1266 | private void handleChangeSourceIntent(Intent intent) { |
| 1267 | Uri dataUri = intent.getData(); |
| 1268 | if (dataUri == null) { |
| 1269 | Log.w(LOG_TAG, "SearchManager.INTENT_ACTION_CHANGE_SOURCE without intent data."); |
| 1270 | return; |
| 1271 | } |
| 1272 | ComponentName componentName = ComponentName.unflattenFromString(dataUri.toString()); |
| 1273 | if (componentName == null) { |
| 1274 | Log.w(LOG_TAG, "Invalid ComponentName: " + dataUri); |
| 1275 | return; |
| 1276 | } |
| 1277 | if (DBG) Log.d(LOG_TAG, "Switching to " + componentName); |
| 1278 | |
| 1279 | ComponentName previous = mLaunchComponent; |
| 1280 | if (!show(componentName, mAppSearchData, false)) { |
| 1281 | Log.w(LOG_TAG, "Failed to switch to source " + componentName); |
| 1282 | return; |
| 1283 | } |
| 1284 | pushPreviousComponent(previous); |
| 1285 | |
| 1286 | String query = intent.getStringExtra(SearchManager.QUERY); |
| 1287 | setUserQuery(query); |
Mike LeBeau | 35df87c | 2009-06-24 13:06:39 -0700 | [diff] [blame] | 1288 | mSearchAutoComplete.showDropDown(); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1289 | } |
Karl Rosaen | a058f02 | 2009-06-01 23:11:44 +0100 | [diff] [blame] | 1290 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1291 | /** |
Mike LeBeau | ae9760b | 2009-06-01 21:53:09 +0100 | [diff] [blame] | 1292 | * Sets the list item selection in the AutoCompleteTextView's ListView. |
| 1293 | */ |
| 1294 | public void setListSelection(int index) { |
| 1295 | mSearchAutoComplete.setListSelection(index); |
| 1296 | } |
Karl Rosaen | a058f02 | 2009-06-01 23:11:44 +0100 | [diff] [blame] | 1297 | |
Mike LeBeau | ae9760b | 2009-06-01 21:53:09 +0100 | [diff] [blame] | 1298 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1299 | * Saves the previous component that was searched, so that we can go |
| 1300 | * back to it. |
| 1301 | */ |
| 1302 | private void pushPreviousComponent(ComponentName componentName) { |
| 1303 | if (mPreviousComponents == null) { |
| 1304 | mPreviousComponents = new ArrayList<ComponentName>(); |
| 1305 | } |
| 1306 | mPreviousComponents.add(componentName); |
| 1307 | } |
| 1308 | |
| 1309 | /** |
| 1310 | * Pops the previous component off the stack and returns it. |
| 1311 | * |
| 1312 | * @return The component name, or <code>null</code> if there was |
| 1313 | * no previous component. |
| 1314 | */ |
| 1315 | private ComponentName popPreviousComponent() { |
| 1316 | if (mPreviousComponents == null) { |
| 1317 | return null; |
| 1318 | } |
| 1319 | int size = mPreviousComponents.size(); |
| 1320 | if (size == 0) { |
| 1321 | return null; |
| 1322 | } |
| 1323 | return mPreviousComponents.remove(size - 1); |
| 1324 | } |
| 1325 | |
| 1326 | /** |
| 1327 | * Goes back to the previous component that was searched, if any. |
| 1328 | * |
| 1329 | * @return <code>true</code> if there was a previous component that we could go back to. |
| 1330 | */ |
| 1331 | private boolean backToPreviousComponent() { |
| 1332 | ComponentName previous = popPreviousComponent(); |
| 1333 | if (previous == null) { |
| 1334 | return false; |
| 1335 | } |
| 1336 | if (!show(previous, mAppSearchData, false)) { |
| 1337 | Log.w(LOG_TAG, "Failed to switch to source " + previous); |
| 1338 | return false; |
| 1339 | } |
| 1340 | |
| 1341 | // must touch text to trigger suggestions |
| 1342 | // TODO: should this be the text as it was when the user left |
| 1343 | // the source that we are now going back to? |
| 1344 | String query = mSearchAutoComplete.getText().toString(); |
| 1345 | setUserQuery(query); |
| 1346 | |
| 1347 | return true; |
| 1348 | } |
| 1349 | |
| 1350 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1351 | * When a particular suggestion has been selected, perform the various lookups required |
| 1352 | * to use the suggestion. This includes checking the cursor for suggestion-specific data, |
| 1353 | * and/or falling back to the XML for defaults; It also creates REST style Uri data when |
| 1354 | * the suggestion includes a data id. |
| 1355 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1356 | * @param c The suggestions cursor, moved to the row of the user's selection |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1357 | * @param actionKey The key code of the action key that was pressed, |
| 1358 | * or {@link KeyEvent#KEYCODE_UNKNOWN} if none. |
| 1359 | * @param actionMsg The message for the action key that was pressed, |
| 1360 | * or <code>null</code> if none. |
| 1361 | * @return An intent for the suggestion at the cursor's position. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1362 | */ |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1363 | private Intent createIntentFromSuggestion(Cursor c, int actionKey, String actionMsg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1364 | try { |
| 1365 | // use specific action if supplied, or default action if supplied, or fixed default |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1366 | String action = getColumnString(c, SearchManager.SUGGEST_COLUMN_INTENT_ACTION); |
Karl Rosaen | a058f02 | 2009-06-01 23:11:44 +0100 | [diff] [blame] | 1367 | |
| 1368 | // some items are display only, or have effect via the cursor respond click reporting. |
| 1369 | if (SearchManager.INTENT_ACTION_NONE.equals(action)) { |
| 1370 | return null; |
| 1371 | } |
| 1372 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1373 | if (action == null) { |
| 1374 | action = mSearchable.getSuggestIntentAction(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1375 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1376 | if (action == null) { |
| 1377 | action = Intent.ACTION_SEARCH; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | // use specific data if supplied, or default data if supplied |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1381 | String data = getColumnString(c, SearchManager.SUGGEST_COLUMN_INTENT_DATA); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1382 | if (data == null) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1383 | data = mSearchable.getSuggestIntentData(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1384 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1385 | // then, if an ID was provided, append it. |
| 1386 | if (data != null) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1387 | String id = getColumnString(c, SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID); |
| 1388 | if (id != null) { |
| 1389 | data = data + "/" + Uri.encode(id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1390 | } |
| 1391 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1392 | Uri dataUri = (data == null) ? null : Uri.parse(data); |
| 1393 | |
Satish Sampath | bf23fe0 | 2009-06-15 23:47:56 +0100 | [diff] [blame] | 1394 | String componentName = getColumnString( |
| 1395 | c, SearchManager.SUGGEST_COLUMN_INTENT_COMPONENT_NAME); |
Karl Rosaen | a058f02 | 2009-06-01 23:11:44 +0100 | [diff] [blame] | 1396 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1397 | String query = getColumnString(c, SearchManager.SUGGEST_COLUMN_QUERY); |
Satish Sampath | bf23fe0 | 2009-06-15 23:47:56 +0100 | [diff] [blame] | 1398 | String extraData = getColumnString(c, SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1399 | |
Satish Sampath | bf23fe0 | 2009-06-15 23:47:56 +0100 | [diff] [blame] | 1400 | return createIntent(action, dataUri, extraData, query, componentName, actionKey, |
| 1401 | actionMsg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1402 | } catch (RuntimeException e ) { |
| 1403 | int rowNum; |
| 1404 | try { // be really paranoid now |
| 1405 | rowNum = c.getPosition(); |
| 1406 | } catch (RuntimeException e2 ) { |
| 1407 | rowNum = -1; |
| 1408 | } |
| 1409 | Log.w(LOG_TAG, "Search Suggestions cursor at row " + rowNum + |
| 1410 | " returned exception" + e.toString()); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1411 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1416 | * Constructs an intent from the given information and the search dialog state. |
| 1417 | * |
| 1418 | * @param action Intent action. |
| 1419 | * @param data Intent data, or <code>null</code>. |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1420 | * @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] | 1421 | * @param query Intent query, or <code>null</code>. |
| 1422 | * @param componentName Data for {@link SearchManager#COMPONENT_NAME_KEY} or <code>null</code>. |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1423 | * @param actionKey The key code of the action key that was pressed, |
| 1424 | * or {@link KeyEvent#KEYCODE_UNKNOWN} if none. |
| 1425 | * @param actionMsg The message for the action key that was pressed, |
| 1426 | * or <code>null</code> if none. |
| 1427 | * @return The intent. |
| 1428 | */ |
Satish Sampath | bf23fe0 | 2009-06-15 23:47:56 +0100 | [diff] [blame] | 1429 | private Intent createIntent(String action, Uri data, String extraData, String query, |
| 1430 | String componentName, int actionKey, String actionMsg) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1431 | // Now build the Intent |
| 1432 | Intent intent = new Intent(action); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 1433 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1434 | if (data != null) { |
| 1435 | intent.setData(data); |
| 1436 | } |
Bjorn Bringert | 5f80605 | 2009-06-24 12:02:26 +0100 | [diff] [blame] | 1437 | intent.putExtra(SearchManager.USER_QUERY, mUserQuery); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1438 | if (query != null) { |
| 1439 | intent.putExtra(SearchManager.QUERY, query); |
| 1440 | } |
| 1441 | if (extraData != null) { |
| 1442 | intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData); |
| 1443 | } |
Satish Sampath | bf23fe0 | 2009-06-15 23:47:56 +0100 | [diff] [blame] | 1444 | if (componentName != null) { |
| 1445 | intent.putExtra(SearchManager.COMPONENT_NAME_KEY, componentName); |
| 1446 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1447 | if (mAppSearchData != null) { |
| 1448 | intent.putExtra(SearchManager.APP_DATA, mAppSearchData); |
| 1449 | } |
| 1450 | if (actionKey != KeyEvent.KEYCODE_UNKNOWN) { |
| 1451 | intent.putExtra(SearchManager.ACTION_KEY, actionKey); |
| 1452 | intent.putExtra(SearchManager.ACTION_MSG, actionMsg); |
| 1453 | } |
| 1454 | // attempt to enforce security requirement (no 3rd-party intents) |
Bjorn Bringert | a920413 | 2009-05-05 14:06:35 +0100 | [diff] [blame] | 1455 | intent.setComponent(mSearchable.getSearchActivity()); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1456 | return intent; |
| 1457 | } |
| 1458 | |
| 1459 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | * For a given suggestion and a given cursor row, get the action message. If not provided |
| 1461 | * by the specific row/column, also check for a single definition (for the action key). |
| 1462 | * |
| 1463 | * @param c The cursor providing suggestions |
| 1464 | * @param actionKey The actionkey record being examined |
| 1465 | * |
| 1466 | * @return Returns a string, or null if no action key message for this suggestion |
| 1467 | */ |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1468 | private static String getActionKeyMessage(Cursor c, SearchableInfo.ActionKeyInfo actionKey) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1469 | String result = null; |
| 1470 | // check first in the cursor data, for a suggestion-specific message |
Bjorn Bringert | a920413 | 2009-05-05 14:06:35 +0100 | [diff] [blame] | 1471 | final String column = actionKey.getSuggestActionMsgColumn(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1472 | if (column != null) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1473 | result = SuggestionsAdapter.getColumnString(c, column); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1474 | } |
| 1475 | // If the cursor didn't give us a message, see if there's a single message defined |
| 1476 | // for the actionkey (for all suggestions) |
| 1477 | if (result == null) { |
Bjorn Bringert | a920413 | 2009-05-05 14:06:35 +0100 | [diff] [blame] | 1478 | result = actionKey.getSuggestActionMsg(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1479 | } |
| 1480 | return result; |
| 1481 | } |
| 1482 | |
| 1483 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1484 | * Local subclass for AutoCompleteTextView. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1485 | */ |
| 1486 | public static class SearchAutoComplete extends AutoCompleteTextView { |
| 1487 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1488 | private int mThreshold; |
| 1489 | private SearchDialog mSearchDialog; |
| 1490 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1491 | public SearchAutoComplete(Context context) { |
Marco Nelissen | 1746d6f | 2009-05-14 13:29:24 -0700 | [diff] [blame] | 1492 | super(context); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1493 | mThreshold = getThreshold(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | public SearchAutoComplete(Context context, AttributeSet attrs) { |
| 1497 | super(context, attrs); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1498 | mThreshold = getThreshold(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | public SearchAutoComplete(Context context, AttributeSet attrs, int defStyle) { |
| 1502 | super(context, attrs, defStyle); |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1503 | mThreshold = getThreshold(); |
| 1504 | } |
| 1505 | |
| 1506 | private void setSearchDialog(SearchDialog searchDialog) { |
| 1507 | mSearchDialog = searchDialog; |
| 1508 | } |
| 1509 | |
| 1510 | @Override |
| 1511 | public void setThreshold(int threshold) { |
| 1512 | super.setThreshold(threshold); |
| 1513 | mThreshold = threshold; |
| 1514 | } |
| 1515 | |
| 1516 | /** |
| 1517 | * Returns true if the text field is empty, or contains only whitespace. |
| 1518 | */ |
| 1519 | private boolean isEmpty() { |
| 1520 | return TextUtils.getTrimmedLength(getText()) == 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1521 | } |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 1522 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1523 | /** |
| 1524 | * We override this method to avoid replacing the query box text |
| 1525 | * when a suggestion is clicked. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1526 | */ |
| 1527 | @Override |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1528 | protected void replaceText(CharSequence text) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | /** |
Mike LeBeau | 617202a | 2009-07-06 14:29:25 -0700 | [diff] [blame] | 1532 | * We override this method to avoid an extra onItemClick being called on the |
| 1533 | * drop-down's OnItemClickListener by {@link AutoCompleteTextView#onKeyUp(int, KeyEvent)} |
| 1534 | * when an item is clicked with the trackball. |
| 1535 | */ |
| 1536 | @Override |
| 1537 | public void performCompletion() { |
| 1538 | } |
Mike LeBeau | 1fffbd9 | 2009-07-14 15:57:41 -0700 | [diff] [blame] | 1539 | |
| 1540 | /** |
| 1541 | * We override this method to be sure and show the soft keyboard if appropriate when |
| 1542 | * the TextView has focus. |
| 1543 | */ |
| 1544 | @Override |
| 1545 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 1546 | super.onWindowFocusChanged(hasWindowFocus); |
| 1547 | |
| 1548 | if (hasWindowFocus) { |
| 1549 | InputMethodManager inputManager = (InputMethodManager) |
| 1550 | getContext().getSystemService(Context.INPUT_METHOD_SERVICE); |
| 1551 | inputManager.showSoftInput(this, 0); |
| 1552 | } |
| 1553 | } |
| 1554 | |
Mike LeBeau | 617202a | 2009-07-06 14:29:25 -0700 | [diff] [blame] | 1555 | /** |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1556 | * We override this method so that we can allow a threshold of zero, which ACTV does not. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1557 | */ |
| 1558 | @Override |
| 1559 | public boolean enoughToFilter() { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1560 | return mThreshold <= 0 || super.enoughToFilter(); |
| 1561 | } |
Karl Rosaen | 98e333f | 2009-04-28 10:39:09 -0700 | [diff] [blame] | 1562 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1563 | /** |
| 1564 | * {@link AutoCompleteTextView#onKeyPreIme(int, KeyEvent)}) dismisses the drop-down on BACK, |
| 1565 | * so we must override this method to modify the BACK behavior. |
| 1566 | */ |
| 1567 | @Override |
| 1568 | public boolean onKeyPreIme(int keyCode, KeyEvent event) { |
Bjorn Bringert | ee716fa | 2009-07-16 09:15:37 +0100 | [diff] [blame^] | 1569 | if (mSearchDialog.mSearchable == null) { |
| 1570 | return false; |
| 1571 | } |
Karl Rosaen | 98e333f | 2009-04-28 10:39:09 -0700 | [diff] [blame] | 1572 | if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) { |
| 1573 | if (mSearchDialog.backToPreviousComponent()) { |
| 1574 | return true; |
| 1575 | } |
| 1576 | return false; // will dismiss soft keyboard if necessary |
| 1577 | } |
| 1578 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1582 | protected boolean handleBackKey(int keyCode, KeyEvent event) { |
| 1583 | if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1584 | if (backToPreviousComponent()) { |
| 1585 | return true; |
| 1586 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1587 | cancel(); |
| 1588 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1589 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1590 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | /** |
| 1594 | * Implements OnItemClickListener |
| 1595 | */ |
| 1596 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1597 | if (DBG) Log.d(LOG_TAG, "onItemClick() position " + position); |
| 1598 | launchSuggestion(position); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1599 | } |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1600 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1601 | /** |
| 1602 | * Implements OnItemSelectedListener |
| 1603 | */ |
| 1604 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1605 | if (DBG) Log.d(LOG_TAG, "onItemSelected() position " + position); |
| 1606 | // A suggestion has been selected, rewrite the query if possible, |
| 1607 | // otherwise the restore the original query. |
| 1608 | if (REWRITE_QUERIES) { |
| 1609 | rewriteQueryFromSuggestion(position); |
| 1610 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | /** |
| 1614 | * Implements OnItemSelectedListener |
| 1615 | */ |
| 1616 | public void onNothingSelected(AdapterView<?> parent) { |
Karl Rosaen | 875d50a | 2009-04-23 19:00:21 -0700 | [diff] [blame] | 1617 | if (DBG) Log.d(LOG_TAG, "onNothingSelected()"); |
| 1618 | } |
| 1619 | |
| 1620 | /** |
| 1621 | * Query rewriting. |
| 1622 | */ |
| 1623 | |
| 1624 | private void rewriteQueryFromSuggestion(int position) { |
| 1625 | Cursor c = mSuggestionsAdapter.getCursor(); |
| 1626 | if (c == null) { |
| 1627 | return; |
| 1628 | } |
| 1629 | if (c.moveToPosition(position)) { |
| 1630 | // Get the new query from the suggestion. |
| 1631 | CharSequence newQuery = mSuggestionsAdapter.convertToString(c); |
| 1632 | if (newQuery != null) { |
| 1633 | // The suggestion rewrites the query. |
| 1634 | if (DBG) Log.d(LOG_TAG, "Rewriting query to '" + newQuery + "'"); |
| 1635 | // Update the text field, without getting new suggestions. |
| 1636 | setQuery(newQuery); |
| 1637 | } else { |
| 1638 | // The suggestion does not rewrite the query, restore the user's query. |
| 1639 | if (DBG) Log.d(LOG_TAG, "Suggestion gives no rewrite, restoring user query."); |
| 1640 | restoreUserQuery(); |
| 1641 | } |
| 1642 | } else { |
| 1643 | // We got a bad position, restore the user's query. |
| 1644 | Log.w(LOG_TAG, "Bad suggestion position: " + position); |
| 1645 | restoreUserQuery(); |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | /** |
| 1650 | * Restores the query entered by the user if needed. |
| 1651 | */ |
| 1652 | private void restoreUserQuery() { |
| 1653 | if (DBG) Log.d(LOG_TAG, "Restoring query to '" + mUserQuery + "'"); |
| 1654 | setQuery(mUserQuery); |
| 1655 | } |
| 1656 | |
| 1657 | /** |
| 1658 | * Sets the text in the query box, without updating the suggestions. |
| 1659 | */ |
| 1660 | private void setQuery(CharSequence query) { |
| 1661 | mSearchAutoComplete.setText(query, false); |
| 1662 | if (query != null) { |
| 1663 | mSearchAutoComplete.setSelection(query.length()); |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | /** |
| 1668 | * Sets the text in the query box, updating the suggestions. |
| 1669 | */ |
| 1670 | private void setUserQuery(String query) { |
| 1671 | if (query == null) { |
| 1672 | query = ""; |
| 1673 | } |
| 1674 | mUserQuery = query; |
| 1675 | mSearchAutoComplete.setText(query); |
| 1676 | mSearchAutoComplete.setSelection(query.length()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | /** |
| 1680 | * Debugging Support |
| 1681 | */ |
| 1682 | |
| 1683 | /** |
| 1684 | * For debugging only, sample the millisecond clock and log it. |
| 1685 | * Uses AtomicLong so we can use in multiple threads |
| 1686 | */ |
| 1687 | private AtomicLong mLastLogTime = new AtomicLong(SystemClock.uptimeMillis()); |
| 1688 | private void dbgLogTiming(final String caller) { |
| 1689 | long millis = SystemClock.uptimeMillis(); |
| 1690 | long oldTime = mLastLogTime.getAndSet(millis); |
| 1691 | long delta = millis - oldTime; |
| 1692 | final String report = millis + " (+" + delta + ") ticks for Search keystroke in " + caller; |
| 1693 | Log.d(LOG_TAG,report); |
| 1694 | } |
| 1695 | } |