导航菜单

页面标题

页面副标题

Easycash v3.75.13 - BridgeWebView.java 源代码

正在查看: Easycash v3.75.13 应用的 BridgeWebView.java JAVA 源代码文件

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


package com.github.lzyzsd.jsbridge;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Looper;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.google.android.gms.common.internal.ImagesContract;
import com.google.android.gms.measurement.api.AppMeasurementSdk;
import com.lingyue.supertoolkit.customtools.CrashReporter;
import com.lingyue.supertoolkit.customtools.Logger;
import com.lingyue.supertoolkit.data.DataExchangeManager;
import com.lingyue.supertoolkit.data.GsonUtil;
import com.sensorsdata.analytics.android.sdk.jsbridge.JSHookAop;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.sentry.ITransaction;
import io.sentry.Sentry;
import io.sentry.SpanStatus;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SuppressLint({"SetJavaScriptEnabled"})
public class BridgeWebView extends WebView implements WebViewJavascriptBridge {
    public static final String toLoadJs = "WebViewJavascriptBridge.js";
    private final String TAG;
    BridgeHandler defaultHandler;
    private volatile boolean enableJsonBridge;
    Set<ITransaction> iTransactions;
    Map<String, BridgeHandler> messageHandlers;
    Map<String, CallBackFunction> responseCallbacks;
    Map<String, ITransaction> sentryResponseCallBack;
    private List<Message> startupMessage;
    private long uniqueId;

