From 3e33bedee01bc5e3b60b6d7df2e0ae31cf0cff05 Mon Sep 17 00:00:00 2001 From: Jacky Kao Date: Fri, 31 Jan 2020 10:31:11 +0800 Subject: [PATCH] Changing the return type of takeScreenshot() (1/n) Roll back the method, takeScreenshot(), of the UiAutomationConnection class using by the UiAutomation class due to the return type of the method, takeScreenshot(), of the AccessibilityService class, isn't the Bitmap. Besides they also used the different methods of the SurfaceControl class now. Bug: 10931661 Test: a11y CTS tests Change-Id: I7459ebfe77162e51ed3a50c8663e13579a8a67a3 --- .../android/app/IUiAutomationConnection.aidl | 1 + core/java/android/app/UiAutomation.java | 36 +++++++++++++++---- .../android/app/UiAutomationConnection.java | 22 ++++++++++-- .../AccessibilityInteractionClient.java | 26 -------------- 4 files changed, 50 insertions(+), 35 deletions(-) diff --git a/core/java/android/app/IUiAutomationConnection.aidl b/core/java/android/app/IUiAutomationConnection.aidl index 80ba464851e0e..8c3180b400efb 100644 --- a/core/java/android/app/IUiAutomationConnection.aidl +++ b/core/java/android/app/IUiAutomationConnection.aidl @@ -39,6 +39,7 @@ interface IUiAutomationConnection { boolean injectInputEvent(in InputEvent event, boolean sync); void syncInputTransactions(); boolean setRotation(int rotation); + Bitmap takeScreenshot(in Rect crop, int rotation); boolean clearWindowContentFrameStats(int windowId); WindowContentFrameStats getWindowContentFrameStats(int windowId); void clearWindowAnimationFrameStats(); diff --git a/core/java/android/app/UiAutomation.java b/core/java/android/app/UiAutomation.java index 35cf68737ccc1..a9a06dabc0491 100644 --- a/core/java/android/app/UiAutomation.java +++ b/core/java/android/app/UiAutomation.java @@ -27,7 +27,10 @@ import android.annotation.Nullable; import android.annotation.TestApi; import android.compat.annotation.UnsupportedAppUsage; import android.graphics.Bitmap; +import android.graphics.Point; +import android.graphics.Rect; import android.graphics.Region; +import android.hardware.display.DisplayManagerGlobal; import android.os.Build; import android.os.Handler; import android.os.HandlerThread; @@ -828,20 +831,39 @@ public final class UiAutomation { } /** - * Takes a screenshot of the default display and returns it by {@link Bitmap.Config#HARDWARE} - * format. + * Takes a screenshot. * * @return The screenshot bitmap on success, null otherwise. */ public Bitmap takeScreenshot() { - final int connectionId; synchronized (mLock) { throwIfNotConnectedLocked(); - connectionId = mConnectionId; } - // Calling out without a lock held. - return AccessibilityInteractionClient.getInstance() - .takeScreenshot(connectionId, Display.DEFAULT_DISPLAY); + Display display = DisplayManagerGlobal.getInstance() + .getRealDisplay(Display.DEFAULT_DISPLAY); + Point displaySize = new Point(); + display.getRealSize(displaySize); + + int rotation = display.getRotation(); + + // Take the screenshot + Bitmap screenShot = null; + try { + // Calling out without a lock held. + screenShot = mUiAutomationConnection.takeScreenshot( + new Rect(0, 0, displaySize.x, displaySize.y), rotation); + if (screenShot == null) { + return null; + } + } catch (RemoteException re) { + Log.e(LOG_TAG, "Error while taking screnshot!", re); + return null; + } + + // Optimization + screenShot.setHasAlpha(false); + + return screenShot; } /** diff --git a/core/java/android/app/UiAutomationConnection.java b/core/java/android/app/UiAutomationConnection.java index 4fb974305e486..82e988109db8e 100644 --- a/core/java/android/app/UiAutomationConnection.java +++ b/core/java/android/app/UiAutomationConnection.java @@ -21,6 +21,8 @@ import android.accessibilityservice.IAccessibilityServiceClient; import android.annotation.Nullable; import android.compat.annotation.UnsupportedAppUsage; import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.Rect; import android.hardware.input.InputManager; import android.os.Binder; import android.os.IBinder; @@ -177,6 +179,23 @@ public final class UiAutomationConnection extends IUiAutomationConnection.Stub { return false; } + @Override + public Bitmap takeScreenshot(Rect crop, int rotation) { + synchronized (mLock) { + throwIfCalledByNotTrustedUidLocked(); + throwIfShutdownLocked(); + throwIfNotConnectedLocked(); + } + final long identity = Binder.clearCallingIdentity(); + try { + int width = crop.width(); + int height = crop.height(); + return SurfaceControl.screenshot(crop, width, height, rotation); + } finally { + Binder.restoreCallingIdentity(identity); + } + } + @Override public boolean clearWindowContentFrameStats(int windowId) throws RemoteException { synchronized (mLock) { @@ -430,8 +449,7 @@ public final class UiAutomationConnection extends IUiAutomationConnection.Stub { info.setCapabilities(AccessibilityServiceInfo.CAPABILITY_CAN_RETRIEVE_WINDOW_CONTENT | AccessibilityServiceInfo.CAPABILITY_CAN_REQUEST_TOUCH_EXPLORATION | AccessibilityServiceInfo.CAPABILITY_CAN_REQUEST_ENHANCED_WEB_ACCESSIBILITY - | AccessibilityServiceInfo.CAPABILITY_CAN_REQUEST_FILTER_KEY_EVENTS - | AccessibilityServiceInfo.CAPABILITY_CAN_TAKE_SCREENSHOT); + | AccessibilityServiceInfo.CAPABILITY_CAN_REQUEST_FILTER_KEY_EVENTS); try { // Calling out with a lock held is fine since if the system // process is gone the client calling in will be killed. diff --git a/core/java/android/view/accessibility/AccessibilityInteractionClient.java b/core/java/android/view/accessibility/AccessibilityInteractionClient.java index b9f08ada3152a..b4c87953567b1 100644 --- a/core/java/android/view/accessibility/AccessibilityInteractionClient.java +++ b/core/java/android/view/accessibility/AccessibilityInteractionClient.java @@ -20,7 +20,6 @@ import android.accessibilityservice.IAccessibilityServiceConnection; import android.annotation.NonNull; import android.annotation.Nullable; import android.compat.annotation.UnsupportedAppUsage; -import android.graphics.Bitmap; import android.os.Binder; import android.os.Build; import android.os.Bundle; @@ -828,31 +827,6 @@ public final class AccessibilityInteractionClient } } - /** - * Takes the screenshot of the specified display and returns it by bitmap format. - * - * @param connectionId The id of a connection for interacting with the system. - * @param displayId The logic display id, use {@link Display#DEFAULT_DISPLAY} for - * default display. - * @return The screenshot bitmap on success, null otherwise. - */ - public Bitmap takeScreenshot(int connectionId, int displayId) { - Bitmap screenShot = null; - try { - IAccessibilityServiceConnection connection = getConnection(connectionId); - if (connection != null) { - screenShot = connection.takeScreenshot(displayId); - } else { - if (DEBUG) { - Log.w(LOG_TAG, "No connection for connection id: " + connectionId); - } - } - } catch (RemoteException re) { - Log.w(LOG_TAG, "Error while calling remote takeScreenshot", re); - } - return screenShot; - } - /** * Clears the result state. */