package com.seewo.ota.util;

import android.content.Context;
import android.util.Log;
import com.seewo.libmyousdk.model.RomUpdateInfo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/* JADX INFO: compiled from: UpdateFileUtil.java */
/* JADX INFO: loaded from: classes.dex */
public class t {

    /* JADX INFO: compiled from: UpdateFileUtil.java */
    public interface a {
        void a(int i2);

        void b();

        void c(Throwable th);
    }

    public static boolean a(final File file, final File file2, final a aVar) {
        Log.d("UpdateFileUtil", "copyFile, src:" + file.getPath() + "  dst:" + file2.getPath());
        if (!file.getPath().equals(file2.getPath())) {
            e.a.h.E(Boolean.TRUE).T(e.a.x.a.b()).F(new e.a.t.d() { // from class: com.seewo.ota.util.e
                @Override // e.a.t.d
                public final Object apply(Object obj) {
                    return t.k(file, aVar, file2, (Boolean) obj);
                }
            }).P(new e.a.t.c() { // from class: com.seewo.ota.util.f
                @Override // e.a.t.c
                public final void accept(Object obj) throws Exception {
                    t.l(aVar, (Boolean) obj);
                }
            }, new e.a.t.c() { // from class: com.seewo.ota.util.c
                @Override // e.a.t.c
                public final void accept(Object obj) throws Exception {
                    t.m(aVar, (Throwable) obj);
                }
            });
            return true;
        }
        aVar.a(100);
        aVar.b();
        return true;
    }

    private static String b(Context context) {
        return context.getFilesDir().getParentFile().getPath() + "/cache";
    }

    public static String c(String str) {
        FileInputStream fileInputStream;
        byte[] bArr;
        MessageDigest messageDigest;
        try {
            fileInputStream = new FileInputStream(str);
            try {
                bArr = new byte[4096];
                messageDigest = MessageDigest.getInstance("MD5");
                messageDigest.reset();
            } finally {
            }
        } catch (IOException | NoSuchAlgorithmException e2) {
            Log.e("UpdateFileUtil", e2.getMessage(), e2);
            return "";
        }
        while (true) {
            int i2 = fileInputStream.read(bArr);
            if (i2 <= 0) {
                String strP = p(messageDigest.digest());
                fileInputStream.close();
                return strP;
            }
            messageDigest.update(bArr, 0, i2);
            Log.e("UpdateFileUtil", e2.getMessage(), e2);
            return "";
        }
    }

    public static List<String> d(String str) {
        ArrayList arrayList = new ArrayList();
        try {
            Enumeration<? extends ZipEntry> enumerationEntries = new ZipFile(str).entries();
            while (enumerationEntries.hasMoreElements()) {
                arrayList.add(enumerationEntries.nextElement().getName());
            }
        } catch (IOException e2) {
            Log.e("UpdateFileUtil", "getFilesInZip: ", e2);
        }
        return arrayList;
    }

