1/* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package androidx.work; 18 19import android.os.Build; 20import android.support.annotation.NonNull; 21import android.support.annotation.RequiresApi; 22 23import java.time.Duration; 24import java.util.ArrayList; 25import java.util.Arrays; 26import java.util.List; 27import java.util.concurrent.TimeUnit; 28 29/** 30 * A class that represents a request for non-repeating work. 31 */ 32 33public final class OneTimeWorkRequest extends WorkRequest { 34 35 /** 36 * Creates an array of {@link OneTimeWorkRequest} with defaults from an array of 37 * {@link Worker} class names. 38 * 39 * @param workerClasses An array of {@link Worker} class names 40 * @return A list of {@link OneTimeWorkRequest} constructed by using defaults in the 41 * {@link Builder} 42 */ 43 @SafeVarargs public static @NonNull List<OneTimeWorkRequest> from( 44 @NonNull Class<? extends Worker>... workerClasses) { 45 return from(Arrays.asList(workerClasses)); 46 } 47 48 /** 49 * Creates a list of {@link OneTimeWorkRequest} with defaults from an array of {@link Worker} 50 * class names. 51 * 52 * @param workerClasses A list of {@link Worker} class names 53 * @return A list of {@link OneTimeWorkRequest} constructed by using defaults in the {@link 54 * Builder} 55 */ 56 public static @NonNull List<OneTimeWorkRequest> from( 57 @NonNull List<Class<? extends Worker>> workerClasses) { 58 List<OneTimeWorkRequest> workList = new ArrayList<>(workerClasses.size()); 59 for (Class<? extends Worker> workerClass : workerClasses) { 60 workList.add(new OneTimeWorkRequest.Builder(workerClass).build()); 61 } 62 return workList; 63 } 64 65 OneTimeWorkRequest(Builder builder) { 66 super(builder.mId, builder.mWorkSpec, builder.mTags); 67 } 68 69 /** 70 * Builder for {@link OneTimeWorkRequest} class. 71 */ 72 public static final class Builder extends WorkRequest.Builder<Builder, OneTimeWorkRequest> { 73 74 public Builder(@NonNull Class<? extends Worker> workerClass) { 75 super(workerClass); 76 mWorkSpec.inputMergerClassName = OverwritingInputMerger.class.getName(); 77 } 78 79 /** 80 * Add an initial delay to the {@link OneTimeWorkRequest}. 81 * 82 * @param duration The length of the delay in {@code timeUnit} units 83 * @param timeUnit The units of time for {@code duration} 84 * @return The current {@link Builder} 85 */ 86 public Builder setInitialDelay(long duration, @NonNull TimeUnit timeUnit) { 87 mWorkSpec.initialDelay = timeUnit.toMillis(duration); 88 return this; 89 } 90 91 /** 92 * Add an initial delay to the {@link OneTimeWorkRequest}. 93 * 94 * @param duration The length of the delay 95 * @return The current {@link Builder} 96 */ 97 @RequiresApi(26) 98 public Builder setInitialDelay(Duration duration) { 99 mWorkSpec.initialDelay = duration.toMillis(); 100 return this; 101 } 102 103 /** 104 * Specify the {@link InputMerger} class name for this {@link OneTimeWorkRequest}. An 105 * InputMerger takes one or more {@link Data} inputs to a {@link Worker} and converts 106 * them to a single merged {@link Data} to be used as its input. 107 * The default InputMerger is {@link OverwritingInputMerger}. 108 * 109 * @param inputMerger The class name of the {@link InputMerger} for this 110 * {@link OneTimeWorkRequest} 111 * @return The current {@link Builder} 112 */ 113 public Builder setInputMerger(@NonNull Class<? extends InputMerger> inputMerger) { 114 mWorkSpec.inputMergerClassName = inputMerger.getName(); 115 return this; 116 } 117 118 119 @Override 120 public OneTimeWorkRequest build() { 121 if (mBackoffCriteriaSet 122 && Build.VERSION.SDK_INT >= 23 123 && mWorkSpec.constraints.requiresDeviceIdle()) { 124 throw new IllegalArgumentException( 125 "Cannot set backoff criteria on an idle mode job"); 126 } 127 return new OneTimeWorkRequest(this); 128 } 129 130 @Override 131 Builder getThis() { 132 return this; 133 } 134 } 135} 136