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