package com.seewo.ota.v;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import com.seewo.adbcommons.model.CmdOTAEvent;
import com.seewo.libmyousdk.model.IUpdateInfo;
import com.seewo.libmyousdk.model.RomUpdateInfo;
import com.seewo.ota.OTAApplication;
import com.seewo.ota.R;
import com.seewo.ota.m.s;
import com.seewo.ota.util.t;
import com.seewo.ota.v.u.r;
import d.e.h.a.b;
import java.util.Arrays;

/* JADX INFO: compiled from: BaseUpdateManager.java */
/* JADX INFO: loaded from: classes.dex */
public abstract class k implements d.e.e.f.a, d.e.e.f.b {
    private com.seewo.ota.s.c a;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    protected Context f2360c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    protected String f2361d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    protected String f2362e;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    protected RomUpdateInfo f2365h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private final com.seewo.ota.p.b f2366i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private long f2367j = 0;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private int f2368k = -1;
    private String l = "";

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    protected b.a f2359b = b.a.NONE;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    protected boolean f2364g = false;

    public k(Context context, com.seewo.ota.p.b bVar) {
        this.f2360c = context;
        this.f2366i = bVar;
    }

    private void I(int i2) {
        switch (i2) {
            case 0:
                Context context = this.f2360c;
                d.e.c.a.c.g(context, context.getString(R.string.download_error_no_space));
                break;
            case 1:
                Context context2 = this.f2360c;
                d.e.c.a.c.g(context2, context2.getString(R.string.download_error_md5_not_math));
                break;
            case 2:
                Context context3 = this.f2360c;
                d.e.c.a.c.g(context3, context3.getString(R.string.download_error_write));
                break;
            case 3:
                Context context4 = this.f2360c;
                d.e.c.a.c.g(context4, context4.getString(R.string.download_error_request));
                break;
            case 4:
                Context context5 = this.f2360c;
                d.e.c.a.c.g(context5, context5.getString(R.string.download_error_io));
                break;
            case 5:
                Context context6 = this.f2360c;
                d.e.c.a.c.g(context6, context6.getString(R.string.download_error_unknown));
                break;
            case 6:
                Context context7 = this.f2360c;
                d.e.c.a.c.g(context7, context7.getString(R.string.download_error_file));
                N();
                break;
        }
    }

    private void J(String str, final boolean z) {
        e.a.h.E(str).t(new e.a.t.e() { // from class: com.seewo.ota.v.b
            @Override // e.a.t.e
            public final boolean test(Object obj) {
                return this.f2347b.D(z, (String) obj);
            }
        }).g(this.a.a()).O(new e.a.t.c() { // from class: com.seewo.ota.v.a
            @Override // e.a.t.c
            public final void accept(Object obj) throws Exception {
                this.a.E((String) obj);
            }
        });
    }

    private void K() {
        if (com.seewo.ota.data.a.g().v()) {
            return;
        }
        com.seewo.ota.data.a.g().B();
        d.e.e.d.g().b(r(), com.seewo.vcommons.data.d.d(), d.e.g.h.a.c()).J(Boolean.FALSE).N();
    }

    @SuppressLint({"CheckResult"})
    private void L() {
        e.a.h.E(this.f2365h).F(new e.a.t.d() { // from class: com.seewo.ota.v.e
            @Override // e.a.t.d
            public final Object apply(Object obj) {
                return this.f2350b.F((RomUpdateInfo) obj);
            }
        }).g(this.a.a()).O(new e.a.t.c() { // from class: com.seewo.ota.v.c
            @Override // e.a.t.c
            public final void accept(Object obj) throws Exception {
                this.a.G((Boolean) obj);
            }
        });
    }

    private void k() {
        d.e.c.a.a.c(y());
        RomUpdateInfo romUpdateInfo = this.f2365h;
        if (romUpdateInfo == null || TextUtils.isEmpty(romUpdateInfo.getDownloadUrl())) {
            return;
        }
        d.e.e.b.f(this.f2365h, y());
    }

