apk(点击下载) / JsCallback.java


package com.just.agentweb;

import android.util.Log;
import android.webkit.WebView;
import com.xiaomi.mipush.sdk.Constants;
import java.lang.ref.WeakReference;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsCallback {
    private static final String CALLBACK_JS_FORMAT = "javascript:%s.callback(%d, %d %s);";
    private boolean mCouldGoOn = true;
    private int mIndex;
    private String mInjectedName;
    private int mIsPermanent;
    private WeakReference<WebView> mWebViewRef;

    public static class JsCallbackException extends Exception {
        public JsCallbackException(String str) {
            super(str);
        }
    }

    public JsCallback(WebView webView, String str, int i) {
        this.mWebViewRef = new WeakReference<>(webView);
        this.mInjectedName = str;
        this.mIndex = i;
    }

    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:7:0x0014 */
    private boolean isJavaScriptObject(Object obj) {
        if ((obj instanceof JSONObject) || (obj instanceof JSONArray)) {
            return true;
        }
        String obj2 = obj.toString();
        new JSONObject(obj2);
        try {
            new JSONArray(obj2);
            return true;
        } catch (JSONException unused) {
            return false;
        }
    }

    public void apply(Object... objArr) throws JsCallbackException {
        if (this.mWebViewRef.get() == null) {
            throw new JsCallbackException("the WebView related to the JsCallback has been recycled");
        } else if (this.mCouldGoOn) {
            StringBuilder sb = new StringBuilder();
            boolean z = false;
            for (Object obj : objArr) {
                sb.append(Constants.ACCEPT_TIME_SEPARATOR_SP);
                boolean z2 = obj instanceof String;
                boolean isJavaScriptObject = isJavaScriptObject(obj);
                if (z2 && !isJavaScriptObject) {
                    sb.append("\"");
                }
                sb.append(String.valueOf(obj));
                if (z2 && !isJavaScriptObject) {
                    sb.append("\"");
                }
            }
            String format = String.format(CALLBACK_JS_FORMAT, this.mInjectedName, Integer.valueOf(this.mIndex), Integer.valueOf(this.mIsPermanent), sb.toString());
            if (LogUtils.isDebug()) {
                Log.d("JsCallBack", format);
            }
            this.mWebViewRef.get().loadUrl(format);
            if (this.mIsPermanent > 0) {
                z = true;
            }
            this.mCouldGoOn = z;
        } else {
            throw new JsCallbackException("the JsCallback isn't permanent,cannot be called more than once");
        }
    }

    public void setPermanent(boolean z) {
        this.mIsPermanent = z ? 1 : 0;
    }
}