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

Commit 075a8094 authored by Jacky Kao's avatar Jacky Kao Committed by Android (Google) Code Review
Browse files

Merge "Improvement for takeScreenshot() API"

parents a4d20a71 5519738d
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -2958,7 +2958,7 @@ package android.accessibilityservice {
  }
  }
  public static final class AccessibilityService.ScreenshotResult {
  public static final class AccessibilityService.ScreenshotResult {
    method @Nullable public android.graphics.ColorSpace getColorSpace();
    method @NonNull public android.graphics.ColorSpace getColorSpace();
    method @NonNull public android.hardware.HardwareBuffer getHardwareBuffer();
    method @NonNull public android.hardware.HardwareBuffer getHardwareBuffer();
    method public long getTimestamp();
    method public long getTimestamp();
  }
  }
+26 −23
Original line number Original line Diff line number Diff line
@@ -29,6 +29,7 @@ import android.content.Intent;
import android.content.pm.ParceledListSlice;
import android.content.pm.ParceledListSlice;
import android.graphics.Bitmap;
import android.graphics.Bitmap;
import android.graphics.ColorSpace;
import android.graphics.ColorSpace;
import android.graphics.ParcelableColorSpace;
import android.graphics.Region;
import android.graphics.Region;
import android.hardware.HardwareBuffer;
import android.hardware.HardwareBuffer;
import android.os.Binder;
import android.os.Binder;
@@ -39,6 +40,7 @@ import android.os.Looper;
import android.os.Message;
import android.os.Message;
import android.os.RemoteCallback;
import android.os.RemoteCallback;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.SystemClock;
import android.util.ArrayMap;
import android.util.ArrayMap;
import android.util.Log;
import android.util.Log;
import android.util.Slog;
import android.util.Slog;
@@ -600,8 +602,12 @@ public abstract class AccessibilityService extends Service {
            "screenshot_hardwareBuffer";
            "screenshot_hardwareBuffer";


    /** @hide */
    /** @hide */
    public static final String KEY_ACCESSIBILITY_SCREENSHOT_COLORSPACE_ID =
    public static final String KEY_ACCESSIBILITY_SCREENSHOT_COLORSPACE =
            "screenshot_colorSpaceId";
            "screenshot_colorSpace";

    /** @hide */
    public static final String KEY_ACCESSIBILITY_SCREENSHOT_TIMESTAMP =
            "screenshot_timestamp";


    /**
    /**
     * Callback for {@link android.view.accessibility.AccessibilityEvent}s.
     * Callback for {@link android.view.accessibility.AccessibilityEvent}s.
@@ -1920,6 +1926,8 @@ public abstract class AccessibilityService extends Service {
     *                  default display.
     *                  default display.
     * @param executor Executor on which to run the callback.
     * @param executor Executor on which to run the callback.
     * @param callback The callback invoked when the taking screenshot is done.
     * @param callback The callback invoked when the taking screenshot is done.
     *                 The {@link AccessibilityService.ScreenshotResult} will be null for an
     *                 invalid display.
     *
     *
     * @return {@code true} if the taking screenshot accepted, {@code false} if not.
     * @return {@code true} if the taking screenshot accepted, {@code false} if not.
     */
     */
@@ -1941,14 +1949,11 @@ public abstract class AccessibilityService extends Service {
                }
                }
                final HardwareBuffer hardwareBuffer =
                final HardwareBuffer hardwareBuffer =
                        result.getParcelable(KEY_ACCESSIBILITY_SCREENSHOT_HARDWAREBUFFER);
                        result.getParcelable(KEY_ACCESSIBILITY_SCREENSHOT_HARDWAREBUFFER);
                final int colorSpaceId =
                final ParcelableColorSpace colorSpace =
                        result.getInt(KEY_ACCESSIBILITY_SCREENSHOT_COLORSPACE_ID);
                        result.getParcelable(KEY_ACCESSIBILITY_SCREENSHOT_COLORSPACE);
                ColorSpace colorSpace = null;
                if (colorSpaceId >= 0 && colorSpaceId < ColorSpace.Named.values().length) {
                    colorSpace = ColorSpace.get(ColorSpace.Named.values()[colorSpaceId]);
                }
                ScreenshotResult screenshot = new ScreenshotResult(hardwareBuffer,
                ScreenshotResult screenshot = new ScreenshotResult(hardwareBuffer,
                        colorSpace, System.currentTimeMillis());
                        colorSpace.getColorSpace(),
                        result.getLong(KEY_ACCESSIBILITY_SCREENSHOT_TIMESTAMP));
                sendScreenshotResult(executor, callback, screenshot);
                sendScreenshotResult(executor, callback, screenshot);
            }));
            }));
        } catch (RemoteException re) {
        } catch (RemoteException re) {
@@ -2361,41 +2366,38 @@ public abstract class AccessibilityService extends Service {
    }
    }


    /**
    /**
     * Class including hardwareBuffer, colorSpace, and timestamp to be the result for
     * Can be used to construct a bitmap of the screenshot or any other operations for
     * {@link AccessibilityService#takeScreenshot} API.
     * {@link AccessibilityService#takeScreenshot} API.
     * <p>
     * <strong>Note:</strong> colorSpace would be null if the name of this colorSpace isn't at
     * {@link ColorSpace.Named}.
     * </p>
     */
     */
    public static final class ScreenshotResult {
    public static final class ScreenshotResult {
        private final @NonNull HardwareBuffer mHardwareBuffer;
        private final @NonNull HardwareBuffer mHardwareBuffer;
        private final @Nullable ColorSpace mColorSpace;
        private final @NonNull ColorSpace mColorSpace;
        private final long mTimestamp;
        private final long mTimestamp;


        private ScreenshotResult(@NonNull HardwareBuffer hardwareBuffer,
        private ScreenshotResult(@NonNull HardwareBuffer hardwareBuffer,
                @Nullable ColorSpace colorSpace, long timestamp) {
                @NonNull ColorSpace colorSpace, long timestamp) {
            Preconditions.checkNotNull(hardwareBuffer, "hardwareBuffer cannot be null");
            Preconditions.checkNotNull(hardwareBuffer, "hardwareBuffer cannot be null");
            Preconditions.checkNotNull(colorSpace, "colorSpace cannot be null");
            mHardwareBuffer = hardwareBuffer;
            mHardwareBuffer = hardwareBuffer;
            mColorSpace = colorSpace;
            mColorSpace = colorSpace;
            mTimestamp = timestamp;
            mTimestamp = timestamp;
        }
        }


        /**
        /**
         * Gets the colorSpace identifying a specific organization of colors of the screenshot.
         * Gets the {@link ColorSpace} identifying a specific organization of colors of the
         * screenshot.
         *
         *
         * @return the colorSpace or {@code null} if the name of colorSpace isn't at
         * @return the color space
         * {@link ColorSpace.Named}
         */
         */
        @Nullable
        @NonNull
        public ColorSpace getColorSpace() {
        public ColorSpace getColorSpace() {
            return mColorSpace;
            return mColorSpace;
        }
        }


        /**
        /**
         * Gets the hardwareBuffer representing a memory buffer of the screenshot.
         * Gets the {@link HardwareBuffer} representing a memory buffer of the screenshot.
         *
         *
         * @return the hardwareBuffer
         * @return the hardware buffer
         */
         */
        @NonNull
        @NonNull
        public HardwareBuffer getHardwareBuffer() {
        public HardwareBuffer getHardwareBuffer() {
@@ -2405,7 +2407,8 @@ public abstract class AccessibilityService extends Service {
        /**
        /**
         * Gets the timestamp of taking the screenshot.
         * Gets the timestamp of taking the screenshot.
         *
         *
         * @return the timestamp from {@link System#currentTimeMillis()}
         * @return milliseconds of non-sleep uptime before screenshot since boot and it's from
         * {@link SystemClock#uptimeMillis()}
         */
         */
        public long getTimestamp() {
        public long getTimestamp() {
            return mTimestamp;
            return mTimestamp;
+7 −3
Original line number Original line Diff line number Diff line
@@ -16,8 +16,9 @@


package com.android.server.accessibility;
package com.android.server.accessibility;


import static android.accessibilityservice.AccessibilityService.KEY_ACCESSIBILITY_SCREENSHOT_COLORSPACE_ID;
import static android.accessibilityservice.AccessibilityService.KEY_ACCESSIBILITY_SCREENSHOT_COLORSPACE;
import static android.accessibilityservice.AccessibilityService.KEY_ACCESSIBILITY_SCREENSHOT_HARDWAREBUFFER;
import static android.accessibilityservice.AccessibilityService.KEY_ACCESSIBILITY_SCREENSHOT_HARDWAREBUFFER;
import static android.accessibilityservice.AccessibilityService.KEY_ACCESSIBILITY_SCREENSHOT_TIMESTAMP;
import static android.accessibilityservice.AccessibilityServiceInfo.DEFAULT;
import static android.accessibilityservice.AccessibilityServiceInfo.DEFAULT;
import static android.view.WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
import static android.view.WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
import static android.view.accessibility.AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS;
import static android.view.accessibility.AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS;
@@ -39,6 +40,7 @@ import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager;
import android.content.pm.ParceledListSlice;
import android.content.pm.ParceledListSlice;
import android.graphics.GraphicBuffer;
import android.graphics.GraphicBuffer;
import android.graphics.ParcelableColorSpace;
import android.graphics.Point;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.Region;
@@ -1021,13 +1023,15 @@ abstract class AbstractAccessibilityServiceConnection extends IAccessibilityServ
                final GraphicBuffer graphicBuffer = screenshotBuffer.getGraphicBuffer();
                final GraphicBuffer graphicBuffer = screenshotBuffer.getGraphicBuffer();
                final HardwareBuffer hardwareBuffer =
                final HardwareBuffer hardwareBuffer =
                        HardwareBuffer.createFromGraphicBuffer(graphicBuffer);
                        HardwareBuffer.createFromGraphicBuffer(graphicBuffer);
                final int colorSpaceId = screenshotBuffer.getColorSpace().getId();
                final ParcelableColorSpace colorSpace =
                        new ParcelableColorSpace(screenshotBuffer.getColorSpace());


                // Send back the result.
                // Send back the result.
                final Bundle payload = new Bundle();
                final Bundle payload = new Bundle();
                payload.putParcelable(KEY_ACCESSIBILITY_SCREENSHOT_HARDWAREBUFFER,
                payload.putParcelable(KEY_ACCESSIBILITY_SCREENSHOT_HARDWAREBUFFER,
                        hardwareBuffer);
                        hardwareBuffer);
                payload.putInt(KEY_ACCESSIBILITY_SCREENSHOT_COLORSPACE_ID, colorSpaceId);
                payload.putParcelable(KEY_ACCESSIBILITY_SCREENSHOT_COLORSPACE, colorSpace);
                payload.putLong(KEY_ACCESSIBILITY_SCREENSHOT_TIMESTAMP, SystemClock.uptimeMillis());
                callback.sendResult(payload);
                callback.sendResult(payload);
            }, null).recycleOnUse());
            }, null).recycleOnUse());
        } finally {
        } finally {