Merge "Move redaction calc to NotifUiAdjustmentProvider" into main
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/PreparationCoordinator.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/PreparationCoordinator.java
index deaf1d1..dfb0f9b 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/PreparationCoordinator.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/PreparationCoordinator.java
@@ -367,7 +367,8 @@
                 /* reason = */ reason,
                 /* showSnooze = */ adjustment.isSnoozeEnabled(),
                 /* isChildInGroup = */ adjustment.isChildInGroup(),
-                /* isGroupSummary = */ adjustment.isGroupSummary()
+                /* isGroupSummary = */ adjustment.isGroupSummary(),
+                /* needsRedaction = */ adjustment.getNeedsRedaction()
         );
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/inflation/NotifInflater.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/inflation/NotifInflater.kt
index 18460c3..7b8a062 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/inflation/NotifInflater.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/inflation/NotifInflater.kt
@@ -61,5 +61,6 @@
         val showSnooze: Boolean,
         val isChildInGroup: Boolean = false,
         val isGroupSummary: Boolean = false,
+        val needsRedaction: Boolean,
     )
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/inflation/NotifUiAdjustmentProvider.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/inflation/NotifUiAdjustmentProvider.kt
index 0b9d19d..e0e5a35 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/inflation/NotifUiAdjustmentProvider.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/inflation/NotifUiAdjustmentProvider.kt
@@ -22,6 +22,7 @@
 import android.os.HandlerExecutor
 import android.os.UserHandle
 import android.provider.Settings.Secure.SHOW_NOTIFICATION_SNOOZE
+import com.android.server.notification.Flags.screenshareNotificationHiding
 import com.android.systemui.dagger.SysUISingleton
 import com.android.systemui.dagger.qualifiers.Main
 import com.android.systemui.settings.UserTracker
@@ -30,6 +31,7 @@
 import com.android.systemui.statusbar.notification.collection.NotificationEntry
 import com.android.systemui.statusbar.notification.collection.provider.SectionStyleProvider
 import com.android.systemui.statusbar.notification.collection.render.GroupMembershipManager
+import com.android.systemui.statusbar.policy.SensitiveNotificationProtectionController
 import com.android.systemui.util.ListenerSet
 import com.android.systemui.util.settings.SecureSettings
 import javax.inject.Inject
@@ -43,6 +45,7 @@
     @Main private val handler: Handler,
     private val secureSettings: SecureSettings,
     private val lockscreenUserManager: NotificationLockscreenUserManager,
+    private val sensitiveNotifProtectionController: SensitiveNotificationProtectionController,
     private val sectionStyleProvider: SectionStyleProvider,
     private val userTracker: UserTracker,
     private val groupMembershipManager: GroupMembershipManager,
