package androidx.work.impl.background.systemjob;

import android.app.Application;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.os.Build;
import android.os.PersistableBundle;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.work.impl.b;
import androidx.work.impl.d;
import androidx.work.impl.o;
import androidx.work.l0;
import androidx.work.u;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
@RequiresApi(23)
@RestrictTo({RestrictTo.Scope.f556b})
public class SystemJobService extends JobService implements b {
    private static final String TAG = u.e("SystemJobService");
    private final Map<String, JobParameters> mJobParameters = new HashMap();
    private o mWorkManagerImpl;

    @Nullable
    private static String getWorkSpecIdFromJobParameters(@NonNull 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 {
            o oVarB = o.b(getApplicationContext());
            this.mWorkManagerImpl = oVarB;
            oVarB.f4628f.a(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().");
            }
            u.c().f(new Throwable[0]);
        }
    }

    @Override // android.app.Service
    public void onDestroy() {
        super.onDestroy();
        o oVar = this.mWorkManagerImpl;
        if (oVar != null) {
            oVar.f4628f.c(this);
        }
    }

    @Override // androidx.work.impl.b
    public void onExecuted(@NonNull String str, boolean z10) {
        JobParameters jobParametersRemove;
        u uVarC = u.c();
        String.format("%s executed on JobScheduler", str);
        uVarC.a(new Throwable[0]);
        synchronized (this.mJobParameters) {
            jobParametersRemove = this.mJobParameters.remove(str);
        }
        if (jobParametersRemove != null) {
            jobFinished(jobParametersRemove, z10);
        }
    }

    @Override // android.app.job.JobService
    public boolean onStartJob(@NonNull JobParameters jobParameters) {
        if (this.mWorkManagerImpl == null) {
            u.c().a(new Throwable[0]);
            jobFinished(jobParameters, true);
            return false;
        }
        String workSpecIdFromJobParameters = getWorkSpecIdFromJobParameters(jobParameters);
        if (TextUtils.isEmpty(workSpecIdFromJobParameters)) {
            u.c().b(new Throwable[0]);
            return false;
        }
        synchronized (this.mJobParameters) {
            try {
                if (this.mJobParameters.containsKey(workSpecIdFromJobParameters)) {
                    u uVarC = u.c();
                    String.format("Job is already being executed by SystemJobService: %s", workSpecIdFromJobParameters);
                    uVarC.a(new Throwable[0]);
                    return false;
                }
                u uVarC2 = u.c();
                String.format("onStartJob for %s", workSpecIdFromJobParameters);
                uVarC2.a(new Throwable[0]);
                this.mJobParameters.put(workSpecIdFromJobParameters, jobParameters);
                int i10 = Build.VERSION.SDK_INT;
                l0 l0Var = new l0();
                if (jobParameters.getTriggeredContentUris() != null) {
                    l0Var.f4688b = Arrays.asList(jobParameters.getTriggeredContentUris());
                }
                if (jobParameters.getTriggeredContentAuthorities() != null) {
                    l0Var.f4687a = Arrays.asList(jobParameters.getTriggeredContentAuthorities());
                }
                if (i10 >= 28) {
                    l0Var.f4689c = jobParameters.getNetwork();
                }
                this.mWorkManagerImpl.startWork(workSpecIdFromJobParameters, l0Var);
                return true;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // android.app.job.JobService
    public boolean onStopJob(@NonNull JobParameters jobParameters) {
        boolean zContains;
        if (this.mWorkManagerImpl == null) {
            u.c().a(new Throwable[0]);
            return true;
        }
        String workSpecIdFromJobParameters = getWorkSpecIdFromJobParameters(jobParameters);
        if (TextUtils.isEmpty(workSpecIdFromJobParameters)) {
            u.c().b(new Throwable[0]);
            return false;
        }
        u uVarC = u.c();
        String.format("onStopJob for %s", workSpecIdFromJobParameters);
        uVarC.a(new Throwable[0]);
        synchronized (this.mJobParameters) {
            this.mJobParameters.remove(workSpecIdFromJobParameters);
        }
        this.mWorkManagerImpl.f(workSpecIdFromJobParameters);
        d dVar = this.mWorkManagerImpl.f4628f;
        synchronized (dVar.f4582j) {
            zContains = dVar.f4580h.contains(workSpecIdFromJobParameters);
        }
        return !zContains;
    }
}
