package x0; import android.app.job.JobInfo; import android.app.job.JobScheduler; import android.content.ComponentName; import android.content.Context; import android.os.Build; import android.os.PersistableBundle; import android.text.TextUtils; import androidx.work.impl.WorkDatabase; import androidx.work.impl.background.systemjob.SystemJobService; import androidx.work.v; import c1.q; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Locale; /* loaded from: classes.dex */ public class m implements v0.e { /* renamed from: e, reason: collision with root package name */ private static final String f15253e = androidx.work.m.f("SystemJobScheduler"); /* renamed from: a, reason: collision with root package name */ private final Context f15254a; /* renamed from: b, reason: collision with root package name */ private final JobScheduler f15255b; /* renamed from: c, reason: collision with root package name */ private final v0.j f15256c; /* renamed from: d, reason: collision with root package name */ private final l f15257d; public m(Context context, v0.j jVar) { this(context, jVar, (JobScheduler) context.getSystemService("jobscheduler"), new l(context)); } public static void b(Context context) { List g10; JobScheduler jobScheduler = (JobScheduler) context.getSystemService("jobscheduler"); if (jobScheduler == null || (g10 = g(context, jobScheduler)) == null || g10.isEmpty()) { return; } Iterator it = g10.iterator(); while (it.hasNext()) { d(jobScheduler, ((JobInfo) it.next()).getId()); } } private static void d(JobScheduler jobScheduler, int i10) { try { jobScheduler.cancel(i10); } catch (Throwable th) { androidx.work.m.c().b(f15253e, String.format(Locale.getDefault(), "Exception while trying to cancel job (%d)", Integer.valueOf(i10)), th); } } private static List f(Context context, JobScheduler jobScheduler, String str) { List g10 = g(context, jobScheduler); if (g10 == null) { return null; } ArrayList arrayList = new ArrayList(2); for (JobInfo jobInfo : g10) { if (str.equals(h(jobInfo))) { arrayList.add(Integer.valueOf(jobInfo.getId())); } } return arrayList; } private static List g(Context context, JobScheduler jobScheduler) { List list; try { list = jobScheduler.getAllPendingJobs(); } catch (Throwable th) { androidx.work.m.c().b(f15253e, "getAllPendingJobs() is not reliable on this device.", th); list = null; } if (list == null) { return null; } ArrayList arrayList = new ArrayList(list.size()); ComponentName componentName = new ComponentName(context, (Class) SystemJobService.class); for (JobInfo jobInfo : list) { if (componentName.equals(jobInfo.getService())) { arrayList.add(jobInfo); } } return arrayList; } private static String h(JobInfo jobInfo) { PersistableBundle extras = jobInfo.getExtras(); if (extras == null) { return null; } try { if (extras.containsKey("EXTRA_WORK_SPEC_ID")) { return extras.getString("EXTRA_WORK_SPEC_ID"); } return null; } catch (NullPointerException unused) { return null; } } public static boolean i(Context context, v0.j jVar) { JobScheduler jobScheduler = (JobScheduler) context.getSystemService("jobscheduler"); List g10 = g(context, jobScheduler); List a10 = jVar.o().y().a(); boolean z10 = false; HashSet hashSet = new HashSet(g10 != null ? g10.size() : 0); if (g10 != null && !g10.isEmpty()) { for (JobInfo jobInfo : g10) { String h10 = h(jobInfo); if (TextUtils.isEmpty(h10)) { d(jobScheduler, jobInfo.getId()); } else { hashSet.add(h10); } } } Iterator it = a10.iterator(); while (true) { if (!it.hasNext()) { break; } if (!hashSet.contains((String) it.next())) { androidx.work.m.c().a(f15253e, "Reconciling jobs", new Throwable[0]); z10 = true; break; } } if (z10) { WorkDatabase o10 = jVar.o(); o10.c(); try { q B = o10.B(); Iterator it2 = a10.iterator(); while (it2.hasNext()) { B.c((String) it2.next(), -1L); } o10.r(); } finally { o10.g(); } } return z10; } @Override // v0.e public void a(c1.p... pVarArr) { List f10; WorkDatabase o10 = this.f15256c.o(); d1.f fVar = new d1.f(o10); for (c1.p pVar : pVarArr) { o10.c(); try { c1.p n10 = o10.B().n(pVar.f3397a); if (n10 == null) { androidx.work.m.c().h(f15253e, "Skipping scheduling " + pVar.f3397a + " because it's no longer in the DB", new Throwable[0]); o10.r(); } else if (n10.f3398b != v.ENQUEUED) { androidx.work.m.c().h(f15253e, "Skipping scheduling " + pVar.f3397a + " because it is no longer enqueued", new Throwable[0]); o10.r(); } else { c1.g c10 = o10.y().c(pVar.f3397a); int d10 = c10 != null ? c10.f3375b : fVar.d(this.f15256c.i().i(), this.f15256c.i().g()); if (c10 == null) { this.f15256c.o().y().b(new c1.g(pVar.f3397a, d10)); } j(pVar, d10); if (Build.VERSION.SDK_INT == 23 && (f10 = f(this.f15254a, this.f15255b, pVar.f3397a)) != null) { int indexOf = f10.indexOf(Integer.valueOf(d10)); if (indexOf >= 0) { f10.remove(indexOf); } j(pVar, !f10.isEmpty() ? ((Integer) f10.get(0)).intValue() : fVar.d(this.f15256c.i().i(), this.f15256c.i().g())); } o10.r(); } o10.g(); } catch (Throwable th) { o10.g(); throw th; } } } @Override // v0.e public boolean c() { return true; } @Override // v0.e public void e(String str) { List f10 = f(this.f15254a, this.f15255b, str); if (f10 == null || f10.isEmpty()) { return; } Iterator it = f10.iterator(); while (it.hasNext()) { d(this.f15255b, ((Integer) it.next()).intValue()); } this.f15256c.o().y().d(str); } public void j(c1.p pVar, int i10) { JobInfo a10 = this.f15257d.a(pVar, i10); androidx.work.m c10 = androidx.work.m.c(); String str = f15253e; c10.a(str, String.format("Scheduling work ID %s Job ID %s", pVar.f3397a, Integer.valueOf(i10)), new Throwable[0]); try { if (this.f15255b.schedule(a10) == 0) { androidx.work.m.c().h(str, String.format("Unable to schedule work ID %s", pVar.f3397a), new Throwable[0]); if (pVar.f3413q && pVar.f3414r == androidx.work.q.RUN_AS_NON_EXPEDITED_WORK_REQUEST) { pVar.f3413q = false; androidx.work.m.c().a(str, String.format("Scheduling a non-expedited job (work ID %s)", pVar.f3397a), new Throwable[0]); j(pVar, i10); } } } catch (IllegalStateException e10) { List g10 = g(this.f15254a, this.f15255b); String format = String.format(Locale.getDefault(), "JobScheduler 100 job limit exceeded. We count %d WorkManager jobs in JobScheduler; we have %d tracked jobs in our DB; our Configuration limit is %d.", Integer.valueOf(g10 != null ? g10.size() : 0), Integer.valueOf(this.f15256c.o().B().g().size()), Integer.valueOf(this.f15256c.i().h())); androidx.work.m.c().b(f15253e, format, new Throwable[0]); throw new IllegalStateException(format, e10); } catch (Throwable th) { androidx.work.m.c().b(f15253e, String.format("Unable to schedule %s", pVar), th); } } public m(Context context, v0.j jVar, JobScheduler jobScheduler, l lVar) { this.f15254a = context; this.f15256c = jVar; this.f15255b = jobScheduler; this.f15257d = lVar; } }