package com.seewo.ota.m;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import com.seewo.adbcommons.model.CmdOTAEvent;
import com.seewo.libmyousdk.model.RomUpdateInfo;
import com.seewo.ota.R;
import com.seewo.ota.m.r;
import f.z.c.u;

/* JADX INFO: compiled from: DefaultUpgradeDialog.kt */
/* JADX INFO: loaded from: classes.dex */
public final class r extends q {
    private final u<Context, Dialog, RomUpdateInfo, Integer, Boolean, String, Boolean, Dialog> l;
    private Dialog m;
    private View n;
    private TextView o;
    private final Handler p;
    private final a q;
    private final b r;

    /* JADX INFO: compiled from: DefaultUpgradeDialog.kt */
    public static final class a extends BroadcastReceiver {
        a() {
        }

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            f.z.d.k.e(context, "context");
            f.z.d.k.e(intent, "intent");
            r.this.p();
            r.this.dismiss();
        }
    }

    /* JADX INFO: compiled from: DefaultUpgradeDialog.kt */
    public static final class b implements com.seewo.ota.v.u.s {
        b() {
        }

        /* JADX INFO: Access modifiers changed from: private */
        public static final void e(r rVar, int i2) {
            f.z.d.k.e(rVar, "this$0");
            rVar.A(i2);
        }

        @Override // com.seewo.ota.v.u.s
        public void a(int i2) {
        }

        @Override // com.seewo.ota.v.u.s
        public void b() {
        }

        @Override // com.seewo.ota.v.u.s
        public void c(int i2, Object obj) {
            r.this.dismiss();
        }

        @Override // com.seewo.ota.v.u.s, android.os.RecoverySystem.ProgressListener
        public void onProgress(final int i2) {
            Handler handler = r.this.p;
            final r rVar = r.this;
            handler.post(new Runnable() { // from class: com.seewo.ota.m.f
                @Override // java.lang.Runnable
                public final void run() {
                    r.b.e(rVar, i2);
                }
            });
        }
    }

    /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
    /* JADX WARN: Multi-variable type inference failed */
    public r(Context context, RomUpdateInfo romUpdateInfo, int i2, boolean z, String str, boolean z2, u<? super Context, ? super Dialog, ? super RomUpdateInfo, ? super Integer, ? super Boolean, ? super String, ? super Boolean, ? extends Dialog> uVar) {
        super(context, romUpdateInfo, i2, z, str, z2);
        f.z.d.k.e(context, "context");
        f.z.d.k.e(romUpdateInfo, "mUpdateFile");
        f.z.d.k.e(str, "mUpdateSrcPath");
        f.z.d.k.e(uVar, "createUpdateConfirmDialog");
        this.l = uVar;
        this.p = new Handler(Looper.getMainLooper());
        this.q = new a();
        this.r = new b();
        q();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void A(int i2) {
        Log.d("DefaultUpgradeDialog", "updateProgress. " + i2);
        TextView textView = this.o;
        if (textView == null) {
            f.z.d.k.p("mProgressText");
            throw null;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(i2);
        sb.append('%');
        textView.setText(sb.toString());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void p() {
        Dialog dialog = this.m;
        if (dialog != null) {
            f.z.d.k.c(dialog);
            if (dialog.isShowing()) {
                Dialog dialog2 = this.m;
                f.z.d.k.c(dialog2);
                dialog2.dismiss();
            }
        }
    }

    private final void q() {
        if ((d().isForceUpdate() && getContext().getResources().getBoolean(R.bool.support_force_update)) || b()) {
            show();
        } else {
            w();
        }
    }

    private final void r() {
        u<Context, Dialog, RomUpdateInfo, Integer, Boolean, String, Boolean, Dialog> uVar = this.l;
        Context context = getContext();
        f.z.d.k.d(context, "context");
        Dialog dialogD = uVar.d(context, this, d(), Integer.valueOf(e()), Boolean.valueOf(b()), f(), Boolean.valueOf(c()));
        this.m = dialogD;
        f.z.d.k.c(dialogD);
        dialogD.getWindow().setType(2003);
        Dialog dialog = this.m;
        f.z.d.k.c(dialog);
        dialog.setCanceledOnTouchOutside(false);
        Dialog dialog2 = this.m;
        f.z.d.k.c(dialog2);
        dialog2.setOnKeyListener(new DialogInterface.OnKeyListener() { // from class: com.seewo.ota.m.g
            @Override // android.content.DialogInterface.OnKeyListener
            public final boolean onKey(DialogInterface dialogInterface, int i2, KeyEvent keyEvent) {
                return r.s(this.f2163b, dialogInterface, i2, keyEvent);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final boolean s(r rVar, DialogInterface dialogInterface, int i2, KeyEvent keyEvent) {
        f.z.d.k.e(rVar, "this$0");
        if (i2 != 4) {
            return false;
        }
        dialogInterface.dismiss();
        rVar.dismiss();
        return true;
    }

    private final void u() {
        CmdOTAEvent cmdOTAEvent = new CmdOTAEvent();
        cmdOTAEvent.mEventCode = 1;
        cmdOTAEvent.mOTAPath = g();
        cmdOTAEvent.mOTAMD5 = d().getFileMd5();
        d.e.a.a.g().j(cmdOTAEvent);
    }

    private final void v() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.seewo.ota.checkstatus");
        getContext().registerReceiver(this.q, intentFilter);
    }

    private final void w() {
        if (this.m == null) {
            r();
        }
        Dialog dialog = this.m;
        f.z.d.k.c(dialog);
        if (dialog.isShowing()) {
            return;
        }
        Dialog dialog2 = this.m;
        f.z.d.k.c(dialog2);
        dialog2.show();
    }

    private final void x() {
    }

    private final void y() {
        View view = this.n;
        if (view == null) {
            f.z.d.k.p("mProgressbar");
            throw null;
        }
        view.setVisibility(0);
        TextView textView = this.o;
        if (textView == null) {
            f.z.d.k.p("mProgressText");
            throw null;
        }
        textView.setVisibility(0);
        x();
    }

    private final void z() {
        getContext().unregisterReceiver(this.q);
    }

    @Override // com.seewo.ota.m.q
    public int h() {
        return R.layout.sys_upgrade_dialog;
    }

    @Override // com.seewo.ota.m.q
    public void l(boolean z) {
        super.l(z);
        if (e() != 1 || !com.seewo.ota.util.h.a()) {
            com.seewo.ota.v.u.r.d().n(g(), false, c());
        } else {
            d.e.e.d.g().f(d.e.e.e.a.d().a(), Boolean.valueOf(z)).J(Boolean.FALSE).N();
            u();
        }
    }

    @Override // android.app.Dialog, android.view.Window.Callback
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        v();
        com.seewo.ota.v.u.r.d().l("DefaultUpgradeDialog", this.r);
        y();
        k(true);
    }

    @Override // com.seewo.ota.m.q, android.app.Dialog
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.n = findViewById(R.id.progressbar);
        this.o = (TextView) findViewById(R.id.progress_text);
    }

    @Override // android.app.Dialog, android.view.Window.Callback
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        p();
        com.seewo.ota.v.u.r.d().m("DefaultUpgradeDialog");
        z();
    }
}
