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