    private void l() {
        Log.d("BaseUpdateManager", "deleteOTAFile: ");
        k();
        if (this.f2363f == 1 && com.seewo.ota.util.h.a()) {
            CmdOTAEvent cmdOTAEvent = new CmdOTAEvent();
            cmdOTAEvent.mEventCode = 4;
            d.e.a.a.g().j(cmdOTAEvent);
        }
    }

    private void m(int i2) {
        this.f2368k = i2;
        this.f2359b = b.a.NONE;
        I(i2);
        com.seewo.ota.p.b bVar = this.f2366i;
        if (bVar != null) {
            bVar.i(this.f2363f);
        }
    }

    private void n(int i2) {
        this.f2359b = b.a.DOWNLOAD_PAUSED;
        com.seewo.ota.p.b bVar = this.f2366i;
        if (bVar != null) {
            bVar.h(this.f2363f, i2);
        }
    }

    private void o(int i2) {
        this.f2359b = b.a.DOWNLOADING;
        com.seewo.ota.p.b bVar = this.f2366i;
        if (bVar != null) {
            bVar.e(i2, this.f2363f);
        }
    }

    private void p(RomUpdateInfo romUpdateInfo, boolean z) {
        this.f2359b = b.a.DOWNLOADED;
        com.seewo.ota.p.b bVar = this.f2366i;
        if (bVar != null) {
            bVar.j(romUpdateInfo, this.f2363f, this.f2364g, z);
        }
    }

    private String w() {
        String strA;
        if (com.seewo.ota.util.h.a()) {
            strA = d.e.e.h.a.a();
        } else {
            strA = (!this.f2360c.getResources().getBoolean(R.bool.use_android_os_sn_number) || Build.VERSION.SDK_INT < 26) ? d.e.g.c.a.a() : Build.getSerial();
            if (TextUtils.isEmpty(strA)) {
                strA = d.e.e.h.a.a();
            }
        }
        Log.d("BaseUpdateManager", "getMachineSerialNumber: " + strA);
        return TextUtils.isEmpty(strA) ? com.seewo.vcommons.data.d.h() : strA;
    }

    protected String A(String str) {
        return str.length() == 14 ? str.substring(0, str.length() - 2) : str;
    }

    protected String B() {
        return this.f2363f == 1 ? com.seewo.ota.data.b.i().j() : com.seewo.ota.data.b.i().k();
    }

    public int C() {
        return this.f2359b.ordinal();
    }

    public /* synthetic */ boolean D(boolean z, String str) throws Exception {
        if (z || t.j(str, this.f2365h)) {
            return true;
        }
        k();
        m(6);
        return false;
    }

    public /* synthetic */ void E(String str) throws Exception {
        p(this.f2365h, true);
        if (this.f2360c.getResources().getBoolean(R.bool.support_auto_update_when_download_success) && com.seewo.vcommons.data.c.k(this.f2360c)) {
            p.r().B(this.f2363f);
        }
    }

    public /* synthetic */ Boolean F(RomUpdateInfo romUpdateInfo) throws Exception {
        long jH = d.e.e.b.h(this.f2365h, y());
        Log.d("BaseUpdateManager", "processHaveNewUpdate: " + jH + " " + this.f2365h.getFileSize());
        return jH == this.f2365h.getFileSize() ? Boolean.TRUE : Boolean.FALSE;
    }

