package a6;

import android.content.Context;
import androidx.work.ListenableWorker;
import androidx.work.WorkerParameters;
import androidx.work.impl.WorkDatabase;
import androidx.work.impl.background.systemalarm.RescheduleReceiver;
import h6.r;
import h6.s;
import h6.v;
import i6.m;
import i6.n;
import i6.o;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import z5.t;

/* loaded from: classes.dex */
public class k implements Runnable {
    static final String I = z5.k.f("WorkerWrapper");
    private s A;
    private h6.b B;
    private v C;
    private List D;
    private String E;
    private volatile boolean H;

    /* renamed from: p, reason: collision with root package name */
    Context f232p;

    /* renamed from: q, reason: collision with root package name */
    private String f233q;

    /* renamed from: r, reason: collision with root package name */
    private List f234r;

    /* renamed from: s, reason: collision with root package name */
    private WorkerParameters.a f235s;

    /* renamed from: t, reason: collision with root package name */
    r f236t;

    /* renamed from: u, reason: collision with root package name */
    ListenableWorker f237u;

    /* renamed from: v, reason: collision with root package name */
    j6.a f238v;

    /* renamed from: x, reason: collision with root package name */
    private androidx.work.a f240x;

    /* renamed from: y, reason: collision with root package name */
    private g6.a f241y;

    /* renamed from: z, reason: collision with root package name */
    private WorkDatabase f242z;