    public BridgeWebView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.TAG = "BridgeWebView";
        this.responseCallbacks = new HashMap();
        this.messageHandlers = new HashMap();
        this.defaultHandler = new DefaultHandler();
        this.startupMessage = new ArrayList();
        this.uniqueId = 0L;
        this.sentryResponseCallBack = new HashMap();
        this.iTransactions = new HashSet();
        init();
    }

    private void doSend(String str, String str2, CallBackFunction callBackFunction) {
        Message message = new Message();
        if (!TextUtils.isEmpty(str2)) {
            if (DataExchangeManager.c().d("JsBridgeJsonParams") || this.enableJsonBridge) {
                message.isJsonData = true;
                message.setData(GsonUtil.b().t(str2));
            } else {
                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);
            try {
                if (this.sentryResponseCallBack.get(format) == null) {
                    ITransaction C = Sentry.C("JSFuncCall", "op_h5");
                    C.a(AppMeasurementSdk.ConditionalUserProperty.NAME, str);
                    C.a(ImagesContract.URL, getUrl() + "");
                    C.a("needCallBack", "true");
                    this.sentryResponseCallBack.put(format, C);
                }
            } catch (Exception e) {
                CrashReporter.a(e);
            }
            message.setCallbackId(format);
        }
        if (!TextUtils.isEmpty(str)) {
            message.setHandlerName(str);
        }
        queueMessage(message);
    }

    public void lambda$dispatchMessage$1(String str) {
        if (!DataExchangeManager.c().d("jsBridgeEval")) {
            JSHookAop.loadUrl(this, str);
            loadUrl(str);
            return;
        }
        evaluateJavascript("try { " + str.replace(BridgeUtil.JAVASCRIPT_STR, "") + " } catch (exception) { console?.error(exception)} ;", new ValueCallback() {
            @Override
            public final void onReceiveValue(Object obj) {
                BridgeWebView.lambda$evalJs$2((String) obj);
            }
        });
    }

    private void init() {
        setVerticalScrollBarEnabled(false);
        setHorizontalScrollBarEnabled(false);
        getSettings().setJavaScriptEnabled(true);
        setWebViewClient(generateBridgeWebViewClient());
        registerHandler("enableJsonBridge", new BridgeHandler() {
            @Override
            public final void handler(String str, CallBackFunction callBackFunction) {
                BridgeWebView.this.lambda$init$0(str, callBackFunction);
            }
        });
    }

    public static void lambda$evalJs$2(String str) {
        Logger.c().b("evaluateJavascriptResult " + str);
    }

    public void lambda$init$0(String str, CallBackFunction callBackFunction) {
        this.enableJsonBridge = true;
        callBackFunction.onCallBack("true");
    }

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

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

    @Override
    public void destroy() {
        Iterator<ITransaction> it = this.iTransactions.iterator();
        while (it.hasNext()) {
            it.next().o(SpanStatus.INTERNAL_ERROR);
        }
        Iterator<ITransaction> it2 = this.sentryResponseCallBack.values().iterator();
        while (it2.hasNext()) {
            it2.next().o(SpanStatus.INTERNAL_ERROR);
        }
        super.destroy();
    }

    void dispatchMessage(Message message) {
        final String format = String.format("javascript:WebViewJavascriptBridge._handleMessageFromNative('%s');", message.toJson().replaceAll("(\\\\)([^utrn])", "\\\\\\\\$1$2").replaceAll("(?<=[^\\\\])(\")", "\\\\\""));
        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            lambda$dispatchMessage$1(format);
        } else {
            post(new Runnable() {
                @Override
                public final void run() {
                    BridgeWebView.this.lambda$dispatchMessage$1(format);
                }
            });
        }
    }

    void flushMessageQueue() {
        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            loadUrl("javascript:WebViewJavascriptBridge._fetchQueue();", new AnonymousClass1());
        }
    }

    protected BridgeWebViewClient generateBridgeWebViewClient() {
        return new BridgeWebViewClient(this);
    }

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

    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);
        }
    }

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

    public void registerHandler(String str, BridgeHandler bridgeHandler) {
        if (bridgeHandler != null) {
            this.messageHandlers.put(str, bridgeHandler);
        }
    }

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

    public void setDefaultHandler(BridgeHandler bridgeHandler) {
        this.defaultHandler = bridgeHandler;
    }

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

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

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

    public BridgeWebView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.TAG = "BridgeWebView";
        this.responseCallbacks = new HashMap();
        this.messageHandlers = new HashMap();
        this.defaultHandler = new DefaultHandler();
        this.startupMessage = new ArrayList();
        this.uniqueId = 0L;
        this.sentryResponseCallBack = new HashMap();
        this.iTransactions = new HashSet();
        init();
    }

    class AnonymousClass1 implements CallBackFunction {
        AnonymousClass1() {
        }

        public void lambda$onCallBack$1(Disposable disposable, ITransaction iTransaction, String str, String str2) {
            disposable.dispose();
            StringBuilder sb = new StringBuilder();
            sb.append((str2 + "").length());
            sb.append("");
            iTransaction.a("resultSize", sb.toString());
            BridgeWebView.this.iTransactions.remove(iTransaction);
            iTransaction.o(SpanStatus.OK);
            Message message = new Message();
            message.setResponseId(str);
            if (DataExchangeManager.c().d("JsBridgeJsonParams") || BridgeWebView.this.enableJsonBridge) {
                message.isJsonData = true;
                message.setResponseData(GsonUtil.b().t(str2));
            } else {
                message.setResponseData(str2);
            }
            BridgeWebView.this.queueMessage(message);
        }

        @Override
        public void onCallBack(String str) {
            CallBackFunction callBackFunction;
            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 handlerName = message.getHandlerName();
                    String responseId = message.getResponseId();
                    if (TextUtils.isEmpty(responseId)) {
                        final ITransaction C = Sentry.C("JsBridgeCall", "op_h5");
                        BridgeWebView.this.iTransactions.add(C);
                        C.a(AppMeasurementSdk.ConditionalUserProperty.NAME, handlerName + "");
                        C.a(ImagesContract.URL, BridgeWebView.this.getUrl() + "");
                        final String callbackId = message.getCallbackId();
                        boolean isEmpty = TextUtils.isEmpty(callbackId) ^ true;
                        C.a("needCallBack", isEmpty ? "true" : "false");
                        if (isEmpty) {
                            final Disposable f0 = Flowable.u0(30L, TimeUnit.SECONDS).f0(new Consumer() {
                                public final void accept(Object obj) {
                                    C.a("timeout", "true");
                                }
                            });
                            callBackFunction = new CallBackFunction() {
                                @Override
                                public final void onCallBack(String str2) {
                                    BridgeWebView.AnonymousClass1.this.lambda$onCallBack$1(f0, C, callbackId, str2);
                                }
                            };
                        } else {
                            callBackFunction = new CallBackFunction() {
                                @Override
                                public final void onCallBack(String str2) {
                                    BridgeWebView.AnonymousClass1.lambda$onCallBack$2(str2);
                                }
                            };
                        }
                        String data = message.getData();
                        C.a("params", data + "");
                        BridgeHandler bridgeHandler = !TextUtils.isEmpty(handlerName) ? BridgeWebView.this.messageHandlers.get(handlerName) : BridgeWebView.this.defaultHandler;
                        if (bridgeHandler != null) {
                            try {
                                bridgeHandler.handler(data, callBackFunction);
                            } catch (Exception e) {
                                C.n(e);
                                C.o(SpanStatus.INTERNAL_ERROR);
                                throw e;
                            }
                        }
                        if (!isEmpty) {
                            C.o(SpanStatus.OK);
                        }
                    } else {
                        CallBackFunction callBackFunction2 = BridgeWebView.this.responseCallbacks.get(responseId);
                        ITransaction remove = BridgeWebView.this.sentryResponseCallBack.remove(responseId);
                        String responseData = message.getResponseData();
                        if (callBackFunction2 != null) {
                            callBackFunction2.onCallBack(responseData);
                        }
                        if (remove != null) {
                            remove.o(SpanStatus.OK);
                        }
                        BridgeWebView.this.responseCallbacks.remove(responseId);
                    }
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

        public static void lambda$onCallBack$2(String str) {
        }
    }

    public BridgeWebView(Context context) {
        super(context);
        this.TAG = "BridgeWebView";
        this.responseCallbacks = new HashMap();
        this.messageHandlers = new HashMap();
        this.defaultHandler = new DefaultHandler();
        this.startupMessage = new ArrayList();
        this.uniqueId = 0L;
        this.sentryResponseCallBack = new HashMap();
        this.iTransactions = new HashSet();
        init();
    }
}