package com.seewo.ota.t;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import com.google.gson.Gson;
import com.seewo.libvui.h.b;
import com.seewo.libvui.h.e;
import com.seewo.ota.OTARefreshService;
import com.seewo.ota.R;
import com.seewo.ota.activity.SchedulerOTADialog;
import com.seewo.ota.util.s;
import com.seewo.ota.v.p;
import d.e.h.a.b;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/* JADX INFO: compiled from: SchedulerUpdateManager.java */
/* JADX INFO: loaded from: classes.dex */
public class n implements DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {
    private static volatile n o;
    private Context a;

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private b f2266e;

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

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

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private com.seewo.libvui.h.b f2270i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private b.a f2271j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private e.a.r.b f2272k;
    private TextView l;
    private CountDownTimer m;
    private boolean n;

    /* JADX INFO: compiled from: SchedulerUpdateManager.java */
    class a extends CountDownTimer {
        a(long j2, long j3) {
            super(j2, j3);
        }

        @Override // android.os.CountDownTimer
        public void onFinish() {
            n.this.N(false);
            if (n.this.f2270i == null || !n.this.f2270i.isShowing()) {
                return;
            }
            n.this.f2270i.dismiss();
        }

        @Override // android.os.CountDownTimer
        public void onTick(long j2) {
            if (n.this.l != null) {
                n.this.l.setText(n.this.a.getString(R.string.scheduler_ota_countdown_tip, Long.valueOf(j2 / 1000)));
            }
        }
    }

    /* JADX INFO: compiled from: SchedulerUpdateManager.java */
    public interface b {
        void a(Calendar calendar);
    }

    /* JADX INFO: compiled from: SchedulerUpdateManager.java */
    public interface c {
        void a();
    }

    static {
        new Gson();
    }

    private n() {
    }

    private void D() {
        c cVar = this.f2267f;
        if (cVar != null) {
            cVar.a();
        }
    }

    private void E() {
        Log.d("SchedulerUpdateManager", "powerOffAfterSchedulerOTA");
        s.a().c(this.a, R.string.scheduler_ota_power_off_tip);
        d.e.g.g.a.c(60);
    }

