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