Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaocong committed Dec 13, 2013
2 parents 8834fd1 + 46c6d5e commit bc33fd6
Show file tree
Hide file tree
Showing 3 changed files with 541 additions and 222 deletions.
104 changes: 61 additions & 43 deletions src/com/github/uiautomatorstub/AutomatorHttpServer.java
Original file line number Diff line number Diff line change
@@ -1,55 +1,73 @@
package com.github.uiautomatorstub;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

import android.os.Build;
import com.android.uiautomator.core.UiDevice;
import com.googlecode.jsonrpc4j.JsonRpcServer;

import fi.iki.elonen.NanoHTTPD;

public class AutomatorHttpServer extends NanoHTTPD {

public AutomatorHttpServer(int port) {
super(port);
}

private static Charset charset = Charset.forName("UTF-8");
private static CharsetEncoder encoder = charset.newEncoder();
private static CharsetDecoder decoder = charset.newDecoder();

private Map<String, JsonRpcServer> router = new HashMap<String, JsonRpcServer>();

public void route(String uri, JsonRpcServer rpc) {
router.put(uri, rpc);
}

@Override
public Response serve(String uri, Method method,
Map<String, String> headers, Map<String, String> parms,
Map<String, String> files) {
Log.d(String.format("URI: %s, Method: %s, Header: %s, parms, %s, files: %s", uri, method, headers, parms, files));

if ("/stop".equals(uri)) {
stop();
return new Response("Server stopped!!!");
}
else if (router.containsKey(uri)) {
JsonRpcServer jsonRpcServer = router.get(uri);
ByteArrayInputStream is = new ByteArrayInputStream(parms.get("NanoHttpd.QUERY_STRING").getBytes());
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
jsonRpcServer.handle(is, os);
return new Response(Response.Status.OK, "application/json", new ByteArrayInputStream((os.toByteArray())));
} catch (IOException e) {
return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "Internal Server Error!!!");
}
} else
return new Response(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not Found!!!");
}
public AutomatorHttpServer(int port) {
super(port);
}

private Map<String, JsonRpcServer> router = new HashMap<String, JsonRpcServer>();

public void route(String uri, JsonRpcServer rpc) {
router.put(uri, rpc);
}

@Override
public Response serve(String uri, Method method,
Map<String, String> headers, Map<String, String> params,
Map<String, String> files) {
Log.d(String.format("URI: %s, Method: %s, Header: %s, params, %s, files: %s", uri, method, headers, params, files));

if ("/stop".equals(uri)) {
stop();
return new Response("Server stopped!!!");
} else if ("/api/screenshot".equals(uri)) {
if (Build.VERSION.SDK_INT < 17)
return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "API level less than 17.");
File f = new File(AutomatorServiceImpl.STORAGE_PATH, "screenshot.png");
float scale = 1.0f;
if (params.containsKey("scale")) {
try {
scale = Float.parseFloat(params.get("scale"));
} catch (NumberFormatException e) {
}
}
int quality = 100;
if (params.containsKey("quality")) {
try {
quality = Integer.parseInt(params.get("quality"));
} catch (NumberFormatException e) {
}
}
UiDevice.getInstance().takeScreenshot(f, scale, quality);
try {
return new Response(Response.Status.OK, "image/png", new FileInputStream(f));
} catch (FileNotFoundException e) {
Log.e(e.getMessage());
return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "Internal Server Error!!!");
}
} else if (router.containsKey(uri)) {
JsonRpcServer jsonRpcServer = router.get(uri);
ByteArrayInputStream is = new ByteArrayInputStream(params.get("NanoHttpd.QUERY_STRING").getBytes());
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
jsonRpcServer.handle(is, os);
return new Response(Response.Status.OK, "application/json", new ByteArrayInputStream(os.toByteArray()));
} catch (IOException e) {
return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "Internal Server Error!!!");
}
} else
return new Response(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not Found!!!");
}

}
47 changes: 39 additions & 8 deletions src/com/github/uiautomatorstub/AutomatorServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import android.os.Build;
import android.os.Environment;
import android.os.RemoteException;
import java.io.File;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -16,7 +17,7 @@

public class AutomatorServiceImpl implements AutomatorService {

final static String STORAGE_PATH = "/data/local/tmp/";
final public static String STORAGE_PATH = "/data/local/tmp/";
private final HashSet<String> watchers = new HashSet<String>();
private final ConcurrentHashMap<String, UiObject> uiObjects = new ConcurrentHashMap<String, UiObject>();

Expand Down Expand Up @@ -142,14 +143,44 @@ public String dumpWindowHierarchy(boolean compressed, String filename) {
File parent = new File(Environment.getDataDirectory(), "local/tmp"); // Environment.getDataDirectory() return /data/local/tmp in android 4.3 but not expected /data
if (!parent.exists())
parent.mkdirs();
boolean return_value = false;
if (filename == null || filename == "") {
filename = "dump.xml";
return_value = true;
}
File dumpFile = new File(parent, filename).getAbsoluteFile();
UiDevice.getInstance().dumpWindowHierarchy(filename);
File f = new File(STORAGE_PATH, filename); // It should be this...
if (f.exists())
return f.getAbsolutePath();
else if (dumpFile.exists())
return dumpFile.getAbsolutePath();
else
File f = new File(STORAGE_PATH, filename); // It should be this one, but in Android4.3, it is "/data/local/tmp/local/tmp"......
if (!f.exists()) f = dumpFile;
if (f.exists()) {
if (return_value) {
BufferedReader reader = null;
try {
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new FileReader(f));
char[] buffer = new char[4096];
int len = 0;
while ((len = reader.read(buffer)) != -1) {
sb.append(new String(buffer, 0, len));
}
reader.close();
reader = null;
return sb.toString();
} catch (IOException e) {
Log.e(e.toString());
} finally {
if (reader != null) {
try {
reader.close();
reader = null;
} catch (IOException e1) {
}
}
}
return null;
} else
return f.getAbsolutePath();
} else
return null;
}

Expand Down
Loading

0 comments on commit bc33fd6

Please sign in to comment.