package com.tencent.mm.plugin.brandservice.ui.timeline.preload.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.plugin.brandservice.ui.timeline.preload.b.b;
import com.tencent.mm.plugin.brandservice.ui.timeline.preload.k;
import com.tencent.mm.plugin.webview.preload.a;
import com.tencent.mm.plugin.webview.ui.tools.jsapi.f;
import com.tencent.mm.plugin.webview.ui.tools.jsapi.m;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.xweb.WebView;
import com.tencent.xweb.n;
import com.tencent.xweb.o;
import com.tencent.xweb.s;
import com.tencent.ytcommon.BuildConfig;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

class e$2 extends s {
    final /* synthetic */ e jVo;

    e$2(e eVar) {
        this.jVo = eVar;
    }

    public final void b(WebView webView, String str, Bitmap bitmap) {
        AppMethodBeat.i(14426);
        ab.i(e.a(this.jVo), "[onPageStarted] url:%s", new Object[]{str});
        super.b(webView, str, bitmap);
        e.a(this.jVo, false);
        AppMethodBeat.o(14426);
    }

    public final void b(WebView webView, String str) {
        AppMethodBeat.i(14427);
        ab.i(e.a(this.jVo), "[onPageFinished] url:%s", new Object[]{str});
        super.b(webView, str);
        e.b(this.jVo);
        e.a(this.jVo, true);
        this.jVo.aJq();
        AppMethodBeat.o(14427);
    }

    private o f(WebView webView, String str) {
        String str2;
        int i = 0;
        AppMethodBeat.i(14428);
        ab.i(e.a(this.jVo), "getResourceResponse, requrl:%s, url:%s", new Object[]{str, this.jVo.jVi});
        if (str.equals(this.jVo.jVi)) {
            str2 = this.jVo.jVk;
        } else if (str.startsWith(b.aXB())) {
            str2 = k.q(this.jVo.jRb.jSV, this.jVo.jRb.jSY, str.replaceFirst(b.aXB(), BuildConfig.FLAVOR));
            i = 1;
        } else if (str.startsWith(b.getPrefix())) {
            str2 = k.q(this.jVo.jRb.jSV, this.jVo.jRb.jSY, str.replaceFirst(b.getPrefix(), BuildConfig.FLAVOR));
            i = 1;
        } else {
            ab.e(e.a(this.jVo), "return null");
            AppMethodBeat.o(14428);
            return null;
        }
        ab.v(e.a(this.jVo), "response path:" + bo.bc(str2, "null"));
        o g = g(webView, str2);
        if (g != null) {
            ab.i(e.a(this.jVo), "return intercepted success");
            AppMethodBeat.o(14428);
            return g;
        }
        ab.e(e.a(this.jVo), "return intercepted null");
        a.e(this.jVo.jRb.upQ, 126, 1, true);
        if (i != 0) {
            a.e(this.jVo.jRb.upQ, 233, 1, true);
        }
        AppMethodBeat.o(14428);
        return null;
    }

    private o g(WebView webView, String str) {
        o oVar;
        Object obj = null;
        AppMethodBeat.i(14429);
        String ako = com.tencent.mm.sdk.f.b.ako(str);
        String str2 = "UTF-8";
        if (str.endsWith(".js") && f.c(webView)) {
            obj = new m(str).toString();
            oVar = new o(ako, str2, new ByteArrayInputStream(obj.getBytes(StandardCharsets.UTF_8)));
        } else {
            InputStream fileInputStream;
            try {
                fileInputStream = new FileInputStream(str);
            } catch (FileNotFoundException e) {
                ab.printErrStackTrace(e.a(this.jVo), e, BuildConfig.FLAVOR, new Object[0]);
                fileInputStream = null;
            }
            if (fileInputStream == null) {
                ab.e(e.a(this.jVo), "stream is null, err");
                AppMethodBeat.o(14429);
                return null;
            }
            oVar = new o(ako, str2, fileInputStream);
        }
        HashMap hashMap = new HashMap();
        ako = Integer.toHexString(str.hashCode() & -1).toLowerCase();
        hashMap.put("Cache-Control", "max-age=31536000");
        hashMap.put("ETag", ako);
        if (obj != null) {
            hashMap.put("RunLocalJS", obj);
        }
        oVar.mResponseHeaders = hashMap;
        ab.i("MicroMsg.TmplWxJsWebView", "readFileWebResp:%s\nETag:%s", new Object[]{str, ako});
        AppMethodBeat.o(14429);
        return oVar;
    }

    public final o c(WebView webView, String str) {
        AppMethodBeat.i(14430);
        if (bo.isNullOrNil(str)) {
            ab.i(e.a(this.jVo), "shouldInterceptRequest, url is null");
            AppMethodBeat.o(14430);
            return null;
        }
        ab.v(e.a(this.jVo), "shouldInterceptRequest, url = %s", new Object[]{str});
        o f = f(webView, str);
        AppMethodBeat.o(14430);
        return f;
    }

    public final o a(WebView webView, n nVar, Bundle bundle) {
        AppMethodBeat.i(14431);
        if (nVar == null || nVar.getUrl() == null || bo.isNullOrNil(nVar.getUrl().toString())) {
            ab.e(e.a(this.jVo), "shouldInterceptRequest, webResourceRequest is null");
            AppMethodBeat.o(14431);
            return null;
        }
        ab.v(e.a(this.jVo), "shouldInterceptRequest, url = %s, method = %s, isForMainFrame = %b", new Object[]{nVar.getUrl(), nVar.getMethod(), Boolean.valueOf(nVar.isForMainFrame())});
        ab.v(e.a(this.jVo), "shouldInterceptRequest, url = %s", new Object[]{nVar.getUrl().toString()});
        o f = f(webView, r0);
        AppMethodBeat.o(14431);
        return f;
    }

    public final o a(WebView webView, n nVar) {
        AppMethodBeat.i(14432);
        if (nVar == null || nVar.getUrl() == null || bo.isNullOrNil(nVar.getUrl().toString())) {
            ab.e(e.a(this.jVo), "shouldInterceptRequest, url is null");
            AppMethodBeat.o(14432);
            return null;
        }
        ab.v(e.a(this.jVo), "shouldInterceptRequest, url = %s, method = %s, isForMainFrame = %b", new Object[]{nVar.getUrl(), nVar.getMethod(), Boolean.valueOf(nVar.isForMainFrame())});
        o f = f(webView, nVar.getUrl().toString());
        AppMethodBeat.o(14432);
        return f;
    }
}
