SearchManager.java revision d24b8183b93e781080b2c16c487e60d51c12da31
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 * &#64;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 * &#64;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 &lt;actionkey&gt;
588 * element.  Within this element you must define two or three attributes.  The first attribute,
589 * &lt;android:keycode&gt;, 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 * &lt;android:queryActionMsg&gt; provides the message that will be sent if the action key is
595 * pressed while the user is simply entering query text.  &lt;android:suggestActionMsgColumn&gt;
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 *        &lt;!-- Search Activity - searchable --&gt;
650 *        &lt;activity android:name="MySearchActivity"
651 *                  android:label="Search"
652 *                  android:launchMode="singleTop"&gt;
653 *            &lt;intent-filter&gt;
654 *                &lt;action android:name="android.intent.action.SEARCH" /&gt;
655 *                &lt;category android:name="android.intent.category.DEFAULT" /&gt;
656 *            &lt;/intent-filter&gt;
657 *            &lt;meta-data android:name="android.app.searchable"
658 *                       android:resource="@xml/searchable" /&gt;
659 *        &lt;/activity&gt;</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 * &lt;searchable xmlns:android="http://schemas.android.com/apk/res/android"
669 *     android:label="@string/search_label"
670 *     android:hint="@string/search_hint" &gt;
671 * &lt;/searchable&gt;</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 *
751 *     </tbody>
752 * </table>
753 *
754 * <p><b>Styleable Resources in your Metadata.</b>  It's possible to provide alternate strings
755 * for your searchable application, in order to provide localization and/or to better visual
756 * presentation on different device configurations.  Each searchable activity has a single XML
757 * metadata file, but any resource references can be replaced at runtime based on device
758 * configuration, language setting, and other system inputs.
759 *
760 * <p>A concrete example is the "hint" text you supply using the android:searchHint attribute.
761 * In portrait mode you'll have less screen space and may need to provide a shorter string, but
762 * in landscape mode you can provide a longer, more descriptive hint.  To do this, you'll need to
763 * define two or more strings.xml files, in the following directories:
764 * <ul><li>.../res/values-land/strings.xml</li>
765 * <li>.../res/values-port/strings.xml</li>
766 * <li>.../res/values/strings.xml</li></ul>
767 *
768 * <p>For more complete documentation on this capability, see
769 * <a href="{@docRoot}guide/topics/resources/resources-i18n.html#AlternateResources">Resources and
770 * Internationalization: Alternate Resources</a>.
771 *
772 * <p><b>Metadata for non-searchable activities.</b>  Activities which are part of a searchable
773 * application, but don't implement search itself, require a bit of "glue" in order to cause
774 * them to invoke search using your searchable activity as their primary context.  If this is not
775 * provided, then searches from these activities will use the system default search context.
776 *
777 * <p>The simplest way to specify this is to add a <i>search reference</i> element to the
778 * application entry in the <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest</a> file.
779 * The value of this reference can be either of:
780 * <ul><li>The name of your searchable activity.
781 * It is typically prefixed by '.' to indicate that it's in the same package.</li>
782 * <li>A "*" indicates that the system may select a default searchable activity, in which
783 * case it will typically select web-based search.</li>
784 * </ul>
785 *
786 * <p>Here is a snippet showing the necessary addition to the manifest entry for your
787 * non-searchable activities.
788 * <pre class="prettyprint">
789 *        &lt;application&gt;
790 *            &lt;meta-data android:name="android.app.default_searchable"
791 *                       android:value=".MySearchActivity" /&gt;
792 *
793 *            &lt;!-- followed by activities, providers, etc... --&gt;
794 *        &lt;/application&gt;</pre>
795 *
796 * <p>You can also specify android.app.default_searchable on a per-activity basis, by including
797 * the meta-data element (as shown above) in one or more activity sections.  If found, these will
798 * override the reference in the application section.  The only reason to configure your application
799 * this way would be if you wish to partition it into separate sections with different search
800 * behaviors;  Otherwise this configuration is not recommended.
801 *
802 * <p><b>Additional Metadata for search suggestions.</b>  If you have defined a content provider
803 * to generate search suggestions, you'll need to publish it to the system, and you'll need to
804 * provide a bit of additional XML metadata in order to configure communications with it.
805 *
806 * <p>First, in your <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest</a>, you'll add the
807 * following lines.
808 * <pre class="prettyprint">
809 *        &lt;!-- Content provider for search suggestions --&gt;
810 *        &lt;provider android:name="YourSuggestionProviderClass"
811 *                android:authorities="your.suggestion.authority" /&gt;</pre>
812 *
813 * <p>Next, you'll add a few lines to your XML metadata file, as shown:
814 * <pre class="prettyprint">
815 *     &lt;!-- Required attribute for any suggestions provider --&gt;
816 *     android:searchSuggestAuthority="your.suggestion.authority"
817 *
818 *     &lt;!-- Optional attribute for configuring queries --&gt;
819 *     android:searchSuggestSelection="field =?"
820 *
821 *     &lt;!-- Optional attributes for configuring intent construction --&gt;
822 *     android:searchSuggestIntentAction="intent action string"
823 *     android:searchSuggestIntentData="intent data Uri" /&gt;</pre>
824 *
825 * <p>Elements of search metadata that support suggestions:
826 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
827 *
828 *     <thead>
829 *     <tr><th>Attribute</th> <th>Description</th> <th>Required?</th></tr>
830 *     </thead>
831 *
832 *     <tbody>
833 *     <tr><th>android:searchSuggestAuthority</th>
834 *         <td>This value must match the authority string provided in the <i>provider</i> section
835 *             of your <a href="{@docRoot}guide/topics/manifest/manifest-intro.html">manifest</a>.</td>
836 *         <td align="center">Yes</td>
837 *     </tr>
838 *
839 *     <tr><th>android:searchSuggestPath</th>
840 *         <td>If provided, this will be inserted in the suggestions query Uri, after the authority
841 *             you have provide but before the standard suggestions path.  This is only required if
842 *             you have a single content provider issuing different types of suggestions (e.g. for
843 *             different data types) and you need a way to disambiguate the suggestions queries
844 *             when they are received.</td>
845 *         <td align="center">No</td>
846 *     </tr>
847 *
848 *     <tr><th>android:searchSuggestSelection</th>
849 *         <td>If provided, this value will be passed into your query function as the
850 *             <i>selection</i> parameter.  Typically this will be a WHERE clause for your database,
851 *             and will contain a single question mark, which represents the actual query string
852 *             that has been typed by the user.  However, you can also use any non-null value
853 *             to simply trigger the delivery of the query text (via selection arguments), and then
854 *             use the query text in any way appropriate for your provider (ignoring the actual
855 *             text of the selection parameter.)</td>
856 *         <td align="center">No</td>
857 *     </tr>
858 *
859 *     <tr><th>android:searchSuggestIntentAction</th>
860 *         <td>If provided, and not overridden by the selected suggestion, this value will be
861 *             placed in the action field of the {@link android.content.Intent Intent} when the
862 *             user clicks a suggestion.</td>
863 *         <td align="center">No</td>
864 *
865 *     <tr><th>android:searchSuggestIntentData</th>
866 *         <td>If provided, and not overridden by the selected suggestion, this value will be
867 *             placed in the data field of the {@link android.content.Intent Intent} when the user
868 *             clicks a suggestion.</td>
869 *         <td align="center">No</td>
870 *     </tr>
871 *
872 *     </tbody>
873 * </table>
874 *
875 * <p><b>Additional Metadata for search action keys.</b>  For each action key that you would like to
876 * define, you'll need to add an additional element defining that key, and using the attributes
877 * discussed in <a href="#ActionKeys">Action Keys</a>.  A simple example is shown here:
878 *
879 * <pre class="prettyprint">&lt;actionkey
880 *     android:keycode="KEYCODE_CALL"
881 *     android:queryActionMsg="call"
882 *     android:suggestActionMsg="call"
883 *     android:suggestActionMsgColumn="call_column" /&gt;</pre>
884 *
885 * <p>Elements of search metadata that support search action keys.  Note that although each of the
886 * action message elements are marked as <i>optional</i>, at least one must be present for the
887 * action key to have any effect.
888 *
889 * <table border="2" width="85%" align="center" frame="hsides" rules="rows">
890 *
891 *     <thead>
892 *     <tr><th>Attribute</th> <th>Description</th> <th>Required?</th></tr>
893 *     </thead>
894 *
895 *     <tbody>
896 *     <tr><th>android:keycode</th>
897 *         <td>This attribute denotes the action key you wish to respond to.  Note that not
898 *             all action keys are actually supported using this mechanism, as many of them are
899 *             used for typing, navigation, or system functions.  This will be added to the
900 *             {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} intent that is passed to
901 *             your searchable activity.  To examine the key code, use
902 *             {@link android.content.Intent#getIntExtra getIntExtra(SearchManager.ACTION_KEY)}.
903 *             <p>Note, in addition to the keycode, you must also provide one or more of the action
904 *             specifier attributes.</td>
905 *         <td align="center">Yes</td>
906 *     </tr>
907 *
908 *     <tr><th>android:queryActionMsg</th>
909 *         <td>If you wish to handle an action key during normal search query entry, you
910 *          must define an action string here.  This will be added to the
911 *          {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} intent that is passed to your
912 *          searchable activity.  To examine the string, use
913 *          {@link android.content.Intent#getStringExtra
914 *          getStringExtra(SearchManager.ACTION_MSG)}.</td>
915 *         <td align="center">No</td>
916 *     </tr>
917 *
918 *     <tr><th>android:suggestActionMsg</th>
919 *         <td>If you wish to handle an action key while a suggestion is being displayed <i>and
920 *             selected</i>, there are two ways to handle this.  If <i>all</i> of your suggestions
921 *             can handle the action key, you can simply define the action message using this
922 *             attribute.  This will be added to the
923 *             {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} intent that is passed to
924 *             your searchable activity.  To examine the string, use
925 *             {@link android.content.Intent#getStringExtra
926 *             getStringExtra(SearchManager.ACTION_MSG)}.</td>
927 *         <td align="center">No</td>
928 *     </tr>
929 *
930 *     <tr><th>android:suggestActionMsgColumn</th>
931 *         <td>If you wish to handle an action key while a suggestion is being displayed <i>and
932 *             selected</i>, but you do not wish to enable this action key for every suggestion,
933 *             then you can use this attribute to control it on a suggestion-by-suggestion basis.
934 *             First, you must define a column (and name it here) where your suggestions will
935 *             include the action string.  Then, in your content provider, you must provide this
936 *             column, and when desired, provide data in this column.
937 *             The search manager will look at your suggestion cursor, using the string
938 *             provided here in order to select a column, and will use that to select a string from
939 *             the cursor.  That string will be added to the
940 *             {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH} intent that is passed to
941 *             your searchable activity.  To examine the string, use
942 *             {@link android.content.Intent#getStringExtra
943 *             getStringExtra(SearchManager.ACTION_MSG)}.  <i>If the data does not exist for the
944 *             selection suggestion, the action key will be ignored.</i></td>
945 *         <td align="center">No</td>
946 *     </tr>
947 *
948 *     </tbody>
949 * </table>
950 *
951 * <a name="PassingSearchContext"></a>
952 * <h3>Passing Search Context</h3>
953 *
954 * <p>In order to improve search experience, an application may wish to specify
955 * additional data along with the search, such as local history or context.  For
956 * example, a maps search would be improved by including the current location.
957 * In order to simplify the structure of your activities, this can be done using
958 * the search manager.
959 *
960 * <p>Any data can be provided at the time the search is launched, as long as it
961 * can be stored in a {@link android.os.Bundle Bundle} object.
962 *
963 * <p>To pass application data into the Search Manager, you'll need to override
964 * {@link android.app.Activity#onSearchRequested onSearchRequested} as follows:
965 *
966 * <pre class="prettyprint">
967 * &#64;Override
968 * public boolean onSearchRequested() {
969 *     Bundle appData = new Bundle();
970 *     appData.put...();
971 *     appData.put...();
972 *     startSearch(null, false, appData);
973 *     return true;
974 * }</pre>
975 *
976 * <p>To receive application data from the Search Manager, you'll extract it from
977 * the {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH}
978 * {@link android.content.Intent Intent} as follows:
979 *
980 * <pre class="prettyprint">
981 * final Bundle appData = queryIntent.getBundleExtra(SearchManager.APP_DATA);
982 * if (appData != null) {
983 *     appData.get...();
984 *     appData.get...();
985 * }</pre>
986 *
987 * <a name="ProtectingUserPrivacy"></a>
988 * <h3>Protecting User Privacy</h3>
989 *
990 * <p>Many users consider their activities on the phone, including searches, to be private
991 * information.  Applications that implement search should take steps to protect users' privacy
992 * wherever possible.  This section covers two areas of concern, but you should consider your search
993 * design carefully and take any additional steps necessary.
994 *
995 * <p><b>Don't send personal information to servers, and if you do, don't log it.</b>
996 * "Personal information" is information that can personally identify your users, such as name,
997 * email address or billing information, or other data which can be reasonably linked to such
998 * information.  If your application implements search with the assistance of a server, try to
999 * avoid sending personal information with your searches.  For example, if you are searching for
1000 * businesses near a zip code, you don't need to send the user ID as well - just send the zip code
1001 * to the server.  If you do need to send personal information, you should take steps to avoid
1002 * logging it.  If you must log it, you should protect that data very carefully, and erase it as
1003 * soon as possible.
1004 *
1005 * <p><b>Provide the user with a way to clear their search history.</b>  The Search Manager helps
1006 * your application provide context-specific suggestions.  Sometimes these suggestions are based
1007 * on previous searches, or other actions taken by the user in an earlier session.  A user may not
1008 * wish for previous searches to be revealed to other users, for instance if they share their phone
1009 * with a friend.  If your application provides suggestions that can reveal previous activities,
1010 * you should implement a "Clear History" menu, preference, or button.  If you are using
1011 * {@link android.provider.SearchRecentSuggestions}, you can simply call its
1012 * {@link android.provider.SearchRecentSuggestions#clearHistory() clearHistory()} method from
1013 * your "Clear History" UI.  If you are implementing your own form of recent suggestions, you'll
1014 * need to provide a similar a "clear history" API in your provider, and call it from your
1015 * "Clear History" UI.
1016 */
1017public class SearchManager
1018        implements DialogInterface.OnDismissListener, DialogInterface.OnCancelListener
1019{
1020    /**
1021     * This is a shortcut definition for the default menu key to use for invoking search.
1022     *
1023     * See Menu.Item.setAlphabeticShortcut() for more information.
1024     */
1025    public final static char MENU_KEY = 's';
1026
1027    /**
1028     * This is a shortcut definition for the default menu key to use for invoking search.
1029     *
1030     * See Menu.Item.setAlphabeticShortcut() for more information.
1031     */
1032    public final static int MENU_KEYCODE = KeyEvent.KEYCODE_S;
1033
1034    /**
1035     * Intent extra data key: Use this key with
1036     * {@link android.content.Intent#getStringExtra
1037     *  content.Intent.getStringExtra()}
1038     * to obtain the query string from Intent.ACTION_SEARCH.
1039     */
1040    public final static String QUERY = "query";
1041
1042    /**
1043     * Intent extra data key: Use this key with Intent.ACTION_SEARCH and
1044     * {@link android.content.Intent#getBundleExtra
1045     *  content.Intent.getBundleExtra()}
1046     * to obtain any additional app-specific data that was inserted by the
1047     * activity that launched the search.
1048     */
1049    public final static String APP_DATA = "app_data";
1050
1051    /**
1052     * Intent app_data bundle key: Use this key with the bundle from
1053     * {@link android.content.Intent#getBundleExtra
1054     * content.Intent.getBundleExtra(APP_DATA)} to obtain the source identifier
1055     * set by the activity that launched the search.
1056     *
1057     * @hide
1058     */
1059    public final static String SOURCE = "source";
1060
1061    /**
1062     * Intent extra data key: Use this key with Intent.ACTION_SEARCH and
1063     * {@link android.content.Intent#getIntExtra content.Intent.getIntExtra()}
1064     * to obtain the keycode that the user used to trigger this query.  It will be zero if the
1065     * user simply pressed the "GO" button on the search UI.  This is primarily used in conjunction
1066     * with the keycode attribute in the actionkey element of your searchable.xml configuration
1067     * file.
1068     */
1069    public final static String ACTION_KEY = "action_key";
1070
1071    /**
1072     * Intent extra data key: Use this key with Intent.ACTION_SEARCH and
1073     * {@link android.content.Intent#getStringExtra content.Intent.getStringExtra()}
1074     * to obtain the action message that was defined for a particular search action key and/or
1075     * suggestion.  It will be null if the search was launched by typing "enter", touched the the
1076     * "GO" button, or other means not involving any action key.
1077     */
1078    public final static String ACTION_MSG = "action_msg";
1079
1080    /**
1081     * Uri path for queried suggestions data.  This is the path that the search manager
1082     * will use when querying your content provider for suggestions data based on user input
1083     * (e.g. looking for partial matches).
1084     * Typically you'll use this with a URI matcher.
1085     */
1086    public final static String SUGGEST_URI_PATH_QUERY = "search_suggest_query";
1087
1088    /**
1089     * MIME type for suggestions data.  You'll use this in your suggestions content provider
1090     * in the getType() function.
1091     */
1092    public final static String SUGGEST_MIME_TYPE =
1093                                  "vnd.android.cursor.dir/vnd.android.search.suggest";
1094
1095    /**
1096     * Column name for suggestions cursor.  <i>Unused - can be null or column can be omitted.</i>
1097     */
1098    public final static String SUGGEST_COLUMN_FORMAT = "suggest_format";
1099    /**
1100     * Column name for suggestions cursor.  <i>Required.</i>  This is the primary line of text that
1101     * will be presented to the user as the suggestion.
1102     */
1103    public final static String SUGGEST_COLUMN_TEXT_1 = "suggest_text_1";
1104    /**
1105     * Column name for suggestions cursor.  <i>Optional.</i>  If your cursor includes this column,
1106     *  then all suggestions will be provided in a two-line format.  The second line of text is in
1107     *  a much smaller appearance.
1108     */
1109    public final static String SUGGEST_COLUMN_TEXT_2 = "suggest_text_2";
1110    /**
1111     * Column name for suggestions cursor.  <i>Optional.</i>  If your cursor includes this column,
1112     *  then all suggestions will be provided in format that includes space for two small icons,
1113     *  one at the left and one at the right of each suggestion.  The data in the column must
1114     *  be a a resource ID for the icon you wish to have displayed.  If you include this column,
1115     *  you must also include {@link #SUGGEST_COLUMN_ICON_2}.
1116     */
1117    public final static String SUGGEST_COLUMN_ICON_1 = "suggest_icon_1";
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_1}.
1124     */
1125    public final static String SUGGEST_COLUMN_ICON_2 = "suggest_icon_2";
1126    /**
1127     * Column name for suggestions cursor.  <i>Optional.</i>  If this column exists <i>and</i>
1128     * this element exists at the given row, this is the action that will be used when
1129     * forming the suggestion's intent.  If the element is not provided, the action will be taken
1130     * from the android:searchSuggestIntentAction field in your XML metadata.  <i>At least one of
1131     * these must be present for the suggestion to generate an intent.</i>  Note:  If your action is
1132     * the same for all suggestions, it is more efficient to specify it using XML metadata and omit
1133     * it from the cursor.
1134     */
1135    public final static String SUGGEST_COLUMN_INTENT_ACTION = "suggest_intent_action";
1136    /**
1137     * Column name for suggestions cursor.  <i>Optional.</i>  If this column exists <i>and</i>
1138     * this element exists at the given row, this is the data that will be used when
1139     * forming the suggestion's intent.  If the element is not provided, the data will be taken
1140     * from the android:searchSuggestIntentData field in your XML metadata.  If neither source
1141     * is provided, the Intent's data field will be null.  Note:  If your data is
1142     * the same for all suggestions, or can be described using a constant part and a specific ID,
1143     * it is more efficient to specify it using XML metadata and omit it from the cursor.
1144     */
1145    public final static String SUGGEST_COLUMN_INTENT_DATA = "suggest_intent_data";
1146    /**
1147     * Column name for suggestions cursor.  <i>Optional.</i>  If this column exists <i>and</i>
1148     * this element exists at the given row, then "/" and this value will be appended to the data
1149     * field in the Intent.  This should only be used if the data field has already been set to an
1150     * appropriate base string.
1151     */
1152    public final static String SUGGEST_COLUMN_INTENT_DATA_ID = "suggest_intent_data_id";
1153    /**
1154     * Column name for suggestions cursor.  <i>Required if action is
1155     * {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH}, optional otherwise.</i>  If this
1156     * column exists <i>and</i> this element exists at the given row, this is the data that will be
1157     * used when forming the suggestion's query.
1158     */
1159    public final static String SUGGEST_COLUMN_QUERY = "suggest_intent_query";
1160
1161
1162    private final Context mContext;
1163    private final Handler mHandler;
1164
1165    private SearchDialog mSearchDialog;
1166
1167    private OnDismissListener mDismissListener = null;
1168    private OnCancelListener mCancelListener = null;
1169
1170    /*package*/ SearchManager(Context context, Handler handler)  {
1171        mContext = context;
1172        mHandler = handler;
1173    }
1174    private static ISearchManager mService;
1175
1176    static {
1177        mService = ISearchManager.Stub.asInterface(
1178                    ServiceManager.getService(Context.SEARCH_SERVICE));
1179    }
1180
1181    /**
1182     * Launch search UI.
1183     *
1184     * <p>The search manager will open a search widget in an overlapping
1185     * window, and the underlying activity may be obscured.  The search
1186     * entry state will remain in effect until one of the following events:
1187     * <ul>
1188     * <li>The user completes the search.  In most cases this will launch
1189     * a search intent.</li>
1190     * <li>The user uses the back, home, or other keys to exit the search.</li>
1191     * <li>The application calls the {@link #stopSearch}
1192     * method, which will hide the search window and return focus to the
1193     * activity from which it was launched.</li>
1194     *
1195     * <p>Most applications will <i>not</i> use this interface to invoke search.
1196     * The primary method for invoking search is to call
1197     * {@link android.app.Activity#onSearchRequested Activity.onSearchRequested()} or
1198     * {@link android.app.Activity#startSearch Activity.startSearch()}.
1199     *
1200     * @param initialQuery A search string can be pre-entered here, but this
1201     * is typically null or empty.
1202     * @param selectInitialQuery If true, the intial query will be preselected, which means that
1203     * any further typing will replace it.  This is useful for cases where an entire pre-formed
1204     * query is being inserted.  If false, the selection point will be placed at the end of the
1205     * inserted query.  This is useful when the inserted query is text that the user entered,
1206     * and the user would expect to be able to keep typing.  <i>This parameter is only meaningful
1207     * if initialQuery is a non-empty string.</i>
1208     * @param launchActivity The ComponentName of the activity that has launched this search.
1209     * @param appSearchData An application can insert application-specific
1210     * context here, in order to improve quality or specificity of its own
1211     * searches.  This data will be returned with SEARCH intent(s).  Null if
1212     * no extra data is required.
1213     * @param globalSearch If false, this will only launch the search that has been specifically
1214     * defined by the application (which is usually defined as a local search).  If no default
1215     * search is defined in the current application or activity, no search will be launched.
1216     * If true, this will always launch a platform-global (e.g. web-based) search instead.
1217     *
1218     * @see android.app.Activity#onSearchRequested
1219     * @see #stopSearch
1220     */
1221    public void startSearch(String initialQuery,
1222                            boolean selectInitialQuery,
1223                            ComponentName launchActivity,
1224                            Bundle appSearchData,
1225                            boolean globalSearch) {
1226
1227        if (mSearchDialog == null) {
1228            mSearchDialog = new SearchDialog(mContext);
1229        }
1230
1231        // activate the search manager and start it up!
1232        mSearchDialog.show(initialQuery, selectInitialQuery, launchActivity, appSearchData,
1233                globalSearch);
1234
1235        mSearchDialog.setOnCancelListener(this);
1236        mSearchDialog.setOnDismissListener(this);
1237    }
1238
1239    /**
1240     * Terminate search UI.
1241     *
1242     * <p>Typically the user will terminate the search UI by launching a
1243     * search or by canceling.  This function allows the underlying application
1244     * or activity to cancel the search prematurely (for any reason).
1245     *
1246     * <p>This function can be safely called at any time (even if no search is active.)
1247     *
1248     * @see #startSearch
1249     */
1250    public void stopSearch()  {
1251        if (mSearchDialog != null) {
1252            mSearchDialog.cancel();
1253        }
1254    }
1255
1256    /**
1257     * Determine if the Search UI is currently displayed.
1258     *
1259     * This is provided primarily for application test purposes.
1260     *
1261     * @return Returns true if the search UI is currently displayed.
1262     *
1263     * @hide
1264     */
1265    public boolean isVisible()  {
1266        if (mSearchDialog != null) {
1267            return mSearchDialog.isShowing();
1268        }
1269        return false;
1270    }
1271
1272    /**
1273     * See {@link #setOnDismissListener} for configuring your activity to monitor search UI state.
1274     */
1275    public interface OnDismissListener {
1276        /**
1277         * This method will be called when the search UI is dismissed. To make use if it, you must
1278         * implement this method in your activity, and call {@link #setOnDismissListener} to
1279         * register it.
1280         */
1281        public void onDismiss();
1282    }
1283
1284    /**
1285     * See {@link #setOnCancelListener} for configuring your activity to monitor search UI state.
1286     */
1287    public interface OnCancelListener {
1288        /**
1289         * This method will be called when the search UI is canceled. To make use if it, you must
1290         * implement this method in your activity, and call {@link #setOnCancelListener} to
1291         * register it.
1292         */
1293        public void onCancel();
1294    }
1295
1296    /**
1297     * Set or clear the callback that will be invoked whenever the search UI is dismissed.
1298     *
1299     * @param listener The {@link OnDismissListener} to use, or null.
1300     */
1301    public void setOnDismissListener(final OnDismissListener listener) {
1302        mDismissListener = listener;
1303    }
1304
1305    /**
1306     * The callback from the search dialog when dismissed
1307     * @hide
1308     */
1309    public void onDismiss(DialogInterface dialog) {
1310        if (dialog == mSearchDialog) {
1311            if (mDismissListener != null) {
1312                mDismissListener.onDismiss();
1313            }
1314        }
1315    }
1316
1317    /**
1318     * Set or clear the callback that will be invoked whenever the search UI is canceled.
1319     *
1320     * @param listener The {@link OnCancelListener} to use, or null.
1321     */
1322    public void setOnCancelListener(final OnCancelListener listener) {
1323        mCancelListener = listener;
1324    }
1325
1326
1327    /**
1328     * The callback from the search dialog when canceled
1329     * @hide
1330     */
1331    public void onCancel(DialogInterface dialog) {
1332        if (dialog == mSearchDialog) {
1333            if (mCancelListener != null) {
1334                mCancelListener.onCancel();
1335            }
1336        }
1337    }
1338
1339    /**
1340     * Save instance state so we can recreate after a rotation.
1341     *
1342     * @hide
1343     */
1344    void saveSearchDialog(Bundle outState, String key) {
1345        if (mSearchDialog != null && mSearchDialog.isShowing()) {
1346            Bundle searchDialogState = mSearchDialog.onSaveInstanceState();
1347            outState.putBundle(key, searchDialogState);
1348        }
1349    }
1350
1351    /**
1352     * Restore instance state after a rotation.
1353     *
1354     * @hide
1355     */
1356    void restoreSearchDialog(Bundle inState, String key) {
1357        Bundle searchDialogState = inState.getBundle(key);
1358        if (searchDialogState != null) {
1359            if (mSearchDialog == null) {
1360                mSearchDialog = new SearchDialog(mContext);
1361            }
1362            mSearchDialog.onRestoreInstanceState(searchDialogState);
1363        }
1364    }
1365
1366    /**
1367     * Hook for updating layout on a rotation
1368     *
1369     * @hide
1370     */
1371    void onConfigurationChanged(Configuration newConfig) {
1372        if (mSearchDialog != null && mSearchDialog.isShowing()) {
1373            mSearchDialog.onConfigurationChanged(newConfig);
1374        }
1375    }
1376
1377}
1378