blob: 671501255e28b70499589f88458da6ea262dccd7 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Bjorn Bringert3c50ef62009-12-09 12:23:12 +000019import android.content.ActivityNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.ComponentName;
Karl Rosaen875d50a2009-04-23 19:00:21 -070021import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
23import android.content.DialogInterface;
Bjorn Bringert3c50ef62009-12-09 12:23:12 +000024import android.content.Intent;
Karl Rosaen875d50a2009-04-23 19:00:21 -070025import android.database.Cursor;
26import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.os.Bundle;
28import android.os.Handler;
Karl Rosaen875d50a2009-04-23 19:00:21 -070029import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.ServiceManager;
Bjorn Bringert3fc7c672009-09-28 20:11:52 +010031import android.text.TextUtils;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +010032import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.view.KeyEvent;
34
Bjorn Bringert6d72e022009-04-29 14:56:12 +010035import java.util.List;
36
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037/**
38 * This class provides access to the system search services.
Amith Yamasanie678f462010-09-15 16:13:43 -070039 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 * <p>In practice, you won't interact with this class directly, as search
41 * services are provided through methods in {@link android.app.Activity Activity}
Scott Main8ad64652010-03-17 12:06:06 -070042 * and the {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH}
43 * {@link android.content.Intent Intent}.
44 * If you do require direct access to the SearchManager, do not instantiate
45 * this class directly. Instead, retrieve it through
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 * {@link android.content.Context#getSystemService
47 * context.getSystemService(Context.SEARCH_SERVICE)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 *
Scott Main8ad64652010-03-17 12:06:06 -070049 * <div class="special">
50 * <p>For a guide to using the search dialog and adding search
51 * suggestions in your application, see the Dev Guide topic about <strong><a
52 * href="{@docRoot}guide/topics/search/index.html">Search</a></strong>.</p>
53 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 */
Amith Yamasanie678f462010-09-15 16:13:43 -070055public class SearchManager
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 implements DialogInterface.OnDismissListener, DialogInterface.OnCancelListener
57{
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +010058
59 private static final boolean DBG = false;
60 private static final String TAG = "SearchManager";
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 /**
63 * This is a shortcut definition for the default menu key to use for invoking search.
Amith Yamasanie678f462010-09-15 16:13:43 -070064 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 * See Menu.Item.setAlphabeticShortcut() for more information.
66 */
67 public final static char MENU_KEY = 's';
68
69 /**
70 * This is a shortcut definition for the default menu key to use for invoking search.
Amith Yamasanie678f462010-09-15 16:13:43 -070071 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 * See Menu.Item.setAlphabeticShortcut() for more information.
73 */
74 public final static int MENU_KEYCODE = KeyEvent.KEYCODE_S;
75
76 /**
Amith Yamasanie678f462010-09-15 16:13:43 -070077 * Intent extra data key: Use this key with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 * {@link android.content.Intent#getStringExtra
79 * content.Intent.getStringExtra()}
80 * to obtain the query string from Intent.ACTION_SEARCH.
81 */
82 public final static String QUERY = "query";
83
84 /**
Bjorn Bringert5f806052009-06-24 12:02:26 +010085 * Intent extra data key: Use this key with
86 * {@link android.content.Intent#getStringExtra
87 * content.Intent.getStringExtra()}
88 * to obtain the query string typed in by the user.
89 * This may be different from the value of {@link #QUERY}
90 * if the intent is the result of selecting a suggestion.
91 * In that case, {@link #QUERY} will contain the value of
92 * {@link #SUGGEST_COLUMN_QUERY} for the suggestion, and
93 * {@link #USER_QUERY} will contain the string typed by the
94 * user.
95 */
96 public final static String USER_QUERY = "user_query";
97
98 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 * Intent extra data key: Use this key with Intent.ACTION_SEARCH and
100 * {@link android.content.Intent#getBundleExtra
101 * content.Intent.getBundleExtra()}
Amith Yamasanie678f462010-09-15 16:13:43 -0700102 * to obtain any additional app-specific data that was inserted by the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * activity that launched the search.
104 */
105 public final static String APP_DATA = "app_data";
106
107 /**
Bjorn Bringertbe5b73c2009-09-21 23:05:23 +0100108 * Intent extra data key: Use {@link android.content.Intent#getBundleExtra
109 * content.Intent.getBundleExtra(SEARCH_MODE)} to get the search mode used
110 * to launch the intent.
111 * The only current value for this is {@link #MODE_GLOBAL_SEARCH_SUGGESTION}.
112 *
113 * @hide
114 */
115 public final static String SEARCH_MODE = "search_mode";
116
117 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 * Intent extra data key: Use this key with Intent.ACTION_SEARCH and
119 * {@link android.content.Intent#getIntExtra content.Intent.getIntExtra()}
120 * to obtain the keycode that the user used to trigger this query. It will be zero if the
121 * user simply pressed the "GO" button on the search UI. This is primarily used in conjunction
122 * with the keycode attribute in the actionkey element of your searchable.xml configuration
123 * file.
124 */
125 public final static String ACTION_KEY = "action_key";
Amith Yamasanie678f462010-09-15 16:13:43 -0700126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 /**
Karl Rosaen875d50a2009-04-23 19:00:21 -0700128 * Intent extra data key: This key will be used for the extra populated by the
129 * {@link #SUGGEST_COLUMN_INTENT_EXTRA_DATA} column.
Karl Rosaen875d50a2009-04-23 19:00:21 -0700130 */
131 public final static String EXTRA_DATA_KEY = "intent_extra_data_key";
Karl Rosaena058f022009-06-01 23:11:44 +0100132
Karl Rosaena058f022009-06-01 23:11:44 +0100133 /**
Bjorn Bringert55f3ac52010-01-14 11:38:22 +0000134 * Boolean extra data key for {@link #INTENT_ACTION_GLOBAL_SEARCH} intents. If {@code true},
135 * the initial query should be selected when the global search activity is started, so
136 * that the user can easily replace it with another query.
Bjorn Bringert3c50ef62009-12-09 12:23:12 +0000137 */
Bjorn Bringert55f3ac52010-01-14 11:38:22 +0000138 public final static String EXTRA_SELECT_QUERY = "select_query";
Bjorn Bringert32d580c2010-01-06 09:29:11 +0000139
Bjorn Bringert3c50ef62009-12-09 12:23:12 +0000140 /**
Amith Yamasanic37cb242010-02-24 17:44:46 -0800141 * Boolean extra data key for a suggestion provider to return in {@link Cursor#getExtras} to
142 * indicate that the search is not complete yet. This can be used by the search UI
143 * to indicate that a search is in progress. The suggestion provider can return partial results
144 * this way and send a change notification on the cursor when more results are available.
145 */
146 public final static String CURSOR_EXTRA_KEY_IN_PROGRESS = "in_progress";
147
148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 * Intent extra data key: Use this key with Intent.ACTION_SEARCH and
150 * {@link android.content.Intent#getStringExtra content.Intent.getStringExtra()}
151 * to obtain the action message that was defined for a particular search action key and/or
Amith Yamasanie678f462010-09-15 16:13:43 -0700152 * suggestion. It will be null if the search was launched by typing "enter", touched the the
153 * "GO" button, or other means not involving any action key.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 */
155 public final static String ACTION_MSG = "action_msg";
Amith Yamasanie678f462010-09-15 16:13:43 -0700156
157 /**
158 * Flag to specify that the entry can be used for query refinement, i.e., the query text
159 * in the search field can be replaced with the text in this entry, when a query refinement
160 * icon is clicked. The suggestion list should show such a clickable icon beside the entry.
161 * <p>Use this flag as a bit-field for {@link #SUGGEST_COLUMN_FLAGS}.
162 */
163 public final static int FLAG_QUERY_REFINEMENT = 1 << 0;
164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 /**
166 * Uri path for queried suggestions data. This is the path that the search manager
167 * will use when querying your content provider for suggestions data based on user input
168 * (e.g. looking for partial matches).
169 * Typically you'll use this with a URI matcher.
170 */
171 public final static String SUGGEST_URI_PATH_QUERY = "search_suggest_query";
Karl Rosaenb2a50112009-05-05 21:14:01 -0700172
173 /**
Karl Rosaenb619c912009-05-08 14:28:51 -0700174 * MIME type for suggestions data. You'll use this in your suggestions content provider
175 * in the getType() function.
176 */
177 public final static String SUGGEST_MIME_TYPE =
178 "vnd.android.cursor.dir/vnd.android.search.suggest";
179
180 /**
Karl Rosaenb2a50112009-05-05 21:14:01 -0700181 * Uri path for shortcut validation. This is the path that the search manager will use when
182 * querying your content provider to refresh a shortcutted suggestion result and to check if it
183 * is still valid. When asked, a source may return an up to date result, or no result. No
184 * result indicates the shortcut refers to a no longer valid sugggestion.
185 *
186 * @see #SUGGEST_COLUMN_SHORTCUT_ID
Karl Rosaenb2a50112009-05-05 21:14:01 -0700187 */
188 public final static String SUGGEST_URI_PATH_SHORTCUT = "search_suggest_shortcut";
Amith Yamasanie678f462010-09-15 16:13:43 -0700189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 /**
Karl Rosaenb619c912009-05-08 14:28:51 -0700191 * MIME type for shortcut validation. You'll use this in your suggestions content provider
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 * in the getType() function.
193 */
Amith Yamasanie678f462010-09-15 16:13:43 -0700194 public final static String SHORTCUT_MIME_TYPE =
Karl Rosaenb619c912009-05-08 14:28:51 -0700195 "vnd.android.cursor.item/vnd.android.search.suggest";
Karl Rosaend4c98c42009-06-09 17:05:54 +0100196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 /**
198 * Column name for suggestions cursor. <i>Unused - can be null or column can be omitted.</i>
199 */
200 public final static String SUGGEST_COLUMN_FORMAT = "suggest_format";
201 /**
Amith Yamasanie678f462010-09-15 16:13:43 -0700202 * Column name for suggestions cursor. <i>Required.</i> This is the primary line of text that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 * will be presented to the user as the suggestion.
204 */
205 public final static String SUGGEST_COLUMN_TEXT_1 = "suggest_text_1";
206 /**
207 * Column name for suggestions cursor. <i>Optional.</i> If your cursor includes this column,
208 * then all suggestions will be provided in a two-line format. The second line of text is in
209 * a much smaller appearance.
210 */
211 public final static String SUGGEST_COLUMN_TEXT_2 = "suggest_text_2";
Bjorn Bringert0b49ab52010-03-02 11:16:17 +0000212
213 /**
214 * Column name for suggestions cursor. <i>Optional.</i> This is a URL that will be shown
215 * as the second line of text instead of {@link #SUGGEST_COLUMN_TEXT_2}. This is a separate
216 * column so that the search UI knows to display the text as a URL, e.g. by using a different
217 * color. If this column is absent, or has the value {@code null},
218 * {@link #SUGGEST_COLUMN_TEXT_2} will be used instead.
219 */
220 public final static String SUGGEST_COLUMN_TEXT_2_URL = "suggest_text_2_url";
221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 /**
223 * Column name for suggestions cursor. <i>Optional.</i> If your cursor includes this column,
Karl Rosaen875d50a2009-04-23 19:00:21 -0700224 * then all suggestions will be provided in a format that includes space for two small icons,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 * one at the left and one at the right of each suggestion. The data in the column must
Karl Rosaen875d50a2009-04-23 19:00:21 -0700226 * be a resource ID of a drawable, or a URI in one of the following formats:
227 *
228 * <ul>
229 * <li>content ({@link android.content.ContentResolver#SCHEME_CONTENT})</li>
230 * <li>android.resource ({@link android.content.ContentResolver#SCHEME_ANDROID_RESOURCE})</li>
231 * <li>file ({@link android.content.ContentResolver#SCHEME_FILE})</li>
232 * </ul>
233 *
Amith Yamasanie678f462010-09-15 16:13:43 -0700234 * See {@link android.content.ContentResolver#openAssetFileDescriptor(Uri, String)}
235 * for more information on these schemes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 */
237 public final static String SUGGEST_COLUMN_ICON_1 = "suggest_icon_1";
238 /**
239 * Column name for suggestions cursor. <i>Optional.</i> If your cursor includes this column,
Karl Rosaen875d50a2009-04-23 19:00:21 -0700240 * then all suggestions will be provided in a format that includes space for two small icons,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 * one at the left and one at the right of each suggestion. The data in the column must
Karl Rosaen875d50a2009-04-23 19:00:21 -0700242 * be a resource ID of a drawable, or a URI in one of the following formats:
243 *
244 * <ul>
245 * <li>content ({@link android.content.ContentResolver#SCHEME_CONTENT})</li>
246 * <li>android.resource ({@link android.content.ContentResolver#SCHEME_ANDROID_RESOURCE})</li>
247 * <li>file ({@link android.content.ContentResolver#SCHEME_FILE})</li>
248 * </ul>
249 *
Amith Yamasanie678f462010-09-15 16:13:43 -0700250 * See {@link android.content.ContentResolver#openAssetFileDescriptor(Uri, String)}
251 * for more information on these schemes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 */
253 public final static String SUGGEST_COLUMN_ICON_2 = "suggest_icon_2";
254 /**
255 * Column name for suggestions cursor. <i>Optional.</i> If this column exists <i>and</i>
256 * this element exists at the given row, this is the action that will be used when
257 * forming the suggestion's intent. If the element is not provided, the action will be taken
258 * from the android:searchSuggestIntentAction field in your XML metadata. <i>At least one of
259 * these must be present for the suggestion to generate an intent.</i> Note: If your action is
260 * the same for all suggestions, it is more efficient to specify it using XML metadata and omit
261 * it from the cursor.
262 */
263 public final static String SUGGEST_COLUMN_INTENT_ACTION = "suggest_intent_action";
264 /**
265 * Column name for suggestions cursor. <i>Optional.</i> If this column exists <i>and</i>
266 * this element exists at the given row, this is the data that will be used when
267 * forming the suggestion's intent. If the element is not provided, the data will be taken
268 * from the android:searchSuggestIntentData field in your XML metadata. If neither source
269 * is provided, the Intent's data field will be null. Note: If your data is
270 * the same for all suggestions, or can be described using a constant part and a specific ID,
271 * it is more efficient to specify it using XML metadata and omit it from the cursor.
272 */
273 public final static String SUGGEST_COLUMN_INTENT_DATA = "suggest_intent_data";
274 /**
Satish Sampathbf23fe02009-06-15 23:47:56 +0100275 * Column name for suggestions cursor. <i>Optional.</i> If this column exists <i>and</i>
276 * this element exists at the given row, this is the data that will be used when
277 * forming the suggestion's intent. If not provided, the Intent's extra data field will be null.
278 * This column allows suggestions to provide additional arbitrary data which will be included as
Mike LeBeau131234c2009-07-20 14:09:52 -0700279 * an extra under the key {@link #EXTRA_DATA_KEY}.
Satish Sampathbf23fe02009-06-15 23:47:56 +0100280 */
281 public final static String SUGGEST_COLUMN_INTENT_EXTRA_DATA = "suggest_intent_extra_data";
282 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 * Column name for suggestions cursor. <i>Optional.</i> If this column exists <i>and</i>
284 * this element exists at the given row, then "/" and this value will be appended to the data
285 * field in the Intent. This should only be used if the data field has already been set to an
286 * appropriate base string.
287 */
288 public final static String SUGGEST_COLUMN_INTENT_DATA_ID = "suggest_intent_data_id";
289 /**
Amith Yamasanie678f462010-09-15 16:13:43 -0700290 * Column name for suggestions cursor. <i>Required if action is
291 * {@link android.content.Intent#ACTION_SEARCH ACTION_SEARCH}, optional otherwise.</i> If this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 * column exists <i>and</i> this element exists at the given row, this is the data that will be
293 * used when forming the suggestion's query.
294 */
295 public final static String SUGGEST_COLUMN_QUERY = "suggest_intent_query";
296
Karl Rosaen875d50a2009-04-23 19:00:21 -0700297 /**
Karl Rosaenb2a50112009-05-05 21:14:01 -0700298 * Column name for suggestions cursor. <i>Optional.</i> This column is used to indicate whether
Karl Rosaenb5041362009-08-11 11:35:07 -0700299 * a search suggestion should be stored as a shortcut, and whether it should be refreshed. If
Karl Rosaenb2a50112009-05-05 21:14:01 -0700300 * missing, the result will be stored as a shortcut and never validated. If set to
301 * {@link #SUGGEST_NEVER_MAKE_SHORTCUT}, the result will not be stored as a shortcut.
Karl Rosaenb5041362009-08-11 11:35:07 -0700302 * Otherwise, the shortcut id will be used to check back for an up to date suggestion using
Karl Rosaenb2a50112009-05-05 21:14:01 -0700303 * {@link #SUGGEST_URI_PATH_SHORTCUT}.
Karl Rosaenb2a50112009-05-05 21:14:01 -0700304 */
305 public final static String SUGGEST_COLUMN_SHORTCUT_ID = "suggest_shortcut_id";
306
307 /**
Mike LeBeauce0959d2009-06-10 23:47:54 +0100308 * Column name for suggestions cursor. <i>Optional.</i> This column is used to specify
309 * that a spinner should be shown in lieu of an icon2 while the shortcut of this suggestion
310 * is being refreshed.
Mike LeBeauce0959d2009-06-10 23:47:54 +0100311 */
312 public final static String SUGGEST_COLUMN_SPINNER_WHILE_REFRESHING =
313 "suggest_spinner_while_refreshing";
Satish Sampath1c5fa0f2009-06-08 23:10:40 +0100314
315 /**
Amith Yamasanie678f462010-09-15 16:13:43 -0700316 * Column name for suggestions cursor. <i>Optional.</i> This column is used to specify
317 * additional flags per item. Multiple flags can be specified.
318 * <p>
319 * Must be one of {@link #FLAG_QUERY_REFINEMENT} or 0 to indicate no flags.
320 * </p>
321 */
322 public final static String SUGGEST_COLUMN_FLAGS = "suggest_flags";
323
324 /**
Karl Rosaenb2a50112009-05-05 21:14:01 -0700325 * Column value for suggestion column {@link #SUGGEST_COLUMN_SHORTCUT_ID} when a suggestion
326 * should not be stored as a shortcut in global search.
Karl Rosaenb2a50112009-05-05 21:14:01 -0700327 */
328 public final static String SUGGEST_NEVER_MAKE_SHORTCUT = "_-1";
329
330 /**
Bjorn Bringert3fc7c672009-09-28 20:11:52 +0100331 * Query parameter added to suggestion queries to limit the number of suggestions returned.
Bjorn Bringert86917db2009-09-28 22:00:50 +0100332 * This limit is only advisory and suggestion providers may chose to ignore it.
Bjorn Bringert3fc7c672009-09-28 20:11:52 +0100333 */
334 public final static String SUGGEST_PARAMETER_LIMIT = "limit";
335
336 /**
Bjorn Bringert55f3ac52010-01-14 11:38:22 +0000337 * Intent action for starting the global search activity.
Bjorn Bringert74708bb2009-04-28 11:26:52 +0100338 * The global search provider should handle this intent.
Bjorn Bringert55f3ac52010-01-14 11:38:22 +0000339 *
340 * Supported extra data keys: {@link #QUERY},
341 * {@link #EXTRA_SELECT_QUERY},
342 * {@link #APP_DATA}.
Bjorn Bringert74708bb2009-04-28 11:26:52 +0100343 */
Amith Yamasanie678f462010-09-15 16:13:43 -0700344 public final static String INTENT_ACTION_GLOBAL_SEARCH
Bjorn Bringert74708bb2009-04-28 11:26:52 +0100345 = "android.search.action.GLOBAL_SEARCH";
Amith Yamasanie678f462010-09-15 16:13:43 -0700346
Bjorn Bringert74708bb2009-04-28 11:26:52 +0100347 /**
Karl Rosaen875d50a2009-04-23 19:00:21 -0700348 * Intent action for starting the global search settings activity.
349 * The global search provider should handle this intent.
Karl Rosaen875d50a2009-04-23 19:00:21 -0700350 */
Amith Yamasanie678f462010-09-15 16:13:43 -0700351 public final static String INTENT_ACTION_SEARCH_SETTINGS
Karl Rosaen875d50a2009-04-23 19:00:21 -0700352 = "android.search.action.SEARCH_SETTINGS";
Amith Yamasanie678f462010-09-15 16:13:43 -0700353
Mike LeBeau3a278032009-06-04 14:09:42 +0100354 /**
355 * Intent action for starting a web search provider's settings activity.
356 * Web search providers should handle this intent if they have provider-specific
357 * settings to implement.
Mike LeBeau3a278032009-06-04 14:09:42 +0100358 */
359 public final static String INTENT_ACTION_WEB_SEARCH_SETTINGS
360 = "android.search.action.WEB_SEARCH_SETTINGS";
Karl Rosaena058f022009-06-01 23:11:44 +0100361
362 /**
Satish Sampathf9acde22009-06-04 11:51:17 +0100363 * Intent action broadcasted to inform that the searchables list or default have changed.
364 * Components should handle this intent if they cache any searchable data and wish to stay
365 * up to date on changes.
Satish Sampathf9acde22009-06-04 11:51:17 +0100366 */
367 public final static String INTENT_ACTION_SEARCHABLES_CHANGED
368 = "android.search.action.SEARCHABLES_CHANGED";
Amith Yamasanie678f462010-09-15 16:13:43 -0700369
Mike LeBeaud4fb7a02009-07-20 17:37:38 -0700370 /**
371 * Intent action broadcasted to inform that the search settings have changed in some way.
Karl Rosaen590f6342009-08-27 17:42:48 -0700372 * Either searchables have been enabled or disabled, or a different web search provider
373 * has been chosen.
Mike LeBeaud4fb7a02009-07-20 17:37:38 -0700374 */
375 public final static String INTENT_ACTION_SEARCH_SETTINGS_CHANGED
376 = "android.search.action.SETTINGS_CHANGED";
Satish Sampathf9acde22009-06-04 11:51:17 +0100377
378 /**
Leon Scroggins4f22a542010-03-09 17:28:13 -0500379 * This means that context is voice, and therefore the SearchDialog should
380 * continue showing the microphone until the user indicates that he/she does
381 * not want to re-speak (e.g. by typing).
382 *
383 * @hide
384 */
385 public final static String CONTEXT_IS_VOICE = "android.search.CONTEXT_IS_VOICE";
386
387 /**
Leon Scroggins III4a028002010-09-14 10:56:25 -0400388 * This means that the voice icon should not be shown at all, because the
389 * current search engine does not support voice search.
390 * @hide
391 */
392 public final static String DISABLE_VOICE_SEARCH
393 = "android.search.DISABLE_VOICE_SEARCH";
394
395 /**
Karl Rosaen875d50a2009-04-23 19:00:21 -0700396 * Reference to the shared system search service.
397 */
Bjorn Bringert8d153822009-06-22 10:31:44 +0100398 private static ISearchManager mService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399
400 private final Context mContext;
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100401
krosaend2d60142009-08-17 08:56:48 -0700402 /**
krosaend2d60142009-08-17 08:56:48 -0700403 * The package associated with this seach manager.
404 */
405 private String mAssociatedPackage;
Amith Yamasanie678f462010-09-15 16:13:43 -0700406
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100407 // package private since they are used by the inner class SearchManagerCallback
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100408 /* package */ final Handler mHandler;
409 /* package */ OnDismissListener mDismissListener = null;
410 /* package */ OnCancelListener mCancelListener = null;
411
Amith Yamasanie9ce3f02010-01-25 09:15:50 -0800412 private SearchDialog mSearchDialog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413
414 /*package*/ SearchManager(Context context, Handler handler) {
415 mContext = context;
416 mHandler = handler;
Bjorn Bringert8d153822009-06-22 10:31:44 +0100417 mService = ISearchManager.Stub.asInterface(
418 ServiceManager.getService(Context.SEARCH_SERVICE));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 }
Amith Yamasanie678f462010-09-15 16:13:43 -0700420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 /**
422 * Launch search UI.
423 *
424 * <p>The search manager will open a search widget in an overlapping
Amith Yamasanie678f462010-09-15 16:13:43 -0700425 * window, and the underlying activity may be obscured. The search
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 * entry state will remain in effect until one of the following events:
427 * <ul>
Amith Yamasanie678f462010-09-15 16:13:43 -0700428 * <li>The user completes the search. In most cases this will launch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 * a search intent.</li>
430 * <li>The user uses the back, home, or other keys to exit the search.</li>
431 * <li>The application calls the {@link #stopSearch}
432 * method, which will hide the search window and return focus to the
433 * activity from which it was launched.</li>
434 *
435 * <p>Most applications will <i>not</i> use this interface to invoke search.
Amith Yamasanie678f462010-09-15 16:13:43 -0700436 * The primary method for invoking search is to call
437 * {@link android.app.Activity#onSearchRequested Activity.onSearchRequested()} or
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 * {@link android.app.Activity#startSearch Activity.startSearch()}.
439 *
440 * @param initialQuery A search string can be pre-entered here, but this
441 * is typically null or empty.
442 * @param selectInitialQuery If true, the intial query will be preselected, which means that
443 * any further typing will replace it. This is useful for cases where an entire pre-formed
444 * query is being inserted. If false, the selection point will be placed at the end of the
445 * inserted query. This is useful when the inserted query is text that the user entered,
446 * and the user would expect to be able to keep typing. <i>This parameter is only meaningful
447 * if initialQuery is a non-empty string.</i>
448 * @param launchActivity The ComponentName of the activity that has launched this search.
Amith Yamasanie678f462010-09-15 16:13:43 -0700449 * @param appSearchData An application can insert application-specific
450 * context here, in order to improve quality or specificity of its own
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 * searches. This data will be returned with SEARCH intent(s). Null if
452 * no extra data is required.
453 * @param globalSearch If false, this will only launch the search that has been specifically
Amith Yamasanie678f462010-09-15 16:13:43 -0700454 * defined by the application (which is usually defined as a local search). If no default
Mike LeBeaucfa419b2009-08-17 10:56:02 -0700455 * search is defined in the current application or activity, global search will be launched.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 * If true, this will always launch a platform-global (e.g. web-based) search instead.
Amith Yamasanie678f462010-09-15 16:13:43 -0700457 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 * @see android.app.Activity#onSearchRequested
459 * @see #stopSearch
460 */
Amith Yamasanie678f462010-09-15 16:13:43 -0700461 public void startSearch(String initialQuery,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 boolean selectInitialQuery,
463 ComponentName launchActivity,
464 Bundle appSearchData,
465 boolean globalSearch) {
Bjorn Bringert3c50ef62009-12-09 12:23:12 +0000466 if (globalSearch) {
467 startGlobalSearch(initialQuery, selectInitialQuery, appSearchData);
468 return;
469 }
470
Amith Yamasani479ae0a2010-02-04 14:52:32 -0800471 ensureSearchDialog();
472
473 mSearchDialog.show(initialQuery, selectInitialQuery, launchActivity, appSearchData);
Amith Yamasanie9ce3f02010-01-25 09:15:50 -0800474 }
475
476 private void ensureSearchDialog() {
477 if (mSearchDialog == null) {
478 mSearchDialog = new SearchDialog(mContext, this);
479 mSearchDialog.setOnCancelListener(this);
480 mSearchDialog.setOnDismissListener(this);
krosaend2d60142009-08-17 08:56:48 -0700481 }
482 }
483
484 /**
Bjorn Bringert3c50ef62009-12-09 12:23:12 +0000485 * Starts the global search activity.
486 */
Amith Yamasanie9ce3f02010-01-25 09:15:50 -0800487 /* package */ void startGlobalSearch(String initialQuery, boolean selectInitialQuery,
Bjorn Bringert3c50ef62009-12-09 12:23:12 +0000488 Bundle appSearchData) {
489 ComponentName globalSearchActivity = getGlobalSearchActivity();
490 if (globalSearchActivity == null) {
491 Log.w(TAG, "No global search activity found.");
492 return;
493 }
Bjorn Bringerta94e7af2010-01-13 21:09:20 +0000494 Intent intent = new Intent(INTENT_ACTION_GLOBAL_SEARCH);
Bjorn Bringertb8144a92010-02-22 20:48:57 +0000495 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bjorn Bringert3c50ef62009-12-09 12:23:12 +0000496 intent.setComponent(globalSearchActivity);
Bjorn Bringert3c24cb72010-03-16 12:54:41 +0000497 // Make sure that we have a Bundle to put source in
498 if (appSearchData == null) {
499 appSearchData = new Bundle();
500 } else {
501 appSearchData = new Bundle(appSearchData);
Bjorn Bringert3c50ef62009-12-09 12:23:12 +0000502 }
Bjorn Bringert3c24cb72010-03-16 12:54:41 +0000503 // Set source to package name of app that starts global search, if not set already.
504 if (!appSearchData.containsKey("source")) {
505 appSearchData.putString("source", mContext.getPackageName());
506 }
507 intent.putExtra(APP_DATA, appSearchData);
Bjorn Bringert3c50ef62009-12-09 12:23:12 +0000508 if (!TextUtils.isEmpty(initialQuery)) {
Bjorn Bringert55f3ac52010-01-14 11:38:22 +0000509 intent.putExtra(QUERY, initialQuery);
Bjorn Bringert3c50ef62009-12-09 12:23:12 +0000510 }
511 if (selectInitialQuery) {
Bjorn Bringert55f3ac52010-01-14 11:38:22 +0000512 intent.putExtra(EXTRA_SELECT_QUERY, selectInitialQuery);
Bjorn Bringert3c50ef62009-12-09 12:23:12 +0000513 }
514 try {
515 if (DBG) Log.d(TAG, "Starting global search: " + intent.toUri(0));
516 mContext.startActivity(intent);
517 } catch (ActivityNotFoundException ex) {
518 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
519 }
520 }
521
522 /**
523 * Gets the name of the global search activity.
524 *
Bjorn Bringert38015f32010-01-14 14:56:18 +0000525 * @hide
Bjorn Bringert3c50ef62009-12-09 12:23:12 +0000526 */
Bjorn Bringert38015f32010-01-14 14:56:18 +0000527 public ComponentName getGlobalSearchActivity() {
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000528 try {
529 return mService.getGlobalSearchActivity();
530 } catch (RemoteException ex) {
531 Log.e(TAG, "getGlobalSearchActivity() failed: " + ex);
532 return null;
Bjorn Bringert3c50ef62009-12-09 12:23:12 +0000533 }
Bjorn Bringert3c50ef62009-12-09 12:23:12 +0000534 }
535
536 /**
Bjorn Bringertea125f42010-02-18 21:57:59 +0000537 * Gets the name of the web search activity.
538 *
539 * @return The name of the default activity for web searches. This activity
540 * can be used to get web search suggestions. Returns {@code null} if
541 * there is no default web search activity.
542 *
543 * @hide
544 */
545 public ComponentName getWebSearchActivity() {
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000546 try {
547 return mService.getWebSearchActivity();
548 } catch (RemoteException ex) {
549 Log.e(TAG, "getWebSearchActivity() failed: " + ex);
Bjorn Bringertea125f42010-02-18 21:57:59 +0000550 return null;
551 }
Bjorn Bringertea125f42010-02-18 21:57:59 +0000552 }
553
554 /**
krosaend2d60142009-08-17 08:56:48 -0700555 * Similar to {@link #startSearch} but actually fires off the search query after invoking
556 * the search dialog. Made available for testing purposes.
557 *
558 * @param query The query to trigger. If empty, request will be ignored.
559 * @param launchActivity The ComponentName of the activity that has launched this search.
560 * @param appSearchData An application can insert application-specific
561 * context here, in order to improve quality or specificity of its own
562 * searches. This data will be returned with SEARCH intent(s). Null if
563 * no extra data is required.
krosaend2d60142009-08-17 08:56:48 -0700564 *
565 * @see #startSearch
566 */
567 public void triggerSearch(String query,
568 ComponentName launchActivity,
Bjorn Bringertb782a2f2009-10-01 09:57:33 +0100569 Bundle appSearchData) {
krosaend2d60142009-08-17 08:56:48 -0700570 if (!mAssociatedPackage.equals(launchActivity.getPackageName())) {
571 throw new IllegalArgumentException("invoking app search on a different package " +
572 "not associated with this search manager");
573 }
574 if (query == null || TextUtils.getTrimmedLength(query) == 0) {
575 Log.w(TAG, "triggerSearch called with empty query, ignoring.");
576 return;
577 }
Amith Yamasanie9ce3f02010-01-25 09:15:50 -0800578 startSearch(query, false, launchActivity, appSearchData, false);
579 mSearchDialog.launchQuerySearch();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 }
581
582 /**
583 * Terminate search UI.
584 *
585 * <p>Typically the user will terminate the search UI by launching a
586 * search or by canceling. This function allows the underlying application
587 * or activity to cancel the search prematurely (for any reason).
Amith Yamasanie678f462010-09-15 16:13:43 -0700588 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 * <p>This function can be safely called at any time (even if no search is active.)
590 *
591 * @see #startSearch
592 */
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100593 public void stopSearch() {
Amith Yamasanie9ce3f02010-01-25 09:15:50 -0800594 if (mSearchDialog != null) {
595 mSearchDialog.cancel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 }
597 }
598
599 /**
Amith Yamasanie678f462010-09-15 16:13:43 -0700600 * Determine if the Search UI is currently displayed.
601 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 * This is provided primarily for application test purposes.
603 *
604 * @return Returns true if the search UI is currently displayed.
Amith Yamasanie678f462010-09-15 16:13:43 -0700605 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 * @hide
607 */
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100608 public boolean isVisible() {
Amith Yamasanie9ce3f02010-01-25 09:15:50 -0800609 return mSearchDialog == null? false : mSearchDialog.isShowing();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 }
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 /**
Karl Rosaena058f022009-06-01 23:11:44 +0100613 * See {@link SearchManager#setOnDismissListener} for configuring your activity to monitor
614 * search UI state.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 */
616 public interface OnDismissListener {
617 /**
Karl Rosaena058f022009-06-01 23:11:44 +0100618 * This method will be called when the search UI is dismissed. To make use of it, you must
619 * implement this method in your activity, and call
620 * {@link SearchManager#setOnDismissListener} to register it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 */
622 public void onDismiss();
623 }
Amith Yamasanie678f462010-09-15 16:13:43 -0700624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 /**
Karl Rosaena058f022009-06-01 23:11:44 +0100626 * See {@link SearchManager#setOnCancelListener} for configuring your activity to monitor
627 * search UI state.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 */
629 public interface OnCancelListener {
630 /**
631 * This method will be called when the search UI is canceled. To make use if it, you must
Karl Rosaena058f022009-06-01 23:11:44 +0100632 * implement this method in your activity, and call
633 * {@link SearchManager#setOnCancelListener} to register it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 */
635 public void onCancel();
636 }
637
638 /**
639 * Set or clear the callback that will be invoked whenever the search UI is dismissed.
Amith Yamasanie678f462010-09-15 16:13:43 -0700640 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 * @param listener The {@link OnDismissListener} to use, or null.
642 */
643 public void setOnDismissListener(final OnDismissListener listener) {
644 mDismissListener = listener;
645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646
647 /**
648 * Set or clear the callback that will be invoked whenever the search UI is canceled.
Amith Yamasanie678f462010-09-15 16:13:43 -0700649 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 * @param listener The {@link OnCancelListener} to use, or null.
651 */
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100652 public void setOnCancelListener(OnCancelListener listener) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 mCancelListener = listener;
654 }
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100655
Bjorn Bringert0e01ea42009-07-08 22:01:29 +0100656 /**
657 * @deprecated This method is an obsolete internal implementation detail. Do not use.
658 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -0700659 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 public void onCancel(DialogInterface dialog) {
Amith Yamasanie9ce3f02010-01-25 09:15:50 -0800661 if (mCancelListener != null) {
662 mCancelListener.onCancel();
663 }
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100664 }
Bjorn Bringert0e01ea42009-07-08 22:01:29 +0100665
666 /**
667 * @deprecated This method is an obsolete internal implementation detail. Do not use.
668 */
Dianne Hackborn4a51c202009-08-21 15:14:02 -0700669 @Deprecated
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100670 public void onDismiss(DialogInterface dialog) {
Amith Yamasanie9ce3f02010-01-25 09:15:50 -0800671 if (mDismissListener != null) {
672 mDismissListener.onDismiss();
673 }
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100674 }
675
676 /**
Bjorn Bringert04086752010-01-12 15:50:04 +0000677 * Gets information about a searchable activity.
678 *
679 * @param componentName The activity to get searchable information for.
680 * @return Searchable information, or <code>null</code> if the activity does not
681 * exist, or is not searchable.
682 */
683 public SearchableInfo getSearchableInfo(ComponentName componentName) {
684 try {
Bjorn Bringert6cf7a322010-02-23 13:17:06 +0000685 return mService.getSearchableInfo(componentName);
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100686 } catch (RemoteException ex) {
687 Log.e(TAG, "getSearchableInfo() failed: " + ex);
Karl Rosaen875d50a2009-04-23 19:00:21 -0700688 return null;
689 }
690 }
Bjorn Bringert8d153822009-06-22 10:31:44 +0100691
Karl Rosaen875d50a2009-04-23 19:00:21 -0700692 /**
Bjorn Bringert97325db2009-06-22 11:37:03 +0100693 * Gets a cursor with search suggestions.
694 *
695 * @param searchable Information about how to get the suggestions.
696 * @param query The search text entered (so far).
697 * @return a cursor with suggestions, or <code>null</null> the suggestion query failed.
698 *
699 * @hide because SearchableInfo is not part of the API.
700 */
701 public Cursor getSuggestions(SearchableInfo searchable, String query) {
Bjorn Bringert3fc7c672009-09-28 20:11:52 +0100702 return getSuggestions(searchable, query, -1);
703 }
704
705 /**
706 * Gets a cursor with search suggestions.
707 *
708 * @param searchable Information about how to get the suggestions.
709 * @param query The search text entered (so far).
710 * @param limit The query limit to pass to the suggestion provider. This is advisory,
711 * the returned cursor may contain more rows. Pass {@code -1} for no limit.
712 * @return a cursor with suggestions, or <code>null</null> the suggestion query failed.
713 *
714 * @hide because SearchableInfo is not part of the API.
715 */
716 public Cursor getSuggestions(SearchableInfo searchable, String query, int limit) {
Karl Rosaen875d50a2009-04-23 19:00:21 -0700717 if (searchable == null) {
718 return null;
719 }
720
721 String authority = searchable.getSuggestAuthority();
722 if (authority == null) {
723 return null;
724 }
725
726 Uri.Builder uriBuilder = new Uri.Builder()
727 .scheme(ContentResolver.SCHEME_CONTENT)
Bjorn Bringert3fc7c672009-09-28 20:11:52 +0100728 .authority(authority)
729 .query("") // TODO: Remove, workaround for a bug in Uri.writeToParcel()
730 .fragment(""); // TODO: Remove, workaround for a bug in Uri.writeToParcel()
Karl Rosaen875d50a2009-04-23 19:00:21 -0700731
732 // if content path provided, insert it now
733 final String contentPath = searchable.getSuggestPath();
734 if (contentPath != null) {
735 uriBuilder.appendEncodedPath(contentPath);
736 }
737
Bjorn Bringert3fc7c672009-09-28 20:11:52 +0100738 // append standard suggestion query path
Karl Rosaen875d50a2009-04-23 19:00:21 -0700739 uriBuilder.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY);
740
741 // get the query selection, may be null
742 String selection = searchable.getSuggestSelection();
743 // inject query, either as selection args or inline
744 String[] selArgs = null;
745 if (selection != null) { // use selection if provided
746 selArgs = new String[] { query };
747 } else { // no selection, use REST pattern
748 uriBuilder.appendPath(query);
749 }
750
Bjorn Bringert3fc7c672009-09-28 20:11:52 +0100751 if (limit > 0) {
752 uriBuilder.appendQueryParameter(SUGGEST_PARAMETER_LIMIT, String.valueOf(limit));
753 }
754
755 Uri uri = uriBuilder.build();
Karl Rosaen875d50a2009-04-23 19:00:21 -0700756
757 // finally, make the query
Bjorn Bringert8d153822009-06-22 10:31:44 +0100758 return mContext.getContentResolver().query(uri, null, selection, selArgs, null);
Karl Rosaen875d50a2009-04-23 19:00:21 -0700759 }
Amith Yamasanie678f462010-09-15 16:13:43 -0700760
Bjorn Bringert6d72e022009-04-29 14:56:12 +0100761 /**
762 * Returns a list of the searchable activities that can be included in global search.
Amith Yamasanie678f462010-09-15 16:13:43 -0700763 *
Bjorn Bringert6d72e022009-04-29 14:56:12 +0100764 * @return a list containing searchable information for all searchable activities
Bjorn Bringert13107bb2009-12-07 10:41:24 +0000765 * that have the <code>android:includeInGlobalSearch</code> attribute set
766 * in their searchable meta-data.
Bjorn Bringert6d72e022009-04-29 14:56:12 +0100767 */
Bjorn Bringert8d153822009-06-22 10:31:44 +0100768 public List<SearchableInfo> getSearchablesInGlobalSearch() {
Bjorn Bringert6d72e022009-04-29 14:56:12 +0100769 try {
Bjorn Bringert8d153822009-06-22 10:31:44 +0100770 return mService.getSearchablesInGlobalSearch();
Bjorn Bringert6d72e022009-04-29 14:56:12 +0100771 } catch (RemoteException e) {
Bjorn Bringert8d17f3f2009-06-05 13:22:28 +0100772 Log.e(TAG, "getSearchablesInGlobalSearch() failed: " + e);
Bjorn Bringert6d72e022009-04-29 14:56:12 +0100773 return null;
774 }
775 }
Satish Sampathf9acde22009-06-04 11:51:17 +0100776
Amith Yamasanie9ce3f02010-01-25 09:15:50 -0800777}