Skip to content

Commit

Permalink
Migrate ImageEntity
Browse files Browse the repository at this point in the history
Migrate ImageEntity from atmosphere-client
  • Loading branch information
vanogrid committed Oct 17, 2016
1 parent cc0a37f commit 80e9e91
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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";
Expand Down Expand Up @@ -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}.
*
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -799,42 +796,22 @@ 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.",
e);
}
}

/**
* 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.
*
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/com/musala/atmosphere/agent/entity/ImageEntity.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 80e9e91

Please sign in to comment.