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