13da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov/* 23da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * Copyright (C) 2010 The Android Open Source Project 33da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * 43da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * Licensed under the Apache License, Version 2.0 (the "License"); you may not 53da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * use this file except in compliance with the License. You may obtain a copy of 63da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * the License at 73da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * 83da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * http://www.apache.org/licenses/LICENSE-2.0 93da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * 103da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * Unless required by applicable law or agreed to in writing, software 113da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 123da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 133da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * License for the specific language governing permissions and limitations under 143da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * the License. 153da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 163da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 173da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsovpackage android.speech; 183da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 19378ae126d9d61feed77ad596fa0823394eac6240Mike LeBeauimport android.annotation.SdkConstant; 20378ae126d9d61feed77ad596fa0823394eac6240Mike LeBeauimport android.annotation.SdkConstant.SdkConstantType; 213da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsovimport android.app.Service; 223da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsovimport android.content.Intent; 233da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsovimport android.content.pm.PackageManager; 248fedcdad3691e613ed192f4beea18926056d251dCedric Hoimport android.os.Binder; 253da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsovimport android.os.Bundle; 263da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsovimport android.os.Handler; 273da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsovimport android.os.IBinder; 283da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsovimport android.os.Message; 293da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsovimport android.os.RemoteException; 303da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsovimport android.util.Log; 313da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 3296156979022a6e1af4b29051aedd0d8e237018e8Jeff Brownimport java.lang.ref.WeakReference; 3396156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown 343da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov/** 353da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * This class provides a base class for recognition service implementations. This class should be 362f853ea47a194680657ddc2cc54336a14f38b8baMike LeBeau * extended only in case you wish to implement a new speech recognizer. Please note that the 372f853ea47a194680657ddc2cc54336a14f38b8baMike LeBeau * implementation of this service is stateless. 383da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 393da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsovpublic abstract class RecognitionService extends Service { 40378ae126d9d61feed77ad596fa0823394eac6240Mike LeBeau /** 41378ae126d9d61feed77ad596fa0823394eac6240Mike LeBeau * The {@link Intent} that must be declared as handled by the service. 42378ae126d9d61feed77ad596fa0823394eac6240Mike LeBeau */ 43378ae126d9d61feed77ad596fa0823394eac6240Mike LeBeau @SdkConstant(SdkConstantType.SERVICE_ACTION) 44378ae126d9d61feed77ad596fa0823394eac6240Mike LeBeau public static final String SERVICE_INTERFACE = "android.speech.RecognitionService"; 45378ae126d9d61feed77ad596fa0823394eac6240Mike LeBeau 46378ae126d9d61feed77ad596fa0823394eac6240Mike LeBeau /** 47378ae126d9d61feed77ad596fa0823394eac6240Mike LeBeau * Name under which a RecognitionService component publishes information about itself. 48378ae126d9d61feed77ad596fa0823394eac6240Mike LeBeau * This meta-data should reference an XML resource containing a 49378ae126d9d61feed77ad596fa0823394eac6240Mike LeBeau * <code><{@link android.R.styleable#RecognitionService recognition-service}></code> tag. 50378ae126d9d61feed77ad596fa0823394eac6240Mike LeBeau */ 51378ae126d9d61feed77ad596fa0823394eac6240Mike LeBeau public static final String SERVICE_META_DATA = "android.speech"; 523da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 533da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** Log messages identifier */ 543da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov private static final String TAG = "RecognitionService"; 553da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 563da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** Debugging flag */ 573da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov private static final boolean DBG = false; 583da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 591c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov /** Binder of the recognition service */ 601c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov private RecognitionServiceBinder mBinder = new RecognitionServiceBinder(this); 611c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov 623da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** 633da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * The current callback of an application that invoked the 643da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * {@link RecognitionService#onStartListening(Intent, Callback)} method 653da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 663da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov private Callback mCurrentCallback = null; 673da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 683da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov private static final int MSG_START_LISTENING = 1; 693da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 703da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov private static final int MSG_STOP_LISTENING = 2; 713da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 723da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov private static final int MSG_CANCEL = 3; 733da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 74483701eb9f36a322ca7fa6cad76fa849a756810aValentin Kravtsov private static final int MSG_RESET = 4; 75483701eb9f36a322ca7fa6cad76fa849a756810aValentin Kravtsov 763da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov private final Handler mHandler = new Handler() { 773da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov @Override 783da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov public void handleMessage(Message msg) { 793da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov switch (msg.what) { 803da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov case MSG_START_LISTENING: 813da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov StartListeningArgs args = (StartListeningArgs) msg.obj; 828fedcdad3691e613ed192f4beea18926056d251dCedric Ho dispatchStartListening(args.mIntent, args.mListener, args.mCallingUid); 833da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov break; 843da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov case MSG_STOP_LISTENING: 853da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov dispatchStopListening((IRecognitionListener) msg.obj); 863da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov break; 873da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov case MSG_CANCEL: 883da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov dispatchCancel((IRecognitionListener) msg.obj); 89483701eb9f36a322ca7fa6cad76fa849a756810aValentin Kravtsov break; 90483701eb9f36a322ca7fa6cad76fa849a756810aValentin Kravtsov case MSG_RESET: 91483701eb9f36a322ca7fa6cad76fa849a756810aValentin Kravtsov dispatchClearCallback(); 92483701eb9f36a322ca7fa6cad76fa849a756810aValentin Kravtsov break; 933da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 943da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 953da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov }; 963da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 978fedcdad3691e613ed192f4beea18926056d251dCedric Ho private void dispatchStartListening(Intent intent, final IRecognitionListener listener, 988fedcdad3691e613ed192f4beea18926056d251dCedric Ho int callingUid) { 993da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov if (mCurrentCallback == null) { 1003da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov if (DBG) Log.d(TAG, "created new mCurrentCallback, listener = " + listener.asBinder()); 101c1fb6dc1a494d73a080348d16b96e70f5735e036Jerome Poichet try { 102c1fb6dc1a494d73a080348d16b96e70f5735e036Jerome Poichet listener.asBinder().linkToDeath(new IBinder.DeathRecipient() { 103c1fb6dc1a494d73a080348d16b96e70f5735e036Jerome Poichet @Override 104c1fb6dc1a494d73a080348d16b96e70f5735e036Jerome Poichet public void binderDied() { 105c1fb6dc1a494d73a080348d16b96e70f5735e036Jerome Poichet mHandler.sendMessage(mHandler.obtainMessage(MSG_CANCEL, listener)); 106c1fb6dc1a494d73a080348d16b96e70f5735e036Jerome Poichet } 107c1fb6dc1a494d73a080348d16b96e70f5735e036Jerome Poichet }, 0); 108c1fb6dc1a494d73a080348d16b96e70f5735e036Jerome Poichet } catch (RemoteException re) { 109c1fb6dc1a494d73a080348d16b96e70f5735e036Jerome Poichet Log.e(TAG, "dead listener on startListening"); 110c1fb6dc1a494d73a080348d16b96e70f5735e036Jerome Poichet return; 111c1fb6dc1a494d73a080348d16b96e70f5735e036Jerome Poichet } 1128fedcdad3691e613ed192f4beea18926056d251dCedric Ho mCurrentCallback = new Callback(listener, callingUid); 1133da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov RecognitionService.this.onStartListening(intent, mCurrentCallback); 1143da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } else { 1153da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov try { 1162a5d9f9b577376768372837723f0f42098aba13bJean-Michel Trivi listener.onError(SpeechRecognizer.ERROR_RECOGNIZER_BUSY); 1173da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } catch (RemoteException e) { 1183da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov Log.d(TAG, "onError call from startListening failed"); 1193da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 1203da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov Log.i(TAG, "concurrent startListening received - ignoring this call"); 1213da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 1223da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 1233da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 1243da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov private void dispatchStopListening(IRecognitionListener listener) { 1253da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov try { 1263da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov if (mCurrentCallback == null) { 1272a5d9f9b577376768372837723f0f42098aba13bJean-Michel Trivi listener.onError(SpeechRecognizer.ERROR_CLIENT); 1283da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov Log.w(TAG, "stopListening called with no preceding startListening - ignoring"); 1293da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } else if (mCurrentCallback.mListener.asBinder() != listener.asBinder()) { 1302a5d9f9b577376768372837723f0f42098aba13bJean-Michel Trivi listener.onError(SpeechRecognizer.ERROR_RECOGNIZER_BUSY); 1313da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov Log.w(TAG, "stopListening called by other caller than startListening - ignoring"); 1323da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } else { // the correct state 1333da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov RecognitionService.this.onStopListening(mCurrentCallback); 1343da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 1353da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } catch (RemoteException e) { // occurs if onError fails 1363da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov Log.d(TAG, "onError call from stopListening failed"); 1373da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 1383da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 1393da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 1403da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov private void dispatchCancel(IRecognitionListener listener) { 1413da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov if (mCurrentCallback == null) { 1422b0c7ab6891294473136bb6f7f2e751d133ae8cfValentin Kravtsov if (DBG) Log.d(TAG, "cancel called with no preceding startListening - ignoring"); 1433da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } else if (mCurrentCallback.mListener.asBinder() != listener.asBinder()) { 1443da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov Log.w(TAG, "cancel called by client who did not call startListening - ignoring"); 1453da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } else { // the correct state 1463da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov RecognitionService.this.onCancel(mCurrentCallback); 1473da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov mCurrentCallback = null; 1483da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov if (DBG) Log.d(TAG, "canceling - setting mCurrentCallback to null"); 1493da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 1503da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 1513da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 152483701eb9f36a322ca7fa6cad76fa849a756810aValentin Kravtsov private void dispatchClearCallback() { 153483701eb9f36a322ca7fa6cad76fa849a756810aValentin Kravtsov mCurrentCallback = null; 154483701eb9f36a322ca7fa6cad76fa849a756810aValentin Kravtsov } 155483701eb9f36a322ca7fa6cad76fa849a756810aValentin Kravtsov 1563da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov private class StartListeningArgs { 1573da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov public final Intent mIntent; 1583da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 1593da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov public final IRecognitionListener mListener; 1608fedcdad3691e613ed192f4beea18926056d251dCedric Ho public final int mCallingUid; 1613da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 1628fedcdad3691e613ed192f4beea18926056d251dCedric Ho public StartListeningArgs(Intent intent, IRecognitionListener listener, int callingUid) { 1633da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov this.mIntent = intent; 1643da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov this.mListener = listener; 1658fedcdad3691e613ed192f4beea18926056d251dCedric Ho this.mCallingUid = callingUid; 1663da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 1673da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 1683da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 1693da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** 1703da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * Checks whether the caller has sufficient permissions 1713da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * 1723da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * @param listener to send the error message to in case of error 1733da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * @return {@code true} if the caller has enough permissions, {@code false} otherwise 1743da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 1753da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov private boolean checkPermissions(IRecognitionListener listener) { 1763da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov if (DBG) Log.d(TAG, "checkPermissions"); 1773da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov if (RecognitionService.this.checkCallingOrSelfPermission(android.Manifest.permission. 1783da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) { 1793da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov return true; 1803da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 1813da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov try { 1823da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov Log.e(TAG, "call for recognition service without RECORD_AUDIO permissions"); 1832a5d9f9b577376768372837723f0f42098aba13bJean-Michel Trivi listener.onError(SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS); 1843da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } catch (RemoteException re) { 1853da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov Log.e(TAG, "sending ERROR_INSUFFICIENT_PERMISSIONS message failed", re); 1863da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 1873da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov return false; 1883da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 1893da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 1903da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** 1913da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * Notifies the service that it should start listening for speech. 1923da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * 1933da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * @param recognizerIntent contains parameters for the recognition to be performed. The intent 1943da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * may also contain optional extras, see {@link RecognizerIntent}. If these values are 1953da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * not set explicitly, default values should be used by the recognizer. 1963da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * @param listener that will receive the service's callbacks 1973da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 1983da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov protected abstract void onStartListening(Intent recognizerIntent, Callback listener); 1993da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 2003da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** 2013da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * Notifies the service that it should cancel the speech recognition. 2023da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 2033da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov protected abstract void onCancel(Callback listener); 2043da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 2053da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** 2063da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * Notifies the service that it should stop listening for speech. Speech captured so far should 2073da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * be recognized as if the user had stopped speaking at this point. This method is only called 2083da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * if the application calls it explicitly. 2093da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 2103da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov protected abstract void onStopListening(Callback listener); 2113da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 2123da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov @Override 2133da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov public final IBinder onBind(final Intent intent) { 2143da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov if (DBG) Log.d(TAG, "onBind, intent=" + intent); 2153da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov return mBinder; 2163da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 2173da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 2181c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov @Override 2191c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov public void onDestroy() { 2201c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov if (DBG) Log.d(TAG, "onDestroy"); 2211c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov mCurrentCallback = null; 2221c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov mBinder.clearReference(); 2231c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov super.onDestroy(); 2241c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov } 2251c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov 2263da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** 2273da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * This class receives callbacks from the speech recognition service and forwards them to the 2283da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * user. An instance of this class is passed to the 2293da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * {@link RecognitionService#onStartListening(Intent, Callback)} method. Recognizers may call 2303da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * these methods on any thread. 2313da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 2323da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov public class Callback { 2333da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov private final IRecognitionListener mListener; 2348fedcdad3691e613ed192f4beea18926056d251dCedric Ho private final int mCallingUid; 2353da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 2368fedcdad3691e613ed192f4beea18926056d251dCedric Ho private Callback(IRecognitionListener listener, int callingUid) { 2373da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov mListener = listener; 2388fedcdad3691e613ed192f4beea18926056d251dCedric Ho mCallingUid = callingUid; 2393da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 2403da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 2413da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** 2423da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * The service should call this method when the user has started to speak. 2433da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 2443da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov public void beginningOfSpeech() throws RemoteException { 2453da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov if (DBG) Log.d(TAG, "beginningOfSpeech"); 2463da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov mListener.onBeginningOfSpeech(); 2473da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 2483da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 2493da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** 2503da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * The service should call this method when sound has been received. The purpose of this 2513da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * function is to allow giving feedback to the user regarding the captured audio. 2523da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * 2533da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * @param buffer a buffer containing a sequence of big-endian 16-bit integers representing a 2543da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * single channel audio stream. The sample rate is implementation dependent. 2553da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 2563da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov public void bufferReceived(byte[] buffer) throws RemoteException { 2573da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov mListener.onBufferReceived(buffer); 2583da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 2593da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 2603da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** 2613da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * The service should call this method after the user stops speaking. 2623da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 2633da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov public void endOfSpeech() throws RemoteException { 2643da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov mListener.onEndOfSpeech(); 2653da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 2663da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 2673da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** 2683da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * The service should call this method when a network or recognition error occurred. 2693da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * 2702a5d9f9b577376768372837723f0f42098aba13bJean-Michel Trivi * @param error code is defined in {@link SpeechRecognizer} 2713da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 2723da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov public void error(int error) throws RemoteException { 273483701eb9f36a322ca7fa6cad76fa849a756810aValentin Kravtsov Message.obtain(mHandler, MSG_RESET).sendToTarget(); 2743da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov mListener.onError(error); 2753da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 2763da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 2773da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** 2783da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * The service should call this method when partial recognition results are available. This 2793da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * method can be called at any time between {@link #beginningOfSpeech()} and 2803da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * {@link #results(Bundle)} when partial results are ready. This method may be called zero, 2812a5d9f9b577376768372837723f0f42098aba13bJean-Michel Trivi * one or multiple times for each call to {@link SpeechRecognizer#startListening(Intent)}, 2823da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * depending on the speech recognition service implementation. 2833da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * 2843da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * @param partialResults the returned results. To retrieve the results in 2853da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * ArrayList<String> format use {@link Bundle#getStringArrayList(String)} with 2862a5d9f9b577376768372837723f0f42098aba13bJean-Michel Trivi * {@link SpeechRecognizer#RESULTS_RECOGNITION} as a parameter 2873da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 2883da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov public void partialResults(Bundle partialResults) throws RemoteException { 2893da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov mListener.onPartialResults(partialResults); 2903da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 2913da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 2923da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** 2933da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * The service should call this method when the endpointer is ready for the user to start 2943da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * speaking. 2953da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * 2963da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * @param params parameters set by the recognition service. Reserved for future use. 2973da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 2983da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov public void readyForSpeech(Bundle params) throws RemoteException { 2993da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov mListener.onReadyForSpeech(params); 3003da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 3013da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 3023da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** 3033da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * The service should call this method when recognition results are ready. 3043da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * 3053da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * @param results the recognition results. To retrieve the results in {@code 30671fbb81b14958b80fe55738607740c6630e4e9daNeil Fuller * ArrayList<String>} format use {@link Bundle#getStringArrayList(String)} with 3072a5d9f9b577376768372837723f0f42098aba13bJean-Michel Trivi * {@link SpeechRecognizer#RESULTS_RECOGNITION} as a parameter 3083da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 3093da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov public void results(Bundle results) throws RemoteException { 310483701eb9f36a322ca7fa6cad76fa849a756810aValentin Kravtsov Message.obtain(mHandler, MSG_RESET).sendToTarget(); 3113da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov mListener.onResults(results); 3123da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 3133da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov 3143da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov /** 3153da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * The service should call this method when the sound level in the audio stream has changed. 3163da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * There is no guarantee that this method will be called. 3173da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * 3183da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov * @param rmsdB the new RMS dB value 3193da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov */ 3203da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov public void rmsChanged(float rmsdB) throws RemoteException { 3213da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov mListener.onRmsChanged(rmsdB); 3223da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 3238fedcdad3691e613ed192f4beea18926056d251dCedric Ho 3248fedcdad3691e613ed192f4beea18926056d251dCedric Ho /** 3258fedcdad3691e613ed192f4beea18926056d251dCedric Ho * Return the Linux uid assigned to the process that sent you the current transaction that 3268fedcdad3691e613ed192f4beea18926056d251dCedric Ho * is being processed. This is obtained from {@link Binder#getCallingUid()}. 3278fedcdad3691e613ed192f4beea18926056d251dCedric Ho */ 3288fedcdad3691e613ed192f4beea18926056d251dCedric Ho public int getCallingUid() { 3298fedcdad3691e613ed192f4beea18926056d251dCedric Ho return mCallingUid; 3308fedcdad3691e613ed192f4beea18926056d251dCedric Ho } 3313da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov } 3321c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov 3331c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov /** Binder of the recognition service */ 33496156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown private static final class RecognitionServiceBinder extends IRecognitionService.Stub { 33596156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown private final WeakReference<RecognitionService> mServiceRef; 3361c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov 3371c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov public RecognitionServiceBinder(RecognitionService service) { 33896156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown mServiceRef = new WeakReference<RecognitionService>(service); 3391c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov } 3401c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov 34196156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown @Override 3421c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov public void startListening(Intent recognizerIntent, IRecognitionListener listener) { 3431c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov if (DBG) Log.d(TAG, "startListening called by:" + listener.asBinder()); 34496156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown final RecognitionService service = mServiceRef.get(); 34596156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown if (service != null && service.checkPermissions(listener)) { 34696156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown service.mHandler.sendMessage(Message.obtain(service.mHandler, 34796156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown MSG_START_LISTENING, service.new StartListeningArgs( 3488fedcdad3691e613ed192f4beea18926056d251dCedric Ho recognizerIntent, listener, Binder.getCallingUid()))); 3491c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov } 3501c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov } 3511c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov 35296156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown @Override 3531c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov public void stopListening(IRecognitionListener listener) { 3541c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov if (DBG) Log.d(TAG, "stopListening called by:" + listener.asBinder()); 35596156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown final RecognitionService service = mServiceRef.get(); 35696156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown if (service != null && service.checkPermissions(listener)) { 35796156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown service.mHandler.sendMessage(Message.obtain(service.mHandler, 3581c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov MSG_STOP_LISTENING, listener)); 3591c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov } 3601c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov } 3611c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov 36296156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown @Override 3631c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov public void cancel(IRecognitionListener listener) { 3641c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov if (DBG) Log.d(TAG, "cancel called by:" + listener.asBinder()); 36596156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown final RecognitionService service = mServiceRef.get(); 36696156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown if (service != null && service.checkPermissions(listener)) { 36796156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown service.mHandler.sendMessage(Message.obtain(service.mHandler, 3681c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov MSG_CANCEL, listener)); 3691c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov } 3701c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov } 3711c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov 3721c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov public void clearReference() { 37396156979022a6e1af4b29051aedd0d8e237018e8Jeff Brown mServiceRef.clear(); 3741c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov } 3751c3cca0abed55516d2c67f2f11fc888a6a66f341Valentin Kravtsov } 3763da3cad97269d694a6153771fb4a0c3775ca6ab5Valentin Kravtsov} 377