123 lines
5.2 KiB
Java
123 lines
5.2 KiB
Java
package androidx.work.impl.background.systemjob;
|
|
|
|
import android.app.job.JobInfo;
|
|
import android.content.ComponentName;
|
|
import android.content.Context;
|
|
import android.os.Build;
|
|
import android.os.PersistableBundle;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.RequiresApi;
|
|
import androidx.annotation.RestrictTo;
|
|
import androidx.annotation.VisibleForTesting;
|
|
import androidx.work.BackoffPolicy;
|
|
import androidx.work.Constraints;
|
|
import androidx.work.ContentUriTriggers;
|
|
import androidx.work.Logger;
|
|
import androidx.work.NetworkType;
|
|
import androidx.work.impl.model.WorkSpec;
|
|
@RequiresApi(api = 23)
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP})
|
|
public class SystemJobInfoConverter {
|
|
public static final String EXTRA_IS_PERIODIC = "EXTRA_IS_PERIODIC";
|
|
public static final String EXTRA_WORK_SPEC_ID = "EXTRA_WORK_SPEC_ID";
|
|
private static final String TAG = Logger.tagWithPrefix("SystemJobInfoConverter");
|
|
private final ComponentName mWorkServiceComponent;
|
|
|
|
/* renamed from: androidx.work.impl.background.systemjob.SystemJobInfoConverter$1 reason: invalid class name */
|
|
public static /* synthetic */ class AnonymousClass1 {
|
|
public static final /* synthetic */ int[] $SwitchMap$androidx$work$NetworkType;
|
|
|
|
static {
|
|
NetworkType.values();
|
|
int[] iArr = new int[5];
|
|
$SwitchMap$androidx$work$NetworkType = iArr;
|
|
try {
|
|
iArr[NetworkType.NOT_REQUIRED.ordinal()] = 1;
|
|
} catch (NoSuchFieldError unused) {
|
|
}
|
|
try {
|
|
$SwitchMap$androidx$work$NetworkType[NetworkType.CONNECTED.ordinal()] = 2;
|
|
} catch (NoSuchFieldError unused2) {
|
|
}
|
|
try {
|
|
$SwitchMap$androidx$work$NetworkType[NetworkType.UNMETERED.ordinal()] = 3;
|
|
} catch (NoSuchFieldError unused3) {
|
|
}
|
|
try {
|
|
$SwitchMap$androidx$work$NetworkType[NetworkType.NOT_ROAMING.ordinal()] = 4;
|
|
} catch (NoSuchFieldError unused4) {
|
|
}
|
|
try {
|
|
$SwitchMap$androidx$work$NetworkType[NetworkType.METERED.ordinal()] = 5;
|
|
} catch (NoSuchFieldError unused5) {
|
|
}
|
|
}
|
|
}
|
|
|
|
@VisibleForTesting(otherwise = 3)
|
|
public SystemJobInfoConverter(@NonNull Context context) {
|
|
this.mWorkServiceComponent = new ComponentName(context.getApplicationContext(), SystemJobService.class);
|
|
}
|
|
|
|
@RequiresApi(24)
|
|
private static JobInfo.TriggerContentUri convertContentUriTrigger(ContentUriTriggers.Trigger trigger) {
|
|
return new JobInfo.TriggerContentUri(trigger.getUri(), trigger.shouldTriggerForDescendants() ? 1 : 0);
|
|
}
|
|
|
|
public static int convertNetworkType(NetworkType networkType) {
|
|
int ordinal = networkType.ordinal();
|
|
if (ordinal == 0) {
|
|
return 0;
|
|
}
|
|
if (ordinal == 1) {
|
|
return 1;
|
|
}
|
|
if (ordinal == 2) {
|
|
return 2;
|
|
}
|
|
if (ordinal != 3) {
|
|
if (ordinal == 4 && Build.VERSION.SDK_INT >= 26) {
|
|
return 4;
|
|
}
|
|
} else if (Build.VERSION.SDK_INT >= 24) {
|
|
return 3;
|
|
}
|
|
Logger.get().debug(TAG, String.format("API version too low. Cannot convert network type value %s", networkType), new Throwable[0]);
|
|
return 1;
|
|
}
|
|
|
|
public JobInfo convert(WorkSpec workSpec, int i) {
|
|
Constraints constraints = workSpec.constraints;
|
|
int convertNetworkType = convertNetworkType(constraints.getRequiredNetworkType());
|
|
PersistableBundle persistableBundle = new PersistableBundle();
|
|
persistableBundle.putString(EXTRA_WORK_SPEC_ID, workSpec.f29id);
|
|
persistableBundle.putBoolean(EXTRA_IS_PERIODIC, workSpec.isPeriodic());
|
|
JobInfo.Builder extras = new JobInfo.Builder(i, this.mWorkServiceComponent).setRequiredNetworkType(convertNetworkType).setRequiresCharging(constraints.requiresCharging()).setRequiresDeviceIdle(constraints.requiresDeviceIdle()).setExtras(persistableBundle);
|
|
if (!constraints.requiresDeviceIdle()) {
|
|
extras.setBackoffCriteria(workSpec.backoffDelayDuration, workSpec.backoffPolicy == BackoffPolicy.LINEAR ? 0 : 1);
|
|
}
|
|
long max = Math.max(workSpec.calculateNextRunTime() - System.currentTimeMillis(), 0L);
|
|
int i2 = Build.VERSION.SDK_INT;
|
|
if (i2 <= 28) {
|
|
extras.setMinimumLatency(max);
|
|
} else if (max > 0) {
|
|
extras.setMinimumLatency(max);
|
|
} else {
|
|
extras.setImportantWhileForeground(true);
|
|
}
|
|
if (i2 >= 24 && constraints.hasContentUriTriggers()) {
|
|
for (ContentUriTriggers.Trigger trigger : constraints.getContentUriTriggers().getTriggers()) {
|
|
extras.addTriggerContentUri(convertContentUriTrigger(trigger));
|
|
}
|
|
extras.setTriggerContentUpdateDelay(constraints.getTriggerContentUpdateDelay());
|
|
extras.setTriggerContentMaxDelay(constraints.getTriggerMaxContentDelay());
|
|
}
|
|
extras.setPersisted(false);
|
|
if (Build.VERSION.SDK_INT >= 26) {
|
|
extras.setRequiresBatteryNotLow(constraints.requiresBatteryNotLow());
|
|
extras.setRequiresStorageNotLow(constraints.requiresStorageNotLow());
|
|
}
|
|
return extras.build();
|
|
}
|
|
}
|