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

Commit e2ec6637 authored by Julia Reynolds's avatar Julia Reynolds Committed by Michael Bestas
Browse files

Prevent long press on profile notifs when locked

Test: NotificationGutsManagerTest
Bug: 378087531
Flag: EXEMPT BUGFIX

(cherry picked from commit a6280c4ccf96685eed5dc57c2bd9cbbe04209bf1)
Cherrypick-From: https://googleplex-android-review.googlesource.com/q/commit:a6b65a7c68f282a1dc097cd98d1f358e0b895ef7
Merged-In: Ic4ab842763d2d9473adcfc426a64434aec5c6ed2
Change-Id: Ic4ab842763d2d9473adcfc426a64434aec5c6ed2
parent 17419c47
Loading
Loading
Loading
Loading
+81 −0
Original line number Diff line number Diff line
@@ -672,6 +672,87 @@ class NotificationGutsManagerTest(flags: FlagsParameterization) : SysuiTestCase(
            )
    }

    @Test
    fun testShowGuts_lockedPrimary_yes() {
        whenever(userManager.isManagedProfile(anyInt())).thenReturn(false)
        whenever(notificationLockscreenUserManager.isLockscreenPublicMode(anyInt()))
            .thenReturn(true)

        val guts = spy(NotificationGuts(mContext))
        whenever(guts.post(any())).thenAnswer { invocation: InvocationOnMock ->
            handler.post(((invocation.arguments[0] as Runnable)))
            null
        }

        // Test doesn't support animation since the guts view is not attached.
        doNothing().whenever(guts).openControls(anyInt(), anyInt(), anyBoolean(), any())

        val realRow = createTestNotificationRow()
        val menuItem = createTestMenuItem(realRow)

        val row = spy(realRow)
        whenever(row.windowToken).thenReturn(Binder())
        whenever(row.guts).thenReturn(guts)

        assertTrue(gutsManager.openGutsInternal(row, 0, 0, menuItem))
        executor.runAllReady()
        verify(guts).openControls(anyInt(), anyInt(), anyBoolean(), any<Runnable>())
    }

    @Test
    fun testShowGuts_unlockedWork_yes() {
        whenever(userManager.isManagedProfile(anyInt())).thenReturn(true)
        whenever(notificationLockscreenUserManager.isLockscreenPublicMode(anyInt()))
            .thenReturn(false)

        val guts = spy(NotificationGuts(mContext))
        whenever(guts.post(any())).thenAnswer { invocation: InvocationOnMock ->
            handler.post(((invocation.arguments[0] as Runnable)))
            null
        }

        // Test doesn't support animation since the guts view is not attached.
        doNothing().whenever(guts).openControls(anyInt(), anyInt(), anyBoolean(), any())

        val realRow = createTestNotificationRow()
        val menuItem = createTestMenuItem(realRow)

        val row = spy(realRow)
        whenever(row.windowToken).thenReturn(Binder())
        whenever(row.guts).thenReturn(guts)

        assertTrue(gutsManager.openGutsInternal(row, 0, 0, menuItem))
        executor.runAllReady()
        verify(guts).openControls(anyInt(), anyInt(), anyBoolean(), any<Runnable>())
    }

    @Test
    fun testShowGuts_lockedWork_no() {
        whenever(userManager.isManagedProfile(anyInt())).thenReturn(true)
        whenever(notificationLockscreenUserManager.isLockscreenPublicMode(anyInt()))
            .thenReturn(true)

        val guts = spy(NotificationGuts(mContext))
        whenever(guts.post(any())).thenAnswer { invocation: InvocationOnMock ->
            handler.post(((invocation.arguments[0] as Runnable)))
            null
        }

        // Test doesn't support animation since the guts view is not attached.
        doNothing().whenever(guts).openControls(anyInt(), anyInt(), anyBoolean(), any())

        val realRow = createTestNotificationRow()
        val menuItem = createTestMenuItem(realRow)

        val row = spy(realRow)
        whenever(row.windowToken).thenReturn(Binder())
        whenever(row.guts).thenReturn(guts)

        assertFalse(gutsManager.openGutsInternal(row, 0, 0, menuItem))
        executor.runAllReady()
        verify(guts, never()).openControls(anyInt(), anyInt(), anyBoolean(), any<Runnable>())
    }

    private fun createTestNotificationRow(
        block: NotificationEntryBuilder.() -> Unit = {}
    ): ExpandableNotificationRow {
+15 −0
Original line number Diff line number Diff line
@@ -70,6 +70,7 @@ import com.android.systemui.statusbar.StatusBarState;
import com.android.systemui.statusbar.StatusBarStateControllerImpl;
import com.android.systemui.statusbar.notification.AssistantFeedbackController;
import com.android.systemui.statusbar.notification.NotificationActivityStarter;
import com.android.systemui.statusbar.notification.collection.BundleEntryAdapter;
import com.android.systemui.statusbar.notification.collection.provider.HighPriorityProvider;
import com.android.systemui.statusbar.notification.collection.render.NotifGutsViewListener;
import com.android.systemui.statusbar.notification.collection.render.NotifGutsViewManager;
@@ -805,6 +806,10 @@ public class NotificationGutsManager implements NotifGutsViewManager, CoreStarta
        }

        final ExpandableNotificationRow row = (ExpandableNotificationRow) view;
        if (affectedByWorkProfileLock(row)) {
            return false;
        }

        if (row.isNotificationRowLongClickable()) {
            view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
        }
