package com.tencent.mm.plugin.appbrand.launching;

import android.text.TextUtils;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.a.g;
import com.tencent.mm.compatible.loader.a;
import com.tencent.mm.plugin.appbrand.app.f;
import com.tencent.mm.plugin.appbrand.appcache.WxaPkgWrappingInfo;
import com.tencent.mm.plugin.appbrand.appcache.aq;
import com.tencent.mm.plugin.appbrand.appcache.at;
import com.tencent.mm.plugin.appbrand.appcache.ax;
import com.tencent.mm.plugin.appbrand.appcache.j;
import com.tencent.mm.plugin.appbrand.appcache.r;
import com.tencent.mm.plugin.appbrand.appcache.y;
import com.tencent.mm.protocal.protobuf.arb;
import com.tencent.mm.sdk.g.d;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.ah;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.mm.vfs.e;
import com.tencent.ytcommon.BuildConfig;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Locale;

public final class v extends u {
    public final String appId;
    public int cBc;
    public final int cvn;
    public final String gTy;
    public final int ihB;
    public final boolean ihC;
    private at ihD;
    private final int[] ihE;

    public v(String str, String str2, int i) {
        this(str, str2, i, true);
    }

    public v(String str, String str2, int i, boolean z) {
        super(new y(str, str2));
        AppMethodBeat.i(131891);
        this.cvn = 0;
        this.cBc = 4;
        this.ihE = new int[]{14, 12};
        this.appId = str;
        this.gTy = str2;
        this.ihB = i;
        this.ihC = z;
        AppMethodBeat.o(131891);
    }

    public v(String str, String str2, int i, int i2) {
        this(str, str2, i, i2, (byte) 0);
    }

    private v(String str, String str2, int i, int i2, byte b) {
        super(new y(str, str2, i2));
        AppMethodBeat.i(131892);
        this.cvn = 0;
        this.cBc = 4;
        this.ihE = new int[]{14, 12};
        this.appId = str;
        this.gTy = str2;
        this.ihB = i;
        this.ihC = true;
        if (i2 != 0) {
            this.cBc = i2;
        }
        AppMethodBeat.o(131892);
    }

    public final String aHu() {
        AppMethodBeat.i(131893);
        String format = String.format(Locale.US, "pkg %s, targetVersion %d, codeType %d", new Object[]{this.ihz.toString(), Integer.valueOf(this.ihB), Integer.valueOf(0)});
        AppMethodBeat.o(131893);
        return format;
    }

    private WxaPkgWrappingInfo Ci(String str) {
        WxaPkgWrappingInfo xM;
        AppMethodBeat.i(131894);
        if (bo.isNullOrNil(this.gTy) || !this.gTy.endsWith("__CODELIB__")) {
            xM = WxaPkgWrappingInfo.xM(str);
        } else {
            xM = WxaPkgWrappingInfo.xN(str);
        }
        xM.gVu = this.ihB;
        AppMethodBeat.o(131894);
        return xM;
    }

    public final void prepare() {
        int i = 1;
        AppMethodBeat.i(131895);
        ax auV = f.auV();
        if (auV == null) {
            ab.e("MicroMsg.AppBrand.LaunchPkgPrepareJobReleaseCode", "get NULL storage with %s", new Object[]{aHu()});
            e(null);
            AppMethodBeat.o(131895);
            return;
        }
        this.ihD = auV.a(this.ihz.toString(), this.ihB, 0, new String[0]);
        String string;
        if (this.ihD == null) {
            ab.e("MicroMsg.AppBrand.LaunchPkgPrepareJobReleaseCode", "get NULL record with %s", new Object[]{aHu()});
            e(null);
            string = ah.getContext().getString(2131296775, new Object[]{BuildConfig.FLAVOR});
            if (!this.ihC || TextUtils.isEmpty(string)) {
                AppMethodBeat.o(131895);
                return;
            }
            d.xDG.d(new 3(this, string));
            AppMethodBeat.o(131895);
        } else if (bo.isNullOrNil(this.ihD.field_versionMd5)) {
            ab.e("MicroMsg.AppBrand.LaunchPkgPrepareJobReleaseCode", "get EMPTY md5 with %s", new Object[]{aHu()});
            e(null);
            AppMethodBeat.o(131895);
        } else {
            try {
                InputStream openRead = e.openRead(this.ihD.field_pkgPath);
                if (this.ihD.field_versionMd5.equals(g.b(openRead, 4096))) {
                    ab.i("MicroMsg.AppBrand.LaunchPkgPrepareJobReleaseCode", "%s prepare ok", new Object[]{aHu()});
                    e(Ci(this.ihD.field_pkgPath));
                    aHs();
                    bo.b(openRead);
                    AppMethodBeat.o(131895);
                    return;
                }
                bo.b(openRead);
                e.deleteFile(this.ihD.field_pkgPath);
                at a = h.a(this.ihz, 0, this.ihB, this.ihD.field_versionMd5);
                if (a != null) {
                    ab.i("MicroMsg.AppBrand.LaunchPkgPrepareJobReleaseCode", "find reusable record, target (%s), reusable (%s %d)", new Object[]{aHu(), a.field_appId, Integer.valueOf(a.field_version)});
                    string = aq.aq(this.ihz.toString(), this.ihB);
                    e.y(a.field_pkgPath, string);
                    f.auV().d(this.ihz.toString(), 0, this.ihB, string);
                    e(Ci(string));
                    aHs();
                    AppMethodBeat.o(131895);
                    return;
                }
                String yVar;
                ab.i("MicroMsg.AppBrand.LaunchPkgPrepareJobReleaseCode", "%s getDownloadURL", new Object[]{aHu()});
                arb arb = new arb();
                arb.csB = this.appId;
                arb.vOP = this.ihB;
                arb.wtb = 0;
                if (!bo.isNullOrNil(this.gTy)) {
                    arb.wtb = this.cBc;
                    if (!a.contains(this.ihE, this.cBc)) {
                        arb.wsY = this.gTy;
                    }
                }
                arb.wuL = this.ihD.field_versionMd5;
                if (r.avD() && j.a.np(0)) {
                    yVar = this.ihz.toString();
                    if (!bo.isNullOrNil(yVar)) {
                        WxaPkgWrappingInfo bm;
                        if (yVar.endsWith("__CODELIB__")) {
                            bm = h.bm(yVar, 0);
                        } else {
                            bm = h.bm(yVar, 1);
                        }
                        if (bm != null) {
                            arb.wuM = bm.gVu;
                        }
                    }
                }
                yVar = "MicroMsg.AppBrand.LaunchPkgPrepareJobReleaseCode";
                String str = "appid:%s,cgirequest pkgtype:%s,modulename:%s,version:%s,old_app_version:%s,record md5:%s,openIncremental:%s";
                Object[] objArr = new Object[7];
                objArr[0] = arb.csB;
                objArr[1] = Integer.valueOf(arb.wtb);
                objArr[2] = arb.wsY;
                objArr[3] = Integer.valueOf(arb.vOP);
                objArr[4] = Integer.valueOf(arb.wuM);
                objArr[5] = arb.wuL;
                if (!r.avD()) {
                    i = 0;
                }
                objArr[6] = Integer.valueOf(i);
                ab.i(yVar, str, objArr);
                new com.tencent.mm.plugin.appbrand.appcache.d(arb).acy().h(new 1(this, arb));
                AppMethodBeat.o(131895);
            } catch (FileNotFoundException e) {
                bo.b(null);
            } catch (Throwable th) {
                bo.b(null);
                AppMethodBeat.o(131895);
            }
        }
    }
}
