package com.cvte.dss.ups.mic.mic3308;

import android.content.Context;
import android.util.Log;
import com.cvte.dss.ups.plugin.IUpsPlugin;
import d.d.c.a.a;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/* JADX INFO: loaded from: classes.dex */
public class UpdTool implements IUpdOperation {
    private static final String TAG = "UpdTool";
    private static final String TMP_SUFFIX = ".tmp";
    private static final String UPD_TOOL_FILE = "mic_array_fw_upd_tool";
    private File mUpdTool;

    public UpdTool(Context context) throws Throwable {
        File file = new File(context.getFilesDir(), UPD_TOOL_FILE);
        this.mUpdTool = file;
        if (file.exists()) {
            return;
        }
        installUpdTool(context, this.mUpdTool);
    }

    private boolean copyFile(InputStream inputStream, OutputStream outputStream) {
        try {
            byte[] bArr = new byte[1024];
            while (true) {
                int i2 = inputStream.read(bArr);
                if (i2 == -1) {
                    return true;
                }
                outputStream.write(bArr, 0, i2);
            }
        } catch (Exception unused) {
            return false;
        }
    }

    private void installUpdTool(Context context, File file) throws Throwable {
        FileOutputStream fileOutputStream;
        InputStream inputStreamOpenRawResource = null;
        try {
            try {
                File file2 = new File(file.getAbsolutePath() + TMP_SUFFIX);
                fileOutputStream = new FileOutputStream(file2);
                try {
                    inputStreamOpenRawResource = context.getResources().openRawResource(R.raw.mic_array_fw_upd_tool);
                    if (copyFile(inputStreamOpenRawResource, fileOutputStream) && file2.renameTo(file)) {
                        a.a("chmod 755 " + this.mUpdTool.getAbsolutePath());
                    }
                } catch (Exception e2) {
                    e = e2;
                    Log.e(TAG, "installUpdTool failed", e);
                }
            } catch (Throwable th) {
                th = th;
                c.n.a.O(inputStreamOpenRawResource);
                c.n.a.O(fileOutputStream);
                throw th;
            }
        } catch (Exception e3) {
            e = e3;
            fileOutputStream = null;
        } catch (Throwable th2) {
            th = th2;
            fileOutputStream = null;
            c.n.a.O(inputStreamOpenRawResource);
            c.n.a.O(fileOutputStream);
            throw th;
        }
        c.n.a.O(inputStreamOpenRawResource);
        c.n.a.O(fileOutputStream);
    }

    @Override // com.cvte.dss.ups.mic.mic3308.IUpdOperation
    public String getModule() {
        String str;
        a.C0036a c0036aA = a.a(this.mUpdTool.getAbsolutePath() + " -g name");
        if (c0036aA.a != 0 || (str = c0036aA.f2490b) == null) {
            return null;
        }
        return str.trim();
    }

    @Override // com.cvte.dss.ups.mic.mic3308.IUpdOperation
    public String getOtaKey() {
        String str;
        a.C0036a c0036aA = a.a(this.mUpdTool.getAbsolutePath() + " -g ota_key");
        if (c0036aA.a != 0 || (str = c0036aA.f2490b) == null) {
            return null;
        }
        return str.trim();
    }

    @Override // com.cvte.dss.ups.mic.mic3308.IUpdOperation
    public String getVersion() {
        String str;
        a.C0036a c0036aA = a.a(this.mUpdTool.getAbsolutePath() + " -g full_version");
        if (c0036aA.a != 0 || (str = c0036aA.f2490b) == null) {
            return null;
        }
        return str.trim();
    }

    @Override // com.cvte.dss.ups.mic.mic3308.IUpdOperation
    public Boolean upgrade(String str, Boolean bool, final IUpsPlugin.IUpgradeStatusListener iUpgradeStatusListener) throws Throwable {
        String str2;
        File file = new File(str);
        if (!file.exists()) {
            iUpgradeStatusListener.onFailed("file not found");
            return Boolean.FALSE;
        }
        if (bool.booleanValue()) {
            str2 = this.mUpdTool.getAbsolutePath() + " -u " + file + " -w " + this.mUpdTool.getParent() + " -f";
        } else {
            str2 = this.mUpdTool.getAbsolutePath() + " -u " + file + " -w " + this.mUpdTool.getParent();
        }
        Log.i(TAG, "doUpgrade: " + str2);
        a.c(new String[]{str2}, new a.b() { // from class: com.cvte.dss.ups.mic.mic3308.UpdTool.1
            public int retryCount = 0;

            @Override // d.d.c.a.a.b
            public void onResult(String str3) {
                Log.i(UpdTool.TAG, "onResult: " + str3);
                if (str3.contains("failed") || str3.contains("not new")) {
                    iUpgradeStatusListener.onFailed(str3);
                    return;
                }
                String[] strArrSplit = str3.split("=");
                if (strArrSplit.length < 2) {
                    return;
                }
                try {
                    int iIntValue = Integer.valueOf(strArrSplit[1].replace("%", "")).intValue();
                    if (iIntValue != 100) {
                        iUpgradeStatusListener.onProcess(iIntValue / 100.0f);
                        return;
                    }
                    while (UpdTool.this.getVersion() == null && this.retryCount < 20) {
                        try {
                            Log.d(UpdTool.TAG, "update finish, wait for reboot count:" + this.retryCount);
                            Thread.sleep(1000L);
                        } catch (InterruptedException unused) {
                        } catch (Throwable th) {
                            this.retryCount++;
                            throw th;
                        }
                        this.retryCount++;
                    }
                    iUpgradeStatusListener.onProcess(1.0f);
                    iUpgradeStatusListener.onComplete();
                } catch (Exception e2) {
                    IUpsPlugin.IUpgradeStatusListener iUpgradeStatusListener2 = iUpgradeStatusListener;
                    StringBuilder sbB = d.b.a.a.a.B("Exception: ");
                    sbB.append(e2.getMessage());
                    iUpgradeStatusListener2.onFailed(sbB.toString());
                }
            }
        });
        return Boolean.TRUE;
    }
}