@@ -875,6 +880,16 @@ public class NotificationGutsManager implements NotifGutsViewManager, CoreStarta
        return true;
    }

    boolean affectedByWorkProfileLock(ExpandableNotificationRow row) {
        if (NotificationBundleUi.isEnabled()
                && row.getEntryAdapter() instanceof BundleEntryAdapter) {
            return false;
        }
        int userId = row.getEntryAdapter().getSbn().getNormalizedUserId();
        return mUserManager.isManagedProfile(userId)
                && mLockscreenUserManager.isLockscreenPublicMode(userId);
    }

    /**
     * @param gutsListener the listener for open and close guts events
     */
+85 −0
Original line number Diff line number Diff line
@@ -86,9 +86,13 @@ import kotlin.test.assertEquals
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runCurrent
import org.junit.Assert
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyBoolean
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.mockito.invocation.InvocationOnMock
@@ -514,6 +518,87 @@ class NotificationGutsManagerWithScenesTest : SysuiTestCase() {
            )
    }

    @Test
    fun testShowGuts_lockedPrimary_yes() {
        whenever(userManager.isManagedProfile(anyInt())).thenReturn(false)
        whenever(notificationLockscreenUserManager.isLockscreenPublicMode(anyInt()))
            .thenReturn(true)

        val guts = spy(NotificationGuts(mContext))
        whenever(guts.post(any())).thenAnswer { invocation: InvocationOnMock ->
            handler.post(((invocation.arguments[0] as Runnable)))
            null
        }

        // Test doesn't support animation since the guts view is not attached.
        doNothing().whenever(guts).openControls(anyInt(), anyInt(), anyBoolean(), any())

        val realRow = createTestNotificationRow()
        val menuItem = createTestMenuItem(realRow)

        val row = spy(realRow)
        whenever(row.windowToken).thenReturn(Binder())
        whenever(row.guts).thenReturn(guts)

        assertTrue(gutsManager.openGutsInternal(row, 0, 0, menuItem))
        executor.runAllReady()
        verify(guts).openControls(anyInt(), anyInt(), anyBoolean(), any<Runnable>())
    }

    @Test
    fun testShowGuts_unlockedWork_yes() {
        whenever(userManager.isManagedProfile(anyInt())).thenReturn(true)
        whenever(notificationLockscreenUserManager.isLockscreenPublicMode(anyInt()))
            .thenReturn(false)

        val guts = spy(NotificationGuts(mContext))
        whenever(guts.post(any())).thenAnswer { invocation: InvocationOnMock ->
            handler.post(((invocation.arguments[0] as Runnable)))
            null
        }

        // Test doesn't support animation since the guts view is not attached.
        doNothing().whenever(guts).openControls(anyInt(), anyInt(), anyBoolean(), any())

        val realRow = createTestNotificationRow()
        val menuItem = createTestMenuItem(realRow)

        val row = spy(realRow)
        whenever(row.windowToken).thenReturn(Binder())
        whenever(row.guts).thenReturn(guts)

        assertTrue(gutsManager.openGutsInternal(row, 0, 0, menuItem))
        executor.runAllReady()
        verify(guts).openControls(anyInt(), anyInt(), anyBoolean(), any<Runnable>())
    }

    @Test
    fun testShowGuts_lockedWork_no() {
        whenever(userManager.isManagedProfile(anyInt())).thenReturn(true)
        whenever(notificationLockscreenUserManager.isLockscreenPublicMode(anyInt()))
            .thenReturn(true)

        val guts = spy(NotificationGuts(mContext))
        whenever(guts.post(any())).thenAnswer { invocation: InvocationOnMock ->
            handler.post(((invocation.arguments[0] as Runnable)))
            null
        }

        // Test doesn't support animation since the guts view is not attached.
        doNothing().whenever(guts).openControls(anyInt(), anyInt(), anyBoolean(), any())

        val realRow = createTestNotificationRow()
        val menuItem = createTestMenuItem(realRow)

        val row = spy(realRow)
        whenever(row.windowToken).thenReturn(Binder())
        whenever(row.guts).thenReturn(guts)

        assertFalse(gutsManager.openGutsInternal(row, 0, 0, menuItem))
        executor.runAllReady()
        verify(guts, never()).openControls(anyInt(), anyInt(), anyBoolean(), any<Runnable>())
    }

    private fun createTestNotificationRow(
        block: NotificationEntryBuilder.() -> Unit = {}
    ): ExpandableNotificationRow {