diff options
author | Jeff Brown <jeffbrown@google.com> | 2014-10-02 22:09:08 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2014-10-02 22:09:08 +0000 |
commit | b82bf9df111031e0f3afa4df29fbc22460e6cc8b (patch) | |
tree | 0cc88553a5eac826f0a350481b9d1aabd7d4e9df /core/java/android | |
parent | 023d6e2292c308b5caddd0172cdbb0552c8ca50d (diff) | |
parent | ae309489c2b22c6cfcc6701b5dea13c652239ecd (diff) | |
download | frameworks_base-b82bf9df111031e0f3afa4df29fbc22460e6cc8b.zip frameworks_base-b82bf9df111031e0f3afa4df29fbc22460e6cc8b.tar.gz frameworks_base-b82bf9df111031e0f3afa4df29fbc22460e6cc8b.tar.bz2 |
am ae309489: Merge "Fix race in RecognizerService teardown." into lmp-dev
* commit 'ae309489c2b22c6cfcc6701b5dea13c652239ecd':
Fix race in RecognizerService teardown.
Diffstat (limited to 'core/java/android')
-rw-r--r-- | core/java/android/speech/RecognitionService.java | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/core/java/android/speech/RecognitionService.java b/core/java/android/speech/RecognitionService.java index 45eb0bf..dcdbba7 100644 --- a/core/java/android/speech/RecognitionService.java +++ b/core/java/android/speech/RecognitionService.java @@ -28,6 +28,8 @@ import android.os.Message; import android.os.RemoteException; import android.util.Log; +import java.lang.ref.WeakReference; + /** * This class provides a base class for recognition service implementations. This class should be * extended only in case you wish to implement a new speech recognizer. Please note that the @@ -315,40 +317,46 @@ public abstract class RecognitionService extends Service { } /** Binder of the recognition service */ - private static class RecognitionServiceBinder extends IRecognitionService.Stub { - private RecognitionService mInternalService; + private static final class RecognitionServiceBinder extends IRecognitionService.Stub { + private final WeakReference<RecognitionService> mServiceRef; public RecognitionServiceBinder(RecognitionService service) { - mInternalService = service; + mServiceRef = new WeakReference<RecognitionService>(service); } + @Override public void startListening(Intent recognizerIntent, IRecognitionListener listener) { if (DBG) Log.d(TAG, "startListening called by:" + listener.asBinder()); - if (mInternalService != null && mInternalService.checkPermissions(listener)) { - mInternalService.mHandler.sendMessage(Message.obtain(mInternalService.mHandler, - MSG_START_LISTENING, mInternalService.new StartListeningArgs( + final RecognitionService service = mServiceRef.get(); + if (service != null && service.checkPermissions(listener)) { + service.mHandler.sendMessage(Message.obtain(service.mHandler, + MSG_START_LISTENING, service.new StartListeningArgs( recognizerIntent, listener))); } } + @Override public void stopListening(IRecognitionListener listener) { if (DBG) Log.d(TAG, "stopListening called by:" + listener.asBinder()); - if (mInternalService != null && mInternalService.checkPermissions(listener)) { - mInternalService.mHandler.sendMessage(Message.obtain(mInternalService.mHandler, + final RecognitionService service = mServiceRef.get(); + if (service != null && service.checkPermissions(listener)) { + service.mHandler.sendMessage(Message.obtain(service.mHandler, MSG_STOP_LISTENING, listener)); } } + @Override public void cancel(IRecognitionListener listener) { if (DBG) Log.d(TAG, "cancel called by:" + listener.asBinder()); - if (mInternalService != null && mInternalService.checkPermissions(listener)) { - mInternalService.mHandler.sendMessage(Message.obtain(mInternalService.mHandler, + final RecognitionService service = mServiceRef.get(); + if (service != null && service.checkPermissions(listener)) { + service.mHandler.sendMessage(Message.obtain(service.mHandler, MSG_CANCEL, listener)); } } public void clearReference() { - mInternalService = null; + mServiceRef.clear(); } } } |