package com.seewo.ota.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.TypefaceSpan;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.seewo.libmyousdk.model.RomUpdateInfo;
import com.seewo.libvui.h.e;
import com.seewo.ota.R;
import com.seewo.ota.view.CircleProgressBar;
import d.e.h.a.b;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
public class DirectOTADialog extends com.seewo.ota.view.e implements com.seewo.ota.p.b {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final Handler f1919b;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private int f1920f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private Dialog f1921g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private int f1922h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private e.a.r.b f1923i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private int f1924j;

    @BindView(R.id.cancel)
    ImageView mCancel;

    @BindView(R.id.progressbar)
    CircleProgressBar mProgressBar;

    @BindView(R.id.progress_layout)
    FrameLayout mProgressLayout;

    @BindView(R.id.progress_text)
    TextView mProgressText;

    @BindView(R.id.upgrade_tip)
    TextView mUpgradeTip;

    public DirectOTADialog(Context context) {
        this(context, -1);
    }

    private void f() {
        e.a.r.b bVar = this.f1923i;
        if (bVar == null || bVar.isDisposed()) {
            return;
        }
        this.f1923i.dispose();
    }

    private void k() {
        this.f1919b.postDelayed(new z(this), 1500L);
    }

    private void l() {
        this.f1919b.postDelayed(new z(this), 1500L);
    }

    private void m() {
        int iV = com.seewo.ota.v.p.r().v(this.f1920f);
        Log.d("DirectOTADialog", "initCancelButton:" + iV);
        if (iV == b.a.DOWNLOADING.ordinal() || iV == b.a.HAVE.ordinal()) {
            this.mCancel.setVisibility(0);
            return;
        }
        this.mCancel.setVisibility(8);
        Dialog dialog = this.f1921g;
        if (dialog == null || !dialog.isShowing()) {
            return;
        }
        this.f1921g.dismiss();
    }

    private void s() {
        int i2 = this.f1922h + 1;
        this.f1922h = i2;
        if (i2 > 4) {
            k();
        } else {
            Log.d("DirectOTADialog", "OTA Check Update Failed. retry!!!!!!!!");
            this.f1923i = e.a.h.V(8000L, TimeUnit.MILLISECONDS).T(e.a.x.a.b()).P(new e.a.t.c() { // from class: com.seewo.ota.activity.u
                @Override // e.a.t.c
                public final void accept(Object obj) throws Exception {
                    this.a.n((Long) obj);
                }
            }, new e.a.t.c() { // from class: com.seewo.ota.activity.v
                @Override // e.a.t.c
                public final void accept(Object obj) throws Exception {
                    this.a.o((Throwable) obj);
                }
            });
        }
    }

    private void t() {
        Dialog dialog = this.f1921g;
        if (dialog == null || !dialog.isShowing()) {
            Log.d("DirectOTADialog", "showCancelUpgradeDialog");
            e.a aVar = new e.a(getContext().getApplicationContext());
            aVar.y(R.string.system_update);
            aVar.n(R.string.cancel_download_ota_file_tip);
            aVar.p(R.string.button_cancel, new DialogInterface.OnClickListener() { // from class: com.seewo.ota.activity.y
                @Override // android.content.DialogInterface.OnClickListener
                public final void onClick(DialogInterface dialogInterface, int i2) {
                    this.f2105b.p(dialogInterface, i2);
                }
            });
            aVar.r(R.string.download_background_text, new DialogInterface.OnClickListener() { // from class: com.seewo.ota.activity.w
                @Override // android.content.DialogInterface.OnClickListener
                public final void onClick(DialogInterface dialogInterface, int i2) {
                    this.f2097b.q(dialogInterface, i2);
                }
            });
            aVar.w(R.string.system_update_cancel, new DialogInterface.OnClickListener() { // from class: com.seewo.ota.activity.x
                @Override // android.content.DialogInterface.OnClickListener
                public final void onClick(DialogInterface dialogInterface, int i2) {
                    this.f2101b.r(dialogInterface, i2);
                }
            });
            com.seewo.libvui.h.b bVarA = aVar.a();
            this.f1921g = bVarA;
            bVarA.getWindow().setType(2003);
            this.f1921g.show();
        }
    }

