package com.seewo.ota.v.u;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.FileUtils;
import android.os.Handler;
import android.os.Looper;
import android.os.UpdateEngine;
import android.os.UpdateEngineCallback;
import android.util.Log;
import com.seewo.libmyousdk.model.DubboResponseInfo;
import com.seewo.ota.util.u;
import d.e.h.a.a;
import d.e.h.e.f;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/* JADX INFO: compiled from: ABUpgradeProcessor.java */
/* JADX INFO: loaded from: classes.dex */
public class l extends n {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private static float f2603f = 0.45f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private static float f2604g = 0.05f;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private static float f2605h = 0.5f;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private String f2606c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final UpdateEngine f2607d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final Handler f2608e;

    /* JADX INFO: compiled from: ABUpgradeProcessor.java */
    class a extends UpdateEngineCallback {
        final /* synthetic */ String a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ boolean f2609b;

        a(String str, boolean z) {
            this.a = str;
            this.f2609b = z;
        }

        public /* synthetic */ void a() {
            u.i(l.this.a);
        }

        public void onPayloadApplicationComplete(int i2) {
            Log.i("ABUpgradeProcessor", "A/B upgrade OTA : onPayloadApplicationComplete errorCode:" + i2);
            if (!l.this.f2606c.equals(this.a)) {
                d.e.h.e.f.b("rm -rf " + l.this.f2606c, false);
            }
            if (i2 != 0) {
                s sVar = l.this.f2615b;
                if (sVar != null) {
                    sVar.c(3, this.a);
                    return;
                }
                return;
            }
            FileUtils.deleteContents(new File(this.a));
            s sVar2 = l.this.f2615b;
            if (sVar2 != null) {
                sVar2.c(5, this.a);
            }
            if (com.seewo.vcommons.data.d.l("persist.sys.ab_upgrade_auto_reboot", DubboResponseInfo.STATUS_OK).equals(DubboResponseInfo.STATUS_OK) && this.f2609b) {
                l.this.f2608e.postDelayed(new Runnable() { // from class: com.seewo.ota.v.u.a
                    @Override // java.lang.Runnable
                    public final void run() {
                        this.f2589b.a();
                    }
                }, 3000L);
            }
        }

        public void onStatusUpdate(int i2, float f2) {
            Log.i("ABUpgradeProcessor", "onStatusUpdate: " + i2 + ", " + f2);
            s sVar = l.this.f2615b;
            if (sVar != null) {
                if (i2 == 3) {
                    sVar.onProgress((int) (f2 * 100.0f * l.f2603f));
                } else if (i2 == 4) {
                    sVar.onProgress((int) ((f2 * 100.0f * l.f2604g) + (l.f2603f * 100.0f)));
                } else if (i2 == 5) {
                    sVar.onProgress((int) ((f2 * 100.0f * l.f2605h) + (l.f2603f * 100.0f) + (l.f2604g * 100.0f)));
                }
            }
        }
    }

    public l(Context context, s sVar) {
        super(context, sVar);
        this.f2608e = new Handler(Looper.getMainLooper());
        this.f2607d = new UpdateEngine();
        if (d.e.h.a.a.h(10) && Build.VERSION.SDK_INT >= 34) {
            f2603f = 0.5f;
            f2604g = 0.49f;
            f2605h = 0.01f;
        } else if (d.e.h.a.a.h(16)) {
            f2603f = 0.49f;
            f2604g = 0.5f;
            f2605h = 0.01f;
        } else if (12 == a.b.a) {
            f2603f = 0.9f;
            f2604g = 0.1f;
            f2605h = 0.0f;
        } else if (d.e.h.a.a.h(9) && Build.VERSION.SDK_INT > 30) {
            f2603f = 0.75f;
            f2604g = 0.24f;
            f2605h = 0.01f;
        } else if (Build.VERSION.SDK_INT > 30) {
            f2603f = 0.45f;
            f2604g = 0.05f;
            f2605h = 0.5f;
        } else {
            f2603f = 1.0f;
            f2604g = 0.0f;
            f2605h = 0.0f;
        }
        d.e.h.e.e.a("ABUpgradeProcessor", "ABUpgradeProcessor: PERCENT_DOWNLOADING_STATUS=" + f2603f + ", PERCENT_VERIFYING_STATUS=" + f2604g + ", PERCENT_FINALIZING_STATUS=" + f2605h + ", platform=" + a.b.a + ", sdk=" + Build.VERSION.SDK_INT);
    }

