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

Commit 69e54bfd authored by Richard MacGregor's avatar Richard MacGregor
Browse files

Move global settings lookup off main thread

Bug: 324447419
Flag: ACONFIG com.android.server.notification.screenshare_notification_hiding DISABLED
Test: atest SensitiveNotificationProtectionControllerTest
Change-Id: Ic5f78244a3dceab664b4863a58ac5de2406c8d5a
parent 25dbc2cb
Loading
Loading
Loading
Loading
+22 −9
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static com.android.server.notification.Flags.screenshareNotificationHidin
import android.annotation.MainThread;
import android.app.IActivityManager;
import android.content.Context;
import android.database.ExecutorContentObserver;
import android.media.projection.MediaProjectionInfo;
import android.media.projection.MediaProjectionManager;
import android.os.Handler;
@@ -50,10 +51,10 @@ import javax.inject.Inject;
public class SensitiveNotificationProtectionControllerImpl
        implements SensitiveNotificationProtectionController {
    private static final String LOG_TAG = "SNPC";
    private final GlobalSettings mGlobalSettings;
    private final ArraySet<String> mExemptPackages = new ArraySet<>();
    private final ListenerSet<Runnable> mListeners = new ListenerSet<>();
    private volatile MediaProjectionInfo mProjection;
    boolean mDisableScreenShareProtections = false;

    @VisibleForTesting
    final MediaProjectionManager.Callback mMediaProjectionCallback =
@@ -62,12 +63,7 @@ public class SensitiveNotificationProtectionControllerImpl
                public void onStart(MediaProjectionInfo info) {
                    Trace.beginSection("SNPC.onProjectionStart");
                    try {
                        // TODO(b/324447419): move GlobalSettings lookup to background thread
                        boolean disableScreenShareProtections =
                                mGlobalSettings.getInt(
                                        DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS,
                                        0) != 0;
                        if (disableScreenShareProtections) {
                        if (mDisableScreenShareProtections) {
                            Log.w(LOG_TAG,
                                    "Screen share protections disabled, ignoring projectionstart");
                            return;
@@ -101,12 +97,29 @@ public class SensitiveNotificationProtectionControllerImpl
            IActivityManager activityManager,
            @Main Handler mainHandler,
            @Background Executor bgExecutor) {
        mGlobalSettings = settings;

        if (!screenshareNotificationHiding()) {
            return;
        }

        ExecutorContentObserver developerOptionsObserver = new ExecutorContentObserver(bgExecutor) {
            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);
                boolean disableScreenShareProtections = settings.getInt(
                        DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS,
                        0) != 0;
                mainHandler.post(() -> {
                    mDisableScreenShareProtections = disableScreenShareProtections;
                });
            }
        };
        settings.registerContentObserver(
                DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS,
                developerOptionsObserver);

        // Get current setting value
        bgExecutor.execute(() -> developerOptionsObserver.onChange(true));

        bgExecutor.execute(() -> {
            ArraySet<String> exemptPackages = new ArraySet<>();
            // Exempt SystemUI
+12 −4
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {
    @Mock private lateinit var listener2: Runnable
    @Mock private lateinit var listener3: Runnable

    private lateinit var executor: FakeExecutor
    private lateinit var globalSettings: FakeGlobalSettings
    private lateinit var mediaProjectionCallback: MediaProjectionManager.Callback
    private lateinit var controller: SensitiveNotificationProtectionControllerImpl
@@ -76,7 +77,7 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {
        whenever(activityManager.bugreportWhitelistedPackages)
            .thenReturn(listOf(BUGREPORT_PACKAGE_NAME))

        val executor = FakeExecutor(FakeSystemClock())
        executor = FakeExecutor(FakeSystemClock())
        globalSettings = FakeGlobalSettings()
        controller =
            SensitiveNotificationProtectionControllerImpl(
@@ -88,7 +89,7 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {
                executor
            )

        // Process exemption processing
        // Process pending work (getting global setting and list of exemptions)
        executor.runAllReady()

        // Obtain useful MediaProjectionCallback
@@ -234,7 +235,7 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {

    @Test
    fun isSensitiveStateActive_projectionActive_disabledViaDevOption_false() {
        globalSettings.putInt(DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS, 1)
        setDisabledViaDeveloperOption()
        mediaProjectionCallback.onStart(mediaProjectionInfo)

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

    @Test
    fun shouldProtectNotification_projectionActive_disabledViaDevOption_false() {
        globalSettings.putInt(DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS, 1)
        setDisabledViaDeveloperOption()
        mediaProjectionCallback.onStart(mediaProjectionInfo)

        val notificationEntry = setupNotificationEntry(TEST_PROJECTION_PACKAGE_NAME)
@@ -316,6 +317,13 @@ class SensitiveNotificationProtectionControllerTest : SysuiTestCase() {
        assertFalse(controller.shouldProtectNotification(notificationEntry))
    }

    private fun setDisabledViaDeveloperOption() {
        globalSettings.putInt(DISABLE_SCREEN_SHARE_PROTECTIONS_FOR_APPS_AND_NOTIFICATIONS, 1)

        // Process pending work that gets current developer option global setting
        executor.runAllReady()
    }

    private fun setShareFullScreen() {
        whenever(mediaProjectionInfo.packageName).thenReturn(TEST_PROJECTION_PACKAGE_NAME)
        whenever(mediaProjectionInfo.launchCookie).thenReturn(null)