CEEX v1.0.1版本的 MD5 值为:d20246587215ec3471b0d051724c6d34

以下内容为反编译后的 WVJBWebViewClient.java 源代码,内容仅作参考


package io.bhex.app.web;

import android.annotation.SuppressLint;
import android.os.Build;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import io.bhex.baselib.utils.DebugLog;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@SuppressLint({"SetJavaScriptEnabled", "NewApi"})
public class WVJBWebViewClient extends WebViewClient {
    public static final String kCustomProtocolScheme = "wvjbscheme";
    private static final String kInterface = "WVJBInterface";
    private static final String kQueueHasMessage = "__WVJB_QUEUE_MESSAGE__";
    private static final String kTag = "WVJB";
    private static boolean logging = false;
    protected WebView a;
    private WVJBHandler messageHandler;
    private Map<String, WVJBHandler> messageHandlers;
    private MyJavascriptInterface myInterface;
    private Map<String, WVJBResponseCallback> responseCallbacks;
    private ArrayList<WVJBMessage> startupMessageQueue;
    private long uniqueId;

    public interface JavascriptCallback {
        void onReceiveValue(String str);
    }

    public class MyJavascriptInterface {
        Map<String, JavascriptCallback> a;

        private MyJavascriptInterface(WVJBWebViewClient wVJBWebViewClient) {
            this.a = new HashMap();
        }

        public void addCallback(String str, JavascriptCallback javascriptCallback) {
            this.a.put(str, javascriptCallback);
        }

        @JavascriptInterface
        public void onResultForScript(String str, String str2) {
            String str3 = "onResultForScript: " + str2;
            JavascriptCallback remove = this.a.remove(str);
            if (remove != null) {
                remove.onReceiveValue(str2);
            }
        }
    }

    public interface WVJBHandler {
        void request(Object obj, WVJBResponseCallback wVJBResponseCallback);
    }

    public class WVJBMessage {
        Object a;
        String b;
        String c;
        String d;
        Object e;

        private WVJBMessage(WVJBWebViewClient wVJBWebViewClient) {
            this.a = null;
            this.b = null;
            this.c = null;
            this.d = null;
            this.e = null;
        }
    }

    public interface WVJBResponseCallback {
        void callback(Object obj);
    }

    public WVJBWebViewClient(WebView webView) {
        this(webView, null);
    }

