• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package android.speech;
18 
19 import android.app.Activity;
20 import android.content.ActivityNotFoundException;
21 import android.content.BroadcastReceiver;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.PackageManager;
26 import android.content.pm.ResolveInfo;
27 import android.os.Bundle;
28 
29 import java.util.ArrayList;
30 
31 /**
32  * Constants for supporting speech recognition through starting an {@link Intent}
33  */
34 public class RecognizerIntent {
35     /**
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 static final String EXTRA_CALLING_PACKAGE = "calling_package";
43 
44     /**
45      * The extra key used in an intent which is providing an already opened audio source for the
46      * RecognitionService to use. Data should be a URI to an audio resource.
47      */
48     public static final String EXTRA_AUDIO_INJECT_SOURCE =
49             "android.speech.extra.AUDIO_INJECT_SOURCE";
50 
RecognizerIntent()51     private RecognizerIntent() {
52         // Not for instantiating.
53     }
54 
55     /**
56      * Starts an activity that will prompt the user for speech and send it through a
57      * speech recognizer.  The results will be returned via activity results (in
58      * {@link Activity#onActivityResult}, if you start the intent using
59      * {@link Activity#startActivityForResult(Intent, int)}), or forwarded via a PendingIntent
60      * if one is provided.
61      *
62      * <p>Starting this intent with just {@link Activity#startActivity(Intent)} is not supported.
63      * You must either use {@link Activity#startActivityForResult(Intent, int)}, or provide a
64      * PendingIntent, to receive recognition results.
65      *
66      * <p>The implementation of this API is likely to stream audio to remote servers to perform
67      * speech recognition which can use a substantial amount of bandwidth.
68      *
69      * <p>Required extras:
70      * <ul>
71      *   <li>{@link #EXTRA_LANGUAGE_MODEL}
72      * </ul>
73      *
74      * <p>Optional extras:
75      * <ul>
76      *   <li>{@link #EXTRA_PROMPT}
77      *   <li>{@link #EXTRA_LANGUAGE}
78      *   <li>{@link #EXTRA_MAX_RESULTS}
79      *   <li>{@link #EXTRA_RESULTS_PENDINGINTENT}
80      *   <li>{@link #EXTRA_RESULTS_PENDINGINTENT_BUNDLE}
81      * </ul>
82      *
83      * <p> Result extras (returned in the result, not to be specified in the request):
84      * <ul>
85      *   <li>{@link #EXTRA_RESULTS}
86      * </ul>
87      *
88      * <p>NOTE: There may not be any applications installed to handle this action, so you should
89      * make sure to catch {@link ActivityNotFoundException}.
90      */
91     public static final String ACTION_RECOGNIZE_SPEECH = "android.speech.action.RECOGNIZE_SPEECH";
92 
93     /**
94      * Starts an activity that will prompt the user for speech, send it through a
95      * speech recognizer, and either display a web search result or trigger
96      * another type of action based on the user's speech.
97      *
98      * <p>If you want to avoid triggering any type of action besides web search, you can use
99      * the {@link #EXTRA_WEB_SEARCH_ONLY} extra.
100      *
101      * <p>Required extras:
102      * <ul>
103      *   <li>{@link #EXTRA_LANGUAGE_MODEL}
104      * </ul>
105      *
106      * <p>Optional extras:
107      * <ul>
108      *   <li>{@link #EXTRA_PROMPT}
109      *   <li>{@link #EXTRA_LANGUAGE}
110      *   <li>{@link #EXTRA_MAX_RESULTS}
111      *   <li>{@link #EXTRA_PARTIAL_RESULTS}
112      *   <li>{@link #EXTRA_WEB_SEARCH_ONLY}
113      *   <li>{@link #EXTRA_ORIGIN}
114      * </ul>
115      *
116      * <p> Result extras (returned in the result, not to be specified in the request):
117      * <ul>
118      *   <li>{@link #EXTRA_RESULTS}
119      *   <li>{@link #EXTRA_CONFIDENCE_SCORES} (optional)
120      * </ul>
121      *
122      * <p>NOTE: There may not be any applications installed to handle this action, so you should
123      * make sure to catch {@link ActivityNotFoundException}.
124      */
125     public static final String ACTION_WEB_SEARCH = "android.speech.action.WEB_SEARCH";
126 
127     /**
128      * Starts an activity that will prompt the user for speech without requiring the user's
129      * visual attention or touch input. It will send it through a speech recognizer,
130      * and either synthesize speech for a web search result or trigger
131      * another type of action based on the user's speech.
132      *
133      * This activity may be launched while device is locked in a secure mode.
134      * Special care must be taken to ensure that the voice actions that are performed while
135      * hands free cannot compromise the device's security.
136      * The activity should check the value of the {@link #EXTRA_SECURE} extra to determine
137      * whether the device has been securely locked. If so, the activity should either restrict
138      * the set of voice actions that are permitted or require some form of secure
139      * authentication before proceeding.
140      *
141      * To ensure that the activity's user interface is visible while the lock screen is showing,
142      * the activity should set the
143      * {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED} window flag.
144      * Otherwise the activity's user interface may be hidden by the lock screen. The activity
145      * should take care not to leak private information when the device is securely locked.
146      *
147      * <p>Optional extras:
148      * <ul>
149      *   <li>{@link #EXTRA_SECURE}
150      * </ul>
151      *
152      * <p class="note">
153      * In some cases, a matching Activity may not exist, so ensure you
154      * safeguard against this.
155      */
156     public static final String ACTION_VOICE_SEARCH_HANDS_FREE =
157             "android.speech.action.VOICE_SEARCH_HANDS_FREE";
158 
159     /**
160      * Optional boolean to indicate that a "hands free" voice search was performed while the device
161      * was in a secure mode. An example of secure mode is when the device's screen lock is active,
162      * and it requires some form of authentication to be unlocked.
163      *
164      * When the device is securely locked, the voice search activity should either restrict
165      * the set of voice actions that are permitted, or require some form of secure authentication
166      * before proceeding.
167      */
168     public static final String EXTRA_SECURE = "android.speech.extras.EXTRA_SECURE";
169 
170     /**
171      * The minimum length of an utterance. We will not stop recording before this amount of time.
172      *
173      * Note that it is extremely rare you'd want to specify this value in an intent. If you don't
174      * have a very good reason to change these, you should leave them as they are. Note also that
175      * certain values may cause undesired or unexpected results - use judiciously! Additionally,
176      * depending on the recognizer implementation, these values may have no effect.
177      */
178     public static final String EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS =
179             "android.speech.extras.SPEECH_INPUT_MINIMUM_LENGTH_MILLIS";
180 
181     /**
182      * The amount of time that it should take after we stop hearing speech to consider the input
183      * complete.
184      *
185      * Note that it is extremely rare you'd want to specify this value in an intent. If
186      * you don't have a very good reason to change these, you should leave them as they are. Note
187      * also that certain values may cause undesired or unexpected results - use judiciously!
188      * Additionally, depending on the recognizer implementation, these values may have no effect.
189      */
190     public static final String EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS =
191             "android.speech.extras.SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS";
192 
193     /**
194      * The amount of time that it should take after we stop hearing speech to consider the input
195      * possibly complete. This is used to prevent the endpointer cutting off during very short
196      * mid-speech pauses.
197      *
198      * Note that it is extremely rare you'd want to specify this value in an intent. If
199      * you don't have a very good reason to change these, you should leave them as they are. Note
200      * also that certain values may cause undesired or unexpected results - use judiciously!
201      * Additionally, depending on the recognizer implementation, these values may have no effect.
202      */
203     public static final String EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS =
204             "android.speech.extras.SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS";
205 
206     /**
207      * Informs the recognizer which speech model to prefer when performing
208      * {@link #ACTION_RECOGNIZE_SPEECH}. The recognizer uses this
209      * information to fine tune the results. This extra is required. Activities implementing
210      * {@link #ACTION_RECOGNIZE_SPEECH} may interpret the values as they see fit.
211      *
212      *  @see #LANGUAGE_MODEL_FREE_FORM
213      *  @see #LANGUAGE_MODEL_WEB_SEARCH
214      */
215     public static final String EXTRA_LANGUAGE_MODEL = "android.speech.extra.LANGUAGE_MODEL";
216 
217     /**
218      * Use a language model based on free-form speech recognition.  This is a value to use for
219      * {@link #EXTRA_LANGUAGE_MODEL}.
220      * @see #EXTRA_LANGUAGE_MODEL
221      */
222     public static final String LANGUAGE_MODEL_FREE_FORM = "free_form";
223     /**
224      * Use a language model based on web search terms.  This is a value to use for
225      * {@link #EXTRA_LANGUAGE_MODEL}.
226      * @see #EXTRA_LANGUAGE_MODEL
227      */
228     public static final String LANGUAGE_MODEL_WEB_SEARCH = "web_search";
229 
230     /** Optional text prompt to show to the user when asking them to speak. */
231     public static final String EXTRA_PROMPT = "android.speech.extra.PROMPT";
232 
233     /**
234      * Optional IETF language tag (as defined by BCP 47), for example "en-US". This tag informs the
235      * recognizer to perform speech recognition in a language different than the one set in the
236      * {@link java.util.Locale#getDefault()}.
237      */
238     public static final String EXTRA_LANGUAGE = "android.speech.extra.LANGUAGE";
239 
240     /**
241      * Optional value which can be used to indicate the referer url of a page in which
242      * speech was requested. For example, a web browser may choose to provide this for
243      * uses of speech on a given page.
244      */
245     public static final String EXTRA_ORIGIN = "android.speech.extra.ORIGIN";
246 
247     /**
248      * Optional limit on the maximum number of results to return. If omitted the recognizer
249      * will choose how many results to return. Must be an integer.
250      */
251     public static final String EXTRA_MAX_RESULTS = "android.speech.extra.MAX_RESULTS";
252 
253     /**
254      * Optional boolean, to be used with {@link #ACTION_WEB_SEARCH}, to indicate whether to
255      * only fire web searches in response to a user's speech. The default is false, meaning
256      * that other types of actions can be taken based on the user's speech.
257      */
258     public static final String EXTRA_WEB_SEARCH_ONLY = "android.speech.extra.WEB_SEARCH_ONLY";
259 
260     /**
261      * Optional boolean to indicate whether partial results should be returned by the recognizer
262      * as the user speaks (default is false).  The server may ignore a request for partial
263      * results in some or all cases.
264      */
265     public static final String EXTRA_PARTIAL_RESULTS = "android.speech.extra.PARTIAL_RESULTS";
266 
267     /**
268      * When the intent is {@link #ACTION_RECOGNIZE_SPEECH}, the speech input activity will
269      * return results to you via the activity results mechanism.  Alternatively, if you use this
270      * extra to supply a PendingIntent, the results will be added to its bundle and the
271      * PendingIntent will be sent to its target.
272      */
273     public static final String EXTRA_RESULTS_PENDINGINTENT =
274             "android.speech.extra.RESULTS_PENDINGINTENT";
275 
276     /**
277      * If you use {@link #EXTRA_RESULTS_PENDINGINTENT} to supply a forwarding intent, you can
278      * also use this extra to supply additional extras for the final intent.  The search results
279      * will be added to this bundle, and the combined bundle will be sent to the target.
280      */
281     public static final String EXTRA_RESULTS_PENDINGINTENT_BUNDLE =
282             "android.speech.extra.RESULTS_PENDINGINTENT_BUNDLE";
283 
284     /** Result code returned when no matches are found for the given speech */
285     public static final int RESULT_NO_MATCH = Activity.RESULT_FIRST_USER;
286     /** Result code returned when there is a generic client error */
287     public static final int RESULT_CLIENT_ERROR = Activity.RESULT_FIRST_USER + 1;
288     /** Result code returned when the recognition server returns an error */
289     public static final int RESULT_SERVER_ERROR = Activity.RESULT_FIRST_USER + 2;
290     /** Result code returned when a network error was encountered */
291     public static final int RESULT_NETWORK_ERROR = Activity.RESULT_FIRST_USER + 3;
292     /** Result code returned when an audio error was encountered */
293     public static final int RESULT_AUDIO_ERROR = Activity.RESULT_FIRST_USER + 4;
294 
295     /**
296      * An ArrayList&lt;String&gt; of the recognition results when performing
297      * {@link #ACTION_RECOGNIZE_SPEECH}. Generally this list should be ordered in
298      * descending order of speech recognizer confidence. (See {@link #EXTRA_CONFIDENCE_SCORES}).
299      * Returned in the results; not to be specified in the recognition request. Only present
300      * when {@link Activity#RESULT_OK} is returned in an activity result. In a PendingIntent,
301      * the lack of this extra indicates failure.
302      */
303     public static final String EXTRA_RESULTS = "android.speech.extra.RESULTS";
304 
305     /**
306      * A float array of confidence scores of the recognition results when performing
307      * {@link #ACTION_RECOGNIZE_SPEECH}. The array should be the same size as the ArrayList
308      * returned in {@link #EXTRA_RESULTS}, and should contain values ranging from 0.0 to 1.0,
309      * or -1 to represent an unavailable confidence score.
310      * <p>
311      * Confidence values close to 1.0 indicate high confidence (the speech recognizer is
312      * confident that the recognition result is correct), while values close to 0.0 indicate
313      * low confidence.
314      * <p>
315      * Returned in the results; not to be specified in the recognition request. This extra is
316      * optional and might not be provided. Only present when {@link Activity#RESULT_OK} is
317      * returned in an activity result.
318      */
319     public static final String EXTRA_CONFIDENCE_SCORES = "android.speech.extra.CONFIDENCE_SCORES";
320 
321     /**
322      * Returns the broadcast intent to fire with
323      * {@link Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, Bundle)}
324      * to receive details from the package that implements voice search.
325      * <p>
326      * This is based on the value specified by the voice search {@link Activity} in
327      * {@link #DETAILS_META_DATA}, and if this is not specified, will return null. Also if there
328      * is no chosen default to resolve for {@link #ACTION_WEB_SEARCH}, this will return null.
329      * <p>
330      * If an intent is returned and is fired, a {@link Bundle} of extras will be returned to the
331      * provided result receiver, and should ideally contain values for
332      * {@link #EXTRA_LANGUAGE_PREFERENCE} and {@link #EXTRA_SUPPORTED_LANGUAGES}.
333      * <p>
334      * (Whether these are actually provided is up to the particular implementation. It is
335      * recommended that {@link Activity}s implementing {@link #ACTION_WEB_SEARCH} provide this
336      * information, but it is not required.)
337      *
338      * @param context a context object
339      * @return the broadcast intent to fire or null if not available
340      */
getVoiceDetailsIntent(Context context)341     public static final Intent getVoiceDetailsIntent(Context context) {
342         Intent voiceSearchIntent = new Intent(ACTION_WEB_SEARCH);
343         ResolveInfo ri = context.getPackageManager().resolveActivity(
344                 voiceSearchIntent, PackageManager.GET_META_DATA);
345         if (ri == null || ri.activityInfo == null || ri.activityInfo.metaData == null) return null;
346 
347         String className = ri.activityInfo.metaData.getString(DETAILS_META_DATA);
348         if (className == null) return null;
349 
350         Intent detailsIntent = new Intent(ACTION_GET_LANGUAGE_DETAILS);
351         detailsIntent.setComponent(new ComponentName(ri.activityInfo.packageName, className));
352         return detailsIntent;
353     }
354 
355     /**
356      * Meta-data name under which an {@link Activity} implementing {@link #ACTION_WEB_SEARCH} can
357      * use to expose the class name of a {@link BroadcastReceiver} which can respond to request for
358      * more information, from any of the broadcast intents specified in this class.
359      * <p>
360      * Broadcast intents can be directed to the class name specified in the meta-data by creating
361      * an {@link Intent}, setting the component with
362      * {@link Intent#setComponent(android.content.ComponentName)}, and using
363      * {@link Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, android.os.Bundle)}
364      * with another {@link BroadcastReceiver} which can receive the results.
365      * <p>
366      * The {@link #getVoiceDetailsIntent(Context)} method is provided as a convenience to create
367      * a broadcast intent based on the value of this meta-data, if available.
368      * <p>
369      * This is optional and not all {@link Activity}s which implement {@link #ACTION_WEB_SEARCH}
370      * are required to implement this. Thus retrieving this meta-data may be null.
371      */
372     public static final String DETAILS_META_DATA = "android.speech.DETAILS";
373 
374     /**
375      * A broadcast intent which can be fired to the {@link BroadcastReceiver} component specified
376      * in the meta-data defined in the {@link #DETAILS_META_DATA} meta-data of an
377      * {@link Activity} satisfying {@link #ACTION_WEB_SEARCH}.
378      * <p>
379      * When fired with
380      * {@link Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, android.os.Bundle)},
381      * a {@link Bundle} of extras will be returned to the provided result receiver, and should
382      * ideally contain values for {@link #EXTRA_LANGUAGE_PREFERENCE} and
383      * {@link #EXTRA_SUPPORTED_LANGUAGES}.
384      * <p>
385      * (Whether these are actually provided is up to the particular implementation. It is
386      * recommended that {@link Activity}s implementing {@link #ACTION_WEB_SEARCH} provide this
387      * information, but it is not required.)
388      */
389     public static final String ACTION_GET_LANGUAGE_DETAILS =
390             "android.speech.action.GET_LANGUAGE_DETAILS";
391 
392     /**
393      * Specify this boolean extra in a broadcast of {@link #ACTION_GET_LANGUAGE_DETAILS} to
394      * indicate that only the current language preference is needed in the response. This
395      * avoids any additional computation if all you need is {@link #EXTRA_LANGUAGE_PREFERENCE}
396      * in the response.
397      */
398     public static final String EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE =
399             "android.speech.extra.ONLY_RETURN_LANGUAGE_PREFERENCE";
400 
401     /**
402      * The key to the extra in the {@link Bundle} returned by {@link #ACTION_GET_LANGUAGE_DETAILS}
403      * which is a {@link String} that represents the current language preference this user has
404      * specified - a locale string like "en-US".
405      */
406     public static final String EXTRA_LANGUAGE_PREFERENCE =
407             "android.speech.extra.LANGUAGE_PREFERENCE";
408 
409     /**
410      * The key to the extra in the {@link Bundle} returned by {@link #ACTION_GET_LANGUAGE_DETAILS}
411      * which is an {@link ArrayList} of {@link String}s that represents the languages supported by
412      * this implementation of voice recognition - a list of strings like "en-US", "cmn-Hans-CN",
413      * etc.
414      */
415     public static final String EXTRA_SUPPORTED_LANGUAGES =
416             "android.speech.extra.SUPPORTED_LANGUAGES";
417 
418     /**
419      * Optional boolean, to be used with {@link #ACTION_RECOGNIZE_SPEECH},
420      * {@link #ACTION_VOICE_SEARCH_HANDS_FREE}, {@link #ACTION_WEB_SEARCH} to indicate whether to
421      * only use an offline speech recognition engine. The default is false, meaning that either
422      * network or offline recognition engines may be used.
423      *
424      * <p>Depending on the recognizer implementation, these values may have
425      * no effect.</p>
426      *
427      */
428     public static final String EXTRA_PREFER_OFFLINE = "android.speech.extra.PREFER_OFFLINE";
429 }
430