小红书.apk(点击下载) / SmCaptchaWebView.java


package com.ishumei.sdk.captcha;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import cn.jpush.android.local.JPushConstants;
import com.alipay.sdk.util.f;
import com.ishumei.sdk.captcha.O000O00000oO.O000O00000OoO;
import com.ishumei.sdk.captcha.O000O00000oO.O000O00000oO;
import com.ishumei.sdk.captcha.O000O00000oO.O000O0000O0oO;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import org.json.JSONException;
import org.json.JSONObject;

public class SmCaptchaWebView extends WebView {
    private static final int MESSAGE_TIMEOUT = 1;
    public static final String MODE_ICON_SELECT = "icon_select";
    public static final String MODE_SELECT = "select";
    public static final String MODE_SEQ_SELECT = "seq_select";
    public static final String MODE_SLIDE = "slide";
    public static final String MODE_SPATIAL_SELECT = "spatial_select";
    private static final String PROTO_CONTENT = "content";
    public static int SMCAPTCHA_SDK_NOLISTENER = 1004;
    public static int SMCAPTCHA_SDK_OPTION_EMPTY = 1001;
    public static int SMCAPTCHA_SDK_OPTION_NOAPPID = 1003;
    public static int SMCAPTCHA_SDK_OPTION_NOORG = 1002;
    public static int SMCAPTCHA_SUCCESS = 0;
    public static int SMCAPTCHA_WV_NETWORK_ERROR = 1005;
    public static int SMCAPTCHA_WV_RESULT_ERROR = 1006;
    private static final String SM_CA_HTML = "https://castatic.fengkongcloud.com/pr/v1.0.4/index.html";
    private static final int SM_CA_LOAD_HTML_RETRY = 2;
    private static final int SM_CA_LOAD_HTML_TIMEOUT = 10000;
    public static final String SM_CA_OS = "android";
    public static final String SM_CA_SDK_VERSION = "1.4.0";
    public static final String SM_R_VERSION = "1.0.4";
    private static final String TAG = "Smlog";
    private String compatHijackUrl;
    private SimpleResultListener listener;
    private SmOption option;
    private int retry = 0;

    public class O0000O000000oO extends WebViewClient {
        private final Timer O0000O000000oO = new Timer();
        private final Handler O000O00000OoO = new HandlerC0407O0000O000000oO();
        public final /* synthetic */ SmOption O000O00000o0O;

        /* renamed from: com.ishumei.sdk.captcha.SmCaptchaWebView$O0000O000000oO$O0000O000000oO  reason: collision with other inner class name */
        public class HandlerC0407O0000O000000oO extends Handler {
            public HandlerC0407O0000O000000oO() {
            }

            public void handleMessage(Message message) {
                super.handleMessage(message);
                if (message.what == 1) {
                    SmCaptchaWebView.this.reportErrorMsg("MESSAGE_TIMEOUT");
                    O0000O000000oO.this.O0000O000000oO();
                }
            }
        }

        public class O000O00000OoO extends TimerTask {
            public O000O00000OoO() {
            }

            public void run() {
                Message message = new Message();
                message.what = 1;
                O0000O000000oO.this.O000O00000OoO.sendMessage(message);
                O0000O000000oO.this.O000O00000OoO();
            }
        }

        public O0000O000000oO(SmOption smOption) {
            this.O000O00000o0O = smOption;
        }

        private void O0000O000000oO(TimerTask timerTask, long j14, long j15) {
            synchronized (this.O0000O000000oO) {
                try {
                    this.O0000O000000oO.schedule(timerTask, j14, j15);
                } catch (Exception e14) {
                    e14.getMessage();
                }
            }
        }

        /* access modifiers changed from: private */
        /* access modifiers changed from: public */
        private void O000O00000OoO() {
            synchronized (this.O0000O000000oO) {
                try {
                    this.O0000O000000oO.cancel();
                } catch (Exception e14) {
                    e14.getMessage();
                }
            }
        }

