1 /* 2 * Copyright (C) 2007 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 19 import android.Manifest; 20 import android.content.ActivityNotFoundException; 21 import android.content.ComponentName; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.DialogInterface; 25 import android.content.Intent; 26 import android.content.pm.ActivityInfo; 27 import android.content.pm.PackageManager; 28 import android.content.pm.ResolveInfo; 29 import android.database.Cursor; 30 import android.net.Uri; 31 import android.os.Bundle; 32 import android.os.Handler; 33 import android.os.RemoteException; 34 import android.os.ServiceManager; 35 import android.text.TextUtils; 36 import android.util.Log; 37 import android.view.KeyEvent; 38 39 import java.util.List; 40 41 /** 42 * This class provides access to the system search services. 43 * 44 * <p>In practice, you won't interact with this class directly, as search 45 * services are provided through methods in {@link android.app.Activity Activity} 46 * and the {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} 47 * {@link android.content.Intent Intent}. 48 * If you do require direct access to the SearchManager, do not instantiate 49 * this class directly. Instead, retrieve it through 50 * {@link android.content.Context#getSystemService 51 * context.getSystemService(Context.SEARCH_SERVICE)}. 52 * 53 * <div class="special"> 54 * <p>For a guide to using the search dialog and adding search 55 * suggestions in your application, see the Dev Guide topic about <strong><a 56 * href="{@docRoot}guide/topics/search/index.html">Search</a></strong>.</p> 57 * </div> 58 */ 59 public class SearchManager 60 implements DialogInterface.OnDismissListener, DialogInterface.OnCancelListener 61 { 62 63 private static final boolean DBG = false; 64 private static final String TAG = "SearchManager"; 65 66 /** 67 * This is a shortcut definition for the default menu key to use for invoking search. 68 * 69 * See Menu.Item.setAlphabeticShortcut() for more information. 70 */ 71 public final static char MENU_KEY = 's'; 72 73 /** 74 * This is a shortcut definition for the default menu key to use for invoking search. 75 * 76 * See Menu.Item.setAlphabeticShortcut() for more information. 77 */ 78 public final static int MENU_KEYCODE = KeyEvent.KEYCODE_S; 79 80 /** 81 * Intent extra data key: Use this key with 82 * {@link android.content.Intent#getStringExtra 83 * content.Intent.getStringExtra()} 84 * to obtain the query string from Intent.ACTION_SEARCH. 85 */ 86 public final static String QUERY = "query"; 87 88 /** 89 * Intent extra data key: Use this key with 90 * {@link android.content.Intent#getStringExtra 91 * content.Intent.getStringExtra()} 92 * to obtain the query string typed in by the user. 93 * This may be different from the value of {@link #QUERY} 94 * if the intent is the result of selecting a suggestion. 95 * In that case, {@link #QUERY} will contain the value of 96 * {@link #SUGGEST_COLUMN_QUERY} for the suggestion, and 97 * {@link #USER_QUERY} will contain the string typed by the 98 * user. 99 */ 100 public final static String USER_QUERY = "user_query"; 101 102 /** 103 * Intent extra data key: Use this key with Intent.ACTION_SEARCH and 104 * {@link android.content.Intent#getBundleExtra 105 * content.Intent.getBundleExtra()} 106 * to obtain any additional app-specific data that was inserted by the 107 * activity that launched the search. 108 */ 109 public final static String APP_DATA = "app_data"; 110 111 /** 112 * Intent extra data key: Use {@link android.content.Intent#getBundleExtra 113 * content.Intent.getBundleExtra(SEARCH_MODE)} to get the search mode used 114 * to launch the intent. 115 * The only current value for this is {@link #MODE_GLOBAL_SEARCH_SUGGESTION}. 116 * 117 * @hide 118 */ 119 public final static String SEARCH_MODE = "search_mode"; 120 121 /** 122 * Intent extra data key: Use this key with Intent.ACTION_SEARCH and 123 * {@link android.content.Intent#getIntExtra content.Intent.getIntExtra()} 124 * to obtain the keycode that the user used to trigger this query. It will be zero if the 125 * user simply pressed the "GO" button on the search UI. This is primarily used in conjunction 126 * with the keycode attribute in the actionkey element of your searchable.xml configuration 127 * file. 128 */ 129 public final static String ACTION_KEY = "action_key"; 130 131 /** 132 * Intent extra data key: This key will be used for the extra populated by the 133 * {@link #SUGGEST_COLUMN_INTENT_EXTRA_DATA} column. 134 */ 135 public final static String EXTRA_DATA_KEY = "intent_extra_data_key"; 136 137 /** 138 * Boolean extra data key for {@link #INTENT_ACTION_GLOBAL_SEARCH} intents. If {@code true}, 139 * the initial query should be selected when the global search activity is started, so 140 * that the user can easily replace it with another query. 141 */ 142 public final static String EXTRA_SELECT_QUERY = "select_query"; 143 144 /** 145 * Boolean extra data key for a suggestion provider to return in {@link Cursor#getExtras} to 146 * indicate that the search is not complete yet. This can be used by the search UI 147 * to indicate that a search is in progress. The suggestion provider can return partial results 148 * this way and send a change notification on the cursor when more results are available. 149 */ 150 public final static String CURSOR_EXTRA_KEY_IN_PROGRESS = "in_progress"; 151 152 /** 153 * Intent extra data key: Use this key with Intent.ACTION_SEARCH and 154 * {@link android.content.Intent#getStringExtra content.Intent.getStringExtra()} 155 * to obtain the action message that was defined for a particular search action key and/or 156 * suggestion. It will be null if the search was launched by typing "enter", touched the the 157 * "GO" button, or other means not involving any action key. 158 */ 159 public final static String ACTION_MSG = "action_msg"; 160 161 /** 162 * Uri path for queried suggestions data. This is the path that the search manager 163 * will use when querying your content provider for suggestions data based on user input 164 * (e.g. looking for partial matches). 165 * Typically you'll use this with a URI matcher. 166 */ 167 public final static String SUGGEST_URI_PATH_QUERY = "search_suggest_query"; 168 169 /** 170 * MIME type for suggestions data. You'll use this in your suggestions content provider 171 * in the getType() function. 172 */ 173 public final static String SUGGEST_MIME_TYPE = 174 "vnd.android.cursor.dir/vnd.android.search.suggest"; 175 176 /** 177 * Uri path for shortcut validation. This is the path that the search manager will use when 178 * querying your content provider to refresh a shortcutted suggestion result and to check if it 179 * is still valid. When asked, a source may return an up to date result, or no result. No 180 * result indicates the shortcut refers to a no longer valid sugggestion. 181 * 182 * @see #SUGGEST_COLUMN_SHORTCUT_ID 183 */ 184 public final static String SUGGEST_URI_PATH_SHORTCUT = "search_suggest_shortcut"; 185 186 /** 187 * MIME type for shortcut validation. You'll use this in your suggestions content provider 188 * in the getType() function. 189 */ 190 public final static String SHORTCUT_MIME_TYPE = 191 "vnd.android.cursor.item/vnd.android.search.suggest"; 192 193 /** 194 * Column name for suggestions cursor. <i>Unused - can be null or column can be omitted.</i> 195 */ 196 public final static String SUGGEST_COLUMN_FORMAT = "suggest_format"; 197 /** 198 * Column name for suggestions cursor. <i>Required.</i> This is the primary line of text that 199 * will be presented to the user as the suggestion. 200 */ 201 public final static String SUGGEST_COLUMN_TEXT_1 = "suggest_text_1"; 202 /** 203 * Column name for suggestions cursor. <i>Optional.</i> If your cursor includes this column, 204 * then all suggestions will be provided in a two-line format. The second line of text is in 205 * a much smaller appearance. 206 */ 207 public final static String SUGGEST_COLUMN_TEXT_2 = "suggest_text_2"; 208 209 /** 210 * Column name for suggestions cursor. <i>Optional.</i> This is a URL that will be shown 211 * as the second line of text instead of {@link #SUGGEST_COLUMN_TEXT_2}. This is a separate 212 * column so that the search UI knows to display the text as a URL, e.g. by using a different 213 * color. If this column is absent, or has the value {@code null}, 214 * {@link #SUGGEST_COLUMN_TEXT_2} will be used instead. 215 */ 216 public final static String SUGGEST_COLUMN_TEXT_2_URL = "suggest_text_2_url"; 217 218 /** 219 * Column name for suggestions cursor. <i>Optional.</i> If your cursor includes this column, 220 * then all suggestions will be provided in a format that includes space for two small icons, 221 * one at the left and one at the right of each suggestion. The data in the column must 222 * be a resource ID of a drawable, or a URI in one of the following formats: 223 * 224 * <ul> 225 * <li>content ({@link android.content.ContentResolver#SCHEME_CONTENT})</li> 226 * <li>android.resource ({@link android.content.ContentResolver#SCHEME_ANDROID_RESOURCE})</li> 227 * <li>file ({@link android.content.ContentResolver#SCHEME_FILE})</li> 228 * </ul> 229 * 230 * See {@link android.content.ContentResolver#openAssetFileDescriptor(Uri, String)} 231 * for more information on these schemes. 232 */ 233 public final static String SUGGEST_COLUMN_ICON_1 = "suggest_icon_1"; 234 /** 235 * Column name for suggestions cursor. <i>Optional.</i> If your cursor includes this column, 236 * then all suggestions will be provided in a format that includes space for two small icons, 237 * one at the left and one at the right of each suggestion. The data in the column must 238 * be a resource ID of a drawable, or a URI in one of the following formats: 239 * 240 * <ul> 241 * <li>content ({@link android.content.ContentResolver#SCHEME_CONTENT})</li> 242 * <li>android.resource ({@link android.content.ContentResolver#SCHEME_ANDROID_RESOURCE})</li> 243 * <li>file ({@link android.content.ContentResolver#SCHEME_FILE})</li> 244 * </ul> 245 * 246 * See {@link android.content.ContentResolver#openAssetFileDescriptor(Uri, String)} 247 * for more information on these schemes. 248 */ 249 public final static String SUGGEST_COLUMN_ICON_2 = "suggest_icon_2"; 250 /** 251 * Column name for suggestions cursor. <i>Optional.</i> If this column exists <i>and</i> 252 * this element exists at the given row, this is the action that will be used when 253 * forming the suggestion's intent. If the element is not provided, the action will be taken 254 * from the android:searchSuggestIntentAction field in your XML metadata. <i>At least one of 255 * these must be present for the suggestion to generate an intent.</i> Note: If your action is 256 * the same for all suggestions, it is more efficient to specify it using XML metadata and omit 257 * it from the cursor. 258 */ 259 public final static String SUGGEST_COLUMN_INTENT_ACTION = "suggest_intent_action"; 260 /** 261 * Column name for suggestions cursor. <i>Optional.</i> If this column exists <i>and</i> 262 * this element exists at the given row, this is the data that will be used when 263 * forming the suggestion's intent. If the element is not provided, the data will be taken 264 * from the android:searchSuggestIntentData field in your XML metadata. If neither source 265 * is provided, the Intent's data field will be null. Note: If your data is 266 * the same for all suggestions, or can be described using a constant part and a specific ID, 267 * it is more efficient to specify it using XML metadata and omit it from the cursor. 268 */ 269 public final static String SUGGEST_COLUMN_INTENT_DATA = "suggest_intent_data"; 270 /** 271 * Column name for suggestions cursor. <i>Optional.</i> If this column exists <i>and</i> 272 * this element exists at the given row, this is the data that will be used when 273 * forming the suggestion's intent. If not provided, the Intent's extra data field will be null. 274 * This column allows suggestions to provide additional arbitrary data which will be included as 275 * an extra under the key {@link #EXTRA_DATA_KEY}. 276 */ 277 public final static String SUGGEST_COLUMN_INTENT_EXTRA_DATA = "suggest_intent_extra_data"; 278 /** 279 * TODO: Remove 280 * 281 * @hide 282 */ 283 public final static String SUGGEST_COLUMN_INTENT_COMPONENT_NAME = "suggest_intent_component"; 284 /** 285 * Column name for suggestions cursor. <i>Optional.</i> If this column exists <i>and</i> 286 * this element exists at the given row, then "/" and this value will be appended to the data 287 * field in the Intent. This should only be used if the data field has already been set to an 288 * appropriate base string. 289 */ 290 public final static String SUGGEST_COLUMN_INTENT_DATA_ID = "suggest_intent_data_id"; 291 /** 292 * Column name for suggestions cursor. <i>Required if action is 293 * {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH}, optional otherwise.</i> If this 294 * column exists <i>and</i> this element exists at the given row, this is the data that will be 295 * used when forming the suggestion's query. 296 */ 297 public final static String SUGGEST_COLUMN_QUERY = "suggest_intent_query"; 298 299 /** 300 * Column name for suggestions cursor. <i>Optional.</i> This column is used to indicate whether 301 * a search suggestion should be stored as a shortcut, and whether it should be refreshed. If 302 * missing, the result will be stored as a shortcut and never validated. If set to 303 * {@link #SUGGEST_NEVER_MAKE_SHORTCUT}, the result will not be stored as a shortcut. 304 * Otherwise, the shortcut id will be used to check back for an up to date suggestion using 305 * {@link #SUGGEST_URI_PATH_SHORTCUT}. 306 */ 307 public final static String SUGGEST_COLUMN_SHORTCUT_ID = "suggest_shortcut_id"; 308 309 /** 310 * Column name for suggestions cursor. <i>Optional.</i> This column is used to specify the 311 * cursor item's background color if it needs a non-default background color. A non-zero value 312 * indicates a valid background color to override the default. 313 * 314 * @hide For internal use, not part of the public API. 315 */ 316 public final static String SUGGEST_COLUMN_BACKGROUND_COLOR = "suggest_background_color"; 317 318 /** 319 * Column name for suggestions cursor. <i>Optional.</i> This column is used to specify 320 * that a spinner should be shown in lieu of an icon2 while the shortcut of this suggestion 321 * is being refreshed. 322 */ 323 public final static String SUGGEST_COLUMN_SPINNER_WHILE_REFRESHING = 324 "suggest_spinner_while_refreshing"; 325 326 /** 327 * Column value for suggestion column {@link #SUGGEST_COLUMN_SHORTCUT_ID} when a suggestion 328 * should not be stored as a shortcut in global search. 329 */ 330 public final static String SUGGEST_NEVER_MAKE_SHORTCUT = "_-1"; 331 332 /** 333 * Query parameter added to suggestion queries to limit the number of suggestions returned. 334 * This limit is only advisory and suggestion providers may chose to ignore it. 335 */ 336 public final static String SUGGEST_PARAMETER_LIMIT = "limit"; 337 338 /** 339 * Intent action for starting the global search activity. 340 * The global search provider should handle this intent. 341 * 342 * Supported extra data keys: {@link #QUERY}, 343 * {@link #EXTRA_SELECT_QUERY}, 344 * {@link #APP_DATA}. 345 */ 346 public final static String INTENT_ACTION_GLOBAL_SEARCH 347 = "android.search.action.GLOBAL_SEARCH"; 348 349 /** 350 * Intent action for starting the global search settings activity. 351 * The global search provider should handle this intent. 352 */ 353 public final static String INTENT_ACTION_SEARCH_SETTINGS 354 = "android.search.action.SEARCH_SETTINGS"; 355 356 /** 357 * Intent action for starting a web search provider's settings activity. 358 * Web search providers should handle this intent if they have provider-specific 359 * settings to implement. 360 */ 361 public final static String INTENT_ACTION_WEB_SEARCH_SETTINGS 362 = "android.search.action.WEB_SEARCH_SETTINGS"; 363 364 /** 365 * Intent action broadcasted to inform that the searchables list or default have changed. 366 * Components should handle this intent if they cache any searchable data and wish to stay 367 * up to date on changes. 368 */ 369 public final static String INTENT_ACTION_SEARCHABLES_CHANGED 370 = "android.search.action.SEARCHABLES_CHANGED"; 371 372 /** 373 * Intent action broadcasted to inform that the search settings have changed in some way. 374 * Either searchables have been enabled or disabled, or a different web search provider 375 * has been chosen. 376 */ 377 public final static String INTENT_ACTION_SEARCH_SETTINGS_CHANGED 378 = "android.search.action.SETTINGS_CHANGED"; 379 380 /** 381 * If a suggestion has this value in {@link #SUGGEST_COLUMN_INTENT_ACTION}, 382 * the search dialog will take no action. 383 * 384 * @hide 385 */ 386 public final static String INTENT_ACTION_NONE = "android.search.action.ZILCH"; 387 388 /** 389 * This means that context is voice, and therefore the SearchDialog should 390 * continue showing the microphone until the user indicates that he/she does 391 * not want to re-speak (e.g. by typing). 392 * 393 * @hide 394 */ 395 public final static String CONTEXT_IS_VOICE = "android.search.CONTEXT_IS_VOICE"; 396 397 /** 398 * Reference to the shared system search service. 399 */ 400 private static ISearchManager mService; 401 402 private final Context mContext; 403 404 /** 405 * The package associated with this seach manager. 406 */ 407 private String mAssociatedPackage; 408 409 // package private since they are used by the inner class SearchManagerCallback 410 /* package */ final Handler mHandler; 411 /* package */ OnDismissListener mDismissListener = null; 412 /* package */ OnCancelListener mCancelListener = null; 413 414 private SearchDialog mSearchDialog; 415 416 /*package*/ SearchManager(Context context, Handler handler) { 417 mContext = context; 418 mHandler = handler; 419 mService = ISearchManager.Stub.asInterface( 420 ServiceManager.getService(Context.SEARCH_SERVICE)); 421 } 422 423 /** 424 * Launch search UI. 425 * 426 * <p>The search manager will open a search widget in an overlapping 427 * window, and the underlying activity may be obscured. The search 428 * entry state will remain in effect until one of the following events: 429 * <ul> 430 * <li>The user completes the search. In most cases this will launch 431 * a search intent.</li> 432 * <li>The user uses the back, home, or other keys to exit the search.</li> 433 * <li>The application calls the {@link #stopSearch} 434 * method, which will hide the search window and return focus to the 435 * activity from which it was launched.</li> 436 * 437 * <p>Most applications will <i>not</i> use this interface to invoke search. 438 * The primary method for invoking search is to call 439 * {@link android.app.Activity#onSearchRequested Activity.onSearchRequested()} or 440 * {@link android.app.Activity#startSearch Activity.startSearch()}. 441 * 442 * @param initialQuery A search string can be pre-entered here, but this 443 * is typically null or empty. 444 * @param selectInitialQuery If true, the intial query will be preselected, which means that 445 * any further typing will replace it. This is useful for cases where an entire pre-formed 446 * query is being inserted. If false, the selection point will be placed at the end of the 447 * inserted query. This is useful when the inserted query is text that the user entered, 448 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful 449 * if initialQuery is a non-empty string.</i> 450 * @param launchActivity The ComponentName of the activity that has launched this search. 451 * @param appSearchData An application can insert application-specific 452 * context here, in order to improve quality or specificity of its own 453 * searches. This data will be returned with SEARCH intent(s). Null if 454 * no extra data is required. 455 * @param globalSearch If false, this will only launch the search that has been specifically 456 * defined by the application (which is usually defined as a local search). If no default 457 * search is defined in the current application or activity, global search will be launched. 458 * If true, this will always launch a platform-global (e.g. web-based) search instead. 459 * 460 * @see android.app.Activity#onSearchRequested 461 * @see #stopSearch 462 */ 463 public void startSearch(String initialQuery, 464 boolean selectInitialQuery, 465 ComponentName launchActivity, 466 Bundle appSearchData, 467 boolean globalSearch) { 468 if (globalSearch) { 469 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData); 470 return; 471 } 472 473 ensureSearchDialog(); 474 475 mSearchDialog.show(initialQuery, selectInitialQuery, launchActivity, appSearchData); 476 } 477 478 private void ensureSearchDialog() { 479 if (mSearchDialog == null) { 480 mSearchDialog = new SearchDialog(mContext, this); 481 mSearchDialog.setOnCancelListener(this); 482 mSearchDialog.setOnDismissListener(this); 483 } 484 } 485 486 /** 487 * Starts the global search activity. 488 */ 489 /* package */ void startGlobalSearch(String initialQuery, boolean selectInitialQuery, 490 Bundle appSearchData) { 491 ComponentName globalSearchActivity = getGlobalSearchActivity(); 492 if (globalSearchActivity == null) { 493 Log.w(TAG, "No global search activity found."); 494 return; 495 } 496 Intent intent = new Intent(INTENT_ACTION_GLOBAL_SEARCH); 497 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 498 intent.setComponent(globalSearchActivity); 499 // Make sure that we have a Bundle to put source in 500 if (appSearchData == null) { 501 appSearchData = new Bundle(); 502 } else { 503 appSearchData = new Bundle(appSearchData); 504 } 505 // Set source to package name of app that starts global search, if not set already. 506 if (!appSearchData.containsKey("source")) { 507 appSearchData.putString("source", mContext.getPackageName()); 508 } 509 intent.putExtra(APP_DATA, appSearchData); 510 if (!TextUtils.isEmpty(initialQuery)) { 511 intent.putExtra(QUERY, initialQuery); 512 } 513 if (selectInitialQuery) { 514 intent.putExtra(EXTRA_SELECT_QUERY, selectInitialQuery); 515 } 516 try { 517 if (DBG) Log.d(TAG, "Starting global search: " + intent.toUri(0)); 518 mContext.startActivity(intent); 519 } catch (ActivityNotFoundException ex) { 520 Log.e(TAG, "Global search activity not found: " + globalSearchActivity); 521 } 522 } 523 524 /** 525 * Gets the name of the global search activity. 526 * 527 * @hide 528 */ 529 public ComponentName getGlobalSearchActivity() { 530 try { 531 return mService.getGlobalSearchActivity(); 532 } catch (RemoteException ex) { 533 Log.e(TAG, "getGlobalSearchActivity() failed: " + ex); 534 return null; 535 } 536 } 537 538 /** 539 * Gets the name of the web search activity. 540 * 541 * @return The name of the default activity for web searches. This activity 542 * can be used to get web search suggestions. Returns {@code null} if 543 * there is no default web search activity. 544 * 545 * @hide 546 */ 547 public ComponentName getWebSearchActivity() { 548 try { 549 return mService.getWebSearchActivity(); 550 } catch (RemoteException ex) { 551 Log.e(TAG, "getWebSearchActivity() failed: " + ex); 552 return null; 553 } 554 } 555 556 /** 557 * Similar to {@link #startSearch} but actually fires off the search query after invoking 558 * the search dialog. Made available for testing purposes. 559 * 560 * @param query The query to trigger. If empty, request will be ignored. 561 * @param launchActivity The ComponentName of the activity that has launched this search. 562 * @param appSearchData An application can insert application-specific 563 * context here, in order to improve quality or specificity of its own 564 * searches. This data will be returned with SEARCH intent(s). Null if 565 * no extra data is required. 566 * 567 * @see #startSearch 568 */ 569 public void triggerSearch(String query, 570 ComponentName launchActivity, 571 Bundle appSearchData) { 572 if (!mAssociatedPackage.equals(launchActivity.getPackageName())) { 573 throw new IllegalArgumentException("invoking app search on a different package " + 574 "not associated with this search manager"); 575 } 576 if (query == null || TextUtils.getTrimmedLength(query) == 0) { 577 Log.w(TAG, "triggerSearch called with empty query, ignoring."); 578 return; 579 } 580 startSearch(query, false, launchActivity, appSearchData, false); 581 mSearchDialog.launchQuerySearch(); 582 } 583 584 /** 585 * Terminate search UI. 586 * 587 * <p>Typically the user will terminate the search UI by launching a 588 * search or by canceling. This function allows the underlying application 589 * or activity to cancel the search prematurely (for any reason). 590 * 591 * <p>This function can be safely called at any time (even if no search is active.) 592 * 593 * @see #startSearch 594 */ 595 public void stopSearch() { 596 if (mSearchDialog != null) { 597 mSearchDialog.cancel(); 598 } 599 } 600 601 /** 602 * Determine if the Search UI is currently displayed. 603 * 604 * This is provided primarily for application test purposes. 605 * 606 * @return Returns true if the search UI is currently displayed. 607 * 608 * @hide 609 */ 610 public boolean isVisible() { 611 return mSearchDialog == null? false : mSearchDialog.isShowing(); 612 } 613 614 /** 615 * See {@link SearchManager#setOnDismissListener} for configuring your activity to monitor 616 * search UI state. 617 */ 618 public interface OnDismissListener { 619 /** 620 * This method will be called when the search UI is dismissed. To make use of it, you must 621 * implement this method in your activity, and call 622 * {@link SearchManager#setOnDismissListener} to register it. 623 */ 624 public void onDismiss(); 625 } 626 627 /** 628 * See {@link SearchManager#setOnCancelListener} for configuring your activity to monitor 629 * search UI state. 630 */ 631 public interface OnCancelListener { 632 /** 633 * This method will be called when the search UI is canceled. To make use if it, you must 634 * implement this method in your activity, and call 635 * {@link SearchManager#setOnCancelListener} to register it. 636 */ 637 public void onCancel(); 638 } 639 640 /** 641 * Set or clear the callback that will be invoked whenever the search UI is dismissed. 642 * 643 * @param listener The {@link OnDismissListener} to use, or null. 644 */ 645 public void setOnDismissListener(final OnDismissListener listener) { 646 mDismissListener = listener; 647 } 648 649 /** 650 * Set or clear the callback that will be invoked whenever the search UI is canceled. 651 * 652 * @param listener The {@link OnCancelListener} to use, or null. 653 */ 654 public void setOnCancelListener(OnCancelListener listener) { 655 mCancelListener = listener; 656 } 657 658 /** 659 * @deprecated This method is an obsolete internal implementation detail. Do not use. 660 */ 661 @Deprecated 662 public void onCancel(DialogInterface dialog) { 663 if (mCancelListener != null) { 664 mCancelListener.onCancel(); 665 } 666 } 667 668 /** 669 * @deprecated This method is an obsolete internal implementation detail. Do not use. 670 */ 671 @Deprecated 672 public void onDismiss(DialogInterface dialog) { 673 if (mDismissListener != null) { 674 mDismissListener.onDismiss(); 675 } 676 } 677 678 /** 679 * Gets information about a searchable activity. 680 * 681 * @param componentName The activity to get searchable information for. 682 * @return Searchable information, or <code>null</code> if the activity does not 683 * exist, or is not searchable. 684 */ 685 public SearchableInfo getSearchableInfo(ComponentName componentName) { 686 try { 687 return mService.getSearchableInfo(componentName); 688 } catch (RemoteException ex) { 689 Log.e(TAG, "getSearchableInfo() failed: " + ex); 690 return null; 691 } 692 } 693 694 /** 695 * Gets a cursor with search suggestions. 696 * 697 * @param searchable Information about how to get the suggestions. 698 * @param query The search text entered (so far). 699 * @return a cursor with suggestions, or <code>null</null> the suggestion query failed. 700 * 701 * @hide because SearchableInfo is not part of the API. 702 */ 703 public Cursor getSuggestions(SearchableInfo searchable, String query) { 704 return getSuggestions(searchable, query, -1); 705 } 706 707 /** 708 * Gets a cursor with search suggestions. 709 * 710 * @param searchable Information about how to get the suggestions. 711 * @param query The search text entered (so far). 712 * @param limit The query limit to pass to the suggestion provider. This is advisory, 713 * the returned cursor may contain more rows. Pass {@code -1} for no limit. 714 * @return a cursor with suggestions, or <code>null</null> the suggestion query failed. 715 * 716 * @hide because SearchableInfo is not part of the API. 717 */ 718 public Cursor getSuggestions(SearchableInfo searchable, String query, int limit) { 719 if (searchable == null) { 720 return null; 721 } 722 723 String authority = searchable.getSuggestAuthority(); 724 if (authority == null) { 725 return null; 726 } 727 728 Uri.Builder uriBuilder = new Uri.Builder() 729 .scheme(ContentResolver.SCHEME_CONTENT) 730 .authority(authority) 731 .query("") // TODO: Remove, workaround for a bug in Uri.writeToParcel() 732 .fragment(""); // TODO: Remove, workaround for a bug in Uri.writeToParcel() 733 734 // if content path provided, insert it now 735 final String contentPath = searchable.getSuggestPath(); 736 if (contentPath != null) { 737 uriBuilder.appendEncodedPath(contentPath); 738 } 739 740 // append standard suggestion query path 741 uriBuilder.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY); 742 743 // get the query selection, may be null 744 String selection = searchable.getSuggestSelection(); 745 // inject query, either as selection args or inline 746 String[] selArgs = null; 747 if (selection != null) { // use selection if provided 748 selArgs = new String[] { query }; 749 } else { // no selection, use REST pattern 750 uriBuilder.appendPath(query); 751 } 752 753 if (limit > 0) { 754 uriBuilder.appendQueryParameter(SUGGEST_PARAMETER_LIMIT, String.valueOf(limit)); 755 } 756 757 Uri uri = uriBuilder.build(); 758 759 // finally, make the query 760 return mContext.getContentResolver().query(uri, null, selection, selArgs, null); 761 } 762 763 /** 764 * Returns a list of the searchable activities that can be included in global search. 765 * 766 * @return a list containing searchable information for all searchable activities 767 * that have the <code>android:includeInGlobalSearch</code> attribute set 768 * in their searchable meta-data. 769 */ 770 public List<SearchableInfo> getSearchablesInGlobalSearch() { 771 try { 772 return mService.getSearchablesInGlobalSearch(); 773 } catch (RemoteException e) { 774 Log.e(TAG, "getSearchablesInGlobalSearch() failed: " + e); 775 return null; 776 } 777 } 778 779 } 780