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

Commit daee6413 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Stop contextual search showing in its own screenshots" into main

parents 74cfeceb d40067dc
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -27,7 +27,10 @@ flag {
    name: "contextual_search_window_layer"
    namespace: "sysui_integrations"
    description: "Identify live contextual search UI to exclude from contextual search screenshot."
    bug: "372510690"
    bug: "390176823"
    metadata {
        purpose: PURPOSE_BUGFIX
    }
}

flag {
+14 −24
Original line number Diff line number Diff line
@@ -27,10 +27,6 @@ import static android.content.Intent.FLAG_ACTIVITY_NO_USER_ACTION;
import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_AWARE;
import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR;
import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL;
import static android.view.WindowManager.LayoutParams.TYPE_POINTER;
import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;

import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_CONTENT;
import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_STRUCTURE;
@@ -74,6 +70,7 @@ import android.util.Log;
import android.util.Slog;
import android.view.IWindowManager;
import android.window.ScreenCapture;
import android.window.ScreenCapture.ScreenshotHardwareBuffer;

import com.android.internal.R;
import com.android.internal.annotations.GuardedBy;
@@ -333,10 +330,10 @@ public class ContextualSearchManagerService extends SystemService {
                isManagedProfileVisible = true;
            }
        }
        final String csPackage = Objects.requireNonNull(launchIntent.getPackage());
        final int csUid = mPackageManager.getPackageUid(csPackage, /* flags */ 0L, userId);
        if (isAssistDataAllowed) {
            try {
                final String csPackage = Objects.requireNonNull(launchIntent.getPackage());
                final int csUid = mPackageManager.getPackageUid(csPackage, 0, 0);
                mAssistDataRequester.requestAssistData(
                        activityTokens,
                        /* fetchData */ true,
@@ -350,17 +347,8 @@ public class ContextualSearchManagerService extends SystemService {
                Log.e(TAG, "Could not request assist data", e);
            }
        }
        final ScreenCapture.ScreenshotHardwareBuffer shb;
        if (mWmInternal != null) {
            shb = mWmInternal.takeAssistScreenshot(Set.of(
                    TYPE_STATUS_BAR,
                    TYPE_NAVIGATION_BAR,
                    TYPE_NAVIGATION_BAR_PANEL,
                    TYPE_POINTER));
        } else {
            if (DEBUG) Log.w(TAG, "Can't capture contextual screenshot: mWmInternal is null");
            shb = null;
        }
        final ScreenshotHardwareBuffer shb = mWmInternal.takeContextualSearchScreenshot(
                (Flags.contextualSearchWindowLayer() ? csUid : -1));
        final Bitmap bm = shb != null ? shb.asBitmap() : null;
        // Now that everything is fetched, putting it in the launchIntent.
        if (bm != null) {
@@ -509,15 +497,17 @@ public class ContextualSearchManagerService extends SystemService {
                bundle.putParcelable(ContextualSearchManager.EXTRA_TOKEN, mToken);
                // We get take the screenshot with the system server's identity because the system
                // server has READ_FRAME_BUFFER permission to get the screenshot.
                final int callingUid = Binder.getCallingUid();
                Binder.withCleanCallingIdentity(() -> {
                    if (mWmInternal != null) {
                    final ScreenshotHardwareBuffer shb =
                            mWmInternal.takeContextualSearchScreenshot(
                               (Flags.contextualSearchWindowLayer() ? callingUid : -1));
                    final Bitmap bm = shb != null ? shb.asBitmap() : null;
                    if (bm != null) {
                        bundle.putParcelable(ContextualSearchManager.EXTRA_SCREENSHOT,
                                mWmInternal.takeAssistScreenshot(Set.of(
                                        TYPE_STATUS_BAR,
                                        TYPE_NAVIGATION_BAR,
                                        TYPE_NAVIGATION_BAR_PANEL,
                                        TYPE_POINTER))
                                .asBitmap().asShared());
                                bm.asShared());
                        bundle.putBoolean(ContextualSearchManager.EXTRA_FLAG_SECURE_FOUND,
                                shb.containsSecureLayers());
                    }
                    try {
                        callback.onResult(
+8 −9
Original line number Diff line number Diff line
@@ -5165,7 +5165,7 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp
    /**
     * Creates a LayerCaptureArgs object to represent the entire DisplayContent
     */
    LayerCaptureArgs getLayerCaptureArgs(Set<Integer> windowTypesToExclude) {
    LayerCaptureArgs getLayerCaptureArgs(@Nullable ToBooleanFunction<WindowState> predicate) {
        if (!mWmService.mPolicy.isScreenOn()) {
            if (DEBUG_SCREENSHOT) {
                Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
@@ -5178,17 +5178,16 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp
        LayerCaptureArgs.Builder builder = new LayerCaptureArgs.Builder(getSurfaceControl())
                .setSourceCrop(mTmpRect);

        if (!windowTypesToExclude.isEmpty()) {
            ArrayList<SurfaceControl> surfaceControls = new ArrayList<>();
        if (predicate != null) {
            ArrayList<SurfaceControl> excludeLayers = new ArrayList<>();
            forAllWindows(
                    window -> {
                        if (windowTypesToExclude.contains(window.getWindowType())) {
                            surfaceControls.add(window.mSurfaceControl);
                        if (!predicate.apply(window)) {
                            excludeLayers.add(window.mSurfaceControl);
                        }
                    }, true
            );
            if (!surfaceControls.isEmpty()) {
                builder.setExcludeLayers(surfaceControls.toArray(new SurfaceControl[0]));
                    }, true);
            if (!excludeLayers.isEmpty()) {
                builder.setExcludeLayers(excludeLayers.toArray(new SurfaceControl[0]));
            }
        }
        return builder.build();
+11 −2
Original line number Diff line number Diff line
@@ -1137,6 +1137,15 @@ public abstract class WindowManagerInternal {
     * Returns an instance of {@link ScreenshotHardwareBuffer} containing the current
     * screenshot.
     */
    public abstract ScreenshotHardwareBuffer takeAssistScreenshot(
            Set<Integer> windowTypesToExclude);
    public abstract ScreenshotHardwareBuffer takeAssistScreenshot();

    /**
     * Returns an instance of {@link ScreenshotHardwareBuffer} containing the current
     * screenshot, excluding layers that are not appropriate to pass to contextual search
     * services - such as the cursor or any current contextual search window.
     *
     * @param uid the UID of the contextual search application. System alert windows belonging
     * to this UID will be excluded from the screenshot.
     */
    public abstract ScreenshotHardwareBuffer takeContextualSearchScreenshot(int uid);
}
+25 −6
Original line number Diff line number Diff line
@@ -325,6 +325,7 @@ import android.window.WindowContainerToken;
import android.window.WindowContextInfo;

import com.android.internal.R;
import com.android.internal.util.ToBooleanFunction;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting.Visibility;
@@ -4159,7 +4160,8 @@ public class WindowManagerService extends IWindowManager.Stub
    }

    @Nullable
    private ScreenshotHardwareBuffer takeAssistScreenshot(Set<Integer> windowTypesToExclude) {
    private ScreenshotHardwareBuffer takeAssistScreenshot(
            @Nullable ToBooleanFunction<WindowState> predicate) {
        if (!checkCallingPermission(READ_FRAME_BUFFER, "requestAssistScreenshot()")) {
            throw new SecurityException("Requires READ_FRAME_BUFFER permission");
        }
@@ -4174,7 +4176,7 @@ public class WindowManagerService extends IWindowManager.Stub
                }
                captureArgs = null;
            } else {
                captureArgs = displayContent.getLayerCaptureArgs(windowTypesToExclude);
                captureArgs = displayContent.getLayerCaptureArgs(predicate);
            }
        }

@@ -4204,8 +4206,7 @@ public class WindowManagerService extends IWindowManager.Stub
     */
    @Override
    public boolean requestAssistScreenshot(final IAssistDataReceiver receiver) {
        final ScreenshotHardwareBuffer shb =
                takeAssistScreenshot(/* windowTypesToExclude= */ Set.of());
        final ScreenshotHardwareBuffer shb = takeAssistScreenshot(/* predicate= */ null);
        final Bitmap bm = shb != null ? shb.asBitmap() : null;
        FgThread.getHandler().post(() -> {
            try {
@@ -8618,9 +8619,27 @@ public class WindowManagerService extends IWindowManager.Stub
        }

        @Override
        public ScreenshotHardwareBuffer takeAssistScreenshot(Set<Integer> windowTypesToExclude) {
        public ScreenshotHardwareBuffer takeAssistScreenshot() {
            // WMS.takeAssistScreenshot takes care of the locking.
            return WindowManagerService.this.takeAssistScreenshot(windowTypesToExclude);
            return WindowManagerService.this.takeAssistScreenshot(/* predicate */ null);
        }

        @Override
        public ScreenshotHardwareBuffer takeContextualSearchScreenshot(int uid) {
            // WMS.takeAssistScreenshot takes care of the locking.
            return WindowManagerService.this.takeAssistScreenshot(win -> {
                switch (win.getWindowType()) {
                    case LayoutParams.TYPE_STATUS_BAR:
                    case LayoutParams.TYPE_NAVIGATION_BAR:
                    case LayoutParams.TYPE_NAVIGATION_BAR_PANEL:
                    case LayoutParams.TYPE_POINTER:
                        return false;
                    case LayoutParams.TYPE_APPLICATION_OVERLAY:
                        return uid != win.getOwningUid();
                    default:
                        return true;
                }
            });
        }
    }

Loading