RecognizerIntent.java revision cc47fae688e55edefd0abd15811d674ba47889e0
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 android.app.Activity;
20import android.content.ActivityNotFoundException;
21import android.content.Intent;
22
23/**
24 * Constants for supporting speech recognition through starting an {@link Intent}
25 */
26public class RecognizerIntent {
27    private RecognizerIntent() {
28        // Not for instantiating.
29    }
30
31    /**
32     * Starts an activity that will prompt the user for speech and sends it through a
33     * speech recognizer.  The results will be returned via activity results (in
34     * {@link Activity#onActivityResult}, if you start the intent using
35     * {@link Activity#startActivityForResult(Intent, int)}), or forwarded via a PendingIntent
36     * if one is provided.
37     *
38     * <p>Starting this intent with just {@link Activity#startActivity(Intent)} is not supported.
39     * You must either use {@link Activity#startActivityForResult(Intent, int)}, or provide a
40     * PendingIntent, to receive recognition results.
41     *
42     * <p>Required extras:
43     * <ul>
44     *   <li>{@link #EXTRA_LANGUAGE_MODEL}
45     * </ul>
46     *
47     * <p>Optional extras:
48     * <ul>
49     *   <li>{@link #EXTRA_PROMPT}
50     *   <li>{@link #EXTRA_LANGUAGE}
51     *   <li>{@link #EXTRA_MAX_RESULTS}
52     *   <li>{@link #EXTRA_RESULTS_PENDINGINTENT}
53     *   <li>{@link #EXTRA_RESULTS_PENDINGINTENT_BUNDLE}
54     * </ul>
55     *
56     * <p> Result extras (returned in the result, not to be specified in the request):
57     * <ul>
58     *   <li>{@link #EXTRA_RESULTS}
59     * </ul>
60     *
61     * <p>NOTE: There may not be any applications installed to handle this action, so you should
62     * make sure to catch {@link ActivityNotFoundException}.
63     */
64    public static final String ACTION_RECOGNIZE_SPEECH = "android.speech.action.RECOGNIZE_SPEECH";
65
66    /**
67     * Starts an activity that will prompt the user for speech, sends it through a
68     * speech recognizer, and invokes and displays a web search result.
69     *
70     * <p>Required extras:
71     * <ul>
72     *   <li>{@link #EXTRA_LANGUAGE_MODEL}
73     * </ul>
74     *
75     * <p>Optional extras:
76     * <ul>
77     *   <li>{@link #EXTRA_PROMPT}
78     *   <li>{@link #EXTRA_LANGUAGE}
79     *   <li>{@link #EXTRA_MAX_RESULTS}
80     *   <li>{@link #EXTRA_PARTIAL_RESULTS}
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_WEB_SEARCH = "android.speech.action.WEB_SEARCH";
92
93    /**
94     * The minimum length of an utterance. We will not stop recording before this amount of time.
95     *
96     * Note that it is extremely rare you'd want to specify this value in an intent. If you don't
97     * have a very good reason to change these, you should leave them as they are. Note also that
98     * certain values may cause undesired or unexpected results - use judiciously! Additionally,
99     * depending on the recognizer implementation, these values may have no effect.
100     */
101    public static final String EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS =
102            "android.speech.extras.SPEECH_INPUT_MINIMUM_LENGTH_MILLIS";
103
104    /**
105     * The amount of time that it should take after we stop hearing speech to consider the input
106     * complete.
107     *
108     * Note that it is extremely rare you'd want to specify this value in an intent. If
109     * you don't have a very good reason to change these, you should leave them as they are. Note
110     * also that certain values may cause undesired or unexpected results - use judiciously!
111     * Additionally, depending on the recognizer implementation, these values may have no effect.
112     */
113    public static final String EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS =
114            "android.speech.extras.SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS";
115
116    /**
117     * The amount of time that it should take after we stop hearing speech to consider the input
118     * possibly complete. This is used to prevent the endpointer cutting off during very short
119     * mid-speech pauses.
120     *
121     * Note that it is extremely rare you'd want to specify this value in an intent. If
122     * you don't have a very good reason to change these, you should leave them as they are. Note
123     * also that certain values may cause undesired or unexpected results - use judiciously!
124     * Additionally, depending on the recognizer implementation, these values may have no effect.
125     */
126    public static final String EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS =
127            "android.speech.extras.SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS";
128
129    /**
130     * Informs the recognizer which speech model to prefer when performing
131     * {@link #ACTION_RECOGNIZE_SPEECH}. The recognizer uses this
132     * information to fine tune the results. This extra is required. Activities implementing
133     * {@link #ACTION_RECOGNIZE_SPEECH} may interpret the values as they see fit.
134     *
135     *  @see #LANGUAGE_MODEL_FREE_FORM
136     *  @see #LANGUAGE_MODEL_WEB_SEARCH
137     */
138    public static final String EXTRA_LANGUAGE_MODEL = "android.speech.extra.LANGUAGE_MODEL";
139
140    /**
141     * Use a language model based on free-form speech recognition.  This is a value to use for
142     * {@link #EXTRA_LANGUAGE_MODEL}.
143     * @see #EXTRA_LANGUAGE_MODEL
144     */
145    public static final String LANGUAGE_MODEL_FREE_FORM = "free_form";
146    /**
147     * Use a language model based on web search terms.  This is a value to use for
148     * {@link #EXTRA_LANGUAGE_MODEL}.
149     * @see #EXTRA_LANGUAGE_MODEL
150     */
151    public static final String LANGUAGE_MODEL_WEB_SEARCH = "web_search";
152
153    /** Optional text prompt to show to the user when asking them to speak. */
154    public static final String EXTRA_PROMPT = "android.speech.extra.PROMPT";
155
156    /**
157     * Optional IETF language tag (as defined by BCP 47), for example "en-US". This tag informs the
158     * recognizer to perform speech recognition in a language different than the one set in the
159     * {@link java.util.Locale#getDefault()}.
160     */
161    public static final String EXTRA_LANGUAGE = "android.speech.extra.LANGUAGE";
162
163    /**
164     * Optional limit on the maximum number of results to return. If omitted the recognizer
165     * will choose how many results to return. Must be an integer.
166     */
167    public static final String EXTRA_MAX_RESULTS = "android.speech.extra.MAX_RESULTS";
168
169    /**
170     * Optional boolean to indicate whether partial results should be returned by the recognizer
171     * as the user speaks (default is false).  The server may ignore a request for partial
172     * results in some or all cases.
173     */
174    public static final String EXTRA_PARTIAL_RESULTS = "android.speech.extra.PARTIAL_RESULTS";
175
176    /**
177     * When the intent is {@link #ACTION_RECOGNIZE_SPEECH}, the speech input activity will
178     * return results to you via the activity results mechanism.  Alternatively, if you use this
179     * extra to supply a PendingIntent, the results will be added to its bundle and the
180     * PendingIntent will be sent to its target.
181     */
182    public static final String EXTRA_RESULTS_PENDINGINTENT =
183            "android.speech.extra.RESULTS_PENDINGINTENT";
184
185    /**
186     * If you use {@link #EXTRA_RESULTS_PENDINGINTENT} to supply a forwarding intent, you can
187     * also use this extra to supply additional extras for the final intent.  The search results
188     * will be added to this bundle, and the combined bundle will be sent to the target.
189     */
190    public static final String EXTRA_RESULTS_PENDINGINTENT_BUNDLE =
191            "android.speech.extra.RESULTS_PENDINGINTENT_BUNDLE";
192
193    /** Result code returned when no matches are found for the given speech */
194    public static final int RESULT_NO_MATCH = Activity.RESULT_FIRST_USER;
195    /** Result code returned when there is a generic client error */
196    public static final int RESULT_CLIENT_ERROR = Activity.RESULT_FIRST_USER + 1;
197    /** Result code returned when the recognition server returns an error */
198    public static final int RESULT_SERVER_ERROR = Activity.RESULT_FIRST_USER + 2;
199    /** Result code returned when a network error was encountered */
200    public static final int RESULT_NETWORK_ERROR = Activity.RESULT_FIRST_USER + 3;
201    /** Result code returned when an audio error was encountered */
202    public static final int RESULT_AUDIO_ERROR = Activity.RESULT_FIRST_USER + 4;
203
204    /**
205     * An ArrayList&lt;String&gt; of the recognition results when performing
206     * {@link #ACTION_RECOGNIZE_SPEECH}. Returned in the results; not to be specified in the
207     * recognition request. Only present when {@link Activity#RESULT_OK} is returned in
208     * an activity result. In a PendingIntent, the lack of this extra indicates failure.
209     */
210    public static final String EXTRA_RESULTS = "android.speech.extra.RESULTS";
211
212    /**
213     * Triggers the voice search settings activity.
214     */
215    public static final String ACTION_VOICE_SEARCH_SETTINGS =
216            "android.speech.action.VOICE_SEARCH_SETTINGS";
217}
218