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