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

Commit 4a02c225 authored by Ioana Alexandru's avatar Ioana Alexandru
Browse files

Fix ZenModeRepository using the wrong broadcast.

Also optimised the flow a bit behind the flag (doing it in the
background + stateflow instead of sharedflow)

Fix: 347707024
Test: ZenModeRepositoryTest
Flag: com.android.settingslib.flags.volume_panel_broadcast_fix
Flag: android.app.modes_api
Flag: com.android.systemui.new_volume_panel
Change-Id: I97557efc4512196ae703af81622e4a5e1ba89ca5
parent a9d79f31
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -89,3 +89,13 @@ flag {
        purpose: PURPOSE_BUGFIX
    }
}

flag {
    name: "volume_panel_broadcast_fix"
    namespace: "systemui"
    description: "Make the volume panel's repository listen for the new ACTION_CONSOLIDATED_NOTIFICATION_POLICY_CHANGED broadcast instead of ACTION_NOTIFICATION_POLICY_CHANGED"
    bug: "347707024"
    metadata {
        purpose: PURPOSE_BUGFIX
    }
}
+23 −9
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import com.android.settingslib.flags.Flags
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.awaitClose
@@ -65,18 +66,31 @@ class ZenModeRepositoryImpl(
                    IntentFilter().apply {
                        addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED)
                        addAction(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED)
                        if (Flags.volumePanelBroadcastFix() && android.app.Flags.modesApi())
                            addAction(
                                NotificationManager.ACTION_CONSOLIDATED_NOTIFICATION_POLICY_CHANGED)
                    })

                awaitClose { context.unregisterReceiver(receiver) }
            }
            .shareIn(
            .apply {
                if (Flags.volumePanelBroadcastFix()) {
                    flowOn(backgroundCoroutineContext)
                    stateIn(scope, SharingStarted.WhileSubscribed(), null)
                } else {
                    shareIn(
                        started = SharingStarted.WhileSubscribed(),
                        scope = scope,
                    )
                }
            }

    override val consolidatedNotificationPolicy: StateFlow<NotificationManager.Policy?> =
        // TODO(b/347707024): This should use ACTION_CONSOLIDATED_NOTIFICATION_POLICY_CHANGED
        // instead.
        if (Flags.volumePanelBroadcastFix() && android.app.Flags.modesApi())
            flowFromBroadcast(NotificationManager.ACTION_CONSOLIDATED_NOTIFICATION_POLICY_CHANGED) {
                notificationManager.consolidatedNotificationPolicy
            }
        else
            flowFromBroadcast(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED) {
                notificationManager.consolidatedNotificationPolicy
            }
+37 −15
Original line number Diff line number Diff line
@@ -20,9 +20,12 @@ import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.platform.test.annotations.DisableFlags
import android.platform.test.annotations.EnableFlags
import android.provider.Settings.Global
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.settingslib.flags.Flags
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.launchIn
@@ -46,14 +49,11 @@ import org.mockito.MockitoAnnotations
@SmallTest
class ZenModeRepositoryTest {

    @Mock
    private lateinit var context: Context
    @Mock private lateinit var context: Context

    @Mock
    private lateinit var notificationManager: NotificationManager
    @Mock private lateinit var notificationManager: NotificationManager

    @Captor
    private lateinit var receiverCaptor: ArgumentCaptor<BroadcastReceiver>
    @Captor private lateinit var receiverCaptor: ArgumentCaptor<BroadcastReceiver>

    private lateinit var underTest: ZenModeRepository

@@ -72,12 +72,14 @@ class ZenModeRepositoryTest {
            )
    }

    @DisableFlags(android.app.Flags.FLAG_MODES_API, Flags.FLAG_VOLUME_PANEL_BROADCAST_FIX)
    @Test
    fun consolidatedPolicyChanges_repositoryEmits() {
    fun consolidatedPolicyChanges_repositoryEmits_flagsOff() {
        testScope.runTest {
            val values = mutableListOf<NotificationManager.Policy?>()
            `when`(notificationManager.consolidatedNotificationPolicy).thenReturn(testPolicy1)
            underTest.consolidatedNotificationPolicy.onEach { values.add(it) }
            underTest.consolidatedNotificationPolicy
                .onEach { values.add(it) }
                .launchIn(backgroundScope)
            runCurrent()

@@ -91,6 +93,27 @@ class ZenModeRepositoryTest {
        }
    }

    @EnableFlags(android.app.Flags.FLAG_MODES_API, Flags.FLAG_VOLUME_PANEL_BROADCAST_FIX)
    @Test
    fun consolidatedPolicyChanges_repositoryEmits_flagsOn() {
        testScope.runTest {
            val values = mutableListOf<NotificationManager.Policy?>()
            `when`(notificationManager.consolidatedNotificationPolicy).thenReturn(testPolicy1)
            underTest.consolidatedNotificationPolicy
                .onEach { values.add(it) }
                .launchIn(backgroundScope)
            runCurrent()

            `when`(notificationManager.consolidatedNotificationPolicy).thenReturn(testPolicy2)
            triggerIntent(NotificationManager.ACTION_CONSOLIDATED_NOTIFICATION_POLICY_CHANGED)
            runCurrent()

            assertThat(values)
                .containsExactlyElementsIn(listOf(null, testPolicy1, testPolicy2))
                .inOrder()
        }
    }

    @Test
    fun zenModeChanges_repositoryEmits() {
        testScope.runTest {
@@ -105,8 +128,7 @@ class ZenModeRepositoryTest {

            assertThat(values)
                .containsExactlyElementsIn(
                        listOf(null, Global.ZEN_MODE_OFF, Global.ZEN_MODE_ALARMS)
                    )
                    listOf(null, Global.ZEN_MODE_OFF, Global.ZEN_MODE_ALARMS))
                .inOrder()
        }
    }