package com.ours.tester;
import java.io.FileWriter;
import java.util.Date;
import java.text.SimpleDateFormat;
import com.android.ddmlib.MultiLineReceiver;
import com.android.ddmlib.SyncException;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.android.ddmlib.ShellCommandUnresponsiveException;
import com.android.ddmlib.AdbCommandRejectedException;
import com.android.ddmlib.TimeoutException;
import com.android.ddmlib.IShellOutputReceiver;
import com.android.ddmlib.Log;
import java.io.File;
import java.nio.file.Paths;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.AndroidDebugBridge;
public class ADB
{
public static final String LOG_TAG = "mytester";
public String file_location;
public static boolean isStartNewLogFile;
private AndroidDebugBridge mAndroidDebugBridge;
public static IDevice[] mDevices;
public static IDevice mDevice;
public static FileOutputReceiver fReciever;
public static boolean isLogCancelled;
private static String myTesterEventScript_file;
static final String mySendEventAdbLocation = "/data/local/tmp/mysendevent";
public static final String mySendEventLocation;
static {
ADB.isStartNewLogFile = true;
ADB.isLogCancelled = false;
ADB.myTesterEventScript_file = "/data/local/tmp/myTesterEventScript.mes";
mySendEventLocation = String.valueOf(Paths.get("", new String[0]).toAbsolutePath().toString()) + File.separator + "mysendevent";
}
public boolean initialize() {
boolean success = true;
String adbLocation = System.getenv("ANDROID_HOME");
if (adbLocation != null) {
adbLocation = String.valueOf(adbLocation) + File.separator + "platform-tools";
}
if (success) {
if (adbLocation != null && adbLocation.length() != 0) {
adbLocation = String.valueOf(adbLocation) + File.separator + "adb";
}
else {
adbLocation = "adb";
}
System.out.println("adb path is " + adbLocation);
AndroidDebugBridge.init(false);
this.mAndroidDebugBridge = AndroidDebugBridge.createBridge(adbLocation, true);
if (this.mAndroidDebugBridge == null) {
success = false;
}
}
if (success) {
int count = 0;
while (!this.mAndroidDebugBridge.hasInitialDeviceList()) {
try {
Thread.sleep(100L);
++count;
}
catch (InterruptedException ex) {}
if (count > 100) {
success = false;
break;
}
}
}
if (!success) {
this.terminate();
}
return success;
}
public void terminate() {
AndroidDebugBridge.terminate();
}
public IDevice[] getDevices() {
IDevice[] devices = null;
if (this.mAndroidDebugBridge != null) {
devices = this.mAndroidDebugBridge.getDevices();
}
return devices;
}
public String[] adbDevices() {
ADB.mDevices = this.getDevices();
if (ADB.mDevices.length == 0) {
System.out.println("No connected devices!");
return null;
}
final String[] list = new String[ADB.mDevices.length + 1];
if (ADB.mDevices != null) {
for (int i = 0; i < ADB.mDevices.length; ++i) {
list = ADB.mDevices.toString();
}
ADB.mDevice = ADB.mDevices[0];
}
list[ADB.mDevices.length] = "All Devices";
return list;
}
public String getDeviceName() {
return ADB.mDevice.getName();
}
public IDevice getMyDevice() {
return ADB.mDevice;
}
public String shell(final String command) throws IOException {
Log.i("mytester", String.format("adb shell %s", command));
System.out.println("adb shell " + command);
final CollectingOutputReceiver receiver = new CollectingOutputReceiver((CollectingOutputReceiver)null);
try {
ADB.mDevice.executeShellCommand(command, (IShellOutputReceiver)receiver);
}
catch (TimeoutException e) {
e.printStackTrace();
}
catch (AdbCommandRejectedException e2) {
e2.printStackTrace();
}
catch (ShellCommandUnresponsiveException e3) {
e3.printStackTrace();
}
final String output = receiver.getOutput();
Log.i("mytester", String.format("Result: %s", output));
return output;
}
public FileOutputReceiver startlogGetEventToFile() {
ADB.isLogCancelled = false;
System.out.println("Start Logging...");
final String command = "getevent -t";
final FileOutputReceiver fReceiver = new FileOutputReceiver();
System.out.println("Before " + ADB.isStartNewLogFile);
try {
ADB.mDevice.executeShellCommand(command, (IShellOutputReceiver)fReceiver);
}
catch (TimeoutException e) {
e.printStackTrace();
}
catch (AdbCommandRejectedException e2) {
e2.printStackTrace();
}
catch (ShellCommandUnresponsiveException e3) {
e3.printStackTrace();
}
catch (IOException e4) {
e4.printStackTrace();
}
System.out.println("From startloggin " + ADB.isStartNewLogFile);
fReceiver.getOutputToFile();
fReceiver.flush();
fReceiver.isCancelled();
fReceiver.done();
return fReceiver;
}
public String stoplogGetEventToFile() {
ADB.isLogCancelled = true;
try {
Thread.sleep(1000L);
}
catch (InterruptedException e) {
e.printStackTrace();
}
ADB.isStartNewLogFile = true;
System.out.println("Stop: getevent logging done\n" + this.file_location);
return this.file_location;
}
public static boolean isStartNewLogFile() {
return ADB.isStartNewLogFile;
}
public void setStartNewLogFile(final boolean isStartNewLogFile) {
ADB.isStartNewLogFile = isStartNewLogFile;
}
public String resumelogGetEventToFile() {
ADB.isStartNewLogFile = false;
System.out.println("Resume getevent logging " + this.file_location);
this.startlogGetEventToFile();
return this.file_location;
}
public String pauselogGetEventToFile() {
ADB.isLogCancelled = true;
try {
Thread.sleep(1000L);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Pause: getevent logging done\n" + this.file_location);
return this.file_location;
}
public void runSendEventScript(final String mes_script_location) throws IOException {
final String cmd = "sh " + mes_script_location;
this.shell(cmd);
}
public void initmySendEvent() throws IOException {
final InputStream is = this.getClass().getClassLoader().getResourceAsStream("mysendevent");
FileOutputStream fos = null;
try {
fos = new FileOutputStream("mysendevent");
final byte[] buf = new byte[2048];
for (int r = is.read(buf); r != -1; r = is.read(buf)) {
fos.write(buf, 0, r);
}
}
finally {
if (fos != null) {
fos.close();
}
}
if (fos != null) {
fos.close();
}
this.adbPush(ADB.mySendEventLocation, "/data/local/tmp/mysendevent");
this.shell("chmod 777 /data/local/tmp/mysendevent");
}
public void convertPushPlayEventScript(final String mel_file_location, final boolean play_only) {
System.out.println("convertpushPlay" + mel_file_location);
try {
if (!play_only) {
final String mes_script_file = new Utils().convertgetEventToSendEvent(mel_file_location);
this.adbPush(mes_script_file, ADB.myTesterEventScript_file);
}
this.runSendEventScript(ADB.myTesterEventScript_file);
}
catch (IOException e) {
e.printStackTrace();
}
}
public void adbPush(final String src, final String dst) {
System.out.println("adb push: " + src + "to " + dst);
try {
ADB.mDevice.pushFile(src, dst);
}
catch (SyncException e) {
e.printStackTrace();
}
catch (IOException e2) {
e2.printStackTrace();
}
catch (AdbCommandRejectedException e3) {
e3.printStackTrace();
}
catch (TimeoutException e4) {
e4.printStackTrace();
}
}
private class CollectingOutputReceiver extends MultiLineReceiver
{
private StringBuffer mOutputBuffer;
private CollectingOutputReceiver() {
this.mOutputBuffer = new StringBuffer();
}
public String getOutput() {
return this.mOutputBuffer.toString();
}
public void processNewLines(final String[] lines) {
for (final String line : lines) {
this.mOutputBuffer.append(line);
this.mOutputBuffer.append("\n");
}
}
public boolean isCancelled() {
return false;
}
}
public class FileOutputReceiver extends MultiLineReceiver
{
private StringBuffer mOutputBuffer;
public FileOutputReceiver() {
this.mOutputBuffer = new StringBuffer();
}
public void getOutputToFile() {
if (ADB.isStartNewLogFile) {
ADB.this.file_location = String.valueOf(Paths.get("", new String[0]).toAbsolutePath().toString()) + File.separator + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + "_events.mel";
System.out.println("New logging");
}
try {
final FileWriter fw = new FileWriter(ADB.this.file_location, true);
fw.write(this.mOutputBuffer.toString());
fw.close();
System.out.println("Written to file " + ADB.this.file_location);
}
catch (IOException e) {
System.out.println("Cant open file in this location: " + ADB.this.file_location);
e.printStackTrace();
}
}
public void processNewLines(final String[] lines) {
for (final String line : lines) {
this.mOutputBuffer.append(line);
this.mOutputBuffer.append("\n");
}
}
public boolean isCancelled() {
return ADB.isLogCancelled;
}
}
}