    public /* synthetic */ void G(Boolean bool) throws Exception {
        this.f2359b = b.a.HAVE;
        if (bool.booleanValue()) {
            Log.d("BaseUpdateManager", "processHaveNewUpdate isDownloaded(isSelfCheck):" + this.f2364g);
            b(y());
        } else if (!this.f2365h.isSilentDownload() || !this.f2364g) {
            Log.d("BaseUpdateManager", "processHaveNewUpdate callback: " + this.f2364g);
            q(0);
        } else if (Arrays.asList(this.f2360c.getResources().getStringArray(R.array.banned_silent_download_list)).contains(this.l)) {
            Log.d("BaseUpdateManager", "processHaveNewUpdate isSilentDownload: banned silent download");
            q(0);
            return;
        } else {
            Log.d("BaseUpdateManager", "processHaveNewUpdate isSilentDownload:");
            d.e.e.d.g().d(r(), Boolean.TRUE).J(Boolean.FALSE).N();
            d.e.e.b.l(this.f2365h, y(), this);
        }
        this.l = "";
    }

    public /* synthetic */ Boolean H(Boolean bool) throws Exception {
        if (t.j(y(), this.f2365h)) {
            Log.d("BaseUpdateManager", "Don't need to download the file again, the upgrade file is already available locally.");
            J(y(), true);
            return Boolean.TRUE;
        }
        d.e.e.d.g().d(r(), Boolean.FALSE).J(Boolean.FALSE).N();
        d.e.e.b.l(this.f2365h, y(), this);
        return Boolean.TRUE;
    }

    public void M(String str) {
        this.l = str;
    }

    public void N() {
        Log.d("BaseUpdateManager", "startDownload: " + this.f2363f);
        if (this.f2365h == null) {
            Log.e("BaseUpdateManager", "startDownload: mRomUpdateInfo == null");
            return;
        }
        this.f2359b = b.a.DOWNLOADING;
        this.f2368k = -1;
        e.a.h.E(Boolean.TRUE).T(e.a.x.a.b()).F(new e.a.t.d() { // from class: com.seewo.ota.v.d
            @Override // e.a.t.d
            public final Object apply(Object obj) {
                return this.f2349b.H((Boolean) obj);
            }
        }).G(e.a.q.b.a.a()).N();
    }

    public void O() {
        s.f2178f.B(this.f2360c, z(), this.f2363f, B(), com.seewo.vcommons.data.c.k(this.f2360c), true);
    }

    public void P() {
        s.f2178f.B(this.f2360c, z(), this.f2363f, B(), true, true);
    }

    public void Q() {
        if (this.a == null) {
            this.a = new com.seewo.ota.s.c();
        }
        this.a.b();
        K();
    }

    public void R() {
        com.seewo.ota.s.c cVar = this.a;
        if (cVar != null) {
            cVar.f();
        }
    }

    public void S(boolean z) {
        r.d().n(B(), z, true);
    }

    @Override // d.e.e.f.a
    public void a(long j2, long j3) {
        if (j3 == 0) {
            Log.e("BaseUpdateManager", "onProgress: file size is 0.");
            return;
        }
        o((int) ((100 * j2) / j3));
        d.e.e.d.g().e(r(), Double.valueOf((j2 - this.f2367j) >> 10)).J(Boolean.FALSE).N();
        this.f2367j = j2;
    }

    @Override // d.e.e.f.a
    @SuppressLint({"CheckResult"})
    public void b(String str) {
        J(str, false);
    }

    @Override // d.e.e.f.a
    public void c(long j2) {
        this.f2359b = b.a.DOWNLOADING;
    }

    @Override // d.e.e.f.a
    public void d(int i2, String str) {
        Log.e("BaseUpdateManager", "onFailure: " + i2 + " " + str);
        m(i2);
    }

    @Override // d.e.e.f.a
    public void e() {
        this.f2359b = b.a.NONE;
    }

    @Override // d.e.e.f.a
    public void f() {
        Log.i("BaseUpdateManager", "onPause: " + v());
        n(v());
    }