    private String[] k(ZipFile zipFile) throws IOException {
        LinkedList linkedList = new LinkedList();
        ZipEntry entry = zipFile.getEntry("payload_properties.txt");
        if (entry != null) {
            InputStream inputStream = zipFile.getInputStream(entry);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            while (true) {
                String line = bufferedReader.readLine();
                if (line == null) {
                    break;
                }
                linkedList.add(line);
            }
            inputStream.close();
            bufferedReader.close();
        }
        return (String[]) linkedList.toArray(new String[0]);
    }

    private long[] l(ZipFile zipFile, ZipEntry zipEntry) throws IOException {
        String strSubstring;
        int iIndexOf;
        String strC = c(zipFile, zipEntry);
        Log.i("ABUpgradeProcessor", "metadata: " + strC);
        int iIndexOf2 = strC.indexOf("payload.bin:") + 12;
        if (iIndexOf2 >= 0 && (iIndexOf = (strSubstring = strC.substring(iIndexOf2)).indexOf(",")) >= 0) {
            String strSubstring2 = strSubstring.substring(0, iIndexOf);
            Log.i("ABUpgradeProcessor", "payloadBinInfo: " + strSubstring2);
            String[] strArrSplit = strSubstring2.split(":");
            if (strArrSplit.length == 2) {
                return new long[]{Long.parseLong(strArrSplit[0]), Long.parseLong(strArrSplit[1])};
            }
        }
        return new long[0];
    }

    private String m(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i2 = 0; i2 < str.length(); i2++) {
            sb.append("\\");
            sb.append(str.charAt(i2));
        }
        return sb.toString();
    }

    @Override // com.seewo.ota.v.u.o
    public void a(String str, boolean z) {
        s sVar;
        Log.d("ABUpgradeProcessor", "AB upgradeSystem " + str);
        if (Build.VERSION.SDK_INT > 30) {
            this.f2606c = "/data/ota_package/update.zip";
        } else {
            this.f2606c = str;
        }
        this.f2607d.bind(new a(str, z), this.f2608e);
        if (n(str) || (sVar = this.f2615b) == null) {
            return;
        }
        sVar.c(3, str);
    }

    @Override // com.seewo.ota.v.u.n
    public boolean d(String str) {
        if (Build.VERSION.SDK_INT >= 30) {
            return super.d(str);
        }
        Log.d("ABUpgradeProcessor", "isAvailablePackage: sdk 30 true");
        return true;
    }

    public boolean n(String str) {
        File file;
        Log.d("ABUpgradeProcessor", "A/B upgrade OTA: copy OTA " + str + "  to " + this.f2606c + "  ,then upgrade");
        try {
            ZipFile zipFile = new ZipFile(str);
            try {
                ZipEntry entry = zipFile.getEntry("META-INF/com/android/metadata");
                if (entry == null) {
                    Log.e("ABUpgradeProcessor", str + " is not a valid OTA package for A/B system update !!");
                    zipFile.close();
                    return false;
                }
                long[] jArrL = l(zipFile, entry);
                if (jArrL.length != 2) {
                    Log.e("ABUpgradeProcessor", "Read payload.bin offset and size error!");
                    zipFile.close();
                    return false;
                }
                String[] strArrK = k(zipFile);
                if (strArrK.length == 0) {
                    Log.e("ABUpgradeProcessor", "Read header key value pairs error!");
                    zipFile.close();
                    return false;
                }
                if (this.f2606c.equals(str)) {
                    file = new File(str);
                } else {
                    f.a aVarB = d.e.h.e.f.b("cp -p " + m(str) + " " + this.f2606c, false);
                    Log.d("ABUpgradeProcessor", "copy file result: " + aVarB.a + " ,successMsg:" + aVarB.f3605b + "  ,errorMsg:" + aVarB.f3606c);
                    file = new File(this.f2606c);
                }
                String string = Uri.fromFile(file).toString();
                long j2 = jArrL[0];
                long j3 = jArrL[1];
                Log.i("ABUpgradeProcessor", "applyPayload - url: " + string + ", offset: " + j2 + ", size: " + j3 + ", headerKeyValuePairs: " + Arrays.toString(strArrK));
                this.f2607d.applyPayload(string, j2, j3, strArrK);
                zipFile.close();
                return true;
            } finally {
            }
        } catch (IOException e2) {
            Log.e("ABUpgradeProcessor", e2.getMessage(), e2);
            return false;
        }
    }
}
