package o;

import android.content.Context;
import android.database.Cursor;
import androidx.work.impl.WorkDatabase;
import androidx.work.impl.WorkDatabase_Impl;
import java.util.ArrayList;
import java.util.LinkedList;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class Lm0 implements Runnable {
    public static final /* synthetic */ int C = 0;
    public final Context l;
    public final String m;
    public final Dm0 n;

    /* renamed from: o, reason: collision with root package name */
    public AbstractC0995dM f214o;
    public final C2062oc0 p;
    public final C1414hl r;
    public final BP s;
    public final XY t;
    public final WorkDatabase u;
    public final Em0 v;
    public final C3040yr w;
    public final ArrayList x;
    public String y;
    public AbstractC0899cM q = new ZL();
    public final C2595u70 z = new C2595u70();
    public final C2595u70 A = new C2595u70();
    public volatile int B = -256;

    static {
        C0669Zu.l("WorkerWrapper");
    }

    public Lm0(C0450Rj c0450Rj) {
        this.l = (Context) c0450Rj.l;
        this.p = (C2062oc0) c0450Rj.n;
        this.t = (XY) c0450Rj.m;
        Dm0 dm0 = (Dm0) c0450Rj.q;
        this.n = dm0;
        this.m = dm0.a;
        this.f214o = null;
        C1414hl c1414hl = (C1414hl) c0450Rj.f317o;
        this.r = c1414hl;
        this.s = (BP) c1414hl.f;
        WorkDatabase workDatabase = (WorkDatabase) c0450Rj.p;
        this.u = workDatabase;
        this.v = workDatabase.s();
        this.w = workDatabase.f();
        this.x = (ArrayList) c0450Rj.r;
    }

    public final void a(AbstractC0899cM abstractC0899cM) {
        boolean z = abstractC0899cM instanceof C0804bM;
        Dm0 dm0 = this.n;
        if (!z) {
            if (abstractC0899cM instanceof C0708aM) {
                C0669Zu.e().getClass();
                c();
                return;
            }
            C0669Zu.e().getClass();
            if (dm0.c()) {
                d();
                return;
            } else {
                g();
                return;
            }
        }
        C0669Zu.e().getClass();
        if (dm0.c()) {
            d();
            return;
        }
        C3040yr c3040yr = this.w;
        String str = this.m;
        Em0 em0 = this.v;
        WorkDatabase workDatabase = this.u;
        workDatabase.c();
        try {
            em0.m(3, str);
            em0.l(str, ((C0804bM) this.q).a);
            this.s.getClass();
            long jCurrentTimeMillis = System.currentTimeMillis();
            ArrayList arrayListC = c3040yr.c(str);
            int size = arrayListC.size();
            int i = 0;
            while (i < size) {
                Object obj = arrayListC.get(i);
                i++;
                String str2 = (String) obj;
                if (em0.f(str2) == 5) {
                    WorkDatabase_Impl workDatabase_Impl = (WorkDatabase_Impl) c3040yr.m;
                    C1449i30 c1449i30D = C1449i30.d(1, "SELECT COUNT(*)=0 FROM dependency WHERE work_spec_id=? AND prerequisite_id IN (SELECT id FROM workspec WHERE state!=2)");
                    if (str2 == null) {
                        c1449i30D.r(1);
                    } else {
                        c1449i30D.k(1, str2);
                    }
                    workDatabase_Impl.b();
                    Cursor cursorL = workDatabase_Impl.l(c1449i30D);
                    try {
                        if (cursorL.moveToFirst() && cursorL.getInt(0) != 0) {
                            C0669Zu.e().getClass();
                            em0.m(1, str2);
                            em0.k(str2, jCurrentTimeMillis);
                        }
                    } finally {
                        cursorL.close();
                        c1449i30D.release();
                    }
                }
            }
            workDatabase.n();
            workDatabase.j();
            e(false);
        } catch (Throwable th) {
            workDatabase.j();
            e(false);
            throw th;
        }
    }

    public final void b() {
        if (h()) {
            return;
        }
        this.u.c();
        try {
            int iF = this.v.f(this.m);
            C3126zm0 c3126zm0R = this.u.r();
            String str = this.m;
            WorkDatabase_Impl workDatabase_Impl = (WorkDatabase_Impl) c3126zm0R.m;
            workDatabase_Impl.b();
            C1967nc0 c1967nc0 = (C1967nc0) c3126zm0R.n;
            LC lcA = c1967nc0.a();
            if (str == null) {
                lcA.r(1);
            } else {
                lcA.k(1, str);
            }
            workDatabase_Impl.c();
            try {
                lcA.b();
                workDatabase_Impl.n();
                if (iF == 0) {
                    e(false);
                } else if (iF == 2) {
                    a(this.q);
                } else if (!Ag0.a(iF)) {
                    this.B = -512;
                    c();
                }
                this.u.n();
                this.u.j();
            } finally {
                workDatabase_Impl.j();
                c1967nc0.c(lcA);
            }
        } catch (Throwable th) {
            this.u.j();
            throw th;
        }
    }

    public final void c() {
        String str = this.m;
        Em0 em0 = this.v;
        WorkDatabase workDatabase = this.u;
        workDatabase.c();
        try {
            em0.m(1, str);
            this.s.getClass();
            em0.k(str, System.currentTimeMillis());
            em0.j(this.n.v, str);
            em0.i(str, -1L);
            workDatabase.n();
        } finally {
            workDatabase.j();
            e(true);
        }
    }

    public final void d() {
        String str = this.m;
        Em0 em0 = this.v;
        WorkDatabase workDatabase = this.u;
        workDatabase.c();
        try {
            this.s.getClass();
            em0.k(str, System.currentTimeMillis());
            WorkDatabase_Impl workDatabase_Impl = em0.a;
            em0.m(1, str);
            workDatabase_Impl.b();
            C1967nc0 c1967nc0 = em0.j;
            LC lcA = c1967nc0.a();
            if (str == null) {
                lcA.r(1);
            } else {
                lcA.k(1, str);
            }
            workDatabase_Impl.c();
            try {
                lcA.b();
                workDatabase_Impl.n();
                workDatabase_Impl.j();
                c1967nc0.c(lcA);
                em0.j(this.n.v, str);
                workDatabase_Impl.b();
                C1967nc0 c1967nc02 = em0.f;
                LC lcA2 = c1967nc02.a();
                if (str == null) {
                    lcA2.r(1);
                } else {
                    lcA2.k(1, str);
                }
                workDatabase_Impl.c();
                try {
                    lcA2.b();
                    workDatabase_Impl.n();
                    workDatabase_Impl.j();
                    c1967nc02.c(lcA2);
                    em0.i(str, -1L);
                    workDatabase.n();
                } catch (Throwable th) {
                    workDatabase_Impl.j();
                    c1967nc02.c(lcA2);
                    throw th;
                }
            } catch (Throwable th2) {
                workDatabase_Impl.j();
                c1967nc0.c(lcA);
                throw th2;
            }
        } finally {
            workDatabase.j();
            e(false);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:11:0x002f  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final void e(boolean r6) {
        /*
            r5 = this;
            androidx.work.impl.WorkDatabase r0 = r5.u
            r0.c()
            androidx.work.impl.WorkDatabase r0 = r5.u     // Catch: java.lang.Throwable -> L40
            o.Em0 r0 = r0.s()     // Catch: java.lang.Throwable -> L40
            r0.getClass()     // Catch: java.lang.Throwable -> L40
            java.lang.String r1 = "SELECT COUNT(*) > 0 FROM workspec WHERE state NOT IN (2, 3, 5) LIMIT 1"
            r2 = 0
            o.i30 r1 = o.C1449i30.d(r2, r1)     // Catch: java.lang.Throwable -> L40
            androidx.work.impl.WorkDatabase_Impl r0 = r0.a     // Catch: java.lang.Throwable -> L40
            r0.b()     // Catch: java.lang.Throwable -> L40
            android.database.Cursor r0 = r0.l(r1)     // Catch: java.lang.Throwable -> L40
            boolean r3 = r0.moveToFirst()     // Catch: java.lang.Throwable -> L2d
            r4 = 1
            if (r3 == 0) goto L2f
            int r3 = r0.getInt(r2)     // Catch: java.lang.Throwable -> L2d
            if (r3 == 0) goto L2f
            r3 = r4
            goto L30
        L2d:
            r6 = move-exception
            goto L71
        L2f:
            r3 = r2
        L30:
            r0.close()     // Catch: java.lang.Throwable -> L40
            r1.release()     // Catch: java.lang.Throwable -> L40
            if (r3 != 0) goto L42
            android.content.Context r0 = r5.l     // Catch: java.lang.Throwable -> L40
            java.lang.Class<androidx.work.impl.background.systemalarm.RescheduleReceiver> r1 = androidx.work.impl.background.systemalarm.RescheduleReceiver.class
            o.AbstractC1862mV.a(r0, r1, r2)     // Catch: java.lang.Throwable -> L40
            goto L42
        L40:
            r6 = move-exception
            goto L78
        L42:
            if (r6 == 0) goto L5d
            o.Em0 r0 = r5.v     // Catch: java.lang.Throwable -> L40
            java.lang.String r1 = r5.m     // Catch: java.lang.Throwable -> L40
            r0.m(r4, r1)     // Catch: java.lang.Throwable -> L40
            o.Em0 r0 = r5.v     // Catch: java.lang.Throwable -> L40
            java.lang.String r1 = r5.m     // Catch: java.lang.Throwable -> L40
            int r2 = r5.B     // Catch: java.lang.Throwable -> L40
            r0.n(r2, r1)     // Catch: java.lang.Throwable -> L40
            o.Em0 r0 = r5.v     // Catch: java.lang.Throwable -> L40
            java.lang.String r1 = r5.m     // Catch: java.lang.Throwable -> L40
            r2 = -1
            r0.i(r1, r2)     // Catch: java.lang.Throwable -> L40
        L5d:
            androidx.work.impl.WorkDatabase r0 = r5.u     // Catch: java.lang.Throwable -> L40
            r0.n()     // Catch: java.lang.Throwable -> L40
            androidx.work.impl.WorkDatabase r0 = r5.u
            r0.j()
            o.u70 r0 = r5.z
            java.lang.Boolean r6 = java.lang.Boolean.valueOf(r6)
            r0.i(r6)
            return
        L71:
            r0.close()     // Catch: java.lang.Throwable -> L40
            r1.release()     // Catch: java.lang.Throwable -> L40
            throw r6     // Catch: java.lang.Throwable -> L40
        L78:
            androidx.work.impl.WorkDatabase r0 = r5.u
            r0.j()
            throw r6
        */
        throw new UnsupportedOperationException("Method not decompiled: o.Lm0.e(boolean):void");
    }

    public final void f() {
        if (this.v.f(this.m) == 2) {
            C0669Zu.e().getClass();
            e(true);
        } else {
            C0669Zu.e().getClass();
            e(false);
        }
    }

    public final void g() {
        String str = this.m;
        WorkDatabase workDatabase = this.u;
        workDatabase.c();
        try {
            LinkedList linkedList = new LinkedList();
            linkedList.add(str);
            while (true) {
                boolean zIsEmpty = linkedList.isEmpty();
                Em0 em0 = this.v;
                if (zIsEmpty) {
                    C0585Wo c0585Wo = ((ZL) this.q).a;
                    em0.j(this.n.v, str);
                    em0.l(str, c0585Wo);
                    workDatabase.n();
                    return;
                }
                String str2 = (String) linkedList.remove();
                if (em0.f(str2) != 6) {
                    em0.m(4, str2);
                }
                linkedList.addAll(this.w.c(str2));
            }
        } finally {
            workDatabase.j();
            e(false);
        }
    }

    public final boolean h() {
        if (this.B == -256) {
            return false;
        }
        C0669Zu.e().getClass();
        if (this.v.f(this.m) == 0) {
            e(false);
            return true;
        }
        e(!Ag0.a(r0));
        return true;
    }

    /* JADX WARN: Removed duplicated region for block: B:30:0x007b A[Catch: all -> 0x0065, TryCatch #1 {all -> 0x0065, blocks: (B:13:0x004e, B:16:0x0054, B:21:0x0068, B:23:0x006e, B:25:0x0072, B:35:0x009d, B:30:0x007b, B:32:0x008c), top: B:104:0x004e }] */
    @Override // java.lang.Runnable
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final void run() {
        /*
            Method dump skipped, instructions count: 578
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: o.Lm0.run():void");
    }
}
