Skip to content

Commit 68e2af5

Browse files
committed
Fix TTS instantiation from webview accessibility handlers.
The TTS instantiated from here shouldn't clobber any existing TTS objects opened within the same package context. Ideally, the TTS API should work fine with multiple TTS object instances within the same package context but making that happen correctly is a larger change. bug:5659758 Change-Id: Ia1f63c61b9f12ac92ff42a427a004d414e42a759
1 parent e35581a commit 68e2af5

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ public class Engine {
490490
private final Map<String, Uri> mUtterances;
491491
private final Bundle mParams = new Bundle();
492492
private final TtsEngines mEnginesHelper;
493+
private final String mPackageName;
493494
private volatile String mCurrentEngine = null;
494495

495496
/**
@@ -518,19 +519,36 @@ public TextToSpeech(Context context, OnInitListener listener) {
518519
* @param engine Package name of the TTS engine to use.
519520
*/
520521
public TextToSpeech(Context context, OnInitListener listener, String engine) {
522+
this(context, listener, engine, null);
523+
}
524+
525+
/**
526+
* Used by the framework to instantiate TextToSpeech objects with a supplied
527+
* package name, instead of using {@link android.content.Context#getPackageName()}
528+
*
529+
* @hide
530+
*/
531+
public TextToSpeech(Context context, OnInitListener listener, String engine,
532+
String packageName) {
521533
mContext = context;
522534
mInitListener = listener;
523535
mRequestedEngine = engine;
524536

525537
mEarcons = new HashMap<String, Uri>();
526538
mUtterances = new HashMap<String, Uri>();
539+
mUtteranceProgressListener = null;
527540

528541
mEnginesHelper = new TtsEngines(mContext);
542+
if (packageName != null) {
543+
mPackageName = packageName;
544+
} else {
545+
mPackageName = mContext.getPackageName();
546+
}
529547
initTts();
530548
}
531549

532550
private String getPackageName() {
533-
return mContext.getPackageName();
551+
return mPackageName;
534552
}
535553

536554
private <R> R runActionNoReconnect(Action<R> action, R errorResult, String method) {

core/java/android/speech/tts/TextToSpeechService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,23 +450,23 @@ public void stop() {
450450
@Override
451451
public void dispatchOnDone() {
452452
final String utteranceId = getUtteranceId();
453-
if (!TextUtils.isEmpty(utteranceId)) {
453+
if (utteranceId != null) {
454454
mCallbacks.dispatchOnDone(getCallingApp(), utteranceId);
455455
}
456456
}
457457

458458
@Override
459459
public void dispatchOnStart() {
460460
final String utteranceId = getUtteranceId();
461-
if (!TextUtils.isEmpty(utteranceId)) {
461+
if (utteranceId != null) {
462462
mCallbacks.dispatchOnStart(getCallingApp(), utteranceId);
463463
}
464464
}
465465

466466
@Override
467467
public void dispatchOnError() {
468468
final String utteranceId = getUtteranceId();
469-
if (!TextUtils.isEmpty(utteranceId)) {
469+
if (utteranceId != null) {
470470
mCallbacks.dispatchOnError(getCallingApp(), utteranceId);
471471
}
472472
}

core/java/android/webkit/WebView.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,8 +1306,15 @@ private void addAccessibilityApisToJavaScript() {
13061306
if (AccessibilityManager.getInstance(mContext).isEnabled()
13071307
&& getSettings().getJavaScriptEnabled()) {
13081308
// exposing the TTS for now ...
1309-
mTextToSpeech = new TextToSpeech(getContext(), null);
1310-
addJavascriptInterface(mTextToSpeech, ALIAS_ACCESSIBILITY_JS_INTERFACE);
1309+
final Context ctx = getContext();
1310+
if (ctx != null) {
1311+
final String packageName = ctx.getPackageName();
1312+
if (packageName != null) {
1313+
mTextToSpeech = new TextToSpeech(getContext(), null, null,
1314+
packageName + ".**webview**");
1315+
addJavascriptInterface(mTextToSpeech, ALIAS_ACCESSIBILITY_JS_INTERFACE);
1316+
}
1317+
}
13111318
}
13121319
}
13131320

0 commit comments

Comments
 (0)