    private WVJBMessage JSONObject2WVJBMessage(JSONObject jSONObject) {
        WVJBMessage wVJBMessage = new WVJBMessage();
        try {
            if (jSONObject.has("callbackId")) {
                wVJBMessage.b = jSONObject.getString("callbackId");
            }
            if (jSONObject.has("data")) {
                wVJBMessage.a = jSONObject.get("data");
            }
            if (jSONObject.has("handlerName")) {
                wVJBMessage.c = jSONObject.getString("handlerName");
            }
            if (jSONObject.has("responseId")) {
                wVJBMessage.d = jSONObject.getString("responseId");
            }
            if (jSONObject.has("responseData")) {
                wVJBMessage.e = jSONObject.get("responseData");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return wVJBMessage;
    }

    private void dispatchMessage(WVJBMessage wVJBMessage) {
        String replaceAll = message2JSONObject(wVJBMessage).toString().replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"").replaceAll("'", "\\\\'").replaceAll(StringUtils.LF, "\\\\\n").replaceAll(StringUtils.CR, "\\\\\r").replaceAll("\f", "\\\\\f");
        c("SEND", replaceAll);
        executeJavascript("WebViewJavascriptBridge._handleMessageFromObjC('" + replaceAll + "');");
    }

    private void flushMessageQueue() {
        executeJavascript("WebViewJavascriptBridge._fetchQueue()", new JavascriptCallback() {
            @Override
            public void onReceiveValue(String str) {
                if (str == null || str.length() == 0) {
                    return;
                }
                WVJBWebViewClient.this.processQueueMessage(str);
            }
        });
    }

    private JSONObject message2JSONObject(WVJBMessage wVJBMessage) {
        JSONObject jSONObject = new JSONObject();
        try {
            String str = wVJBMessage.b;
            if (str != null) {
                jSONObject.put("callbackId", str);
            }
            Object obj = wVJBMessage.a;
            if (obj != null) {
                jSONObject.put("data", obj);
            }
            String str2 = wVJBMessage.c;
            if (str2 != null) {
                jSONObject.put("handlerName", str2);
            }
            String str3 = wVJBMessage.d;
            if (str3 != null) {
                jSONObject.put("responseId", str3);
            }
            Object obj2 = wVJBMessage.e;
            if (obj2 != null) {
                jSONObject.put("responseData", obj2);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jSONObject;
    }

    public void processQueueMessage(String str) {
        WVJBHandler wVJBHandler;
        try {
            JSONArray jSONArray = new JSONArray(str);
            for (int i = 0; i < jSONArray.length(); i++) {
                JSONObject jSONObject = jSONArray.getJSONObject(i);
                c("RCVD", jSONObject);
                WVJBMessage JSONObject2WVJBMessage = JSONObject2WVJBMessage(jSONObject);
                String str2 = JSONObject2WVJBMessage.d;
                if (str2 != null) {
                    WVJBResponseCallback remove = this.responseCallbacks.remove(str2);
                    if (remove != null) {
                        remove.callback(JSONObject2WVJBMessage.e);
                    }
                } else {
                    final String str3 = JSONObject2WVJBMessage.b;
                    WVJBResponseCallback wVJBResponseCallback = str3 != null ? new WVJBResponseCallback() {
                        @Override
                        public void callback(Object obj) {
                            WVJBMessage wVJBMessage = new WVJBMessage();
                            wVJBMessage.d = str3;
                            wVJBMessage.e = obj;
                            WVJBWebViewClient.this.queueMessage(wVJBMessage);
                        }
                    } : null;
                    String str4 = JSONObject2WVJBMessage.c;
                    if (str4 != null) {
                        wVJBHandler = this.messageHandlers.get(str4);
                    } else {
                        wVJBHandler = this.messageHandler;
                    }
                    if (wVJBHandler != null) {
                        wVJBHandler.request(JSONObject2WVJBMessage.a, wVJBResponseCallback);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void queueMessage(WVJBMessage wVJBMessage) {
        ArrayList<WVJBMessage> arrayList = this.startupMessageQueue;
        if (arrayList != null) {
            arrayList.add(wVJBMessage);
        } else {
            dispatchMessage(wVJBMessage);
        }
    }

    private void sendData(Object obj, WVJBResponseCallback wVJBResponseCallback, String str) {
        if (obj == null && (str == null || str.length() == 0)) {
            return;
        }
        WVJBMessage wVJBMessage = new WVJBMessage();
        if (obj != null) {
            wVJBMessage.a = obj;
        }
        if (wVJBResponseCallback != null) {
            StringBuilder sb = new StringBuilder();
            sb.append("objc_cb_");
            long j = this.uniqueId + 1;
            this.uniqueId = j;
            sb.append(j);
            String sb2 = sb.toString();
            this.responseCallbacks.put(sb2, wVJBResponseCallback);
            wVJBMessage.b = sb2;
        }
        if (str != null) {
            wVJBMessage.c = str;
        }
        queueMessage(wVJBMessage);
    }

    void c(String str, Object obj) {
        if (logging) {
            String valueOf = String.valueOf(obj);
            if (valueOf.length() > 500) {
                String str2 = str + ": " + valueOf.substring(0, 500) + " [...]";
                return;
            }
            String str3 = str + ": " + valueOf;
        }
    }

    public void callHandler(String str) {
        callHandler(str, null, null);
    }

    public void enableLogging() {
        logging = true;
    }

    public void executeJavascript(String str) {
        executeJavascript(str, null);
    }

    @Override
    public void onPageFinished(WebView webView, String str) {
        try {
            InputStream open = this.a.getContext().getAssets().open("WebViewJavascriptBridge.js.txt");
            byte[] bArr = new byte[open.available()];
            open.read(bArr);
            open.close();
            executeJavascript(new String(bArr));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (this.startupMessageQueue != null) {
            for (int i = 0; i < this.startupMessageQueue.size(); i++) {
                dispatchMessage(this.startupMessageQueue.get(i));
            }
            this.startupMessageQueue = null;
        }
        super.onPageFinished(webView, str);
    }

    public void registerHandler(String str, WVJBHandler wVJBHandler) {
        if (str == null || str.length() == 0 || wVJBHandler == null) {
            return;
        }
        this.messageHandlers.put(str, wVJBHandler);
    }

    public void send(Object obj) {
        send(obj, null);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String str) {
        if (str.startsWith(kCustomProtocolScheme)) {
            if (str.indexOf(kQueueHasMessage) > 0) {
                flushMessageQueue();
                return true;
            }
            return true;
        }
        return super.shouldOverrideUrlLoading(webView, str);
    }

    public WVJBWebViewClient(WebView webView, WVJBHandler wVJBHandler) {
        this.startupMessageQueue = null;
        this.responseCallbacks = null;
        this.messageHandlers = null;
        this.uniqueId = 0L;
        this.myInterface = new MyJavascriptInterface();
        this.a = webView;
        webView.getSettings().setJavaScriptEnabled(true);
        this.a.addJavascriptInterface(this.myInterface, kInterface);
        this.responseCallbacks = new HashMap();
        this.messageHandlers = new HashMap();
        this.startupMessageQueue = new ArrayList<>();
        this.messageHandler = wVJBHandler;
        enableLogging();
    }

    public void callHandler(String str, Object obj) {
        callHandler(str, obj, null);
    }

    public void executeJavascript(String str, final JavascriptCallback javascriptCallback) {
        if (Build.VERSION.SDK_INT >= 19) {
            this.a.evaluateJavascript(str, new ValueCallback<String>(this) {
                @Override
                public void onReceiveValue(String str2) {
                    DebugLog.e("WEB", "WEB::::VALUE:::  " + str2);
                    if (javascriptCallback != null) {
                        if (str2 != null && str2.startsWith("\"") && str2.endsWith("\"")) {
                            str2 = str2.substring(1, str2.length() - 1).replaceAll("\\\\", "");
                        }
                        javascriptCallback.onReceiveValue(str2);
                    }
                }
            });
        } else if (javascriptCallback != null) {
            MyJavascriptInterface myJavascriptInterface = this.myInterface;
            StringBuilder sb = new StringBuilder();
            long j = this.uniqueId + 1;
            this.uniqueId = j;
            sb.append(j);
            sb.append("");
            myJavascriptInterface.addCallback(sb.toString(), javascriptCallback);
            WebView webView = this.a;
            webView.loadUrl("javascript:window.WVJBInterface.onResultForScript(" + this.uniqueId + "," + str + ")");
        } else {
            WebView webView2 = this.a;
            webView2.loadUrl("javascript:" + str);
        }
    }

    public void send(Object obj, WVJBResponseCallback wVJBResponseCallback) {
        sendData(obj, wVJBResponseCallback, null);
    }

    public void callHandler(String str, Object obj, WVJBResponseCallback wVJBResponseCallback) {
        sendData(obj, wVJBResponseCallback, str);
    }
}