Jan Clarin | 7a8d14a | 2017-09-14 16:18:12 -0700 | [diff] [blame] | 1 | /* |
Jan Clarin | 1ecbe02 | 2017-11-13 15:19:00 -0800 | [diff] [blame] | 2 | * Copyright 2017 The Android Open Source Project |
Jan Clarin | 7a8d14a | 2017-09-14 16:18:12 -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.impl.background.systemjob; |
Jan Clarin | 7a8d14a | 2017-09-14 16:18:12 -0700 | [diff] [blame] | 18 | |
Rahul Ravikumar | a1957df | 2019-07-29 09:48:05 -0700 | [diff] [blame^] | 19 | import static androidx.work.impl.background.systemjob.SystemJobInfoConverter.EXTRA_WORK_SPEC_ID; |
| 20 | |
Sumir Kataria | de7fdf7 | 2018-07-02 15:09:37 -0700 | [diff] [blame] | 21 | import android.app.Application; |
Jan Clarin | 7a8d14a | 2017-09-14 16:18:12 -0700 | [diff] [blame] | 22 | import android.app.job.JobParameters; |
| 23 | import android.app.job.JobService; |
Sumir Kataria | 681a8d8 | 2018-03-21 10:34:32 -0700 | [diff] [blame] | 24 | import android.os.Build; |
Jan Clarin | fc84439 | 2017-11-13 13:52:57 -0800 | [diff] [blame] | 25 | import android.os.PersistableBundle; |
Xyan Bhatnagar | dc31aa8 | 2017-09-26 10:53:38 -0700 | [diff] [blame] | 26 | import android.text.TextUtils; |
Jan Clarin | 7a8d14a | 2017-09-14 16:18:12 -0700 | [diff] [blame] | 27 | |
Rahul Ravikumar | e535666 | 2019-03-04 16:22:14 -0800 | [diff] [blame] | 28 | import androidx.annotation.NonNull; |
Rahul Ravikumar | a1957df | 2019-07-29 09:48:05 -0700 | [diff] [blame^] | 29 | import androidx.annotation.Nullable; |
Rahul Ravikumar | e535666 | 2019-03-04 16:22:14 -0800 | [diff] [blame] | 30 | import androidx.annotation.RequiresApi; |
| 31 | import androidx.annotation.RestrictTo; |
Sumir Kataria | fd60d22 | 2018-06-22 16:23:41 -0700 | [diff] [blame] | 32 | import androidx.work.Logger; |
Sumir Kataria | f82a3d6 | 2018-09-12 14:18:23 -0700 | [diff] [blame] | 33 | import androidx.work.WorkerParameters; |
Sumir Kataria | 564e430 | 2018-02-14 11:22:30 -0800 | [diff] [blame] | 34 | import androidx.work.impl.ExecutionListener; |
| 35 | import androidx.work.impl.WorkManagerImpl; |
Sumir Kataria | 564e430 | 2018-02-14 11:22:30 -0800 | [diff] [blame] | 36 | |
Rahul Ravikumar | b3a76ba | 2018-10-12 14:28:06 -0700 | [diff] [blame] | 37 | import java.util.Arrays; |
Sumir Kataria | b5728f4 | 2018-03-19 12:58:41 -0700 | [diff] [blame] | 38 | import java.util.HashMap; |
| 39 | import java.util.Map; |
| 40 | |
Jan Clarin | 7a8d14a | 2017-09-14 16:18:12 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Service invoked by {@link android.app.job.JobScheduler} to run work tasks. |
Sumir Kataria | 2881509 | 2017-11-01 14:06:08 -0700 | [diff] [blame] | 43 | * |
| 44 | * @hide |
Jan Clarin | 7a8d14a | 2017-09-14 16:18:12 -0700 | [diff] [blame] | 45 | */ |
Sumir Kataria | 2881509 | 2017-11-01 14:06:08 -0700 | [diff] [blame] | 46 | @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) |
Jake Wharton | 2c830717 | 2018-06-23 01:24:14 -0400 | [diff] [blame] | 47 | @RequiresApi(WorkManagerImpl.MIN_JOB_SCHEDULER_API_LEVEL) |
Sumir Kataria | 115eb6a | 2017-09-28 16:04:07 -0700 | [diff] [blame] | 48 | public class SystemJobService extends JobService implements ExecutionListener { |
Sumir Kataria | bce6053 | 2019-01-23 00:05:10 +0000 | [diff] [blame] | 49 | private static final String TAG = Logger.tagWithPrefix("SystemJobService"); |
Sumir Kataria | c458f4a | 2018-02-08 16:52:08 -0800 | [diff] [blame] | 50 | private WorkManagerImpl mWorkManagerImpl; |
Sumir Kataria | c458f4a | 2018-02-08 16:52:08 -0800 | [diff] [blame] | 51 | private final Map<String, JobParameters> mJobParameters = new HashMap<>(); |
Jan Clarin | 9a45df6 | 2017-09-22 15:41:43 -0700 | [diff] [blame] | 52 | |
| 53 | @Override |
| 54 | public void onCreate() { |
| 55 | super.onCreate(); |
Sumir Kataria | e14104e | 2019-06-24 10:50:57 -0700 | [diff] [blame] | 56 | try { |
| 57 | mWorkManagerImpl = WorkManagerImpl.getInstance(getApplicationContext()); |
| 58 | mWorkManagerImpl.getProcessor().addExecutionListener(this); |
| 59 | } catch (IllegalStateException e) { |
Sumir Kataria | de7fdf7 | 2018-07-02 15:09:37 -0700 | [diff] [blame] | 60 | // This can occur if... |
| 61 | // 1. The app is performing an auto-backup. Prior to O, JobScheduler could erroneously |
| 62 | // try to send commands to JobService in this state (b/32180780). Since neither |
| 63 | // Application#onCreate nor ContentProviders have run, WorkManager won't be |
| 64 | // initialized. In this case, we should ignore all JobScheduler commands and tell it |
| 65 | // to retry. |
| 66 | // 2. The app is not performing auto-backup. WorkManagerInitializer has been disabled |
| 67 | // but WorkManager is not manually initialized in Application#onCreate. This is a |
| 68 | // developer error and we should throw an Exception. |
| 69 | if (!Application.class.equals(getApplication().getClass())) { |
| 70 | // During auto-backup, we don't get a custom Application subclass. This code path |
| 71 | // indicates we are either performing auto-backup or the user never used a custom |
| 72 | // Application class (or both). |
| 73 | throw new IllegalStateException("WorkManager needs to be initialized via a " |
| 74 | + "ContentProvider#onCreate() or an Application#onCreate()."); |
| 75 | } |
Sumir Kataria | e14104e | 2019-06-24 10:50:57 -0700 | [diff] [blame] | 76 | Logger.get().warning(TAG, "Could not find WorkManager instance; this may be because " |
| 77 | + "an auto-backup is in progress. Ignoring JobScheduler commands for now. " |
| 78 | + "Please make sure that you are initializing WorkManager if you have manually " |
Sumir Kataria | de7fdf7 | 2018-07-02 15:09:37 -0700 | [diff] [blame] | 79 | + "disabled WorkManagerInitializer."); |
Sumir Kataria | de7fdf7 | 2018-07-02 15:09:37 -0700 | [diff] [blame] | 80 | } |
Sumir Kataria | f0d52dd | 2018-02-06 14:23:41 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void onDestroy() { |
| 85 | super.onDestroy(); |
Sumir Kataria | de7fdf7 | 2018-07-02 15:09:37 -0700 | [diff] [blame] | 86 | if (mWorkManagerImpl != null) { |
| 87 | mWorkManagerImpl.getProcessor().removeExecutionListener(this); |
| 88 | } |
Jan Clarin | 9a45df6 | 2017-09-22 15:41:43 -0700 | [diff] [blame] | 89 | } |
Jan Clarin | 7a8d14a | 2017-09-14 16:18:12 -0700 | [diff] [blame] | 90 | |
| 91 | @Override |
Rahul Ravikumar | a1957df | 2019-07-29 09:48:05 -0700 | [diff] [blame^] | 92 | public boolean onStartJob(@NonNull JobParameters params) { |
Sumir Kataria | de7fdf7 | 2018-07-02 15:09:37 -0700 | [diff] [blame] | 93 | if (mWorkManagerImpl == null) { |
Sumir Kataria | bd134a0 | 2018-11-29 16:02:17 -0800 | [diff] [blame] | 94 | Logger.get().debug(TAG, "WorkManager is not initialized; requesting retry."); |
Sumir Kataria | de7fdf7 | 2018-07-02 15:09:37 -0700 | [diff] [blame] | 95 | jobFinished(params, true); |
| 96 | return false; |
| 97 | } |
| 98 | |
Rahul Ravikumar | a1957df | 2019-07-29 09:48:05 -0700 | [diff] [blame^] | 99 | String workSpecId = getWorkSpecIdFromJobParameters(params); |
Xyan Bhatnagar | dc31aa8 | 2017-09-26 10:53:38 -0700 | [diff] [blame] | 100 | if (TextUtils.isEmpty(workSpecId)) { |
Sumir Kataria | bd134a0 | 2018-11-29 16:02:17 -0800 | [diff] [blame] | 101 | Logger.get().error(TAG, "WorkSpec id not found!"); |
Xyan Bhatnagar | dc31aa8 | 2017-09-26 10:53:38 -0700 | [diff] [blame] | 102 | return false; |
| 103 | } |
Sumir Kataria | 5683c48 | 2017-10-25 17:03:07 -0700 | [diff] [blame] | 104 | |
Sumir Kataria | c458f4a | 2018-02-08 16:52:08 -0800 | [diff] [blame] | 105 | synchronized (mJobParameters) { |
Sumir Kataria | c68c7cd | 2018-02-20 15:06:24 -0800 | [diff] [blame] | 106 | if (mJobParameters.containsKey(workSpecId)) { |
| 107 | // This condition may happen due to our workaround for an undesired behavior in API |
| 108 | // 23. See the documentation in {@link SystemJobScheduler#schedule}. |
Sumir Kataria | bd134a0 | 2018-11-29 16:02:17 -0800 | [diff] [blame] | 109 | Logger.get().debug(TAG, String.format( |
Rahul Ravikumar | 697d6a4 | 2018-04-16 15:44:09 -0700 | [diff] [blame] | 110 | "Job is already being executed by SystemJobService: %s", workSpecId)); |
Sumir Kataria | c68c7cd | 2018-02-20 15:06:24 -0800 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | |
Rahul Ravikumar | 2aa2b40 | 2018-07-18 13:23:21 -0700 | [diff] [blame] | 114 | // We don't need to worry about the case where JobParams#isOverrideDeadlineExpired() |
| 115 | // returns true. This is because JobScheduler ensures that for PeriodicWork, constraints |
| 116 | // are actually met irrespective. |
Sumir Kataria | c68c7cd | 2018-02-20 15:06:24 -0800 | [diff] [blame] | 117 | |
Sumir Kataria | bd134a0 | 2018-11-29 16:02:17 -0800 | [diff] [blame] | 118 | Logger.get().debug(TAG, String.format("onStartJob for %s", workSpecId)); |
Sumir Kataria | c458f4a | 2018-02-08 16:52:08 -0800 | [diff] [blame] | 119 | mJobParameters.put(workSpecId, params); |
| 120 | } |
Sumir Kataria | c68c7cd | 2018-02-20 15:06:24 -0800 | [diff] [blame] | 121 | |
Sumir Kataria | f82a3d6 | 2018-09-12 14:18:23 -0700 | [diff] [blame] | 122 | WorkerParameters.RuntimeExtras runtimeExtras = null; |
Sumir Kataria | 681a8d8 | 2018-03-21 10:34:32 -0700 | [diff] [blame] | 123 | if (Build.VERSION.SDK_INT >= 24) { |
Sumir Kataria | f82a3d6 | 2018-09-12 14:18:23 -0700 | [diff] [blame] | 124 | runtimeExtras = new WorkerParameters.RuntimeExtras(); |
Rahul Ravikumar | b3a76ba | 2018-10-12 14:28:06 -0700 | [diff] [blame] | 125 | if (params.getTriggeredContentUris() != null) { |
| 126 | runtimeExtras.triggeredContentUris = |
| 127 | Arrays.asList(params.getTriggeredContentUris()); |
Sumir Kataria | 681a8d8 | 2018-03-21 10:34:32 -0700 | [diff] [blame] | 128 | } |
Rahul Ravikumar | b3a76ba | 2018-10-12 14:28:06 -0700 | [diff] [blame] | 129 | if (params.getTriggeredContentAuthorities() != null) { |
| 130 | runtimeExtras.triggeredContentAuthorities = |
| 131 | Arrays.asList(params.getTriggeredContentAuthorities()); |
| 132 | } |
Sumir Kataria | eda66d6 | 2018-05-31 15:24:48 -0700 | [diff] [blame] | 133 | if (Build.VERSION.SDK_INT >= 28) { |
| 134 | runtimeExtras.network = params.getNetwork(); |
| 135 | } |
Sumir Kataria | 681a8d8 | 2018-03-21 10:34:32 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Rahul Ravikumar | e3abf46 | 2019-01-24 10:24:46 -0800 | [diff] [blame] | 138 | // It is important that we return true, and hang on this onStartJob() request. |
| 139 | // The call to startWork() may no-op because the WorkRequest could have been picked up |
| 140 | // by the GreedyScheduler, and was already being executed. GreedyScheduler does not |
| 141 | // handle retries, and the Processor notifies all Schedulers about an intent to reschedule. |
| 142 | // In such cases, we rely on SystemJobService to ask for a reschedule by calling |
| 143 | // jobFinished(params, true) in onExecuted(...); |
| 144 | // For more information look at b/123211993 |
Sumir Kataria | bce6053 | 2019-01-23 00:05:10 +0000 | [diff] [blame] | 145 | mWorkManagerImpl.startWork(workSpecId, runtimeExtras); |
Jan Clarin | 7a8d14a | 2017-09-14 16:18:12 -0700 | [diff] [blame] | 146 | return true; |
| 147 | } |
| 148 | |
| 149 | @Override |
Rahul Ravikumar | a1957df | 2019-07-29 09:48:05 -0700 | [diff] [blame^] | 150 | public boolean onStopJob(@NonNull JobParameters params) { |
Sumir Kataria | de7fdf7 | 2018-07-02 15:09:37 -0700 | [diff] [blame] | 151 | if (mWorkManagerImpl == null) { |
Sumir Kataria | bd134a0 | 2018-11-29 16:02:17 -0800 | [diff] [blame] | 152 | Logger.get().debug(TAG, "WorkManager is not initialized; requesting retry."); |
Sumir Kataria | de7fdf7 | 2018-07-02 15:09:37 -0700 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | |
Rahul Ravikumar | a1957df | 2019-07-29 09:48:05 -0700 | [diff] [blame^] | 156 | String workSpecId = getWorkSpecIdFromJobParameters(params); |
Xyan Bhatnagar | dc31aa8 | 2017-09-26 10:53:38 -0700 | [diff] [blame] | 157 | if (TextUtils.isEmpty(workSpecId)) { |
Sumir Kataria | bd134a0 | 2018-11-29 16:02:17 -0800 | [diff] [blame] | 158 | Logger.get().error(TAG, "WorkSpec id not found!"); |
Xyan Bhatnagar | dc31aa8 | 2017-09-26 10:53:38 -0700 | [diff] [blame] | 159 | return false; |
| 160 | } |
Sumir Kataria | c458f4a | 2018-02-08 16:52:08 -0800 | [diff] [blame] | 161 | |
Sumir Kataria | bd134a0 | 2018-11-29 16:02:17 -0800 | [diff] [blame] | 162 | Logger.get().debug(TAG, String.format("onStopJob for %s", workSpecId)); |
Sumir Kataria | c458f4a | 2018-02-08 16:52:08 -0800 | [diff] [blame] | 163 | |
| 164 | synchronized (mJobParameters) { |
| 165 | mJobParameters.remove(workSpecId); |
| 166 | } |
| 167 | mWorkManagerImpl.stopWork(workSpecId); |
Sumir Kataria | 137c84c | 2018-02-13 15:13:33 -0800 | [diff] [blame] | 168 | return !mWorkManagerImpl.getProcessor().isCancelled(workSpecId); |
Xyan Bhatnagar | dc31aa8 | 2017-09-26 10:53:38 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
Sumir Kataria | 2384524 | 2018-09-05 13:54:15 -0700 | [diff] [blame] | 172 | public void onExecuted(@NonNull String workSpecId, boolean needsReschedule) { |
Sumir Kataria | bd134a0 | 2018-11-29 16:02:17 -0800 | [diff] [blame] | 173 | Logger.get().debug(TAG, String.format("%s executed on JobScheduler", workSpecId)); |
Sumir Kataria | c458f4a | 2018-02-08 16:52:08 -0800 | [diff] [blame] | 174 | JobParameters parameters; |
| 175 | synchronized (mJobParameters) { |
Sumir Kataria | 26a1f5c | 2018-08-20 15:38:18 -0700 | [diff] [blame] | 176 | parameters = mJobParameters.remove(workSpecId); |
Sumir Kataria | c458f4a | 2018-02-08 16:52:08 -0800 | [diff] [blame] | 177 | } |
| 178 | if (parameters != null) { |
| 179 | jobFinished(parameters, needsReschedule); |
| 180 | } |
Jan Clarin | 7a8d14a | 2017-09-14 16:18:12 -0700 | [diff] [blame] | 181 | } |
Rahul Ravikumar | a1957df | 2019-07-29 09:48:05 -0700 | [diff] [blame^] | 182 | |
| 183 | @Nullable |
| 184 | @SuppressWarnings("ConstantConditions") |
| 185 | private static String getWorkSpecIdFromJobParameters(@NonNull JobParameters parameters) { |
| 186 | try { |
| 187 | PersistableBundle extras = parameters.getExtras(); |
| 188 | if (extras != null && extras.containsKey(EXTRA_WORK_SPEC_ID)) { |
| 189 | return extras.getString(EXTRA_WORK_SPEC_ID); |
| 190 | } |
| 191 | } catch (NullPointerException e) { |
| 192 | // b/138441699: BaseBundle.getString sometimes throws an NPE. Ignore and return null. |
| 193 | } |
| 194 | return null; |
| 195 | } |
Jan Clarin | 7a8d14a | 2017-09-14 16:18:12 -0700 | [diff] [blame] | 196 | } |