diff options
Diffstat (limited to 'core/java')
| -rw-r--r-- | core/java/com/android/internal/accessibility/AccessibilityShortcutController.java | 58 |
1 files changed, 41 insertions, 17 deletions
diff --git a/core/java/com/android/internal/accessibility/AccessibilityShortcutController.java b/core/java/com/android/internal/accessibility/AccessibilityShortcutController.java index bb404654b741..5cdcab029877 100644 --- a/core/java/com/android/internal/accessibility/AccessibilityShortcutController.java +++ b/core/java/com/android/internal/accessibility/AccessibilityShortcutController.java @@ -412,8 +412,13 @@ public class AccessibilityShortcutController { * Class to wrap TextToSpeech for shortcut dialog spoken feedback. */ private class TtsPrompt implements TextToSpeech.OnInitListener { + private static final int RETRY_MILLIS = 1000; + private final CharSequence mText; + + private int mRetryCount = 3; private boolean mDismiss; + private boolean mLanguageReady = false; private TextToSpeech mTts; TtsPrompt(String serviceName) { @@ -437,17 +442,15 @@ public class AccessibilityShortcutController { playNotificationTone(); return; } - mHandler.sendMessage(PooledLambda.obtainMessage(TtsPrompt::play, this)); + mHandler.sendMessage(PooledLambda.obtainMessage( + TtsPrompt::waitForTtsReady, this)); } private void play() { if (mDismiss) { return; } - int status = TextToSpeech.ERROR; - if (setLanguage(Locale.getDefault())) { - status = mTts.speak(mText, TextToSpeech.QUEUE_FLUSH, null, null); - } + final int status = mTts.speak(mText, TextToSpeech.QUEUE_FLUSH, null, null); if (status != TextToSpeech.SUCCESS) { Slog.d(TAG, "Tts play fail"); playNotificationTone(); @@ -455,21 +458,42 @@ public class AccessibilityShortcutController { } /** - * @return false if tts language is not available + * Waiting for tts is ready to speak. Trying again if tts language pack is not available + * or tts voice data is not installed yet. */ - private boolean setLanguage(final Locale locale) { - int status = mTts.isLanguageAvailable(locale); - if (status == TextToSpeech.LANG_MISSING_DATA - || status == TextToSpeech.LANG_NOT_SUPPORTED) { - return false; + private void waitForTtsReady() { + if (mDismiss) { + return; + } + if (!mLanguageReady) { + final int status = mTts.setLanguage(Locale.getDefault()); + // True if language is available and TTS#loadVoice has called once + // that trigger TTS service to start initialization. + mLanguageReady = status != TextToSpeech.LANG_MISSING_DATA + && status != TextToSpeech.LANG_NOT_SUPPORTED; } - mTts.setLanguage(locale); - Voice voice = mTts.getVoice(); - if (voice == null || (voice.getFeatures() != null && voice.getFeatures() - .contains(TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED))) { - return false; + if (mLanguageReady) { + final Voice voice = mTts.getVoice(); + final boolean voiceDataInstalled = voice != null + && voice.getFeatures() != null + && !voice.getFeatures().contains( + TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED); + if (voiceDataInstalled) { + mHandler.sendMessage(PooledLambda.obtainMessage( + TtsPrompt::play, this)); + return; + } + } + + if (mRetryCount == 0) { + Slog.d(TAG, "Tts not ready to speak."); + playNotificationTone(); + return; } - return true; + // Retry if TTS service not ready yet. + mRetryCount -= 1; + mHandler.sendMessageDelayed(PooledLambda.obtainMessage( + TtsPrompt::waitForTtsReady, this), RETRY_MILLIS); } } |
