Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3fc1c56c authored by Hall Liu's avatar Hall Liu Committed by Android (Google) Code Review
Browse files

Merge "Don't call startRinging twice" into rvc-dev

parents 2b0facb2 b112bd6e
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -353,7 +353,17 @@ public class CallAudioModeStateMachine extends StateMachine {
    }

    private class RingingFocusState extends BaseState {
        // Keeps track of whether we're ringing with audio focus or if we've just entered the state
        // without acquiring focus because of a silent ringtone or something.
        private boolean mHasFocus = false;

        private void tryStartRinging() {
            if (mHasFocus) {
                Log.i(LOG_TAG, "RingingFocusState#tryStartRinging -- audio focus previously"
                        + " acquired and ringtone already playing -- skipping.");
                return;
            }

            if (mCallAudioManager.startRinging()) {
                mAudioManager.requestAudioFocusForCall(AudioManager.STREAM_RING,
                        AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
@@ -364,6 +374,7 @@ public class CallAudioModeStateMachine extends StateMachine {
                }
                mCallAudioManager.setCallAudioRouteFocusState(
                        CallAudioRouteStateMachine.RINGING_FOCUS);
                mHasFocus = true;
            } else {
                Log.i(LOG_TAG, "RINGING state, try start ringing but not acquiring audio focus");
            }
@@ -380,6 +391,7 @@ public class CallAudioModeStateMachine extends StateMachine {
        public void exit() {
            // Audio mode and audio stream will be set by the next state.
            mCallAudioManager.stopRinging();
            mHasFocus = false;
        }

        @Override
+36 −0
Original line number Diff line number Diff line
@@ -180,6 +180,42 @@ public class CallAudioModeStateMachineTest extends TelecomTestCase {
                CallAudioRouteStateMachine.RINGING_FOCUS);
    }

    @SmallTest
    @Test
    public void testDoNotRingTwiceWhenHfpConnected() {
        CallAudioModeStateMachine sm = new CallAudioModeStateMachine(mSystemStateHelper,
                mAudioManager, mTestThread.getLooper());
        sm.setCallAudioManager(mCallAudioManager);
        sm.sendMessage(CallAudioModeStateMachine.ABANDON_FOCUS_FOR_TESTING);
        waitForHandlerAction(sm.getHandler(), TEST_TIMEOUT);

        resetMocks();
        when(mCallAudioManager.startRinging()).thenReturn(true);

        sm.sendMessage(CallAudioModeStateMachine.NEW_RINGING_CALL, new Builder()
                .setHasActiveOrDialingCalls(false)
                .setHasRingingCalls(true)
                .setHasHoldingCalls(false)
                .setIsTonePlaying(false)
                .setForegroundCallIsVoip(false)
                .setSession(null)
                .build());
        waitForHandlerAction(sm.getHandler(), TEST_TIMEOUT);

        assertEquals(CallAudioModeStateMachine.RING_STATE_NAME, sm.getCurrentStateName());

        verify(mAudioManager).requestAudioFocusForCall(AudioManager.STREAM_RING,
                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
        verify(mAudioManager).setMode(AudioManager.MODE_RINGTONE);
        verify(mCallAudioManager).setCallAudioRouteFocusState(
                CallAudioRouteStateMachine.RINGING_FOCUS);

        sm.sendMessage(CallAudioModeStateMachine.RINGER_MODE_CHANGE);
        waitForHandlerAction(sm.getHandler(), TEST_TIMEOUT);

        // Make sure we don't try and start ringing again.
        verify(mCallAudioManager, times(1)).startRinging();
    }

    private void resetMocks() {
        clearInvocations(mCallAudioManager, mAudioManager);