1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package android.speech; 18 19 import android.annotation.SdkConstant; 20 import android.annotation.SdkConstant.SdkConstantType; 21 import android.app.Service; 22 import android.content.Intent; 23 import android.content.PermissionChecker; 24 import android.os.Binder; 25 import android.os.Bundle; 26 import android.os.Handler; 27 import android.os.IBinder; 28 import android.os.Message; 29 import android.os.RemoteException; 30 import android.util.Log; 31 32 import java.lang.ref.WeakReference; 33 34 /** 35 * This class provides a base class for recognition service implementations. This class should be 36 * extended only in case you wish to implement a new speech recognizer. Please note that the 37 * implementation of this service is stateless. 38 */ 39 public abstract class RecognitionService extends Service { 40 /** 41 * The {@link Intent} that must be declared as handled by the service. 42 */ 43 @SdkConstant(SdkConstantType.SERVICE_ACTION) 44 public static final String SERVICE_INTERFACE = "android.speech.RecognitionService"; 45 46 /** 47 * Name under which a RecognitionService component publishes information about itself. 48 * This meta-data should reference an XML resource containing a 49 * <code><{@link android.R.styleable#RecognitionService recognition-service}></code> tag. 50 */ 51 public static final String SERVICE_META_DATA = "android.speech"; 52 53 /** Log messages identifier */ 54 private static final String TAG = "RecognitionService"; 55 56 /** Debugging flag */ 57 private static final boolean DBG = false; 58 59 /** Binder of the recognition service */ 60 private RecognitionServiceBinder mBinder = new RecognitionServiceBinder(this); 61 62 /** 63 * The current callback of an application that invoked the 64 * {@link RecognitionService#onStartListening(Intent, Callback)} method 65 */ 66 private Callback mCurrentCallback = null; 67 68 private static final int MSG_START_LISTENING = 1; 69 70 private static final int MSG_STOP_LISTENING = 2; 71 72 private static final int MSG_CANCEL = 3; 73 74 private static final int MSG_RESET = 4; 75 76 private final Handler mHandler = new Handler() { 77 @Override 78 public void handleMessage(Message msg) { 79 switch (msg.what) { 80 case MSG_START_LISTENING: 81 StartListeningArgs args = (StartListeningArgs) msg.obj; 82 dispatchStartListening(args.mIntent, args.mListener, args.mCallingUid); 83 break; 84 case MSG_STOP_LISTENING: 85 dispatchStopListening((IRecognitionListener) msg.obj); 86 break; 87 case MSG_CANCEL: 88 dispatchCancel((IRecognitionListener) msg.obj); 89 break; 90 case MSG_RESET: 91 dispatchClearCallback(); 92 break; 93 } 94 } 95 }; 96 dispatchStartListening(Intent intent, final IRecognitionListener listener, int callingUid)97 private void dispatchStartListening(Intent intent, final IRecognitionListener listener, 98 int callingUid) { 99 if (mCurrentCallback == null) { 100 if (DBG) Log.d(TAG, "created new mCurrentCallback, listener = " + listener.asBinder()); 101 try { 102 listener.asBinder().linkToDeath(new IBinder.DeathRecipient() { 103 @Override 104 public void binderDied() { 105 mHandler.sendMessage(mHandler.obtainMessage(MSG_CANCEL, listener)); 106 } 107 }, 0); 108 } catch (RemoteException re) { 109 Log.e(TAG, "dead listener on startListening"); 110 return; 111 } 112 mCurrentCallback = new Callback(listener, callingUid); 113 RecognitionService.this.onStartListening(intent, mCurrentCallback); 114 } else { 115 try { 116 listener.onError(SpeechRecognizer.ERROR_RECOGNIZER_BUSY); 117 } catch (RemoteException e) { 118 Log.d(TAG, "onError call from startListening failed"); 119 } 120 Log.i(TAG, "concurrent startListening received - ignoring this call"); 121 } 122 } 123 dispatchStopListening(IRecognitionListener listener)124 private void dispatchStopListening(IRecognitionListener listener) { 125 try { 126 if (mCurrentCallback == null) { 127 listener.onError(SpeechRecognizer.ERROR_CLIENT); 128 Log.w(TAG, "stopListening called with no preceding startListening - ignoring"); 129 } else if (mCurrentCallback.mListener.asBinder() != listener.asBinder()) { 130 listener.onError(SpeechRecognizer.ERROR_RECOGNIZER_BUSY); 131 Log.w(TAG, "stopListening called by other caller than startListening - ignoring"); 132 } else { // the correct state 133 RecognitionService.this.onStopListening(mCurrentCallback); 134 } 135 } catch (RemoteException e) { // occurs if onError fails 136 Log.d(TAG, "onError call from stopListening failed"); 137 } 138 } 139 dispatchCancel(IRecognitionListener listener)140 private void dispatchCancel(IRecognitionListener listener) { 141 if (mCurrentCallback == null) { 142 if (DBG) Log.d(TAG, "cancel called with no preceding startListening - ignoring"); 143 } else if (mCurrentCallback.mListener.asBinder() != listener.asBinder()) { 144 Log.w(TAG, "cancel called by client who did not call startListening - ignoring"); 145 } else { // the correct state 146 RecognitionService.this.onCancel(mCurrentCallback); 147 mCurrentCallback = null; 148 if (DBG) Log.d(TAG, "canceling - setting mCurrentCallback to null"); 149 } 150 } 151 dispatchClearCallback()152 private void dispatchClearCallback() { 153 mCurrentCallback = null; 154 } 155 156 private class StartListeningArgs { 157 public final Intent mIntent; 158 159 public final IRecognitionListener mListener; 160 public final int mCallingUid; 161 StartListeningArgs(Intent intent, IRecognitionListener listener, int callingUid)162 public StartListeningArgs(Intent intent, IRecognitionListener listener, int callingUid) { 163 this.mIntent = intent; 164 this.mListener = listener; 165 this.mCallingUid = callingUid; 166 } 167 } 168 169 /** 170 * Checks whether the caller has sufficient permissions 171 * 172 * @param listener to send the error message to in case of error 173 * @param forDataDelivery If the permission check is for delivering the sensitive data. 174 * @return {@code true} if the caller has enough permissions, {@code false} otherwise 175 */ checkPermissions(IRecognitionListener listener, boolean forDataDelivery)176 private boolean checkPermissions(IRecognitionListener listener, boolean forDataDelivery) { 177 if (DBG) Log.d(TAG, "checkPermissions"); 178 if (forDataDelivery) { 179 if (PermissionChecker.checkCallingOrSelfPermissionForDataDelivery(this, 180 android.Manifest.permission.RECORD_AUDIO) 181 == PermissionChecker.PERMISSION_GRANTED) { 182 return true; 183 } 184 } else { 185 if (PermissionChecker.checkCallingOrSelfPermissionForPreflight(this, 186 android.Manifest.permission.RECORD_AUDIO) 187 == PermissionChecker.PERMISSION_GRANTED) { 188 return true; 189 } 190 } 191 try { 192 Log.e(TAG, "call for recognition service without RECORD_AUDIO permissions"); 193 listener.onError(SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS); 194 } catch (RemoteException re) { 195 Log.e(TAG, "sending ERROR_INSUFFICIENT_PERMISSIONS message failed", re); 196 } 197 return false; 198 } 199 200 /** 201 * Notifies the service that it should start listening for speech. 202 * 203 * @param recognizerIntent contains parameters for the recognition to be performed. The intent 204 * may also contain optional extras, see {@link RecognizerIntent}. If these values are 205 * not set explicitly, default values should be used by the recognizer. 206 * @param listener that will receive the service's callbacks 207 */ onStartListening(Intent recognizerIntent, Callback listener)208 protected abstract void onStartListening(Intent recognizerIntent, Callback listener); 209 210 /** 211 * Notifies the service that it should cancel the speech recognition. 212 */ onCancel(Callback listener)213 protected abstract void onCancel(Callback listener); 214 215 /** 216 * Notifies the service that it should stop listening for speech. Speech captured so far should 217 * be recognized as if the user had stopped speaking at this point. This method is only called 218 * if the application calls it explicitly. 219 */ onStopListening(Callback listener)220 protected abstract void onStopListening(Callback listener); 221 222 @Override onBind(final Intent intent)223 public final IBinder onBind(final Intent intent) { 224 if (DBG) Log.d(TAG, "onBind, intent=" + intent); 225 return mBinder; 226 } 227 228 @Override onDestroy()229 public void onDestroy() { 230 if (DBG) Log.d(TAG, "onDestroy"); 231 mCurrentCallback = null; 232 mBinder.clearReference(); 233 super.onDestroy(); 234 } 235 236 /** 237 * This class receives callbacks from the speech recognition service and forwards them to the 238 * user. An instance of this class is passed to the 239 * {@link RecognitionService#onStartListening(Intent, Callback)} method. Recognizers may call 240 * these methods on any thread. 241 */ 242 public class Callback { 243 private final IRecognitionListener mListener; 244 private final int mCallingUid; 245 Callback(IRecognitionListener listener, int callingUid)246 private Callback(IRecognitionListener listener, int callingUid) { 247 mListener = listener; 248 mCallingUid = callingUid; 249 } 250 251 /** 252 * The service should call this method when the user has started to speak. 253 */ beginningOfSpeech()254 public void beginningOfSpeech() throws RemoteException { 255 if (DBG) Log.d(TAG, "beginningOfSpeech"); 256 mListener.onBeginningOfSpeech(); 257 } 258 259 /** 260 * The service should call this method when sound has been received. The purpose of this 261 * function is to allow giving feedback to the user regarding the captured audio. 262 * 263 * @param buffer a buffer containing a sequence of big-endian 16-bit integers representing a 264 * single channel audio stream. The sample rate is implementation dependent. 265 */ bufferReceived(byte[] buffer)266 public void bufferReceived(byte[] buffer) throws RemoteException { 267 mListener.onBufferReceived(buffer); 268 } 269 270 /** 271 * The service should call this method after the user stops speaking. 272 */ endOfSpeech()273 public void endOfSpeech() throws RemoteException { 274 mListener.onEndOfSpeech(); 275 } 276 277 /** 278 * The service should call this method when a network or recognition error occurred. 279 * 280 * @param error code is defined in {@link SpeechRecognizer} 281 */ error(int error)282 public void error(int error) throws RemoteException { 283 Message.obtain(mHandler, MSG_RESET).sendToTarget(); 284 mListener.onError(error); 285 } 286 287 /** 288 * The service should call this method when partial recognition results are available. This 289 * method can be called at any time between {@link #beginningOfSpeech()} and 290 * {@link #results(Bundle)} when partial results are ready. This method may be called zero, 291 * one or multiple times for each call to {@link SpeechRecognizer#startListening(Intent)}, 292 * depending on the speech recognition service implementation. 293 * 294 * @param partialResults the returned results. To retrieve the results in 295 * ArrayList<String> format use {@link Bundle#getStringArrayList(String)} with 296 * {@link SpeechRecognizer#RESULTS_RECOGNITION} as a parameter 297 */ partialResults(Bundle partialResults)298 public void partialResults(Bundle partialResults) throws RemoteException { 299 mListener.onPartialResults(partialResults); 300 } 301 302 /** 303 * The service should call this method when the endpointer is ready for the user to start 304 * speaking. 305 * 306 * @param params parameters set by the recognition service. Reserved for future use. 307 */ readyForSpeech(Bundle params)308 public void readyForSpeech(Bundle params) throws RemoteException { 309 mListener.onReadyForSpeech(params); 310 } 311 312 /** 313 * The service should call this method when recognition results are ready. 314 * 315 * @param results the recognition results. To retrieve the results in {@code 316 * ArrayList<String>} format use {@link Bundle#getStringArrayList(String)} with 317 * {@link SpeechRecognizer#RESULTS_RECOGNITION} as a parameter 318 */ results(Bundle results)319 public void results(Bundle results) throws RemoteException { 320 Message.obtain(mHandler, MSG_RESET).sendToTarget(); 321 mListener.onResults(results); 322 } 323 324 /** 325 * The service should call this method when the sound level in the audio stream has changed. 326 * There is no guarantee that this method will be called. 327 * 328 * @param rmsdB the new RMS dB value 329 */ rmsChanged(float rmsdB)330 public void rmsChanged(float rmsdB) throws RemoteException { 331 mListener.onRmsChanged(rmsdB); 332 } 333 334 /** 335 * Return the Linux uid assigned to the process that sent you the current transaction that 336 * is being processed. This is obtained from {@link Binder#getCallingUid()}. 337 */ getCallingUid()338 public int getCallingUid() { 339 return mCallingUid; 340 } 341 } 342 343 /** Binder of the recognition service */ 344 private static final class RecognitionServiceBinder extends IRecognitionService.Stub { 345 private final WeakReference<RecognitionService> mServiceRef; 346 RecognitionServiceBinder(RecognitionService service)347 public RecognitionServiceBinder(RecognitionService service) { 348 mServiceRef = new WeakReference<RecognitionService>(service); 349 } 350 351 @Override startListening(Intent recognizerIntent, IRecognitionListener listener)352 public void startListening(Intent recognizerIntent, IRecognitionListener listener) { 353 if (DBG) Log.d(TAG, "startListening called by:" + listener.asBinder()); 354 final RecognitionService service = mServiceRef.get(); 355 if (service != null && service.checkPermissions(listener, true /*forDataDelivery*/)) { 356 service.mHandler.sendMessage(Message.obtain(service.mHandler, 357 MSG_START_LISTENING, service.new StartListeningArgs( 358 recognizerIntent, listener, Binder.getCallingUid()))); 359 } 360 } 361 362 @Override stopListening(IRecognitionListener listener)363 public void stopListening(IRecognitionListener listener) { 364 if (DBG) Log.d(TAG, "stopListening called by:" + listener.asBinder()); 365 final RecognitionService service = mServiceRef.get(); 366 if (service != null && service.checkPermissions(listener, false /*forDataDelivery*/)) { 367 service.mHandler.sendMessage(Message.obtain(service.mHandler, 368 MSG_STOP_LISTENING, listener)); 369 } 370 } 371 372 @Override cancel(IRecognitionListener listener)373 public void cancel(IRecognitionListener listener) { 374 if (DBG) Log.d(TAG, "cancel called by:" + listener.asBinder()); 375 final RecognitionService service = mServiceRef.get(); 376 if (service != null && service.checkPermissions(listener, false /*forDataDelivery*/)) { 377 service.mHandler.sendMessage(Message.obtain(service.mHandler, 378 MSG_CANCEL, listener)); 379 } 380 } 381 clearReference()382 public void clearReference() { 383 mServiceRef.clear(); 384 } 385 } 386 } 387