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
This commit is contained in:
Przemyslaw Szczepaniak
2012-08-16 16:34:54 +01:00
committed by Android (Google) Code Review
parent bf5740e75e
commit 091d56cab8

View File

@@ -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,24 +1319,33 @@ 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) {
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) {
disconnect();
initTts();
}
return errorResult;
}
} catch (RemoteException ex) {
Log.e(TAG, method + " failed", ex);
if (reconnect) {
disconnect();
initTts();
}
return errorResult;
}
}
}