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