Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.ServiceConnection; |
| 23 | import android.content.pm.ResolveInfo; |
| 24 | import android.os.Bundle; |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 25 | import android.os.Handler; |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 26 | import android.os.IBinder; |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 27 | import android.os.Looper; |
| 28 | import android.os.Message; |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 29 | import android.os.RemoteException; |
Mike LeBeau | 5d34e9b | 2010-02-10 19:34:56 -0800 | [diff] [blame] | 30 | import android.provider.Settings; |
| 31 | import android.text.TextUtils; |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 32 | import android.util.Log; |
| 33 | |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 34 | import java.util.LinkedList; |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 35 | import java.util.List; |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 36 | import java.util.Queue; |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * This class provides access to the speech recognition service. This service allows access to the |
| 40 | * speech recognizer. Do not instantiate this class directly, instead, call |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 41 | * {@link SpeechRecognizer#createSpeechRecognizer(Context)}. This class's methods must be |
Marc Wilson | 2921cee | 2013-09-10 15:29:00 +0100 | [diff] [blame] | 42 | * invoked only from the main application thread. |
| 43 | * |
| 44 | * <p>The implementation of this API is likely to stream audio to remote servers to perform speech |
| 45 | * recognition. As such this API is not intended to be used for continuous recognition, which would |
| 46 | * consume a significant amount of battery and bandwidth. |
| 47 | * |
| 48 | * <p>Please note that the application must have {@link android.Manifest.permission#RECORD_AUDIO} |
| 49 | * permission to use this class. |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 50 | */ |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 51 | public class SpeechRecognizer { |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 52 | /** DEBUG value to enable verbose debug prints */ |
| 53 | private final static boolean DBG = false; |
| 54 | |
| 55 | /** Log messages identifier */ |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 56 | private static final String TAG = "SpeechRecognizer"; |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 57 | |
| 58 | /** |
Mike LeBeau | bb7cf91 | 2011-04-05 09:37:30 -0400 | [diff] [blame] | 59 | * Key used to retrieve an {@code ArrayList<String>} from the {@link Bundle} passed to the |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 60 | * {@link RecognitionListener#onResults(Bundle)} and |
| 61 | * {@link RecognitionListener#onPartialResults(Bundle)} methods. These strings are the possible |
| 62 | * recognition results, where the first element is the most likely candidate. |
| 63 | */ |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 64 | public static final String RESULTS_RECOGNITION = "results_recognition"; |
Mike LeBeau | bb7cf91 | 2011-04-05 09:37:30 -0400 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Key used to retrieve a float array from the {@link Bundle} passed to the |
| 68 | * {@link RecognitionListener#onResults(Bundle)} and |
| 69 | * {@link RecognitionListener#onPartialResults(Bundle)} methods. The array should be |
| 70 | * the same size as the ArrayList provided in {@link #RESULTS_RECOGNITION}, and should contain |
| 71 | * values ranging from 0.0 to 1.0, or -1 to represent an unavailable confidence score. |
| 72 | * <p> |
| 73 | * Confidence values close to 1.0 indicate high confidence (the speech recognizer is confident |
| 74 | * that the recognition result is correct), while values close to 0.0 indicate low confidence. |
| 75 | * <p> |
| 76 | * This value is optional and might not be provided. |
| 77 | */ |
| 78 | public static final String CONFIDENCE_SCORES = "confidence_scores"; |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 79 | |
| 80 | /** Network operation timed out. */ |
| 81 | public static final int ERROR_NETWORK_TIMEOUT = 1; |
| 82 | |
| 83 | /** Other network related errors. */ |
| 84 | public static final int ERROR_NETWORK = 2; |
| 85 | |
| 86 | /** Audio recording error. */ |
| 87 | public static final int ERROR_AUDIO = 3; |
| 88 | |
| 89 | /** Server sends error status. */ |
| 90 | public static final int ERROR_SERVER = 4; |
| 91 | |
| 92 | /** Other client side errors. */ |
| 93 | public static final int ERROR_CLIENT = 5; |
| 94 | |
| 95 | /** No speech input */ |
| 96 | public static final int ERROR_SPEECH_TIMEOUT = 6; |
| 97 | |
| 98 | /** No recognition result matched. */ |
| 99 | public static final int ERROR_NO_MATCH = 7; |
| 100 | |
| 101 | /** RecognitionService busy. */ |
| 102 | public static final int ERROR_RECOGNIZER_BUSY = 8; |
| 103 | |
| 104 | /** Insufficient permissions */ |
| 105 | public static final int ERROR_INSUFFICIENT_PERMISSIONS = 9; |
| 106 | |
| 107 | /** action codes */ |
| 108 | private final static int MSG_START = 1; |
| 109 | private final static int MSG_STOP = 2; |
| 110 | private final static int MSG_CANCEL = 3; |
| 111 | private final static int MSG_CHANGE_LISTENER = 4; |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 112 | |
| 113 | /** The actual RecognitionService endpoint */ |
| 114 | private IRecognitionService mService; |
| 115 | |
| 116 | /** The connection to the actual service */ |
| 117 | private Connection mConnection; |
| 118 | |
| 119 | /** Context with which the manager was created */ |
| 120 | private final Context mContext; |
Mike LeBeau | 79375f7 | 2010-02-18 19:01:47 -0800 | [diff] [blame] | 121 | |
| 122 | /** Component to direct service intent to */ |
| 123 | private final ComponentName mServiceComponent; |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 124 | |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 125 | /** Handler that will execute the main tasks */ |
| 126 | private Handler mHandler = new Handler() { |
| 127 | @Override |
| 128 | public void handleMessage(Message msg) { |
| 129 | switch (msg.what) { |
| 130 | case MSG_START: |
| 131 | handleStartListening((Intent) msg.obj); |
| 132 | break; |
| 133 | case MSG_STOP: |
| 134 | handleStopMessage(); |
| 135 | break; |
| 136 | case MSG_CANCEL: |
| 137 | handleCancelMessage(); |
| 138 | break; |
| 139 | case MSG_CHANGE_LISTENER: |
| 140 | handleChangeListener((RecognitionListener) msg.obj); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | }; |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 145 | |
| 146 | /** |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 147 | * Temporary queue, saving the messages until the connection will be established, afterwards, |
| 148 | * only mHandler will receive the messages |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 149 | */ |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 150 | private final Queue<Message> mPendingTasks = new LinkedList<Message>(); |
| 151 | |
| 152 | /** The Listener that will receive all the callbacks */ |
| 153 | private final InternalListener mListener = new InternalListener(); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 154 | |
| 155 | /** |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 156 | * The right way to create a {@code SpeechRecognizer} is by using |
| 157 | * {@link #createSpeechRecognizer} static factory method |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 158 | */ |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 159 | private SpeechRecognizer(final Context context, final ComponentName serviceComponent) { |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 160 | mContext = context; |
Mike LeBeau | 79375f7 | 2010-02-18 19:01:47 -0800 | [diff] [blame] | 161 | mServiceComponent = serviceComponent; |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 165 | * Basic ServiceConnection that records the mService variable. Additionally, on creation it |
| 166 | * invokes the {@link IRecognitionService#startListening(Intent, IRecognitionListener)}. |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 167 | */ |
| 168 | private class Connection implements ServiceConnection { |
| 169 | |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 170 | public void onServiceConnected(final ComponentName name, final IBinder service) { |
| 171 | // always done on the application main thread, so no need to send message to mHandler |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 172 | mService = IRecognitionService.Stub.asInterface(service); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 173 | if (DBG) Log.d(TAG, "onServiceConnected - Success"); |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 174 | while (!mPendingTasks.isEmpty()) { |
| 175 | mHandler.sendMessage(mPendingTasks.poll()); |
| 176 | } |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | public void onServiceDisconnected(final ComponentName name) { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 180 | // always done on the application main thread, so no need to send message to mHandler |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 181 | mService = null; |
| 182 | mConnection = null; |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 183 | mPendingTasks.clear(); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 184 | if (DBG) Log.d(TAG, "onServiceDisconnected - Success"); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Checks whether a speech recognition service is available on the system. If this method |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 190 | * returns {@code false}, {@link SpeechRecognizer#createSpeechRecognizer(Context)} will |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 191 | * fail. |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 192 | * |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 193 | * @param context with which {@code SpeechRecognizer} will be created |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 194 | * @return {@code true} if recognition is available, {@code false} otherwise |
| 195 | */ |
| 196 | public static boolean isRecognitionAvailable(final Context context) { |
| 197 | final List<ResolveInfo> list = context.getPackageManager().queryIntentServices( |
Valentin Kravtsov | cccd63c | 2010-02-12 14:19:11 +0000 | [diff] [blame] | 198 | new Intent(RecognitionService.SERVICE_INTERFACE), 0); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 199 | return list != null && list.size() != 0; |
| 200 | } |
| 201 | |
| 202 | /** |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 203 | * Factory method to create a new {@code SpeechRecognizer}. Please note that |
Valentin Kravtsov | 1168622 | 2010-03-18 07:41:38 +0000 | [diff] [blame] | 204 | * {@link #setRecognitionListener(RecognitionListener)} should be called before dispatching any |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 205 | * command to the created {@code SpeechRecognizer}, otherwise no notifications will be |
Valentin Kravtsov | 1168622 | 2010-03-18 07:41:38 +0000 | [diff] [blame] | 206 | * received. |
| 207 | * |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 208 | * @param context in which to create {@code SpeechRecognizer} |
| 209 | * @return a new {@code SpeechRecognizer} |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 210 | */ |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 211 | public static SpeechRecognizer createSpeechRecognizer(final Context context) { |
| 212 | return createSpeechRecognizer(context, null); |
Mike LeBeau | 79375f7 | 2010-02-18 19:01:47 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 216 | * Factory method to create a new {@code SpeechRecognizer}. Please note that |
Valentin Kravtsov | 1168622 | 2010-03-18 07:41:38 +0000 | [diff] [blame] | 217 | * {@link #setRecognitionListener(RecognitionListener)} should be called before dispatching any |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 218 | * command to the created {@code SpeechRecognizer}, otherwise no notifications will be |
Valentin Kravtsov | 1168622 | 2010-03-18 07:41:38 +0000 | [diff] [blame] | 219 | * received. |
| 220 | * |
Mike LeBeau | 79375f7 | 2010-02-18 19:01:47 -0800 | [diff] [blame] | 221 | * Use this version of the method to specify a specific service to direct this |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 222 | * {@link SpeechRecognizer} to. Normally you would not use this; use |
| 223 | * {@link #createSpeechRecognizer(Context)} instead to use the system default recognition |
Valentin Kravtsov | 1168622 | 2010-03-18 07:41:38 +0000 | [diff] [blame] | 224 | * service. |
Mike LeBeau | 79375f7 | 2010-02-18 19:01:47 -0800 | [diff] [blame] | 225 | * |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 226 | * @param context in which to create {@code SpeechRecognizer} |
Mike LeBeau | 79375f7 | 2010-02-18 19:01:47 -0800 | [diff] [blame] | 227 | * @param serviceComponent the {@link ComponentName} of a specific service to direct this |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 228 | * {@code SpeechRecognizer} to |
| 229 | * @return a new {@code SpeechRecognizer} |
Mike LeBeau | 79375f7 | 2010-02-18 19:01:47 -0800 | [diff] [blame] | 230 | */ |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 231 | public static SpeechRecognizer createSpeechRecognizer(final Context context, |
Mike LeBeau | 79375f7 | 2010-02-18 19:01:47 -0800 | [diff] [blame] | 232 | final ComponentName serviceComponent) { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 233 | if (context == null) { |
| 234 | throw new IllegalArgumentException("Context cannot be null)"); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 235 | } |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 236 | checkIsCalledFromMainThread(); |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 237 | return new SpeechRecognizer(context, serviceComponent); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 241 | * Sets the listener that will receive all the callbacks. The previous unfinished commands will |
| 242 | * be executed with the old listener, while any following command will be executed with the new |
| 243 | * listener. |
| 244 | * |
| 245 | * @param listener listener that will receive all the callbacks from the created |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 246 | * {@link SpeechRecognizer}, this must not be null. |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 247 | */ |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 248 | public void setRecognitionListener(RecognitionListener listener) { |
| 249 | checkIsCalledFromMainThread(); |
| 250 | putMessage(Message.obtain(mHandler, MSG_CHANGE_LISTENER, listener)); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 254 | * Starts listening for speech. Please note that |
Valentin Kravtsov | 1168622 | 2010-03-18 07:41:38 +0000 | [diff] [blame] | 255 | * {@link #setRecognitionListener(RecognitionListener)} should be called beforehand, otherwise |
| 256 | * no notifications will be received. |
| 257 | * |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 258 | * @param recognizerIntent contains parameters for the recognition to be performed. The intent |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 259 | * may also contain optional extras, see {@link RecognizerIntent}. If these values are |
| 260 | * not set explicitly, default values will be used by the recognizer. |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 261 | */ |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 262 | public void startListening(final Intent recognizerIntent) { |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 263 | if (recognizerIntent == null) { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 264 | throw new IllegalArgumentException("intent must not be null"); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 265 | } |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 266 | checkIsCalledFromMainThread(); |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 267 | if (mConnection == null) { // first time connection |
| 268 | mConnection = new Connection(); |
Mike LeBeau | 5d34e9b | 2010-02-10 19:34:56 -0800 | [diff] [blame] | 269 | |
Mike LeBeau | cb0be8a | 2010-02-11 15:01:27 -0800 | [diff] [blame] | 270 | Intent serviceIntent = new Intent(RecognitionService.SERVICE_INTERFACE); |
Mike LeBeau | 5d34e9b | 2010-02-10 19:34:56 -0800 | [diff] [blame] | 271 | |
Mike LeBeau | 79375f7 | 2010-02-18 19:01:47 -0800 | [diff] [blame] | 272 | if (mServiceComponent == null) { |
| 273 | String serviceComponent = Settings.Secure.getString(mContext.getContentResolver(), |
| 274 | Settings.Secure.VOICE_RECOGNITION_SERVICE); |
| 275 | |
| 276 | if (TextUtils.isEmpty(serviceComponent)) { |
| 277 | Log.e(TAG, "no selected voice recognition service"); |
| 278 | mListener.onError(ERROR_CLIENT); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | serviceIntent.setComponent(ComponentName.unflattenFromString(serviceComponent)); |
| 283 | } else { |
| 284 | serviceIntent.setComponent(mServiceComponent); |
Mike LeBeau | 5d34e9b | 2010-02-10 19:34:56 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Mike LeBeau | 5d34e9b | 2010-02-10 19:34:56 -0800 | [diff] [blame] | 287 | if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 288 | Log.e(TAG, "bind to recognition service failed"); |
| 289 | mConnection = null; |
| 290 | mService = null; |
| 291 | mListener.onError(ERROR_CLIENT); |
| 292 | return; |
| 293 | } |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 294 | } |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 295 | putMessage(Message.obtain(mHandler, MSG_START, recognizerIntent)); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Stops listening for speech. Speech captured so far will be recognized as if the user had |
| 300 | * stopped speaking at this point. Note that in the default case, this does not need to be |
| 301 | * called, as the speech endpointer will automatically stop the recognizer listening when it |
| 302 | * determines speech has completed. However, you can manipulate endpointer parameters directly |
| 303 | * using the intent extras defined in {@link RecognizerIntent}, in which case you may sometimes |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 304 | * want to manually call this method to stop listening sooner. Please note that |
Valentin Kravtsov | 1168622 | 2010-03-18 07:41:38 +0000 | [diff] [blame] | 305 | * {@link #setRecognitionListener(RecognitionListener)} should be called beforehand, otherwise |
| 306 | * no notifications will be received. |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 307 | */ |
| 308 | public void stopListening() { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 309 | checkIsCalledFromMainThread(); |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 310 | putMessage(Message.obtain(mHandler, MSG_STOP)); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Cancels the speech recognition. Please note that |
Valentin Kravtsov | 1168622 | 2010-03-18 07:41:38 +0000 | [diff] [blame] | 315 | * {@link #setRecognitionListener(RecognitionListener)} should be called beforehand, otherwise |
| 316 | * no notifications will be received. |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 317 | */ |
| 318 | public void cancel() { |
| 319 | checkIsCalledFromMainThread(); |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 320 | putMessage(Message.obtain(mHandler, MSG_CANCEL)); |
| 321 | } |
| 322 | |
| 323 | private static void checkIsCalledFromMainThread() { |
| 324 | if (Looper.myLooper() != Looper.getMainLooper()) { |
| 325 | throw new RuntimeException( |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 326 | "SpeechRecognizer should be used only from the application's main thread"); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 327 | } |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 330 | private void putMessage(Message msg) { |
| 331 | if (mService == null) { |
| 332 | mPendingTasks.offer(msg); |
| 333 | } else { |
| 334 | mHandler.sendMessage(msg); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /** sends the actual message to the service */ |
| 339 | private void handleStartListening(Intent recognizerIntent) { |
Valentin Kravtsov | 7a3c9d3 | 2010-02-05 16:26:04 +0000 | [diff] [blame] | 340 | if (!checkOpenConnection()) { |
| 341 | return; |
| 342 | } |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 343 | try { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 344 | mService.startListening(recognizerIntent, mListener); |
| 345 | if (DBG) Log.d(TAG, "service start listening command succeded"); |
| 346 | } catch (final RemoteException e) { |
| 347 | Log.e(TAG, "startListening() failed", e); |
| 348 | mListener.onError(ERROR_CLIENT); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /** sends the actual message to the service */ |
| 353 | private void handleStopMessage() { |
Valentin Kravtsov | 7a3c9d3 | 2010-02-05 16:26:04 +0000 | [diff] [blame] | 354 | if (!checkOpenConnection()) { |
| 355 | return; |
| 356 | } |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 357 | try { |
| 358 | mService.stopListening(mListener); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 359 | if (DBG) Log.d(TAG, "service stop listening command succeded"); |
| 360 | } catch (final RemoteException e) { |
| 361 | Log.e(TAG, "stopListening() failed", e); |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 362 | mListener.onError(ERROR_CLIENT); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 366 | /** sends the actual message to the service */ |
| 367 | private void handleCancelMessage() { |
Valentin Kravtsov | 7a3c9d3 | 2010-02-05 16:26:04 +0000 | [diff] [blame] | 368 | if (!checkOpenConnection()) { |
| 369 | return; |
| 370 | } |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 371 | try { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 372 | mService.cancel(mListener); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 373 | if (DBG) Log.d(TAG, "service cancel command succeded"); |
| 374 | } catch (final RemoteException e) { |
| 375 | Log.e(TAG, "cancel() failed", e); |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 376 | mListener.onError(ERROR_CLIENT); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
Valentin Kravtsov | 7a3c9d3 | 2010-02-05 16:26:04 +0000 | [diff] [blame] | 379 | |
| 380 | private boolean checkOpenConnection() { |
| 381 | if (mService != null) { |
| 382 | return true; |
| 383 | } |
| 384 | mListener.onError(ERROR_CLIENT); |
| 385 | Log.e(TAG, "not connected to the recognition service"); |
| 386 | return false; |
| 387 | } |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 388 | |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 389 | /** changes the listener */ |
| 390 | private void handleChangeListener(RecognitionListener listener) { |
| 391 | if (DBG) Log.d(TAG, "handleChangeListener, listener=" + listener); |
| 392 | mListener.mInternalListener = listener; |
| 393 | } |
| 394 | |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 395 | /** |
Jean-Michel Trivi | 2a5d9f9 | 2010-03-29 18:31:19 -0700 | [diff] [blame] | 396 | * Destroys the {@code SpeechRecognizer} object. |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 397 | */ |
| 398 | public void destroy() { |
Jerome Poichet | c1fb6dc | 2014-09-30 17:54:03 -0700 | [diff] [blame] | 399 | if (mService != null) { |
| 400 | try { |
| 401 | mService.cancel(mListener); |
| 402 | } catch (final RemoteException e) { |
| 403 | // Not important |
| 404 | } |
| 405 | } |
| 406 | |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 407 | if (mConnection != null) { |
| 408 | mContext.unbindService(mConnection); |
| 409 | } |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 410 | mPendingTasks.clear(); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 411 | mService = null; |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 412 | mConnection = null; |
Valentin Kravtsov | 1c3cca0 | 2010-04-21 22:27:05 +0100 | [diff] [blame] | 413 | mListener.mInternalListener = null; |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /** |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 417 | * Internal wrapper of IRecognitionListener which will propagate the results to |
| 418 | * RecognitionListener |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 419 | */ |
Mark Renouf | b37866d | 2014-03-17 17:51:35 -0400 | [diff] [blame] | 420 | private static class InternalListener extends IRecognitionListener.Stub { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 421 | private RecognitionListener mInternalListener; |
| 422 | |
| 423 | private final static int MSG_BEGINNING_OF_SPEECH = 1; |
| 424 | private final static int MSG_BUFFER_RECEIVED = 2; |
| 425 | private final static int MSG_END_OF_SPEECH = 3; |
| 426 | private final static int MSG_ERROR = 4; |
| 427 | private final static int MSG_READY_FOR_SPEECH = 5; |
| 428 | private final static int MSG_RESULTS = 6; |
| 429 | private final static int MSG_PARTIAL_RESULTS = 7; |
| 430 | private final static int MSG_RMS_CHANGED = 8; |
| 431 | private final static int MSG_ON_EVENT = 9; |
| 432 | |
| 433 | private final Handler mInternalHandler = new Handler() { |
| 434 | @Override |
| 435 | public void handleMessage(Message msg) { |
| 436 | if (mInternalListener == null) { |
| 437 | return; |
| 438 | } |
| 439 | switch (msg.what) { |
| 440 | case MSG_BEGINNING_OF_SPEECH: |
| 441 | mInternalListener.onBeginningOfSpeech(); |
| 442 | break; |
| 443 | case MSG_BUFFER_RECEIVED: |
| 444 | mInternalListener.onBufferReceived((byte[]) msg.obj); |
| 445 | break; |
| 446 | case MSG_END_OF_SPEECH: |
| 447 | mInternalListener.onEndOfSpeech(); |
| 448 | break; |
| 449 | case MSG_ERROR: |
| 450 | mInternalListener.onError((Integer) msg.obj); |
| 451 | break; |
| 452 | case MSG_READY_FOR_SPEECH: |
| 453 | mInternalListener.onReadyForSpeech((Bundle) msg.obj); |
| 454 | break; |
| 455 | case MSG_RESULTS: |
| 456 | mInternalListener.onResults((Bundle) msg.obj); |
| 457 | break; |
| 458 | case MSG_PARTIAL_RESULTS: |
| 459 | mInternalListener.onPartialResults((Bundle) msg.obj); |
| 460 | break; |
| 461 | case MSG_RMS_CHANGED: |
| 462 | mInternalListener.onRmsChanged((Float) msg.obj); |
| 463 | break; |
| 464 | case MSG_ON_EVENT: |
| 465 | mInternalListener.onEvent(msg.arg1, (Bundle) msg.obj); |
| 466 | break; |
| 467 | } |
| 468 | } |
| 469 | }; |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 470 | |
| 471 | public void onBeginningOfSpeech() { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 472 | Message.obtain(mInternalHandler, MSG_BEGINNING_OF_SPEECH).sendToTarget(); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | public void onBufferReceived(final byte[] buffer) { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 476 | Message.obtain(mInternalHandler, MSG_BUFFER_RECEIVED, buffer).sendToTarget(); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | public void onEndOfSpeech() { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 480 | Message.obtain(mInternalHandler, MSG_END_OF_SPEECH).sendToTarget(); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | public void onError(final int error) { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 484 | Message.obtain(mInternalHandler, MSG_ERROR, error).sendToTarget(); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | public void onReadyForSpeech(final Bundle noiseParams) { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 488 | Message.obtain(mInternalHandler, MSG_READY_FOR_SPEECH, noiseParams).sendToTarget(); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | public void onResults(final Bundle results) { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 492 | Message.obtain(mInternalHandler, MSG_RESULTS, results).sendToTarget(); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | public void onPartialResults(final Bundle results) { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 496 | Message.obtain(mInternalHandler, MSG_PARTIAL_RESULTS, results).sendToTarget(); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | public void onRmsChanged(final float rmsdB) { |
Valentin Kravtsov | 3da3cad | 2010-01-28 14:53:41 +0000 | [diff] [blame] | 500 | Message.obtain(mInternalHandler, MSG_RMS_CHANGED, rmsdB).sendToTarget(); |
| 501 | } |
| 502 | |
| 503 | public void onEvent(final int eventType, final Bundle params) { |
| 504 | Message.obtain(mInternalHandler, MSG_ON_EVENT, eventType, eventType, params) |
| 505 | .sendToTarget(); |
Valentin Kravtsov | 79896bd | 2010-01-15 11:56:03 +0000 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | } |