Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 17 | package com.android.server.job; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 18 | |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 20 | import android.app.job.JobInfo; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 21 | import android.app.job.JobParameters; |
| 22 | import android.app.job.IJobCallback; |
| 23 | import android.app.job.IJobService; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 24 | import android.app.job.JobWorkItem; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 25 | import android.content.ComponentName; |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 28 | import android.content.ServiceConnection; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 29 | import android.net.Uri; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 30 | import android.os.Binder; |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 31 | import android.os.Handler; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 32 | import android.os.IBinder; |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 33 | import android.os.Looper; |
| 34 | import android.os.Message; |
| 35 | import android.os.PowerManager; |
| 36 | import android.os.RemoteException; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 37 | import android.os.SystemClock; |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 38 | import android.os.UserHandle; |
| 39 | import android.os.WorkSource; |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 40 | import android.util.Slog; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 41 | |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 42 | import com.android.internal.annotations.GuardedBy; |
| 43 | import com.android.internal.annotations.VisibleForTesting; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 44 | import com.android.internal.app.IBatteryStats; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 45 | import com.android.server.job.controllers.JobStatus; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 46 | |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 47 | import java.util.concurrent.atomic.AtomicBoolean; |
| 48 | |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 49 | /** |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 50 | * Handles client binding and lifecycle of a job. Jobs execute one at a time on an instance of this |
| 51 | * class. |
| 52 | * |
| 53 | * There are two important interactions into this class from the |
| 54 | * {@link com.android.server.job.JobSchedulerService}. To execute a job and to cancel a job. |
| 55 | * - Execution of a new job is handled by the {@link #mAvailable}. This bit is flipped once when a |
| 56 | * job lands, and again when it is complete. |
| 57 | * - Cancelling is trickier, because there are also interactions from the client. It's possible |
| 58 | * the {@link com.android.server.job.JobServiceContext.JobServiceHandler} tries to process a |
| 59 | * {@link #MSG_CANCEL} after the client has already finished. This is handled by having |
| 60 | * {@link com.android.server.job.JobServiceContext.JobServiceHandler#handleCancelH} check whether |
| 61 | * the context is still valid. |
| 62 | * To mitigate this, tearing down the context removes all messages from the handler, including any |
| 63 | * tardy {@link #MSG_CANCEL}s. Additionally, we avoid sending duplicate onStopJob() |
| 64 | * calls to the client after they've specified jobFinished(). |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 65 | */ |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 66 | public class JobServiceContext extends IJobCallback.Stub implements ServiceConnection { |
Matthew Williams | fa8e508 | 2015-10-15 15:59:12 -0700 | [diff] [blame] | 67 | private static final boolean DEBUG = JobSchedulerService.DEBUG; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 68 | private static final String TAG = "JobServiceContext"; |
| 69 | /** Define the maximum # of jobs allowed to run on a service at once. */ |
| 70 | private static final int defaultMaxActiveJobsPerService = |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 71 | ActivityManager.isLowRamDeviceStatic() ? 1 : 3; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 72 | /** Amount of time a job is allowed to execute for before being considered timed-out. */ |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 73 | private static final long EXECUTING_TIMESLICE_MILLIS = 10 * 60 * 1000; // 10mins. |
Christopher Tate | 2022bad | 2017-02-02 18:07:42 -0800 | [diff] [blame] | 74 | /** Amount of time the JobScheduler waits for the initial service launch+bind. */ |
| 75 | private static final long OP_BIND_TIMEOUT_MILLIS = 18 * 1000; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 76 | /** Amount of time the JobScheduler will wait for a response from an app for a message. */ |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 77 | private static final long OP_TIMEOUT_MILLIS = 8 * 1000; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 78 | |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 79 | private static final String[] VERB_STRINGS = { |
riddle_hsu | 6a94aba | 2015-05-01 01:52:58 +0800 | [diff] [blame] | 80 | "VERB_BINDING", "VERB_STARTING", "VERB_EXECUTING", "VERB_STOPPING", "VERB_FINISHED" |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 83 | // States that a job occupies while interacting with the client. |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 84 | static final int VERB_BINDING = 0; |
| 85 | static final int VERB_STARTING = 1; |
| 86 | static final int VERB_EXECUTING = 2; |
| 87 | static final int VERB_STOPPING = 3; |
riddle_hsu | 6a94aba | 2015-05-01 01:52:58 +0800 | [diff] [blame] | 88 | static final int VERB_FINISHED = 4; |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 89 | |
| 90 | // Messages that result from interactions with the client service. |
| 91 | /** System timed out waiting for a response. */ |
| 92 | private static final int MSG_TIMEOUT = 0; |
| 93 | /** Received a callback from client. */ |
| 94 | private static final int MSG_CALLBACK = 1; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 95 | /** Run through list and start any ready jobs.*/ |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 96 | private static final int MSG_SERVICE_BOUND = 2; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 97 | /** Cancel a job. */ |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 98 | private static final int MSG_CANCEL = 3; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 99 | /** Shutdown the job. Used when the client crashes and we can't die gracefully.*/ |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 100 | private static final int MSG_SHUTDOWN_EXECUTION = 4; |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 101 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 102 | public static final int NO_PREFERRED_UID = -1; |
| 103 | |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 104 | private final Handler mCallbackHandler; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 105 | /** Make callbacks to {@link JobSchedulerService} to inform on job completion status. */ |
| 106 | private final JobCompletedListener mCompletedListener; |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 107 | /** Used for service binding, etc. */ |
| 108 | private final Context mContext; |
Dianne Hackborn | d506b2b | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 109 | private final Object mLock; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 110 | private final IBatteryStats mBatteryStats; |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 111 | private final JobPackageTracker mJobPackageTracker; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 112 | private PowerManager.WakeLock mWakeLock; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 113 | |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 114 | // Execution state. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 115 | private JobParameters mParams; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 116 | @VisibleForTesting |
| 117 | int mVerb; |
| 118 | private AtomicBoolean mCancelled = new AtomicBoolean(); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 119 | |
Matthew Williams | 0cc7654 | 2015-10-16 21:04:51 -0700 | [diff] [blame] | 120 | /** |
| 121 | * All the information maintained about the job currently being executed. |
| 122 | * |
| 123 | * Any reads (dereferences) not done from the handler thread must be synchronized on |
| 124 | * {@link #mLock}. |
| 125 | * Writes can only be done from the handler thread, or {@link #executeRunnableJob(JobStatus)}. |
| 126 | */ |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 127 | private JobStatus mRunningJob; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 128 | /** Used to store next job to run when current job is to be preempted. */ |
| 129 | private int mPreferredUid; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 130 | IJobService service; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 131 | |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 132 | /** |
| 133 | * Whether this context is free. This is set to false at the start of execution, and reset to |
| 134 | * true when execution is complete. |
| 135 | */ |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 136 | @GuardedBy("mLock") |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 137 | private boolean mAvailable; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 138 | /** Track start time. */ |
| 139 | private long mExecutionStartTimeElapsed; |
| 140 | /** Track when job will timeout. */ |
| 141 | private long mTimeoutElapsed; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 142 | |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 143 | JobServiceContext(JobSchedulerService service, IBatteryStats batteryStats, |
| 144 | JobPackageTracker tracker, Looper looper) { |
| 145 | this(service.getContext(), service.getLock(), batteryStats, tracker, service, looper); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @VisibleForTesting |
Dianne Hackborn | d506b2b | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 149 | JobServiceContext(Context context, Object lock, IBatteryStats batteryStats, |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 150 | JobPackageTracker tracker, JobCompletedListener completedListener, Looper looper) { |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 151 | mContext = context; |
Dianne Hackborn | d506b2b | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 152 | mLock = lock; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 153 | mBatteryStats = batteryStats; |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 154 | mJobPackageTracker = tracker; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 155 | mCallbackHandler = new JobServiceHandler(looper); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 156 | mCompletedListener = completedListener; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 157 | mAvailable = true; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 158 | mVerb = VERB_FINISHED; |
| 159 | mPreferredUid = NO_PREFERRED_UID; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 163 | * Give a job to this context for execution. Callers must first check {@link #getRunningJob()} |
| 164 | * and ensure it is null to make sure this is a valid context. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 165 | * @param job The status of the job that we are going to run. |
| 166 | * @return True if the job is valid and is running. False if the job cannot be executed. |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 167 | */ |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 168 | boolean executeRunnableJob(JobStatus job) { |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 169 | synchronized (mLock) { |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 170 | if (!mAvailable) { |
| 171 | Slog.e(TAG, "Starting new runnable but context is unavailable > Error."); |
| 172 | return false; |
| 173 | } |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 174 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 175 | mPreferredUid = NO_PREFERRED_UID; |
| 176 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 177 | mRunningJob = job; |
Matthew Williams | 673ef9e | 2015-04-03 21:52:02 -0700 | [diff] [blame] | 178 | final boolean isDeadlineExpired = |
Matthew Williams | d9db9d7 | 2015-05-14 17:47:33 -0700 | [diff] [blame] | 179 | job.hasDeadlineConstraint() && |
| 180 | (job.getLatestRunTimeElapsed() < SystemClock.elapsedRealtime()); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 181 | Uri[] triggeredUris = null; |
| 182 | if (job.changedUris != null) { |
| 183 | triggeredUris = new Uri[job.changedUris.size()]; |
| 184 | job.changedUris.toArray(triggeredUris); |
| 185 | } |
| 186 | String[] triggeredAuthorities = null; |
| 187 | if (job.changedAuthorities != null) { |
| 188 | triggeredAuthorities = new String[job.changedAuthorities.size()]; |
| 189 | job.changedAuthorities.toArray(triggeredAuthorities); |
| 190 | } |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 191 | final JobInfo ji = job.getJob(); |
| 192 | mParams = new JobParameters(this, job.getJobId(), ji.getExtras(), |
| 193 | ji.getTransientExtras(), ji.getClipData(), ji.getClipGrantFlags(), |
| 194 | isDeadlineExpired, triggeredUris, triggeredAuthorities); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 195 | mExecutionStartTimeElapsed = SystemClock.elapsedRealtime(); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 196 | |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 197 | mVerb = VERB_BINDING; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 198 | scheduleOpTimeOutLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 199 | final Intent intent = new Intent().setComponent(job.getServiceComponent()); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 200 | boolean binding = mContext.bindServiceAsUser(intent, this, |
| 201 | Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND, |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 202 | new UserHandle(job.getUserId())); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 203 | if (!binding) { |
| 204 | if (DEBUG) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 205 | Slog.d(TAG, job.getServiceComponent().getShortClassName() + " unavailable."); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 206 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 207 | mRunningJob = null; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 208 | mParams = null; |
| 209 | mExecutionStartTimeElapsed = 0L; |
riddle_hsu | 6a94aba | 2015-05-01 01:52:58 +0800 | [diff] [blame] | 210 | mVerb = VERB_FINISHED; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 211 | removeOpTimeOutLocked(); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 212 | return false; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 213 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 214 | try { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 215 | mBatteryStats.noteJobStart(job.getBatteryName(), job.getSourceUid()); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 216 | } catch (RemoteException e) { |
| 217 | // Whatever. |
| 218 | } |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 219 | mJobPackageTracker.noteActive(job); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 220 | mAvailable = false; |
| 221 | return true; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 222 | } |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 225 | /** |
| 226 | * Used externally to query the running job. Will return null if there is no job running. |
| 227 | * Be careful when using this function, at any moment it's possible that the job returned may |
| 228 | * stop executing. |
| 229 | */ |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 230 | JobStatus getRunningJob() { |
Christopher Tate | 3eddecc | 2016-05-25 10:44:24 -0700 | [diff] [blame] | 231 | final JobStatus job; |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 232 | synchronized (mLock) { |
Christopher Tate | 3eddecc | 2016-05-25 10:44:24 -0700 | [diff] [blame] | 233 | job = mRunningJob; |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 234 | } |
Christopher Tate | 3eddecc | 2016-05-25 10:44:24 -0700 | [diff] [blame] | 235 | return job == null ? null : new JobStatus(job); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Christopher Tate | eafb535 | 2016-10-04 16:34:48 -0700 | [diff] [blame] | 238 | /** |
| 239 | * Internal non-cloning inspection of the currently running job, if any. The lock |
| 240 | * must be held when calling this *and* for the entire lifetime of using its returned |
| 241 | * JobStatus object! |
| 242 | */ |
| 243 | JobStatus getRunningJobUnsafeLocked() { |
| 244 | return mRunningJob; |
| 245 | } |
| 246 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 247 | /** Called externally when a job that was scheduled for execution should be cancelled. */ |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 248 | void cancelExecutingJob(int reason) { |
| 249 | mCallbackHandler.obtainMessage(MSG_CANCEL, reason, 0 /* unused */).sendToTarget(); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 252 | void preemptExecutingJob() { |
| 253 | Message m = mCallbackHandler.obtainMessage(MSG_CANCEL); |
| 254 | m.arg1 = JobParameters.REASON_PREEMPT; |
| 255 | m.sendToTarget(); |
| 256 | } |
| 257 | |
| 258 | int getPreferredUid() { |
| 259 | return mPreferredUid; |
| 260 | } |
| 261 | |
| 262 | void clearPreferredUid() { |
| 263 | mPreferredUid = NO_PREFERRED_UID; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 266 | long getExecutionStartTimeElapsed() { |
| 267 | return mExecutionStartTimeElapsed; |
| 268 | } |
| 269 | |
| 270 | long getTimeoutElapsed() { |
| 271 | return mTimeoutElapsed; |
| 272 | } |
| 273 | |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 274 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 275 | public void jobFinished(int jobId, boolean reschedule) { |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 276 | if (!verifyCallingUid()) { |
| 277 | return; |
| 278 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 279 | mCallbackHandler.obtainMessage(MSG_CALLBACK, jobId, reschedule ? 1 : 0) |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 280 | .sendToTarget(); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 284 | public void acknowledgeStopMessage(int jobId, boolean reschedule) { |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 285 | if (!verifyCallingUid()) { |
| 286 | return; |
| 287 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 288 | mCallbackHandler.obtainMessage(MSG_CALLBACK, jobId, reschedule ? 1 : 0) |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 289 | .sendToTarget(); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 293 | public void acknowledgeStartMessage(int jobId, boolean ongoing) { |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 294 | if (!verifyCallingUid()) { |
| 295 | return; |
| 296 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 297 | mCallbackHandler.obtainMessage(MSG_CALLBACK, jobId, ongoing ? 1 : 0).sendToTarget(); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 298 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 299 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 300 | @Override |
| 301 | public JobWorkItem dequeueWork(int jobId) { |
| 302 | if (!verifyCallingUid()) { |
| 303 | throw new SecurityException("Bad calling uid: " + Binder.getCallingUid()); |
| 304 | } |
| 305 | JobWorkItem work = null; |
| 306 | boolean stillWorking = false; |
| 307 | synchronized (mLock) { |
| 308 | if (mRunningJob != null) { |
| 309 | work = mRunningJob.dequeueWorkLocked(); |
| 310 | stillWorking = mRunningJob.hasExecutingWorkLocked(); |
| 311 | } |
| 312 | } |
| 313 | if (work == null && !stillWorking) { |
| 314 | jobFinished(jobId, false); |
| 315 | } |
| 316 | return work; |
| 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public boolean completeWork(int jobId, int workId) { |
| 321 | if (!verifyCallingUid()) { |
| 322 | throw new SecurityException("Bad calling uid: " + Binder.getCallingUid()); |
| 323 | } |
| 324 | synchronized (mLock) { |
| 325 | if (mRunningJob != null) { |
| 326 | return mRunningJob.completeWorkLocked(workId); |
| 327 | } |
| 328 | return false; |
| 329 | } |
| 330 | } |
| 331 | |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 332 | /** |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 333 | * We acquire/release a wakelock on onServiceConnected/unbindService. This mirrors the work |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 334 | * we intend to send to the client - we stop sending work when the service is unbound so until |
| 335 | * then we keep the wakelock. |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 336 | * @param name The concrete component name of the service that has been connected. |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 337 | * @param service The IBinder of the Service's communication channel, |
| 338 | */ |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 339 | @Override |
| 340 | public void onServiceConnected(ComponentName name, IBinder service) { |
Matthew Williams | 0cc7654 | 2015-10-16 21:04:51 -0700 | [diff] [blame] | 341 | JobStatus runningJob; |
| 342 | synchronized (mLock) { |
| 343 | // This isn't strictly necessary b/c the JobServiceHandler is running on the main |
| 344 | // looper and at this point we can't get any binder callbacks from the client. Better |
| 345 | // safe than sorry. |
| 346 | runningJob = mRunningJob; |
| 347 | } |
| 348 | if (runningJob == null || !name.equals(runningJob.getServiceComponent())) { |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 349 | mCallbackHandler.obtainMessage(MSG_SHUTDOWN_EXECUTION).sendToTarget(); |
| 350 | return; |
| 351 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 352 | this.service = IJobService.Stub.asInterface(service); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 353 | final PowerManager pm = |
| 354 | (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
Christopher Tate | 4b17e98 | 2016-09-26 12:59:10 -0700 | [diff] [blame] | 355 | PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, |
| 356 | runningJob.getTag()); |
| 357 | wl.setWorkSource(new WorkSource(runningJob.getSourceUid())); |
| 358 | wl.setReferenceCounted(false); |
| 359 | wl.acquire(); |
| 360 | synchronized (mLock) { |
| 361 | // We use a new wakelock instance per job. In rare cases there is a race between |
| 362 | // teardown following job completion/cancellation and new job service spin-up |
| 363 | // such that if we simply assign mWakeLock to be the new instance, we orphan |
| 364 | // the currently-live lock instead of cleanly replacing it. Watch for this and |
| 365 | // explicitly fast-forward the release if we're in that situation. |
| 366 | if (mWakeLock != null) { |
| 367 | Slog.w(TAG, "Bound new job " + runningJob + " but live wakelock " + mWakeLock |
| 368 | + " tag=" + mWakeLock.getTag()); |
| 369 | mWakeLock.release(); |
| 370 | } |
| 371 | mWakeLock = wl; |
| 372 | } |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 373 | mCallbackHandler.obtainMessage(MSG_SERVICE_BOUND).sendToTarget(); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 376 | /** If the client service crashes we reschedule this job and clean up. */ |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 377 | @Override |
| 378 | public void onServiceDisconnected(ComponentName name) { |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 379 | mCallbackHandler.obtainMessage(MSG_SHUTDOWN_EXECUTION).sendToTarget(); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /** |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 383 | * This class is reused across different clients, and passes itself in as a callback. Check |
| 384 | * whether the client exercising the callback is the client we expect. |
| 385 | * @return True if the binder calling is coming from the client we expect. |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 386 | */ |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 387 | private boolean verifyCallingUid() { |
Matthew Williams | 0cc7654 | 2015-10-16 21:04:51 -0700 | [diff] [blame] | 388 | synchronized (mLock) { |
| 389 | if (mRunningJob == null || Binder.getCallingUid() != mRunningJob.getUid()) { |
| 390 | if (DEBUG) { |
| 391 | Slog.d(TAG, "Stale callback received, ignoring."); |
| 392 | } |
| 393 | return false; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 394 | } |
Matthew Williams | 0cc7654 | 2015-10-16 21:04:51 -0700 | [diff] [blame] | 395 | return true; |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 396 | } |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 400 | * Handles the lifecycle of the JobService binding/callbacks, etc. The convention within this |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 401 | * class is to append 'H' to each function name that can only be called on this handler. This |
| 402 | * isn't strictly necessary because all of these functions are private, but helps clarity. |
| 403 | */ |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 404 | private class JobServiceHandler extends Handler { |
| 405 | JobServiceHandler(Looper looper) { |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 406 | super(looper); |
| 407 | } |
| 408 | |
| 409 | @Override |
| 410 | public void handleMessage(Message message) { |
| 411 | switch (message.what) { |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 412 | case MSG_SERVICE_BOUND: |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 413 | doServiceBound(); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 414 | break; |
| 415 | case MSG_CALLBACK: |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 416 | doCallback(message.arg2); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 417 | break; |
| 418 | case MSG_CANCEL: |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 419 | doCancel(message.arg1); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 420 | break; |
| 421 | case MSG_TIMEOUT: |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 422 | synchronized (mLock) { |
| 423 | handleOpTimeoutH(); |
| 424 | } |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 425 | break; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 426 | case MSG_SHUTDOWN_EXECUTION: |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 427 | closeAndCleanupJobH(true /* needsReschedule */); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 428 | break; |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 429 | default: |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 430 | Slog.e(TAG, "Unrecognised message: " + message); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 434 | void doServiceBound() { |
| 435 | synchronized (mLock) { |
| 436 | removeOpTimeOutLocked(); |
| 437 | handleServiceBoundH(); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | void doCallback(int arg2) { |
| 442 | synchronized (mLock) { |
| 443 | if (DEBUG) { |
| 444 | Slog.d(TAG, "MSG_CALLBACK of : " + mRunningJob |
| 445 | + " v:" + VERB_STRINGS[mVerb]); |
| 446 | } |
| 447 | removeOpTimeOutLocked(); |
| 448 | |
| 449 | if (mVerb == VERB_STARTING) { |
| 450 | final boolean workOngoing = arg2 == 1; |
| 451 | handleStartedH(workOngoing); |
| 452 | } else if (mVerb == VERB_EXECUTING || |
| 453 | mVerb == VERB_STOPPING) { |
| 454 | final boolean reschedule = arg2 == 1; |
| 455 | handleFinishedH(reschedule); |
| 456 | } else { |
| 457 | if (DEBUG) { |
| 458 | Slog.d(TAG, "Unrecognised callback: " + mRunningJob); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | void doCancel(int arg1) { |
| 465 | synchronized (mLock) { |
| 466 | if (mVerb == VERB_FINISHED) { |
| 467 | if (DEBUG) { |
| 468 | Slog.d(TAG, |
| 469 | "Trying to process cancel for torn-down context, ignoring."); |
| 470 | } |
| 471 | return; |
| 472 | } |
| 473 | mParams.setStopReason(arg1); |
| 474 | if (arg1 == JobParameters.REASON_PREEMPT) { |
| 475 | mPreferredUid = mRunningJob != null ? mRunningJob.getUid() : |
| 476 | NO_PREFERRED_UID; |
| 477 | } |
| 478 | handleCancelH(); |
| 479 | } |
| 480 | |
| 481 | } |
| 482 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 483 | /** Start the job on the service. */ |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 484 | private void handleServiceBoundH() { |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 485 | if (DEBUG) { |
| 486 | Slog.d(TAG, "MSG_SERVICE_BOUND for " + mRunningJob.toShortString()); |
| 487 | } |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 488 | if (mVerb != VERB_BINDING) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 489 | Slog.e(TAG, "Sending onStartJob for a job that isn't pending. " |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 490 | + VERB_STRINGS[mVerb]); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 491 | closeAndCleanupJobH(false /* reschedule */); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 492 | return; |
| 493 | } |
| 494 | if (mCancelled.get()) { |
| 495 | if (DEBUG) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 496 | Slog.d(TAG, "Job cancelled while waiting for bind to complete. " |
| 497 | + mRunningJob); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 498 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 499 | closeAndCleanupJobH(true /* reschedule */); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 500 | return; |
| 501 | } |
| 502 | try { |
| 503 | mVerb = VERB_STARTING; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 504 | scheduleOpTimeOutLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 505 | service.startJob(mParams); |
Christopher Tate | a8e8e4a | 2017-01-04 11:18:22 -0800 | [diff] [blame] | 506 | } catch (Exception e) { |
| 507 | // We catch 'Exception' because client-app malice or bugs might induce a wide |
| 508 | // range of possible exception-throw outcomes from startJob() and its handling |
| 509 | // of the client's ParcelableBundle extras. |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 510 | Slog.e(TAG, "Error sending onStart message to '" + |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 511 | mRunningJob.getServiceComponent().getShortClassName() + "' ", e); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 515 | /** |
| 516 | * State behaviours. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 517 | * VERB_STARTING -> Successful start, change job to VERB_EXECUTING and post timeout. |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 518 | * _PENDING -> Error |
| 519 | * _EXECUTING -> Error |
| 520 | * _STOPPING -> Error |
| 521 | */ |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 522 | private void handleStartedH(boolean workOngoing) { |
| 523 | switch (mVerb) { |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 524 | case VERB_STARTING: |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 525 | mVerb = VERB_EXECUTING; |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 526 | if (!workOngoing) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 527 | // Job is finished already so fast-forward to handleFinished. |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 528 | handleFinishedH(false); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 529 | return; |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 530 | } |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 531 | if (mCancelled.get()) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 532 | if (DEBUG) { |
| 533 | Slog.d(TAG, "Job cancelled while waiting for onStartJob to complete."); |
| 534 | } |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 535 | // Cancelled *while* waiting for acknowledgeStartMessage from client. |
| 536 | handleCancelH(); |
| 537 | return; |
| 538 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 539 | scheduleOpTimeOutLocked(); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 540 | break; |
| 541 | default: |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 542 | Slog.e(TAG, "Handling started job but job wasn't starting! Was " |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 543 | + VERB_STRINGS[mVerb] + "."); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 544 | return; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 549 | * VERB_EXECUTING -> Client called jobFinished(), clean up and notify done. |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 550 | * _STOPPING -> Successful finish, clean up and notify done. |
| 551 | * _STARTING -> Error |
| 552 | * _PENDING -> Error |
| 553 | */ |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 554 | private void handleFinishedH(boolean reschedule) { |
| 555 | switch (mVerb) { |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 556 | case VERB_EXECUTING: |
| 557 | case VERB_STOPPING: |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 558 | closeAndCleanupJobH(reschedule); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 559 | break; |
| 560 | default: |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 561 | Slog.e(TAG, "Got an execution complete message for a job that wasn't being" + |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 562 | "executed. Was " + VERB_STRINGS[mVerb] + "."); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 567 | * A job can be in various states when a cancel request comes in: |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 568 | * VERB_BINDING -> Cancelled before bind completed. Mark as cancelled and wait for |
| 569 | * {@link #onServiceConnected(android.content.ComponentName, android.os.IBinder)} |
| 570 | * _STARTING -> Mark as cancelled and wait for |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 571 | * {@link JobServiceContext#acknowledgeStartMessage(int, boolean)} |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 572 | * _EXECUTING -> call {@link #sendStopMessageH}}, but only if there are no callbacks |
| 573 | * in the message queue. |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 574 | * _ENDING -> No point in doing anything here, so we ignore. |
| 575 | */ |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 576 | private void handleCancelH() { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 577 | if (JobSchedulerService.DEBUG) { |
| 578 | Slog.d(TAG, "Handling cancel for: " + mRunningJob.getJobId() + " " |
| 579 | + VERB_STRINGS[mVerb]); |
| 580 | } |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 581 | switch (mVerb) { |
| 582 | case VERB_BINDING: |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 583 | case VERB_STARTING: |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 584 | mCancelled.set(true); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 585 | break; |
| 586 | case VERB_EXECUTING: |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 587 | if (hasMessages(MSG_CALLBACK)) { |
| 588 | // If the client has called jobFinished, ignore this cancel. |
| 589 | return; |
| 590 | } |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 591 | sendStopMessageH(); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 592 | break; |
| 593 | case VERB_STOPPING: |
| 594 | // Nada. |
| 595 | break; |
| 596 | default: |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 597 | Slog.e(TAG, "Cancelling a job without a valid verb: " + mVerb); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 598 | break; |
| 599 | } |
| 600 | } |
| 601 | |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 602 | /** Process MSG_TIMEOUT here. */ |
| 603 | private void handleOpTimeoutH() { |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 604 | switch (mVerb) { |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 605 | case VERB_BINDING: |
| 606 | Slog.e(TAG, "Time-out while trying to bind " + mRunningJob.toShortString() + |
| 607 | ", dropping."); |
| 608 | closeAndCleanupJobH(false /* needsReschedule */); |
| 609 | break; |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 610 | case VERB_STARTING: |
| 611 | // Client unresponsive - wedged or failed to respond in time. We don't really |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 612 | // know what happened so let's log it and notify the JobScheduler |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 613 | // FINISHED/NO-RETRY. |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 614 | Slog.e(TAG, "No response from client for onStartJob '" + |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 615 | mRunningJob.toShortString()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 616 | closeAndCleanupJobH(false /* needsReschedule */); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 617 | break; |
| 618 | case VERB_STOPPING: |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 619 | // At least we got somewhere, so fail but ask the JobScheduler to reschedule. |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 620 | Slog.e(TAG, "No response from client for onStopJob, '" + |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 621 | mRunningJob.toShortString()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 622 | closeAndCleanupJobH(true /* needsReschedule */); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 623 | break; |
| 624 | case VERB_EXECUTING: |
| 625 | // Not an error - client ran out of time. |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 626 | Slog.i(TAG, "Client timed out while executing (no jobFinished received)." + |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 627 | " sending onStop. " + mRunningJob.toShortString()); |
Shreyas Basarge | 7d08905 | 2016-02-22 22:29:39 +0000 | [diff] [blame] | 628 | mParams.setStopReason(JobParameters.REASON_TIMEOUT); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 629 | sendStopMessageH(); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 630 | break; |
| 631 | default: |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 632 | Slog.e(TAG, "Handling timeout for an invalid job state: " + |
| 633 | mRunningJob.toShortString() + ", dropping."); |
| 634 | closeAndCleanupJobH(false /* needsReschedule */); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
| 638 | /** |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 639 | * Already running, need to stop. Will switch {@link #mVerb} from VERB_EXECUTING -> |
| 640 | * VERB_STOPPING. |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 641 | */ |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 642 | private void sendStopMessageH() { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 643 | removeOpTimeOutLocked(); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 644 | if (mVerb != VERB_EXECUTING) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 645 | Slog.e(TAG, "Sending onStopJob for a job that isn't started. " + mRunningJob); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 646 | closeAndCleanupJobH(false /* reschedule */); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 647 | return; |
| 648 | } |
| 649 | try { |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 650 | mVerb = VERB_STOPPING; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 651 | scheduleOpTimeOutLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 652 | service.stopJob(mParams); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 653 | } catch (RemoteException e) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 654 | Slog.e(TAG, "Error sending onStopJob to client.", e); |
Christopher Tate | b2087f8 | 2017-03-29 17:42:39 -0700 | [diff] [blame] | 655 | // The job's host app apparently crashed during the job, so we should reschedule. |
| 656 | closeAndCleanupJobH(true /* reschedule */); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 661 | * The provided job has finished, either by calling |
| 662 | * {@link android.app.job.JobService#jobFinished(android.app.job.JobParameters, boolean)} |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 663 | * or from acknowledging the stop message we sent. Either way, we're done tracking it and |
| 664 | * we want to clean up internally. |
| 665 | */ |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 666 | private void closeAndCleanupJobH(boolean reschedule) { |
riddle_hsu | 6a94aba | 2015-05-01 01:52:58 +0800 | [diff] [blame] | 667 | final JobStatus completedJob; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 668 | synchronized (mLock) { |
riddle_hsu | 6a94aba | 2015-05-01 01:52:58 +0800 | [diff] [blame] | 669 | if (mVerb == VERB_FINISHED) { |
| 670 | return; |
| 671 | } |
| 672 | completedJob = mRunningJob; |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 673 | mJobPackageTracker.noteInactive(completedJob); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 674 | try { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 675 | mBatteryStats.noteJobFinish(mRunningJob.getBatteryName(), |
| 676 | mRunningJob.getSourceUid()); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 677 | } catch (RemoteException e) { |
| 678 | // Whatever. |
| 679 | } |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 680 | if (mWakeLock != null) { |
| 681 | mWakeLock.release(); |
| 682 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 683 | mContext.unbindService(JobServiceContext.this); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 684 | mWakeLock = null; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 685 | mRunningJob = null; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 686 | mParams = null; |
riddle_hsu | 6a94aba | 2015-05-01 01:52:58 +0800 | [diff] [blame] | 687 | mVerb = VERB_FINISHED; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 688 | mCancelled.set(false); |
| 689 | service = null; |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 690 | mAvailable = true; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 691 | removeOpTimeOutLocked(); |
| 692 | removeMessages(MSG_CALLBACK); |
| 693 | removeMessages(MSG_SERVICE_BOUND); |
| 694 | removeMessages(MSG_CANCEL); |
| 695 | removeMessages(MSG_SHUTDOWN_EXECUTION); |
| 696 | mCompletedListener.onJobCompletedLocked(completedJob, reschedule); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 697 | } |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 698 | } |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 699 | } |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 700 | |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 701 | /** |
| 702 | * Called when sending a message to the client, over whose execution we have no control. If |
| 703 | * we haven't received a response in a certain amount of time, we want to give up and carry |
| 704 | * on with life. |
| 705 | */ |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 706 | private void scheduleOpTimeOutLocked() { |
| 707 | removeOpTimeOutLocked(); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 708 | |
Christopher Tate | 2022bad | 2017-02-02 18:07:42 -0800 | [diff] [blame] | 709 | final long timeoutMillis; |
| 710 | switch (mVerb) { |
| 711 | case VERB_EXECUTING: |
| 712 | timeoutMillis = EXECUTING_TIMESLICE_MILLIS; |
| 713 | break; |
| 714 | |
| 715 | case VERB_BINDING: |
| 716 | timeoutMillis = OP_BIND_TIMEOUT_MILLIS; |
| 717 | break; |
| 718 | |
| 719 | default: |
| 720 | timeoutMillis = OP_TIMEOUT_MILLIS; |
| 721 | break; |
| 722 | } |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 723 | if (DEBUG) { |
| 724 | Slog.d(TAG, "Scheduling time out for '" + |
| 725 | mRunningJob.getServiceComponent().getShortClassName() + "' jId: " + |
| 726 | mParams.getJobId() + ", in " + (timeoutMillis / 1000) + " s"); |
Matthew Williams | 691e93e | 2014-05-12 15:33:09 -0700 | [diff] [blame] | 727 | } |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 728 | Message m = mCallbackHandler.obtainMessage(MSG_TIMEOUT); |
| 729 | mCallbackHandler.sendMessageDelayed(m, timeoutMillis); |
| 730 | mTimeoutElapsed = SystemClock.elapsedRealtime() + timeoutMillis; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 731 | } |
Matthew Williams | 7ac52d5 | 2014-09-12 14:40:18 -0700 | [diff] [blame] | 732 | |
| 733 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 734 | private void removeOpTimeOutLocked() { |
Matthew Williams | 7ac52d5 | 2014-09-12 14:40:18 -0700 | [diff] [blame] | 735 | mCallbackHandler.removeMessages(MSG_TIMEOUT); |
| 736 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 737 | } |