blob: 8dcb8beb659c193a93acccce151289286ab482ad [file] [log] [blame]
Sumir Katariaa8fce8a2017-09-11 17:38:58 -07001/*
Sumir Kataria564e4302018-02-14 11:22:30 -08002 * Copyright 2018 The Android Open Source Project
Sumir Katariaa8fce8a2017-09-11 17:38:58 -07003 *
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
Sumir Kataria564e4302018-02-14 11:22:30 -080017package androidx.work;
Sumir Katariaa8fce8a2017-09-11 17:38:58 -070018
Sumir Katariaa8fce8a2017-09-11 17:38:58 -070019import android.content.Context;
Sumir Kataria9e637902017-11-27 14:03:47 -080020import android.support.annotation.NonNull;
Xyan Bhatnagarf6d48462017-11-17 10:57:53 -080021import android.support.annotation.WorkerThread;
Sumir Katariaa8fce8a2017-09-11 17:38:58 -070022
Sumir Katariaa8fce8a2017-09-11 17:38:58 -070023/**
24 * The basic unit of work.
Sumir Katariaa8fce8a2017-09-11 17:38:58 -070025 */
Xyan Bhatnagard7b783e2017-09-22 14:21:25 -070026public abstract class Worker {
Sumir Katariaa8fce8a2017-09-11 17:38:58 -070027
Sumir Katariad7c33252018-01-29 13:55:50 -080028 public enum WorkerResult {
29 SUCCESS,
30 FAILURE,
31 RETRY
Sumir Kataria58e4d6f2017-11-14 14:31:15 -080032 }
33
Xyan Bhatnagarcb571282017-09-20 15:29:53 -070034 private Context mAppContext;
Sumir Katariaaecfc092017-12-18 16:28:05 -080035 private @NonNull String mId;
Sumir Kataria9244d372017-11-30 13:39:07 -080036 private @NonNull Arguments mArguments;
Sumir Kataria163775a2017-11-22 15:59:41 -080037 private Arguments mOutput;
Sumir Katariaa8fce8a2017-09-11 17:38:58 -070038
Sumir Katariada315bf2017-12-06 16:29:10 -080039 public final Context getAppContext() {
Xyan Bhatnagarcb571282017-09-20 15:29:53 -070040 return mAppContext;
41 }
42
Sumir Katariaaecfc092017-12-18 16:28:05 -080043 public final @NonNull String getId() {
44 return mId;
45 }
46
Sumir Katariada315bf2017-12-06 16:29:10 -080047 public final @NonNull Arguments getArguments() {
Xyan Bhatnagard7b783e2017-09-22 14:21:25 -070048 return mArguments;
Xyan Bhatnagarcb571282017-09-20 15:29:53 -070049 }
50
Sumir Katariaa8fce8a2017-09-11 17:38:58 -070051 /**
52 * Override this method to do your actual background processing.
Sumir Katariae89fa652017-10-04 15:00:44 -070053 *
Sumir Kataria58e4d6f2017-11-14 14:31:15 -080054 * @return The result of the work, corresponding to a {@link WorkerResult} value. If a
Sumir Katariad7c33252018-01-29 13:55:50 -080055 * different value is returned, the result shall be defaulted to
56 * {@link Worker.WorkerResult#FAILURE}.
Sumir Katariaa8fce8a2017-09-11 17:38:58 -070057 */
Xyan Bhatnagarf6d48462017-11-17 10:57:53 -080058 @WorkerThread
Sumir Katariad7c33252018-01-29 13:55:50 -080059 public abstract WorkerResult doWork();
Sumir Katariaa8fce8a2017-09-11 17:38:58 -070060
Sumir Kataria1e9589d2017-11-22 13:23:46 -080061 /**
Sumir Kataria163775a2017-11-22 15:59:41 -080062 * Call this method to pass an {@link Arguments} object to {@link Work} that is dependent on
Sumir Kataria1e9589d2017-11-22 13:23:46 -080063 * this one. Note that if there are multiple {@link Worker}s that contribute to the target, the
64 * Arguments will be merged together, so it is up to the developer to make sure that keys are
65 * unique. New values and types will clobber old values and types, and if there are multiple
66 * parent Workers of a child Worker, the order of clobbering may not be deterministic.
67 *
Sumir Katariad7c33252018-01-29 13:55:50 -080068 * This method is invoked after {@link #doWork()} returns {@link Worker.WorkerResult#SUCCESS}
69 * and there are chained jobs available.
Sumir Kataria1e9589d2017-11-22 13:23:46 -080070 *
71 * For example, if you had this structure:
72 *
Sumir Kataria515ed742018-02-20 13:04:06 -080073 * {@code WorkManager.getInstance(context)
Sumir Kataria51b5cd22018-01-30 16:05:01 -080074 * .enqueueWithDefaults(WorkerA.class, WorkerB.class)
75 * .then(WorkerC.class)
76 * .enqueue()}
Sumir Kataria1e9589d2017-11-22 13:23:46 -080077 *
78 * This method would be called for both WorkerA and WorkerB after their successful completion,
79 * modifying the input Arguments for WorkerC.
80 *
Sumir Kataria163775a2017-11-22 15:59:41 -080081 * @param output An {@link Arguments} object that will be merged into the input Arguments of any
82 * Work that is dependent on this one, or {@code null} if there is nothing to
83 * contribute
Sumir Kataria1e9589d2017-11-22 13:23:46 -080084 */
Sumir Kataria163775a2017-11-22 15:59:41 -080085 public final void setOutput(Arguments output) {
86 mOutput = output;
87 }
88
Sumir Katariada315bf2017-12-06 16:29:10 -080089 public final Arguments getOutput() {
Sumir Kataria163775a2017-11-22 15:59:41 -080090 return mOutput;
Sumir Kataria1e9589d2017-11-22 13:23:46 -080091 }
92
Sumir Katariaaecfc092017-12-18 16:28:05 -080093 private void internalInit(
94 Context appContext,
95 @NonNull String id,
96 @NonNull Arguments arguments) {
Sumir Kataria1fb35332017-09-20 17:19:51 -070097 mAppContext = appContext;
Sumir Katariaaecfc092017-12-18 16:28:05 -080098 mId = id;
Xyan Bhatnagard7b783e2017-09-22 14:21:25 -070099 mArguments = arguments;
Sumir Kataria1fb35332017-09-20 17:19:51 -0700100 }
101
Xyan Bhatnagar38ae5f52017-11-17 11:28:00 -0800102 /**
103 * Determines if the {@link Worker} was interrupted and should stop executing.
104 * The {@link Worker} can be interrupted for the following reasons:
105 * 1. The {@link Work} or {@link PeriodicWork} was explicitly cancelled.
106 * {@link WorkManager#cancelAllWorkWithTag(String)}
Xyan Bhatnagar38ae5f52017-11-17 11:28:00 -0800107 * 2. Constraints set in {@link Work} or {@link PeriodicWork} are no longer valid.
108 * @return {@code true} if {@link Worker} is instructed to stop executing.
109 */
110 protected final boolean isInterrupted() {
111 return Thread.currentThread().isInterrupted();
112 }
Sumir Katariaa8fce8a2017-09-11 17:38:58 -0700113}