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

Commit 3a0c2d52 authored by Garvit Narang's avatar Garvit Narang
Browse files

Process updates to ringer and audio sequentially

Without this, synchronous calls to postUpdateRingerAndAudio with
different values for shouldApplyToRinger will call the first update to
be missed causing ringer external state to not update

Fixes: 345247710
Test: unit test, manual flash and check
Flag: EXEMPT trivial bug fix
Change-Id: I88386737b5d0983eba240e28cd39efd83118d382
parent ed7ef7ad
Loading
Loading
Loading
Loading
+14 −6
Original line number Diff line number Diff line
@@ -2811,8 +2811,9 @@ public class ZenModeHelper {
    private final class H extends Handler {
        private static final int MSG_DISPATCH = 1;
        private static final int MSG_METRICS = 2;
        private static final int MSG_RINGER_AUDIO = 5;
        private static final int MSG_APPLY_EFFECTS = 6;
        private static final int MSG_AUDIO_APPLIED_TO_RINGER = 7;
        private static final int MSG_AUDIO_NOT_APPLIED_TO_RINGER = 8;

        private static final long METRICS_PERIOD_MS = 6 * 60 * 60 * 1000;

@@ -2831,8 +2832,13 @@ public class ZenModeHelper {
        }

        private void postUpdateRingerAndAudio(boolean shouldApplyToRinger) {
            removeMessages(MSG_RINGER_AUDIO);
            sendMessage(obtainMessage(MSG_RINGER_AUDIO, shouldApplyToRinger));
            if (shouldApplyToRinger) {
                removeMessages(MSG_AUDIO_APPLIED_TO_RINGER);
                sendEmptyMessage(MSG_AUDIO_APPLIED_TO_RINGER);
            } else {
                removeMessages(MSG_AUDIO_NOT_APPLIED_TO_RINGER);
                sendEmptyMessage(MSG_AUDIO_NOT_APPLIED_TO_RINGER);
            }
        }

        private void postApplyDeviceEffects(@ConfigChangeOrigin int origin) {
@@ -2849,9 +2855,11 @@ public class ZenModeHelper {
                case MSG_METRICS:
                    mMetrics.emit();
                    break;
                case MSG_RINGER_AUDIO:
                    boolean shouldApplyToRinger = (boolean) msg.obj;
                    updateRingerAndAudio(shouldApplyToRinger);
                case MSG_AUDIO_APPLIED_TO_RINGER:
                    updateRingerAndAudio(/* shouldApplyToRinger= */ true);
                    break;
                case MSG_AUDIO_NOT_APPLIED_TO_RINGER:
                    updateRingerAndAudio(/* shouldApplyToRinger= */ false);
                    break;
                case MSG_APPLY_EFFECTS:
                    @ConfigChangeOrigin int origin = msg.arg1;
+40 −0
Original line number Diff line number Diff line
@@ -1044,6 +1044,8 @@ public class ZenModeHelperTest extends UiServiceTestCase {
        AudioManagerInternal mAudioManager = mock(AudioManagerInternal.class);
        mZenModeHelper.mAudioManager = mAudioManager;
        setupZenConfig();
        mTestableLooper.processAllMessages();
        reset(mAudioManager);

        // Turn manual zen mode on
        mZenModeHelper.setManualZenMode(ZEN_MODE_IMPORTANT_INTERRUPTIONS, null, UPDATE_ORIGIN_APP,
@@ -1062,6 +1064,44 @@ public class ZenModeHelperTest extends UiServiceTestCase {
        verify(mAudioManager, atLeastOnce()).getRingerModeInternal();
    }

    @Test
    public void testSetConfig_updatesAudioForSequentialChangesToZenMode() {
        AudioManagerInternal mAudioManager = mock(AudioManagerInternal.class);
        mZenModeHelper.mAudioManager = mAudioManager;
        setupZenConfig();
        mTestableLooper.processAllMessages();
        reset(mAudioManager);

        // Turn manual zen mode on
        mZenModeHelper.setManualZenMode(
                ZEN_MODE_IMPORTANT_INTERRUPTIONS,
                null,
                UPDATE_ORIGIN_APP,
                null,
                "test",
                CUSTOM_PKG_UID);
        mZenModeHelper.setManualZenMode(
                ZEN_MODE_IMPORTANT_INTERRUPTIONS,
                null,
                UPDATE_ORIGIN_APP,
                null,
                "test",
                CUSTOM_PKG_UID);

        // audio manager shouldn't do anything until the handler processes its messages
        verify(mAudioManager, never()).updateRingerModeAffectedStreamsInternal();

        // now process the looper's messages
        mTestableLooper.processAllMessages();

        // Expect calls to audio manager
        verify(mAudioManager, times(2)).updateRingerModeAffectedStreamsInternal();
        verify(mAudioManager, times(1)).setRingerModeInternal(anyInt(), anyString());

        // called during applyZenToRingerMode(), which should be true since zen changed
        verify(mAudioManager, atLeastOnce()).getRingerModeInternal();
    }

    @Test
    public void testParcelConfig() {
        mZenModeHelper.setNotificationPolicy(new Policy(PRIORITY_CATEGORY_EVENTS