    /* renamed from: w, reason: collision with root package name */
    ListenableWorker.a f239w = ListenableWorker.a.a();
    androidx.work.impl.utils.futures.c F = androidx.work.impl.utils.futures.c.s();
    ek.d G = null;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class a implements Runnable {

        /* renamed from: p, reason: collision with root package name */
        final /* synthetic */ ek.d f243p;

        /* renamed from: q, reason: collision with root package name */
        final /* synthetic */ androidx.work.impl.utils.futures.c f244q;

        a(ek.d dVar, androidx.work.impl.utils.futures.c cVar) {
            this.f243p = dVar;
            this.f244q = cVar;
        }

        @Override // java.lang.Runnable
        public void run() {
            try {
                this.f243p.get();
                z5.k.c().a(k.I, String.format("Starting work for %s", k.this.f236t.f20892c), new Throwable[0]);
                k kVar = k.this;
                kVar.G = kVar.f237u.p();
                this.f244q.q(k.this.G);
            } catch (Throwable th2) {
                this.f244q.p(th2);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class b implements Runnable {

        /* renamed from: p, reason: collision with root package name */
        final /* synthetic */ androidx.work.impl.utils.futures.c f246p;

        /* renamed from: q, reason: collision with root package name */
        final /* synthetic */ String f247q;

        b(androidx.work.impl.utils.futures.c cVar, String str) {
            this.f246p = cVar;
            this.f247q = str;
        }

        /* JADX WARN: Multi-variable type inference failed */
        /* JADX WARN: Type inference failed for: r8v3, types: [a6.k] */
        @Override // java.lang.Runnable
        public void run() {
            try {
                try {
                    ListenableWorker.a aVar = (ListenableWorker.a) this.f246p.get();
                    if (aVar == null) {
                        z5.k.c().b(k.I, String.format("%s returned a null result. Treating it as a failure.", k.this.f236t.f20892c), new Throwable[0]);
                    } else {
                        z5.k.c().a(k.I, String.format("%s returned a %s result.", k.this.f236t.f20892c, aVar), new Throwable[0]);
                        k.this.f239w = aVar;
                    }
                } catch (InterruptedException | ExecutionException e10) {
                    z5.k.c().b(k.I, String.format("%s failed because it threw an exception/error", this.f247q), e10);
                } catch (CancellationException e11) {
                    z5.k.c().d(k.I, String.format("%s was cancelled", this.f247q), e11);
                }
            } finally {
                k.this.f();
            }
        }
    }

    /* loaded from: classes.dex */
    public static class c {

        /* renamed from: a, reason: collision with root package name */
        Context f249a;

        /* renamed from: b, reason: collision with root package name */
        ListenableWorker f250b;

        /* renamed from: c, reason: collision with root package name */
        g6.a f251c;

        /* renamed from: d, reason: collision with root package name */
        j6.a f252d;

        /* renamed from: e, reason: collision with root package name */
        androidx.work.a f253e;

        /* renamed from: f, reason: collision with root package name */
        WorkDatabase f254f;

        /* renamed from: g, reason: collision with root package name */
        String f255g;

        /* renamed from: h, reason: collision with root package name */
        List f256h;

        /* renamed from: i, reason: collision with root package name */
        WorkerParameters.a f257i = new WorkerParameters.a();

        public c(Context context, androidx.work.a aVar, j6.a aVar2, g6.a aVar3, WorkDatabase workDatabase, String str) {
            this.f249a = context.getApplicationContext();
            this.f252d = aVar2;
            this.f251c = aVar3;
            this.f253e = aVar;
            this.f254f = workDatabase;
            this.f255g = str;
        }

        public k a() {
            return new k(this);
        }

        public c b(WorkerParameters.a aVar) {
            if (aVar != null) {
                this.f257i = aVar;
            }
            return this;
        }

        public c c(List list) {
            this.f256h = list;
            return this;
        }
    }

    k(c cVar) {
        this.f232p = cVar.f249a;
        this.f238v = cVar.f252d;
        this.f241y = cVar.f251c;
        this.f233q = cVar.f255g;
        this.f234r = cVar.f256h;
        this.f235s = cVar.f257i;
        this.f237u = cVar.f250b;
        this.f240x = cVar.f253e;
        WorkDatabase workDatabase = cVar.f254f;
        this.f242z = workDatabase;
        this.A = workDatabase.O();
        this.B = this.f242z.F();
        this.C = this.f242z.P();
    }

    private String a(List list) {
        StringBuilder sb2 = new StringBuilder("Work [ id=");
        sb2.append(this.f233q);
        sb2.append(", tags={ ");
        Iterator it = list.iterator();
        boolean z10 = true;
        while (it.hasNext()) {
            String str = (String) it.next();
            if (z10) {
                z10 = false;
            } else {
                sb2.append(", ");
            }
            sb2.append(str);
        }
        sb2.append(" } ]");
        return sb2.toString();
    }

    private void c(ListenableWorker.a aVar) {
        if (aVar instanceof ListenableWorker.a.c) {
            z5.k.c().d(I, String.format("Worker result SUCCESS for %s", this.E), new Throwable[0]);
            if (this.f236t.d()) {
                h();
                return;
            } else {
                m();
                return;
            }
        }
        if (aVar instanceof ListenableWorker.a.b) {
            z5.k.c().d(I, String.format("Worker result RETRY for %s", this.E), new Throwable[0]);
            g();
            return;
        }
        z5.k.c().d(I, String.format("Worker result FAILURE for %s", this.E), new Throwable[0]);
        if (this.f236t.d()) {
            h();
        } else {
            l();
        }
    }

    private void e(String str) {
        LinkedList linkedList = new LinkedList();
        linkedList.add(str);
        while (!linkedList.isEmpty()) {
            String str2 = (String) linkedList.remove();
            if (this.A.n(str2) != t.a.CANCELLED) {
                this.A.i(t.a.FAILED, str2);
            }
            linkedList.addAll(this.B.b(str2));
        }
    }

    private void g() {
        this.f242z.e();
        try {
            this.A.i(t.a.ENQUEUED, this.f233q);
            this.A.t(this.f233q, System.currentTimeMillis());
            this.A.b(this.f233q, -1L);
            this.f242z.C();
        } finally {
            this.f242z.i();
            i(true);
        }
    }

    private void h() {
        this.f242z.e();
        try {
            this.A.t(this.f233q, System.currentTimeMillis());
            this.A.i(t.a.ENQUEUED, this.f233q);
            this.A.p(this.f233q);
            this.A.b(this.f233q, -1L);
            this.f242z.C();
        } finally {
            this.f242z.i();
            i(false);
        }
    }

    private void i(boolean z10) {
        ListenableWorker listenableWorker;
        this.f242z.e();
        try {
            if (!this.f242z.O().l()) {
                i6.d.a(this.f232p, RescheduleReceiver.class, false);
            }
            if (z10) {
                this.A.i(t.a.ENQUEUED, this.f233q);
                this.A.b(this.f233q, -1L);
            }
            if (this.f236t != null && (listenableWorker = this.f237u) != null && listenableWorker.j()) {
                this.f241y.a(this.f233q);
            }
            this.f242z.C();
            this.f242z.i();
            this.F.o(Boolean.valueOf(z10));
        } catch (Throwable th2) {
            this.f242z.i();
            throw th2;
        }
    }

    private void j() {
        t.a n10 = this.A.n(this.f233q);
        if (n10 == t.a.RUNNING) {
            z5.k.c().a(I, String.format("Status for %s is RUNNING;not doing any work and rescheduling for later execution", this.f233q), new Throwable[0]);
            i(true);
        } else {
            z5.k.c().a(I, String.format("Status for %s is %s; not doing any work", this.f233q, n10), new Throwable[0]);
            i(false);
        }
    }

    private void k() {
        androidx.work.b b10;
        if (n()) {
            return;
        }
        this.f242z.e();
        try {
            r o10 = this.A.o(this.f233q);
            this.f236t = o10;
            if (o10 == null) {
                z5.k.c().b(I, String.format("Didn't find WorkSpec for id %s", this.f233q), new Throwable[0]);
                i(false);
                this.f242z.C();
                return;
            }
            if (o10.f20891b != t.a.ENQUEUED) {
                j();
                this.f242z.C();
                z5.k.c().a(I, String.format("%s is not in ENQUEUED state. Nothing more to do.", this.f236t.f20892c), new Throwable[0]);
                return;
            }
            if (o10.d() || this.f236t.c()) {
                long currentTimeMillis = System.currentTimeMillis();
                r rVar = this.f236t;
                if (!(rVar.f20903n == 0) && currentTimeMillis < rVar.a()) {
                    z5.k.c().a(I, String.format("Delaying execution for %s because it is being executed before schedule.", this.f236t.f20892c), new Throwable[0]);
                    i(true);
                    this.f242z.C();
                    return;
                }
            }
            this.f242z.C();
            this.f242z.i();
            if (this.f236t.d()) {
                b10 = this.f236t.f20894e;
            } else {
                z5.h b11 = this.f240x.f().b(this.f236t.f20893d);
                if (b11 == null) {
                    z5.k.c().b(I, String.format("Could not create Input Merger %s", this.f236t.f20893d), new Throwable[0]);
                    l();
                    return;
                } else {
                    ArrayList arrayList = new ArrayList();
                    arrayList.add(this.f236t.f20894e);
                    arrayList.addAll(this.A.r(this.f233q));
                    b10 = b11.b(arrayList);
                }
            }
            WorkerParameters workerParameters = new WorkerParameters(UUID.fromString(this.f233q), b10, this.D, this.f235s, this.f236t.f20900k, this.f240x.e(), this.f238v, this.f240x.m(), new o(this.f242z, this.f238v), new n(this.f242z, this.f241y, this.f238v));
            if (this.f237u == null) {
                this.f237u = this.f240x.m().b(this.f232p, this.f236t.f20892c, workerParameters);
            }
            ListenableWorker listenableWorker = this.f237u;
            if (listenableWorker == null) {
                z5.k.c().b(I, String.format("Could not create Worker %s", this.f236t.f20892c), new Throwable[0]);
                l();
                return;
            }
            if (listenableWorker.l()) {
                z5.k.c().b(I, String.format("Received an already-used Worker %s; WorkerFactory should return new instances", this.f236t.f20892c), new Throwable[0]);
                l();
                return;
            }
            this.f237u.o();
            if (!o()) {
                j();
                return;
            }
            if (n()) {
                return;
            }
            androidx.work.impl.utils.futures.c s10 = androidx.work.impl.utils.futures.c.s();
            m mVar = new m(this.f232p, this.f236t, this.f237u, workerParameters.b(), this.f238v);
            this.f238v.a().execute(mVar);
            ek.d a10 = mVar.a();
            a10.addListener(new a(a10, s10), this.f238v.a());
            s10.addListener(new b(s10, this.E), this.f238v.c());
        } finally {
            this.f242z.i();
        }
    }

    private void m() {
        this.f242z.e();
        try {
            this.A.i(t.a.SUCCEEDED, this.f233q);
            this.A.h(this.f233q, ((ListenableWorker.a.c) this.f239w).c());
            long currentTimeMillis = System.currentTimeMillis();
            for (String str : this.B.b(this.f233q)) {
                if (this.A.n(str) == t.a.BLOCKED && this.B.c(str)) {
                    z5.k.c().d(I, String.format("Setting status to enqueued for %s", str), new Throwable[0]);
                    this.A.i(t.a.ENQUEUED, str);
                    this.A.t(str, currentTimeMillis);
                }
            }
            this.f242z.C();
        } finally {
            this.f242z.i();
            i(false);
        }
    }

    private boolean n() {
        if (!this.H) {
            return false;
        }
        z5.k.c().a(I, String.format("Work interrupted for %s", this.E), new Throwable[0]);
        if (this.A.n(this.f233q) == null) {
            i(false);
        } else {
            i(!r0.isFinished());
        }
        return true;
    }

    private boolean o() {
        this.f242z.e();
        try {
            boolean z10 = false;
            if (this.A.n(this.f233q) == t.a.ENQUEUED) {
                this.A.i(t.a.RUNNING, this.f233q);
                this.A.s(this.f233q);
                z10 = true;
            }
            this.f242z.C();
            return z10;
        } finally {
            this.f242z.i();
        }
    }

    public ek.d b() {
        return this.F;
    }

    public void d() {
        boolean z10;
        this.H = true;
        n();
        ek.d dVar = this.G;
        if (dVar != null) {
            z10 = dVar.isDone();
            this.G.cancel(true);
        } else {
            z10 = false;
        }
        ListenableWorker listenableWorker = this.f237u;
        if (listenableWorker != null && !z10) {
            listenableWorker.q();
        } else {
            z5.k.c().a(I, String.format("WorkSpec %s is already done. Not interrupting.", this.f236t), new Throwable[0]);
        }
    }

    void f() {
        if (!n()) {
            this.f242z.e();
            try {
                t.a n10 = this.A.n(this.f233q);
                this.f242z.N().a(this.f233q);
                if (n10 == null) {
                    i(false);
                } else if (n10 == t.a.RUNNING) {
                    c(this.f239w);
                } else if (!n10.isFinished()) {
                    g();
                }
                this.f242z.C();
            } finally {
                this.f242z.i();
            }
        }
        List list = this.f234r;
        if (list != null) {
            Iterator it = list.iterator();
            while (it.hasNext()) {
                ((e) it.next()).cancel(this.f233q);
            }
            f.b(this.f240x, this.f242z, this.f234r);
        }
    }

    void l() {
        this.f242z.e();
        try {
            e(this.f233q);
            this.A.h(this.f233q, ((ListenableWorker.a.C0147a) this.f239w).c());
            this.f242z.C();
        } finally {
            this.f242z.i();
            i(false);
        }
    }

    @Override // java.lang.Runnable
    public void run() {
        List a10 = this.C.a(this.f233q);
        this.D = a10;
        this.E = a(a10);
        k();
    }
}
