-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
541 additions
and
222 deletions.
There are no files selected for viewing
104 changes: 61 additions & 43 deletions
104
src/com/github/uiautomatorstub/AutomatorHttpServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!!!"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.