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

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

Refactor nullchecks

Move nullchecked earlier in code flows, add nonnull annotations where
necessary.
Refactor notification sensitive check into boolean method that doesn't
repackage as PackageInfo

Bug: 331946904
Test: atest SensitiveContentProtectionManagerServiceNotificationTest
Flag: EXEMPT - strict refactor and null checks
Change-Id: Ib100da3a01010019685d6806f92a8fe04ccbfab0
parent 10632fc9
Loading
Loading
Loading
Loading
+37 −21
Original line number Diff line number Diff line
@@ -316,11 +316,16 @@ public final class SensitiveContentProtectionManagerService extends SystemServic
            rankingMap = null;
        }

        if (rankingMap == null) {
            Log.w(TAG, "Ranking map not initialized.");
            return;
        }

        updateAppsThatShouldBlockScreenCapture(rankingMap);
    }

    @GuardedBy("mSensitiveContentProtectionLock")
    private void updateAppsThatShouldBlockScreenCapture(RankingMap rankingMap) {
    private void updateAppsThatShouldBlockScreenCapture(@NonNull RankingMap rankingMap) {
        StatusBarNotification[] notifications;
        try {
            notifications = mNotificationListener.getActiveNotifications();
@@ -337,15 +342,15 @@ public final class SensitiveContentProtectionManagerService extends SystemServic
        }
    }

    private ArraySet<PackageInfo> getSensitivePackagesFromNotifications(
            @NonNull StatusBarNotification[] notifications, RankingMap rankingMap) {
    private static @NonNull ArraySet<PackageInfo> getSensitivePackagesFromNotifications(
            @NonNull StatusBarNotification[] notifications, @NonNull RankingMap rankingMap) {
        ArraySet<PackageInfo> sensitivePackages = new ArraySet<>();
        if (rankingMap == null) {
            Log.w(TAG, "Ranking map not initialized.");
            return sensitivePackages;
        for (StatusBarNotification sbn : notifications) {
            if (sbn == null) {
                Log.w(TAG, "Unable to parse null notification");
                continue;
            }

        for (StatusBarNotification sbn : notifications) {
            PackageInfo info = getSensitivePackageFromNotification(sbn, rankingMap);
            if (info != null) {
                sensitivePackages.add(info);
@@ -354,22 +359,18 @@ public final class SensitiveContentProtectionManagerService extends SystemServic
        return sensitivePackages;
    }

    private PackageInfo getSensitivePackageFromNotification(
            StatusBarNotification sbn, RankingMap rankingMap) {
        if (sbn == null) {
            Log.w(TAG, "Unable to protect null notification");
            return null;
    private static @Nullable PackageInfo getSensitivePackageFromNotification(
            @NonNull StatusBarNotification sbn, @NonNull RankingMap rankingMap) {
        if (notificationHasSensitiveContent(sbn, rankingMap)) {
            return new PackageInfo(sbn.getPackageName(), sbn.getUid());
        }
        if (rankingMap == null) {
            Log.w(TAG, "Ranking map not initialized.");
        return null;
    }

    private static boolean notificationHasSensitiveContent(
            @NonNull StatusBarNotification sbn, @NonNull RankingMap rankingMap) {
        NotificationListenerService.Ranking ranking = rankingMap.getRawRankingObject(sbn.getKey());
        if (ranking != null && ranking.hasSensitiveContent()) {
            return new PackageInfo(sbn.getPackageName(), sbn.getUid());
        }
        return null;
        return ranking != null && ranking.hasSensitiveContent();
    }

    @VisibleForTesting
@@ -397,6 +398,16 @@ public final class SensitiveContentProtectionManagerService extends SystemServic
            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER,
                    "SensitiveContentProtectionManagerService.onNotificationPosted");
            try {
                if (sbn == null) {
                    Log.w(TAG, "Unable to parse null notification");
                    return;
                }

                if (rankingMap == null) {
                    Log.w(TAG, "Ranking map not initialized.");
                    return;
                }

                synchronized (mSensitiveContentProtectionLock) {
                    if (!mProjectionActive) {
                        return;
@@ -421,6 +432,11 @@ public final class SensitiveContentProtectionManagerService extends SystemServic
            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER,
                    "SensitiveContentProtectionManagerService.onNotificationRankingUpdate");
            try {
                if (rankingMap == null) {
                    Log.w(TAG, "Ranking map not initialized.");
                    return;
                }

                synchronized (mSensitiveContentProtectionLock) {
                    if (mProjectionActive) {
                        updateAppsThatShouldBlockScreenCapture(rankingMap);
+19 −0
Original line number Diff line number Diff line
@@ -282,6 +282,14 @@ public class SensitiveContentProtectionManagerServiceNotificationTest {
                .getActiveNotifications();
    }

    private void setupNullNotifications() {
        // Setup Notification Values
        StatusBarNotification[] mNotifications = new StatusBarNotification[] { null, null};
        doReturn(mNotifications)
                .when(mSensitiveContentProtectionManagerService.mNotificationListener)
                .getActiveNotifications();
    }

    private MediaProjectionInfo createMediaProjectionInfo() {
        return new MediaProjectionInfo(SCREEN_RECORDER_PACKAGE, Process.myUserHandle(), null);
    }
@@ -501,6 +509,17 @@ public class SensitiveContentProtectionManagerServiceNotificationTest {
        verifyZeroInteractions(mWindowManager);
    }

    @Test
    public void nlsOnListenerConnected_nullNotifications_noBlockedPackages() {
        setupNullNotifications();
        mMediaProjectionCallbackCaptor.getValue().onStart(createMediaProjectionInfo());
        Mockito.reset(mWindowManager);

        mSensitiveContentProtectionManagerService.mNotificationListener.onListenerConnected();

        verifyZeroInteractions(mWindowManager);
    }

    @Test
    public void nlsOnListenerConnected_nullRankingMap_noBlockedPackages() {
        // Sets up mNotification1 & mRankingMap to be a sensitive notification, and mNotification2