Merge "Changing the return type of takeScreenshot() (1/n)"

This commit is contained in:
Jacky Kao
2020-02-05 05:53:34 +00:00
committed by Android (Google) Code Review
4 changed files with 50 additions and 35 deletions

View File

@@ -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();

View File

@@ -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;
}
/**

View File

@@ -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.

View File

@@ -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.
*/