Worker.java revision b9701e2752b53221501cfaeb247256f1ae12baec
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.content.Context; 20import android.net.Uri; 21import android.support.annotation.Keep; 22import android.support.annotation.NonNull; 23import android.support.annotation.Nullable; 24import android.support.annotation.RequiresApi; 25import android.support.annotation.RestrictTo; 26import android.support.annotation.WorkerThread; 27 28import androidx.work.impl.RuntimeExtras; 29 30/** 31 * The basic unit of work. 32 */ 33public abstract class Worker { 34 35 public enum WorkerResult { 36 SUCCESS, 37 FAILURE, 38 RETRY 39 } 40 41 private @NonNull Context mAppContext; 42 private @NonNull String mId; 43 private @NonNull Data mInputData; 44 private @NonNull Data mOutputData = Data.EMPTY; 45 private @Nullable RuntimeExtras mRuntimeExtras; 46 47 public final @NonNull Context getAppContext() { 48 return mAppContext; 49 } 50 51 public final @NonNull String getId() { 52 return mId; 53 } 54 55 public final @NonNull Data getInputData() { 56 return mInputData; 57 } 58 59 @RequiresApi(24) 60 public final @Nullable Uri[] getTriggeredContentUris() { 61 return (mRuntimeExtras == null) ? null : mRuntimeExtras.triggeredContentUris; 62 } 63 64 @RequiresApi(24) 65 public final @Nullable String[] getTriggeredContentAuthorities() { 66 return (mRuntimeExtras == null) 67 ? null 68 : mRuntimeExtras.triggeredContentAuthorities; 69 } 70 71 /** 72 * Override this method to do your actual background processing. 73 * 74 * @return The result of the work, corresponding to a {@link WorkerResult} value. If a 75 * different value is returned, the result shall be defaulted to 76 * {@link Worker.WorkerResult#FAILURE}. 77 */ 78 @WorkerThread 79 public abstract @NonNull WorkerResult doWork(); 80 81 /** 82 * Call this method to pass an {@link Data} object to {@link WorkRequest} that is dependent on 83 * this one. Note that if there are multiple {@link Worker}s that contribute to the target, the 84 * Data will be merged together, so it is up to the developer to make sure that keys are 85 * unique. New values and types will clobber old values and types, and if there are multiple 86 * parent Workers of a child Worker, the order of clobbering may not be deterministic. 87 * 88 * This method is invoked after {@link #doWork()} returns {@link Worker.WorkerResult#SUCCESS} 89 * and there are chained jobs available. 90 * 91 * For example, if you had this structure: 92 * 93 * {@code WorkManager.getInstance(context) 94 * .enqueueWithDefaults(WorkerA.class, WorkerB.class) 95 * .then(WorkerC.class) 96 * .enqueue()} 97 * 98 * This method would be called for both WorkerA and WorkerB after their successful completion, 99 * modifying the input Data for WorkerC. 100 * 101 * @param outputData An {@link Data} object that will be merged into the input Data of any 102 * WorkRequest that is dependent on this one, or {@code null} if there is nothing 103 * to contribute 104 */ 105 public final void setOutputData(@NonNull Data outputData) { 106 mOutputData = outputData; 107 } 108 109 public final @NonNull Data getOutputData() { 110 return mOutputData; 111 } 112 113 @Keep 114 private void internalInit( 115 @NonNull Context appContext, 116 @NonNull String id, 117 @NonNull Data inputData, 118 @Nullable RuntimeExtras runtimeExtras) { 119 mAppContext = appContext; 120 mId = id; 121 mInputData = inputData; 122 mRuntimeExtras = runtimeExtras; 123 } 124 125 /** 126 * Determines if the {@link Worker} was interrupted and should stop executing. 127 * The {@link Worker} can be interrupted for the following reasons: 128 * 1. The {@link WorkRequest} or {@link PeriodicWorkRequest} was explicitly cancelled. 129 * {@link WorkManager#cancelAllWorkByTag(String)} 130 * 2. Constraints set in {@link WorkRequest} or {@link PeriodicWorkRequest} are no longer valid. 131 * @return {@code true} if {@link Worker} is instructed to stop executing. 132 */ 133 protected final boolean isInterrupted() { 134 return Thread.currentThread().isInterrupted(); 135 } 136 137 /** 138 * @hide 139 */ 140 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 141 public @Nullable RuntimeExtras getRuntimeExtras() { 142 return mRuntimeExtras; 143 } 144} 145