package com.cvte.dss.ups.u3hub.hub;

import android.content.Context;
import android.util.Log;
import com.cvte.dss.ups.u3hub.hub.HubDeviceManager;
import com.cvte.dss.ups.u3hub.listener.OnUpgradeResultListener;
import com.cvte.dss.ups.u3hub.model.UpgradeResult;
import com.cvte.udi.utils.FileHelper;
import com.cvte.udi.utils.ZipUtils;
import java.io.File;
import java.util.Objects;
import o.a.e.a;
import o.a.g.b;
import o.a.h.e.a.d;
import o.a.h.e.a.g;

/* JADX INFO: loaded from: classes.dex */
public class HubDeviceManager extends BaseDevice {
    private static final String TAG = "HubDevice";
    private static final String UNZIP_DIR = "UsbUpgradeUnzipTmp";
    private static final String ZIP_SUFFIX = ".zip";
    private a mCompositeDisposable = new a();
    public File vl817 = null;
    public File vl103 = null;

    private void findFile(File file) {
        for (File file2 : file.listFiles()) {
            StringBuilder sbF = g.a.a.a.a.F("targetFile: ");
            sbF.append(file2.getName());
            Log.d(TAG, sbF.toString());
            if (file2.isDirectory()) {
                findFile(file2);
            } else if (file2.getName().startsWith("vl817")) {
                this.vl817 = file2;
            } else if (file2.getName().startsWith("vl103")) {
                this.vl103 = file2;
            }
        }
    }

    @Override // com.cvte.dss.ups.u3hub.hub.BaseDevice
    public void destroy() {
        this.mCompositeDisposable.f();
    }

    @Override // com.cvte.dss.ups.u3hub.hub.BaseDevice
    public String getVersion(Context context) {
        return HubUpgradeApi.INSTANCE.getVersion(context);
    }

    @Override // com.cvte.dss.ups.u3hub.hub.BaseDevice
    public UpgradeResult upgrade(Context context, String str) {
        boolean zStartUpgrade;
        String strT;
        if (str.endsWith(ZIP_SUFFIX)) {
            File file = new File(str);
            if (file.exists()) {
                File file2 = new File(file.getParent(), UNZIP_DIR);
                if (file2.exists()) {
                    FileHelper.deleteFolder(file2.getAbsolutePath());
                }
                if (!file2.mkdirs()) {
                    return UpgradeResult.IO_ERROR;
                }
                if (ZipUtils.unzip(file, file2)) {
                    findFile(file2);
                }
                File file3 = this.vl817;
                if (file3 == null || this.vl103 == null || !file3.exists() || !this.vl103.exists()) {
                    FileHelper.deleteFolder(file2.getAbsolutePath());
                    strT = "vl817 or vl103 upgrade file not exists";
                } else {
                    zStartUpgrade = HubUpgradeApi.INSTANCE.startUpgrade(context, this.vl817.getAbsolutePath(), this.vl103.getAbsolutePath());
                    Log.d(TAG, "upgradeResult: " + zStartUpgrade + " start delete dir" + file2.getAbsolutePath());
                    g.a.a.a.a.k0("deleteResult: ", FileHelper.deleteFolder(file2.getAbsolutePath()), TAG);
                }
            } else {
                strT = g.a.a.a.a.t(str, "not exists");
            }
            Log.d(TAG, strT);
            return UpgradeResult.PATH_ERROR;
        }
        String[] strArrSplit = str.split(",");
        if (strArrSplit.length == 2) {
            this.vl817 = new File(strArrSplit[0]);
            this.vl103 = new File(strArrSplit[1]);
        }
        File file4 = this.vl817;
        if (file4 == null || this.vl103 == null || !file4.exists() || !this.vl103.exists()) {
            return UpgradeResult.PATH_ERROR;
        }
        zStartUpgrade = HubUpgradeApi.INSTANCE.startUpgrade(context, this.vl817.getAbsolutePath(), this.vl103.getAbsolutePath());
        return zStartUpgrade ? UpgradeResult.SUCCESS : UpgradeResult.UNKNOWN_ERROR;
    }

    @Override // com.cvte.dss.ups.u3hub.hub.BaseDevice
    public void upgrade(final Context context, final String str, final OnUpgradeResultListener onUpgradeResultListener) {
        Boolean bool = Boolean.TRUE;
        Objects.requireNonNull(bool, "item is null");
        d dVar = new d(bool);
        o.a.d dVar2 = o.a.i.a.f6107b;
        Objects.requireNonNull(dVar2, "scheduler is null");
        this.mCompositeDisposable.c(new g(dVar, dVar2).a(new b() { // from class: g.b.a.a.d.a.c
            @Override // o.a.g.b
            public final void accept(Object obj) {
                HubDeviceManager hubDeviceManager = this.a;
                Context context2 = context;
                String str2 = str;
                OnUpgradeResultListener onUpgradeResultListener2 = onUpgradeResultListener;
                UpgradeResult upgradeResultUpgrade = hubDeviceManager.upgrade(context2, str2);
                if (onUpgradeResultListener2 != null) {
                    onUpgradeResultListener2.onUpgradeResult(upgradeResultUpgrade);
                } else {
                    Log.i("HubDevice", "onUpgradeResultListener is null");
                }
            }
        }).d(new b() { // from class: g.b.a.a.d.a.b
            @Override // o.a.g.b
            public final void accept(Object obj) {
            }
        }, new b() { // from class: g.b.a.a.d.a.a
            @Override // o.a.g.b
            public final void accept(Object obj) {
                OnUpgradeResultListener onUpgradeResultListener2 = onUpgradeResultListener;
                Log.e("HubDevice", "upgrade error", (Throwable) obj);
                onUpgradeResultListener2.onUpgradeResult(UpgradeResult.UNKNOWN_ERROR);
            }
        }));
    }
}
