/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.speech; import java.util.ArrayList; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Bundle; /** * Constants for supporting speech recognition through starting an {@link Intent} */ public class RecognizerIntent { /** * The extra key used in an intent to the speech recognizer for voice search. Not * generally to be used by developers. The system search dialog uses this, for example, * to set a calling package for identification by a voice search API. If this extra * is set by anyone but the system process, it should be overridden by the voice search * implementation. */ public final static String EXTRA_CALLING_PACKAGE = "calling_package"; private RecognizerIntent() { // Not for instantiating. } /** * Starts an activity that will prompt the user for speech and send it through a * speech recognizer. The results will be returned via activity results (in * {@link Activity#onActivityResult}, if you start the intent using * {@link Activity#startActivityForResult(Intent, int)}), or forwarded via a PendingIntent * if one is provided. * *
Starting this intent with just {@link Activity#startActivity(Intent)} is not supported. * You must either use {@link Activity#startActivityForResult(Intent, int)}, or provide a * PendingIntent, to receive recognition results. * *
Required extras: *
Optional extras: *
Result extras (returned in the result, not to be specified in the request): *
NOTE: There may not be any applications installed to handle this action, so you should * make sure to catch {@link ActivityNotFoundException}. */ public static final String ACTION_RECOGNIZE_SPEECH = "android.speech.action.RECOGNIZE_SPEECH"; /** * Starts an activity that will prompt the user for speech, send it through a * speech recognizer, and either display a web search result or trigger * another type of action based on the user's speech. * *
If you want to avoid triggering any type of action besides web search, you can use * the {@link #EXTRA_WEB_SEARCH_ONLY} extra. * *
Required extras: *
Optional extras: *
Result extras (returned in the result, not to be specified in the request): *
NOTE: There may not be any applications installed to handle this action, so you should * make sure to catch {@link ActivityNotFoundException}. */ public static final String ACTION_WEB_SEARCH = "android.speech.action.WEB_SEARCH"; /** * Starts an activity that will prompt the user for speech without requiring the user's * visual attention or touch input. It will send it through a speech recognizer, * and either synthesize speech for a web search result or trigger * another type of action based on the user's speech. * * This activity may be launched while device is locked in a secure mode. * Special care must be taken to ensure that the voice actions that are performed while * hands free cannot compromise the device's security. * The activity should check the value of the {@link #EXTRA_SECURE} extra to determine * whether the device has been securely locked. If so, the activity should either restrict * the set of voice actions that are permitted or require some form of secure * authentication before proceeding. * * To ensure that the activity's user interface is visible while the lock screen is showing, * the activity should set the * {@link android.view.WindowManager.LayoutParams#FLAG_SHOW_WHEN_LOCKED} window flag. * Otherwise the activity's user interface may be hidden by the lock screen. The activity * should take care not to leak private information when the device is securely locked. * *
Optional extras: *
* Confidence values close to 1.0 indicate high confidence (the speech recognizer is * confident that the recognition result is correct), while values close to 0.0 indicate * low confidence. *
* Returned in the results; not to be specified in the recognition request. This extra is * optional and might not be provided. Only present when {@link Activity#RESULT_OK} is * returned in an activity result. */ public static final String EXTRA_CONFIDENCE_SCORES = "android.speech.extra.CONFIDENCE_SCORES"; /** * Returns the broadcast intent to fire with * {@link Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, Bundle)} * to receive details from the package that implements voice search. *
* This is based on the value specified by the voice search {@link Activity} in * {@link #DETAILS_META_DATA}, and if this is not specified, will return null. Also if there * is no chosen default to resolve for {@link #ACTION_WEB_SEARCH}, this will return null. *
* If an intent is returned and is fired, a {@link Bundle} of extras will be returned to the * provided result receiver, and should ideally contain values for * {@link #EXTRA_LANGUAGE_PREFERENCE} and {@link #EXTRA_SUPPORTED_LANGUAGES}. *
* (Whether these are actually provided is up to the particular implementation. It is * recommended that {@link Activity}s implementing {@link #ACTION_WEB_SEARCH} provide this * information, but it is not required.) * * @param context a context object * @return the broadcast intent to fire or null if not available */ public static final Intent getVoiceDetailsIntent(Context context) { Intent voiceSearchIntent = new Intent(ACTION_WEB_SEARCH); ResolveInfo ri = context.getPackageManager().resolveActivity( voiceSearchIntent, PackageManager.GET_META_DATA); if (ri == null || ri.activityInfo == null || ri.activityInfo.metaData == null) return null; String className = ri.activityInfo.metaData.getString(DETAILS_META_DATA); if (className == null) return null; Intent detailsIntent = new Intent(ACTION_GET_LANGUAGE_DETAILS); detailsIntent.setComponent(new ComponentName(ri.activityInfo.packageName, className)); return detailsIntent; } /** * Meta-data name under which an {@link Activity} implementing {@link #ACTION_WEB_SEARCH} can * use to expose the class name of a {@link BroadcastReceiver} which can respond to request for * more information, from any of the broadcast intents specified in this class. *
* Broadcast intents can be directed to the class name specified in the meta-data by creating * an {@link Intent}, setting the component with * {@link Intent#setComponent(android.content.ComponentName)}, and using * {@link Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, android.os.Bundle)} * with another {@link BroadcastReceiver} which can receive the results. *
* The {@link #getVoiceDetailsIntent(Context)} method is provided as a convenience to create * a broadcast intent based on the value of this meta-data, if available. *
* This is optional and not all {@link Activity}s which implement {@link #ACTION_WEB_SEARCH} * are required to implement this. Thus retrieving this meta-data may be null. */ public static final String DETAILS_META_DATA = "android.speech.DETAILS"; /** * A broadcast intent which can be fired to the {@link BroadcastReceiver} component specified * in the meta-data defined in the {@link #DETAILS_META_DATA} meta-data of an * {@link Activity} satisfying {@link #ACTION_WEB_SEARCH}. *
* When fired with * {@link Context#sendOrderedBroadcast(Intent, String, BroadcastReceiver, android.os.Handler, int, String, android.os.Bundle)}, * a {@link Bundle} of extras will be returned to the provided result receiver, and should * ideally contain values for {@link #EXTRA_LANGUAGE_PREFERENCE} and * {@link #EXTRA_SUPPORTED_LANGUAGES}. *
* (Whether these are actually provided is up to the particular implementation. It is * recommended that {@link Activity}s implementing {@link #ACTION_WEB_SEARCH} provide this * information, but it is not required.) */ public static final String ACTION_GET_LANGUAGE_DETAILS = "android.speech.action.GET_LANGUAGE_DETAILS"; /** * Specify this boolean extra in a broadcast of {@link #ACTION_GET_LANGUAGE_DETAILS} to * indicate that only the current language preference is needed in the response. This * avoids any additional computation if all you need is {@link #EXTRA_LANGUAGE_PREFERENCE} * in the response. */ public static final String EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE = "android.speech.extra.ONLY_RETURN_LANGUAGE_PREFERENCE"; /** * The key to the extra in the {@link Bundle} returned by {@link #ACTION_GET_LANGUAGE_DETAILS} * which is a {@link String} that represents the current language preference this user has * specified - a locale string like "en-US". */ public static final String EXTRA_LANGUAGE_PREFERENCE = "android.speech.extra.LANGUAGE_PREFERENCE"; /** * The key to the extra in the {@link Bundle} returned by {@link #ACTION_GET_LANGUAGE_DETAILS} * which is an {@link ArrayList} of {@link String}s that represents the languages supported by * this implementation of voice recognition - a list of strings like "en-US", "cmn-Hans-CN", * etc. */ public static final String EXTRA_SUPPORTED_LANGUAGES = "android.speech.extra.SUPPORTED_LANGUAGES"; }