diff options
author | Hui Wang <huiwang@google.com> | 2022-08-09 17:03:37 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2022-08-09 17:03:37 +0000 |
commit | 6f395a9d54b88f4f45ecb02f4ee1e8f06251b020 (patch) | |
tree | dfc445b3d939e5c6c33e0c9d9711a60e9b630e8e | |
parent | d0865152fc8edfcc9975bf785d783170146869ba (diff) | |
parent | 7f0f2d94fa7c2728737f4bd46e730d4275d6ce64 (diff) | |
download | CellBroadcastReceiver-6f395a9d54b88f4f45ecb02f4ee1e8f06251b020.tar.gz |
Merge "Flush the text to speech queue before speaking the alert" into tm-mainline-prod
-rw-r--r-- | src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java | 2 | ||||
-rw-r--r-- | tests/unit/src/com/android/cellbroadcastreceiver/unit/CellBroadcastAlertAudioTest.java | 70 |
2 files changed, 72 insertions, 0 deletions
diff --git a/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java b/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java index 7f2adf3c8..cbfbd44ad 100644 --- a/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java +++ b/src/com/android/cellbroadcastreceiver/CellBroadcastAlertAudio.java @@ -256,6 +256,8 @@ public class CellBroadcastAlertAudio extends Service implements TextToSpeech.OnI if (DBG) log("Speaking broadcast text: " + mMessageBody); mTts.setAudioAttributes(getAlertAudioAttributes()); + // Flush the text to speech queue + mTts.speak("", TextToSpeech.QUEUE_FLUSH, null, null); res = mTts.speak(mMessageBody, 2, null, TTS_UTTERANCE_ID); mIsTextToSpeechSpeaking = true; setState(STATE_SPEAKING); diff --git a/tests/unit/src/com/android/cellbroadcastreceiver/unit/CellBroadcastAlertAudioTest.java b/tests/unit/src/com/android/cellbroadcastreceiver/unit/CellBroadcastAlertAudioTest.java index 86e64de8d..2bb87cb96 100644 --- a/tests/unit/src/com/android/cellbroadcastreceiver/unit/CellBroadcastAlertAudioTest.java +++ b/tests/unit/src/com/android/cellbroadcastreceiver/unit/CellBroadcastAlertAudioTest.java @@ -23,6 +23,7 @@ import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -31,7 +32,9 @@ import android.content.res.Configuration; import android.media.AudioAttributes; import android.media.AudioDeviceInfo; import android.media.AudioManager; +import android.os.Handler; import android.os.HandlerThread; +import android.speech.tts.TextToSpeech; import android.telephony.TelephonyManager; import com.android.cellbroadcastreceiver.CellBroadcastAlertAudio; @@ -39,8 +42,11 @@ import com.android.cellbroadcastreceiver.CellBroadcastSettings; import org.junit.After; import org.junit.Before; +import org.mockito.ArgumentCaptor; import org.mockito.MockitoAnnotations; +import java.lang.reflect.Field; + public class CellBroadcastAlertAudioTest extends CellBroadcastServiceTestCase<CellBroadcastAlertAudio> { @@ -242,6 +248,70 @@ public class CellBroadcastAlertAudioTest extends phoneStateListenerHandler.quit(); } + public void testStartServiceWithTts() throws Throwable { + PhoneStateListenerHandler phoneStateListenerHandler = new PhoneStateListenerHandler( + "testStartService", + () -> { + doReturn(AudioManager.RINGER_MODE_NORMAL).when( + mMockedAudioManager).getRingerMode(); + + Intent intent = new Intent(mContext, CellBroadcastAlertAudio.class); + intent.setAction(SHOW_NEW_ALERT_ACTION); + intent.putExtra(CellBroadcastAlertAudio.ALERT_AUDIO_MESSAGE_BODY, + TEST_MESSAGE_BODY); + intent.putExtra(CellBroadcastAlertAudio.ALERT_AUDIO_VIBRATION_PATTERN_EXTRA, + TEST_VIBRATION_PATTERN); + intent.putExtra(CellBroadcastAlertAudio.ALERT_AUDIO_MESSAGE_LANGUAGE, + TEST_MESSAGE_LANGUAGE); + intent.putExtra(CellBroadcastAlertAudio.ALERT_AUDIO_OVERRIDE_DND_EXTRA, + true); + startService(intent); + }); + phoneStateListenerHandler.start(); + waitUntilReady(); + + CellBroadcastAlertAudio audio = (CellBroadcastAlertAudio) getService(); + Field fieldTts = CellBroadcastAlertAudio.class.getDeclaredField("mTts"); + fieldTts.setAccessible(true); + TextToSpeech mockTts = mock(TextToSpeech.class); + fieldTts.set(audio, mockTts); + + Field fieldTtsEngineReady = CellBroadcastAlertAudio.class + .getDeclaredField("mTtsEngineReady"); + fieldTtsEngineReady.setAccessible(true); + fieldTtsEngineReady.set(audio, true); + + Field fieldTtsLanguageSupported = CellBroadcastAlertAudio.class + .getDeclaredField("mTtsLanguageSupported"); + fieldTtsLanguageSupported.setAccessible(true); + fieldTtsLanguageSupported.set(audio, true); + + Field fieldHandler = CellBroadcastAlertAudio.class.getDeclaredField("mHandler"); + fieldHandler.setAccessible(true); + Handler handler = (Handler) fieldHandler.get(audio); + + Field fieldSpeaking = CellBroadcastAlertAudio.class + .getDeclaredField("mIsTextToSpeechSpeaking"); + fieldSpeaking.setAccessible(true); + ArgumentCaptor<Integer> queueMode = ArgumentCaptor.forClass(Integer.class); + + // Send empty message of ALERT_PAUSE_FINISHED to trigger tts + handler.sendEmptyMessage(1001); + for (int i = 0; i < 10; i++) { + if (fieldSpeaking.getBoolean(audio)) { + break; + } + Thread.sleep(100); + } + + verify(mockTts, times(2)).speak(any(), queueMode.capture(), any(), any()); + assertEquals(TextToSpeech.QUEUE_FLUSH, queueMode.getAllValues().get(0).intValue()); + assertEquals(2, queueMode.getAllValues().get(1).intValue()); + + phoneStateListenerHandler.quit(); + waitUntilReady(); + } + /** * When an alert is triggered while an alert is already happening, the system needs to stop * the previous alert. |