package androidx.work.impl.background.systemjob;

import a6.b;
import a6.i;
import android.app.Application;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.os.PersistableBundle;
import android.text.TextUtils;
import androidx.work.WorkerParameters;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import z5.k;

/* loaded from: classes.dex */
public class SystemJobService extends JobService implements b {

    /* renamed from: r, reason: collision with root package name */
    private static final String f5964r = k.f("SystemJobService");

    /* renamed from: p, reason: collision with root package name */
    private i f5965p;

    /* renamed from: q, reason: collision with root package name */
    private final Map f5966q = new HashMap();

    private static String a(JobParameters jobParameters) {
        try {
            PersistableBundle extras = jobParameters.getExtras();
            if (extras == null || !extras.containsKey("EXTRA_WORK_SPEC_ID")) {
                return null;
            }
            return extras.getString("EXTRA_WORK_SPEC_ID");
        } catch (NullPointerException unused) {
            return null;
        }
    }

    @Override // android.app.Service
    public void onCreate() {
        super.onCreate();
        try {
            i o10 = i.o(getApplicationContext());
            this.f5965p = o10;
            o10.q().c(this);
        } catch (IllegalStateException unused) {
            if (!Application.class.equals(getApplication().getClass())) {
                throw new IllegalStateException("WorkManager needs to be initialized via a ContentProvider#onCreate() or an Application#onCreate().");
            }
            k.c().h(f5964r, "Could not find WorkManager instance; this may be because an auto-backup is in progress. Ignoring JobScheduler commands for now. Please make sure that you are initializing WorkManager if you have manually disabled WorkManagerInitializer.", new Throwable[0]);
        }
    }

    @Override // android.app.Service
    public void onDestroy() {
        super.onDestroy();
        i iVar = this.f5965p;
        if (iVar != null) {
            iVar.q().h(this);
        }
    }

    @Override // a6.b
    public void onExecuted(String str, boolean z10) {
        JobParameters jobParameters;
        k.c().a(f5964r, String.format("%s executed on JobScheduler", str), new Throwable[0]);
        synchronized (this.f5966q) {
            jobParameters = (JobParameters) this.f5966q.remove(str);
        }
        if (jobParameters != null) {
            jobFinished(jobParameters, z10);
        }
    }

    @Override // android.app.job.JobService
    public boolean onStartJob(JobParameters jobParameters) {
        if (this.f5965p == null) {
            k.c().a(f5964r, "WorkManager is not initialized; requesting retry.", new Throwable[0]);
            jobFinished(jobParameters, true);
            return false;
        }
        String a10 = a(jobParameters);
        if (TextUtils.isEmpty(a10)) {
            k.c().b(f5964r, "WorkSpec id not found!", new Throwable[0]);
            return false;
        }
        synchronized (this.f5966q) {
            if (this.f5966q.containsKey(a10)) {
                k.c().a(f5964r, String.format("Job is already being executed by SystemJobService: %s", a10), new Throwable[0]);
                return false;
            }
            k.c().a(f5964r, String.format("onStartJob for %s", a10), new Throwable[0]);
            this.f5966q.put(a10, jobParameters);
            WorkerParameters.a aVar = new WorkerParameters.a();
            if (jobParameters.getTriggeredContentUris() != null) {
                aVar.f5868b = Arrays.asList(jobParameters.getTriggeredContentUris());
            }
            if (jobParameters.getTriggeredContentAuthorities() != null) {
                aVar.f5867a = Arrays.asList(jobParameters.getTriggeredContentAuthorities());
            }
            aVar.f5869c = jobParameters.getNetwork();
            this.f5965p.A(a10, aVar);
            return true;
        }
    }

    @Override // android.app.job.JobService
    public boolean onStopJob(JobParameters jobParameters) {
        if (this.f5965p == null) {
            k.c().a(f5964r, "WorkManager is not initialized; requesting retry.", new Throwable[0]);
            return true;
        }
        String a10 = a(jobParameters);
        if (TextUtils.isEmpty(a10)) {
            k.c().b(f5964r, "WorkSpec id not found!", new Throwable[0]);
            return false;
        }
        k.c().a(f5964r, String.format("onStopJob for %s", a10), new Throwable[0]);
        synchronized (this.f5966q) {
            this.f5966q.remove(a10);
        }
        this.f5965p.C(a10);
        return !this.f5965p.q().e(a10);
    }
}
