SearchView.java revision 2427192e58b2ff31c3238b6c04440a15f4e7bf20
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.widget; 18 19import static android.widget.SuggestionsAdapter.getColumnString; 20 21import android.app.PendingIntent; 22import android.app.SearchManager; 23import android.app.SearchableInfo; 24import android.content.ActivityNotFoundException; 25import android.content.ComponentName; 26import android.content.Context; 27import android.content.Intent; 28import android.content.pm.PackageManager; 29import android.content.pm.ResolveInfo; 30import android.content.res.Configuration; 31import android.content.res.Resources; 32import android.content.res.TypedArray; 33import android.database.Cursor; 34import android.graphics.Rect; 35import android.graphics.drawable.Drawable; 36import android.net.Uri; 37import android.os.Bundle; 38import android.speech.RecognizerIntent; 39import android.text.Editable; 40import android.text.InputType; 41import android.text.Spannable; 42import android.text.SpannableStringBuilder; 43import android.text.TextUtils; 44import android.text.TextWatcher; 45import android.text.style.ImageSpan; 46import android.util.AttributeSet; 47import android.util.Log; 48import android.util.TypedValue; 49import android.view.CollapsibleActionView; 50import android.view.KeyEvent; 51import android.view.LayoutInflater; 52import android.view.View; 53import android.view.accessibility.AccessibilityEvent; 54import android.view.accessibility.AccessibilityNodeInfo; 55import android.view.inputmethod.EditorInfo; 56import android.view.inputmethod.InputMethodManager; 57import android.widget.AdapterView.OnItemClickListener; 58import android.widget.AdapterView.OnItemSelectedListener; 59import android.widget.TextView.OnEditorActionListener; 60 61import com.android.internal.R; 62 63import java.util.WeakHashMap; 64 65/** 66 * A widget that provides a user interface for the user to enter a search query and submit a request 67 * to a search provider. Shows a list of query suggestions or results, if available, and allows the 68 * user to pick a suggestion or result to launch into. 69 * 70 * <p> 71 * When the SearchView is used in an ActionBar as an action view for a collapsible menu item, it 72 * needs to be set to iconified by default using {@link #setIconifiedByDefault(boolean) 73 * setIconifiedByDefault(true)}. This is the default, so nothing needs to be done. 74 * </p> 75 * <p> 76 * If you want the search field to always be visible, then call setIconifiedByDefault(false). 77 * </p> 78 * 79 * <div class="special reference"> 80 * <h3>Developer Guides</h3> 81 * <p>For information about using {@code SearchView}, read the 82 * <a href="{@docRoot}guide/topics/search/index.html">Search</a> developer guide.</p> 83 * </div> 84 * 85 * @see android.view.MenuItem#SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW 86 * @attr ref android.R.styleable#SearchView_iconifiedByDefault 87 * @attr ref android.R.styleable#SearchView_imeOptions 88 * @attr ref android.R.styleable#SearchView_inputType 89 * @attr ref android.R.styleable#SearchView_maxWidth 90 * @attr ref android.R.styleable#SearchView_queryHint 91 */ 92public class SearchView extends LinearLayout implements CollapsibleActionView { 93 94 private static final boolean DBG = false; 95 private static final String LOG_TAG = "SearchView"; 96 97 /** 98 * Private constant for removing the microphone in the keyboard. 99 */ 100 private static final String IME_OPTION_NO_MICROPHONE = "nm"; 101 102 private OnQueryTextListener mOnQueryChangeListener; 103 private OnCloseListener mOnCloseListener; 104 private OnFocusChangeListener mOnQueryTextFocusChangeListener; 105 private OnSuggestionListener mOnSuggestionListener; 106 private OnClickListener mOnSearchClickListener; 107 108 private boolean mIconifiedByDefault; 109 private boolean mIconified; 110 private CursorAdapter mSuggestionsAdapter; 111 private View mSearchButton; 112 private View mSubmitButton; 113 private View mSearchPlate; 114 private View mSubmitArea; 115 private ImageView mCloseButton; 116 private View mSearchEditFrame; 117 private View mVoiceButton; 118 private SearchAutoComplete mQueryTextView; 119 private View mDropDownAnchor; 120 private ImageView mSearchHintIcon; 121 private boolean mSubmitButtonEnabled; 122 private CharSequence mQueryHint; 123 private boolean mQueryRefinement; 124 private boolean mClearingFocus; 125 private int mMaxWidth; 126 private boolean mVoiceButtonEnabled; 127 private CharSequence mOldQueryText; 128 private CharSequence mUserQuery; 129 private boolean mExpandedInActionView; 130 private int mCollapsedImeOptions; 131 132 private SearchableInfo mSearchable; 133 private Bundle mAppSearchData; 134 135 /* 136 * SearchView can be set expanded before the IME is ready to be shown during 137 * initial UI setup. The show operation is asynchronous to account for this. 138 */ 139 private Runnable mShowImeRunnable = new Runnable() { 140 public void run() { 141 InputMethodManager imm = (InputMethodManager) 142 getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 143 144 if (imm != null) { 145 imm.showSoftInputUnchecked(0, null); 146 } 147 } 148 }; 149 150 private Runnable mUpdateDrawableStateRunnable = new Runnable() { 151 public void run() { 152 updateFocusedState(); 153 } 154 }; 155 156 private Runnable mReleaseCursorRunnable = new Runnable() { 157 public void run() { 158 if (mSuggestionsAdapter != null && mSuggestionsAdapter instanceof SuggestionsAdapter) { 159 mSuggestionsAdapter.changeCursor(null); 160 } 161 } 162 }; 163 164 // For voice searching 165 private final Intent mVoiceWebSearchIntent; 166 private final Intent mVoiceAppSearchIntent; 167 168 // A weak map of drawables we've gotten from other packages, so we don't load them 169 // more than once. 170 private final WeakHashMap<String, Drawable.ConstantState> mOutsideDrawablesCache = 171 new WeakHashMap<String, Drawable.ConstantState>(); 172 173 /** 174 * Callbacks for changes to the query text. 175 */ 176 public interface OnQueryTextListener { 177 178 /** 179 * Called when the user submits the query. This could be due to a key press on the 180 * keyboard or due to pressing a submit button. 181 * The listener can override the standard behavior by returning true 182 * to indicate that it has handled the submit request. Otherwise return false to 183 * let the SearchView handle the submission by launching any associated intent. 184 * 185 * @param query the query text that is to be submitted 186 * 187 * @return true if the query has been handled by the listener, false to let the 188 * SearchView perform the default action. 189 */ 190 boolean onQueryTextSubmit(String query); 191 192 /** 193 * Called when the query text is changed by the user. 194 * 195 * @param newText the new content of the query text field. 196 * 197 * @return false if the SearchView should perform the default action of showing any 198 * suggestions if available, true if the action was handled by the listener. 199 */ 200 boolean onQueryTextChange(String newText); 201 } 202 203 public interface OnCloseListener { 204 205 /** 206 * The user is attempting to close the SearchView. 207 * 208 * @return true if the listener wants to override the default behavior of clearing the 209 * text field and dismissing it, false otherwise. 210 */ 211 boolean onClose(); 212 } 213 214 /** 215 * Callback interface for selection events on suggestions. These callbacks 216 * are only relevant when a SearchableInfo has been specified by {@link #setSearchableInfo}. 217 */ 218 public interface OnSuggestionListener { 219 220 /** 221 * Called when a suggestion was selected by navigating to it. 222 * @param position the absolute position in the list of suggestions. 223 * 224 * @return true if the listener handles the event and wants to override the default 225 * behavior of possibly rewriting the query based on the selected item, false otherwise. 226 */ 227 boolean onSuggestionSelect(int position); 228 229 /** 230 * Called when a suggestion was clicked. 231 * @param position the absolute position of the clicked item in the list of suggestions. 232 * 233 * @return true if the listener handles the event and wants to override the default 234 * behavior of launching any intent or submitting a search query specified on that item. 235 * Return false otherwise. 236 */ 237 boolean onSuggestionClick(int position); 238 } 239 240 public SearchView(Context context) { 241 this(context, null); 242 } 243 244 public SearchView(Context context, AttributeSet attrs) { 245 super(context, attrs); 246 247 LayoutInflater inflater = (LayoutInflater) context 248 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 249 inflater.inflate(R.layout.search_view, this, true); 250 251 mSearchButton = findViewById(R.id.search_button); 252 mQueryTextView = (SearchAutoComplete) findViewById(R.id.search_src_text); 253 mQueryTextView.setSearchView(this); 254 255 mSearchEditFrame = findViewById(R.id.search_edit_frame); 256 mSearchPlate = findViewById(R.id.search_plate); 257 mSubmitArea = findViewById(R.id.submit_area); 258 mSubmitButton = findViewById(R.id.search_go_btn); 259 mCloseButton = (ImageView) findViewById(R.id.search_close_btn); 260 mVoiceButton = findViewById(R.id.search_voice_btn); 261 mSearchHintIcon = (ImageView) findViewById(R.id.search_mag_icon); 262 263 mSearchButton.setOnClickListener(mOnClickListener); 264 mCloseButton.setOnClickListener(mOnClickListener); 265 mSubmitButton.setOnClickListener(mOnClickListener); 266 mVoiceButton.setOnClickListener(mOnClickListener); 267 mQueryTextView.setOnClickListener(mOnClickListener); 268 269 mQueryTextView.addTextChangedListener(mTextWatcher); 270 mQueryTextView.setOnEditorActionListener(mOnEditorActionListener); 271 mQueryTextView.setOnItemClickListener(mOnItemClickListener); 272 mQueryTextView.setOnItemSelectedListener(mOnItemSelectedListener); 273 mQueryTextView.setOnKeyListener(mTextKeyListener); 274 // Inform any listener of focus changes 275 mQueryTextView.setOnFocusChangeListener(new OnFocusChangeListener() { 276 277 public void onFocusChange(View v, boolean hasFocus) { 278 if (mOnQueryTextFocusChangeListener != null) { 279 mOnQueryTextFocusChangeListener.onFocusChange(SearchView.this, hasFocus); 280 } 281 } 282 }); 283 284 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SearchView, 0, 0); 285 setIconifiedByDefault(a.getBoolean(R.styleable.SearchView_iconifiedByDefault, true)); 286 int maxWidth = a.getDimensionPixelSize(R.styleable.SearchView_maxWidth, -1); 287 if (maxWidth != -1) { 288 setMaxWidth(maxWidth); 289 } 290 CharSequence queryHint = a.getText(R.styleable.SearchView_queryHint); 291 if (!TextUtils.isEmpty(queryHint)) { 292 setQueryHint(queryHint); 293 } 294 int imeOptions = a.getInt(R.styleable.SearchView_imeOptions, -1); 295 if (imeOptions != -1) { 296 setImeOptions(imeOptions); 297 } 298 int inputType = a.getInt(R.styleable.SearchView_inputType, -1); 299 if (inputType != -1) { 300 setInputType(inputType); 301 } 302 303 a.recycle(); 304 305 boolean focusable = true; 306 307 a = context.obtainStyledAttributes(attrs, R.styleable.View, 0, 0); 308 focusable = a.getBoolean(R.styleable.View_focusable, focusable); 309 a.recycle(); 310 setFocusable(focusable); 311 312 // Save voice intent for later queries/launching 313 mVoiceWebSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH); 314 mVoiceWebSearchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 315 mVoiceWebSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 316 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); 317 318 mVoiceAppSearchIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 319 mVoiceAppSearchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 320 321 mDropDownAnchor = findViewById(mQueryTextView.getDropDownAnchor()); 322 if (mDropDownAnchor != null) { 323 mDropDownAnchor.addOnLayoutChangeListener(new OnLayoutChangeListener() { 324 @Override 325 public void onLayoutChange(View v, int left, int top, int right, int bottom, 326 int oldLeft, int oldTop, int oldRight, int oldBottom) { 327 adjustDropDownSizeAndPosition(); 328 } 329 330 }); 331 } 332 333 updateViewsVisibility(mIconifiedByDefault); 334 updateQueryHint(); 335 } 336 337 /** 338 * Sets the SearchableInfo for this SearchView. Properties in the SearchableInfo are used 339 * to display labels, hints, suggestions, create intents for launching search results screens 340 * and controlling other affordances such as a voice button. 341 * 342 * @param searchable a SearchableInfo can be retrieved from the SearchManager, for a specific 343 * activity or a global search provider. 344 */ 345 public void setSearchableInfo(SearchableInfo searchable) { 346 mSearchable = searchable; 347 if (mSearchable != null) { 348 updateSearchAutoComplete(); 349 updateQueryHint(); 350 } 351 // Cache the voice search capability 352 mVoiceButtonEnabled = hasVoiceSearch(); 353 354 if (mVoiceButtonEnabled) { 355 // Disable the microphone on the keyboard, as a mic is displayed near the text box 356 // TODO: use imeOptions to disable voice input when the new API will be available 357 mQueryTextView.setPrivateImeOptions(IME_OPTION_NO_MICROPHONE); 358 } 359 updateViewsVisibility(isIconified()); 360 } 361 362 /** 363 * Sets the APP_DATA for legacy SearchDialog use. 364 * @param appSearchData bundle provided by the app when launching the search dialog 365 * @hide 366 */ 367 public void setAppSearchData(Bundle appSearchData) { 368 mAppSearchData = appSearchData; 369 } 370 371 /** 372 * Sets the IME options on the query text field. 373 * 374 * @see TextView#setImeOptions(int) 375 * @param imeOptions the options to set on the query text field 376 * 377 * @attr ref android.R.styleable#SearchView_imeOptions 378 */ 379 public void setImeOptions(int imeOptions) { 380 mQueryTextView.setImeOptions(imeOptions); 381 } 382 383 /** 384 * Returns the IME options set on the query text field. 385 * @return the ime options 386 * @see TextView#setImeOptions(int) 387 * 388 * @attr ref android.R.styleable#SearchView_imeOptions 389 */ 390 public int getImeOptions() { 391 return mQueryTextView.getImeOptions(); 392 } 393 394 /** 395 * Sets the input type on the query text field. 396 * 397 * @see TextView#setInputType(int) 398 * @param inputType the input type to set on the query text field 399 * 400 * @attr ref android.R.styleable#SearchView_inputType 401 */ 402 public void setInputType(int inputType) { 403 mQueryTextView.setInputType(inputType); 404 } 405 406 /** 407 * Returns the input type set on the query text field. 408 * @return the input type 409 * 410 * @attr ref android.R.styleable#SearchView_inputType 411 */ 412 public int getInputType() { 413 return mQueryTextView.getInputType(); 414 } 415 416 /** @hide */ 417 @Override 418 public boolean requestFocus(int direction, Rect previouslyFocusedRect) { 419 // Don't accept focus if in the middle of clearing focus 420 if (mClearingFocus) return false; 421 // Check if SearchView is focusable. 422 if (!isFocusable()) return false; 423 // If it is not iconified, then give the focus to the text field 424 if (!isIconified()) { 425 boolean result = mQueryTextView.requestFocus(direction, previouslyFocusedRect); 426 if (result) { 427 updateViewsVisibility(false); 428 } 429 return result; 430 } else { 431 return super.requestFocus(direction, previouslyFocusedRect); 432 } 433 } 434 435 /** @hide */ 436 @Override 437 public void clearFocus() { 438 mClearingFocus = true; 439 setImeVisibility(false); 440 super.clearFocus(); 441 mQueryTextView.clearFocus(); 442 mClearingFocus = false; 443 } 444 445 /** 446 * Sets a listener for user actions within the SearchView. 447 * 448 * @param listener the listener object that receives callbacks when the user performs 449 * actions in the SearchView such as clicking on buttons or typing a query. 450 */ 451 public void setOnQueryTextListener(OnQueryTextListener listener) { 452 mOnQueryChangeListener = listener; 453 } 454 455 /** 456 * Sets a listener to inform when the user closes the SearchView. 457 * 458 * @param listener the listener to call when the user closes the SearchView. 459 */ 460 public void setOnCloseListener(OnCloseListener listener) { 461 mOnCloseListener = listener; 462 } 463 464 /** 465 * Sets a listener to inform when the focus of the query text field changes. 466 * 467 * @param listener the listener to inform of focus changes. 468 */ 469 public void setOnQueryTextFocusChangeListener(OnFocusChangeListener listener) { 470 mOnQueryTextFocusChangeListener = listener; 471 } 472 473 /** 474 * Sets a listener to inform when a suggestion is focused or clicked. 475 * 476 * @param listener the listener to inform of suggestion selection events. 477 */ 478 public void setOnSuggestionListener(OnSuggestionListener listener) { 479 mOnSuggestionListener = listener; 480 } 481 482 /** 483 * Sets a listener to inform when the search button is pressed. This is only 484 * relevant when the text field is not visible by default. Calling {@link #setIconified 485 * setIconified(false)} can also cause this listener to be informed. 486 * 487 * @param listener the listener to inform when the search button is clicked or 488 * the text field is programmatically de-iconified. 489 */ 490 public void setOnSearchClickListener(OnClickListener listener) { 491 mOnSearchClickListener = listener; 492 } 493 494 /** 495 * Returns the query string currently in the text field. 496 * 497 * @return the query string 498 */ 499 public CharSequence getQuery() { 500 return mQueryTextView.getText(); 501 } 502 503 /** 504 * Sets a query string in the text field and optionally submits the query as well. 505 * 506 * @param query the query string. This replaces any query text already present in the 507 * text field. 508 * @param submit whether to submit the query right now or only update the contents of 509 * text field. 510 */ 511 public void setQuery(CharSequence query, boolean submit) { 512 mQueryTextView.setText(query); 513 if (query != null) { 514 mQueryTextView.setSelection(mQueryTextView.length()); 515 mUserQuery = query; 516 } 517 518 // If the query is not empty and submit is requested, submit the query 519 if (submit && !TextUtils.isEmpty(query)) { 520 onSubmitQuery(); 521 } 522 } 523 524 /** 525 * Sets the hint text to display in the query text field. This overrides any hint specified 526 * in the SearchableInfo. 527 * 528 * @param hint the hint text to display 529 * 530 * @attr ref android.R.styleable#SearchView_queryHint 531 */ 532 public void setQueryHint(CharSequence hint) { 533 mQueryHint = hint; 534 updateQueryHint(); 535 } 536 537 /** 538 * Gets the hint text to display in the query text field. 539 * @return the query hint text, if specified, null otherwise. 540 * 541 * @attr ref android.R.styleable#SearchView_queryHint 542 */ 543 public CharSequence getQueryHint() { 544 if (mQueryHint != null) { 545 return mQueryHint; 546 } else if (mSearchable != null) { 547 CharSequence hint = null; 548 int hintId = mSearchable.getHintId(); 549 if (hintId != 0) { 550 hint = getContext().getString(hintId); 551 } 552 return hint; 553 } 554 return null; 555 } 556 557 /** 558 * Sets the default or resting state of the search field. If true, a single search icon is 559 * shown by default and expands to show the text field and other buttons when pressed. Also, 560 * if the default state is iconified, then it collapses to that state when the close button 561 * is pressed. Changes to this property will take effect immediately. 562 * 563 * <p>The default value is true.</p> 564 * 565 * @param iconified whether the search field should be iconified by default 566 * 567 * @attr ref android.R.styleable#SearchView_iconifiedByDefault 568 */ 569 public void setIconifiedByDefault(boolean iconified) { 570 if (mIconifiedByDefault == iconified) return; 571 mIconifiedByDefault = iconified; 572 updateViewsVisibility(iconified); 573 updateQueryHint(); 574 } 575 576 /** 577 * Returns the default iconified state of the search field. 578 * @return 579 * 580 * @attr ref android.R.styleable#SearchView_iconifiedByDefault 581 */ 582 public boolean isIconfiedByDefault() { 583 return mIconifiedByDefault; 584 } 585 586 /** 587 * Iconifies or expands the SearchView. Any query text is cleared when iconified. This is 588 * a temporary state and does not override the default iconified state set by 589 * {@link #setIconifiedByDefault(boolean)}. If the default state is iconified, then 590 * a false here will only be valid until the user closes the field. And if the default 591 * state is expanded, then a true here will only clear the text field and not close it. 592 * 593 * @param iconify a true value will collapse the SearchView to an icon, while a false will 594 * expand it. 595 */ 596 public void setIconified(boolean iconify) { 597 if (iconify) { 598 onCloseClicked(); 599 } else { 600 onSearchClicked(); 601 } 602 } 603 604 /** 605 * Returns the current iconified state of the SearchView. 606 * 607 * @return true if the SearchView is currently iconified, false if the search field is 608 * fully visible. 609 */ 610 public boolean isIconified() { 611 return mIconified; 612 } 613 614 /** 615 * Enables showing a submit button when the query is non-empty. In cases where the SearchView 616 * is being used to filter the contents of the current activity and doesn't launch a separate 617 * results activity, then the submit button should be disabled. 618 * 619 * @param enabled true to show a submit button for submitting queries, false if a submit 620 * button is not required. 621 */ 622 public void setSubmitButtonEnabled(boolean enabled) { 623 mSubmitButtonEnabled = enabled; 624 updateViewsVisibility(isIconified()); 625 } 626 627 /** 628 * Returns whether the submit button is enabled when necessary or never displayed. 629 * 630 * @return whether the submit button is enabled automatically when necessary 631 */ 632 public boolean isSubmitButtonEnabled() { 633 return mSubmitButtonEnabled; 634 } 635 636 /** 637 * Specifies if a query refinement button should be displayed alongside each suggestion 638 * or if it should depend on the flags set in the individual items retrieved from the 639 * suggestions provider. Clicking on the query refinement button will replace the text 640 * in the query text field with the text from the suggestion. This flag only takes effect 641 * if a SearchableInfo has been specified with {@link #setSearchableInfo(SearchableInfo)} 642 * and not when using a custom adapter. 643 * 644 * @param enable true if all items should have a query refinement button, false if only 645 * those items that have a query refinement flag set should have the button. 646 * 647 * @see SearchManager#SUGGEST_COLUMN_FLAGS 648 * @see SearchManager#FLAG_QUERY_REFINEMENT 649 */ 650 public void setQueryRefinementEnabled(boolean enable) { 651 mQueryRefinement = enable; 652 if (mSuggestionsAdapter instanceof SuggestionsAdapter) { 653 ((SuggestionsAdapter) mSuggestionsAdapter).setQueryRefinement( 654 enable ? SuggestionsAdapter.REFINE_ALL : SuggestionsAdapter.REFINE_BY_ENTRY); 655 } 656 } 657 658 /** 659 * Returns whether query refinement is enabled for all items or only specific ones. 660 * @return true if enabled for all items, false otherwise. 661 */ 662 public boolean isQueryRefinementEnabled() { 663 return mQueryRefinement; 664 } 665 666 /** 667 * You can set a custom adapter if you wish. Otherwise the default adapter is used to 668 * display the suggestions from the suggestions provider associated with the SearchableInfo. 669 * 670 * @see #setSearchableInfo(SearchableInfo) 671 */ 672 public void setSuggestionsAdapter(CursorAdapter adapter) { 673 mSuggestionsAdapter = adapter; 674 675 mQueryTextView.setAdapter(mSuggestionsAdapter); 676 } 677 678 /** 679 * Returns the adapter used for suggestions, if any. 680 * @return the suggestions adapter 681 */ 682 public CursorAdapter getSuggestionsAdapter() { 683 return mSuggestionsAdapter; 684 } 685 686 /** 687 * Makes the view at most this many pixels wide 688 * 689 * @attr ref android.R.styleable#SearchView_maxWidth 690 */ 691 public void setMaxWidth(int maxpixels) { 692 mMaxWidth = maxpixels; 693 694 requestLayout(); 695 } 696 697 /** 698 * Gets the specified maximum width in pixels, if set. Returns zero if 699 * no maximum width was specified. 700 * @return the maximum width of the view 701 * 702 * @attr ref android.R.styleable#SearchView_maxWidth 703 */ 704 public int getMaxWidth() { 705 return mMaxWidth; 706 } 707 708 @Override 709 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 710 // Let the standard measurements take effect in iconified state. 711 if (isIconified()) { 712 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 713 return; 714 } 715 716 int widthMode = MeasureSpec.getMode(widthMeasureSpec); 717 int width = MeasureSpec.getSize(widthMeasureSpec); 718 719 switch (widthMode) { 720 case MeasureSpec.AT_MOST: 721 // If there is an upper limit, don't exceed maximum width (explicit or implicit) 722 if (mMaxWidth > 0) { 723 width = Math.min(mMaxWidth, width); 724 } else { 725 width = Math.min(getPreferredWidth(), width); 726 } 727 break; 728 case MeasureSpec.EXACTLY: 729 // If an exact width is specified, still don't exceed any specified maximum width 730 if (mMaxWidth > 0) { 731 width = Math.min(mMaxWidth, width); 732 } 733 break; 734 case MeasureSpec.UNSPECIFIED: 735 // Use maximum width, if specified, else preferred width 736 width = mMaxWidth > 0 ? mMaxWidth : getPreferredWidth(); 737 break; 738 } 739 widthMode = MeasureSpec.EXACTLY; 740 super.onMeasure(MeasureSpec.makeMeasureSpec(width, widthMode), heightMeasureSpec); 741 } 742 743 private int getPreferredWidth() { 744 return getContext().getResources() 745 .getDimensionPixelSize(R.dimen.search_view_preferred_width); 746 } 747 748 private void updateViewsVisibility(final boolean collapsed) { 749 mIconified = collapsed; 750 // Visibility of views that are visible when collapsed 751 final int visCollapsed = collapsed ? VISIBLE : GONE; 752 // Is there text in the query 753 final boolean hasText = !TextUtils.isEmpty(mQueryTextView.getText()); 754 755 mSearchButton.setVisibility(visCollapsed); 756 updateSubmitButton(hasText); 757 mSearchEditFrame.setVisibility(collapsed ? GONE : VISIBLE); 758 mSearchHintIcon.setVisibility(mIconifiedByDefault ? GONE : VISIBLE); 759 updateCloseButton(); 760 updateVoiceButton(!hasText); 761 updateSubmitArea(); 762 } 763 764 private boolean hasVoiceSearch() { 765 if (mSearchable != null && mSearchable.getVoiceSearchEnabled()) { 766 Intent testIntent = null; 767 if (mSearchable.getVoiceSearchLaunchWebSearch()) { 768 testIntent = mVoiceWebSearchIntent; 769 } else if (mSearchable.getVoiceSearchLaunchRecognizer()) { 770 testIntent = mVoiceAppSearchIntent; 771 } 772 if (testIntent != null) { 773 ResolveInfo ri = getContext().getPackageManager().resolveActivity(testIntent, 774 PackageManager.MATCH_DEFAULT_ONLY); 775 return ri != null; 776 } 777 } 778 return false; 779 } 780 781 private boolean isSubmitAreaEnabled() { 782 return (mSubmitButtonEnabled || mVoiceButtonEnabled) && !isIconified(); 783 } 784 785 private void updateSubmitButton(boolean hasText) { 786 int visibility = GONE; 787 if (mSubmitButtonEnabled && isSubmitAreaEnabled() && hasFocus() 788 && (hasText || !mVoiceButtonEnabled)) { 789 visibility = VISIBLE; 790 } 791 mSubmitButton.setVisibility(visibility); 792 } 793 794 private void updateSubmitArea() { 795 int visibility = GONE; 796 if (isSubmitAreaEnabled() 797 && (mSubmitButton.getVisibility() == VISIBLE 798 || mVoiceButton.getVisibility() == VISIBLE)) { 799 visibility = VISIBLE; 800 } 801 mSubmitArea.setVisibility(visibility); 802 } 803 804 private void updateCloseButton() { 805 final boolean hasText = !TextUtils.isEmpty(mQueryTextView.getText()); 806 // Should we show the close button? It is not shown if there's no focus, 807 // field is not iconified by default and there is no text in it. 808 final boolean showClose = hasText || (mIconifiedByDefault && !mExpandedInActionView); 809 mCloseButton.setVisibility(showClose ? VISIBLE : GONE); 810 mCloseButton.getDrawable().setState(hasText ? ENABLED_STATE_SET : EMPTY_STATE_SET); 811 } 812 813 private void postUpdateFocusedState() { 814 post(mUpdateDrawableStateRunnable); 815 } 816 817 private void updateFocusedState() { 818 boolean focused = mQueryTextView.hasFocus(); 819 mSearchPlate.getBackground().setState(focused ? FOCUSED_STATE_SET : EMPTY_STATE_SET); 820 mSubmitArea.getBackground().setState(focused ? FOCUSED_STATE_SET : EMPTY_STATE_SET); 821 invalidate(); 822 } 823 824 @Override 825 protected void onDetachedFromWindow() { 826 removeCallbacks(mUpdateDrawableStateRunnable); 827 post(mReleaseCursorRunnable); 828 super.onDetachedFromWindow(); 829 } 830 831 private void setImeVisibility(final boolean visible) { 832 if (visible) { 833 post(mShowImeRunnable); 834 } else { 835 removeCallbacks(mShowImeRunnable); 836 InputMethodManager imm = (InputMethodManager) 837 getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 838 839 if (imm != null) { 840 imm.hideSoftInputFromWindow(getWindowToken(), 0); 841 } 842 } 843 } 844 845 /** 846 * Called by the SuggestionsAdapter 847 * @hide 848 */ 849 /* package */void onQueryRefine(CharSequence queryText) { 850 setQuery(queryText); 851 } 852 853 private final OnClickListener mOnClickListener = new OnClickListener() { 854 855 public void onClick(View v) { 856 if (v == mSearchButton) { 857 onSearchClicked(); 858 } else if (v == mCloseButton) { 859 onCloseClicked(); 860 } else if (v == mSubmitButton) { 861 onSubmitQuery(); 862 } else if (v == mVoiceButton) { 863 onVoiceClicked(); 864 } else if (v == mQueryTextView) { 865 forceSuggestionQuery(); 866 } 867 } 868 }; 869 870 /** 871 * Handles the key down event for dealing with action keys. 872 * 873 * @param keyCode This is the keycode of the typed key, and is the same value as 874 * found in the KeyEvent parameter. 875 * @param event The complete event record for the typed key 876 * 877 * @return true if the event was handled here, or false if not. 878 */ 879 @Override 880 public boolean onKeyDown(int keyCode, KeyEvent event) { 881 if (mSearchable == null) { 882 return false; 883 } 884 885 // if it's an action specified by the searchable activity, launch the 886 // entered query with the action key 887 SearchableInfo.ActionKeyInfo actionKey = mSearchable.findActionKey(keyCode); 888 if ((actionKey != null) && (actionKey.getQueryActionMsg() != null)) { 889 launchQuerySearch(keyCode, actionKey.getQueryActionMsg(), mQueryTextView.getText() 890 .toString()); 891 return true; 892 } 893 894 return super.onKeyDown(keyCode, event); 895 } 896 897 /** 898 * React to the user typing "enter" or other hardwired keys while typing in 899 * the search box. This handles these special keys while the edit box has 900 * focus. 901 */ 902 View.OnKeyListener mTextKeyListener = new View.OnKeyListener() { 903 public boolean onKey(View v, int keyCode, KeyEvent event) { 904 // guard against possible race conditions 905 if (mSearchable == null) { 906 return false; 907 } 908 909 if (DBG) { 910 Log.d(LOG_TAG, "mTextListener.onKey(" + keyCode + "," + event + "), selection: " 911 + mQueryTextView.getListSelection()); 912 } 913 914 // If a suggestion is selected, handle enter, search key, and action keys 915 // as presses on the selected suggestion 916 if (mQueryTextView.isPopupShowing() 917 && mQueryTextView.getListSelection() != ListView.INVALID_POSITION) { 918 return onSuggestionsKey(v, keyCode, event); 919 } 920 921 // If there is text in the query box, handle enter, and action keys 922 // The search key is handled by the dialog's onKeyDown(). 923 if (!mQueryTextView.isEmpty() && event.hasNoModifiers()) { 924 if (event.getAction() == KeyEvent.ACTION_UP) { 925 if (keyCode == KeyEvent.KEYCODE_ENTER) { 926 v.cancelLongPress(); 927 928 // Launch as a regular search. 929 launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, mQueryTextView.getText() 930 .toString()); 931 return true; 932 } 933 } 934 if (event.getAction() == KeyEvent.ACTION_DOWN) { 935 SearchableInfo.ActionKeyInfo actionKey = mSearchable.findActionKey(keyCode); 936 if ((actionKey != null) && (actionKey.getQueryActionMsg() != null)) { 937 launchQuerySearch(keyCode, actionKey.getQueryActionMsg(), mQueryTextView 938 .getText().toString()); 939 return true; 940 } 941 } 942 } 943 return false; 944 } 945 }; 946 947 /** 948 * React to the user typing while in the suggestions list. First, check for 949 * action keys. If not handled, try refocusing regular characters into the 950 * EditText. 951 */ 952 private boolean onSuggestionsKey(View v, int keyCode, KeyEvent event) { 953 // guard against possible race conditions (late arrival after dismiss) 954 if (mSearchable == null) { 955 return false; 956 } 957 if (mSuggestionsAdapter == null) { 958 return false; 959 } 960 if (event.getAction() == KeyEvent.ACTION_DOWN && event.hasNoModifiers()) { 961 // First, check for enter or search (both of which we'll treat as a 962 // "click") 963 if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_SEARCH 964 || keyCode == KeyEvent.KEYCODE_TAB) { 965 int position = mQueryTextView.getListSelection(); 966 return onItemClicked(position, KeyEvent.KEYCODE_UNKNOWN, null); 967 } 968 969 // Next, check for left/right moves, which we use to "return" the 970 // user to the edit view 971 if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) { 972 // give "focus" to text editor, with cursor at the beginning if 973 // left key, at end if right key 974 // TODO: Reverse left/right for right-to-left languages, e.g. 975 // Arabic 976 int selPoint = (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) ? 0 : mQueryTextView 977 .length(); 978 mQueryTextView.setSelection(selPoint); 979 mQueryTextView.setListSelection(0); 980 mQueryTextView.clearListSelection(); 981 mQueryTextView.ensureImeVisible(true); 982 983 return true; 984 } 985 986 // Next, check for an "up and out" move 987 if (keyCode == KeyEvent.KEYCODE_DPAD_UP && 0 == mQueryTextView.getListSelection()) { 988 // TODO: restoreUserQuery(); 989 // let ACTV complete the move 990 return false; 991 } 992 993 // Next, check for an "action key" 994 SearchableInfo.ActionKeyInfo actionKey = mSearchable.findActionKey(keyCode); 995 if ((actionKey != null) 996 && ((actionKey.getSuggestActionMsg() != null) || (actionKey 997 .getSuggestActionMsgColumn() != null))) { 998 // launch suggestion using action key column 999 int position = mQueryTextView.getListSelection(); 1000 if (position != ListView.INVALID_POSITION) { 1001 Cursor c = mSuggestionsAdapter.getCursor(); 1002 if (c.moveToPosition(position)) { 1003 final String actionMsg = getActionKeyMessage(c, actionKey); 1004 if (actionMsg != null && (actionMsg.length() > 0)) { 1005 return onItemClicked(position, keyCode, actionMsg); 1006 } 1007 } 1008 } 1009 } 1010 } 1011 return false; 1012 } 1013 1014 /** 1015 * For a given suggestion and a given cursor row, get the action message. If 1016 * not provided by the specific row/column, also check for a single 1017 * definition (for the action key). 1018 * 1019 * @param c The cursor providing suggestions 1020 * @param actionKey The actionkey record being examined 1021 * 1022 * @return Returns a string, or null if no action key message for this 1023 * suggestion 1024 */ 1025 private static String getActionKeyMessage(Cursor c, SearchableInfo.ActionKeyInfo actionKey) { 1026 String result = null; 1027 // check first in the cursor data, for a suggestion-specific message 1028 final String column = actionKey.getSuggestActionMsgColumn(); 1029 if (column != null) { 1030 result = SuggestionsAdapter.getColumnString(c, column); 1031 } 1032 // If the cursor didn't give us a message, see if there's a single 1033 // message defined 1034 // for the actionkey (for all suggestions) 1035 if (result == null) { 1036 result = actionKey.getSuggestActionMsg(); 1037 } 1038 return result; 1039 } 1040 1041 private int getSearchIconId() { 1042 TypedValue outValue = new TypedValue(); 1043 getContext().getTheme().resolveAttribute(com.android.internal.R.attr.searchViewSearchIcon, 1044 outValue, true); 1045 return outValue.resourceId; 1046 } 1047 1048 private CharSequence getDecoratedHint(CharSequence hintText) { 1049 // If the field is always expanded, then don't add the search icon to the hint 1050 if (!mIconifiedByDefault) return hintText; 1051 1052 SpannableStringBuilder ssb = new SpannableStringBuilder(" "); // for the icon 1053 ssb.append(hintText); 1054 Drawable searchIcon = getContext().getResources().getDrawable(getSearchIconId()); 1055 int textSize = (int) (mQueryTextView.getTextSize() * 1.25); 1056 searchIcon.setBounds(0, 0, textSize, textSize); 1057 ssb.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 1058 return ssb; 1059 } 1060 1061 private void updateQueryHint() { 1062 if (mQueryHint != null) { 1063 mQueryTextView.setHint(getDecoratedHint(mQueryHint)); 1064 } else if (mSearchable != null) { 1065 CharSequence hint = null; 1066 int hintId = mSearchable.getHintId(); 1067 if (hintId != 0) { 1068 hint = getContext().getString(hintId); 1069 } 1070 if (hint != null) { 1071 mQueryTextView.setHint(getDecoratedHint(hint)); 1072 } 1073 } else { 1074 mQueryTextView.setHint(getDecoratedHint("")); 1075 } 1076 } 1077 1078 /** 1079 * Updates the auto-complete text view. 1080 */ 1081 private void updateSearchAutoComplete() { 1082 mQueryTextView.setDropDownAnimationStyle(0); // no animation 1083 mQueryTextView.setThreshold(mSearchable.getSuggestThreshold()); 1084 mQueryTextView.setImeOptions(mSearchable.getImeOptions()); 1085 int inputType = mSearchable.getInputType(); 1086 // We only touch this if the input type is set up for text (which it almost certainly 1087 // should be, in the case of search!) 1088 if ((inputType & InputType.TYPE_MASK_CLASS) == InputType.TYPE_CLASS_TEXT) { 1089 // The existence of a suggestions authority is the proxy for "suggestions 1090 // are available here" 1091 inputType &= ~InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE; 1092 if (mSearchable.getSuggestAuthority() != null) { 1093 inputType |= InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE; 1094 } 1095 } 1096 mQueryTextView.setInputType(inputType); 1097 if (mSuggestionsAdapter != null) { 1098 mSuggestionsAdapter.changeCursor(null); 1099 } 1100 // attach the suggestions adapter, if suggestions are available 1101 // The existence of a suggestions authority is the proxy for "suggestions available here" 1102 if (mSearchable.getSuggestAuthority() != null) { 1103 mSuggestionsAdapter = new SuggestionsAdapter(getContext(), 1104 this, mSearchable, mOutsideDrawablesCache); 1105 mQueryTextView.setAdapter(mSuggestionsAdapter); 1106 ((SuggestionsAdapter) mSuggestionsAdapter).setQueryRefinement( 1107 mQueryRefinement ? SuggestionsAdapter.REFINE_ALL 1108 : SuggestionsAdapter.REFINE_BY_ENTRY); 1109 } 1110 } 1111 1112 /** 1113 * Update the visibility of the voice button. There are actually two voice search modes, 1114 * either of which will activate the button. 1115 * @param empty whether the search query text field is empty. If it is, then the other 1116 * criteria apply to make the voice button visible. 1117 */ 1118 private void updateVoiceButton(boolean empty) { 1119 int visibility = GONE; 1120 if (mVoiceButtonEnabled && !isIconified() && empty) { 1121 visibility = VISIBLE; 1122 mSubmitButton.setVisibility(GONE); 1123 } 1124 mVoiceButton.setVisibility(visibility); 1125 } 1126 1127 private final OnEditorActionListener mOnEditorActionListener = new OnEditorActionListener() { 1128 1129 /** 1130 * Called when the input method default action key is pressed. 1131 */ 1132 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 1133 onSubmitQuery(); 1134 return true; 1135 } 1136 }; 1137 1138 private void onTextChanged(CharSequence newText) { 1139 CharSequence text = mQueryTextView.getText(); 1140 mUserQuery = text; 1141 boolean hasText = !TextUtils.isEmpty(text); 1142 updateSubmitButton(hasText); 1143 updateVoiceButton(!hasText); 1144 updateCloseButton(); 1145 updateSubmitArea(); 1146 if (mOnQueryChangeListener != null && !TextUtils.equals(newText, mOldQueryText)) { 1147 mOnQueryChangeListener.onQueryTextChange(newText.toString()); 1148 } 1149 mOldQueryText = newText.toString(); 1150 } 1151 1152 private void onSubmitQuery() { 1153 CharSequence query = mQueryTextView.getText(); 1154 if (query != null && TextUtils.getTrimmedLength(query) > 0) { 1155 if (mOnQueryChangeListener == null 1156 || !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) { 1157 if (mSearchable != null) { 1158 launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, query.toString()); 1159 setImeVisibility(false); 1160 } 1161 dismissSuggestions(); 1162 } 1163 } 1164 } 1165 1166 private void dismissSuggestions() { 1167 mQueryTextView.dismissDropDown(); 1168 } 1169 1170 private void onCloseClicked() { 1171 CharSequence text = mQueryTextView.getText(); 1172 if (TextUtils.isEmpty(text)) { 1173 if (mIconifiedByDefault) { 1174 // If the app doesn't override the close behavior 1175 if (mOnCloseListener == null || !mOnCloseListener.onClose()) { 1176 // hide the keyboard and remove focus 1177 clearFocus(); 1178 // collapse the search field 1179 updateViewsVisibility(true); 1180 } 1181 } 1182 } else { 1183 mQueryTextView.setText(""); 1184 mQueryTextView.requestFocus(); 1185 setImeVisibility(true); 1186 } 1187 1188 } 1189 1190 private void onSearchClicked() { 1191 updateViewsVisibility(false); 1192 mQueryTextView.requestFocus(); 1193 setImeVisibility(true); 1194 if (mOnSearchClickListener != null) { 1195 mOnSearchClickListener.onClick(this); 1196 } 1197 } 1198 1199 private void onVoiceClicked() { 1200 // guard against possible race conditions 1201 if (mSearchable == null) { 1202 return; 1203 } 1204 SearchableInfo searchable = mSearchable; 1205 try { 1206 if (searchable.getVoiceSearchLaunchWebSearch()) { 1207 Intent webSearchIntent = createVoiceWebSearchIntent(mVoiceWebSearchIntent, 1208 searchable); 1209 getContext().startActivity(webSearchIntent); 1210 } else if (searchable.getVoiceSearchLaunchRecognizer()) { 1211 Intent appSearchIntent = createVoiceAppSearchIntent(mVoiceAppSearchIntent, 1212 searchable); 1213 getContext().startActivity(appSearchIntent); 1214 } 1215 } catch (ActivityNotFoundException e) { 1216 // Should not happen, since we check the availability of 1217 // voice search before showing the button. But just in case... 1218 Log.w(LOG_TAG, "Could not find voice search activity"); 1219 } 1220 } 1221 1222 void onTextFocusChanged() { 1223 updateViewsVisibility(isIconified()); 1224 // Delayed update to make sure that the focus has settled down and window focus changes 1225 // don't affect it. A synchronous update was not working. 1226 postUpdateFocusedState(); 1227 if (mQueryTextView.hasFocus()) { 1228 forceSuggestionQuery(); 1229 } 1230 } 1231 1232 @Override 1233 public void onWindowFocusChanged(boolean hasWindowFocus) { 1234 super.onWindowFocusChanged(hasWindowFocus); 1235 1236 postUpdateFocusedState(); 1237 } 1238 1239 /** 1240 * {@inheritDoc} 1241 */ 1242 @Override 1243 public void onActionViewCollapsed() { 1244 clearFocus(); 1245 updateViewsVisibility(true); 1246 mQueryTextView.setImeOptions(mCollapsedImeOptions); 1247 mExpandedInActionView = false; 1248 } 1249 1250 /** 1251 * {@inheritDoc} 1252 */ 1253 @Override 1254 public void onActionViewExpanded() { 1255 if (mExpandedInActionView) return; 1256 1257 mExpandedInActionView = true; 1258 mCollapsedImeOptions = mQueryTextView.getImeOptions(); 1259 mQueryTextView.setImeOptions(mCollapsedImeOptions | EditorInfo.IME_FLAG_NO_FULLSCREEN); 1260 mQueryTextView.setText(""); 1261 setIconified(false); 1262 } 1263 1264 @Override 1265 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 1266 super.onInitializeAccessibilityEvent(event); 1267 event.setClassName(SearchView.class.getName()); 1268 } 1269 1270 @Override 1271 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 1272 super.onInitializeAccessibilityNodeInfo(info); 1273 info.setClassName(SearchView.class.getName()); 1274 } 1275 1276 private void adjustDropDownSizeAndPosition() { 1277 if (mDropDownAnchor.getWidth() > 1) { 1278 Resources res = getContext().getResources(); 1279 int anchorPadding = mSearchPlate.getPaddingLeft(); 1280 Rect dropDownPadding = new Rect(); 1281 int iconOffset = mIconifiedByDefault 1282 ? res.getDimensionPixelSize(R.dimen.dropdownitem_icon_width) 1283 + res.getDimensionPixelSize(R.dimen.dropdownitem_text_padding_left) 1284 : 0; 1285 mQueryTextView.getDropDownBackground().getPadding(dropDownPadding); 1286 mQueryTextView.setDropDownHorizontalOffset(-(dropDownPadding.left + iconOffset) 1287 + anchorPadding); 1288 mQueryTextView.setDropDownWidth(mDropDownAnchor.getWidth() + dropDownPadding.left 1289 + dropDownPadding.right + iconOffset - (anchorPadding)); 1290 } 1291 } 1292 1293 private boolean onItemClicked(int position, int actionKey, String actionMsg) { 1294 if (mOnSuggestionListener == null 1295 || !mOnSuggestionListener.onSuggestionClick(position)) { 1296 launchSuggestion(position, KeyEvent.KEYCODE_UNKNOWN, null); 1297 setImeVisibility(false); 1298 dismissSuggestions(); 1299 return true; 1300 } 1301 return false; 1302 } 1303 1304 private boolean onItemSelected(int position) { 1305 if (mOnSuggestionListener == null 1306 || !mOnSuggestionListener.onSuggestionSelect(position)) { 1307 rewriteQueryFromSuggestion(position); 1308 return true; 1309 } 1310 return false; 1311 } 1312 1313 private final OnItemClickListener mOnItemClickListener = new OnItemClickListener() { 1314 1315 /** 1316 * Implements OnItemClickListener 1317 */ 1318 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 1319 if (DBG) Log.d(LOG_TAG, "onItemClick() position " + position); 1320 onItemClicked(position, KeyEvent.KEYCODE_UNKNOWN, null); 1321 } 1322 }; 1323 1324 private final OnItemSelectedListener mOnItemSelectedListener = new OnItemSelectedListener() { 1325 1326 /** 1327 * Implements OnItemSelectedListener 1328 */ 1329 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 1330 if (DBG) Log.d(LOG_TAG, "onItemSelected() position " + position); 1331 SearchView.this.onItemSelected(position); 1332 } 1333 1334 /** 1335 * Implements OnItemSelectedListener 1336 */ 1337 public void onNothingSelected(AdapterView<?> parent) { 1338 if (DBG) 1339 Log.d(LOG_TAG, "onNothingSelected()"); 1340 } 1341 }; 1342 1343 /** 1344 * Query rewriting. 1345 */ 1346 private void rewriteQueryFromSuggestion(int position) { 1347 CharSequence oldQuery = mQueryTextView.getText(); 1348 Cursor c = mSuggestionsAdapter.getCursor(); 1349 if (c == null) { 1350 return; 1351 } 1352 if (c.moveToPosition(position)) { 1353 // Get the new query from the suggestion. 1354 CharSequence newQuery = mSuggestionsAdapter.convertToString(c); 1355 if (newQuery != null) { 1356 // The suggestion rewrites the query. 1357 // Update the text field, without getting new suggestions. 1358 setQuery(newQuery); 1359 } else { 1360 // The suggestion does not rewrite the query, restore the user's query. 1361 setQuery(oldQuery); 1362 } 1363 } else { 1364 // We got a bad position, restore the user's query. 1365 setQuery(oldQuery); 1366 } 1367 } 1368 1369 /** 1370 * Launches an intent based on a suggestion. 1371 * 1372 * @param position The index of the suggestion to create the intent from. 1373 * @param actionKey The key code of the action key that was pressed, 1374 * or {@link KeyEvent#KEYCODE_UNKNOWN} if none. 1375 * @param actionMsg The message for the action key that was pressed, 1376 * or <code>null</code> if none. 1377 * @return true if a successful launch, false if could not (e.g. bad position). 1378 */ 1379 private boolean launchSuggestion(int position, int actionKey, String actionMsg) { 1380 Cursor c = mSuggestionsAdapter.getCursor(); 1381 if ((c != null) && c.moveToPosition(position)) { 1382 1383 Intent intent = createIntentFromSuggestion(c, actionKey, actionMsg); 1384 1385 // launch the intent 1386 launchIntent(intent); 1387 1388 return true; 1389 } 1390 return false; 1391 } 1392 1393 /** 1394 * Launches an intent, including any special intent handling. 1395 */ 1396 private void launchIntent(Intent intent) { 1397 if (intent == null) { 1398 return; 1399 } 1400 try { 1401 // If the intent was created from a suggestion, it will always have an explicit 1402 // component here. 1403 getContext().startActivity(intent); 1404 } catch (RuntimeException ex) { 1405 Log.e(LOG_TAG, "Failed launch activity: " + intent, ex); 1406 } 1407 } 1408 1409 /** 1410 * Sets the text in the query box, without updating the suggestions. 1411 */ 1412 private void setQuery(CharSequence query) { 1413 mQueryTextView.setText(query, true); 1414 // Move the cursor to the end 1415 mQueryTextView.setSelection(TextUtils.isEmpty(query) ? 0 : query.length()); 1416 } 1417 1418 private void launchQuerySearch(int actionKey, String actionMsg, String query) { 1419 String action = Intent.ACTION_SEARCH; 1420 Intent intent = createIntent(action, null, null, query, actionKey, actionMsg); 1421 getContext().startActivity(intent); 1422 } 1423 1424 /** 1425 * Constructs an intent from the given information and the search dialog state. 1426 * 1427 * @param action Intent action. 1428 * @param data Intent data, or <code>null</code>. 1429 * @param extraData Data for {@link SearchManager#EXTRA_DATA_KEY} or <code>null</code>. 1430 * @param query Intent query, or <code>null</code>. 1431 * @param actionKey The key code of the action key that was pressed, 1432 * or {@link KeyEvent#KEYCODE_UNKNOWN} if none. 1433 * @param actionMsg The message for the action key that was pressed, 1434 * or <code>null</code> if none. 1435 * @param mode The search mode, one of the acceptable values for 1436 * {@link SearchManager#SEARCH_MODE}, or {@code null}. 1437 * @return The intent. 1438 */ 1439 private Intent createIntent(String action, Uri data, String extraData, String query, 1440 int actionKey, String actionMsg) { 1441 // Now build the Intent 1442 Intent intent = new Intent(action); 1443 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 1444 // We need CLEAR_TOP to avoid reusing an old task that has other activities 1445 // on top of the one we want. We don't want to do this in in-app search though, 1446 // as it can be destructive to the activity stack. 1447 if (data != null) { 1448 intent.setData(data); 1449 } 1450 intent.putExtra(SearchManager.USER_QUERY, mUserQuery); 1451 if (query != null) { 1452 intent.putExtra(SearchManager.QUERY, query); 1453 } 1454 if (extraData != null) { 1455 intent.putExtra(SearchManager.EXTRA_DATA_KEY, extraData); 1456 } 1457 if (mAppSearchData != null) { 1458 intent.putExtra(SearchManager.APP_DATA, mAppSearchData); 1459 } 1460 if (actionKey != KeyEvent.KEYCODE_UNKNOWN) { 1461 intent.putExtra(SearchManager.ACTION_KEY, actionKey); 1462 intent.putExtra(SearchManager.ACTION_MSG, actionMsg); 1463 } 1464 intent.setComponent(mSearchable.getSearchActivity()); 1465 return intent; 1466 } 1467 1468 /** 1469 * Create and return an Intent that can launch the voice search activity for web search. 1470 */ 1471 private Intent createVoiceWebSearchIntent(Intent baseIntent, SearchableInfo searchable) { 1472 Intent voiceIntent = new Intent(baseIntent); 1473 ComponentName searchActivity = searchable.getSearchActivity(); 1474 voiceIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, searchActivity == null ? null 1475 : searchActivity.flattenToShortString()); 1476 return voiceIntent; 1477 } 1478 1479 /** 1480 * Create and return an Intent that can launch the voice search activity, perform a specific 1481 * voice transcription, and forward the results to the searchable activity. 1482 * 1483 * @param baseIntent The voice app search intent to start from 1484 * @return A completely-configured intent ready to send to the voice search activity 1485 */ 1486 private Intent createVoiceAppSearchIntent(Intent baseIntent, SearchableInfo searchable) { 1487 ComponentName searchActivity = searchable.getSearchActivity(); 1488 1489 // create the necessary intent to set up a search-and-forward operation 1490 // in the voice search system. We have to keep the bundle separate, 1491 // because it becomes immutable once it enters the PendingIntent 1492 Intent queryIntent = new Intent(Intent.ACTION_SEARCH); 1493 queryIntent.setComponent(searchActivity); 1494 PendingIntent pending = PendingIntent.getActivity(getContext(), 0, queryIntent, 1495 PendingIntent.FLAG_ONE_SHOT); 1496 1497 // Now set up the bundle that will be inserted into the pending intent 1498 // when it's time to do the search. We always build it here (even if empty) 1499 // because the voice search activity will always need to insert "QUERY" into 1500 // it anyway. 1501 Bundle queryExtras = new Bundle(); 1502 1503 // Now build the intent to launch the voice search. Add all necessary 1504 // extras to launch the voice recognizer, and then all the necessary extras 1505 // to forward the results to the searchable activity 1506 Intent voiceIntent = new Intent(baseIntent); 1507 1508 // Add all of the configuration options supplied by the searchable's metadata 1509 String languageModel = RecognizerIntent.LANGUAGE_MODEL_FREE_FORM; 1510 String prompt = null; 1511 String language = null; 1512 int maxResults = 1; 1513 1514 Resources resources = getResources(); 1515 if (searchable.getVoiceLanguageModeId() != 0) { 1516 languageModel = resources.getString(searchable.getVoiceLanguageModeId()); 1517 } 1518 if (searchable.getVoicePromptTextId() != 0) { 1519 prompt = resources.getString(searchable.getVoicePromptTextId()); 1520 } 1521 if (searchable.getVoiceLanguageId() != 0) { 1522 language = resources.getString(searchable.getVoiceLanguageId()); 1523 } 1524 if (searchable.getVoiceMaxResults() != 0) { 1525 maxResults = searchable.getVoiceMaxResults(); 1526 } 1527 voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, languageModel); 1528 voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt); 1529 voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language); 1530 voiceIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxResults); 1531 voiceIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, searchActivity == null ? null 1532 : searchActivity.flattenToShortString()); 1533 1534 // Add the values that configure forwarding the results 1535 voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pending); 1536 voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT_BUNDLE, queryExtras); 1537 1538 return voiceIntent; 1539 } 1540 1541 /** 1542 * When a particular suggestion has been selected, perform the various lookups required 1543 * to use the suggestion. This includes checking the cursor for suggestion-specific data, 1544 * and/or falling back to the XML for defaults; It also creates REST style Uri data when 1545 * the suggestion includes a data id. 1546 * 1547 * @param c The suggestions cursor, moved to the row of the user's selection 1548 * @param actionKey The key code of the action key that was pressed, 1549 * or {@link KeyEvent#KEYCODE_UNKNOWN} if none. 1550 * @param actionMsg The message for the action key that was pressed, 1551 * or <code>null</code> if none. 1552 * @return An intent for the suggestion at the cursor's position. 1553 */ 1554 private Intent createIntentFromSuggestion(Cursor c, int actionKey, String actionMsg) { 1555 try { 1556 // use specific action if supplied, or default action if supplied, or fixed default 1557 String action = getColumnString(c, SearchManager.SUGGEST_COLUMN_INTENT_ACTION); 1558 1559 if (action == null) { 1560 action = mSearchable.getSuggestIntentAction(); 1561 } 1562 if (action == null) { 1563 action = Intent.ACTION_SEARCH; 1564 } 1565 1566 // use specific data if supplied, or default data if supplied 1567 String data = getColumnString(c, SearchManager.SUGGEST_COLUMN_INTENT_DATA); 1568 if (data == null) { 1569 data = mSearchable.getSuggestIntentData(); 1570 } 1571 // then, if an ID was provided, append it. 1572 if (data != null) { 1573 String id = getColumnString(c, SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID); 1574 if (id != null) { 1575 data = data + "/" + Uri.encode(id); 1576 } 1577 } 1578 Uri dataUri = (data == null) ? null : Uri.parse(data); 1579 1580 String query = getColumnString(c, SearchManager.SUGGEST_COLUMN_QUERY); 1581 String extraData = getColumnString(c, SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA); 1582 1583 return createIntent(action, dataUri, extraData, query, actionKey, actionMsg); 1584 } catch (RuntimeException e ) { 1585 int rowNum; 1586 try { // be really paranoid now 1587 rowNum = c.getPosition(); 1588 } catch (RuntimeException e2 ) { 1589 rowNum = -1; 1590 } 1591 Log.w(LOG_TAG, "Search Suggestions cursor at row " + rowNum + 1592 " returned exception" + e.toString()); 1593 return null; 1594 } 1595 } 1596 1597 private void forceSuggestionQuery() { 1598 mQueryTextView.doBeforeTextChanged(); 1599 mQueryTextView.doAfterTextChanged(); 1600 } 1601 1602 static boolean isLandscapeMode(Context context) { 1603 return context.getResources().getConfiguration().orientation 1604 == Configuration.ORIENTATION_LANDSCAPE; 1605 } 1606 1607 /** 1608 * Callback to watch the text field for empty/non-empty 1609 */ 1610 private TextWatcher mTextWatcher = new TextWatcher() { 1611 1612 public void beforeTextChanged(CharSequence s, int start, int before, int after) { } 1613 1614 public void onTextChanged(CharSequence s, int start, 1615 int before, int after) { 1616 SearchView.this.onTextChanged(s); 1617 } 1618 1619 public void afterTextChanged(Editable s) { 1620 } 1621 }; 1622 1623 /** 1624 * Local subclass for AutoCompleteTextView. 1625 * @hide 1626 */ 1627 public static class SearchAutoComplete extends AutoCompleteTextView { 1628 1629 private int mThreshold; 1630 private SearchView mSearchView; 1631 1632 public SearchAutoComplete(Context context) { 1633 super(context); 1634 mThreshold = getThreshold(); 1635 } 1636 1637 public SearchAutoComplete(Context context, AttributeSet attrs) { 1638 super(context, attrs); 1639 mThreshold = getThreshold(); 1640 } 1641 1642 public SearchAutoComplete(Context context, AttributeSet attrs, int defStyle) { 1643 super(context, attrs, defStyle); 1644 mThreshold = getThreshold(); 1645 } 1646 1647 void setSearchView(SearchView searchView) { 1648 mSearchView = searchView; 1649 } 1650 1651 @Override 1652 public void setThreshold(int threshold) { 1653 super.setThreshold(threshold); 1654 mThreshold = threshold; 1655 } 1656 1657 /** 1658 * Returns true if the text field is empty, or contains only whitespace. 1659 */ 1660 private boolean isEmpty() { 1661 return TextUtils.getTrimmedLength(getText()) == 0; 1662 } 1663 1664 /** 1665 * We override this method to avoid replacing the query box text when a 1666 * suggestion is clicked. 1667 */ 1668 @Override 1669 protected void replaceText(CharSequence text) { 1670 } 1671 1672 /** 1673 * We override this method to avoid an extra onItemClick being called on 1674 * the drop-down's OnItemClickListener by 1675 * {@link AutoCompleteTextView#onKeyUp(int, KeyEvent)} when an item is 1676 * clicked with the trackball. 1677 */ 1678 @Override 1679 public void performCompletion() { 1680 } 1681 1682 /** 1683 * We override this method to be sure and show the soft keyboard if 1684 * appropriate when the TextView has focus. 1685 */ 1686 @Override 1687 public void onWindowFocusChanged(boolean hasWindowFocus) { 1688 super.onWindowFocusChanged(hasWindowFocus); 1689 1690 if (hasWindowFocus && mSearchView.hasFocus() && getVisibility() == VISIBLE) { 1691 InputMethodManager inputManager = (InputMethodManager) getContext() 1692 .getSystemService(Context.INPUT_METHOD_SERVICE); 1693 inputManager.showSoftInput(this, 0); 1694 // If in landscape mode, then make sure that 1695 // the ime is in front of the dropdown. 1696 if (isLandscapeMode(getContext())) { 1697 ensureImeVisible(true); 1698 } 1699 } 1700 } 1701 1702 @Override 1703 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { 1704 super.onFocusChanged(focused, direction, previouslyFocusedRect); 1705 mSearchView.onTextFocusChanged(); 1706 } 1707 1708 /** 1709 * We override this method so that we can allow a threshold of zero, 1710 * which ACTV does not. 1711 */ 1712 @Override 1713 public boolean enoughToFilter() { 1714 return mThreshold <= 0 || super.enoughToFilter(); 1715 } 1716 1717 @Override 1718 public boolean onKeyPreIme(int keyCode, KeyEvent event) { 1719 if (keyCode == KeyEvent.KEYCODE_BACK) { 1720 // special case for the back key, we do not even try to send it 1721 // to the drop down list but instead, consume it immediately 1722 if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) { 1723 KeyEvent.DispatcherState state = getKeyDispatcherState(); 1724 if (state != null) { 1725 state.startTracking(event, this); 1726 } 1727 return true; 1728 } else if (event.getAction() == KeyEvent.ACTION_UP) { 1729 KeyEvent.DispatcherState state = getKeyDispatcherState(); 1730 if (state != null) { 1731 state.handleUpEvent(event); 1732 } 1733 if (event.isTracking() && !event.isCanceled()) { 1734 mSearchView.clearFocus(); 1735 mSearchView.setImeVisibility(false); 1736 return true; 1737 } 1738 } 1739 } 1740 return super.onKeyPreIme(keyCode, event); 1741 } 1742 1743 } 1744} 1745