134 lines
6.0 KiB
Java
134 lines
6.0 KiB
Java
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.Logger;
|
|
import androidx.work.WorkerParameters;
|
|
import androidx.work.impl.ExecutionListener;
|
|
import androidx.work.impl.WorkManagerImpl;
|
|
import java.util.Arrays;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
@RequiresApi(23)
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP})
|
|
public class SystemJobService extends JobService implements ExecutionListener {
|
|
private static final String TAG = Logger.tagWithPrefix("SystemJobService");
|
|
private final Map<String, JobParameters> mJobParameters = new HashMap();
|
|
private WorkManagerImpl mWorkManagerImpl;
|
|
|
|
@Nullable
|
|
private static String getWorkSpecIdFromJobParameters(@NonNull JobParameters jobParameters) {
|
|
try {
|
|
PersistableBundle extras = jobParameters.getExtras();
|
|
if (extras == null || !extras.containsKey(SystemJobInfoConverter.EXTRA_WORK_SPEC_ID)) {
|
|
return null;
|
|
}
|
|
return extras.getString(SystemJobInfoConverter.EXTRA_WORK_SPEC_ID);
|
|
} catch (NullPointerException unused) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@Override // android.app.Service
|
|
public void onCreate() {
|
|
super.onCreate();
|
|
try {
|
|
WorkManagerImpl instance = WorkManagerImpl.getInstance(getApplicationContext());
|
|
this.mWorkManagerImpl = instance;
|
|
instance.getProcessor().addExecutionListener(this);
|
|
} catch (IllegalStateException unused) {
|
|
if (Application.class.equals(getApplication().getClass())) {
|
|
Logger.get().warning(TAG, "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]);
|
|
return;
|
|
}
|
|
throw new IllegalStateException("WorkManager needs to be initialized via a ContentProvider#onCreate() or an Application#onCreate().");
|
|
}
|
|
}
|
|
|
|
@Override // android.app.Service
|
|
public void onDestroy() {
|
|
super.onDestroy();
|
|
WorkManagerImpl workManagerImpl = this.mWorkManagerImpl;
|
|
if (workManagerImpl != null) {
|
|
workManagerImpl.getProcessor().removeExecutionListener(this);
|
|
}
|
|
}
|
|
|
|
@Override // androidx.work.impl.ExecutionListener
|
|
public void onExecuted(@NonNull String str, boolean z2) {
|
|
JobParameters remove;
|
|
Logger.get().debug(TAG, String.format("%s executed on JobScheduler", str), new Throwable[0]);
|
|
synchronized (this.mJobParameters) {
|
|
remove = this.mJobParameters.remove(str);
|
|
}
|
|
if (remove != null) {
|
|
jobFinished(remove, z2);
|
|
}
|
|
}
|
|
|
|
@Override // android.app.job.JobService
|
|
public boolean onStartJob(@NonNull JobParameters jobParameters) {
|
|
if (this.mWorkManagerImpl == null) {
|
|
Logger.get().debug(TAG, "WorkManager is not initialized; requesting retry.", new Throwable[0]);
|
|
jobFinished(jobParameters, true);
|
|
return false;
|
|
}
|
|
String workSpecIdFromJobParameters = getWorkSpecIdFromJobParameters(jobParameters);
|
|
if (TextUtils.isEmpty(workSpecIdFromJobParameters)) {
|
|
Logger.get().error(TAG, "WorkSpec id not found!", new Throwable[0]);
|
|
return false;
|
|
}
|
|
synchronized (this.mJobParameters) {
|
|
if (this.mJobParameters.containsKey(workSpecIdFromJobParameters)) {
|
|
Logger.get().debug(TAG, String.format("Job is already being executed by SystemJobService: %s", workSpecIdFromJobParameters), new Throwable[0]);
|
|
return false;
|
|
}
|
|
Logger.get().debug(TAG, String.format("onStartJob for %s", workSpecIdFromJobParameters), new Throwable[0]);
|
|
this.mJobParameters.put(workSpecIdFromJobParameters, jobParameters);
|
|
}
|
|
WorkerParameters.RuntimeExtras runtimeExtras = null;
|
|
int i = Build.VERSION.SDK_INT;
|
|
if (i >= 24) {
|
|
runtimeExtras = new WorkerParameters.RuntimeExtras();
|
|
if (jobParameters.getTriggeredContentUris() != null) {
|
|
runtimeExtras.triggeredContentUris = Arrays.asList(jobParameters.getTriggeredContentUris());
|
|
}
|
|
if (jobParameters.getTriggeredContentAuthorities() != null) {
|
|
runtimeExtras.triggeredContentAuthorities = Arrays.asList(jobParameters.getTriggeredContentAuthorities());
|
|
}
|
|
if (i >= 28) {
|
|
runtimeExtras.network = jobParameters.getNetwork();
|
|
}
|
|
}
|
|
this.mWorkManagerImpl.startWork(workSpecIdFromJobParameters, runtimeExtras);
|
|
return true;
|
|
}
|
|
|
|
@Override // android.app.job.JobService
|
|
public boolean onStopJob(@NonNull JobParameters jobParameters) {
|
|
if (this.mWorkManagerImpl == null) {
|
|
Logger.get().debug(TAG, "WorkManager is not initialized; requesting retry.", new Throwable[0]);
|
|
return true;
|
|
}
|
|
String workSpecIdFromJobParameters = getWorkSpecIdFromJobParameters(jobParameters);
|
|
if (TextUtils.isEmpty(workSpecIdFromJobParameters)) {
|
|
Logger.get().error(TAG, "WorkSpec id not found!", new Throwable[0]);
|
|
return false;
|
|
}
|
|
Logger.get().debug(TAG, String.format("onStopJob for %s", workSpecIdFromJobParameters), new Throwable[0]);
|
|
synchronized (this.mJobParameters) {
|
|
this.mJobParameters.remove(workSpecIdFromJobParameters);
|
|
}
|
|
this.mWorkManagerImpl.stopWork(workSpecIdFromJobParameters);
|
|
return !this.mWorkManagerImpl.getProcessor().isCancelled(workSpecIdFromJobParameters);
|
|
}
|
|
}
|