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