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