package v0; import android.content.Context; import android.os.PowerManager; import androidx.work.WorkerParameters; import androidx.work.impl.WorkDatabase; import androidx.work.m; import d1.n; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ExecutionException; import v0.k; /* loaded from: classes.dex */ public class d implements b, b1.a { /* renamed from: l, reason: collision with root package name */ private static final String f14785l = m.f("Processor"); /* renamed from: b, reason: collision with root package name */ private Context f14787b; /* renamed from: c, reason: collision with root package name */ private androidx.work.b f14788c; /* renamed from: d, reason: collision with root package name */ private e1.a f14789d; /* renamed from: e, reason: collision with root package name */ private WorkDatabase f14790e; /* renamed from: h, reason: collision with root package name */ private List f14793h; /* renamed from: g, reason: collision with root package name */ private Map f14792g = new HashMap(); /* renamed from: f, reason: collision with root package name */ private Map f14791f = new HashMap(); /* renamed from: i, reason: collision with root package name */ private Set f14794i = new HashSet(); /* renamed from: j, reason: collision with root package name */ private final List f14795j = new ArrayList(); /* renamed from: a, reason: collision with root package name */ private PowerManager.WakeLock f14786a = null; /* renamed from: k, reason: collision with root package name */ private final Object f14796k = new Object(); private static class a implements Runnable { /* renamed from: a, reason: collision with root package name */ private b f14797a; /* renamed from: b, reason: collision with root package name */ private String f14798b; /* renamed from: c, reason: collision with root package name */ private v3.a f14799c; a(b bVar, String str, v3.a aVar) { this.f14797a = bVar; this.f14798b = str; this.f14799c = aVar; } @Override // java.lang.Runnable public void run() { boolean z10; try { z10 = ((Boolean) this.f14799c.get()).booleanValue(); } catch (InterruptedException | ExecutionException unused) { z10 = true; } this.f14797a.d(this.f14798b, z10); } } public d(Context context, androidx.work.b bVar, e1.a aVar, WorkDatabase workDatabase, List list) { this.f14787b = context; this.f14788c = bVar; this.f14789d = aVar; this.f14790e = workDatabase; this.f14793h = list; } private static boolean e(String str, k kVar) { if (kVar == null) { m.c().a(f14785l, String.format("WorkerWrapper could not be found for %s", str), new Throwable[0]); return false; } kVar.d(); m.c().a(f14785l, String.format("WorkerWrapper interrupted for %s", str), new Throwable[0]); return true; } private void m() { synchronized (this.f14796k) { if (!(!this.f14791f.isEmpty())) { try { this.f14787b.startService(androidx.work.impl.foreground.a.e(this.f14787b)); } catch (Throwable th) { m.c().b(f14785l, "Unable to stop foreground service", th); } PowerManager.WakeLock wakeLock = this.f14786a; if (wakeLock != null) { wakeLock.release(); this.f14786a = null; } } } } @Override // b1.a public void a(String str, androidx.work.g gVar) { synchronized (this.f14796k) { m.c().d(f14785l, String.format("Moving WorkSpec (%s) to the foreground", str), new Throwable[0]); k kVar = (k) this.f14792g.remove(str); if (kVar != null) { if (this.f14786a == null) { PowerManager.WakeLock b10 = n.b(this.f14787b, "ProcessorForegroundLck"); this.f14786a = b10; b10.acquire(); } this.f14791f.put(str, kVar); androidx.core.content.a.startForegroundService(this.f14787b, androidx.work.impl.foreground.a.c(this.f14787b, str, gVar)); } } } @Override // b1.a public void b(String str) { synchronized (this.f14796k) { this.f14791f.remove(str); m(); } } public void c(b bVar) { synchronized (this.f14796k) { this.f14795j.add(bVar); } } @Override // v0.b public void d(String str, boolean z10) { synchronized (this.f14796k) { this.f14792g.remove(str); m.c().a(f14785l, String.format("%s %s executed; reschedule = %s", getClass().getSimpleName(), str, Boolean.valueOf(z10)), new Throwable[0]); Iterator it = this.f14795j.iterator(); while (it.hasNext()) { ((b) it.next()).d(str, z10); } } } public boolean f(String str) { boolean contains; synchronized (this.f14796k) { contains = this.f14794i.contains(str); } return contains; } public boolean g(String str) { boolean z10; synchronized (this.f14796k) { z10 = this.f14792g.containsKey(str) || this.f14791f.containsKey(str); } return z10; } public boolean h(String str) { boolean containsKey; synchronized (this.f14796k) { containsKey = this.f14791f.containsKey(str); } return containsKey; } public void i(b bVar) { synchronized (this.f14796k) { this.f14795j.remove(bVar); } } public boolean j(String str) { return k(str, null); } public boolean k(String str, WorkerParameters.a aVar) { synchronized (this.f14796k) { if (g(str)) { m.c().a(f14785l, String.format("Work %s is already enqueued for processing", str), new Throwable[0]); return false; } k a10 = new k.c(this.f14787b, this.f14788c, this.f14789d, this, this.f14790e, str).c(this.f14793h).b(aVar).a(); v3.a b10 = a10.b(); b10.a(new a(this, str, b10), this.f14789d.b()); this.f14792g.put(str, a10); this.f14789d.a().execute(a10); m.c().a(f14785l, String.format("%s: processing %s", getClass().getSimpleName(), str), new Throwable[0]); return true; } } public boolean l(String str) { boolean e10; synchronized (this.f14796k) { boolean z10 = true; m.c().a(f14785l, String.format("Processor cancelling %s", str), new Throwable[0]); this.f14794i.add(str); k kVar = (k) this.f14791f.remove(str); if (kVar == null) { z10 = false; } if (kVar == null) { kVar = (k) this.f14792g.remove(str); } e10 = e(str, kVar); if (z10) { m(); } } return e10; } public boolean n(String str) { boolean e10; synchronized (this.f14796k) { m.c().a(f14785l, String.format("Processor stopping foreground work %s", str), new Throwable[0]); e10 = e(str, (k) this.f14791f.remove(str)); } return e10; } public boolean o(String str) { boolean e10; synchronized (this.f14796k) { m.c().a(f14785l, String.format("Processor stopping background work %s", str), new Throwable[0]); e10 = e(str, (k) this.f14792g.remove(str)); } return e10; } }