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

Commit fd3ec641 authored by Hall Liu's avatar Hall Liu
Browse files

Properly restart ringtone when BT device connected

A previous change stoppped the ringer from starting after a HFP device
connects when audio focus was previously acquired. This behavior is
partially incorrect, since sometimes we can acquire audio focus without
starting a ringtone. The result of the incorrect behavior is that an
in-band ringtone will fail to play over the HFP device if the device is
on vibrate.

The fix here is to gate the second try on both acquisition of audio
focus and starting the ringtone.

Test: unit tests, manual
Fixes: 160109967
Change-Id: I6c184795b29e09765bfdba300f83cb9471f30751
parent b5f02086
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -255,4 +255,8 @@ public class AsyncRingtonePlayer {
            }
        }
    }

    public boolean isPlaying() {
        return mRingtone != null;
    }
}
+4 −0
Original line number Diff line number Diff line
@@ -449,6 +449,10 @@ public class CallAudioManager extends CallsManagerListenerBase {
        }
    }

    public boolean isRingtonePlaying() {
        return mRinger.isRinging();
    }

    @VisibleForTesting
    public boolean startRinging() {
        synchronized (mCallsManager.getLock()) {
+1 −1
Original line number Diff line number Diff line
@@ -358,7 +358,7 @@ public class CallAudioModeStateMachine extends StateMachine {
        private boolean mHasFocus = false;

        private void tryStartRinging() {
            if (mHasFocus) {
            if (mHasFocus && mCallAudioManager.isRingtonePlaying()) {
                Log.i(LOG_TAG, "RingingFocusState#tryStartRinging -- audio focus previously"
                        + " acquired and ringtone already playing -- skipping.");
                return;
+4 −0
Original line number Diff line number Diff line
@@ -458,6 +458,10 @@ public class Ringer {
        }
    }

    public boolean isRinging() {
        return mRingtonePlayer.isPlaying();
    }

    private boolean shouldRingForContact(Uri contactUri) {
        final NotificationManager manager =
                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
+39 −0
Original line number Diff line number Diff line
@@ -210,6 +210,7 @@ public class CallAudioModeStateMachineTest extends TelecomTestCase {
        verify(mCallAudioManager).setCallAudioRouteFocusState(
                CallAudioRouteStateMachine.RINGING_FOCUS);

        when(mCallAudioManager.isRingtonePlaying()).thenReturn(true);
        sm.sendMessage(CallAudioModeStateMachine.RINGER_MODE_CHANGE);
        waitForHandlerAction(sm.getHandler(), TEST_TIMEOUT);

@@ -217,6 +218,44 @@ public class CallAudioModeStateMachineTest extends TelecomTestCase {
        verify(mCallAudioManager, times(1)).startRinging();
    }

    @SmallTest
    @Test
    public void testStartRingingAfterHfpConnectedIfNotAlreadyPlaying() {
        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);

        when(mCallAudioManager.isRingtonePlaying()).thenReturn(false);
        sm.sendMessage(CallAudioModeStateMachine.RINGER_MODE_CHANGE);
        waitForHandlerAction(sm.getHandler(), TEST_TIMEOUT);

        // Make sure we do try and start ringing again, since the ringtone wasn't already playing.
        verify(mCallAudioManager, times(2)).startRinging();
    }

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