    @Override // d.e.e.f.b
    public void g(int i2, IUpdateInfo iUpdateInfo) {
        if (com.seewo.ota.util.o.a.c()) {
            f.k<Integer, RomUpdateInfo> kVarD = com.seewo.ota.util.o.a.d();
            int iIntValue = kVarD.c().intValue();
            iUpdateInfo = kVarD.d();
            i2 = iIntValue;
        }
        if (com.seewo.vcommons.data.c.a(OTAApplication.b(), "KEY_DISABLE_SYSTEM_UPDATE", false)) {
            Log.d("BaseUpdateManager", "onUpdateReturned:system update is Disable.");
            this.f2365h = null;
            this.f2359b = b.a.NONE;
            q(1);
            return;
        }
        if (i2 == 0) {
            l();
            this.f2365h = null;
            this.f2359b = b.a.NONE;
            q(1);
            com.seewo.ota.r.g.i().e();
            com.seewo.ota.t.n.j().f();
            return;
        }
        if (i2 == 1) {
            this.f2365h = x((RomUpdateInfo) iUpdateInfo);
            L();
        } else if (i2 == 2 || i2 == 3) {
            this.f2365h = null;
            this.f2359b = b.a.NONE;
            q(2);
        }
    }

    public void h() {
        Log.d("BaseUpdateManager", "cancel: " + this.f2363f);
        RomUpdateInfo romUpdateInfo = this.f2365h;
        if (romUpdateInfo == null) {
            Log.e("BaseUpdateManager", "cancel: mRomUpdateInfo == null");
        } else {
            this.f2359b = b.a.NONE;
            d.e.e.b.i(romUpdateInfo, y());
        }
    }

    protected abstract void i();

    public void j(boolean z) {
        Log.d("BaseUpdateManager", "OTA checkUpdate, appKey:" + this.f2362e + "  ,srcVersion:" + this.f2361d + "  ,deviceMac:" + w() + "  ,deviceId:" + s() + "  , updateStatus:" + this.f2359b);
        if (this.f2359b != b.a.CHECKING) {
            if (z && !com.seewo.vcommons.data.c.a(this.f2360c, "KEY_AUTOMATIC_OTA_UPDATES", true)) {
                Log.d("BaseUpdateManager", "checkUpdate: disable automatic ota updates");
                return;
            }
            this.f2359b = b.a.CHECKING;
            this.f2364g = z;
            Log.d("BaseUpdateManager", "checkUpdate: " + this.f2363f + "  , isSelfCheck:" + z);
            d.e.e.d.g().c(r(), Boolean.valueOf(z)).J(Boolean.FALSE).N();
            d.e.e.b.d(t(), this, w(), r());
        }
    }

    protected void q(int i2) {
        com.seewo.ota.p.b bVar = this.f2366i;
        if (bVar != null) {
            bVar.g(i2, this.f2363f, this.f2365h, this.f2364g);
        }
    }

    protected abstract String r();

    protected abstract String s();

    protected abstract String t();

    protected int u() {
        return this.f2368k;
    }

    public int v() {
        if (z() == null) {
            Log.e("BaseUpdateManager", "getDownloadProcess: mRomUpdateInfo == null");
            return 0;
        }
        if (z().getFileSize() != 0) {
            return (int) ((d.e.e.b.h(z(), y()) * 100) / z().getFileSize());
        }
        Log.e("BaseUpdateManager", "onProgress: file size is 0.");
        return 0;
    }

    protected RomUpdateInfo x(RomUpdateInfo romUpdateInfo) {
        return new RomUpdateInfo(romUpdateInfo.getName(), romUpdateInfo.getPolicyId(), romUpdateInfo.isHasUpdate(), romUpdateInfo.getUpdateDesc(), romUpdateInfo.getDownloadUrl(), romUpdateInfo.getFileMd5(), romUpdateInfo.getFileSize(), romUpdateInfo.getVersionCode(), romUpdateInfo.isForceUpdate(), romUpdateInfo.isSilentDownload(), romUpdateInfo.isPopUp(), romUpdateInfo.getUpdateType());
    }

    protected abstract String y();

    public RomUpdateInfo z() {
        return this.f2365h;
    }
}
