package com.ifpdos.debugmenu.action;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import c.n.a;
import com.ifpdos.debugmenu.DebugMenuApplication;
import com.ifpdos.debugmenu.R;
import com.ifpdos.debugmenu.action.base.ItemButtonAction;
import com.ifpdos.debugmenu.entity.UpgradeException;
import com.seewo.sdk.model.SDKUpgradeResult;
import com.trello.rxlifecycle3.android.ActivityEvent;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;
import d.f.a.c1;
import d.f.a.p2.p0;
import d.f.a.p2.t0;
import d.f.a.r2.f3;
import d.f.a.r2.t2;
import d.f.a.r2.v2;
import d.f.a.s1.e;
import f.a.v.c;
import f.a.v.d;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public class UpgradeTeaAction extends ItemButtonAction implements c1 {
    private static final String TAG = "UpgradeTeaAction";
    private e mActionFinishCallback;
    private v2 mFindFrontUSBHelper;
    private p0 upgradeHandler;

    public UpgradeTeaAction(Context context) {
        super(context);
        this.upgradeHandler = t0.a;
        this.mContext = context;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void showConfirmDialog() {
        a.V(R.string.str_debug_upgrade, R.string.str_debug_upgrade_tea_hint, new e() { // from class: d.f.a.p1.mf
            @Override // d.f.a.s1.e
            public final void call() {
                this.a.j();
            }
        });
    }

    @SuppressLint({"CheckResult"})
    private void upgrade() {
        t2.e();
        a.a0(R.string.str_debug_upgrade_tea);
        final Context context = this.mContext;
        final p0 p0Var = this.upgradeHandler;
        final String str = "";
        f3.a(p0Var != null, SDKUpgradeResult.INVALID_MACHINE).k(new d() { // from class: d.f.a.r2.j0
            @Override // f.a.v.d
            public final Object apply(Object obj) {
                d.f.a.p2.p0 p0Var2 = p0Var;
                String str2 = str;
                Objects.requireNonNull((d.f.a.p2.t0) p0Var2);
                return f3.a(TextUtils.isEmpty(str2) ? d.f.a.p2.r0.c("FirmwareUpgrade.*\\.zip") : d.b.a.a.a.a0(str2), SDKUpgradeResult.BIN_NOT_FOUND);
            }
        }).k(new d() { // from class: d.f.a.r2.n0
            /* JADX WARN: Removed duplicated region for block: B:18:0x006d  */
            /* JADX WARN: Removed duplicated region for block: B:19:0x0074  */
            @Override // f.a.v.d
            /*
                Code decompiled incorrectly, please refer to instructions dump.
                To view partially-correct add '--show-bad-code' argument
            */
            public final java.lang.Object apply(java.lang.Object r8) {
                /*
                    r7 = this;
                    d.f.a.p2.p0 r0 = r1
                    android.content.Context r1 = r2
                    java.lang.String r2 = r3
                    java.lang.Boolean r8 = (java.lang.Boolean) r8
                    d.f.a.p2.t0 r0 = (d.f.a.p2.t0) r0
                    java.util.Objects.requireNonNull(r0)
                    java.lang.String r8 = "UpgradeHisiHelper"
                    boolean r0 = android.text.TextUtils.isEmpty(r2)
                    if (r0 == 0) goto L1b
                    java.lang.String r0 = "FirmwareUpgrade.*\\.zip"
                    java.lang.String r2 = d.f.a.r2.u2.p(r0)
                L1b:
                    java.io.File r0 = new java.io.File
                    r0.<init>(r2)
                    r3 = 8192(0x2000, float:1.148E-41)
                    byte[] r3 = new byte[r3]     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    java.lang.String r4 = "MD5"
                    java.security.MessageDigest r4 = java.security.MessageDigest.getInstance(r4)     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    java.lang.StringBuilder r5 = new java.lang.StringBuilder     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    r5.<init>()     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    java.lang.String r6 = "upgradeTea file exists = "
                    r5.append(r6)     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    boolean r6 = r0.exists()     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    r5.append(r6)     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    java.lang.String r5 = r5.toString()     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    android.util.Log.i(r8, r5)     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    java.io.FileInputStream r5 = new java.io.FileInputStream     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    r5.<init>(r0)     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                L47:
                    int r0 = r5.read(r3)     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    r6 = -1
                    if (r0 == r6) goto L53
                    r6 = 0
                    r4.update(r3, r6, r0)     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    goto L47
                L53:
                    r5.close()     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    byte[] r0 = r4.digest()     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    java.math.BigInteger r3 = new java.math.BigInteger     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    r4 = 1
                    r3.<init>(r4, r0)     // Catch: java.io.IOException -> L61 java.security.NoSuchAlgorithmException -> L66
                    goto L6b
                L61:
                    r0 = move-exception
                    r0.printStackTrace()
                    goto L6a
                L66:
                    r0 = move-exception
                    r0.printStackTrace()
                L6a:
                    r3 = 0
                L6b:
                    if (r3 == 0) goto L74
                    r0 = 16
                    java.lang.String r0 = r3.toString(r0)
                    goto L76
                L74:
                    java.lang.String r0 = ""
                L76:
                    java.lang.StringBuilder r3 = new java.lang.StringBuilder
                    r3.<init>()
                    java.lang.String r4 = "upgradeTea path = "
                    r3.append(r4)
                    r3.append(r2)
                    java.lang.String r4 = " md5 = "
                    r3.append(r4)
                    r3.append(r0)
                    java.lang.String r0 = r3.toString()
                    android.util.Log.i(r8, r0)
                    d.f.a.p2.i r8 = new d.f.a.p2.i
                    r8.<init>()
                    f.a.w.e.d.c r0 = new f.a.w.e.d.c
                    r0.<init>(r8)
                    return r0
                */
                throw new UnsupportedOperationException("Method not decompiled: d.f.a.r2.n0.apply(java.lang.Object):java.lang.Object");
            }
        }).w(f.a.z.a.f4044c).g(((RxAppCompatActivity) this.mContext).bindUntilEvent(ActivityEvent.DESTROY)).u(new c() { // from class: d.f.a.p1.qf
            @Override // f.a.v.c
            public final void accept(Object obj) {
                if (d.b.a.a.a.Z((Boolean) obj, R.string.str_debug_upgrade_tea)) {
                    c.n.a.N(2000L);
                }
            }
        }, new c() { // from class: d.f.a.p1.lf
            @Override // f.a.v.c
            public final void accept(Object obj) {
                Throwable th = (Throwable) obj;
                c.n.a.f0(R.string.str_debug_upgrade_tea, false);
                if (th instanceof UpgradeException) {
                    UpgradeException upgradeException = (UpgradeException) th;
                    if (upgradeException.getErrorReason() != SDKUpgradeResult.BIN_NOT_FOUND && upgradeException.getErrorReason() != SDKUpgradeResult.SUCCESS) {
                        d.f.a.r2.t2.c(new d.f.a.s1.e() { // from class: d.f.a.p1.pf
                            @Override // d.f.a.s1.e
                            public final void call() {
                            }
                        });
                        c.n.a.Y("Tea", upgradeException.getErrorReason().name());
                    }
                    d.b.a.a.a.L(upgradeException, DebugMenuApplication.f1956b, "Tea");
                }
            }
        }, f.a.w.b.a.f3725b, f.a.w.b.a.f3726c);
    }

    private void upgradeCheckUSB() {
        a.Z(true);
        v2 v2Var = new v2(new v2.a() { // from class: com.ifpdos.debugmenu.action.UpgradeTeaAction.1
            @Override // d.f.a.r2.v2.a
            public void onFail() {
                a.Z(false);
                d.b.a.a.a.J(R.string.str_export_log_fail_no_usb, k.c.a.c.c());
            }

            @Override // d.f.a.r2.v2.a
            public void onSuccess() {
                a.Z(false);
                UpgradeTeaAction.this.showConfirmDialog();
            }
        });
        this.mFindFrontUSBHelper = v2Var;
        v2Var.a();
    }

    public /* synthetic */ void h() {
        a.o();
        upgradeCheckUSB();
    }

    public /* synthetic */ void i(DialogInterface dialogInterface) {
        e eVar = this.mActionFinishCallback;
        if (eVar != null) {
            eVar.call();
        }
    }

    public /* synthetic */ void j() {
        a.o();
        upgrade();
    }

    @Override // com.ifpdos.debugmenu.action.base.ItemButtonAction, d.f.a.j2.e.e.a
    public void onClick(Context context, d.f.a.j2.e.e eVar) {
        upgradeCheckUSB();
    }

    @Override // com.ifpdos.debugmenu.action.base.ItemButtonAction, d.f.a.j2.e.e.a
    public void onLongClick(Context context, d.f.a.j2.e.e eVar) {
    }

    @Override // d.f.a.c1
    public void quickAction(e eVar) {
        this.mActionFinishCallback = eVar;
        a.U(R.string.str_debug_upgrade, R.string.str_debug_upgrade_tea_hint, new e() { // from class: d.f.a.p1.nf
            @Override // d.f.a.s1.e
            public final void call() {
                this.a.h();
            }
        }, new DialogInterface.OnCancelListener() { // from class: d.f.a.p1.of
            @Override // android.content.DialogInterface.OnCancelListener
            public final void onCancel(DialogInterface dialogInterface) {
                this.f2855b.i(dialogInterface);
            }
        });
    }
}