        public void O0000O000000oO() {
            if (SmCaptchaWebView.this.retry < this.O000O00000o0O.getRetry()) {
                SmCaptchaWebView.this.loadUrl("about:blank");
                SmCaptchaWebView.this.reloadCaptcha();
                return;
            }
            SmCaptchaWebView.this.notifyWebLoadError((SmCaptchaWebView) O0000O000000oO.O0000O000000oO(this.O000O00000o0O.getCaptchaUuid(), SmCaptchaWebView.SMCAPTCHA_WV_NETWORK_ERROR));
        }

        public void onPageFinished(WebView webView, String str) {
            super.onPageFinished(webView, str);
            com.ishumei.sdk.captcha.O000O00000oO.O000O00000OoO.O0000O000000oO().O0000O000000oO(new com.ishumei.sdk.captcha.O000O00000oO.O0000O000000oO("webviewInitSuccess", System.currentTimeMillis(), this.O000O00000o0O.getCaptchaUuid(), this.O000O00000o0O.getOrganization(), this.O000O00000o0O.getMode()));
            O000O00000OoO();
        }

        public void onPageStarted(WebView webView, String str, Bitmap bitmap) {
            if (SmCaptchaWebView.this.compatHijackUrl == null || !SmCaptchaWebView.this.compatHijackUrl.equals(str)) {
                SmCaptchaWebView.this.compatHijackUrl = null;
                super.onPageStarted(webView, str, bitmap);
            }
            O0000O000000oO(new O000O00000OoO(), (long) this.O000O00000o0O.getTimeout(), 1);
        }

        public void onReceivedError(WebView webView, int i10, String str, String str2) {
            super.onReceivedError(webView, i10, str, str2);
            if (this.O000O00000o0O.getCaptchaHtml().equals(str2)) {
                O0000O000000oO();
            }
            SmCaptchaWebView smCaptchaWebView = SmCaptchaWebView.this;
            smCaptchaWebView.reportErrorMsg("onReceivedError: " + i10 + "," + str + "," + str2);
        }

        public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
            super.onReceivedError(webView, webResourceRequest, webResourceError);
            if (Build.VERSION.SDK_INT >= 21) {
                if (this.O000O00000o0O.getCaptchaHtml().equals(webResourceRequest.getUrl().toString())) {
                    O0000O000000oO();
                }
            }
            SmCaptchaWebView.this.reportErrorMsg((SmCaptchaWebView) "onReceivedError", (String) webResourceRequest, (WebResourceRequest) webResourceError);
        }

        public void onReceivedHttpError(WebView webView, WebResourceRequest webResourceRequest, WebResourceResponse webResourceResponse) {
            super.onReceivedHttpError(webView, webResourceRequest, webResourceResponse);
            if (Build.VERSION.SDK_INT >= 21) {
                if (this.O000O00000o0O.getCaptchaHtml().equals(webResourceRequest.getUrl().toString())) {
                    O0000O000000oO();
                }
            }
            SmCaptchaWebView.this.reportErrorMsg((SmCaptchaWebView) "onReceivedHttpError", (String) webResourceRequest, (WebResourceRequest) webResourceResponse);
        }

