package v0; 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 androidx.work.m; import androidx.work.v; import c1.p; import c1.q; import c1.t; import d1.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; /* loaded from: classes.dex */ public class k implements Runnable { /* renamed from: t, reason: collision with root package name */ static final String f14826t = m.f("WorkerWrapper"); /* renamed from: a, reason: collision with root package name */ Context f14827a; /* renamed from: b, reason: collision with root package name */ private String f14828b; /* renamed from: c, reason: collision with root package name */ private List f14829c; /* renamed from: d, reason: collision with root package name */ private WorkerParameters.a f14830d; /* renamed from: e, reason: collision with root package name */ p f14831e; /* renamed from: f, reason: collision with root package name */ ListenableWorker f14832f; /* renamed from: g, reason: collision with root package name */ e1.a f14833g; /* renamed from: i, reason: collision with root package name */ private androidx.work.b f14835i; /* renamed from: j, reason: collision with root package name */ private b1.a f14836j; /* renamed from: k, reason: collision with root package name */ private WorkDatabase f14837k; /* renamed from: l, reason: collision with root package name */ private q f14838l; /* renamed from: m, reason: collision with root package name */ private c1.b f14839m; /* renamed from: n, reason: collision with root package name */ private t f14840n; /* renamed from: o, reason: collision with root package name */ private List f14841o; /* renamed from: p, reason: collision with root package name */ private String f14842p; /* renamed from: s, reason: collision with root package name */ private volatile boolean f14845s; /* renamed from: h, reason: collision with root package name */ ListenableWorker.a f14834h = ListenableWorker.a.a(); /* renamed from: q, reason: collision with root package name */ androidx.work.impl.utils.futures.c f14843q = androidx.work.impl.utils.futures.c.t(); /* renamed from: r, reason: collision with root package name */ v3.a f14844r = null; class a implements Runnable { /* renamed from: a, reason: collision with root package name */ final /* synthetic */ v3.a f14846a; /* renamed from: b, reason: collision with root package name */ final /* synthetic */ androidx.work.impl.utils.futures.c f14847b; a(v3.a aVar, androidx.work.impl.utils.futures.c cVar) { this.f14846a = aVar; this.f14847b = cVar; } @Override // java.lang.Runnable public void run() { try { this.f14846a.get(); m.c().a(k.f14826t, String.format("Starting work for %s", k.this.f14831e.f3399c), new Throwable[0]); k kVar = k.this; kVar.f14844r = kVar.f14832f.startWork(); this.f14847b.r(k.this.f14844r); } catch (Throwable th) { this.f14847b.q(th); } } } class b implements Runnable { /* renamed from: a, reason: collision with root package name */ final /* synthetic */ androidx.work.impl.utils.futures.c f14849a; /* renamed from: b, reason: collision with root package name */ final /* synthetic */ String f14850b; b(androidx.work.impl.utils.futures.c cVar, String str) { this.f14849a = cVar; this.f14850b = str; } @Override // java.lang.Runnable public void run() { try { try { ListenableWorker.a aVar = (ListenableWorker.a) this.f14849a.get(); if (aVar == null) { m.c().b(k.f14826t, String.format("%s returned a null result. Treating it as a failure.", k.this.f14831e.f3399c), new Throwable[0]); } else { m.c().a(k.f14826t, String.format("%s returned a %s result.", k.this.f14831e.f3399c, aVar), new Throwable[0]); k.this.f14834h = aVar; } } catch (InterruptedException e10) { e = e10; m.c().b(k.f14826t, String.format("%s failed because it threw an exception/error", this.f14850b), e); } catch (CancellationException e11) { m.c().d(k.f14826t, String.format("%s was cancelled", this.f14850b), e11); } catch (ExecutionException e12) { e = e12; m.c().b(k.f14826t, String.format("%s failed because it threw an exception/error", this.f14850b), e); } } finally { k.this.f(); } } } public static class c { /* renamed from: a, reason: collision with root package name */ Context f14852a; /* renamed from: b, reason: collision with root package name */ ListenableWorker f14853b; /* renamed from: c, reason: collision with root package name */ b1.a f14854c; /* renamed from: d, reason: collision with root package name */ e1.a f14855d; /* renamed from: e, reason: collision with root package name */ androidx.work.b f14856e; /* renamed from: f, reason: collision with root package name */ WorkDatabase f14857f; /* renamed from: g, reason: collision with root package name */ String f14858g; /* renamed from: h, reason: collision with root package name */ List f14859h; /* renamed from: i, reason: collision with root package name */ WorkerParameters.a f14860i = new WorkerParameters.a(); public c(Context context, androidx.work.b bVar, e1.a aVar, b1.a aVar2, WorkDatabase workDatabase, String str) { this.f14852a = context.getApplicationContext(); this.f14855d = aVar; this.f14854c = aVar2; this.f14856e = bVar; this.f14857f = workDatabase; this.f14858g = str; } public k a() { return new k(this); } public c b(WorkerParameters.a aVar) { if (aVar != null) { this.f14860i = aVar; } return this; } public c c(List list) { this.f14859h = list; return this; } } k(c cVar) { this.f14827a = cVar.f14852a; this.f14833g = cVar.f14855d; this.f14836j = cVar.f14854c; this.f14828b = cVar.f14858g; this.f14829c = cVar.f14859h; this.f14830d = cVar.f14860i; this.f14832f = cVar.f14853b; this.f14835i = cVar.f14856e; WorkDatabase workDatabase = cVar.f14857f; this.f14837k = workDatabase; this.f14838l = workDatabase.B(); this.f14839m = this.f14837k.t(); this.f14840n = this.f14837k.C(); } private String a(List list) { StringBuilder sb = new StringBuilder("Work [ id="); sb.append(this.f14828b); sb.append(", tags={ "); Iterator it = list.iterator(); boolean z10 = true; while (it.hasNext()) { String str = (String) it.next(); if (z10) { z10 = false; } else { sb.append(", "); } sb.append(str); } sb.append(" } ]"); return sb.toString(); } private void c(ListenableWorker.a aVar) { if (aVar instanceof ListenableWorker.a.c) { m.c().d(f14826t, String.format("Worker result SUCCESS for %s", this.f14842p), new Throwable[0]); if (this.f14831e.d()) { h(); return; } else { m(); return; } } if (aVar instanceof ListenableWorker.a.b) { m.c().d(f14826t, String.format("Worker result RETRY for %s", this.f14842p), new Throwable[0]); g(); return; } m.c().d(f14826t, String.format("Worker result FAILURE for %s", this.f14842p), new Throwable[0]); if (this.f14831e.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.f14838l.m(str2) != v.CANCELLED) { this.f14838l.b(v.FAILED, str2); } linkedList.addAll(this.f14839m.b(str2)); } } private void g() { this.f14837k.c(); try { this.f14838l.b(v.ENQUEUED, this.f14828b); this.f14838l.s(this.f14828b, System.currentTimeMillis()); this.f14838l.c(this.f14828b, -1L); this.f14837k.r(); } finally { this.f14837k.g(); i(true); } } private void h() { this.f14837k.c(); try { this.f14838l.s(this.f14828b, System.currentTimeMillis()); this.f14838l.b(v.ENQUEUED, this.f14828b); this.f14838l.o(this.f14828b); this.f14838l.c(this.f14828b, -1L); this.f14837k.r(); } finally { this.f14837k.g(); i(false); } } private void i(boolean z10) { ListenableWorker listenableWorker; this.f14837k.c(); try { if (!this.f14837k.B().j()) { d1.g.a(this.f14827a, RescheduleReceiver.class, false); } if (z10) { this.f14838l.b(v.ENQUEUED, this.f14828b); this.f14838l.c(this.f14828b, -1L); } if (this.f14831e != null && (listenableWorker = this.f14832f) != null && listenableWorker.isRunInForeground()) { this.f14836j.b(this.f14828b); } this.f14837k.r(); this.f14837k.g(); this.f14843q.p(Boolean.valueOf(z10)); } catch (Throwable th) { this.f14837k.g(); throw th; } } private void j() { v m10 = this.f14838l.m(this.f14828b); if (m10 == v.RUNNING) { m.c().a(f14826t, String.format("Status for %s is RUNNING;not doing any work and rescheduling for later execution", this.f14828b), new Throwable[0]); i(true); } else { m.c().a(f14826t, String.format("Status for %s is %s; not doing any work", this.f14828b, m10), new Throwable[0]); i(false); } } private void k() { androidx.work.e b10; if (n()) { return; } this.f14837k.c(); try { p n10 = this.f14838l.n(this.f14828b); this.f14831e = n10; if (n10 == null) { m.c().b(f14826t, String.format("Didn't find WorkSpec for id %s", this.f14828b), new Throwable[0]); i(false); this.f14837k.r(); return; } if (n10.f3398b != v.ENQUEUED) { j(); this.f14837k.r(); m.c().a(f14826t, String.format("%s is not in ENQUEUED state. Nothing more to do.", this.f14831e.f3399c), new Throwable[0]); return; } if (n10.d() || this.f14831e.c()) { long currentTimeMillis = System.currentTimeMillis(); p pVar = this.f14831e; if (!(pVar.f3410n == 0) && currentTimeMillis < pVar.a()) { m.c().a(f14826t, String.format("Delaying execution for %s because it is being executed before schedule.", this.f14831e.f3399c), new Throwable[0]); i(true); this.f14837k.r(); return; } } this.f14837k.r(); this.f14837k.g(); if (this.f14831e.d()) { b10 = this.f14831e.f3401e; } else { androidx.work.j b11 = this.f14835i.f().b(this.f14831e.f3400d); if (b11 == null) { m.c().b(f14826t, String.format("Could not create Input Merger %s", this.f14831e.f3400d), new Throwable[0]); l(); return; } else { ArrayList arrayList = new ArrayList(); arrayList.add(this.f14831e.f3401e); arrayList.addAll(this.f14838l.q(this.f14828b)); b10 = b11.b(arrayList); } } WorkerParameters workerParameters = new WorkerParameters(UUID.fromString(this.f14828b), b10, this.f14841o, this.f14830d, this.f14831e.f3407k, this.f14835i.e(), this.f14833g, this.f14835i.m(), new d1.q(this.f14837k, this.f14833g), new d1.p(this.f14837k, this.f14836j, this.f14833g)); if (this.f14832f == null) { this.f14832f = this.f14835i.m().b(this.f14827a, this.f14831e.f3399c, workerParameters); } ListenableWorker listenableWorker = this.f14832f; if (listenableWorker == null) { m.c().b(f14826t, String.format("Could not create Worker %s", this.f14831e.f3399c), new Throwable[0]); l(); return; } if (listenableWorker.isUsed()) { m.c().b(f14826t, String.format("Received an already-used Worker %s; WorkerFactory should return new instances", this.f14831e.f3399c), new Throwable[0]); l(); return; } this.f14832f.setUsed(); if (!o()) { j(); return; } if (n()) { return; } androidx.work.impl.utils.futures.c t10 = androidx.work.impl.utils.futures.c.t(); o oVar = new o(this.f14827a, this.f14831e, this.f14832f, workerParameters.b(), this.f14833g); this.f14833g.b().execute(oVar); v3.a a10 = oVar.a(); a10.a(new a(a10, t10), this.f14833g.b()); t10.a(new b(t10, this.f14842p), this.f14833g.a()); } finally { this.f14837k.g(); } } private void m() { this.f14837k.c(); try { this.f14838l.b(v.SUCCEEDED, this.f14828b); this.f14838l.h(this.f14828b, ((ListenableWorker.a.c) this.f14834h).e()); long currentTimeMillis = System.currentTimeMillis(); for (String str : this.f14839m.b(this.f14828b)) { if (this.f14838l.m(str) == v.BLOCKED && this.f14839m.c(str)) { m.c().d(f14826t, String.format("Setting status to enqueued for %s", str), new Throwable[0]); this.f14838l.b(v.ENQUEUED, str); this.f14838l.s(str, currentTimeMillis); } } this.f14837k.r(); } finally { this.f14837k.g(); i(false); } } private boolean n() { if (!this.f14845s) { return false; } m.c().a(f14826t, String.format("Work interrupted for %s", this.f14842p), new Throwable[0]); if (this.f14838l.m(this.f14828b) == null) { i(false); } else { i(!r0.a()); } return true; } private boolean o() { this.f14837k.c(); try { boolean z10 = false; if (this.f14838l.m(this.f14828b) == v.ENQUEUED) { this.f14838l.b(v.RUNNING, this.f14828b); this.f14838l.r(this.f14828b); z10 = true; } this.f14837k.r(); return z10; } finally { this.f14837k.g(); } } public v3.a b() { return this.f14843q; } public void d() { boolean z10; this.f14845s = true; n(); v3.a aVar = this.f14844r; if (aVar != null) { z10 = aVar.isDone(); this.f14844r.cancel(true); } else { z10 = false; } ListenableWorker listenableWorker = this.f14832f; if (listenableWorker == null || z10) { m.c().a(f14826t, String.format("WorkSpec %s is already done. Not interrupting.", this.f14831e), new Throwable[0]); } else { listenableWorker.stop(); } } void f() { if (!n()) { this.f14837k.c(); try { v m10 = this.f14838l.m(this.f14828b); this.f14837k.A().a(this.f14828b); if (m10 == null) { i(false); } else if (m10 == v.RUNNING) { c(this.f14834h); } else if (!m10.a()) { g(); } this.f14837k.r(); } finally { this.f14837k.g(); } } List list = this.f14829c; if (list != null) { Iterator it = list.iterator(); while (it.hasNext()) { ((e) it.next()).e(this.f14828b); } f.b(this.f14835i, this.f14837k, this.f14829c); } } void l() { this.f14837k.c(); try { e(this.f14828b); this.f14838l.h(this.f14828b, ((ListenableWorker.a.C0043a) this.f14834h).e()); this.f14837k.r(); } finally { this.f14837k.g(); i(false); } } @Override // java.lang.Runnable public void run() { List a10 = this.f14840n.a(this.f14828b); this.f14841o = a10; this.f14842p = a(a10); k(); } }