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