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

Commit 09d743af authored by Julia Reynolds's avatar Julia Reynolds Committed by Nishith Khanna
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:2533e349017a749e8b6bf3039958c3cc7428f754
Merged-In: Ic4ab842763d2d9473adcfc426a64434aec5c6ed2
Change-Id: Ic4ab842763d2d9473adcfc426a64434aec5c6ed2
parent 2fe5d488
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -686,6 +686,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);
        }
@@ -748,6 +752,12 @@ public class NotificationGutsManager implements NotifGutsViewManager, CoreStarta
        return true;
    }

    boolean affectedByWorkProfileLock(ExpandableNotificationRow row) {
        int userId = row.getEntry().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
@@ -79,9 +79,13 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
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
@@ -511,6 +515,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(): ExpandableNotificationRow? {
        val nb =
            Notification.Builder(mContext, testNotificationChannel.id)