    private void u(int i2) {
        Log.d("DirectOTADialog", "updateProgress. " + i2);
        int iMin = Math.min(Math.max(i2, 0), 100);
        SpannableString spannableString = new SpannableString(String.format(Locale.getDefault(), "%d%%", Integer.valueOf(iMin)));
        spannableString.setSpan(new AbsoluteSizeSpan(17, true), spannableString.length() - 1, spannableString.length(), 17);
        spannableString.setSpan(new TypefaceSpan("sans-serif"), spannableString.length() - 1, spannableString.length(), 17);
        this.mProgressText.setText(spannableString);
        this.mProgressBar.setProgress(iMin);
    }

    private void v() {
        Log.d("DirectOTADialog", "updateUIToDownload");
        this.f1924j = 1;
        u(0);
        m();
        this.mProgressLayout.setVisibility(0);
        this.mUpgradeTip.setText(R.string.schedule_update_download_tip);
    }

    private void w() {
        Log.d("DirectOTADialog", "updateUIToUpgrade");
        this.f1924j = 2;
        m();
        this.mProgressLayout.setVisibility(0);
        u(100);
        this.mUpgradeTip.setText(R.string.auto_upgrade_system_point);
    }

    @Override // com.seewo.ota.p.b
    public void a(int i2) {
        u(i2);
    }

    @Override // com.seewo.ota.p.b
    public void b() {
        if (this.f1924j != 2) {
            w();
        }
    }

    @Override // com.seewo.ota.p.b
    public void c(int i2, Object obj) {
        if (i2 != 5) {
            k();
        } else {
            l();
            u(100);
        }
    }

    @Override // com.seewo.ota.p.b
    public void d(int i2) {
        u(i2);
    }

    @Override // android.app.Dialog, android.content.DialogInterface
    public void dismiss() {
        Dialog dialog = this.f1921g;
        if (dialog != null && dialog.isShowing()) {
            this.f1921g.dismiss();
        }
        super.dismiss();
        com.seewo.ota.v.p.r().E(DirectOTADialog.class.getName());
    }

    @Override // com.seewo.ota.p.b
    public void e(int i2, int i3) {
        if (this.f1924j != 1) {
            v();
        } else {
            u(i2);
        }
    }

    @Override // com.seewo.ota.p.b
    public void g(int i2, int i3, RomUpdateInfo romUpdateInfo, boolean z) {
        f();
        if (i2 == 0) {
            v();
            com.seewo.ota.v.p.r().A(i3);
        } else if (2 == i2) {
            Log.e("DirectOTADialog", "OTA Check Update, but failed.");
            s();
        } else {
            Log.e("DirectOTADialog", "OTA Check Update, but not New Version");
            k();
        }
    }

    @Override // com.seewo.ota.p.b
    public void i(int i2) {
        Log.e("DirectOTADialog", "ota update download File Error");
        k();
    }

    @Override // com.seewo.ota.p.b
    public void j(RomUpdateInfo romUpdateInfo, int i2, boolean z, boolean z2) {
        w();
        com.seewo.ota.v.p.r().F(i2);
    }

    public /* synthetic */ void n(Long l) throws Exception {
        com.seewo.ota.v.p.r().l(this.f1920f, false);
    }

    public /* synthetic */ void o(Throwable th) throws Exception {
        k();
    }

    @OnClick({R.id.cancel})
    public void onCancelClick(View view) {
        t();
    }

    public /* synthetic */ void p(DialogInterface dialogInterface, int i2) {
        this.f1921g.dismiss();
    }

    public /* synthetic */ void q(DialogInterface dialogInterface, int i2) {
        dismiss();
        this.f1921g.dismiss();
    }

    public /* synthetic */ void r(DialogInterface dialogInterface, int i2) {
        com.seewo.ota.v.p.r().f(this.f1920f);
        dismiss();
        this.f1921g.dismiss();
    }

    @Override // android.app.Dialog
    public void show() {
        u(0);
        super.show();
        this.f1924j = 0;
        com.seewo.ota.v.p.r().y(DirectOTADialog.class.getName(), this);
        com.seewo.ota.v.p.r().l(this.f1920f, false);
    }

    public DirectOTADialog(Context context, int i2) {
        super(context, R.style.TransparentDialog);
        this.f1919b = new Handler(Looper.getMainLooper());
        this.f1922h = 0;
        setContentView(R.layout.schedule_upgrade_layout);
        ButterKnife.bind(this);
        getWindow().setType(2003);
        if (com.seewo.ota.data.b.i().q()) {
            this.f1920f = 1;
        } else {
            this.f1920f = 0;
        }
    }
}