    public static String e(String str) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec("seewo".getBytes("UTF-8"), "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(secretKeySpec);
            byte[] bArrDoFinal = mac.doFinal(str.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte b2 : bArrDoFinal) {
                sb.append(Integer.toString((b2 & 255) + 256, 16).substring(1));
            }
            return sb.toString();
        } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException e2) {
            Log.e("UpdateFileUtil", e2.getMessage(), e2);
            return null;
        }
    }

    public static boolean f(File file, long j2) {
        long jLastModified = file.lastModified();
        d.e.h.e.e.a("UpdateFileUtil", "hasFileChanged: lastModified=" + jLastModified + ", previousModifiedTime=" + j2);
        return jLastModified != j2;
    }

    public static boolean g(String str, long j2) {
        return f(new File(str), j2);
    }

    public static boolean h(String str) {
        return g(str, com.seewo.ota.data.a.g().r(str));
    }

    public static boolean i(String str) {
        List<String> listD = d(str);
        Log.d("UpdateFileUtil", "is3in1OTAZipFile: zipFilePath=" + str + "，fileList=" + listD.toString());
        return listD.contains("firmware/camera.bin") || listD.contains("firmware/update.upd");
    }

    public static boolean j(String str, RomUpdateInfo romUpdateInfo) {
        File file = new File(str);
        if (file.exists() && file.length() == romUpdateInfo.getFileSize()) {
            Log.d("UpdateFileUtil", "isUpdateFileExist start Calculate md5: ");
            if (!h(str)) {
                Log.d("UpdateFileUtil", "isUpdateFileExist file not changed: " + str);
                return true;
            }
            String strC = c(str);
            Log.d("UpdateFileUtil", "isUpdateFileExist size equal: " + strC + " : " + romUpdateInfo.getFileMd5().toUpperCase());
            if (strC.equalsIgnoreCase(romUpdateInfo.getFileMd5())) {
                com.seewo.ota.data.a.g().R(str, file.lastModified());
                return true;
            }
        }
        Log.d("UpdateFileUtil", "isUpdateFileExist size not equal: fileSize: " + file.length() + " : " + romUpdateInfo.getFileSize());
        return false;
    }

    static /* synthetic */ Boolean k(File file, a aVar, File file2, Boolean bool) throws Exception {
        long length = file.length();
        aVar.a(0);
        if (file2.exists()) {
            file2.delete();
        }
        file2.createNewFile();
        FileInputStream fileInputStream = new FileInputStream(file);
        FileOutputStream fileOutputStream = new FileOutputStream(file2);
        byte[] bArr = new byte[1024];
        long j2 = 0;
        int i2 = 0;
        while (true) {
            int i3 = fileInputStream.read(bArr);
            if (i3 == -1) {
                fileOutputStream.flush();
                fileInputStream.close();
                fileOutputStream.close();
                return Boolean.TRUE;
            }
            fileOutputStream.write(bArr, 0, i3);
            j2 += (long) i3;
            int i4 = (int) ((j2 / length) * 100.0f);
            if (i4 != i2) {
                aVar.a(i4);
                i2 = i4;
            }
        }
    }

    static /* synthetic */ void l(a aVar, Boolean bool) throws Exception {
        Log.i("UpdateFileUtil", "upgradeFromOta copyFile =" + bool);
        aVar.b();
    }

    static /* synthetic */ void m(a aVar, Throwable th) throws Exception {
        Log.e("UpdateFileUtil", th.getMessage(), th);
        aVar.c(th);
    }

    public static void o(String str) throws Throwable {
        Log.d("UpdateFileUtil", "prepare3In1UpdateFiles: start.");
        File file = new File(new File(str).getParent() + "/3in1/");
        if (file.exists()) {
            d.e.c.a.a.b(file, true);
        }
        file.mkdir();
        Log.d("UpdateFileUtil", "prepare3In1UpdateFiles: unzip start.");
        d.e.c.a.d.b(str, file.getAbsolutePath());
        Log.d("UpdateFileUtil", "prepare3In1UpdateFiles: unzip end.");
        d.e.h.e.f.b("cp -r -p " + file + "/firmware/* /cache/", true);
        String[] list = file.list(new FilenameFilter() { // from class: com.seewo.ota.util.d
            @Override // java.io.FilenameFilter
            public final boolean accept(File file2, String str2) {
                return str2.matches("^OTA_.*\\.zip$");
            }
        });
        if (list.length <= 0) {
            throw new IOException("unable to find real update.zip file.");
        }
        d.e.h.e.f.b("mv " + file.getAbsolutePath() + "/" + list[0] + " " + com.seewo.ota.data.b.i().j(), false);
        d.e.c.a.a.b(file, true);
        Log.d("UpdateFileUtil", "prepare3In1UpdateFiles: end.");
    }

    private static String p(byte[] bArr) {
        StringBuilder sb = new StringBuilder();
        for (byte b2 : bArr) {
            int i2 = b2 & 255;
            if (i2 < 16) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(i2).toUpperCase());
        }
        return sb.toString();
    }

    public static void q(Context context, com.seewo.ota.q.a aVar, int i2) {
        String str = i2 == 0 ? "SlotUpgradeConfig.bat" : "PlatformUpgradeConfig.bat";
        String strB = b(context);
        d.e.c.a.a.d(strB);
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File(strB, str)));
            try {
                objectOutputStream.writeObject(aVar);
                objectOutputStream.close();
            } finally {
            }
        } catch (IOException e2) {
            Log.e("UpdateFileUtil", e2.getMessage(), e2);
        }
    }
}
