diff options
author | Jeff Brown <jeffbrown@google.com> | 2014-10-02 12:06:50 -0700 |
---|---|---|
committer | Jeff Brown <jeffbrown@google.com> | 2014-10-02 12:10:08 -0700 |
commit | 96156979022a6e1af4b29051aedd0d8e237018e8 (patch) | |
tree | af7ae891975fd8c450e74daf0ac92dfc658bc81b /core/java/android | |
parent | 134b62ed14b830b467423082036b3222f84f291c (diff) | |
download | frameworks_base-96156979022a6e1af4b29051aedd0d8e237018e8.zip frameworks_base-96156979022a6e1af4b29051aedd0d8e237018e8.tar.gz frameworks_base-96156979022a6e1af4b29051aedd0d8e237018e8.tar.bz2 |
Fix race in RecognizerService teardown.
Use a WeakReference to refer to the outer class to prevent leaks.
Ensure atomicity of access to the reference.
Bug: 17584947
Change-Id: I7ad7c7793b60fa125e04fc4d803ed905e8a00a95
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(); } } } |