        public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
            sslErrorHandler.cancel();
        }

        @Override // android.webkit.WebViewClient
        public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest webResourceRequest) {
            if (Build.VERSION.SDK_INT < 21 || !SmCaptchaWebView.this.dispatchPersonalSchemeUrl(webView, webResourceRequest.getUrl())) {
                return super.shouldOverrideUrlLoading(webView, webResourceRequest);
            }
            return true;
        }

        @Override // android.webkit.WebViewClient
        public boolean shouldOverrideUrlLoading(WebView webView, String str) {
            if (!SmCaptchaWebView.this.dispatchPersonalSchemeUrl(webView, Uri.parse(str))) {
                return super.shouldOverrideUrlLoading(webView, str);
            }
            SmCaptchaWebView.this.compatHijackUrl = str;
            try {
                SmCaptchaWebView.this.stopLoading();
                return true;
            } catch (Throwable unused) {
                return true;
            }
        }
    }

    @Deprecated
    public interface ResultListener {
        void onClose();

        void onError(int i10);

        void onReady();

        void onSuccess(CharSequence charSequence, boolean z14);
    }

    public static class SmOption {
        private String appId;
        private String captchaHtml = SmCaptchaWebView.SM_CA_HTML;
        private String captchaUuid;
        private String cdnHost;
        private String channel;
        private String deviceId;
        private Map<String, String> extData;
        private Map<String, Object> extOption;
        private String host;
        private boolean https = true;
        private String mode = SmCaptchaWebView.MODE_SLIDE;
        private String organization;
        private int retry = 2;
        private int timeout = 10000;
        private String tipMessage;

        private String genCustomHtml() {
            boolean isEmpty = TextUtils.isEmpty(this.cdnHost);
            String str = SmCaptchaWebView.SM_CA_HTML;
            if (!isEmpty) {
                try {
                    str = str.replace(new URL(this.captchaHtml).getHost(), this.cdnHost);
                } catch (Throwable unused) {
                }
            }
            return isHttps() ? str.replaceFirst(JPushConstants.HTTP_PRE, JPushConstants.HTTPS_PRE) : str.replaceFirst(JPushConstants.HTTPS_PRE, JPushConstants.HTTP_PRE);
        }

        public String getAppId() {
            return this.appId;
        }

        public String getCaptchaHtml() {
            return !TextUtils.equals(this.captchaHtml, SmCaptchaWebView.SM_CA_HTML) ? this.captchaHtml : genCustomHtml();
        }

        public String getCaptchaUuid() {
            return this.captchaUuid;
        }

        public String getChannel() {
            return this.channel;
        }

        public String getDeviceId() {
            return this.deviceId;
        }

        public Map<String, String> getExtData() {
            return this.extData;
        }

        public Map<String, Object> getExtOption() {
            return this.extOption;
        }

        public String getHost() {
            return this.host;
        }

        public String getMode() {
            return this.mode;
        }

        public String getOrganization() {
            return this.organization;
        }

        public int getRetry() {
            return this.retry;
        }

        public int getTimeout() {
            return this.timeout;
        }

        public String getTipMessage() {
            return this.tipMessage;
        }

        public boolean isHttps() {
            return (TextUtils.isEmpty(this.captchaHtml) || TextUtils.equals(this.captchaHtml, SmCaptchaWebView.SM_CA_HTML)) ? this.https : this.captchaHtml.startsWith(JPushConstants.HTTPS_PRE);
        }

        public void setAppId(String str) {
            this.appId = str;
        }

        public void setCaptchaHtml(String str) {
            this.captchaHtml = str;
        }

        public void setCaptchaUuid(String str) {
            this.captchaUuid = str;
        }

        public void setCdnHost(String str) {
            this.cdnHost = str;
        }

        public void setChannel(String str) {
            this.channel = str;
        }

        public void setDeviceId(String str) {
            this.deviceId = str;
        }

        public void setExtData(Map<String, String> map) {
            this.extData = map;
        }

        public void setExtOption(Map<String, Object> map) {
            this.extOption = map;
        }

        public void setHost(String str) {
            this.host = str;
        }

        public void setHttps(boolean z14) {
            this.https = z14;
        }

        public void setMode(String str) {
            this.mode = str;
        }

        public void setOrganization(String str) {
            this.organization = str;
        }

        public void setRetry(int i10) {
            this.retry = i10;
        }

        public void setTimeout(int i10) {
            this.timeout = i10;
        }

        public void setTipMessage(String str) {
            this.tipMessage = str;
        }
    }

    public SmCaptchaWebView(Context context) {
        super(context);
    }

    public SmCaptchaWebView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public SmCaptchaWebView(Context context, AttributeSet attributeSet, int i10) {
        super(context, attributeSet, i10);
    }

    @TargetApi(21)
    public SmCaptchaWebView(Context context, AttributeSet attributeSet, int i10, int i14) {
        super(context, attributeSet, i10, i14);
    }

    public SmCaptchaWebView(Context context, AttributeSet attributeSet, int i10, boolean z14) {
        super(context, attributeSet, i10, z14);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private boolean dispatchPersonalSchemeUrl(WebView webView, Uri uri) {
        if (!TextUtils.equals(uri.getScheme(), "shumei")) {
            return false;
        }
        if (TextUtils.equals(uri.getAuthority(), "onresult")) {
            try {
                JSONObject jSONObject = new JSONObject(uri.getQueryParameter("data"));
                if (jSONObject.has("content")) {
                    handle104Version(jSONObject);
                    return true;
                }
                handle103Version(jSONObject);
                return true;
            } catch (JSONException e14) {
                reportErrorMsg("shumei://onresult:JSONException:" + SMCAPTCHA_WV_RESULT_ERROR + "," + e14);
                e14.getMessage();
                notifyWebLoadError(O0000O000000oO.O0000O000000oO(this.option.getCaptchaUuid(), SMCAPTCHA_WV_RESULT_ERROR));
                return true;
            }
        } else if (!TextUtils.equals(uri.getAuthority(), "requestnativeparams")) {
            return true;
        } else {
            webView.loadUrl(getInjectJSdeliverNativeParams());
            return true;
        }
    }

    private O000O0000O0oO generateReportBean(String str) {
        return new O000O0000O0oO(this.option.organization, this.option.appId, SM_CA_SDK_VERSION, str, Build.VERSION.RELEASE, Build.MODEL, O000O00000o0O.O0000O000000oO(getContext()));
    }

    private String getInjectJSdeliverNativeParams() {
        HashMap hashMap = new HashMap();
        try {
            hashMap.put("organization", this.option.getOrganization());
            hashMap.put("appId", this.option.getAppId());
            hashMap.put("channel", this.option.getChannel());
            hashMap.put("mode", this.option.getMode());
            hashMap.put("https", Boolean.valueOf(this.option.isHttps()));
            if (this.option.getExtOption() != null) {
                for (Map.Entry<String, Object> entry : this.option.getExtOption().entrySet()) {
                    hashMap.put(entry.getKey(), entry.getValue());
                }
            }
            if (!TextUtils.isEmpty(this.option.getHost())) {
                hashMap.put("domains", Collections.singletonList(this.option.getHost()));
            }
            HashMap hashMap2 = new HashMap();
            if (this.option.getExtData() != null) {
                for (Map.Entry<String, String> entry2 : this.option.getExtData().entrySet()) {
                    hashMap2.put(entry2.getKey(), entry2.getValue());
                }
            }
            if (O000O00000OoO.O000O00000OoO(this.option.getDeviceId())) {
                hashMap2.put("deviceId", this.option.getDeviceId());
            }
            hashMap2.put("os", "android");
            hashMap2.put("sdkver", SM_CA_SDK_VERSION);
            hashMap.put("captchaUuid", this.option.getCaptchaUuid());
            hashMap.put("data", hashMap2);
            if (!TextUtils.isEmpty(this.option.getTipMessage())) {
                HashMap hashMap3 = new HashMap();
                hashMap3.put("sliderPlaceholder", this.option.getTipMessage());
                hashMap.put("tipsMessage", hashMap3);
            }
            String replaceAll = O000O00000o0O.O0000O000000oO((Map<?, ?>) hashMap).toString().replaceAll("'", "\\\\'");
            return "javascript:deliverNativeParams('" + replaceAll + "')";
        } catch (Exception unused) {
            return "";
        }
    }

    private void handle103Version(JSONObject jSONObject) {
        String string = jSONObject.getString("method");
        if (O000O00000OoO.O0000O000000oO(string, "onError")) {
            if (jSONObject.has("detail")) {
                jSONObject = jSONObject.getJSONObject("detail");
            }
            int optInt = jSONObject.optInt("code", SMCAPTCHA_WV_RESULT_ERROR);
            reportErrorMsg("shumei://onresult.onError;code=" + optInt);
            notifyWebLoadError(optInt);
        } else if (O000O00000OoO.O0000O000000oO(string, "onSuccess")) {
            notifySuccess(jSONObject.getString("rid"), jSONObject.getBoolean("pass"));
        } else if (O000O00000OoO.O0000O000000oO(string, "onReady")) {
            notifyReady();
        } else if (O000O00000OoO.O0000O000000oO(string, "onClose")) {
            notifyClose();
        } else {
            throw new JSONException("method value not found");
        }
    }

    private void handle104Version(JSONObject jSONObject) {
        String string = jSONObject.getString("method");
        JSONObject jSONObject2 = jSONObject.getJSONObject("content");
        if (O000O00000OoO.O0000O000000oO(string, "onInit")) {
            notifyInit(jSONObject2);
        } else if (O000O00000OoO.O0000O000000oO(string, "onError")) {
            notifyWebLoadError(jSONObject2);
        } else if (O000O00000OoO.O0000O000000oO(string, "onSuccess")) {
            notifySuccess(jSONObject2);
        } else if (O000O00000OoO.O0000O000000oO(string, "onReady")) {
            notifyReady(jSONObject2);
        } else if (O000O00000OoO.O0000O000000oO(string, "onClose")) {
            notifyClose(jSONObject2);
        } else {
            throw new JSONException("method value not found");
        }
    }

    @SuppressLint({"SetJavaScriptEnabled"})
    private void initStyle() {
        getSettings().setJavaScriptEnabled(true);
        getSettings().setSavePassword(false);
        getSettings().setUseWideViewPort(false);
        getSettings().setSupportZoom(true);
        getSettings().setLoadWithOverviewMode(false);
        getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        getSettings().setCacheMode(2);
        getSettings().setAllowFileAccess(false);
        getSettings().setNeedInitialFocus(false);
        getSettings().setBuiltInZoomControls(false);
        getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        getSettings().setLoadsImagesAutomatically(true);
        getSettings().setUseWideViewPort(false);
        setHorizontalScrollBarEnabled(false);
        setVerticalScrollBarEnabled(false);
    }

    private void notifyClose() {
        SimpleResultListener simpleResultListener = this.listener;
        if (simpleResultListener != null) {
            simpleResultListener.onClose();
        }
    }

    private void notifyClose(JSONObject jSONObject) {
        SimpleResultListener simpleResultListener = this.listener;
        if (simpleResultListener != null) {
            simpleResultListener.onCloseWithContent(jSONObject);
        }
    }

    private void notifyInit(JSONObject jSONObject) {
        SimpleResultListener simpleResultListener = this.listener;
        if (simpleResultListener != null) {
            simpleResultListener.onInitWithContent(jSONObject);
        }
    }

    private void notifyReady() {
        SimpleResultListener simpleResultListener = this.listener;
        if (simpleResultListener != null) {
            simpleResultListener.onReady();
        }
    }

    private void notifyReady(JSONObject jSONObject) {
        SimpleResultListener simpleResultListener = this.listener;
        if (simpleResultListener != null) {
            simpleResultListener.onReadyWithContent(jSONObject);
        }
    }

    private void notifySuccess(String str, boolean z14) {
        SimpleResultListener simpleResultListener = this.listener;
        if (simpleResultListener != null) {
            simpleResultListener.onSuccess(str, z14);
        }
    }

    private void notifySuccess(JSONObject jSONObject) {
        SimpleResultListener simpleResultListener = this.listener;
        if (simpleResultListener != null) {
            simpleResultListener.onSuccessWithContent(jSONObject);
        }
    }

    private void notifyWebLoadError(int i10) {
        SimpleResultListener simpleResultListener = this.listener;
        if (simpleResultListener != null) {
            simpleResultListener.onError(i10);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void notifyWebLoadError(JSONObject jSONObject) {
        loadDefaultHtml();
        SimpleResultListener simpleResultListener = this.listener;
        if (simpleResultListener != null) {
            simpleResultListener.onErrorWithContent(jSONObject);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void reportErrorMsg(String str) {
        O000O00000oO.O0000O000000oO(getContext()).O0000O000000oO(generateReportBean(str));
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void reportErrorMsg(String str, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
        String str2 = "WebResourceRequest:";
        if (webResourceRequest != null && Build.VERSION.SDK_INT >= 21) {
            str2 = str2 + webResourceRequest.getUrl();
        }
        String str3 = "WebResourceError:";
        if (webResourceError != null && Build.VERSION.SDK_INT >= 23) {
            str3 = str3 + webResourceError.getErrorCode() + "," + ((Object) webResourceError.getDescription());
        }
        reportErrorMsg(str + f.f18324b + str2 + f.f18324b + str3);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void reportErrorMsg(String str, WebResourceRequest webResourceRequest, WebResourceResponse webResourceResponse) {
        String str2 = "WebResourceRequest:";
        if (webResourceRequest != null && Build.VERSION.SDK_INT >= 21) {
            str2 = str2 + webResourceRequest.getUrl();
        }
        String str3 = "WebResourceResponse:";
        if (webResourceResponse != null) {
            String str4 = str3 + webResourceResponse.getEncoding() + ",";
            if (Build.VERSION.SDK_INT >= 21) {
                str3 = str4 + webResourceResponse.getReasonPhrase() + "," + webResourceResponse.getStatusCode() + "," + webResourceResponse.getResponseHeaders();
            } else {
                str3 = str4;
            }
        }
        reportErrorMsg(str + f.f18324b + str2 + f.f18324b + str3);
    }

    public void disableCaptcha() {
        loadUrl("javascript:SMCaptcha.disableCaptcha();");
    }

    public void enableCaptcha() {
        loadUrl("javascript:SMCaptcha.enableCaptcha();");
    }

    public int initWithOption(SmOption smOption, SimpleResultListener simpleResultListener) {
        if (smOption == null) {
            return SMCAPTCHA_SDK_OPTION_EMPTY;
        }
        if (O000O00000OoO.O0000O000000oO(smOption.getOrganization())) {
            return SMCAPTCHA_SDK_OPTION_NOORG;
        }
        if (O000O00000OoO.O0000O000000oO(smOption.getAppId())) {
            return SMCAPTCHA_SDK_OPTION_NOAPPID;
        }
        if (TextUtils.isEmpty(smOption.getCaptchaUuid())) {
            smOption.setCaptchaUuid(O000O00000OoO.O0000O000000oO());
        }
        this.option = smOption;
        if (simpleResultListener == null) {
            return SMCAPTCHA_SDK_NOLISTENER;
        }
        if (smOption.getMode() == null) {
            smOption.setMode(MODE_SLIDE);
        }
        O000O00000OoO.O0000O000000oO().O0000O000000oO(new com.ishumei.sdk.captcha.O000O00000oO.O0000O000000oO("webviewInit", System.currentTimeMillis(), smOption.getCaptchaUuid(), smOption.organization, smOption.getMode()));
        smOption.setHttps(smOption.getCaptchaHtml().startsWith("https"));
        this.listener = simpleResultListener;
        initStyle();
        setWebViewClient(new O0000O000000oO(smOption));
        O000O00000oO O0000O000000oO2 = O000O00000oO.O0000O000000oO(getContext());
        O0000O000000oO2.O0000O000000oO(smOption.isHttps());
        O0000O000000oO2.O0000O000000oO();
        O0000O000000oO.O0000O000000oO(smOption.getCaptchaHtml());
        reloadCaptcha();
        return SMCAPTCHA_SUCCESS;
    }

    public void loadDefaultHtml() {
        loadDataWithBaseURL(null, O0000O000000oO.O0000O000000oO(), "text/html", "utf-8", null);
    }

    public void reloadCaptcha() {
        loadUrl(this.option.getCaptchaHtml());
        this.retry++;
    }
}