From 80e9e91ff8374f781dbf9263c5919c224378cf36 Mon Sep 17 00:00:00 2001 From: Aleksander Ivanov Date: Tue, 19 Jul 2016 13:41:23 +0300 Subject: [PATCH] Migrate ImageEntity Migrate ImageEntity from atmosphere-client --- .../devicewrapper/AbstractWrapDevice.java | 49 ++++-------- .../atmosphere/agent/entity/ImageEntity.java | 78 +++++++++++++++++++ 2 files changed, 91 insertions(+), 36 deletions(-) create mode 100644 src/main/java/com/musala/atmosphere/agent/entity/ImageEntity.java diff --git a/src/main/java/com/musala/atmosphere/agent/devicewrapper/AbstractWrapDevice.java b/src/main/java/com/musala/atmosphere/agent/devicewrapper/AbstractWrapDevice.java index 37458b1..0954d93 100755 --- a/src/main/java/com/musala/atmosphere/agent/devicewrapper/AbstractWrapDevice.java +++ b/src/main/java/com/musala/atmosphere/agent/devicewrapper/AbstractWrapDevice.java @@ -50,6 +50,7 @@ import com.musala.atmosphere.agent.entity.DeviceSettingsEntity; import com.musala.atmosphere.agent.entity.GestureEntity; import com.musala.atmosphere.agent.entity.HardwareButtonEntity; +import com.musala.atmosphere.agent.entity.ImageEntity; import com.musala.atmosphere.agent.entity.ImeEntity; import com.musala.atmosphere.agent.exception.OnDeviceServiceTerminationException; import com.musala.atmosphere.agent.exception.UnresolvedEntityTypeException; @@ -96,10 +97,6 @@ public abstract class AbstractWrapDevice extends UnicastRemoteObject implements private static final String XMLDUMP_LOCAL_FILE_NAME = "uidump-%s.xml"; - private static final String SCREENSHOT_REMOTE_FILE_NAME = "/data/local/tmp/remote_screen.png"; - - private static final String SCREENSHOT_COMMAND = "screencap -p " + SCREENSHOT_REMOTE_FILE_NAME; - private static final String FORCE_STOP_PROCESS_COMMAND = "am force-stop "; // TODO the command should be moved to a different enumeration @@ -111,8 +108,6 @@ public abstract class AbstractWrapDevice extends UnicastRemoteObject implements private static final String GET_PID_PATTERN = "| grep -Eo [0-9]+ | grep -m 1 -Eo [0-9]+"; - private static final String SCREENSHOT_LOCAL_FILE_NAME = "local_screen.png"; - private static final String FIRST_SCREEN_RECORD_NAME = "1.mp4"; private static final String FALLBACK_COMPONENT_PATH = "/data/local/tmp"; @@ -179,6 +174,8 @@ public abstract class AbstractWrapDevice extends UnicastRemoteObject implements private DeviceSettingsEntity settingsEntity; + private ImageEntity imageEntity; + /** * Creates an abstract wrapper of the given {@link IDevice device}. * @@ -264,7 +261,7 @@ public Object route(RoutingAction action, Object... args) throws RemoteException returnValue = getDeviceInformation(); break; case GET_SCREENSHOT: - returnValue = getScreenshot(); + returnValue = imageEntity.getScreenshot(); break; case GET_UI_XML_DUMP: returnValue = getUiXml(); @@ -799,6 +796,15 @@ private void setupDeviceEntities(DeviceInformation deviceInformation) { DeviceSettingsEntity settingsEntity = (DeviceSettingsEntity) settingsEntitiyConstructor.newInstance(new Object[] { shellCommandExecutor, deviceInformation}); this.settingsEntity = settingsEntity; + + Constructor imageEntityConstructor = ImageEntity.class.getDeclaredConstructor(ShellCommandExecutor.class, + IDevice.class); + imageEntityConstructor.setAccessible(true); + ImageEntity imageEntity = (ImageEntity) imageEntityConstructor.newInstance(new Object[] { + shellCommandExecutor, + wrappedDevice + }); + this.imageEntity = imageEntity; } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new UnresolvedEntityTypeException("Failed to find the correct set of entities implementations matching the given device information.", @@ -806,35 +812,6 @@ private void setupDeviceEntities(DeviceInformation deviceInformation) { } } - /** - * Returns a JPEG compressed display screenshot. - * - * @return Image in an array of bytes that, when dumped to a file, shows the device display. - * @throws CommandFailedException - */ - private byte[] getScreenshot() throws CommandFailedException { - shellCommandExecutor.execute(SCREENSHOT_COMMAND); - - FileInputStream fileReader = null; - - try { - wrappedDevice.pullFile(SCREENSHOT_REMOTE_FILE_NAME, SCREENSHOT_LOCAL_FILE_NAME); - - File localScreenshotFile = new File(SCREENSHOT_LOCAL_FILE_NAME); - fileReader = new FileInputStream(localScreenshotFile); - - final long sizeOfScreenshotFile = localScreenshotFile.length(); - byte[] screenshotData = new byte[(int) sizeOfScreenshotFile]; - fileReader.read(screenshotData); - fileReader.close(); - - return screenshotData; - } catch (IOException | AdbCommandRejectedException | TimeoutException | SyncException e) { - LOGGER.error("Screenshot fetching failed.", e); - throw new CommandFailedException("Screenshot fetching failed.", e); - } - } - /** * Gets the UIAutomator UI XML dump. * diff --git a/src/main/java/com/musala/atmosphere/agent/entity/ImageEntity.java b/src/main/java/com/musala/atmosphere/agent/entity/ImageEntity.java new file mode 100644 index 0000000..5b5e869 --- /dev/null +++ b/src/main/java/com/musala/atmosphere/agent/entity/ImageEntity.java @@ -0,0 +1,78 @@ +package com.musala.atmosphere.agent.entity; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +import org.apache.log4j.Logger; + +import com.android.ddmlib.AdbCommandRejectedException; +import com.android.ddmlib.IDevice; +import com.android.ddmlib.SyncException; +import com.android.ddmlib.TimeoutException; +import com.musala.atmosphere.agent.devicewrapper.util.ShellCommandExecutor; +import com.musala.atmosphere.commons.exceptions.CommandFailedException; + +/** + * Entity responsible for operations related with images. + * + * @author yavor.stankov + * + */ +public class ImageEntity { + private static final Logger LOGGER = Logger.getLogger(ImageEntity.class.getCanonicalName()); + + private static final String SCREENSHOT_REMOTE_FILE_NAME = "/data/local/tmp/remote_screen.png"; + + private static final String SCREENSHOT_LOCAL_FILE_NAME = "local_screen.png"; + + private static final String SCREENSHOT_COMMAND = "screencap -p " + SCREENSHOT_REMOTE_FILE_NAME; + + private ShellCommandExecutor shellCommandExecutor; + + private IDevice wrappedDevice; + + /** + * Constructs a new {@link ImageEntity} object by a given {@link ShellCommandExecutor shell command executor} + * and a {@link IDevice wrapped ddmlib device}. + * + * @param shellCommandExecutor + * - a shell command executor to execute shell commands with + * + * @param wrappedDevice + * - a wrapped ddmlib device + */ + ImageEntity(ShellCommandExecutor shellCommandExecutor, IDevice wrappedDevice) { + this.shellCommandExecutor = shellCommandExecutor; + this.wrappedDevice = wrappedDevice; + } + + /** + * Returns a JPEG compressed display screenshot. + * + * @return Image in an array of bytes that, when dumped to a file, shows the device display. + * @throws CommandFailedException + */ + public byte[] getScreenshot() throws CommandFailedException { + shellCommandExecutor.execute(SCREENSHOT_COMMAND); + + FileInputStream fileReader = null; + + try { + wrappedDevice.pullFile(SCREENSHOT_REMOTE_FILE_NAME, SCREENSHOT_LOCAL_FILE_NAME); + + File localScreenshotFile = new File(SCREENSHOT_LOCAL_FILE_NAME); + fileReader = new FileInputStream(localScreenshotFile); + + final long sizeOfScreenshotFile = localScreenshotFile.length(); + byte[] screenshotData = new byte[(int) sizeOfScreenshotFile]; + fileReader.read(screenshotData); + fileReader.close(); + + return screenshotData; + } catch (IOException | AdbCommandRejectedException | TimeoutException | SyncException e) { + LOGGER.error("Screenshot fetching failed.", e); + throw new CommandFailedException("Screenshot fetching failed.", e); + } + } +}