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

Commit cae8959d authored by Richard MacGregor's avatar Richard MacGregor
Browse files

Fix metric uid for screenshare protection

Bug: 324333236
Test: atest SensitiveNotificationProtectionControllerTest
Flag: ACONFIG com.android.server.notification.screenshare_notification_hiding TRUNK_FOOD
Change-Id: Icb8fd941b1ef0177af6cbc9737014533f1f2a27c
parent aab4db2c
Loading
Loading
Loading
Loading
+2 −1
Original line number Original line Diff line number Diff line
@@ -94,7 +94,8 @@ public class SensitiveNotificationProtectionControllerImpl


                        int packageUid;
                        int packageUid;
                        try {
                        try {
                            packageUid = mPackageManager.getPackageUid(info.getPackageName(), 0);
                            packageUid = mPackageManager.getPackageUidAsUser(info.getPackageName(),
                                    info.getUserHandle().getIdentifier());
                        } catch (PackageManager.NameNotFoundException e) {
                        } catch (PackageManager.NameNotFoundException e) {
                            Log.w(LOG_TAG, "Package " + info.getPackageName() + " not found");
                            Log.w(LOG_TAG, "Package " + info.getPackageName() + " not found");
                            packageUid = -1;
                            packageUid = -1;
+45 −19
Original line number Original line Diff line number Diff line
@@ -28,6 +28,7 @@ import android.app.NotificationManager.VISIBILITY_NO_OVERRIDE
import android.content.pm.PackageManager
import android.content.pm.PackageManager
import android.media.projection.MediaProjectionInfo
import android.media.projection.MediaProjectionInfo
import android.media.projection.MediaProjectionManager
import android.media.projection.MediaProjectionManager
import android.os.UserHandle
import android.permission.flags.Flags.FLAG_SENSITIVE_NOTIFICATION_APP_PROTECTION
import android.permission.flags.Flags.FLAG_SENSITIVE_NOTIFICATION_APP_PROTECTION
import android.platform.test.annotations.EnableFlags
import android.platform.test.annotations.EnableFlags
import android.platform.test.annotations.RequiresFlagsDisabled
import android.platform.test.annotations.RequiresFlagsDisabled
@@ -85,7 +86,6 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {
    @Mock private lateinit var activityManager: IActivityManager
    @Mock private lateinit var activityManager: IActivityManager
    @Mock private lateinit var mediaProjectionManager: MediaProjectionManager
    @Mock private lateinit var mediaProjectionManager: MediaProjectionManager
    @Mock private lateinit var packageManager: PackageManager
    @Mock private lateinit var packageManager: PackageManager
    @Mock private lateinit var mediaProjectionInfo: MediaProjectionInfo
    @Mock private lateinit var listener1: Runnable
    @Mock private lateinit var listener1: Runnable
    @Mock private lateinit var listener2: Runnable
    @Mock private lateinit var listener2: Runnable
    @Mock private lateinit var listener3: Runnable
    @Mock private lateinit var listener3: Runnable
@@ -95,6 +95,7 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {
    private lateinit var globalSettings: FakeGlobalSettings
    private lateinit var globalSettings: FakeGlobalSettings
    private lateinit var mediaProjectionCallback: MediaProjectionManager.Callback
    private lateinit var mediaProjectionCallback: MediaProjectionManager.Callback
    private lateinit var controller: SensitiveNotificationProtectionControllerImpl
    private lateinit var controller: SensitiveNotificationProtectionControllerImpl
    private lateinit var mediaProjectionInfo: MediaProjectionInfo


    @Before
    @Before
    fun setUp() {
    fun setUp() {
@@ -109,14 +110,29 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {
        setShareFullScreen()
        setShareFullScreen()
        whenever(activityManager.bugreportWhitelistedPackages)
        whenever(activityManager.bugreportWhitelistedPackages)
            .thenReturn(listOf(BUGREPORT_PACKAGE_NAME))
            .thenReturn(listOf(BUGREPORT_PACKAGE_NAME))
        whenever(packageManager.getPackageUid(TEST_PROJECTION_PACKAGE_NAME, 0))
        whenever(
                packageManager.getPackageUidAsUser(
                    TEST_PROJECTION_PACKAGE_NAME,
                    UserHandle.CURRENT.identifier
                )
            )
            .thenReturn(TEST_PROJECTION_PACKAGE_UID)
            .thenReturn(TEST_PROJECTION_PACKAGE_UID)
        whenever(packageManager.getPackageUid(BUGREPORT_PACKAGE_NAME, 0))
        whenever(
                packageManager.getPackageUidAsUser(
                    BUGREPORT_PACKAGE_NAME,
                    UserHandle.CURRENT.identifier
                )
            )
            .thenReturn(BUGREPORT_PACKAGE_UID)
            .thenReturn(BUGREPORT_PACKAGE_UID)
        // SystemUi context package name is exempt, but in test scenarios its
        // SystemUi context package name is exempt, but in test scenarios its
        // com.android.systemui.tests so use that instead of hardcoding. Setup packagemanager to
        // com.android.systemui.tests so use that instead of hardcoding. Setup packagemanager to
        // return the correct uid in this scenario
        // return the correct uid in this scenario
        whenever(packageManager.getPackageUid(mContext.packageName, 0))
        whenever(
                packageManager.getPackageUidAsUser(
                    mContext.packageName,
                    UserHandle.CURRENT.identifier
                )
            )
            .thenReturn(mContext.applicationInfo.uid)
            .thenReturn(mContext.applicationInfo.uid)


        whenever(packageManager.checkPermission(anyString(), anyString()))
        whenever(packageManager.checkPermission(anyString(), anyString()))
@@ -271,7 +287,7 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {
    fun isSensitiveStateActive_projectionActive_sysuiExempt_false() {
    fun isSensitiveStateActive_projectionActive_sysuiExempt_false() {
        // SystemUi context package name is exempt, but in test scenarios its
        // SystemUi context package name is exempt, but in test scenarios its
        // com.android.systemui.tests so use that instead of hardcoding
        // com.android.systemui.tests so use that instead of hardcoding
        whenever(mediaProjectionInfo.packageName).thenReturn(mContext.packageName)
        setShareFullScreenViaSystemUi()
        mediaProjectionCallback.onStart(mediaProjectionInfo)
        mediaProjectionCallback.onStart(mediaProjectionInfo)


        assertFalse(controller.isSensitiveStateActive)
        assertFalse(controller.isSensitiveStateActive)
@@ -309,7 +325,7 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {


    @Test
    @Test
    fun isSensitiveStateActive_projectionActive_bugReportHandlerExempt_false() {
    fun isSensitiveStateActive_projectionActive_bugReportHandlerExempt_false() {
        whenever(mediaProjectionInfo.packageName).thenReturn(BUGREPORT_PACKAGE_NAME)
        setShareFullScreenViaBugReportHandler()
        mediaProjectionCallback.onStart(mediaProjectionInfo)
        mediaProjectionCallback.onStart(mediaProjectionInfo)


        assertFalse(controller.isSensitiveStateActive)
        assertFalse(controller.isSensitiveStateActive)
@@ -371,7 +387,7 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {
    fun shouldProtectNotification_projectionActive_sysuiExempt_false() {
    fun shouldProtectNotification_projectionActive_sysuiExempt_false() {
        // SystemUi context package name is exempt, but in test scenarios its
        // SystemUi context package name is exempt, but in test scenarios its
        // com.android.systemui.tests so use that instead of hardcoding
        // com.android.systemui.tests so use that instead of hardcoding
        whenever(mediaProjectionInfo.packageName).thenReturn(mContext.packageName)
        setShareFullScreenViaSystemUi()
        mediaProjectionCallback.onStart(mediaProjectionInfo)
        mediaProjectionCallback.onStart(mediaProjectionInfo)


        val notificationEntry = setupNotificationEntry(TEST_PACKAGE_NAME, false)
        val notificationEntry = setupNotificationEntry(TEST_PACKAGE_NAME, false)
@@ -415,7 +431,7 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {


    @Test
    @Test
    fun shouldProtectNotification_projectionActive_bugReportHandlerExempt_false() {
    fun shouldProtectNotification_projectionActive_bugReportHandlerExempt_false() {
        whenever(mediaProjectionInfo.packageName).thenReturn(BUGREPORT_PACKAGE_NAME)
        setShareFullScreenViaBugReportHandler()
        mediaProjectionCallback.onStart(mediaProjectionInfo)
        mediaProjectionCallback.onStart(mediaProjectionInfo)


        val notificationEntry = setupNotificationEntry(TEST_PACKAGE_NAME, false)
        val notificationEntry = setupNotificationEntry(TEST_PACKAGE_NAME, false)
@@ -548,9 +564,7 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {
    fun logSensitiveContentProtectionSession_exemptViaSystemUi() {
    fun logSensitiveContentProtectionSession_exemptViaSystemUi() {
        // SystemUi context package name is exempt, but in test scenarios its
        // SystemUi context package name is exempt, but in test scenarios its
        // com.android.systemui.tests so use that instead of hardcoding
        // com.android.systemui.tests so use that instead of hardcoding
        val testPackageName = mContext.packageName
        setShareFullScreenViaSystemUi()
        val testUid = mContext.applicationInfo.uid
        whenever(mediaProjectionInfo.packageName).thenReturn(testPackageName)


        mediaProjectionCallback.onStart(mediaProjectionInfo)
        mediaProjectionCallback.onStart(mediaProjectionInfo)


@@ -558,7 +572,7 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {
            FrameworkStatsLog.write(
            FrameworkStatsLog.write(
                eq(FrameworkStatsLog.SENSITIVE_CONTENT_MEDIA_PROJECTION_SESSION),
                eq(FrameworkStatsLog.SENSITIVE_CONTENT_MEDIA_PROJECTION_SESSION),
                anyLong(),
                anyLong(),
                eq(testUid),
                eq(mContext.applicationInfo.uid),
                eq(true),
                eq(true),
                eq(FrameworkStatsLog.SENSITIVE_CONTENT_MEDIA_PROJECTION_SESSION__STATE__START),
                eq(FrameworkStatsLog.SENSITIVE_CONTENT_MEDIA_PROJECTION_SESSION__STATE__START),
                eq(FrameworkStatsLog.SENSITIVE_CONTENT_MEDIA_PROJECTION_SESSION__SOURCE__SYS_UI)
                eq(FrameworkStatsLog.SENSITIVE_CONTENT_MEDIA_PROJECTION_SESSION__SOURCE__SYS_UI)
@@ -571,7 +585,7 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {
            FrameworkStatsLog.write(
            FrameworkStatsLog.write(
                eq(FrameworkStatsLog.SENSITIVE_CONTENT_MEDIA_PROJECTION_SESSION),
                eq(FrameworkStatsLog.SENSITIVE_CONTENT_MEDIA_PROJECTION_SESSION),
                anyLong(),
                anyLong(),
                eq(testUid),
                eq(mContext.applicationInfo.uid),
                eq(true),
                eq(true),
                eq(FrameworkStatsLog.SENSITIVE_CONTENT_MEDIA_PROJECTION_SESSION__STATE__STOP),
                eq(FrameworkStatsLog.SENSITIVE_CONTENT_MEDIA_PROJECTION_SESSION__STATE__STOP),
                eq(FrameworkStatsLog.SENSITIVE_CONTENT_MEDIA_PROJECTION_SESSION__SOURCE__SYS_UI)
                eq(FrameworkStatsLog.SENSITIVE_CONTENT_MEDIA_PROJECTION_SESSION__SOURCE__SYS_UI)
@@ -582,8 +596,7 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {
    @Test
    @Test
    fun logSensitiveContentProtectionSession_exemptViaBugReportHandler() {
    fun logSensitiveContentProtectionSession_exemptViaBugReportHandler() {
        // Setup exempt via bugreport handler
        // Setup exempt via bugreport handler
        whenever(mediaProjectionInfo.packageName).thenReturn(BUGREPORT_PACKAGE_NAME)
        setShareFullScreenViaBugReportHandler()

        mediaProjectionCallback.onStart(mediaProjectionInfo)
        mediaProjectionCallback.onStart(mediaProjectionInfo)


        verify {
        verify {
@@ -619,13 +632,26 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {
    }
    }


    private fun setShareFullScreen() {
    private fun setShareFullScreen() {
        whenever(mediaProjectionInfo.packageName).thenReturn(TEST_PROJECTION_PACKAGE_NAME)
        setShareScreen(TEST_PROJECTION_PACKAGE_NAME, true)
        whenever(mediaProjectionInfo.launchCookie).thenReturn(null)
    }

    private fun setShareFullScreenViaBugReportHandler() {
        setShareScreen(BUGREPORT_PACKAGE_NAME, true)
    }

    private fun setShareFullScreenViaSystemUi() {
        // SystemUi context package name is exempt, but in test scenarios its
        // com.android.systemui.tests so use that instead of hardcoding
        setShareScreen(mContext.packageName, true)
    }
    }


    private fun setShareSingleApp() {
    private fun setShareSingleApp() {
        whenever(mediaProjectionInfo.packageName).thenReturn(TEST_PROJECTION_PACKAGE_NAME)
        setShareScreen(TEST_PROJECTION_PACKAGE_NAME, false)
        whenever(mediaProjectionInfo.launchCookie).thenReturn(ActivityOptions.LaunchCookie())
    }

    private fun setShareScreen(packageName: String, fullScreen: Boolean) {
        val launchCookie = if (fullScreen) null else ActivityOptions.LaunchCookie()
        mediaProjectionInfo = MediaProjectionInfo(packageName, UserHandle.CURRENT, launchCookie)
    }
    }


    private fun setupNotificationEntry(
    private fun setupNotificationEntry(