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:
committed by
Android (Google) Code Review
parent
bf5740e75e
commit
091d56cab8
@@ -1282,6 +1282,7 @@ public class TextToSpeech {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||||
Log.i(TAG, "Connected to " + name);
|
Log.i(TAG, "Connected to " + name);
|
||||||
synchronized(mStartLock) {
|
synchronized(mStartLock) {
|
||||||
@@ -1305,6 +1306,7 @@ public class TextToSpeech {
|
|||||||
return mCallback;
|
return mCallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void onServiceDisconnected(ComponentName name) {
|
public void onServiceDisconnected(ComponentName name) {
|
||||||
synchronized(mStartLock) {
|
synchronized(mStartLock) {
|
||||||
mService = null;
|
mService = null;
|
||||||
@@ -1317,24 +1319,33 @@ public class TextToSpeech {
|
|||||||
|
|
||||||
public void disconnect() {
|
public void disconnect() {
|
||||||
mContext.unbindService(this);
|
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) {
|
public <R> R runAction(Action<R> action, R errorResult, String method, boolean reconnect) {
|
||||||
try {
|
synchronized (mStartLock) {
|
||||||
synchronized (mStartLock) {
|
try {
|
||||||
if (mService == null) {
|
if (mService == null) {
|
||||||
Log.w(TAG, method + " failed: not connected to TTS engine");
|
Log.w(TAG, method + " failed: not connected to TTS engine");
|
||||||
return errorResult;
|
return errorResult;
|
||||||
}
|
}
|
||||||
return action.run(mService);
|
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user