|
1 | 1 | package com.github.uiautomatorstub; |
2 | 2 |
|
3 | | -import java.io.ByteArrayInputStream; |
4 | | -import java.io.ByteArrayOutputStream; |
5 | | -import java.io.IOException; |
6 | | -import java.nio.charset.Charset; |
7 | | -import java.nio.charset.CharsetDecoder; |
8 | | -import java.nio.charset.CharsetEncoder; |
| 3 | +import java.io.*; |
9 | 4 | import java.util.HashMap; |
10 | 5 | import java.util.Map; |
11 | 6 |
|
| 7 | +import android.os.Build; |
| 8 | +import com.android.uiautomator.core.UiDevice; |
12 | 9 | import com.googlecode.jsonrpc4j.JsonRpcServer; |
13 | 10 |
|
14 | 11 | import fi.iki.elonen.NanoHTTPD; |
15 | 12 |
|
16 | 13 | public class AutomatorHttpServer extends NanoHTTPD { |
17 | 14 |
|
18 | | - public AutomatorHttpServer(int port) { |
19 | | - super(port); |
20 | | - } |
21 | | - |
22 | | - private static Charset charset = Charset.forName("UTF-8"); |
23 | | - private static CharsetEncoder encoder = charset.newEncoder(); |
24 | | - private static CharsetDecoder decoder = charset.newDecoder(); |
25 | | - |
26 | | - private Map<String, JsonRpcServer> router = new HashMap<String, JsonRpcServer>(); |
27 | | - |
28 | | - public void route(String uri, JsonRpcServer rpc) { |
29 | | - router.put(uri, rpc); |
30 | | - } |
31 | | - |
32 | | - @Override |
33 | | - public Response serve(String uri, Method method, |
34 | | - Map<String, String> headers, Map<String, String> parms, |
35 | | - Map<String, String> files) { |
36 | | - Log.d(String.format("URI: %s, Method: %s, Header: %s, parms, %s, files: %s", uri, method, headers, parms, files)); |
37 | | - |
38 | | - if ("/stop".equals(uri)) { |
39 | | - stop(); |
40 | | - return new Response("Server stopped!!!"); |
41 | | - } |
42 | | - else if (router.containsKey(uri)) { |
43 | | - JsonRpcServer jsonRpcServer = router.get(uri); |
44 | | - ByteArrayInputStream is = new ByteArrayInputStream(parms.get("NanoHttpd.QUERY_STRING").getBytes()); |
45 | | - ByteArrayOutputStream os = new ByteArrayOutputStream(); |
46 | | - try { |
47 | | - jsonRpcServer.handle(is, os); |
48 | | - return new Response(Response.Status.OK, "application/json", new ByteArrayInputStream((os.toByteArray()))); |
49 | | - } catch (IOException e) { |
50 | | - return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "Internal Server Error!!!"); |
51 | | - } |
52 | | - } else |
53 | | - return new Response(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not Found!!!"); |
54 | | - } |
| 15 | + public AutomatorHttpServer(int port) { |
| 16 | + super(port); |
| 17 | + } |
| 18 | + |
| 19 | + private Map<String, JsonRpcServer> router = new HashMap<String, JsonRpcServer>(); |
| 20 | + |
| 21 | + public void route(String uri, JsonRpcServer rpc) { |
| 22 | + router.put(uri, rpc); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public Response serve(String uri, Method method, |
| 27 | + Map<String, String> headers, Map<String, String> params, |
| 28 | + Map<String, String> files) { |
| 29 | + Log.d(String.format("URI: %s, Method: %s, Header: %s, params, %s, files: %s", uri, method, headers, params, files)); |
| 30 | + |
| 31 | + if ("/stop".equals(uri)) { |
| 32 | + stop(); |
| 33 | + return new Response("Server stopped!!!"); |
| 34 | + } else if ("/api/screenshot".equals(uri)) { |
| 35 | + if (Build.VERSION.SDK_INT < 17) |
| 36 | + return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "API level less than 17."); |
| 37 | + File f = new File(AutomatorServiceImpl.STORAGE_PATH, "screenshot.png"); |
| 38 | + float scale = 1.0f; |
| 39 | + if (params.containsKey("scale")) { |
| 40 | + try { |
| 41 | + scale = Float.parseFloat(params.get("scale")); |
| 42 | + } catch (NumberFormatException e) { |
| 43 | + } |
| 44 | + } |
| 45 | + int quality = 100; |
| 46 | + if (params.containsKey("quality")) { |
| 47 | + try { |
| 48 | + quality = Integer.parseInt(params.get("quality")); |
| 49 | + } catch (NumberFormatException e) { |
| 50 | + } |
| 51 | + } |
| 52 | + UiDevice.getInstance().takeScreenshot(f, scale, quality); |
| 53 | + try { |
| 54 | + return new Response(Response.Status.OK, "image/png", new FileInputStream(f)); |
| 55 | + } catch (FileNotFoundException e) { |
| 56 | + Log.e(e.getMessage()); |
| 57 | + return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "Internal Server Error!!!"); |
| 58 | + } |
| 59 | + } else if (router.containsKey(uri)) { |
| 60 | + JsonRpcServer jsonRpcServer = router.get(uri); |
| 61 | + ByteArrayInputStream is = new ByteArrayInputStream(params.get("NanoHttpd.QUERY_STRING").getBytes()); |
| 62 | + ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 63 | + try { |
| 64 | + jsonRpcServer.handle(is, os); |
| 65 | + return new Response(Response.Status.OK, "application/json", new ByteArrayInputStream(os.toByteArray())); |
| 66 | + } catch (IOException e) { |
| 67 | + return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "Internal Server Error!!!"); |
| 68 | + } |
| 69 | + } else |
| 70 | + return new Response(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not Found!!!"); |
| 71 | + } |
| 72 | + |
55 | 73 | } |
0 commit comments