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

Commit 7a9db9ba authored by Steven Ng's avatar Steven Ng Committed by Android (Google) Code Review
Browse files

Merge "Add a TestApi to capture screenshots based on a specified display ID" into main

parents 06722d90 64d71fb1
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -485,6 +485,7 @@ package android.app {
    method @Deprecated public boolean revokeRuntimePermission(String, String, android.os.UserHandle);
    method public void syncInputTransactions();
    method public void syncInputTransactions(boolean);
    method @NonNull public android.graphics.Bitmap takeScreenshot(int) throws java.io.IOException;
    field @NonNull public static final java.util.Set<java.lang.String> ALL_PERMISSIONS;
    field public static final int FLAG_NOT_ACCESSIBILITY_TOOL = 4; // 0x4
  }
+58 −0
Original line number Diff line number Diff line
@@ -1315,6 +1315,64 @@ public final class UiAutomation {
        return swBitmap;
    }

    /**
     * Takes a screenshot from the specified display.
     *
     * @param displayId The ID of the display to capture.
     * @return A {@link android.graphics.Bitmap} representing the screenshot of the specified
     *         display.
     * @throws IllegalArgumentException If the provided {@code displayId} does not correspond to
     * a valid display.
     * @throws IOException If an error occurs while creating the screenshot or processing the
     * captured screenshot into a bitmap.
     * @hide
     */
    @TestApi
    @NonNull
    @SuppressLint("UnflaggedApi") // TestApi
    public Bitmap takeScreenshot(int displayId) throws IOException {
        if (DEBUG) {
            Log.d(LOG_TAG, "Taking screenshot of display " + displayId);
        }
        Display display = DisplayManagerGlobal.getInstance().getRealDisplay(displayId);
        if (display == null) {
            throw new IllegalArgumentException("Error finding the display " + displayId);
        }
        Point displaySize = new Point();
        display.getRealSize(displaySize);

        // Take the screenshot
        ScreenCapture.SynchronousScreenCaptureListener syncScreenCapture =
                ScreenCapture.createSyncCaptureListener();
        try {
            if (!mUiAutomationConnection.takeScreenshot(
                    new Rect(0, 0, displaySize.x, displaySize.y), syncScreenCapture, displayId)) {
                throw new IOException("Fail to capture screenshot for display=" + displayId
                        + " due to remote error.");
            }
        } catch (RemoteException e) {
            e.rethrowFromSystemServer();
        }

        final ScreenshotHardwareBuffer screenshotBuffer = syncScreenCapture.getBuffer();
        if (screenshotBuffer == null) {
            throw new IOException("Empty screenshot buffer for display=" + displayId);
        }
        Bitmap screenShot = screenshotBuffer.asBitmap();
        if (screenShot == null) {
            throw new IOException("Fail to create screenshot bitmap for display=" + displayId);
        }
        Bitmap swBitmap;
        try (HardwareBuffer buffer = screenshotBuffer.getHardwareBuffer()) {
            swBitmap = screenShot.copy(Bitmap.Config.ARGB_8888, false);
        }
        screenShot.recycle();

        // Optimization
        swBitmap.setHasAlpha(false);
        return swBitmap;
    }

    /**
     * Used to capture a screenshot of a Window. This can return null in the following cases:
     * 1. Window content hasn't been layed out.