RecognizerIntent.java revision 79375f761922b208e2e50ff13a63552c9d01567b
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
17package android.speech;
18
19import java.util.ArrayList;
20
21import android.app.Activity;
22import android.content.ActivityNotFoundException;
23import android.content.BroadcastReceiver;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
27import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
29import android.os.Bundle;
30
31/**
32 * Constants for supporting speech recognition through starting an {@link Intent}
33 */
34public class RecognizerIntent {
35    private RecognizerIntent() {
36        // Not for instantiating.
37    }
38
39    /**
40     * Starts an activity that will prompt the user for speech and sends it through a
41     * speech recognizer.  The results will be returned via activity results (in
42     * {@link Activity#onActivityResult}, if you start the intent using
43     * {@link Activity#startActivityForResult(Intent, int)}), or forwarded via a PendingIntent
44     * if one is provided.
45     *
46     * <p>Starting this intent with just {@link Activity#startActivity(Intent)} is not supported.
47     * You must either use {@link Activity#startActivityForResult(Intent, int)}, or provide a
48     * PendingIntent, to receive recognition results.
49     *
50     * <p>Required extras:
51     * <ul>
52     *   <li>{@link #EXTRA_LANGUAGE_MODEL}
53     * </ul>
54     *
55     * <p>Optional extras:
56     * <ul>
57     *   <li>{@link #EXTRA_PROMPT}
58     *   <li>{@link #EXTRA_LANGUAGE}
59     *   <li>{@link #EXTRA_MAX_RESULTS}
60     *   <li>{@link #EXTRA_RESULTS_PENDINGINTENT}
61     *   <li>{@link #EXTRA_RESULTS_PENDINGINTENT_BUNDLE}
62     * </ul>
63     *
64     * <p> Result extras (returned in the result, not to be specified in the request):
65     * <ul>
66     *   <li>{@link #EXTRA_RESULTS}
67     * </ul>
68     *
69     * <p>NOTE: There may not be any applications installed to handle this action, so you should
70     * make sure to catch {@link ActivityNotFoundException}.
71     */
72    public static final String ACTION_RECOGNIZE_SPEECH = "android.speech.action.RECOGNIZE_SPEECH";
73
74    /**
75     * Starts an activity that will prompt the user for speech, sends it through a
76     * speech recognizer, and invokes and displays a web search result.
77     *
78     * <p>Required extras:
79     * <ul>
80     *   <li>{@link #EXTRA_LANGUAGE_MODEL}
81     * </ul>
82     *
83     * <p>Optional extras:
84     * <ul>
85     *   <li>{@link #EXTRA_PROMPT}
86     *   <li>{@link #EXTRA_LANGUAGE}
87     *   <li>{@link #EXTRA_MAX_RESULTS}
88     *   <li>{@link #EXTRA_PARTIAL_RESULTS}
89     * </ul>
90     *
91     * <p> Result extras (returned in the result, not to be specified in the request):
92     * <ul>
93     *   <li>{@link #EXTRA_RESULTS}
94     * </ul>
95     *
96     * <p>NOTE: There may not be any applications installed to handle this action, so you should
97     * make sure to catch {@link ActivityNotFoundException}.
98     */
99    public static final String ACTION_WEB_SEARCH = "android.speech.action.WEB_SEARCH";
100
101    /**
102     * The minimum length of an utterance. We will not stop recording before this amount of time.
103     *
104     * Note that it is extremely rare you'd want to specify this value in an intent. If you don't
105     * have a very good reason to change these, you should leave them as they are. Note also that
106     * certain values may cause undesired or unexpected results - use judiciously! Additionally,
107     * depending on the recognizer implementation, these values may have no effect.
108     */
109    public static final String EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS =
110            "android.speech.extras.SPEECH_INPUT_MINIMUM_LENGTH_MILLIS";
111
112    /**
113     * The amount of time that it should take after we stop hearing speech to consider the input
114     * complete.
115     *
116     * Note that it is extremely rare you'd want to specify this value in an intent. If
117     * you don't have a very good reason to change these, you should leave them as they are. Note
118     * also that certain values may cause undesired or unexpected results - use judiciously!
119     * Additionally, depending on the recognizer implementation, these values may have no effect.
120     */
121    public static final String EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS =
122            "android.speech.extras.SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS";
123
124    /**
125     * The amount of time that it should take after we stop hearing speech to consider the input
126     * possibly complete. This is used to prevent the endpointer cutting off during very short
127     * mid-speech pauses.
128     *
129     * Note that it is extremely rare you'd want to specify this value in an intent. If
130     * you don't have a very good reason to change these, you should leave them as they are. Note
131     * also that certain values may cause undesired or unexpected results - use judiciously!
132     * Additionally, depending on the recognizer implementation, these values may have no effect.
133     */
134    public static final String EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS =
135            "android.speech.extras.SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS";
136
137    /**
138     * Informs the recognizer which speech model to prefer when performing
139     * {@link #ACTION_RECOGNIZE_SPEECH}. The recognizer uses this
140     * information to fine tune the results. This extra is required. Activities implementing
141     * {@link #ACTION_RECOGNIZE_SPEECH} may interpret the values as they see fit.
142     *
143     *  @see #LANGUAGE_MODEL_FREE_FORM
144     *  @see #LANGUAGE_MODEL_WEB_SEARCH
145     */
146    public static final String EXTRA_LANGUAGE_MODEL = "android.speech.extra.LANGUAGE_MODEL";
147
148    /**
149     * Use a language model based on free-form speech recognition.  This is a value to use for
150     * {@link #EXTRA_LANGUAGE_MODEL}.
151     * @see #EXTRA_LANGUAGE_MODEL
152     */
153    public static final String LANGUAGE_MODEL_FREE_FORM = "free_form";
154    /**
155     * Use a language model based on web search terms.  This is a value to use for
156     * {@link #EXTRA_LANGUAGE_MODEL}.
157     * @see #EXTRA_LANGUAGE_MODEL
158     */
159    public static final String LANGUAGE_MODEL_WEB_SEARCH = "web_search";
160
161    /** Optional text prompt to show to the user when asking them to speak. */
162    public static final String EXTRA_PROMPT = "android.speech.extra.PROMPT";
163
164    /**
165     * Optional IETF language tag (as defined by BCP 47), for example "en-US". This tag informs the
166     * recognizer to perform speech recognition in a language different than the one set in the
167     * {@link java.util.Locale#getDefault()}.
168     */
169    public static final String EXTRA_LANGUAGE = "android.speech.extra.LANGUAGE";
170
171    /**
172     * Optional limit on the maximum number of results to return. If omitted the recognizer
173     * will choose how many results to return. Must be an integer.
174     */
175    public static final String EXTRA_MAX_RESULTS = "android.speech.extra.MAX_RESULTS";
176
177    /**
178     * Optional boolean to indicate whether partial results should be returned by the recognizer
179     * as the user speaks (default is false).  The server may ignore a request for partial
180     * results in some or all cases.
181     */
182    public static final String EXTRA_PARTIAL_RESULTS = "android.speech.extra.PARTIAL_RESULTS";
183
184    /**
185     * When the intent is {@link #ACTION_RECOGNIZE_SPEECH}, the speech input activity will
186     * return results to you via the activity results mechanism.  Alternatively, if you use this
187     * extra to supply a PendingIntent, the results will be added to its bundle and the
188     * PendingIntent will be sent to its target.
189     */
190    public static final String EXTRA_RESULTS_PENDINGINTENT =
191            "android.speech.extra.RESULTS_PENDINGINTENT";
192
193    /**
194     * If you use {@link #EXTRA_RESULTS_PENDINGINTENT} to supply a forwarding intent, you can
195     * also use this extra to supply additional extras for the final intent.  The search results
196     * will be added to this bundle, and the combined bundle will be sent to the target.
197     */
198    public static final String EXTRA_RESULTS_PENDINGINTENT_BUNDLE =
199            "android.speech.extra.RESULTS_PENDINGINTENT_BUNDLE";
200
201    /** Result code returned when no matches are found for the given speech */
202    public static final int RESULT_NO_MATCH = Activity.RESULT_FIRST_USER;
203    /** Result code returned when there is a generic client error */
204    public static final int RESULT_CLIENT_ERROR = Activity.RESULT_FIRST_USER + 1;
205    /** Result code returned when the recognition server returns an error */
206    public static final int RESULT_SERVER_ERROR = Activity.RESULT_FIRST_USER + 2;
207    /** Result code returned when a network error was encountered */
208    public static final int RESULT_NETWORK_ERROR = Activity.RESULT_FIRST_USER + 3;
209    /** Result code returned when an audio error was encountered */
210    public static final int RESULT_AUDIO_ERROR = Activity.RESULT_FIRST_USER + 4;
211
212    /**
213     * An ArrayList&lt;String&gt; of the recognition results when performing
214     * {@link #ACTION_RECOGNIZE_SPEECH}. Returned in the results; not to be specified in the
215     * recognition request. Only present when {@link Activity#RESULT_OK} is returned in
216     * an activity result. In a PendingIntent, the lack of this extra indicates failure.
217     */
218    public static final String EXTRA_RESULTS = "android.speech.extra.RESULTS";
219
220    /**
221     * Returns the broadcast intent to fire with
222     * {@link Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, Bundle)}
223     * to receive details from the package that implements voice search.
224     * <p>
225     * This is based on the value specified by the voice search {@link Activity} in
226     * {@link #DETAILS_META_DATA}, and if this is not specified, will return null. Also if there
227     * is no chosen default to resolve for {@link #ACTION_WEB_SEARCH}, this will return null.
228     * <p>
229     * If an intent is returned and is fired, a {@link Bundle} of extras will be returned to the
230     * provided result receiver, and should ideally contain values for
231     * {@link #EXTRA_LANGUAGE_PREFERENCE} and {@link #EXTRA_SUPPORTED_LANGUAGES}.
232     * <p>
233     * (Whether these are actually provided is up to the particular implementation. It is
234     * recommended that {@link Activity}s implementing {@link #ACTION_WEB_SEARCH} provide this
235     * information, but it is not required.)
236     *
237     * @param context a context object
238     * @return the broadcast intent to fire or null if not available
239     */
240    public static final Intent getVoiceDetailsIntent(Context context) {
241        Intent voiceSearchIntent = new Intent(ACTION_WEB_SEARCH);
242        ResolveInfo ri = context.getPackageManager().resolveActivity(
243                voiceSearchIntent, PackageManager.GET_META_DATA);
244        if (ri == null || ri.activityInfo == null || ri.activityInfo.metaData == null) return null;
245
246        String className = ri.activityInfo.metaData.getString(DETAILS_META_DATA);
247        if (className == null) return null;
248
249        Intent detailsIntent = new Intent(ACTION_GET_LANGUAGE_DETAILS);
250        detailsIntent.setComponent(new ComponentName(ri.activityInfo.packageName, className));
251        return detailsIntent;
252    }
253
254    /**
255     * Meta-data name under which an {@link Activity} implementing {@link #ACTION_WEB_SEARCH} can
256     * use to expose the class name of a {@link BroadcastReceiver} which can respond to request for
257     * more information, from any of the broadcast intents specified in this class.
258     * <p>
259     * Broadcast intents can be directed to the class name specified in the meta-data by creating
260     * an {@link Intent}, setting the component with
261     * {@link Intent#setComponent(android.content.ComponentName)}, and using
262     * {@link Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, android.os.Bundle)}
263     * with another {@link BroadcastReceiver} which can receive the results.
264     * <p>
265     * The {@link #getVoiceDetailsIntent(Context)} method is provided as a convenience to create
266     * a broadcast intent based on the value of this meta-data, if available.
267     * <p>
268     * This is optional and not all {@link Activity}s which implement {@link #ACTION_WEB_SEARCH}
269     * are required to implement this. Thus retrieving this meta-data may be null.
270     */
271    public static final String DETAILS_META_DATA = "android.speech.DETAILS";
272
273    /**
274     * A broadcast intent which can be fired to the {@link BroadcastReceiver} component specified
275     * in the meta-data defined in the {@link #DETAILS_META_DATA} meta-data of an
276     * {@link Activity} satisfying {@link #ACTION_WEB_SEARCH}.
277     * <p>
278     * When fired with
279     * {@link Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, android.os.Bundle)},
280     * a {@link Bundle} of extras will be returned to the provided result receiver, and should
281     * ideally contain values for {@link #EXTRA_LANGUAGE_PREFERENCE} and
282     * {@link #EXTRA_SUPPORTED_LANGUAGES}.
283     * <p>
284     * (Whether these are actually provided is up to the particular implementation. It is
285     * recommended that {@link Activity}s implementing {@link #ACTION_WEB_SEARCH} provide this
286     * information, but it is not required.)
287     */
288    public static final String ACTION_GET_LANGUAGE_DETAILS =
289            "android.speech.action.GET_LANGUAGE_DETAILS";
290
291    /**
292     * The key to the extra in the {@link Bundle} returned by {@link #ACTION_GET_LANGUAGE_DETAILS}
293     * which is a {@link String} that represents the current language preference this user has
294     * specified - a locale string like "en-US".
295     */
296    public static final String EXTRA_LANGUAGE_PREFERENCE =
297            "android.speech.extra.LANGUAGE_PREFERENCE";
298
299    /**
300     * The key to the extra in the {@link Bundle} returned by {@link #ACTION_GET_LANGUAGE_DETAILS}
301     * which is an {@link ArrayList} of {@link String}s that represents the languages supported by
302     * this implementation of voice recognition - a list of strings like "en-US", "cmn-Hans-CN",
303     * etc.
304     */
305    public static final String EXTRA_SUPPORTED_LANGUAGES =
306            "android.speech.extra.SUPPORTED_LANGUAGES";
307}
308