@@ -66,6 +69,11 @@
     fun addDirtyListener(listener: Runnable) {
         if (dirtyListeners.isEmpty()) {
             lockscreenUserManager.addNotificationStateChangedListener(notifStateChangedListener)
+            if (screenshareNotificationHiding()) {
+                sensitiveNotifProtectionController.registerSensitiveStateListener(
+                    onSensitiveStateChangedListener
+                )
+            }
             updateSnoozeEnabled()
             secureSettings.registerContentObserverForUser(
                 SHOW_NOTIFICATION_SNOOZE,
@@ -80,6 +88,11 @@
         dirtyListeners.remove(listener)
         if (dirtyListeners.isEmpty()) {
             lockscreenUserManager.removeNotificationStateChangedListener(notifStateChangedListener)
+            if (screenshareNotificationHiding()) {
+                sensitiveNotifProtectionController.unregisterSensitiveStateListener(
+                    onSensitiveStateChangedListener
+                )
+            }
             secureSettings.unregisterContentObserver(settingsObserver)
         }
     }
@@ -89,6 +102,8 @@
             dirtyListeners.forEach(Runnable::run)
         }
 
+    private val onSensitiveStateChangedListener = Runnable { dirtyListeners.forEach(Runnable::run) }
+
     private val settingsObserver = object : ContentObserver(handler) {
         override fun onChange(selfChange: Boolean) {
             updateSnoozeEnabled()
@@ -122,7 +137,10 @@
         isConversation = entry.ranking.isConversation,
         isSnoozeEnabled = isSnoozeSettingsEnabled && !entry.isCanceled,
         isMinimized = isEntryMinimized(entry),
-        needsRedaction = lockscreenUserManager.needsRedaction(entry),
+        needsRedaction =
+            lockscreenUserManager.needsRedaction(entry) ||
+                (screenshareNotificationHiding() &&
+                    sensitiveNotifProtectionController.shouldProtectNotification(entry)),
         isChildInGroup = entry.sbn.isAppOrSystemGroupChild,
         isGroupSummary = entry.sbn.isAppOrSystemGroupSummary,
     )
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/inflation/NotificationRowBinderImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/inflation/NotificationRowBinderImpl.java
index c5b55c7..6400ff6 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/inflation/NotificationRowBinderImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/inflation/NotificationRowBinderImpl.java
@@ -254,11 +254,9 @@
         params.setUseIncreasedCollapsedHeight(useIncreasedCollapsedHeight);
         params.setUseLowPriority(isLowPriority);
 
-        // If screenshareNotificationHiding is enabled, both public and private views should be
-        // inflated to avoid any latency associated with reinflating all notification views when
-        // screen share starts and stops
         if (screenshareNotificationHiding()
-                || mNotificationLockscreenUserManager.needsRedaction(entry)) {
+                ? inflaterParams.getNeedsRedaction()
+                : mNotificationLockscreenUserManager.needsRedaction(entry)) {
             params.requireContentViews(FLAG_CONTENT_VIEW_PUBLIC);
         } else {
             params.markContentViewsFreeable(FLAG_CONTENT_VIEW_PUBLIC);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/PreparationCoordinatorTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/PreparationCoordinatorTest.java
index 4519ba6..419b0fd 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/PreparationCoordinatorTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/PreparationCoordinatorTest.java
@@ -68,6 +68,7 @@
 import com.android.systemui.statusbar.notification.collection.render.GroupMembershipManager;
 import com.android.systemui.statusbar.notification.collection.render.NotifViewBarn;
 import com.android.systemui.statusbar.notification.row.NotifInflationErrorManager;
+import com.android.systemui.statusbar.policy.SensitiveNotificationProtectionController;
 import com.android.systemui.util.settings.SecureSettings;
 
 import org.junit.Before;
@@ -107,6 +108,7 @@
     @Mock private IStatusBarService mService;
     @Mock private BindEventManagerImpl mBindEventManagerImpl;
     @Mock private NotificationLockscreenUserManager mLockscreenUserManager;
+    @Mock private SensitiveNotificationProtectionController mSensitiveNotifProtectionController;
     @Mock private Handler mHandler;
     @Mock private SecureSettings mSecureSettings;
     @Spy private FakeNotifInflater mNotifInflater = new FakeNotifInflater();
@@ -128,6 +130,7 @@
                 mHandler,
                 mSecureSettings,
                 mLockscreenUserManager,
+                mSensitiveNotifProtectionController,
                 mSectionStyleProvider,
                 mUserTracker,
                 mGroupMembershipManager
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/inflation/NotifUiAdjustmentProviderTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/inflation/NotifUiAdjustmentProviderTest.kt
index 115a0d3..34eeba0 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/inflation/NotifUiAdjustmentProviderTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/inflation/NotifUiAdjustmentProviderTest.kt
@@ -23,6 +23,7 @@
 import android.testing.AndroidTestingRunner
 import android.testing.TestableLooper.RunWithLooper
 import androidx.test.filters.SmallTest
+import com.android.server.notification.Flags.FLAG_SCREENSHARE_NOTIFICATION_HIDING
 import com.android.systemui.SysuiTestCase
 import com.android.systemui.settings.UserTracker
 import com.android.systemui.statusbar.NotificationLockscreenUserManager
@@ -33,6 +34,7 @@
 import com.android.systemui.statusbar.notification.collection.render.GroupMembershipManager
 import com.android.systemui.statusbar.notification.row.shared.AsyncGroupHeaderViewInflation
 import com.android.systemui.statusbar.notification.row.shared.AsyncHybridViewInflation
+import com.android.systemui.statusbar.policy.SensitiveNotificationProtectionController
 import com.android.systemui.util.mockito.any
 import com.android.systemui.util.mockito.eq
 import com.android.systemui.util.mockito.mock
@@ -57,6 +59,8 @@
 @RunWithLooper
 class NotifUiAdjustmentProviderTest : SysuiTestCase() {
     private val lockscreenUserManager: NotificationLockscreenUserManager = mock()
+    private val sensitiveNotifProtectionController: SensitiveNotificationProtectionController =
+        mock()
     private val sectionStyleProvider: SectionStyleProvider = mock()
     private val handler: Handler = mock()
     private val secureSettings: SecureSettings = mock()
@@ -77,6 +81,7 @@
         handler,
         secureSettings,
         lockscreenUserManager,
+        sensitiveNotifProtectionController,
         sectionStyleProvider,
         userTracker,
         groupMembershipManager,
@@ -108,6 +113,19 @@
     }
 
     @Test
+    @EnableFlags(FLAG_SCREENSHARE_NOTIFICATION_HIDING)
+    fun sensitiveNotifProtectionStateChangeWillNotifDirty() {
+        val dirtyListener = mock<Runnable>()
+        adjustmentProvider.addDirtyListener(dirtyListener)
+        val sensitiveStateChangedListener =
+            withArgCaptor<Runnable> {
+                verify(sensitiveNotifProtectionController).registerSensitiveStateListener(capture())
+            }
+        sensitiveStateChangedListener.run()
+        verify(dirtyListener).run()
+    }
+
+    @Test
     fun additionalAddDoesNotRegisterAgain() {
         clearInvocations(secureSettings)
         adjustmentProvider.addDirtyListener(mock())
@@ -199,4 +217,38 @@
         // Then: Need re-inflation
         assertTrue(NotifUiAdjustment.needReinflate(oldAdjustment, newAdjustment))
     }
+
+    @Test
+    @EnableFlags(FLAG_SCREENSHARE_NOTIFICATION_HIDING)
+    fun changeSensitiveNotifProtection_screenshareNotificationHidingEnabled_needReinflate() {
+        whenever(sensitiveNotifProtectionController.shouldProtectNotification(entry))
+            .thenReturn(false)
+        val oldAdjustment: NotifUiAdjustment = adjustmentProvider.calculateAdjustment(entry)
+        assertFalse(oldAdjustment.needsRedaction)
+
+        whenever(sensitiveNotifProtectionController.shouldProtectNotification(entry))
+            .thenReturn(true)
+        val newAdjustment = adjustmentProvider.calculateAdjustment(entry)
+        assertTrue(newAdjustment.needsRedaction)
+
+        // Then: need re-inflation
+        assertTrue(NotifUiAdjustment.needReinflate(oldAdjustment, newAdjustment))
+    }
+
+    @Test
+    @DisableFlags(FLAG_SCREENSHARE_NOTIFICATION_HIDING)
+    fun changeSensitiveNotifProtection_screenshareNotificationHidingDisabled_noNeedReinflate() {
+        whenever(sensitiveNotifProtectionController.shouldProtectNotification(entry))
+            .thenReturn(false)
+        val oldAdjustment = adjustmentProvider.calculateAdjustment(entry)
+        assertFalse(oldAdjustment.needsRedaction)
+
+        whenever(sensitiveNotifProtectionController.shouldProtectNotification(entry))
+            .thenReturn(true)
+        val newAdjustment = adjustmentProvider.calculateAdjustment(entry)
+        assertFalse(newAdjustment.needsRedaction)
+
+        // Then: need no re-inflation
+        assertFalse(NotifUiAdjustment.needReinflate(oldAdjustment, newAdjustment))
+    }
 }