Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 091d56ca authored by Przemyslaw Szczepaniak's avatar Przemyslaw Szczepaniak Committed by Android (Google) Code Review
Browse files

Fix double call to TTS connection disconnect() on reconnect

- Sets the service connection to null when unbindService is called,
instead of in onServiceDisconnected. This avoids a double disconnect
if a call to onServiceConnected is received before a call to
onServiceDisconnected.

- Extended synchronize on runAction error handling and reconnection.
This prevents from reconnecting N times if N>1 threads enter this method
while there's issue with TTS service.

Bug:6993880
Change-Id: I5a387622c6032a18d17fc072029ae6be1a9b8e6c
parent bf5740e7
Loading
Loading
Loading
Loading
+20 −9
Original line number Diff line number Diff line
@@ -1282,6 +1282,7 @@ public class TextToSpeech {
            }
        };

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "Connected to " + name);
            synchronized(mStartLock) {
@@ -1305,6 +1306,7 @@ public class TextToSpeech {
            return mCallback;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            synchronized(mStartLock) {
                mService = null;
@@ -1317,17 +1319,25 @@ public class TextToSpeech {

        public void disconnect() {
            mContext.unbindService(this);

            synchronized (mStartLock) {
                mService = null;
                // If this is the active connection, clear it
                if (mServiceConnection == this) {
                    mServiceConnection = null;
                }

            }
        }

        public <R> R runAction(Action<R> action, R errorResult, String method, boolean reconnect) {
            try {
            synchronized (mStartLock) {
                try {
                    if (mService == null) {
                        Log.w(TAG, method + " failed: not connected to TTS engine");
                        return errorResult;
                    }
                    return action.run(mService);
                }
                } catch (RemoteException ex) {
                    Log.e(TAG, method + " failed", ex);
                    if (reconnect) {
@@ -1338,6 +1348,7 @@ public class TextToSpeech {
                }
            }
        }
    }

    private interface Action<R> {
        R run(ITextToSpeechService service) throws RemoteException;