Skip to content

Commit 091d56c

Browse files
pszcAndroid (Google) Code Review
authored andcommitted
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
1 parent bf5740e commit 091d56c

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

core/java/android/speech/tts/TextToSpeech.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,7 @@ public void onStart(String utteranceId) {
12821282
}
12831283
};
12841284

1285+
@Override
12851286
public void onServiceConnected(ComponentName name, IBinder service) {
12861287
Log.i(TAG, "Connected to " + name);
12871288
synchronized(mStartLock) {
@@ -1305,6 +1306,7 @@ public IBinder getCallerIdentity() {
13051306
return mCallback;
13061307
}
13071308

1309+
@Override
13081310
public void onServiceDisconnected(ComponentName name) {
13091311
synchronized(mStartLock) {
13101312
mService = null;
@@ -1317,24 +1319,33 @@ public void onServiceDisconnected(ComponentName name) {
13171319

13181320
public void disconnect() {
13191321
mContext.unbindService(this);
1322+
1323+
synchronized (mStartLock) {
1324+
mService = null;
1325+
// If this is the active connection, clear it
1326+
if (mServiceConnection == this) {
1327+
mServiceConnection = null;
1328+
}
1329+
1330+
}
13201331
}
13211332

13221333
public <R> R runAction(Action<R> action, R errorResult, String method, boolean reconnect) {
1323-
try {
1324-
synchronized (mStartLock) {
1334+
synchronized (mStartLock) {
1335+
try {
13251336
if (mService == null) {
13261337
Log.w(TAG, method + " failed: not connected to TTS engine");
13271338
return errorResult;
13281339
}
13291340
return action.run(mService);
1341+
} catch (RemoteException ex) {
1342+
Log.e(TAG, method + " failed", ex);
1343+
if (reconnect) {
1344+
disconnect();
1345+
initTts();
1346+
}
1347+
return errorResult;
13301348
}
1331-
} catch (RemoteException ex) {
1332-
Log.e(TAG, method + " failed", ex);
1333-
if (reconnect) {
1334-
disconnect();
1335-
initTts();
1336-
}
1337-
return errorResult;
13381349
}
13391350
}
13401351
}

0 commit comments

Comments
 (0)