package com.dowse.base.util;

import android.content.Context;
import android.os.Environment;
import android.os.RecoverySystem;
import com.dowse.base.log.DSLog;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/util/OtaUpdateUtil.class */
public class OtaUpdateUtil {
    public static final String TAG = "OtaUpdateUtil";

    /* loaded from: ds_base_2.0.9_23030112.aar:classes.jar:com/dowse/base/util/OtaUpdateUtil$ProgressListener.class */
    public interface ProgressListener {
        void onProgress(int i, String str);
    }

    private File copyFileToCache(ProgressListener progressListener) {
        File updateFile = getUdapteFile();
        if (updateFile.exists()) {
            progressListener.onProgress(0, "发现ota升级包=" + updateFile.getPath());
            try {
                InputStream inStream = new FileInputStream(updateFile);
                FileOutputStream fos = new FileOutputStream("/data/update.zip");
                byte[] buf = new byte[1024];
                progressListener.onProgress(0, "正在准备ota升级包, 请稍后..." + updateFile.getPath());
                while (true) {
                    int length = inStream.read(buf);
                    if (length != -1) {
                        fos.write(buf, 0, length);
                    } else {
                        inStream.close();
                        fos.close();
                        return new File("/data/update.zip");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    public File getUdapteFile() {
        File updateFile = null;
        if (Environment.getExternalStorageState().equals("mounted")) {
            File updateFile2 = new File("/sdcard/update.zip");
            if (updateFile2.exists()) {
                return updateFile2;
            }
            File updateFile3 = new File("/mnt/sdcard/update.zip");
            if (updateFile3.exists()) {
                return updateFile3;
            }
            File updateFile4 = new File("/mnt/sdcard1/update.zip");
            if (updateFile4.exists()) {
                return updateFile4;
            }
            updateFile = new File("/storage/sdcard1/update.zip");
            if (updateFile.exists()) {
                return updateFile;
            }
        }
        return updateFile;
    }

    public void systemUpdate(final Context context, final ProgressListener progressListener) {
        final File otaFile = copyFileToCache(progressListener);
        if (otaFile == null || !otaFile.exists()) {
            DSLog.e(TAG, "ota文件不存在");
            progressListener.onProgress(-1, "ota文件不存在.");
            return;
        }
        DSLog.d(TAG, "升级包：" + otaFile.getAbsoluteFile());
        try {
            RecoverySystem.verifyPackage(otaFile, new RecoverySystem.ProgressListener() { // from class: com.dowse.base.util.OtaUpdateUtil.1
                @Override // android.os.RecoverySystem.ProgressListener
                public void onProgress(int progress) {
                    progressListener.onProgress(progress, "验证OTA升级包进度..." + progress);
                    DSLog.d(OtaUpdateUtil.TAG, "验证OTA升级包进度..." + progress);
                    if (progress == 100) {
                        try {
                            progressListener.onProgress(progress, "开始安装ota升级包.");
                            DSLog.d(OtaUpdateUtil.TAG, "开始安装ota升级包.");
                            RecoverySystem.installPackage(context, otaFile);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