    private void M() {
        Calendar calendar = Calendar.getInstance();
        final TimePickerDialog timePickerDialog = new TimePickerDialog(this.a, R.style.OTATimePickerTheme, this, calendar.get(11), calendar.get(12), DateFormat.is24HourFormat(this.a));
        timePickerDialog.getWindow().setType(2003);
        timePickerDialog.show();
        Button button = timePickerDialog.getButton(-1);
        if (button != null) {
            button.setOnClickListener(new View.OnClickListener() { // from class: com.seewo.ota.t.c
                @Override // android.view.View.OnClickListener
                public final void onClick(View view) {
                    this.f2252b.y(timePickerDialog, view);
                }
            });
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void N(boolean z) {
        Log.d("SchedulerUpdateManager", "startScheduleOtaUpdate for schedule update");
        if (!d.e.h.e.a.l(this.a, OTARefreshService.class)) {
            this.a.startService(new Intent(this.a, (Class<?>) OTARefreshService.class));
        }
        F(z);
        if (z) {
            p.r().A(1);
            return;
        }
        if (this.f2269h == null) {
            this.f2269h = new SchedulerOTADialog(this.a);
        }
        if (this.f2269h.isShowing()) {
            return;
        }
        this.f2269h.show();
    }

    private void g() {
        boolean zY = com.seewo.ota.data.a.g().y();
        Log.d("SchedulerUpdateManager", "checkSchedulerOtaPowerOff :needPowerOff->" + zY);
        if (zY) {
            com.seewo.ota.data.a.g().M(false);
            E();
        }
    }

    public static n j() {
        if (o == null) {
            synchronized (n.class) {
                if (o == null) {
                    o = new n();
                }
            }
        }
        return o;
    }

    private boolean o() {
        if (Build.VERSION.SDK_INT >= 33) {
            String strK = com.seewo.vcommons.data.d.k("ro.boot.wakeup.source");
            Log.d("SchedulerUpdateManager", "isRtcStartUpDevice: " + strK);
            return !TextUtils.isEmpty(strK) && strK.equals("wifi_rtc");
        }
        String strL = com.seewo.vcommons.data.d.l("persist.sys.wakeup.mode", "-1");
        Log.d("SchedulerUpdateManager", "isRtcStartUpDevice: " + strL);
        return strL.equals("2");
    }

    public /* synthetic */ Void B(Void r3) {
        I(-1L);
        return null;
    }

    public void C(Calendar calendar) {
        I(calendar.getTimeInMillis());
        Log.d("SchedulerUpdateManager", "onSchedulerSet:" + calendar.getTimeInMillis() + ", currentTime:" + Calendar.getInstance().getTimeInMillis());
        O();
    }

    public void F(boolean z) {
        this.n = z;
    }

    public void G(b bVar) {
        this.f2266e = bVar;
    }

    public void H(c cVar) {
        this.f2267f = cVar;
    }

    public void I(long j2) {
        Log.d("SchedulerUpdateManager", "setOtaUpdateSchedulerTime setRtcTime:" + j2);
        com.seewo.vcommons.data.c.m(this.a, "otaUpdateSchedulerTime", j2);
        Intent intent = new Intent("com.seewo.osservice.action.UPDATE_OTA_SCHEDULER_TIME");
        intent.addFlags(16777216);
        this.a.sendBroadcast(intent);
    }

    public void J() {
        Calendar calendar = Calendar.getInstance();
        final DatePickerDialog datePickerDialog = new DatePickerDialog(this.a, R.style.OTATimePickerTheme, this, calendar.get(1), calendar.get(2), calendar.get(5));
        datePickerDialog.getWindow().setType(2003);
        datePickerDialog.show();
        Button button = datePickerDialog.getButton(-1);
        if (button != null) {
            button.setOnClickListener(new View.OnClickListener() { // from class: com.seewo.ota.t.b
                @Override // android.view.View.OnClickListener
                public final void onClick(View view) {
                    this.f2250b.r(datePickerDialog, view);
                }
            });
        }
    }

    public void K() {
        L(null);
    }

    public void L(Function<Void, Void> function) {
        e.a.r.b bVar = this.f2272k;
        if (bVar != null) {
            bVar.dispose();
            this.f2272k = null;
        }
        if (p()) {
            Log.d("SchedulerUpdateManager", "showStartScheduleOTATipDialog: scheduler ota is running, ignore!");
            return;
        }
        Log.d("SchedulerUpdateManager", "showStartScheduleOTATipDialog");
        int iV = p.r().v(1);
        if (b.a.CHECKING.ordinal() == iV) {
            this.f2272k = e.a.h.W(10000L, TimeUnit.MILLISECONDS, e.a.x.a.b()).G(e.a.q.b.a.a()).P(new e.a.t.c() { // from class: com.seewo.ota.t.a
                @Override // e.a.t.c
                public final void accept(Object obj) throws Exception {
                    this.a.u((Long) obj);
                }
            }, new e.a.t.c() { // from class: com.seewo.ota.t.d
                @Override // e.a.t.c
                public final void accept(Object obj) {
                    Log.e("SchedulerUpdateManager", ((Throwable) obj).getMessage());
                }
            });
            return;
        }
        e.a aVar = new e.a(this.a.getApplicationContext());
        aVar.y(R.string.schedule_update_text);
        aVar.o(this.a.getString(R.string.scheduler_ota_countdown_tip, 60));
        aVar.t(new DialogInterface.OnDismissListener() { // from class: com.seewo.ota.t.g
            @Override // android.content.DialogInterface.OnDismissListener
            public final void onDismiss(DialogInterface dialogInterface) {
                this.f2256b.w(dialogInterface);
            }
        });
        aVar.r(R.string.update_now, new DialogInterface.OnClickListener() { // from class: com.seewo.ota.t.f
            @Override // android.content.DialogInterface.OnClickListener
            public final void onClick(DialogInterface dialogInterface, int i2) {
                this.f2255b.x(dialogInterface, i2);
            }
        });
        aVar.p(R.string.btn_cancel, new DialogInterface.OnClickListener() { // from class: com.seewo.ota.t.h
            @Override // android.content.DialogInterface.OnClickListener
            public final void onClick(DialogInterface dialogInterface, int i2) {
                this.f2257b.s(dialogInterface, i2);
            }
        });
        this.f2271j = aVar;
        if (b.a.DOWNLOADED.ordinal() == iV || b.a.NONE.ordinal() == iV) {
            this.f2271j.x(null, null);
        } else {
            this.f2271j.w(R.string.download_background_text, new DialogInterface.OnClickListener() { // from class: com.seewo.ota.t.k
                @Override // android.content.DialogInterface.OnClickListener
                public final void onClick(DialogInterface dialogInterface, int i2) {
                    this.f2258b.t(dialogInterface, i2);
                }
            });
        }
        com.seewo.libvui.h.b bVarA = this.f2271j.a();
        this.f2270i = bVarA;
        bVarA.setCancelable(false);
        this.f2270i.setCanceledOnTouchOutside(false);
        this.f2270i.getWindow().setType(2003);
        this.l = (TextView) this.f2270i.findViewById(R.id.message);
        this.m = new a(60000L, 1000L);
        this.f2270i.show();
        this.m.start();
        d.e.h.e.e.a("SchedulerUpdateManager", "showStartScheduleOTATipDialog: show dialog");
        if (function != null) {
            function.apply(null);
        }
    }

    public void O() {
        Log.d("SchedulerUpdateManager", "startScheduleTimer");
        g();
        e.a.r.b bVar = this.f2268g;
        if (bVar != null) {
            bVar.dispose();
        }
        long jK = k();
        Log.d("SchedulerUpdateManager", "startScheduleTimer:otaUpdateSchedulerTime->" + jK);
        if (jK <= 0) {
            Log.d("SchedulerUpdateManager", "has not scheduler ota task");
            return;
        }
        long timeInMillis = Calendar.getInstance().getTimeInMillis();
        if (jK > timeInMillis) {
            long j2 = jK - timeInMillis;
            Log.d("SchedulerUpdateManager", "startScheduleOtaUpdateTimer(ms):" + j2);
            this.f2268g = e.a.h.W(j2, TimeUnit.MILLISECONDS, e.a.x.a.b()).G(e.a.q.b.a.a()).P(new e.a.t.c() { // from class: com.seewo.ota.t.j
                @Override // e.a.t.c
                public final void accept(Object obj) throws Exception {
                    this.a.z((Long) obj);
                }
            }, new e.a.t.c() { // from class: com.seewo.ota.t.i
                @Override // e.a.t.c
                public final void accept(Object obj) {
                    Log.e("SchedulerUpdateManager", ((Throwable) obj).getMessage());
                }
            });
            return;
        }
        if (timeInMillis - jK > 120000 || SystemClock.uptimeMillis() > 120000) {
            Log.d("SchedulerUpdateManager", "timeout, invalid ota update scheduler. reset!");
            I(-1L);
            return;
        }
        Log.d("SchedulerUpdateManager", "startup and timeout, start ota update.");
        L(new Function() { // from class: com.seewo.ota.t.e
            @Override // java.util.function.Function
            public final Object apply(Object obj) {
                return this.f2254b.B((Void) obj);
            }
        });
        D();
        if (o()) {
            d.e.g.d.a.b(false);
            d.e.g.b.f3546b.b(true);
        }
    }

    public void e() {
        Log.d("SchedulerUpdateManager", "beforeSchedulerOtaUpdate");
        if (o()) {
            com.seewo.ota.data.a.g().M(true);
        }
    }

    public void f() {
        e.a.r.b bVar = this.f2268g;
        if (bVar != null && !bVar.isDisposed()) {
            this.f2268g.dispose();
        }
        I(-1L);
    }

    public void h() {
        Log.d("SchedulerUpdateManager", "failToSchedulerOtaUpdate");
        com.seewo.ota.data.a.g().M(false);
        if (o()) {
            s.a().c(this.a, R.string.scheduler_ota_failed_power_off_tip);
            d.e.g.g.a.c(60);
        }
    }

    public void i() {
        Log.d("SchedulerUpdateManager", "finishSchedulerOtaUpdate");
    }

    public long k() {
        return com.seewo.vcommons.data.c.b(this.a, "otaUpdateSchedulerTime", -1L);
    }

    public boolean l() {
        return k() > 0;
    }

    public void m(Context context) {
        this.a = context;
    }

    public boolean n() {
        return this.n;
    }

    @Override // android.app.DatePickerDialog.OnDateSetListener
    public void onDateSet(DatePicker datePicker, int i2, int i3, int i4) {
        Log.e("SchedulerUpdateManager", "onDateSet, year:" + i2 + "  month:" + i3 + "  day:" + i4);
        this.f2263b = i2;
        this.f2264c = i3;
        this.f2265d = i4;
        M();
    }

    @Override // android.app.TimePickerDialog.OnTimeSetListener
    public void onTimeSet(TimePicker timePicker, int i2, int i3) {
        Log.e("SchedulerUpdateManager", "onTimeSet, hourOfDay:" + i2 + "  minute:" + i3);
        Calendar calendar = Calendar.getInstance();
        calendar.set(this.f2263b, this.f2264c, this.f2265d, i2, i3, 0);
        C(calendar);
        b bVar = this.f2266e;
        if (bVar != null) {
            bVar.a(calendar);
        }
    }

    public boolean p() {
        com.seewo.libvui.h.b bVar;
        SchedulerOTADialog schedulerOTADialog = this.f2269h;
        return (schedulerOTADialog != null && schedulerOTADialog.isShowing()) || this.n || ((bVar = this.f2270i) != null && bVar.isShowing());
    }

    public /* synthetic */ Void q(Void r3) {
        I(-1L);
        return null;
    }

    public /* synthetic */ void r(DatePickerDialog datePickerDialog, View view) {
        DatePicker datePicker = datePickerDialog.getDatePicker();
        int year = datePicker.getYear();
        int month = datePicker.getMonth();
        int dayOfMonth = datePicker.getDayOfMonth();
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, dayOfMonth, 23, 59);
        Log.e("SchedulerUpdateManager", "" + calendar.getTimeInMillis() + "  " + Calendar.getInstance().getTimeInMillis());
        if (calendar.getTimeInMillis() <= Calendar.getInstance().getTimeInMillis()) {
            s.a().c(this.a, R.string.schedule_time_set_error);
        } else {
            datePickerDialog.dismiss();
            onDateSet(datePicker, year, month, dayOfMonth);
        }
    }

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

    public /* synthetic */ void t(DialogInterface dialogInterface, int i2) {
        this.f2270i.dismiss();
        N(true);
    }

    public /* synthetic */ void u(Long l) throws Exception {
        Log.d("SchedulerUpdateManager", "wait check update, timeout to check ota status and show dialog again!");
        K();
    }

    public /* synthetic */ void w(DialogInterface dialogInterface) {
        CountDownTimer countDownTimer = this.m;
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    public /* synthetic */ void x(DialogInterface dialogInterface, int i2) {
        this.f2270i.dismiss();
        N(false);
    }

    public /* synthetic */ void y(TimePickerDialog timePickerDialog, View view) {
        TimePicker timePicker = timePickerDialog.getTimePicker();
        int hour = timePicker.getHour();
        int minute = timePicker.getMinute();
        Calendar calendar = Calendar.getInstance();
        calendar.set(this.f2263b, this.f2264c, this.f2265d, hour, minute);
        Log.e("SchedulerUpdateManager", "" + calendar.getTimeInMillis() + "  " + Calendar.getInstance().getTimeInMillis());
        if (calendar.getTimeInMillis() <= Calendar.getInstance().getTimeInMillis() + 300000) {
            s.a().c(this.a, R.string.schedule_time_set_error);
        } else {
            timePickerDialog.dismiss();
            onTimeSet(timePicker, hour, minute);
        }
    }

    public /* synthetic */ void z(Long l) throws Exception {
        Log.i("SchedulerUpdateManager", "Timer to ota scheduler update.");
        L(new Function() { // from class: com.seewo.ota.t.l
            @Override // java.util.function.Function
            public final Object apply(Object obj) {
                return this.f2259b.q((Void) obj);
            }
        });
        D();
    }
}
