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

Commit fb4d7ea9 authored by mrulhania's avatar mrulhania Committed by Manjeet Rulhania
Browse files

Add metrics log for sensitive content app protection

Test: build
Bug: 325329095
Change-Id: Iee8eea0cafa40d1a0099e9457c5322f330b2b45a
parent 5f9c22a9
Loading
Loading
Loading
Loading
+54 −13
Original line number Diff line number Diff line
@@ -43,10 +43,12 @@ import android.view.ISensitiveContentProtectionManager;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.FrameworkStatsLog;
import com.android.server.wm.SensitiveContentPackages.PackageInfo;
import com.android.server.wm.WindowManagerInternal;

import java.util.Objects;
import java.util.Random;
import java.util.Set;

/**
@@ -61,8 +63,15 @@ public final class SensitiveContentProtectionManagerService extends SystemServic
    @VisibleForTesting
    @Nullable
    NotificationListener mNotificationListener;
    @Nullable private MediaProjectionManager mProjectionManager;
    @Nullable private WindowManagerInternal mWindowManager;
    @Nullable
    private MediaProjectionManager mProjectionManager;
    @Nullable
    private MediaProjectionSession mMediaProjectionSession;

    private PackageManagerInternal mPackageManagerInternal;

    @Nullable
    private WindowManagerInternal mWindowManager;

    // screen recorder packages exempted from screen share protection.
    private ArraySet<String> mExemptedPackages = null;
@@ -72,6 +81,16 @@ public final class SensitiveContentProtectionManagerService extends SystemServic
    @GuardedBy("mSensitiveContentProtectionLock")
    private boolean mProjectionActive = false;

    private static class MediaProjectionSession {
        final int mUid;
        final long mSessionId;

        MediaProjectionSession(int uid, long sessionId) {
            mUid = uid;
            mSessionId = sessionId;
        }
    }

    private final MediaProjectionManager.Callback mProjectionCallback =
            new MediaProjectionManager.Callback() {
                @Override
@@ -80,7 +99,7 @@ public final class SensitiveContentProtectionManagerService extends SystemServic
                    Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER,
                            "SensitiveContentProtectionManagerService.onProjectionStart");
                    try {
                        onProjectionStart(info.getPackageName());
                        onProjectionStart(info);
                    } finally {
                        Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
                    }
@@ -117,12 +136,13 @@ public final class SensitiveContentProtectionManagerService extends SystemServic

        if (DEBUG) Log.d(TAG, "onBootPhase - PHASE_BOOT_COMPLETED");

        mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
        init(getContext().getSystemService(MediaProjectionManager.class),
                LocalServices.getService(WindowManagerInternal.class),
                getExemptedPackages());
        if (sensitiveContentAppProtection()) {
            publishBinderService(Context.SENSITIVE_CONTENT_PROTECTION_SERVICE,
                    new SensitiveContentProtectionManagerServiceBinder());
                    new SensitiveContentProtectionManagerServiceBinder(mPackageManagerInternal));
        }
    }

@@ -184,11 +204,12 @@ public final class SensitiveContentProtectionManagerService extends SystemServic
        return SystemConfig.getInstance().getBugreportWhitelistedPackages();
    }

    private void onProjectionStart(String packageName) {
    private void onProjectionStart(MediaProjectionInfo projectionInfo) {
        // exempt on device screen recorder as well.
        if ((mExemptedPackages != null && mExemptedPackages.contains(packageName))
                || canRecordSensitiveContent(packageName)) {
            Log.w(TAG, packageName + " is exempted from screen share protection.");
        if ((mExemptedPackages != null && mExemptedPackages.contains(
                projectionInfo.getPackageName()))
                || canRecordSensitiveContent(projectionInfo.getPackageName())) {
            Log.w(TAG, projectionInfo.getPackageName() + " is exempted.");
            return;
        }
        // TODO(b/324447419): move GlobalSettings lookup to background thread
@@ -202,6 +223,10 @@ public final class SensitiveContentProtectionManagerService extends SystemServic

        synchronized (mSensitiveContentProtectionLock) {
            mProjectionActive = true;
            int uid = mPackageManagerInternal.getPackageUid(projectionInfo.getPackageName(), 0,
                    projectionInfo.getUserHandle().getIdentifier());
            // TODO review sessionId, whether to use a sequence generator or random is good?
            mMediaProjectionSession = new MediaProjectionSession(uid, new Random().nextLong());
            if (sensitiveNotificationAppProtection()) {
                updateAppsThatShouldBlockScreenCapture();
            }
@@ -211,6 +236,7 @@ public final class SensitiveContentProtectionManagerService extends SystemServic
    private void onProjectionEnd() {
        synchronized (mSensitiveContentProtectionLock) {
            mProjectionActive = false;
            mMediaProjectionSession = null;

            // notify windowmanager to clear any sensitive notifications observed during projection
            // session
@@ -369,8 +395,22 @@ public final class SensitiveContentProtectionManagerService extends SystemServic
            packageInfos.add(packageInfo);
            if (isShowingSensitiveContent) {
                mWindowManager.addBlockScreenCaptureForApps(packageInfos);
                FrameworkStatsLog.write(
                        FrameworkStatsLog.SENSITIVE_CONTENT_APP_PROTECTION,
                        mMediaProjectionSession.mSessionId,
                        uid,
                        mMediaProjectionSession.mUid,
                        FrameworkStatsLog.SENSITIVE_CONTENT_APP_PROTECTION__STATE__BLOCKED
                );
            } else {
                mWindowManager.removeBlockScreenCaptureForApps(packageInfos);
                FrameworkStatsLog.write(
                        FrameworkStatsLog.SENSITIVE_CONTENT_APP_PROTECTION,
                        mMediaProjectionSession.mSessionId,
                        uid,
                        mMediaProjectionSession.mUid,
                        FrameworkStatsLog.SENSITIVE_CONTENT_APP_PROTECTION__STATE__UNBLOCKED
                );
            }
        }
    }
@@ -379,8 +419,9 @@ public final class SensitiveContentProtectionManagerService extends SystemServic
            extends ISensitiveContentProtectionManager.Stub {
        private final PackageManagerInternal mPackageManagerInternal;

        SensitiveContentProtectionManagerServiceBinder() {
            mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
        SensitiveContentProtectionManagerServiceBinder(
                PackageManagerInternal packageManagerInternal) {
            mPackageManagerInternal = packageManagerInternal;
        }

        public void setSensitiveContentProtection(IBinder windowToken, String packageName,