浏览代码

[tmp] wip backup

isundil 8 年之前
父节点
当前提交
b9d5e34d0b

+ 4 - 2
app/src/main/java/com/knacki/mimou/activity/MainActivity.java

@@ -156,8 +156,10 @@ public class MainActivity extends AppCompatActivity {
         uiThread(new Runnable() {
             @Override
             public void run() {
-                if (web != null)
-                    web.loadUrl("javascript:" +js);
+                if (web != null) {
+                    log.severe(js);
+                    web.loadUrl("javascript:" + js);
+                }
             }
         });
     }

+ 41 - 4
app/src/main/java/com/knacki/mimou/bridge/JavascriptFunction.java

@@ -19,15 +19,18 @@ public class JavascriptFunction {
         fncStr = new StringBuilder(fncName).append('(');
     }
 
-    public JavascriptFunction(number callbackId) {
-        fncStr = new StringBuilder("CALLBACK.onResult").append('(').addArgument(callbackId);
+    public JavascriptFunction(long callbackId) {
+        fncStr = new StringBuilder("__CALLBACK.onResult").append('(');
+        addArgument(callbackId);
     }
 
     private static String escapeStr(String s) {
-        return s.replaceAll("\"", "\\\"");
+        return s.replaceAll("\"", "\\\\\"");
     }
 
     public JavascriptFunction addArgument(String arg) {
+        if (arg == null)
+            return addNullArgument();
         if (!immutable) {
             if (!firstArg)
                 fncStr.append(',');
@@ -37,7 +40,31 @@ public class JavascriptFunction {
         return this;
     }
 
-    public JavascriptFunction addArgument(JSONOBJECTIFIABLE arg) {
+    public JavascriptFunction addNullArgument() {
+        if (!immutable) {
+            if (!firstArg)
+                fncStr.append(',');
+            fncStr.append("null");
+            firstArg = false;
+        }
+        return this;
+    }
+
+    public JavascriptFunction addJSONArgument(String s) {
+        if (s == null)
+            return addNullArgument();
+        if (!immutable) {
+            if (!firstArg)
+                fncStr.append(',');
+            fncStr.append(s);
+            firstArg = false;
+        }
+        return this;
+    }
+
+    public JavascriptFunction addArgument(JSONObjifiable arg) {
+        if (arg == null)
+            return addNullArgument();
         if (!immutable) {
             if (!firstArg)
                 fncStr.append(',');
@@ -57,6 +84,16 @@ public class JavascriptFunction {
         return this;
     }
 
+    public JavascriptFunction addArgument(long arg) {
+        if (!immutable) {
+            if (!firstArg)
+                fncStr.append(',');
+            fncStr.append(Long.toString(arg));
+            firstArg = false;
+        }
+        return this;
+    }
+
     public String toString() {
         if (!immutable) {
             fncStr.append(')');

+ 14 - 15
app/src/main/java/com/knacki/mimou/bridge/JsInterface.java

@@ -16,6 +16,9 @@ import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -33,7 +36,6 @@ public class JsInterface {
     public static final String TOGGLE_MENU = "toggleMenu";
     public static final String ON_APPLICATION_PAUSED = "onApplicationPaused";
     public static final String ON_APPLICATION_RESUMED = "onApplicationResumed";
-    public static final String ON_SMS_PERMISSION_UPDATED = "onSmsPermissionUpdated";
     public static final String SET_CHANNEL_FAVORITE = "setChannelFavorite";
 
     MainActivity mainActivity;
@@ -49,19 +51,11 @@ public class JsInterface {
     }
 
     @JavascriptInterface
-<<<<<<< HEAD
-    public void readSmsPermission(final number callbackId) {
+    public void readSmsPermission(final long callbackId) {
         mainActivity.requestSmsPermission(new TypedCallback<Boolean>() {
             @Override
             public void onResult(Boolean result) {
                 mainActivity.runJavascript(new JavascriptFunction(callbackId).addArgument(result));
-=======
-    public void readSmsPermission() {
-        mainActivity.requestSmsPermission(new TypedCallback<Boolean>() {
-            @Override
-            public void onResult(Boolean result) {
-                mainActivity.runJavascript(new JavascriptFunction(JsInterface.ON_SMS_PERMISSION_UPDATED).addArgument(result));
->>>>>>> db2b417f1ecb71380c1577978914febf20fcde40
             }
         });
     }
@@ -98,23 +92,28 @@ public class JsInterface {
     }
 
     @JavascriptInterface
-    public void TCPConnect(number sockId, String address, number port) {
+    public void TCPConnect(long sockId, String address, short port) {
         // FIXME
     }
 
     @JavascriptInterface
-    public void TCPWrite(number sockId, String msg) {
+    public void TCPWrite(long sockId, String msg) {
         // FIXME
     }
 
     @JavascriptInterface
-    public void TCPClose(number sockId) {
+    public void TCPClose(long sockId) {
         // FIXME
     }
 
     @JavascriptInterface
-    public void sendHttpRequest(String method, String url, number timeo, String expectedResponseType, String postPayload, number callbackId) {
-        // FIXME
+    public void sendHttpRequest(String method, String url, int timeo, String expectedResponseType, String postPayload, final long callbackId) {
+        new XHRController(method, url).setTimeo(timeo).setResponseType(expectedResponseType).makeRequest(postPayload, new TypedCallback<XHRController.Response>() {
+            @Override
+            public void onResult(XHRController.Response result) {
+                mainActivity.runJavascript(new JavascriptFunction(callbackId).addArgument(result.statusCode).addArgument(result.statusText).addArgument(result.response));
+            }
+        });
     }
 
     @JavascriptInterface

+ 141 - 0
app/src/main/java/com/knacki/mimou/bridge/XHRController.java

@@ -0,0 +1,141 @@
+package com.knacki.mimou.bridge;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.CharBuffer;
+import java.nio.charset.StandardCharsets;
+import java.security.InvalidParameterException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Created by thibal on 1/12/18.
+ */
+public class XHRController {
+    public final Method method;
+    public final String url;
+    protected int timeo = 5;
+    protected String responseType;
+
+    private static final Logger log = Logger.getLogger(XHRController.class.getName());
+
+    public XHRController(String method, String url) throws InvalidParameterException {
+        this.method = parseMethod(method);
+        this.url = url;
+    }
+
+    public XHRController setTimeo(int timeo) {
+        this.timeo = timeo;
+        return this;
+    }
+
+    public XHRController setResponseType(String respType) {
+        responseType = respType;
+        return this;
+    }
+
+    public void makeRequest(String payload, JsInterface.TypedCallback<Response> callback) {
+        HttpURLConnection con = null;
+
+        Logger.getAnonymousLogger().severe("REQUEST " +this.url);
+        try {
+            con = (HttpURLConnection) (new URL(this.url).openConnection());
+            con.setDoOutput(true);
+            con.setRequestMethod(this.method.str);
+            if (payload != null)
+                con.getOutputStream().write(payload.getBytes());
+            InputStream in = new BufferedInputStream(con.getInputStream());
+            callback.onResult(readStream(con, in));
+        } catch (IOException e) {
+            log.log(Level.WARNING, "Cannot " +this.method.str +" " +this.url +": " +e.getMessage(), e);
+            callback.onResult(new Response(e));
+        } finally {
+            if (con != null)
+                con.disconnect();
+        }
+    }
+
+    private Response readStream(HttpURLConnection con, InputStream stream) throws IOException {
+        final int BUFFER_LEN = 10;
+        int readCount;
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+
+        do {
+            byte[] byteRead = new byte[BUFFER_LEN];
+            readCount = stream.read(byteRead);
+            if (readCount > 0)
+                buffer.write(byteRead, 0, readCount);
+        } while (readCount == BUFFER_LEN);
+        return new Response((short) con.getResponseCode(), con.getResponseMessage(), buffer.toString("UTF-8"));
+    }
+
+    public static Method parseMethod(String s) {
+        s = s.toUpperCase();
+        if (Method.GET.equals(s)) {
+            return Method.GET;
+        }
+        if (Method.POST.equals(s)) {
+            return Method.POST;
+        }
+        if (Method.PUT.equals(s)) {
+            return Method.PUT;
+        }
+        if (Method.DELETE.equals(s)) {
+            return Method.DELETE;
+        }
+        if (Method.HEAD.equals(s)) {
+            return Method.HEAD;
+        }
+        if (Method.OPTIONS.equals(s)) {
+            return Method.OPTIONS;
+        }
+        if (Method.TRACE.equals(s)) {
+            return Method.TRACE;
+        }
+        throw new InvalidParameterException("unknown method: " +s);
+    }
+
+    public enum Method {
+        GET("GET"),
+        POST("POST"),
+        PUT("PUT"),
+        DELETE("DELETE"),
+        HEAD("HEAD"),
+        OPTIONS("OPTIONS"),
+        TRACE("TRACE");
+
+        public final String str;
+
+        Method(String s) {
+            this.str = s;
+        }
+
+        public boolean equals(String s) {
+            return this.str.equals(s);
+        }
+    }
+
+    public class Response {
+        public final short statusCode;
+        public final String statusText;
+        public final String response;
+
+        Response(Exception e) {
+            statusCode = 0;
+            statusText = e.getMessage();
+            response = e.getMessage();
+        }
+
+        Response(short statusCode, String statusText, String response) {
+            this.statusCode = statusCode;
+            this.statusText = statusText;
+            this.response = response;
+        }
+    }
+}