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

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.cvte.dss.ups.plugin.IUpsPlugin;
import com.cvte.udi.utils.CommonUtils;
import com.cvte.udi.utils.ShellUtils;
import g.a.a.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 final 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, file);
    }

    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 String getMicMsg() {
        String systemProperty = SystemPropertyUtil.getSystemProperty("persist.ctm.usb_mic.vidpid");
        Log.i(TAG, "getMicPidVidFromProp: " + systemProperty);
        return TextUtils.isEmpty(systemProperty) ? "" : a.t(" -EUID=", systemProperty);
    }

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

    @Override // com.cvte.dss.ups.mic.mic3308.IUpdOperation
    public String getModule() {
        String str;
        ShellUtils.CommandResult commandResultExecuteCommand = ShellUtils.executeCommand(this.mUpdTool.getAbsolutePath() + getMicMsg() + " -g name");
        if (commandResultExecuteCommand.result != 0 || (str = commandResultExecuteCommand.successMsg) == null) {
            return null;
        }
        return str.trim();
    }

    @Override // com.cvte.dss.ups.mic.mic3308.IUpdOperation
    public String getOtaKey() {
        String str;
        ShellUtils.CommandResult commandResultExecuteCommand = ShellUtils.executeCommand(this.mUpdTool.getAbsolutePath() + getMicMsg() + " -g ota_key");
        if (commandResultExecuteCommand.result != 0 || (str = commandResultExecuteCommand.successMsg) == null) {
            return null;
        }
        return str.trim();
    }

    @Override // com.cvte.dss.ups.mic.mic3308.IUpdOperation
    public String getVersion() {
        String str;
        ShellUtils.CommandResult commandResultExecuteCommand = ShellUtils.executeCommand(this.mUpdTool.getAbsolutePath() + getMicMsg() + " -g full_version");
        if (commandResultExecuteCommand.result != 0 || (str = commandResultExecuteCommand.successMsg) == 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) {
        StringBuilder sb;
        String parent;
        File file = new File(str);
        if (!file.exists()) {
            iUpgradeStatusListener.onFailed("file not found");
            return Boolean.FALSE;
        }
        if (bool.booleanValue()) {
            sb = new StringBuilder();
            sb.append(this.mUpdTool.getAbsolutePath());
            sb.append(getMicMsg());
            sb.append(" -u ");
            sb.append(file);
            sb.append(" -w ");
            sb.append(this.mUpdTool.getParent());
            parent = " -f";
        } else {
            sb = new StringBuilder();
            sb.append(this.mUpdTool.getAbsolutePath());
            sb.append(getMicMsg());
            sb.append(" -u ");
            sb.append(file);
            sb.append(" -w ");
            parent = this.mUpdTool.getParent();
        }
        sb.append(parent);
        String string = sb.toString();
        Log.i(TAG, "doUpgrade: " + string);
        ShellUtils.executeCommand(string, new ShellUtils.OnCommandListener() { // from class: com.cvte.dss.ups.mic.mic3308.UpdTool.1
            public int retryCount = 0;

            @Override // com.cvte.udi.utils.ShellUtils.OnCommandListener
            public void onResult(String str2) {
                Log.i(UpdTool.TAG, "onResult: " + str2);
                if (str2.contains("failed") || str2.contains("not new")) {
                    iUpgradeStatusListener.onFailed(str2);
                    return;
                }
                String[] strArrSplit = str2.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 sbF = a.F("Exception: ");
                    sbF.append(e2.getMessage());
                    iUpgradeStatusListener2.onFailed(sbF.toString());
                }
            }
        });
        return Boolean.TRUE;
    }
}
