blob: fd709f214bb7eab52e34e49b165b129e12d0d401 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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.speech;
18
Mike LeBeau79375f72010-02-18 19:01:47 -080019import java.util.ArrayList;
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.app.Activity;
22import android.content.ActivityNotFoundException;
Mike LeBeau79375f72010-02-18 19:01:47 -080023import android.content.BroadcastReceiver;
24import android.content.ComponentName;
25import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Intent;
Mike LeBeau79375f72010-02-18 19:01:47 -080027import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
29import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
31/**
32 * Constants for supporting speech recognition through starting an {@link Intent}
33 */
34public class RecognizerIntent {
Dianne Hackborn2269d1572010-02-24 19:54:22 -080035 /**
36 * The extra key used in an intent to the speech recognizer for voice search. Not
37 * generally to be used by developers. The system search dialog uses this, for example,
38 * to set a calling package for identification by a voice search API. If this extra
39 * is set by anyone but the system process, it should be overridden by the voice search
40 * implementation.
41 */
42 public final static String EXTRA_CALLING_PACKAGE = "calling_package";
43
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 private RecognizerIntent() {
45 // Not for instantiating.
46 }
47
48 /**
Mike LeBeaubb7cf912011-04-05 09:37:30 -040049 * Starts an activity that will prompt the user for speech and send it through a
Mike LeBeau5a346d62010-02-02 15:28:52 -080050 * speech recognizer. The results will be returned via activity results (in
51 * {@link Activity#onActivityResult}, if you start the intent using
52 * {@link Activity#startActivityForResult(Intent, int)}), or forwarded via a PendingIntent
53 * if one is provided.
54 *
55 * <p>Starting this intent with just {@link Activity#startActivity(Intent)} is not supported.
56 * You must either use {@link Activity#startActivityForResult(Intent, int)}, or provide a
57 * PendingIntent, to receive recognition results.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 *
59 * <p>Required extras:
60 * <ul>
61 * <li>{@link #EXTRA_LANGUAGE_MODEL}
62 * </ul>
63 *
64 * <p>Optional extras:
65 * <ul>
66 * <li>{@link #EXTRA_PROMPT}
67 * <li>{@link #EXTRA_LANGUAGE}
68 * <li>{@link #EXTRA_MAX_RESULTS}
69 * <li>{@link #EXTRA_RESULTS_PENDINGINTENT}
70 * <li>{@link #EXTRA_RESULTS_PENDINGINTENT_BUNDLE}
71 * </ul>
72 *
Mike LeBeau5a346d62010-02-02 15:28:52 -080073 * <p> Result extras (returned in the result, not to be specified in the request):
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 * <ul>
75 * <li>{@link #EXTRA_RESULTS}
76 * </ul>
77 *
78 * <p>NOTE: There may not be any applications installed to handle this action, so you should
79 * make sure to catch {@link ActivityNotFoundException}.
80 */
81 public static final String ACTION_RECOGNIZE_SPEECH = "android.speech.action.RECOGNIZE_SPEECH";
82
83 /**
Mike LeBeaubb7cf912011-04-05 09:37:30 -040084 * Starts an activity that will prompt the user for speech, send it through a
85 * speech recognizer, and either display a web search result or trigger
Mike LeBeaub30023c2010-12-16 04:45:26 -050086 * another type of action based on the user's speech.
87 *
88 * <p>If you want to avoid triggering any type of action besides web search, you can use
89 * the {@link #EXTRA_WEB_SEARCH_ONLY} extra.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 *
91 * <p>Required extras:
92 * <ul>
93 * <li>{@link #EXTRA_LANGUAGE_MODEL}
94 * </ul>
95 *
96 * <p>Optional extras:
97 * <ul>
98 * <li>{@link #EXTRA_PROMPT}
99 * <li>{@link #EXTRA_LANGUAGE}
100 * <li>{@link #EXTRA_MAX_RESULTS}
Alex Gruensteincc47fae2010-02-09 14:36:42 -0800101 * <li>{@link #EXTRA_PARTIAL_RESULTS}
Mike LeBeaub30023c2010-12-16 04:45:26 -0500102 * <li>{@link #EXTRA_WEB_SEARCH_ONLY}
Mike LeBeau216ce0f2011-04-05 16:38:24 -0400103 * <li>{@link #EXTRA_ORIGIN}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 * </ul>
105 *
Mike LeBeau5a346d62010-02-02 15:28:52 -0800106 * <p> Result extras (returned in the result, not to be specified in the request):
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * <ul>
108 * <li>{@link #EXTRA_RESULTS}
Mike LeBeaubb7cf912011-04-05 09:37:30 -0400109 * <li>{@link #EXTRA_CONFIDENCE_SCORES} (optional)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 * </ul>
111 *
112 * <p>NOTE: There may not be any applications installed to handle this action, so you should
113 * make sure to catch {@link ActivityNotFoundException}.
114 */
115 public static final String ACTION_WEB_SEARCH = "android.speech.action.WEB_SEARCH";
116
117 /**
Valentin Kravtsov79896bd2010-01-15 11:56:03 +0000118 * The minimum length of an utterance. We will not stop recording before this amount of time.
119 *
120 * Note that it is extremely rare you'd want to specify this value in an intent. If you don't
121 * have a very good reason to change these, you should leave them as they are. Note also that
122 * certain values may cause undesired or unexpected results - use judiciously! Additionally,
123 * depending on the recognizer implementation, these values may have no effect.
124 */
125 public static final String EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS =
126 "android.speech.extras.SPEECH_INPUT_MINIMUM_LENGTH_MILLIS";
127
128 /**
129 * The amount of time that it should take after we stop hearing speech to consider the input
130 * complete.
131 *
132 * Note that it is extremely rare you'd want to specify this value in an intent. If
133 * you don't have a very good reason to change these, you should leave them as they are. Note
134 * also that certain values may cause undesired or unexpected results - use judiciously!
135 * Additionally, depending on the recognizer implementation, these values may have no effect.
136 */
137 public static final String EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS =
138 "android.speech.extras.SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS";
139
140 /**
141 * The amount of time that it should take after we stop hearing speech to consider the input
142 * possibly complete. This is used to prevent the endpointer cutting off during very short
143 * mid-speech pauses.
144 *
145 * Note that it is extremely rare you'd want to specify this value in an intent. If
146 * you don't have a very good reason to change these, you should leave them as they are. Note
147 * also that certain values may cause undesired or unexpected results - use judiciously!
148 * Additionally, depending on the recognizer implementation, these values may have no effect.
149 */
150 public static final String EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS =
151 "android.speech.extras.SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS";
152
153 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 * Informs the recognizer which speech model to prefer when performing
155 * {@link #ACTION_RECOGNIZE_SPEECH}. The recognizer uses this
156 * information to fine tune the results. This extra is required. Activities implementing
157 * {@link #ACTION_RECOGNIZE_SPEECH} may interpret the values as they see fit.
158 *
159 * @see #LANGUAGE_MODEL_FREE_FORM
160 * @see #LANGUAGE_MODEL_WEB_SEARCH
161 */
162 public static final String EXTRA_LANGUAGE_MODEL = "android.speech.extra.LANGUAGE_MODEL";
163
164 /**
165 * Use a language model based on free-form speech recognition. This is a value to use for
166 * {@link #EXTRA_LANGUAGE_MODEL}.
167 * @see #EXTRA_LANGUAGE_MODEL
168 */
169 public static final String LANGUAGE_MODEL_FREE_FORM = "free_form";
170 /**
171 * Use a language model based on web search terms. This is a value to use for
172 * {@link #EXTRA_LANGUAGE_MODEL}.
173 * @see #EXTRA_LANGUAGE_MODEL
174 */
175 public static final String LANGUAGE_MODEL_WEB_SEARCH = "web_search";
176
177 /** Optional text prompt to show to the user when asking them to speak. */
178 public static final String EXTRA_PROMPT = "android.speech.extra.PROMPT";
179
180 /**
Valentin Kravtsov79896bd2010-01-15 11:56:03 +0000181 * Optional IETF language tag (as defined by BCP 47), for example "en-US". This tag informs the
182 * recognizer to perform speech recognition in a language different than the one set in the
183 * {@link java.util.Locale#getDefault()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 */
185 public static final String EXTRA_LANGUAGE = "android.speech.extra.LANGUAGE";
Mike LeBeau216ce0f2011-04-05 16:38:24 -0400186
187 /**
188 * Optional value which can be used to indicate the referer url of a page in which
189 * speech was requested. For example, a web browser may choose to provide this for
190 * uses of speech on a given page.
191 */
192 public static final String EXTRA_ORIGIN = "android.speech.extra.ORIGIN";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
194 /**
195 * Optional limit on the maximum number of results to return. If omitted the recognizer
196 * will choose how many results to return. Must be an integer.
197 */
198 public static final String EXTRA_MAX_RESULTS = "android.speech.extra.MAX_RESULTS";
Mike LeBeaub30023c2010-12-16 04:45:26 -0500199
200 /**
201 * Optional boolean, to be used with {@link #ACTION_WEB_SEARCH}, to indicate whether to
202 * only fire web searches in response to a user's speech. The default is false, meaning
203 * that other types of actions can be taken based on the user's speech.
204 */
205 public static final String EXTRA_WEB_SEARCH_ONLY = "android.speech.extra.WEB_SEARCH_ONLY";
Valentin Kravtsov79896bd2010-01-15 11:56:03 +0000206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 /**
Alex Gruensteincc47fae2010-02-09 14:36:42 -0800208 * Optional boolean to indicate whether partial results should be returned by the recognizer
209 * as the user speaks (default is false). The server may ignore a request for partial
210 * results in some or all cases.
211 */
212 public static final String EXTRA_PARTIAL_RESULTS = "android.speech.extra.PARTIAL_RESULTS";
213
214 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 * When the intent is {@link #ACTION_RECOGNIZE_SPEECH}, the speech input activity will
216 * return results to you via the activity results mechanism. Alternatively, if you use this
217 * extra to supply a PendingIntent, the results will be added to its bundle and the
218 * PendingIntent will be sent to its target.
219 */
220 public static final String EXTRA_RESULTS_PENDINGINTENT =
221 "android.speech.extra.RESULTS_PENDINGINTENT";
Mike LeBeau5a346d62010-02-02 15:28:52 -0800222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 /**
224 * If you use {@link #EXTRA_RESULTS_PENDINGINTENT} to supply a forwarding intent, you can
225 * also use this extra to supply additional extras for the final intent. The search results
226 * will be added to this bundle, and the combined bundle will be sent to the target.
227 */
228 public static final String EXTRA_RESULTS_PENDINGINTENT_BUNDLE =
229 "android.speech.extra.RESULTS_PENDINGINTENT_BUNDLE";
230
231 /** Result code returned when no matches are found for the given speech */
232 public static final int RESULT_NO_MATCH = Activity.RESULT_FIRST_USER;
233 /** Result code returned when there is a generic client error */
234 public static final int RESULT_CLIENT_ERROR = Activity.RESULT_FIRST_USER + 1;
235 /** Result code returned when the recognition server returns an error */
236 public static final int RESULT_SERVER_ERROR = Activity.RESULT_FIRST_USER + 2;
237 /** Result code returned when a network error was encountered */
238 public static final int RESULT_NETWORK_ERROR = Activity.RESULT_FIRST_USER + 3;
239 /** Result code returned when an audio error was encountered */
240 public static final int RESULT_AUDIO_ERROR = Activity.RESULT_FIRST_USER + 4;
241
242 /**
Mike LeBeau5a346d62010-02-02 15:28:52 -0800243 * An ArrayList&lt;String&gt; of the recognition results when performing
Mike LeBeaubb7cf912011-04-05 09:37:30 -0400244 * {@link #ACTION_RECOGNIZE_SPEECH}. Generally this list should be ordered in
245 * descending order of speech recognizer confidence. (See {@link #EXTRA_CONFIDENCE_SCORES}).
246 * Returned in the results; not to be specified in the recognition request. Only present
247 * when {@link Activity#RESULT_OK} is returned in an activity result. In a PendingIntent,
248 * the lack of this extra indicates failure.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 */
250 public static final String EXTRA_RESULTS = "android.speech.extra.RESULTS";
Mike LeBeau79375f72010-02-18 19:01:47 -0800251
252 /**
Mike LeBeaubb7cf912011-04-05 09:37:30 -0400253 * A float array of confidence scores of the recognition results when performing
254 * {@link #ACTION_RECOGNIZE_SPEECH}. The array should be the same size as the ArrayList
255 * returned in {@link #EXTRA_RESULTS}, and should contain values ranging from 0.0 to 1.0,
256 * or -1 to represent an unavailable confidence score.
257 * <p>
258 * Confidence values close to 1.0 indicate high confidence (the speech recognizer is
259 * confident that the recognition result is correct), while values close to 0.0 indicate
260 * low confidence.
261 * <p>
262 * Returned in the results; not to be specified in the recognition request. This extra is
263 * optional and might not be provided. Only present when {@link Activity#RESULT_OK} is
264 * returned in an activity result.
265 */
266 public static final String EXTRA_CONFIDENCE_SCORES = "android.speech.extra.CONFIDENCE_SCORES";
267
268 /**
Mike LeBeau79375f72010-02-18 19:01:47 -0800269 * Returns the broadcast intent to fire with
270 * {@link Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, Bundle)}
271 * to receive details from the package that implements voice search.
272 * <p>
273 * This is based on the value specified by the voice search {@link Activity} in
274 * {@link #DETAILS_META_DATA}, and if this is not specified, will return null. Also if there
275 * is no chosen default to resolve for {@link #ACTION_WEB_SEARCH}, this will return null.
276 * <p>
277 * If an intent is returned and is fired, a {@link Bundle} of extras will be returned to the
278 * provided result receiver, and should ideally contain values for
279 * {@link #EXTRA_LANGUAGE_PREFERENCE} and {@link #EXTRA_SUPPORTED_LANGUAGES}.
280 * <p>
281 * (Whether these are actually provided is up to the particular implementation. It is
282 * recommended that {@link Activity}s implementing {@link #ACTION_WEB_SEARCH} provide this
283 * information, but it is not required.)
284 *
285 * @param context a context object
286 * @return the broadcast intent to fire or null if not available
287 */
288 public static final Intent getVoiceDetailsIntent(Context context) {
289 Intent voiceSearchIntent = new Intent(ACTION_WEB_SEARCH);
290 ResolveInfo ri = context.getPackageManager().resolveActivity(
291 voiceSearchIntent, PackageManager.GET_META_DATA);
292 if (ri == null || ri.activityInfo == null || ri.activityInfo.metaData == null) return null;
293
294 String className = ri.activityInfo.metaData.getString(DETAILS_META_DATA);
295 if (className == null) return null;
296
297 Intent detailsIntent = new Intent(ACTION_GET_LANGUAGE_DETAILS);
298 detailsIntent.setComponent(new ComponentName(ri.activityInfo.packageName, className));
299 return detailsIntent;
300 }
301
302 /**
303 * Meta-data name under which an {@link Activity} implementing {@link #ACTION_WEB_SEARCH} can
304 * use to expose the class name of a {@link BroadcastReceiver} which can respond to request for
305 * more information, from any of the broadcast intents specified in this class.
306 * <p>
307 * Broadcast intents can be directed to the class name specified in the meta-data by creating
308 * an {@link Intent}, setting the component with
309 * {@link Intent#setComponent(android.content.ComponentName)}, and using
310 * {@link Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, android.os.Bundle)}
311 * with another {@link BroadcastReceiver} which can receive the results.
312 * <p>
313 * The {@link #getVoiceDetailsIntent(Context)} method is provided as a convenience to create
314 * a broadcast intent based on the value of this meta-data, if available.
315 * <p>
316 * This is optional and not all {@link Activity}s which implement {@link #ACTION_WEB_SEARCH}
317 * are required to implement this. Thus retrieving this meta-data may be null.
318 */
319 public static final String DETAILS_META_DATA = "android.speech.DETAILS";
320
321 /**
322 * A broadcast intent which can be fired to the {@link BroadcastReceiver} component specified
323 * in the meta-data defined in the {@link #DETAILS_META_DATA} meta-data of an
324 * {@link Activity} satisfying {@link #ACTION_WEB_SEARCH}.
325 * <p>
326 * When fired with
327 * {@link Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, android.os.Bundle)},
328 * a {@link Bundle} of extras will be returned to the provided result receiver, and should
329 * ideally contain values for {@link #EXTRA_LANGUAGE_PREFERENCE} and
330 * {@link #EXTRA_SUPPORTED_LANGUAGES}.
331 * <p>
332 * (Whether these are actually provided is up to the particular implementation. It is
333 * recommended that {@link Activity}s implementing {@link #ACTION_WEB_SEARCH} provide this
334 * information, but it is not required.)
335 */
336 public static final String ACTION_GET_LANGUAGE_DETAILS =
337 "android.speech.action.GET_LANGUAGE_DETAILS";
338
339 /**
Mike LeBeau340acd82010-02-26 11:46:00 -0800340 * Specify this boolean extra in a broadcast of {@link #ACTION_GET_LANGUAGE_DETAILS} to
341 * indicate that only the current language preference is needed in the response. This
342 * avoids any additional computation if all you need is {@link #EXTRA_LANGUAGE_PREFERENCE}
343 * in the response.
344 */
345 public static final String EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE =
346 "android.speech.extra.ONLY_RETURN_LANGUAGE_PREFERENCE";
347
348 /**
Mike LeBeau79375f72010-02-18 19:01:47 -0800349 * The key to the extra in the {@link Bundle} returned by {@link #ACTION_GET_LANGUAGE_DETAILS}
350 * which is a {@link String} that represents the current language preference this user has
351 * specified - a locale string like "en-US".
352 */
353 public static final String EXTRA_LANGUAGE_PREFERENCE =
354 "android.speech.extra.LANGUAGE_PREFERENCE";
355
356 /**
357 * The key to the extra in the {@link Bundle} returned by {@link #ACTION_GET_LANGUAGE_DETAILS}
358 * which is an {@link ArrayList} of {@link String}s that represents the languages supported by
359 * this implementation of voice recognition - a list of strings like "en-US", "cmn-Hans-CN",
360 * etc.
361 */
362 public static final String EXTRA_SUPPORTED_LANGUAGES =
363 "android.speech.extra.SUPPORTED_LANGUAGES";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364}