SearchManager.java revision 875d50a4b9294b2be33cff6493cae7acd1d07ea7
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.ComponentName; 20import android.content.ContentResolver; 21import android.content.Context; 22import android.content.DialogInterface; 23import android.content.res.Configuration; 24import android.database.Cursor; 25import android.net.Uri; 26import android.os.Bundle; 27import android.os.Handler; 28import android.os.RemoteException; 29import android.os.ServiceManager; 30import android.server.search.SearchableInfo; 31import android.view.KeyEvent; 32 33/** 34 * This class provides access to the system search services. 35 * 36 * <p>In practice, you won't interact with this class directly, as search 37 * services are provided through methods in {@link android.app.Activity Activity} 38 * methods and the the {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} 39 * {@link android.content.Intent Intent}. This class does provide a basic 40 * overview of search services and how to integrate them with your activities. 41 * If you do require direct access to the Search Manager, do not instantiate 42 * this class directly; instead, retrieve it through 43 * {@link android.content.Context#getSystemService 44 * context.getSystemService(Context.SEARCH_SERVICE)}. 45 * 46 * <p>Topics covered here: 47 * <ol> 48 * <li><a href="#DeveloperGuide">Developer Guide</a> 49 * <li><a href="#HowSearchIsInvoked">How Search Is Invoked</a> 50 * <li><a href="#QuerySearchApplications">Query-Search Applications</a> 51 * <li><a href="#FilterSearchApplications">Filter-Search Applications</a> 52 * <li><a href="#Suggestions">Search Suggestions</a> 53 * <li><a href="#ActionKeys">Action Keys</a> 54 * <li><a href="#SearchabilityMetadata">Searchability Metadata</a> 55 * <li><a href="#PassingSearchContext">Passing Search Context</a> 56 * <li><a href="#ProtectingUserPrivacy">Protecting User Privacy</a> 57 * </ol> 58 * 59 * <a name="DeveloperGuide"></a> 60 * <h3>Developer Guide</h3> 61 * 62 * <p>The ability to search for user, system, or network based data is considered to be 63 * a core user-level feature of the android platform. At any time, the user should be 64 * able to use a familiar command, button, or keystroke to invoke search, and the user 65 * should be able to search any data which is available to them. The goal is to make search 66 * appear to the user as a seamless, system-wide feature. 67 * 68 * <p>In terms of implementation, there are three broad classes of Applications: 69 * <ol> 70 * <li>Applications that are not inherently searchable</li> 71 * <li>Query-Search Applications</li> 72 * <li>Filter-Search Applications</li> 73 * </ol> 74 * <p>These categories, as well as related topics, are discussed in 75 * the sections below. 76 * 77 * <p>Even if your application is not <i>searchable</i>, it can still support the invocation of 78 * search. Please review the section <a href="#HowSearchIsInvoked">How Search Is Invoked</a> 79 * for more information on how to support this. 80 * 81 * <p>Many applications are <i>searchable</i>. These are 82 * the applications which can convert a query string into a list of results. 83 * Within this subset, applications can be grouped loosely into two families: 84 * <ul><li><i>Query Search</i> applications perform batch-mode searches - each query string is 85 * converted to a list of results.</li> 86 * <li><i>Filter Search</i> applications provide live filter-as-you-type searches.</li></ul> 87 * <p>Generally speaking, you would use query search for network-based data, and filter 88 * search for local data, but this is not a hard requirement and applications 89 * are free to use the model that fits them best (or invent a new model). 90 * <p>It should be clear that the search implementation decouples "search 91 * invocation" from "searchable". This satisfies the goal of making search appear 92 * to be "universal". The user should be able to launch any search from 93 * almost any context. 94 * 95 * <a name="HowSearchIsInvoked"></a> 96 * <h3>How Search Is Invoked</h3> 97 * 98 * <p>Unless impossible or inapplicable, all applications should support 99 * invoking the search UI. This means that when the user invokes the search command, 100 * a search UI will be presented to them. The search command is currently defined as a menu 101 * item called "Search" (with an alphabetic shortcut key of "S"), or on some devices, a dedicated 102 * search button key. 103 * <p>If your application is not inherently searchable, you can also allow the search UI 104 * to be invoked in a "web search" mode. If the user enters a search term and clicks the 105 * "Search" button, this will bring the browser to the front and will launch a web-based 106 * search. The user will be able to click the "Back" button and return to your application. 107 * <p>In general this is implemented by your activity, or the {@link android.app.Activity Activity} 108 * base class, which captures the search command and invokes the Search Manager to 109 * display and operate the search UI. You can also cause the search UI to be presented in response 110 * to user keystrokes in your activity (for example, to instantly start filter searching while 111 * viewing a list and typing any key). 112 * <p>The search UI is presented as a floating 113 * window and does not cause any change in the activity stack. If the user 114 * cancels search, the previous activity re-emerges. If the user launches a 115 * search, this will be done by sending a search {@link android.content.Intent Intent} (see below), 116 * and the normal intent-handling sequence will take place (your activity will pause, 117 * etc.) 118 * <p><b>What you need to do:</b> First, you should consider the way in which you want to 119 * handle invoking search. There are four broad (and partially overlapping) categories for 120 * you to choose from. 121 * <ul><li>You can capture the search command yourself, by including a <i>search</i> 122 * button or menu item - and invoking the search UI directly.</li> 123 * <li>You can provide a <i>type-to-search</i> feature, in which search is invoked automatically 124 * when the user enters any characters.</li> 125 * <li>Even if your application is not inherently searchable, you can allow web search, 126 * via the search key (or even via a search menu item). 127 * <li>You can disable search entirely. This should only be used in very rare circumstances, 128 * as search is a system-wide feature and users will expect it to be available in all contexts.</li> 129 * </ul> 130 * 131 * <p><b>How to define a search menu.</b> The system provides the following resources which may 132 * be useful when adding a search item to your menu: 133 * <ul><li>android.R.drawable.ic_search_category_default is an icon you can use in your menu.</li> 134 * <li>{@link #MENU_KEY SearchManager.MENU_KEY} is the recommended alphabetic shortcut.</li> 135 * </ul> 136 * 137 * <p><b>How to invoke search directly.</b> In order to invoke search directly, from a button 138 * or menu item, you can launch a generic search by calling 139 * {@link android.app.Activity#onSearchRequested onSearchRequested} as shown: 140 * <pre class="prettyprint"> 141 * onSearchRequested();</pre> 142 * 143 * <p><b>How to implement type-to-search.</b> While setting up your activity, call 144 * {@link android.app.Activity#setDefaultKeyMode setDefaultKeyMode}: 145 * <pre class="prettyprint"> 146 * setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); // search within your activity 147 * setDefaultKeyMode(DEFAULT_KEYS_SEARCH_GLOBAL); // search using platform global search</pre> 148 * 149 * <p><b>How to enable web-based search.</b> In addition to searching within your activity or 150 * application, you can also use the Search Manager to invoke a platform-global search, typically 151 * a web search. There are two ways to do this: 152 * <ul><li>You can simply define "search" within your application or activity to mean global search. 153 * This is described in more detail in the 154 * <a href="#SearchabilityMetadata">Searchability Metadata</a> section. Briefly, you will 155 * add a single meta-data entry to your manifest, declaring that the default search 156 * for your application is "*". This indicates to the system that no application-specific 157 * search activity is provided, and that it should launch web-based search instead.</li> 158 * <li>You can specify this at invocation time via default keys (see above), overriding 159 * {@link android.app.Activity#onSearchRequested}, or via a direct call to 160 * {@link android.app.Activity#startSearch}. This is most useful if you wish to provide local 161 * searchability <i>and</i> access to global search.</li></ul> 162 * 163 * <p><b>How to disable search from your activity.</b> search is a system-wide feature and users 164 * will expect it to be available in all contexts. If your UI design absolutely precludes 165 * launching search, override {@link android.app.Activity#onSearchRequested onSearchRequested} 166 * as shown: 167 * <pre class="prettyprint"> 168 * @Override 169 * public boolean onSearchRequested() { 170 * return false; 171 * }</pre> 172 * 173 * <p><b>Managing focus and knowing if Search is active.</b> The search UI is not a separate 174 * activity, and when the UI is invoked or dismissed, your activity will not typically be paused, 175 * resumed, or otherwise notified by the methods defined in 176 * <a href="{@docRoot}guide/topics/fundamentals.html#actlife">Application Fundamentals: 177 * Activity Lifecycle</a>. The search UI is 178 * handled in the same way as other system UI elements which may appear from time to time, such as 179 * notifications, screen locks, or other system alerts: 180 * <p>When the search UI appears, your activity will lose input focus. 181 * <p>When the search activity is dismissed, there are three possible outcomes: 182 * <ul><li>If the user simply canceled the search UI, your activity will regain input focus and 183 * proceed as before. See {@link #setOnDismissListener} and {@link #setOnCancelListener} if you 184 * required direct notification of search dialog dismissals.</li> 185 * <li>If the user launched a search, and this required switching to another activity to receive 186 * and process the search {@link android.content.Intent Intent}, your activity will receive the 187 * normal sequence of activity pause or stop notifications.</li> 188 * <li>If the user launched a search, and the current activity is the recipient of the search 189 * {@link android.content.Intent Intent}, you will receive notification via the 190 * {@link android.app.Activity#onNewIntent onNewIntent()} method.</li></ul> 191 * <p>This list is provided in order to clarify the ways in which your activities will interact with 192 * the search UI. More details on searchable activities and search intents are provided in the 193 * sections below. 194 * 195 * <a name="QuerySearchApplications"></a> 196 * <h3>Query-Search Applications</h3> 197 * 198 * <p>Query-search applications are those that take a single query (e.g. a search 199 * string) and present a set of results that may fit. Primary examples include 200 * web queries, map lookups, or email searches (with the common thread being 201 * network query dispatch). It may also be the case that certain local searches 202 * are treated this way. It's up to the application to decide. 203 * 204 * <p><b>What you need to do:</b> The following steps are necessary in order to 205 * implement query search. 206 * <ul> 207 * <li>Implement search invocation as described above. (Strictly speaking, 208 * these are decoupled, but it would make little sense to be "searchable" but not 209 * "search-invoking".)</li> 210 * <li>Your application should have an activity that takes a search string and 211 * converts it to a list of results. This could be your primary display activity 212 * or it could be a dedicated search results activity. This is your <i>searchable</i> 213 * activity and every query-search application must have one.</li> 214 * <li>In the searchable activity, in onCreate(), you must receive and handle the 215 * {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} 216 * {@link android.content.Intent Intent}. The text to search (query string) for is provided by 217 * calling 218 * {@link #QUERY getStringExtra(SearchManager.QUERY)}.</li> 219 * <li>To identify and support your searchable activity, you'll need to 220 * provide an XML file providing searchability configuration parameters, a reference to that 221 * in your searchable activity's <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest</a> 222 * entry, and an intent-filter declaring that you can 223 * receive ACTION_SEARCH intents. This is described in more detail in the 224 * <a href="#SearchabilityMetadata">Searchability Metadata</a> section.</li> 225 * <li>Your <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest</a> also needs a metadata entry 226 * providing a global reference to the searchable activity. This is the "glue" directing the search 227 * UI, when invoked from any of your <i>other</i> activities, to use your application as the 228 * default search context. This is also described in more detail in the 229 * <a href="#SearchabilityMetadata">Searchability Metadata</a> section.</li> 230 * <li>Finally, you may want to define your search results activity as with the 231 * {@link android.R.attr#launchMode singleTop} launchMode flag. This allows the system 232 * to launch searches from/to the same activity without creating a pile of them on the 233 * activity stack. If you do this, be sure to also override 234 * {@link android.app.Activity#onNewIntent onNewIntent} to handle the 235 * updated intents (with new queries) as they arrive.</li> 236 * </ul> 237 * 238 * <p>Code snippet showing handling of intents in your search activity: 239 * <pre class="prettyprint"> 240 * @Override 241 * protected void onCreate(Bundle icicle) { 242 * super.onCreate(icicle); 243 * 244 * final Intent queryIntent = getIntent(); 245 * final String queryAction = queryIntent.getAction(); 246 * if (Intent.ACTION_SEARCH.equals(queryAction)) { 247 * doSearchWithIntent(queryIntent); 248 * } 249 * } 250 * 251 * private void doSearchWithIntent(final Intent queryIntent) { 252 * final String queryString = queryIntent.getStringExtra(SearchManager.QUERY); 253 * doSearchWithQuery(queryString); 254 * }</pre> 255 * 256 * <a name="FilterSearchApplications"></a> 257 * <h3>Filter-Search Applications</h3> 258 * 259 * <p>Filter-search applications are those that use live text entry (e.g. keystrokes)) to 260 * display and continuously update a list of results. Primary examples include applications 261 * that use locally-stored data. 262 * 263 * <p>Filter search is not directly supported by the Search Manager. Most filter search 264 * implementations will use variants of {@link android.widget.Filterable}, such as a 265 * {@link android.widget.ListView} bound to a {@link android.widget.SimpleCursorAdapter}. However, 266 * you may find it useful to mix them together, by declaring your filtered view searchable. With 267 * this configuration, you can still present the standard search dialog in all activities 268 * within your application, but transition to a filtered search when you enter the activity 269 * and display the results. 270 * 271 * <a name="Suggestions"></a> 272 * <h3>Search Suggestions</h3> 273 * 274 * <p>A powerful feature of the Search Manager is the ability of any application to easily provide 275 * live "suggestions" in order to prompt the user. Each application implements suggestions in a 276 * different, unique, and appropriate way. Suggestions be drawn from many sources, including but 277 * not limited to: 278 * <ul> 279 * <li>Actual searchable results (e.g. names in the address book)</li> 280 * <li>Recently entered queries</li> 281 * <li>Recently viewed data or results</li> 282 * <li>Contextually appropriate queries or results</li> 283 * <li>Summaries of possible results</li> 284 * </ul> 285 * 286 * <p>Another feature of suggestions is that they can expose queries or results before the user 287 * ever visits the application. This reduces the amount of context switching required, and helps 288 * the user access their data quickly and with less context shifting. In order to provide this 289 * capability, suggestions are accessed via a 290 * {@link android.content.ContentProvider Content Provider}. 291 * 292 * <p>The primary form of suggestions is known as <i>queried suggestions</i> and is based on query 293 * text that the user has already typed. This would generally be based on partial matches in 294 * the available data. In certain situations - for example, when no query text has been typed yet - 295 * an application may also opt to provide <i>zero-query suggestions</i>. 296 * These would typically be drawn from the same data source, but because no partial query text is 297 * available, they should be weighted based on other factors - for example, most recent queries 298 * or most recent results. 299 * 300 * <p><b>Overview of how suggestions are provided.</b> When the search manager identifies a 301 * particular activity as searchable, it will check for certain metadata which indicates that 302 * there is also a source of suggestions. If suggestions are provided, the following steps are 303 * taken. 304 * <ul><li>Using formatting information found in the metadata, the user's query text (whatever 305 * has been typed so far) will be formatted into a query and sent to the suggestions 306 * {@link android.content.ContentProvider Content Provider}.</li> 307 * <li>The suggestions {@link android.content.ContentProvider Content Provider} will create a 308 * {@link android.database.Cursor Cursor} which can iterate over the possible suggestions.</li> 309 * <li>The search manager will populate a list using display data found in each row of the cursor, 310 * and display these suggestions to the user.</li> 311 * <li>If the user types another key, or changes the query in any way, the above steps are repeated 312 * and the suggestions list is updated or repopulated.</li> 313 * <li>If the user clicks or touches the "GO" button, the suggestions are ignored and the search is 314 * launched using the normal {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} type of 315 * {@link android.content.Intent Intent}.</li> 316 * <li>If the user uses the directional controls to navigate the focus into the suggestions list, 317 * the query text will be updated while the user navigates from suggestion to suggestion. The user 318 * can then click or touch the updated query and edit it further. If the user navigates back to 319 * the edit field, the original typed query is restored.</li> 320 * <li>If the user clicks or touches a particular suggestion, then a combination of data from the 321 * cursor and 322 * values found in the metadata are used to synthesize an Intent and send it to the application. 323 * Depending on the design of the activity and the way it implements search, this might be a 324 * {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} (in order to launch a query), or it 325 * might be a {@link android.content.Intent#ACTION_VIEW ACTION_VIEW}, in order to proceed directly 326 * to display of specific data.</li> 327 * </ul> 328 * 329 * <p><b>Simple Recent-Query-Based Suggestions.</b> The Android framework provides a simple Search 330 * Suggestions provider, which simply records and replays recent queries. For many applications, 331 * this will be sufficient. The basic steps you will need to 332 * do, in order to use the built-in recent queries suggestions provider, are as follows: 333 * <ul> 334 * <li>Implement and test query search, as described in the previous sections.</li> 335 * <li>Create a Provider within your application by extending 336 * {@link android.content.SearchRecentSuggestionsProvider}.</li> 337 * <li>Create a manifest entry describing your provider.</li> 338 * <li>Update your searchable activity's XML configuration file with information about your 339 * provider.</li> 340 * <li>In your searchable activities, capture any user-generated queries and record them 341 * for future searches by calling {@link android.provider.SearchRecentSuggestions#saveRecentQuery}. 342 * </li> 343 * </ul> 344 * <p>For complete implementation details, please refer to 345 * {@link android.content.SearchRecentSuggestionsProvider}. The rest of the information in this 346 * section should not be necessary, as it refers to custom suggestions providers. 347 * 348 * <p><b>Creating a Customized Suggestions Provider:</b> In order to create more sophisticated 349 * suggestion providers, you'll need to take the following steps: 350 * <ul> 351 * <li>Implement and test query search, as described in the previous sections.</li> 352 * <li>Decide how you wish to <i>receive</i> suggestions. Just like queries that the user enters, 353 * suggestions will be delivered to your searchable activity as 354 * {@link android.content.Intent Intent} messages; Unlike simple queries, you have quite a bit of 355 * flexibility in forming those intents. A query search application will probably 356 * wish to continue receiving the {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} 357 * {@link android.content.Intent Intent}, which will launch a query search using query text as 358 * provided by the suggestion. A filter search application will probably wish to 359 * receive the {@link android.content.Intent#ACTION_VIEW ACTION_VIEW} 360 * {@link android.content.Intent Intent}, which will take the user directly to a selected entry. 361 * Other interesting suggestions, including hybrids, are possible, and the suggestion provider 362 * can easily mix-and-match results to provide a richer set of suggestions for the user. Finally, 363 * you'll need to update your searchable activity (or other activities) to receive the intents 364 * as you've defined them.</li> 365 * <li>Implement a Content Provider that provides suggestions. If you already have one, and it 366 * has access to your suggestions data. If not, you'll have to create one. 367 * You'll also provide information about your Content Provider in your 368 * package's <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest</a>.</li> 369 * <li>Update your searchable activity's XML configuration file. There are two categories of 370 * information used for suggestions: 371 * <ul><li>The first is (required) data that the search manager will 372 * use to format the queries which are sent to the Content Provider.</li> 373 * <li>The second is (optional) parameters to configure structure 374 * if intents generated by suggestions.</li></li> 375 * </ul> 376 * </ul> 377 * 378 * <p><b>Configuring your Content Provider to Receive Suggestion Queries.</b> The basic job of 379 * a search suggestions {@link android.content.ContentProvider Content Provider} is to provide 380 * "live" (while-you-type) conversion of the user's query text into a set of zero or more 381 * suggestions. Each application is free to define the conversion, and as described above there are 382 * many possible solutions. This section simply defines how to communicate with the suggestion 383 * provider. 384 * 385 * <p>The Search Manager must first determine if your package provides suggestions. This is done 386 * by examination of your searchable meta-data XML file. The android:searchSuggestAuthority 387 * attribute, if provided, is the signal to obtain & display suggestions. 388 * 389 * <p>Every query includes a Uri, and the Search Manager will format the Uri as shown: 390 * <p><pre class="prettyprint"> 391 * content:// your.suggest.authority / your.suggest.path / SearchManager.SUGGEST_URI_PATH_QUERY</pre> 392 * 393 * <p>Your Content Provider can receive the query text in one of two ways. 394 * <ul> 395 * <li><b>Query provided as a selection argument.</b> If you define the attribute value 396 * android:searchSuggestSelection and include a string, this string will be passed as the 397 * <i>selection</i> parameter to your Content Provider's query function. You must define a single 398 * selection argument, using the '?' character. The user's query text will be passed to you 399 * as the first element of the selection arguments array.</li> 400 * <li><b>Query provided with Data Uri.</b> If you <i>do not</i> define the attribute value 401 * android:searchSuggestSelection, then the Search Manager will append another "/" followed by 402 * the user's query to the query Uri. The query will be encoding using Uri encoding rules - don't 403 * forget to decode it. (See {@link android.net.Uri#getPathSegments} and 404 * {@link android.net.Uri#getLastPathSegment} for helpful utilities you can use here.)</li> 405 * </ul> 406 * 407 * <p><b>Handling empty queries.</b> Your application should handle the "empty query" 408 * (no user text entered) case properly, and generate useful suggestions in this case. There are a 409 * number of ways to do this; Two are outlined here: 410 * <ul><li>For a simple filter search of local data, you could simply present the entire dataset, 411 * unfiltered. (example: People)</li> 412 * <li>For a query search, you could simply present the most recent queries. This allows the user 413 * to quickly repeat a recent search.</li></ul> 414 * 415 * <p><b>The Format of Individual Suggestions.</b> Your suggestions are communicated back to the 416 * Search Manager by way of a {@link android.database.Cursor Cursor}. The Search Manager will 417 * usually pass a null Projection, which means that your provider can simply return all appropriate 418 * columns for each suggestion. The columns currently defined are: 419 * 420 * <table border="2" width="85%" align="center" frame="hsides" rules="rows"> 421 * 422 * <thead> 423 * <tr><th>Column Name</th> <th>Description</th> <th>Required?</th></tr> 424 * </thead> 425 * 426 * <tbody> 427 * <tr><th>{@link #SUGGEST_COLUMN_FORMAT}</th> 428 * <td><i>Unused - can be null.</i></td> 429 * <td align="center">No</td> 430 * </tr> 431 * 432 * <tr><th>{@link #SUGGEST_COLUMN_TEXT_1}</th> 433 * <td>This is the line of text that will be presented to the user as the suggestion.</td> 434 * <td align="center">Yes</td> 435 * </tr> 436 * 437 * <tr><th>{@link #SUGGEST_COLUMN_TEXT_2}</th> 438 * <td>If your cursor includes this column, then all suggestions will be provided in a 439 * two-line format. The data in this column will be displayed as a second, smaller 440 * line of text below the primary suggestion, or it can be null or empty to indicate no 441 * text in this row's suggestion.</td> 442 * <td align="center">No</td> 443 * </tr> 444 * 445 * <tr><th>{@link #SUGGEST_COLUMN_ICON_1}</th> 446 * <td>If your cursor includes this column, then all suggestions will be provided in an 447 * icons+text format. This value should be a reference to the icon to 448 * draw on the left side, or it can be null or zero to indicate no icon in this row. 449 * </td> 450 * <td align="center">No.</td> 451 * </tr> 452 * 453 * <tr><th>{@link #SUGGEST_COLUMN_ICON_2}</th> 454 * <td>If your cursor includes this column, then all suggestions will be provided in an 455 * icons+text format. This value should be a reference to the icon to 456 * draw on the right side, or it can be null or zero to indicate no icon in this row. 457 * </td> 458 * <td align="center">No.</td> 459 * </tr> 460 * 461 * <tr><th>{@link #SUGGEST_COLUMN_INTENT_ACTION}</th> 462 * <td>If this column exists <i>and</i> this element exists at the given row, this is the 463 * action that will be used when forming the suggestion's intent. If the element is 464 * not provided, the action will be taken from the android:searchSuggestIntentAction 465 * field in your XML metadata. <i>At least one of these must be present for the 466 * suggestion to generate an intent.</i> Note: If your action is the same for all 467 * suggestions, it is more efficient to specify it using XML metadata and omit it from 468 * the cursor.</td> 469 * <td align="center">No</td> 470 * </tr> 471 * 472 * <tr><th>{@link #SUGGEST_COLUMN_INTENT_DATA}</th> 473 * <td>If this column exists <i>and</i> this element exists at the given row, this is the 474 * data that will be used when forming the suggestion's intent. If the element is not 475 * provided, the data will be taken from the android:searchSuggestIntentData field in 476 * your XML metadata. If neither source is provided, the Intent's data field will be 477 * null. Note: If your data is the same for all suggestions, or can be described 478 * using a constant part and a specific ID, it is more efficient to specify it using 479 * XML metadata and omit it from the cursor.</td> 480 * <td align="center">No</td> 481 * </tr> 482 * 483 * <tr><th>{@link #SUGGEST_COLUMN_INTENT_DATA_ID}</th> 484 * <td>If this column exists <i>and</i> this element exists at the given row, then "/" and 485 * this value will be appended to the data field in the Intent. This should only be 486 * used if the data field has already been set to an appropriate base string.</td> 487 * <td align="center">No</td> 488 * </tr> 489 * 490 * <tr><th>{@link #SUGGEST_COLUMN_QUERY}</th> 491 * <td>If this column exists <i>and</i> this element exists at the given row, this is the 492 * data that will be used when forming the suggestion's query.</td> 493 * <td align="center">Required if suggestion's action is 494 * {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH}, optional otherwise.</td> 495 * </tr> 496 * 497 * <tr><th><i>Other Columns</i></th> 498 * <td>Finally, if you have defined any <a href="#ActionKeys">Action Keys</a> and you wish 499 * for them to have suggestion-specific definitions, you'll need to define one 500 * additional column per action key. The action key will only trigger if the 501 * currently-selection suggestion has a non-empty string in the corresponding column. 502 * See the section on <a href="#ActionKeys">Action Keys</a> for additional details and 503 * implementation steps.</td> 504 * <td align="center">No</td> 505 * </tr> 506 * 507 * </tbody> 508 * </table> 509 * 510 * <p>Clearly there are quite a few permutations of your suggestion data, but in the next section 511 * we'll look at a few simple combinations that you'll select from. 512 * 513 * <p><b>The Format Of Intents Sent By Search Suggestions.</b> Although there are many ways to 514 * configure these intents, this document will provide specific information on just a few of them. 515 * <ul><li><b>Launch a query.</b> In this model, each suggestion represents a query that your 516 * searchable activity can perform, and the {@link android.content.Intent Intent} will be formatted 517 * exactly like those sent when the user enters query text and clicks the "GO" button: 518 * <ul> 519 * <li><b>Action:</b> {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} provided 520 * using your XML metadata (android:searchSuggestIntentAction).</li> 521 * <li><b>Data:</b> empty (not used).</li> 522 * <li><b>Query:</b> query text supplied by the cursor.</li> 523 * </ul> 524 * </li> 525 * <li><b>Go directly to a result, using a complete Data Uri.</b> In this model, the user will be 526 * taken directly to a specific result. 527 * <ul> 528 * <li><b>Action:</b> {@link android.content.Intent#ACTION_VIEW ACTION_VIEW}</li> 529 * <li><b>Data:</b> a complete Uri, supplied by the cursor, that identifies the desired data.</li> 530 * <li><b>Query:</b> query text supplied with the suggestion (probably ignored)</li> 531 * </ul> 532 * </li> 533 * <li><b>Go directly to a result, using a synthesized Data Uri.</b> This has the same result 534 * as the previous suggestion, but provides the Data Uri in a different way. 535 * <ul> 536 * <li><b>Action:</b> {@link android.content.Intent#ACTION_VIEW ACTION_VIEW}</li> 537 * <li><b>Data:</b> The search manager will assemble a Data Uri using the following elements: 538 * a Uri fragment provided in your XML metadata (android:searchSuggestIntentData), followed by 539 * a single "/", followed by the value found in the {@link #SUGGEST_COLUMN_INTENT_DATA_ID} 540 * entry in your cursor.</li> 541 * <li><b>Query:</b> query text supplied with the suggestion (probably ignored)</li> 542 * </ul> 543 * </li> 544 * </ul> 545 * <p>This list is not meant to be exhaustive. Applications should feel free to define other types 546 * of suggestions. For example, you could reduce long lists of results to summaries, and use one 547 * of the above intents (or one of your own) with specially formatted Data Uri's to display more 548 * detailed results. Or you could display textual shortcuts as suggestions, but launch a display 549 * in a more data-appropriate format such as media artwork. 550 * 551 * <p><b>Suggestion Rewriting.</b> If the user navigates through the suggestions list, the UI 552 * may temporarily rewrite the user's query with a query that matches the currently selected 553 * suggestion. This enables the user to see what query is being suggested, and also allows the user 554 * to click or touch in the entry EditText element and make further edits to the query before 555 * dispatching it. In order to perform this correctly, the Search UI needs to know exactly what 556 * text to rewrite the query with. 557 * 558 * <p>For each suggestion, the following logic is used to select a new query string: 559 * <ul><li>If the suggestion provides an explicit value in the {@link #SUGGEST_COLUMN_QUERY} 560 * column, this value will be used.</li> 561 * <li>If the metadata includes the queryRewriteFromData flag, and the suggestion provides an 562 * explicit value for the intent Data field, this Uri will be used. Note that this should only be 563 * used with Uri's that are intended to be user-visible, such as HTTP. Internal Uri schemes should 564 * not be used in this way.</li> 565 * <li>If the metadata includes the queryRewriteFromText flag, the text in 566 * {@link #SUGGEST_COLUMN_TEXT_1} will be used. This should be used for suggestions in which no 567 * query text is provided and the SUGGEST_COLUMN_INTENT_DATA values are not suitable for user 568 * inspection and editing.</li></ul> 569 * 570 * <a name="ActionKeys"></a> 571 * <h3>Action Keys</h3> 572 * 573 * <p>Searchable activities may also wish to provide shortcuts based on the various action keys 574 * available on the device. The most basic example of this is the contacts app, which enables the 575 * green "dial" key for quick access during searching. Not all action keys are available on 576 * every device, and not all are allowed to be overriden in this way. (For example, the "Home" 577 * key must always return to the home screen, with no exceptions.) 578 * 579 * <p>In order to define action keys for your searchable application, you must do two things. 580 * 581 * <ul> 582 * <li>You'll add one or more <i>actionkey</i> elements to your searchable metadata configuration 583 * file. Each element defines one of the keycodes you are interested in, 584 * defines the conditions under which they are sent, and provides details 585 * on how to communicate the action key event back to your searchable activity.</li> 586 * <li>In your broadcast receiver, if you wish, you can check for action keys by checking the 587 * extras field of the {@link android.content.Intent Intent}.</li> 588 * </ul> 589 * 590 * <p><b>Updating metadata.</b> For each keycode of interest, you must add an <actionkey> 591 * element. Within this element you must define two or three attributes. The first attribute, 592 * <android:keycode>, is required; It is the key code of the action key event, as defined in 593 * {@link android.view.KeyEvent}. The remaining two attributes define the value of the actionkey's 594 * <i>message</i>, which will be passed to your searchable activity in the 595 * {@link android.content.Intent Intent} (see below for more details). Although each of these 596 * attributes is optional, you must define one or both for the action key to have any effect. 597 * <android:queryActionMsg> provides the message that will be sent if the action key is 598 * pressed while the user is simply entering query text. <android:suggestActionMsgColumn> 599 * is used when action keys are tied to specific suggestions. This attribute provides the name 600 * of a <i>column</i> in your suggestion cursor; The individual suggestion, in that column, 601 * provides the message. (If the cell is empty or null, that suggestion will not work with that 602 * action key.) 603 * <p>See the <a href="#SearchabilityMetadata">Searchability Metadata</a> section for more details 604 * and examples. 605 * 606 * <p><b>Receiving Action Keys</b> Intents launched by action keys will be specially marked 607 * using a combination of values. This enables your searchable application to examine the intent, 608 * if necessary, and perform special processing. For example, clicking a suggested contact might 609 * simply display them; Selecting a suggested contact and clicking the dial button might 610 * immediately call them. 611 * 612 * <p>When a search {@link android.content.Intent Intent} is launched by an action key, two values 613 * will be added to the extras field. 614 * <ul> 615 * <li>To examine the key code, use {@link android.content.Intent#getIntExtra 616 * getIntExtra(SearchManager.ACTION_KEY)}.</li> 617 * <li>To examine the message string, use {@link android.content.Intent#getStringExtra 618 * getStringExtra(SearchManager.ACTION_MSG)}</li> 619 * </ul> 620 * 621 * <a name="SearchabilityMetadata"></a> 622 * <h3>Searchability Metadata</h3> 623 * 624 * <p>Every activity that is searchable must provide a small amount of additional information 625 * in order to properly configure the search system. This controls the way that your search 626 * is presented to the user, and controls for the various modalities described previously. 627 * 628 * <p>If your application is not searchable, 629 * then you do not need to provide any search metadata, and you can skip the rest of this section. 630 * When this search metadata cannot be found, the search manager will assume that the activity 631 * does not implement search. (Note: to implement web-based search, you will need to add 632 * the android.app.default_searchable metadata to your manifest, as shown below.) 633 * 634 * <p>Values you supply in metadata apply only to each local searchable activity. Each 635 * searchable activity can define a completely unique search experience relevant to its own 636 * capabilities and user experience requirements, and a single application can even define multiple 637 * searchable activities. 638 * 639 * <p><b>Metadata for searchable activity.</b> As with your search implementations described 640 * above, you must first identify which of your activities is searchable. In the 641 * <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest</a> entry for this activity, you must 642 * provide two elements: 643 * <ul><li>An intent-filter specifying that you can receive and process the 644 * {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} {@link android.content.Intent Intent}. 645 * </li> 646 * <li>A reference to a small XML file (typically called "searchable.xml") which contains the 647 * remaining configuration information for how your application implements search.</li></ul> 648 * 649 * <p>Here is a snippet showing the necessary elements in the 650 * <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest</a> entry for your searchable activity. 651 * <pre class="prettyprint"> 652 * <!-- Search Activity - searchable --> 653 * <activity android:name="MySearchActivity" 654 * android:label="Search" 655 * android:launchMode="singleTop"> 656 * <intent-filter> 657 * <action android:name="android.intent.action.SEARCH" /> 658 * <category android:name="android.intent.category.DEFAULT" /> 659 * </intent-filter> 660 * <meta-data android:name="android.app.searchable" 661 * android:resource="@xml/searchable" /> 662 * </activity></pre> 663 * 664 * <p>Next, you must provide the rest of the searchability configuration in 665 * the small XML file, stored in the ../xml/ folder in your build. The XML file is a 666 * simple enumeration of the search configuration parameters for searching within this activity, 667 * application, or package. Here is a sample XML file (named searchable.xml, for use with 668 * the above manifest) for a query-search activity. 669 * 670 * <pre class="prettyprint"> 671 * <searchable xmlns:android="http://schemas.android.com/apk/res/android" 672 * android:label="@string/search_label" 673 * android:hint="@string/search_hint" > 674 * </searchable></pre> 675 * 676 * <p>Note that all user-visible strings <i>must</i> be provided in the form of "@string" 677 * references. Hard-coded strings, which cannot be localized, will not work properly in search 678 * metadata. 679 * 680 * <p>Attributes you can set in search metadata: 681 * <table border="2" width="85%" align="center" frame="hsides" rules="rows"> 682 * 683 * <thead> 684 * <tr><th>Attribute</th> <th>Description</th> <th>Required?</th></tr> 685 * </thead> 686 * 687 * <tbody> 688 * <tr><th>android:label</th> 689 * <td>This is the name for your application that will be presented to the user in a 690 * list of search targets, or in the search box as a label.</td> 691 * <td align="center">Yes</td> 692 * </tr> 693 * 694 * <tr><th>android:icon</th> 695 * <td>If provided, this icon will be used <i>in place</i> of the label string. This 696 * is provided in order to present logos or other non-textual banners.</td> 697 * <td align="center">No</td> 698 * </tr> 699 * 700 * <tr><th>android:hint</th> 701 * <td>This is the text to display in the search text field when no user text has been 702 * entered.</td> 703 * <td align="center">No</td> 704 * </tr> 705 * 706 * <tr><th>android:searchButtonText</th> 707 * <td>If provided, this text will replace the default text in the "Search" button.</td> 708 * <td align="center">No</td> 709 * </tr> 710 * 711 * <tr><th>android:searchMode</th> 712 * <td>If provided and non-zero, sets additional modes for control of the search 713 * presentation. The following mode bits are defined: 714 * <table border="2" align="center" frame="hsides" rules="rows"> 715 * <tbody> 716 * <tr><th>showSearchLabelAsBadge</th> 717 * <td>If set, this flag enables the display of the search target (label) 718 * within the search bar. If this flag and showSearchIconAsBadge 719 * (see below) are both not set, no badge will be shown.</td> 720 * </tr> 721 * <tr><th>showSearchIconAsBadge</th> 722 * <td>If set, this flag enables the display of the search target (icon) within 723 * the search bar. If this flag and showSearchLabelAsBadge 724 * (see above) are both not set, no badge will be shown. If both flags 725 * are set, showSearchIconAsBadge has precedence and the icon will be 726 * shown.</td> 727 * </tr> 728 * <tr><th>queryRewriteFromData</th> 729 * <td>If set, this flag causes the suggestion column SUGGEST_COLUMN_INTENT_DATA 730 * to be considered as the text for suggestion query rewriting. This should 731 * only be used when the values in SUGGEST_COLUMN_INTENT_DATA are suitable 732 * for user inspection and editing - typically, HTTP/HTTPS Uri's.</td> 733 * </tr> 734 * <tr><th>queryRewriteFromText</th> 735 * <td>If set, this flag causes the suggestion column SUGGEST_COLUMN_TEXT_1 to 736 * be considered as the text for suggestion query rewriting. This should 737 * be used for suggestions in which no query text is provided and the 738 * SUGGEST_COLUMN_INTENT_DATA values are not suitable for user inspection 739 * and editing.</td> 740 * </tr> 741 * </tbody> 742 * </table></td> 743 * <td align="center">No</td> 744 * </tr> 745 * 746 * <tr><th>android:inputType</th> 747 * <td>If provided, supplies a hint about the type of search text the user will be 748 * entering. For most searches, in which free form text is expected, this attribute 749 * need not be provided. Suitable values for this attribute are described in the 750 * <a href="../R.attr.html#inputType">inputType</a> attribute.</td> 751 * <td align="center">No</td> 752 * </tr> 753 * <tr><th>android:imeOptions</th> 754 * <td>If provided, supplies additional options for the input method. 755 * For most searches, in which free form text is expected, this attribute 756 * need not be provided, and will default to "actionSearch". 757 * Suitable values for this attribute are described in the 758 * <a href="../R.attr.html#imeOptions">imeOptions</a> attribute.</td> 759 * <td align="center">No</td> 760 * </tr> 761 * 762 * </tbody> 763 * </table> 764 * 765 * <p><b>Styleable Resources in your Metadata.</b> It's possible to provide alternate strings 766 * for your searchable application, in order to provide localization and/or to better visual 767 * presentation on different device configurations. Each searchable activity has a single XML 768 * metadata file, but any resource references can be replaced at runtime based on device 769 * configuration, language setting, and other system inputs. 770 * 771 * <p>A concrete example is the "hint" text you supply using the android:searchHint attribute. 772 * In portrait mode you'll have less screen space and may need to provide a shorter string, but 773 * in landscape mode you can provide a longer, more descriptive hint. To do this, you'll need to 774 * define two or more strings.xml files, in the following directories: 775 * <ul><li>.../res/values-land/strings.xml</li> 776 * <li>.../res/values-port/strings.xml</li> 777 * <li>.../res/values/strings.xml</li></ul> 778 * 779 * <p>For more complete documentation on this capability, see 780 * <a href="{@docRoot}guide/topics/resources/resources-i18n.html#AlternateResources">Resources and 781 * Internationalization: Alternate Resources</a>. 782 * 783 * <p><b>Metadata for non-searchable activities.</b> Activities which are part of a searchable 784 * application, but don't implement search itself, require a bit of "glue" in order to cause 785 * them to invoke search using your searchable activity as their primary context. If this is not 786 * provided, then searches from these activities will use the system default search context. 787 * 788 * <p>The simplest way to specify this is to add a <i>search reference</i> element to the 789 * application entry in the <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest</a> file. 790 * The value of this reference can be either of: 791 * <ul><li>The name of your searchable activity. 792 * It is typically prefixed by '.' to indicate that it's in the same package.</li> 793 * <li>A "*" indicates that the system may select a default searchable activity, in which 794 * case it will typically select web-based search.</li> 795 * </ul> 796 * 797 * <p>Here is a snippet showing the necessary addition to the manifest entry for your 798 * non-searchable activities. 799 * <pre class="prettyprint"> 800 * <application> 801 * <meta-data android:name="android.app.default_searchable" 802 * android:value=".MySearchActivity" /> 803 * 804 * <!-- followed by activities, providers, etc... --> 805 * </application></pre> 806 * 807 * <p>You can also specify android.app.default_searchable on a per-activity basis, by including 808 * the meta-data element (as shown above) in one or more activity sections. If found, these will 809 * override the reference in the application section. The only reason to configure your application 810 * this way would be if you wish to partition it into separate sections with different search 811 * behaviors; Otherwise this configuration is not recommended. 812 * 813 * <p><b>Additional metadata for search suggestions.</b> If you have defined a content provider 814 * to generate search suggestions, you'll need to publish it to the system, and you'll need to 815 * provide a bit of additional XML metadata in order to configure communications with it. 816 * 817 * <p>First, in your <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest</a>, you'll add the 818 * following lines. 819 * <pre class="prettyprint"> 820 * <!-- Content provider for search suggestions --> 821 * <provider android:name="YourSuggestionProviderClass" 822 * android:authorities="your.suggestion.authority" /></pre> 823 * 824 * <p>Next, you'll add a few lines to your XML metadata file, as shown: 825 * <pre class="prettyprint"> 826 * <!-- Required attribute for any suggestions provider --> 827 * android:searchSuggestAuthority="your.suggestion.authority" 828 * 829 * <!-- Optional attribute for configuring queries --> 830 * android:searchSuggestSelection="field =?" 831 * 832 * <!-- Optional attributes for configuring intent construction --> 833 * android:searchSuggestIntentAction="intent action string" 834 * android:searchSuggestIntentData="intent data Uri" /></pre> 835 * 836 * <p>Elements of search metadata that support suggestions: 837 * <table border="2" width="85%" align="center" frame="hsides" rules="rows"> 838 * 839 * <thead> 840 * <tr><th>Attribute</th> <th>Description</th> <th>Required?</th></tr> 841 * </thead> 842 * 843 * <tbody> 844 * <tr><th>android:searchSuggestAuthority</th> 845 * <td>This value must match the authority string provided in the <i>provider</i> section 846 * of your <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest</a>.</td> 847 * <td align="center">Yes</td> 848 * </tr> 849 * 850 * <tr><th>android:searchSuggestPath</th> 851 * <td>If provided, this will be inserted in the suggestions query Uri, after the authority 852 * you have provide but before the standard suggestions path. This is only required if 853 * you have a single content provider issuing different types of suggestions (e.g. for 854 * different data types) and you need a way to disambiguate the suggestions queries 855 * when they are received.</td> 856 * <td align="center">No</td> 857 * </tr> 858 * 859 * <tr><th>android:searchSuggestSelection</th> 860 * <td>If provided, this value will be passed into your query function as the 861 * <i>selection</i> parameter. Typically this will be a WHERE clause for your database, 862 * and will contain a single question mark, which represents the actual query string 863 * that has been typed by the user. However, you can also use any non-null value 864 * to simply trigger the delivery of the query text (via selection arguments), and then 865 * use the query text in any way appropriate for your provider (ignoring the actual 866 * text of the selection parameter.)</td> 867 * <td align="center">No</td> 868 * </tr> 869 * 870 * <tr><th>android:searchSuggestIntentAction</th> 871 * <td>If provided, and not overridden by the selected suggestion, this value will be 872 * placed in the action field of the {@link android.content.Intent Intent} when the 873 * user clicks a suggestion.</td> 874 * <td align="center">No</td> 875 * 876 * <tr><th>android:searchSuggestIntentData</th> 877 * <td>If provided, and not overridden by the selected suggestion, this value will be 878 * placed in the data field of the {@link android.content.Intent Intent} when the user 879 * clicks a suggestion.</td> 880 * <td align="center">No</td> 881 * </tr> 882 * 883 * </tbody> 884 * </table> 885 * 886 * <p><b>Additional metadata for search action keys.</b> For each action key that you would like to 887 * define, you'll need to add an additional element defining that key, and using the attributes 888 * discussed in <a href="#ActionKeys">Action Keys</a>. A simple example is shown here: 889 * 890 * <pre class="prettyprint"><actionkey 891 * android:keycode="KEYCODE_CALL" 892 * android:queryActionMsg="call" 893 * android:suggestActionMsg="call" 894 * android:suggestActionMsgColumn="call_column" /></pre> 895 * 896 * <p>Elements of search metadata that support search action keys. Note that although each of the 897 * action message elements are marked as <i>optional</i>, at least one must be present for the 898 * action key to have any effect. 899 * 900 * <table border="2" width="85%" align="center" frame="hsides" rules="rows"> 901 * 902 * <thead> 903 * <tr><th>Attribute</th> <th>Description</th> <th>Required?</th></tr> 904 * </thead> 905 * 906 * <tbody> 907 * <tr><th>android:keycode</th> 908 * <td>This attribute denotes the action key you wish to respond to. Note that not 909 * all action keys are actually supported using this mechanism, as many of them are 910 * used for typing, navigation, or system functions. This will be added to the 911 * {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} intent that is passed to 912 * your searchable activity. To examine the key code, use 913 * {@link android.content.Intent#getIntExtra getIntExtra(SearchManager.ACTION_KEY)}. 914 * <p>Note, in addition to the keycode, you must also provide one or more of the action 915 * specifier attributes.</td> 916 * <td align="center">Yes</td> 917 * </tr> 918 * 919 * <tr><th>android:queryActionMsg</th> 920 * <td>If you wish to handle an action key during normal search query entry, you 921 * must define an action string here. This will be added to the 922 * {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} intent that is passed to your 923 * searchable activity. To examine the string, use 924 * {@link android.content.Intent#getStringExtra 925 * getStringExtra(SearchManager.ACTION_MSG)}.</td> 926 * <td align="center">No</td> 927 * </tr> 928 * 929 * <tr><th>android:suggestActionMsg</th> 930 * <td>If you wish to handle an action key while a suggestion is being displayed <i>and 931 * selected</i>, there are two ways to handle this. If <i>all</i> of your suggestions 932 * can handle the action key, you can simply define the action message using this 933 * attribute. This will be added to the 934 * {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} intent that is passed to 935 * your searchable activity. To examine the string, use 936 * {@link android.content.Intent#getStringExtra 937 * getStringExtra(SearchManager.ACTION_MSG)}.</td> 938 * <td align="center">No</td> 939 * </tr> 940 * 941 * <tr><th>android:suggestActionMsgColumn</th> 942 * <td>If you wish to handle an action key while a suggestion is being displayed <i>and 943 * selected</i>, but you do not wish to enable this action key for every suggestion, 944 * then you can use this attribute to control it on a suggestion-by-suggestion basis. 945 * First, you must define a column (and name it here) where your suggestions will 946 * include the action string. Then, in your content provider, you must provide this 947 * column, and when desired, provide data in this column. 948 * The search manager will look at your suggestion cursor, using the string 949 * provided here in order to select a column, and will use that to select a string from 950 * the cursor. That string will be added to the 951 * {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} intent that is passed to 952 * your searchable activity. To examine the string, use 953 * {@link android.content.Intent#getStringExtra 954 * getStringExtra(SearchManager.ACTION_MSG)}. <i>If the data does not exist for the 955 * selection suggestion, the action key will be ignored.</i></td> 956 * <td align="center">No</td> 957 * </tr> 958 * 959 * </tbody> 960 * </table> 961 * 962 * <p><b>Additional metadata for enabling voice search.</b> To enable voice search for your 963 * activity, you can add fields to the metadata that enable and configure voice search. When 964 * enabled (and available on the device), a voice search button will be displayed in the 965 * Search UI. Clicking this button will launch a voice search activity. When the user has 966 * finished speaking, the voice search phrase will be transcribed into text and presented to the 967 * searchable activity as if it were a typed query. 968 * 969 * <p>Elements of search metadata that support voice search: 970 * <table border="2" width="85%" align="center" frame="hsides" rules="rows"> 971 * 972 * <thead> 973 * <tr><th>Attribute</th> <th>Description</th> <th>Required?</th></tr> 974 * </thead> 975 * 976 * <tr><th>android:voiceSearchMode</th> 977 * <td>If provided and non-zero, enables voice search. (Voice search may not be 978 * provided by the device, in which case these flags will have no effect.) The 979 * following mode bits are defined: 980 * <table border="2" align="center" frame="hsides" rules="rows"> 981 * <tbody> 982 * <tr><th>showVoiceSearchButton</th> 983 * <td>If set, display a voice search button. This only takes effect if voice 984 * search is available on the device. If set, then launchWebSearch or 985 * launchRecognizer must also be set.</td> 986 * </tr> 987 * <tr><th>launchWebSearch</th> 988 * <td>If set, the voice search button will take the user directly to a 989 * built-in voice web search activity. Most applications will not use this 990 * flag, as it will take the user away from the activity in which search 991 * was invoked.</td> 992 * </tr> 993 * <tr><th>launchRecognizer</th> 994 * <td>If set, the voice search button will take the user directly to a 995 * built-in voice recording activity. This activity will prompt the user 996 * to speak, transcribe the spoken text, and forward the resulting query 997 * text to the searchable activity, just as if the user had typed it into 998 * the search UI and clicked the search button.</td> 999 * </tr> 1000 * </tbody> 1001 * </table></td> 1002 * <td align="center">No</td> 1003 * </tr> 1004 * 1005 * <tr><th>android:voiceLanguageModel</th> 1006 * <td>If provided, this specifies the language model that should be used by the voice 1007 * recognition system. 1008 * See {@link android.speech.RecognizerIntent#EXTRA_LANGUAGE_MODEL} 1009 * for more information. If not provided, the default value 1010 * {@link android.speech.RecognizerIntent#LANGUAGE_MODEL_FREE_FORM} will be used.</td> 1011 * <td align="center">No</td> 1012 * </tr> 1013 * 1014 * <tr><th>android:voicePromptText</th> 1015 * <td>If provided, this specifies a prompt that will be displayed during voice input. 1016 * (If not provided, a default prompt will be displayed.)</td> 1017 * <td align="center">No</td> 1018 * </tr> 1019 * 1020 * <tr><th>android:voiceLanguage</th> 1021 * <td>If provided, this specifies the spoken language to be expected. This is only 1022 * needed if it is different from the current value of 1023 * {@link java.util.Locale#getDefault()}. 1024 * </td> 1025 * <td align="center">No</td> 1026 * </tr> 1027 * 1028 * <tr><th>android:voiceMaxResults</th> 1029 * <td>If provided, enforces the maximum number of results to return, including the "best" 1030 * result which will always be provided as the SEARCH intent's primary query. Must be 1031 * one or greater. Use {@link android.speech.RecognizerIntent#EXTRA_RESULTS} 1032 * to get the results from the intent. If not provided, the recognizer will choose 1033 * how many results to return.</td> 1034 * <td align="center">No</td> 1035 * </tr> 1036 * 1037 * </tbody> 1038 * </table> 1039 * 1040 * <a name="PassingSearchContext"></a> 1041 * <h3>Passing Search Context</h3> 1042 * 1043 * <p>In order to improve search experience, an application may wish to specify 1044 * additional data along with the search, such as local history or context. For 1045 * example, a maps search would be improved by including the current location. 1046 * In order to simplify the structure of your activities, this can be done using 1047 * the search manager. 1048 * 1049 * <p>Any data can be provided at the time the search is launched, as long as it 1050 * can be stored in a {@link android.os.Bundle Bundle} object. 1051 * 1052 * <p>To pass application data into the Search Manager, you'll need to override 1053 * {@link android.app.Activity#onSearchRequested onSearchRequested} as follows: 1054 * 1055 * <pre class="prettyprint"> 1056 * @Override 1057 * public boolean onSearchRequested() { 1058 * Bundle appData = new Bundle(); 1059 * appData.put...(); 1060 * appData.put...(); 1061 * startSearch(null, false, appData); 1062 * return true; 1063 * }</pre> 1064 * 1065 * <p>To receive application data from the Search Manager, you'll extract it from 1066 * the {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} 1067 * {@link android.content.Intent Intent} as follows: 1068 * 1069 * <pre class="prettyprint"> 1070 * final Bundle appData = queryIntent.getBundleExtra(SearchManager.APP_DATA); 1071 * if (appData != null) { 1072 * appData.get...(); 1073 * appData.get...(); 1074 * }</pre> 1075 * 1076 * <a name="ProtectingUserPrivacy"></a> 1077 * <h3>Protecting User Privacy</h3> 1078 * 1079 * <p>Many users consider their activities on the phone, including searches, to be private 1080 * information. Applications that implement search should take steps to protect users' privacy 1081 * wherever possible. This section covers two areas of concern, but you should consider your search 1082 * design carefully and take any additional steps necessary. 1083 * 1084 * <p><b>Don't send personal information to servers, and if you do, don't log it.</b> 1085 * "Personal information" is information that can personally identify your users, such as name, 1086 * email address or billing information, or other data which can be reasonably linked to such 1087 * information. If your application implements search with the assistance of a server, try to 1088 * avoid sending personal information with your searches. For example, if you are searching for 1089 * businesses near a zip code, you don't need to send the user ID as well - just send the zip code 1090 * to the server. If you do need to send personal information, you should take steps to avoid 1091 * logging it. If you must log it, you should protect that data very carefully, and erase it as 1092 * soon as possible. 1093 * 1094 * <p><b>Provide the user with a way to clear their search history.</b> The Search Manager helps 1095 * your application provide context-specific suggestions. Sometimes these suggestions are based 1096 * on previous searches, or other actions taken by the user in an earlier session. A user may not 1097 * wish for previous searches to be revealed to other users, for instance if they share their phone 1098 * with a friend. If your application provides suggestions that can reveal previous activities, 1099 * you should implement a "Clear History" menu, preference, or button. If you are using 1100 * {@link android.provider.SearchRecentSuggestions}, you can simply call its 1101 * {@link android.provider.SearchRecentSuggestions#clearHistory() clearHistory()} method from 1102 * your "Clear History" UI. If you are implementing your own form of recent suggestions, you'll 1103 * need to provide a similar a "clear history" API in your provider, and call it from your 1104 * "Clear History" UI. 1105 */ 1106public class SearchManager 1107 implements DialogInterface.OnDismissListener, DialogInterface.OnCancelListener 1108{ 1109 /** 1110 * This is a shortcut definition for the default menu key to use for invoking search. 1111 * 1112 * See Menu.Item.setAlphabeticShortcut() for more information. 1113 */ 1114 public final static char MENU_KEY = 's'; 1115 1116 /** 1117 * This is a shortcut definition for the default menu key to use for invoking search. 1118 * 1119 * See Menu.Item.setAlphabeticShortcut() for more information. 1120 */ 1121 public final static int MENU_KEYCODE = KeyEvent.KEYCODE_S; 1122 1123 /** 1124 * Intent extra data key: Use this key with 1125 * {@link android.content.Intent#getStringExtra 1126 * content.Intent.getStringExtra()} 1127 * to obtain the query string from Intent.ACTION_SEARCH. 1128 */ 1129 public final static String QUERY = "query"; 1130 1131 /** 1132 * Intent extra data key: Use this key with Intent.ACTION_SEARCH and 1133 * {@link android.content.Intent#getBundleExtra 1134 * content.Intent.getBundleExtra()} 1135 * to obtain any additional app-specific data that was inserted by the 1136 * activity that launched the search. 1137 */ 1138 public final static String APP_DATA = "app_data"; 1139 1140 /** 1141 * Intent app_data bundle key: Use this key with the bundle from 1142 * {@link android.content.Intent#getBundleExtra 1143 * content.Intent.getBundleExtra(APP_DATA)} to obtain the source identifier 1144 * set by the activity that launched the search. 1145 * 1146 * @hide 1147 */ 1148 public final static String SOURCE = "source"; 1149 1150 /** 1151 * Intent extra data key: Use this key with Intent.ACTION_SEARCH and 1152 * {@link android.content.Intent#getIntExtra content.Intent.getIntExtra()} 1153 * to obtain the keycode that the user used to trigger this query. It will be zero if the 1154 * user simply pressed the "GO" button on the search UI. This is primarily used in conjunction 1155 * with the keycode attribute in the actionkey element of your searchable.xml configuration 1156 * file. 1157 */ 1158 public final static String ACTION_KEY = "action_key"; 1159 1160 /** 1161 * Intent extra data key: This key will be used for the extra populated by the 1162 * {@link #SUGGEST_COLUMN_INTENT_EXTRA_DATA} column. 1163 * {@hide} 1164 */ 1165 public final static String EXTRA_DATA_KEY = "intent_extra_data_key"; 1166 1167 /** 1168 * Intent extra data key: Use this key with Intent.ACTION_SEARCH and 1169 * {@link android.content.Intent#getStringExtra content.Intent.getStringExtra()} 1170 * to obtain the action message that was defined for a particular search action key and/or 1171 * suggestion. It will be null if the search was launched by typing "enter", touched the the 1172 * "GO" button, or other means not involving any action key. 1173 */ 1174 public final static String ACTION_MSG = "action_msg"; 1175 1176 /** 1177 * Uri path for queried suggestions data. This is the path that the search manager 1178 * will use when querying your content provider for suggestions data based on user input 1179 * (e.g. looking for partial matches). 1180 * Typically you'll use this with a URI matcher. 1181 */ 1182 public final static String SUGGEST_URI_PATH_QUERY = "search_suggest_query"; 1183 1184 /** 1185 * MIME type for suggestions data. You'll use this in your suggestions content provider 1186 * in the getType() function. 1187 */ 1188 public final static String SUGGEST_MIME_TYPE = 1189 "vnd.android.cursor.dir/vnd.android.search.suggest"; 1190 1191 /** 1192 * Column name for suggestions cursor. <i>Unused - can be null or column can be omitted.</i> 1193 */ 1194 public final static String SUGGEST_COLUMN_FORMAT = "suggest_format"; 1195 /** 1196 * Column name for suggestions cursor. <i>Required.</i> This is the primary line of text that 1197 * will be presented to the user as the suggestion. 1198 */ 1199 public final static String SUGGEST_COLUMN_TEXT_1 = "suggest_text_1"; 1200 /** 1201 * Column name for suggestions cursor. <i>Optional.</i> If your cursor includes this column, 1202 * then all suggestions will be provided in a two-line format. The second line of text is in 1203 * a much smaller appearance. 1204 */ 1205 public final static String SUGGEST_COLUMN_TEXT_2 = "suggest_text_2"; 1206 /** 1207 * Column name for suggestions cursor. <i>Optional.</i> If your cursor includes this column, 1208 * then all suggestions will be provided in a format that includes space for two small icons, 1209 * one at the left and one at the right of each suggestion. The data in the column must 1210 * be a resource ID of a drawable, or a URI in one of the following formats: 1211 * 1212 * <ul> 1213 * <li>content ({@link android.content.ContentResolver#SCHEME_CONTENT})</li> 1214 * <li>android.resource ({@link android.content.ContentResolver#SCHEME_ANDROID_RESOURCE})</li> 1215 * <li>file ({@link android.content.ContentResolver#SCHEME_FILE})</li> 1216 * </ul> 1217 * 1218 * See {@link android.content.ContentResolver#openAssetFileDescriptor(Uri, String)} 1219 * for more information on these schemes. 1220 */ 1221 public final static String SUGGEST_COLUMN_ICON_1 = "suggest_icon_1"; 1222 /** 1223 * Column name for suggestions cursor. <i>Optional.</i> If your cursor includes this column, 1224 * then all suggestions will be provided in a format that includes space for two small icons, 1225 * one at the left and one at the right of each suggestion. The data in the column must 1226 * be a resource ID of a drawable, or a URI in one of the following formats: 1227 * 1228 * <ul> 1229 * <li>content ({@link android.content.ContentResolver#SCHEME_CONTENT})</li> 1230 * <li>android.resource ({@link android.content.ContentResolver#SCHEME_ANDROID_RESOURCE})</li> 1231 * <li>file ({@link android.content.ContentResolver#SCHEME_FILE})</li> 1232 * </ul> 1233 * 1234 * See {@link android.content.ContentResolver#openAssetFileDescriptor(Uri, String)} 1235 * for more information on these schemes. 1236 */ 1237 public final static String SUGGEST_COLUMN_ICON_2 = "suggest_icon_2"; 1238 /** 1239 * Column name for suggestions cursor. <i>Optional.</i> If your cursor includes this column, 1240 * then all suggestions will be provided in a format that includes space for two small icons, 1241 * one at the left and one at the right of each suggestion. The data in the column must 1242 * be a blob that contains a bitmap. 1243 * 1244 * This column overrides any icon provided in the {@link #SUGGEST_COLUMN_ICON_1} column. 1245 * 1246 * @hide 1247 */ 1248 public final static String SUGGEST_COLUMN_ICON_1_BITMAP = "suggest_icon_1_bitmap"; 1249 /** 1250 * Column name for suggestions cursor. <i>Optional.</i> If your cursor includes this column, 1251 * then all suggestions will be provided in a format that includes space for two small icons, 1252 * one at the left and one at the right of each suggestion. The data in the column must 1253 * be a blob that contains a bitmap. 1254 * 1255 * This column overrides any icon provided in the {@link #SUGGEST_COLUMN_ICON_2} column. 1256 * 1257 * @hide 1258 */ 1259 public final static String SUGGEST_COLUMN_ICON_2_BITMAP = "suggest_icon_2_bitmap"; 1260 /** 1261 * Column name for suggestions cursor. <i>Optional.</i> If this column exists <i>and</i> 1262 * this element exists at the given row, this is the action that will be used when 1263 * forming the suggestion's intent. If the element is not provided, the action will be taken 1264 * from the android:searchSuggestIntentAction field in your XML metadata. <i>At least one of 1265 * these must be present for the suggestion to generate an intent.</i> Note: If your action is 1266 * the same for all suggestions, it is more efficient to specify it using XML metadata and omit 1267 * it from the cursor. 1268 */ 1269 public final static String SUGGEST_COLUMN_INTENT_ACTION = "suggest_intent_action"; 1270 /** 1271 * Column name for suggestions cursor. <i>Optional.</i> If this column exists <i>and</i> 1272 * this element exists at the given row, this is the data that will be used when 1273 * forming the suggestion's intent. If the element is not provided, the data will be taken 1274 * from the android:searchSuggestIntentData field in your XML metadata. If neither source 1275 * is provided, the Intent's data field will be null. Note: If your data is 1276 * the same for all suggestions, or can be described using a constant part and a specific ID, 1277 * it is more efficient to specify it using XML metadata and omit it from the cursor. 1278 */ 1279 public final static String SUGGEST_COLUMN_INTENT_DATA = "suggest_intent_data"; 1280 /** 1281 * Column name for suggestions cursor. <i>Optional.</i> This column allows suggestions 1282 * to provide additional arbitrary data which will be included as an extra under the key 1283 * {@link #EXTRA_DATA_KEY}. 1284 * 1285 * @hide pending API council approval 1286 */ 1287 public final static String SUGGEST_COLUMN_INTENT_EXTRA_DATA = "suggest_intent_extra_data"; 1288 /** 1289 * Column name for suggestions cursor. <i>Optional.</i> If this column exists <i>and</i> 1290 * this element exists at the given row, then "/" and this value will be appended to the data 1291 * field in the Intent. This should only be used if the data field has already been set to an 1292 * appropriate base string. 1293 */ 1294 public final static String SUGGEST_COLUMN_INTENT_DATA_ID = "suggest_intent_data_id"; 1295 /** 1296 * Column name for suggestions cursor. <i>Required if action is 1297 * {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH}, optional otherwise.</i> If this 1298 * column exists <i>and</i> this element exists at the given row, this is the data that will be 1299 * used when forming the suggestion's query. 1300 */ 1301 public final static String SUGGEST_COLUMN_QUERY = "suggest_intent_query"; 1302 1303 /** 1304 * If a suggestion has this value in {@link #SUGGEST_COLUMN_INTENT_ACTION}, 1305 * the search dialog will switch to a different suggestion source when the 1306 * suggestion is clicked. 1307 * 1308 * {@link #SUGGEST_COLUMN_INTENT_DATA} must contain 1309 * the flattened {@link ComponentName} of the activity which is to be searched. 1310 * 1311 * TODO: Should {@link #SUGGEST_COLUMN_INTENT_DATA} instead contain a URI in the format 1312 * used by {@link android.provider.Applications}? 1313 * 1314 * TODO: This intent should be protected by the same permission that we use 1315 * for replacing the global search provider. 1316 * 1317 * The query text field will be set to the value of {@link #SUGGEST_COLUMN_QUERY}. 1318 * 1319 * @hide Pending API council approval. 1320 */ 1321 public final static String INTENT_ACTION_CHANGE_SEARCH_SOURCE 1322 = "android.search.action.CHANGE_SEARCH_SOURCE"; 1323 1324 /** 1325 * If a suggestion has this value in {@link #SUGGEST_COLUMN_INTENT_ACTION}, 1326 * the search dialog will call {@link Cursor#respond(Bundle)} when the 1327 * suggestion is clicked. 1328 * 1329 * The {@link Bundle} argument will be constructed 1330 * in the same way as the "extra" bundle included in an Intent constructed 1331 * from the suggestion. 1332 * 1333 * @hide Pending API council approval. 1334 */ 1335 public final static String INTENT_ACTION_CURSOR_RESPOND 1336 = "android.search.action.CURSOR_RESPOND"; 1337 1338 /** 1339 * Intent action for starting the global search settings activity. 1340 * The global search provider should handle this intent. 1341 * 1342 * @hide Pending API council approval. 1343 */ 1344 public final static String INTENT_ACTION_SEARCH_SETTINGS 1345 = "android.search.action.SEARCH_SETTINGS"; 1346 1347 /** 1348 * Reference to the shared system search service. 1349 */ 1350 private static ISearchManager sService = getSearchManagerService(); 1351 1352 private final Context mContext; 1353 private final Handler mHandler; 1354 1355 private SearchDialog mSearchDialog; 1356 1357 private OnDismissListener mDismissListener = null; 1358 private OnCancelListener mCancelListener = null; 1359 1360 /*package*/ SearchManager(Context context, Handler handler) { 1361 mContext = context; 1362 mHandler = handler; 1363 } 1364 1365 /** 1366 * Launch search UI. 1367 * 1368 * <p>The search manager will open a search widget in an overlapping 1369 * window, and the underlying activity may be obscured. The search 1370 * entry state will remain in effect until one of the following events: 1371 * <ul> 1372 * <li>The user completes the search. In most cases this will launch 1373 * a search intent.</li> 1374 * <li>The user uses the back, home, or other keys to exit the search.</li> 1375 * <li>The application calls the {@link #stopSearch} 1376 * method, which will hide the search window and return focus to the 1377 * activity from which it was launched.</li> 1378 * 1379 * <p>Most applications will <i>not</i> use this interface to invoke search. 1380 * The primary method for invoking search is to call 1381 * {@link android.app.Activity#onSearchRequested Activity.onSearchRequested()} or 1382 * {@link android.app.Activity#startSearch Activity.startSearch()}. 1383 * 1384 * @param initialQuery A search string can be pre-entered here, but this 1385 * is typically null or empty. 1386 * @param selectInitialQuery If true, the intial query will be preselected, which means that 1387 * any further typing will replace it. This is useful for cases where an entire pre-formed 1388 * query is being inserted. If false, the selection point will be placed at the end of the 1389 * inserted query. This is useful when the inserted query is text that the user entered, 1390 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful 1391 * if initialQuery is a non-empty string.</i> 1392 * @param launchActivity The ComponentName of the activity that has launched this search. 1393 * @param appSearchData An application can insert application-specific 1394 * context here, in order to improve quality or specificity of its own 1395 * searches. This data will be returned with SEARCH intent(s). Null if 1396 * no extra data is required. 1397 * @param globalSearch If false, this will only launch the search that has been specifically 1398 * defined by the application (which is usually defined as a local search). If no default 1399 * search is defined in the current application or activity, no search will be launched. 1400 * If true, this will always launch a platform-global (e.g. web-based) search instead. 1401 * 1402 * @see android.app.Activity#onSearchRequested 1403 * @see #stopSearch 1404 */ 1405 public void startSearch(String initialQuery, 1406 boolean selectInitialQuery, 1407 ComponentName launchActivity, 1408 Bundle appSearchData, 1409 boolean globalSearch) { 1410 1411 if (mSearchDialog == null) { 1412 mSearchDialog = new SearchDialog(mContext); 1413 } 1414 1415 // activate the search manager and start it up! 1416 mSearchDialog.show(initialQuery, selectInitialQuery, launchActivity, appSearchData, 1417 globalSearch); 1418 1419 mSearchDialog.setOnCancelListener(this); 1420 mSearchDialog.setOnDismissListener(this); 1421 } 1422 1423 /** 1424 * Terminate search UI. 1425 * 1426 * <p>Typically the user will terminate the search UI by launching a 1427 * search or by canceling. This function allows the underlying application 1428 * or activity to cancel the search prematurely (for any reason). 1429 * 1430 * <p>This function can be safely called at any time (even if no search is active.) 1431 * 1432 * @see #startSearch 1433 */ 1434 public void stopSearch() { 1435 if (mSearchDialog != null) { 1436 mSearchDialog.cancel(); 1437 } 1438 } 1439 1440 /** 1441 * Determine if the Search UI is currently displayed. 1442 * 1443 * This is provided primarily for application test purposes. 1444 * 1445 * @return Returns true if the search UI is currently displayed. 1446 * 1447 * @hide 1448 */ 1449 public boolean isVisible() { 1450 if (mSearchDialog != null) { 1451 return mSearchDialog.isShowing(); 1452 } 1453 return false; 1454 } 1455 1456 /** 1457 * See {@link #setOnDismissListener} for configuring your activity to monitor search UI state. 1458 */ 1459 public interface OnDismissListener { 1460 /** 1461 * This method will be called when the search UI is dismissed. To make use if it, you must 1462 * implement this method in your activity, and call {@link #setOnDismissListener} to 1463 * register it. 1464 */ 1465 public void onDismiss(); 1466 } 1467 1468 /** 1469 * See {@link #setOnCancelListener} for configuring your activity to monitor search UI state. 1470 */ 1471 public interface OnCancelListener { 1472 /** 1473 * This method will be called when the search UI is canceled. To make use if it, you must 1474 * implement this method in your activity, and call {@link #setOnCancelListener} to 1475 * register it. 1476 */ 1477 public void onCancel(); 1478 } 1479 1480 /** 1481 * Set or clear the callback that will be invoked whenever the search UI is dismissed. 1482 * 1483 * @param listener The {@link OnDismissListener} to use, or null. 1484 */ 1485 public void setOnDismissListener(final OnDismissListener listener) { 1486 mDismissListener = listener; 1487 } 1488 1489 /** 1490 * The callback from the search dialog when dismissed 1491 * @hide 1492 */ 1493 public void onDismiss(DialogInterface dialog) { 1494 if (dialog == mSearchDialog) { 1495 if (mDismissListener != null) { 1496 mDismissListener.onDismiss(); 1497 } 1498 } 1499 } 1500 1501 /** 1502 * Set or clear the callback that will be invoked whenever the search UI is canceled. 1503 * 1504 * @param listener The {@link OnCancelListener} to use, or null. 1505 */ 1506 public void setOnCancelListener(final OnCancelListener listener) { 1507 mCancelListener = listener; 1508 } 1509 1510 1511 /** 1512 * The callback from the search dialog when canceled 1513 * @hide 1514 */ 1515 public void onCancel(DialogInterface dialog) { 1516 if (dialog == mSearchDialog) { 1517 if (mCancelListener != null) { 1518 mCancelListener.onCancel(); 1519 } 1520 } 1521 } 1522 1523 /** 1524 * Save instance state so we can recreate after a rotation. 1525 * 1526 * @hide 1527 */ 1528 void saveSearchDialog(Bundle outState, String key) { 1529 if (mSearchDialog != null && mSearchDialog.isShowing()) { 1530 Bundle searchDialogState = mSearchDialog.onSaveInstanceState(); 1531 outState.putBundle(key, searchDialogState); 1532 } 1533 } 1534 1535 /** 1536 * Restore instance state after a rotation. 1537 * 1538 * @hide 1539 */ 1540 void restoreSearchDialog(Bundle inState, String key) { 1541 Bundle searchDialogState = inState.getBundle(key); 1542 if (searchDialogState != null) { 1543 if (mSearchDialog == null) { 1544 mSearchDialog = new SearchDialog(mContext); 1545 } 1546 mSearchDialog.onRestoreInstanceState(searchDialogState); 1547 } 1548 } 1549 1550 /** 1551 * Hook for updating layout on a rotation 1552 * 1553 * @hide 1554 */ 1555 void onConfigurationChanged(Configuration newConfig) { 1556 if (mSearchDialog != null && mSearchDialog.isShowing()) { 1557 mSearchDialog.onConfigurationChanged(newConfig); 1558 } 1559 } 1560 1561 private static ISearchManager getSearchManagerService() { 1562 return ISearchManager.Stub.asInterface( 1563 ServiceManager.getService(Context.SEARCH_SERVICE)); 1564 } 1565 1566 /** 1567 * Gets information about a searchable activity. This method is static so that it can 1568 * be used from non-Activity contexts. 1569 * 1570 * @param componentName The activity to get searchable information for. 1571 * @param globalSearch If <code>false</code>, return information about the given activity. 1572 * If <code>true</code>, return information about the global search activity. 1573 * @return Searchable information, or <code>null</code> if the activity is not searchable. 1574 * 1575 * @hide because SearchableInfo is not part of the API. 1576 */ 1577 public static SearchableInfo getSearchableInfo(ComponentName componentName, 1578 boolean globalSearch) { 1579 try { 1580 return sService.getSearchableInfo(componentName, globalSearch); 1581 } catch (RemoteException e) { 1582 return null; 1583 } 1584 } 1585 1586 /** 1587 * Checks whether the given searchable is the default searchable. 1588 * 1589 * @hide because SearchableInfo is not part of the API. 1590 */ 1591 public static boolean isDefaultSearchable(SearchableInfo searchable) { 1592 SearchableInfo defaultSearchable = SearchManager.getSearchableInfo(null, true); 1593 return defaultSearchable != null 1594 && defaultSearchable.mSearchActivity.equals(searchable.mSearchActivity); 1595 } 1596 1597 /** 1598 * Gets a cursor with search suggestions. This method is static so that it can 1599 * be used from non-Activity context. 1600 * 1601 * @param searchable Information about how to get the suggestions. 1602 * @param query The search text entered (so far). 1603 * @return a cursor with suggestions, or <code>null</null> the suggestion query failed. 1604 * 1605 * @hide because SearchableInfo is not part of the API. 1606 */ 1607 public static Cursor getSuggestions(Context context, SearchableInfo searchable, String query) { 1608 if (searchable == null) { 1609 return null; 1610 } 1611 1612 String authority = searchable.getSuggestAuthority(); 1613 if (authority == null) { 1614 return null; 1615 } 1616 1617 Uri.Builder uriBuilder = new Uri.Builder() 1618 .scheme(ContentResolver.SCHEME_CONTENT) 1619 .authority(authority); 1620 1621 // if content path provided, insert it now 1622 final String contentPath = searchable.getSuggestPath(); 1623 if (contentPath != null) { 1624 uriBuilder.appendEncodedPath(contentPath); 1625 } 1626 1627 // append standard suggestion query path 1628 uriBuilder.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY); 1629 1630 // get the query selection, may be null 1631 String selection = searchable.getSuggestSelection(); 1632 // inject query, either as selection args or inline 1633 String[] selArgs = null; 1634 if (selection != null) { // use selection if provided 1635 selArgs = new String[] { query }; 1636 } else { // no selection, use REST pattern 1637 uriBuilder.appendPath(query); 1638 } 1639 1640 Uri uri = uriBuilder 1641 .query("") // TODO: Remove, workaround for a bug in Uri.writeToParcel() 1642 .fragment("") // TODO: Remove, workaround for a bug in Uri.writeToParcel() 1643 .build(); 1644 1645 // finally, make the query 1646 return context.getContentResolver().query(uri, null, selection, selArgs, null); 1647 } 1648 1649} 1650