Sumir Kataria | a8fce8a | 2017-09-11 17:38:58 -0700 | [diff] [blame] | 1 | /* |
Sumir Kataria | 564e430 | 2018-02-14 11:22:30 -0800 | [diff] [blame] | 2 | * Copyright 2018 The Android Open Source Project |
Sumir Kataria | a8fce8a | 2017-09-11 17:38:58 -0700 | [diff] [blame] | 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 | |
Sumir Kataria | 564e430 | 2018-02-14 11:22:30 -0800 | [diff] [blame] | 17 | package androidx.work; |
Sumir Kataria | a8fce8a | 2017-09-11 17:38:58 -0700 | [diff] [blame] | 18 | |
Sumir Kataria | a8fce8a | 2017-09-11 17:38:58 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Sumir Kataria | 9e63790 | 2017-11-27 14:03:47 -0800 | [diff] [blame] | 20 | import android.support.annotation.NonNull; |
Xyan Bhatnagar | f6d4846 | 2017-11-17 10:57:53 -0800 | [diff] [blame] | 21 | import android.support.annotation.WorkerThread; |
Sumir Kataria | a8fce8a | 2017-09-11 17:38:58 -0700 | [diff] [blame] | 22 | |
Sumir Kataria | a8fce8a | 2017-09-11 17:38:58 -0700 | [diff] [blame] | 23 | /** |
| 24 | * The basic unit of work. |
Sumir Kataria | a8fce8a | 2017-09-11 17:38:58 -0700 | [diff] [blame] | 25 | */ |
Xyan Bhatnagar | d7b783e | 2017-09-22 14:21:25 -0700 | [diff] [blame] | 26 | public abstract class Worker { |
Sumir Kataria | a8fce8a | 2017-09-11 17:38:58 -0700 | [diff] [blame] | 27 | |
Sumir Kataria | d7c3325 | 2018-01-29 13:55:50 -0800 | [diff] [blame] | 28 | public enum WorkerResult { |
| 29 | SUCCESS, |
| 30 | FAILURE, |
| 31 | RETRY |
Sumir Kataria | 58e4d6f | 2017-11-14 14:31:15 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Xyan Bhatnagar | cb57128 | 2017-09-20 15:29:53 -0700 | [diff] [blame] | 34 | private Context mAppContext; |
Sumir Kataria | aecfc09 | 2017-12-18 16:28:05 -0800 | [diff] [blame] | 35 | private @NonNull String mId; |
Sumir Kataria | 9244d37 | 2017-11-30 13:39:07 -0800 | [diff] [blame] | 36 | private @NonNull Arguments mArguments; |
Sumir Kataria | 163775a | 2017-11-22 15:59:41 -0800 | [diff] [blame] | 37 | private Arguments mOutput; |
Sumir Kataria | a8fce8a | 2017-09-11 17:38:58 -0700 | [diff] [blame] | 38 | |
Sumir Kataria | da315bf | 2017-12-06 16:29:10 -0800 | [diff] [blame] | 39 | public final Context getAppContext() { |
Xyan Bhatnagar | cb57128 | 2017-09-20 15:29:53 -0700 | [diff] [blame] | 40 | return mAppContext; |
| 41 | } |
| 42 | |
Sumir Kataria | aecfc09 | 2017-12-18 16:28:05 -0800 | [diff] [blame] | 43 | public final @NonNull String getId() { |
| 44 | return mId; |
| 45 | } |
| 46 | |
Sumir Kataria | da315bf | 2017-12-06 16:29:10 -0800 | [diff] [blame] | 47 | public final @NonNull Arguments getArguments() { |
Xyan Bhatnagar | d7b783e | 2017-09-22 14:21:25 -0700 | [diff] [blame] | 48 | return mArguments; |
Xyan Bhatnagar | cb57128 | 2017-09-20 15:29:53 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Sumir Kataria | a8fce8a | 2017-09-11 17:38:58 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Override this method to do your actual background processing. |
Sumir Kataria | e89fa65 | 2017-10-04 15:00:44 -0700 | [diff] [blame] | 53 | * |
Sumir Kataria | 58e4d6f | 2017-11-14 14:31:15 -0800 | [diff] [blame] | 54 | * @return The result of the work, corresponding to a {@link WorkerResult} value. If a |
Sumir Kataria | d7c3325 | 2018-01-29 13:55:50 -0800 | [diff] [blame] | 55 | * different value is returned, the result shall be defaulted to |
| 56 | * {@link Worker.WorkerResult#FAILURE}. |
Sumir Kataria | a8fce8a | 2017-09-11 17:38:58 -0700 | [diff] [blame] | 57 | */ |
Xyan Bhatnagar | f6d4846 | 2017-11-17 10:57:53 -0800 | [diff] [blame] | 58 | @WorkerThread |
Sumir Kataria | d7c3325 | 2018-01-29 13:55:50 -0800 | [diff] [blame] | 59 | public abstract WorkerResult doWork(); |
Sumir Kataria | a8fce8a | 2017-09-11 17:38:58 -0700 | [diff] [blame] | 60 | |
Sumir Kataria | 1e9589d | 2017-11-22 13:23:46 -0800 | [diff] [blame] | 61 | /** |
Sumir Kataria | 163775a | 2017-11-22 15:59:41 -0800 | [diff] [blame] | 62 | * Call this method to pass an {@link Arguments} object to {@link Work} that is dependent on |
Sumir Kataria | 1e9589d | 2017-11-22 13:23:46 -0800 | [diff] [blame] | 63 | * 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 Kataria | d7c3325 | 2018-01-29 13:55:50 -0800 | [diff] [blame] | 68 | * This method is invoked after {@link #doWork()} returns {@link Worker.WorkerResult#SUCCESS} |
| 69 | * and there are chained jobs available. |
Sumir Kataria | 1e9589d | 2017-11-22 13:23:46 -0800 | [diff] [blame] | 70 | * |
| 71 | * For example, if you had this structure: |
| 72 | * |
Sumir Kataria | 515ed74 | 2018-02-20 13:04:06 -0800 | [diff] [blame] | 73 | * {@code WorkManager.getInstance(context) |
Sumir Kataria | 51b5cd2 | 2018-01-30 16:05:01 -0800 | [diff] [blame] | 74 | * .enqueueWithDefaults(WorkerA.class, WorkerB.class) |
| 75 | * .then(WorkerC.class) |
| 76 | * .enqueue()} |
Sumir Kataria | 1e9589d | 2017-11-22 13:23:46 -0800 | [diff] [blame] | 77 | * |
| 78 | * This method would be called for both WorkerA and WorkerB after their successful completion, |
| 79 | * modifying the input Arguments for WorkerC. |
| 80 | * |
Sumir Kataria | 163775a | 2017-11-22 15:59:41 -0800 | [diff] [blame] | 81 | * @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 Kataria | 1e9589d | 2017-11-22 13:23:46 -0800 | [diff] [blame] | 84 | */ |
Sumir Kataria | 163775a | 2017-11-22 15:59:41 -0800 | [diff] [blame] | 85 | public final void setOutput(Arguments output) { |
| 86 | mOutput = output; |
| 87 | } |
| 88 | |
Sumir Kataria | da315bf | 2017-12-06 16:29:10 -0800 | [diff] [blame] | 89 | public final Arguments getOutput() { |
Sumir Kataria | 163775a | 2017-11-22 15:59:41 -0800 | [diff] [blame] | 90 | return mOutput; |
Sumir Kataria | 1e9589d | 2017-11-22 13:23:46 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Sumir Kataria | aecfc09 | 2017-12-18 16:28:05 -0800 | [diff] [blame] | 93 | private void internalInit( |
| 94 | Context appContext, |
| 95 | @NonNull String id, |
| 96 | @NonNull Arguments arguments) { |
Sumir Kataria | 1fb3533 | 2017-09-20 17:19:51 -0700 | [diff] [blame] | 97 | mAppContext = appContext; |
Sumir Kataria | aecfc09 | 2017-12-18 16:28:05 -0800 | [diff] [blame] | 98 | mId = id; |
Xyan Bhatnagar | d7b783e | 2017-09-22 14:21:25 -0700 | [diff] [blame] | 99 | mArguments = arguments; |
Sumir Kataria | 1fb3533 | 2017-09-20 17:19:51 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Xyan Bhatnagar | 38ae5f5 | 2017-11-17 11:28:00 -0800 | [diff] [blame] | 102 | /** |
| 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 Bhatnagar | 38ae5f5 | 2017-11-17 11:28:00 -0800 | [diff] [blame] | 107 | * 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 Kataria | a8fce8a | 2017-09-11 17:38:58 -0700 | [diff] [blame] | 113 | } |