package com.seewo.ota.v.u;

import android.content.Context;
import android.os.Build;
import android.os.RecoverySystem;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/* JADX INFO: compiled from: BaseUpgradeProcessor.java */
/* JADX INFO: loaded from: classes.dex */
public abstract class n implements o {
    protected Context a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    protected s f2615b;

    public n(Context context, s sVar) {
        this.a = context;
        this.f2615b = sVar;
    }

    private String b(ZipFile zipFile, ZipEntry zipEntry) {
        int i2;
        try {
            String strC = c(zipFile, zipEntry);
            Log.i("BaseUpgradeProcessor", "getOtaSpecialString :metadata: " + strC);
            int iIndexOf = strC.indexOf("ro.cvte.ota.special=");
            if (iIndexOf >= 0 && iIndexOf < strC.length() && (i2 = iIndexOf + 20) >= 0) {
                String strSubstring = strC.substring(i2);
                int iIndexOf2 = strSubstring.indexOf("\n");
                if (iIndexOf2 >= 0) {
                    String strSubstring2 = strSubstring.substring(0, iIndexOf2);
                    Log.i("BaseUpgradeProcessor", "getOtaSpecialString:" + strSubstring2);
                    if (!TextUtils.isEmpty(strSubstring2)) {
                        return strSubstring2;
                    }
                } else {
                    String strTrim = strSubstring.trim();
                    if (!TextUtils.isEmpty(strTrim)) {
                        return strTrim;
                    }
                }
            }
            return "0";
        } catch (IOException e2) {
            Log.e("BaseUpgradeProcessor", e2.getMessage(), e2);
            return "0";
        }
    }

    private boolean e(String str) {
        Log.d("BaseUpgradeProcessor", "isAvailableZip start:" + str);
        if (d.e.h.a.a.a == 9 && Build.VERSION.SDK_INT <= 30) {
            try {
                ZipFile zipFile = new ZipFile(str);
                try {
                    ZipEntry entry = zipFile.getEntry("META-INF/com/android/metadata");
                    if (entry == null) {
                        Log.e("BaseUpgradeProcessor", str + " is not a valid OTA package for A/B system update !!");
                        zipFile.close();
                        return false;
                    }
                    String strF = com.seewo.vcommons.data.d.f();
                    String strB = b(zipFile, entry);
                    Log.d("BaseUpgradeProcessor", "isAvailableZip currentSpecialString:" + strF + ",fileSpecialString:" + strB);
                    if (!strF.equals(strB)) {
                        Log.e("BaseUpgradeProcessor", str + " is not a valid OTA package, 4G or 8G not match!!");
                        zipFile.close();
                        return false;
                    }
                    zipFile.close();
                } finally {
                }
            } catch (IOException e2) {
                Log.e("BaseUpgradeProcessor", e2.getMessage(), e2);
            }
        }
        return true;
    }

    protected String c(ZipFile zipFile, ZipEntry zipEntry) throws IOException {
        int size = (int) zipEntry.getSize();
        byte[] bArr = new byte[size];
        InputStream inputStream = zipFile.getInputStream(zipEntry);
        int i2 = inputStream.read(bArr);
        inputStream.close();
        if (size == i2) {
            return new String(bArr).trim();
        }
        throw new IOException("Error reading " + zipFile.getName() + ", wrong buffer length.");
    }

    public boolean d(String str) {
        Log.d("BaseUpgradeProcessor", "isAvailablePackage start:" + str);
        try {
            RecoverySystem.verifyPackage(new File(str), null, null);
            return e(str);
        } catch (Exception e2) {
            Log.e("BaseUpgradeProcessor", e2.getMessage(), e2);
            return false;
        }
    }
}
