导航菜单

页面标题

页面副标题

AdaModal v1.4.0 - BridgeWebView.java 源代码

正在查看: AdaModal v1.4.0 应用的 BridgeWebView.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.common.fine.utils.jsbridge;

import android.content.Context;
import android.os.Looper;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.common.fine.utils.ChannelUtils;
import com.common.fine.utils.twebview.ATWebView;
import com.common.fine.utils.twebview.TJavaFunc;
import com.common.fine.utils.twebview.TJsInterface;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BridgeWebView extends ATWebView implements WebViewJavascriptBridge {
    CommonCallbackBridge defaultHandler;
    Map<String, CommonCallbackBridge> mBaseBridgeList;
    private TJsInterface mJsInterface;
    Map<String, CallBackFunction> responseCallbacks;
    private List<Message> startupMessage;
    private long uniqueId;

    static void lambda$flushMessageQueue$1(String str) {
    }

    public BridgeWebView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.responseCallbacks = new HashMap();
        this.mBaseBridgeList = new HashMap();
        this.defaultHandler = new DefaultCommonCallback();
        this.startupMessage = new ArrayList();
        this.uniqueId = 0L;
        init();
    }

    public BridgeWebView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.responseCallbacks = new HashMap();
        this.mBaseBridgeList = new HashMap();
        this.defaultHandler = new DefaultCommonCallback();
        this.startupMessage = new ArrayList();
        this.uniqueId = 0L;
        init();
    }

    @Override
    public void registerJavaFunc(String str, TJavaFunc tJavaFunc) {
        this.mJsInterface.add(str, tJavaFunc);
    }

    @Override
    public void unregisterJavaFunc(String str) {
        this.mJsInterface.remove(str);
    }

    @Override
    public boolean containsFunc(String str) {
        return this.mJsInterface.contains(str);
    }

    public BridgeWebView(Context context) {
        super(context);
        this.responseCallbacks = new HashMap();
        this.mBaseBridgeList = new HashMap();
        this.defaultHandler = new DefaultCommonCallback();
        this.startupMessage = new ArrayList();
        this.uniqueId = 0L;
        init();
    }

    public List<Message> getStartupMessage() {
        return this.startupMessage;
    }

    public void setStartupMessage(List<Message> list) {
        this.startupMessage = list;
    }

    public void setDefaultHandler(CommonCallbackBridge commonCallbackBridge) {
        this.defaultHandler = commonCallbackBridge;
    }

    private void init() {
        if (ChannelUtils.isTestEnv()) {
            WebView.setWebContentsDebuggingEnabled(false);
        }
        setVerticalScrollBarEnabled(false);
        setHorizontalScrollBarEnabled(false);
        WebSettings settings = getSettings();
        settings.setAllowFileAccess(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(false);
        settings.setDisplayZoomControls(false);
        settings.setJavaScriptEnabled(true);
        settings.setGeolocationEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setLoadsImagesAutomatically(true);
        settings.setDefaultTextEncodingName("utf-8");
        settings.setAllowContentAccess(true);
        settings.setDatabaseEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setMediaPlaybackRequiresUserGesture(false);
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setMixedContentMode(0);
        enableAccessFromFileURLs();
        TJsInterface tJsInterface = new TJsInterface();
        this.mJsInterface = tJsInterface;
        addJavascriptInterface(tJsInterface, "bridge");
    }

    public void enableAccessFromFileURLs() {
        getSettings().setAllowUniversalAccessFromFileURLs(true);
    }

    void handlerReturnData(String str) {
        String functionFromReturnUrl = BridgeUtil.getFunctionFromReturnUrl(str);
        CallBackFunction callBackFunction = this.responseCallbacks.get(functionFromReturnUrl);
        String dataFromReturnUrl = BridgeUtil.getDataFromReturnUrl(str);
        if (callBackFunction != null) {
            callBackFunction.onCallBack(dataFromReturnUrl);
            this.responseCallbacks.remove(functionFromReturnUrl);
        }
    }

    @Override
    public void send(String str) {
        send(str, null);
    }

    @Override
    public void send(String str, CallBackFunction callBackFunction) {
        doSend(null, str, callBackFunction);
    }

    private void doSend(String str, String str2, CallBackFunction callBackFunction) {
        Message message = new Message();
        if (!TextUtils.isEmpty(str2)) {
            message.setData(str2);
        }
        if (callBackFunction != null) {
            StringBuilder sb = new StringBuilder();
            long j = this.uniqueId + 1;
            this.uniqueId = j;
            sb.append(j);
            sb.append("_");
            sb.append(SystemClock.currentThreadTimeMillis());
            String format = String.format("JAVA_CB_%s", sb.toString());
            this.responseCallbacks.put(format, callBackFunction);
            message.setCallbackId(format);
        }
        if (!TextUtils.isEmpty(str)) {
            message.setHandlerName(str);
        }
        queueMessage(message);
    }

    private void queueMessage(Message message) {
        List<Message> list = this.startupMessage;
        if (list != null) {
            list.add(message);
        } else {
            dispatchMessage(message);
        }
    }

    void dispatchMessage(Message message) {
        String format = String.format("javascript:WebViewJavascriptBridge._handleMessageFromNative('%s');", message.toJson().replaceAll("(\\\\)([^utrn])", "\\\\\\\\$1$2").replaceAll("(?<=[^\\\\])(\")", "\\\\\"").replaceAll("(?<=[^\\\\])(')", "\\\\'").replaceAll("%7B", URLEncoder.encode("%7B")).replaceAll("%7D", URLEncoder.encode("%7D")).replaceAll("%22", URLEncoder.encode("%22")));
        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            loadUrl(format);
        }
    }

    void flushMessageQueue() {
        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            loadUrl("javascript:WebViewJavascriptBridge._fetchQueue();", new CallBackFunction() {
                @Override
                public final void onCallBack(String str) {
                    BridgeWebView.this.lambda$flushMessageQueue$2(str);
                }
            });
        }
    }

    public void lambda$flushMessageQueue$2(String str) {
        CallBackFunction callBackFunction;
        CommonCallbackBridge commonCallbackBridge;
        try {
            List<Message> arrayList = Message.toArrayList(str);
            if (arrayList == null || arrayList.size() == 0) {
                return;
            }
            for (int i = 0; i < arrayList.size(); i++) {
                Message message = arrayList.get(i);
                String responseId = message.getResponseId();
                if (!TextUtils.isEmpty(responseId)) {
                    CallBackFunction callBackFunction2 = this.responseCallbacks.get(responseId);
                    String responseData = message.getResponseData();
                    if (callBackFunction2 != null) {
                        callBackFunction2.onCallBack(responseData);
                    }
                    this.responseCallbacks.remove(responseId);
                } else {
                    final String callbackId = message.getCallbackId();
                    if (!TextUtils.isEmpty(callbackId)) {
                        callBackFunction = new CallBackFunction() {
                            @Override
                            public final void onCallBack(String str2) {
                                BridgeWebView.this.lambda$flushMessageQueue$0(callbackId, str2);
                            }
                        };
                    } else {
                        callBackFunction = new CallBackFunction() {
                            @Override
                            public final void onCallBack(String str2) {
                                BridgeWebView.lambda$flushMessageQueue$1(str2);
                            }
                        };
                    }
                    if (!TextUtils.isEmpty(message.getHandlerName())) {
                        commonCallbackBridge = this.mBaseBridgeList.get(message.getHandlerName());
                    } else {
                        commonCallbackBridge = this.defaultHandler;
                    }
                    if (commonCallbackBridge != null) {
                        commonCallbackBridge.handler(message.getData(), callBackFunction);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void lambda$flushMessageQueue$0(String str, String str2) {
        Message message = new Message();
        message.setResponseId(str);
        message.setResponseData(str2);
        queueMessage(message);
    }

    public void loadUrl(String str, CallBackFunction callBackFunction) {
        loadUrl(str);
        this.responseCallbacks.put(BridgeUtil.parseFunctionName(str), callBackFunction);
    }

    public void register(String str, CommonCallbackBridge commonCallbackBridge) {
        if (commonCallbackBridge != null) {
            this.mBaseBridgeList.put(str, commonCallbackBridge);
        }
    }

    public Map<String, CommonCallbackBridge> getBridgeList() {
        return this.mBaseBridgeList;
    }

    public void unregister(String str) {
        if (str != null) {
            this.mBaseBridgeList.remove(str);
        }
    }

    public void callHandler(String str, String str2, CallBackFunction callBackFunction) {
        doSend(str, str2, callBackFunction);
    }
}