Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | package com.android.server.job.controllers; |
| 18 | |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 19 | import android.annotation.UserIdInt; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 20 | import android.app.job.JobInfo; |
| 21 | import android.content.Context; |
| 22 | import android.database.ContentObserver; |
| 23 | import android.net.Uri; |
| 24 | import android.os.Handler; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 25 | import android.os.UserHandle; |
Jeff Sharkey | 01bb530 | 2018-02-21 20:12:40 -0700 | [diff] [blame] | 26 | import android.util.ArrayMap; |
| 27 | import android.util.ArraySet; |
| 28 | import android.util.Log; |
Christopher Tate | 4b42571 | 2016-06-27 16:12:41 -0700 | [diff] [blame] | 29 | import android.util.Slog; |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 30 | import android.util.SparseArray; |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 31 | import android.util.TimeUtils; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 32 | import android.util.proto.ProtoOutputStream; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 33 | |
| 34 | import com.android.internal.annotations.VisibleForTesting; |
| 35 | import com.android.server.job.JobSchedulerService; |
| 36 | import com.android.server.job.StateChangedListener; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 37 | import com.android.server.job.StateControllerProto; |
| 38 | import com.android.server.job.StateControllerProto.ContentObserverController.Observer.TriggerContentData; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 39 | |
| 40 | import java.io.PrintWriter; |
| 41 | import java.util.ArrayList; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Controller for monitoring changes to content URIs through a ContentObserver. |
| 45 | */ |
Dianne Hackborn | 6466c1c | 2017-06-13 10:33:19 -0700 | [diff] [blame] | 46 | public final class ContentObserverController extends StateController { |
Jeff Sharkey | 01bb530 | 2018-02-21 20:12:40 -0700 | [diff] [blame] | 47 | private static final String TAG = "JobScheduler.ContentObserver"; |
| 48 | private static final boolean DEBUG = JobSchedulerService.DEBUG |
| 49 | || Log.isLoggable(TAG, Log.DEBUG); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Maximum number of changing URIs we will batch together to report. |
| 53 | * XXX Should be smarter about this, restricting it by the maximum number |
| 54 | * of characters we will retain. |
| 55 | */ |
| 56 | private static final int MAX_URIS_REPORTED = 50; |
| 57 | |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 58 | /** |
| 59 | * At this point we consider it urgent to schedule the job ASAP. |
| 60 | */ |
| 61 | private static final int URIS_URGENT_THRESHOLD = 40; |
| 62 | |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 63 | private static final Object sCreationLock = new Object(); |
| 64 | private static volatile ContentObserverController sController; |
| 65 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 66 | final private ArraySet<JobStatus> mTrackedTasks = new ArraySet<>(); |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 67 | /** |
| 68 | * Per-userid {@link JobInfo.TriggerContentUri} keyed ContentObserver cache. |
| 69 | */ |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 70 | final SparseArray<ArrayMap<JobInfo.TriggerContentUri, ObserverInstance>> mObservers = |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 71 | new SparseArray<>(); |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 72 | final Handler mHandler; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 73 | |
| 74 | public static ContentObserverController get(JobSchedulerService taskManagerService) { |
| 75 | synchronized (sCreationLock) { |
| 76 | if (sController == null) { |
| 77 | sController = new ContentObserverController(taskManagerService, |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 78 | taskManagerService.getContext(), taskManagerService.getLock()); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | return sController; |
| 82 | } |
| 83 | |
| 84 | @VisibleForTesting |
| 85 | public static ContentObserverController getForTesting(StateChangedListener stateChangedListener, |
| 86 | Context context) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 87 | return new ContentObserverController(stateChangedListener, context, new Object()); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 90 | private ContentObserverController(StateChangedListener stateChangedListener, Context context, |
| 91 | Object lock) { |
| 92 | super(stateChangedListener, context, lock); |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 93 | mHandler = new Handler(context.getMainLooper()); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 97 | public void maybeStartTrackingJobLocked(JobStatus taskStatus, JobStatus lastJob) { |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 98 | if (taskStatus.hasContentTriggerConstraint()) { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 99 | if (taskStatus.contentObserverJobInstance == null) { |
| 100 | taskStatus.contentObserverJobInstance = new JobInstance(taskStatus); |
| 101 | } |
Christopher Tate | 4b42571 | 2016-06-27 16:12:41 -0700 | [diff] [blame] | 102 | if (DEBUG) { |
| 103 | Slog.i(TAG, "Tracking content-trigger job " + taskStatus); |
| 104 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 105 | mTrackedTasks.add(taskStatus); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 106 | taskStatus.setTrackingController(JobStatus.TRACKING_CONTENT); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 107 | boolean havePendingUris = false; |
| 108 | // If there is a previous job associated with the new job, propagate over |
| 109 | // any pending content URI trigger reports. |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 110 | if (taskStatus.contentObserverJobInstance.mChangedAuthorities != null) { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 111 | havePendingUris = true; |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 112 | } |
| 113 | // If we have previously reported changed authorities/uris, then we failed |
| 114 | // to complete the job with them so will re-record them to report again. |
| 115 | if (taskStatus.changedAuthorities != null) { |
| 116 | havePendingUris = true; |
| 117 | if (taskStatus.contentObserverJobInstance.mChangedAuthorities == null) { |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 118 | taskStatus.contentObserverJobInstance.mChangedAuthorities |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 119 | = new ArraySet<>(); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 120 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 121 | for (String auth : taskStatus.changedAuthorities) { |
| 122 | taskStatus.contentObserverJobInstance.mChangedAuthorities.add(auth); |
| 123 | } |
| 124 | if (taskStatus.changedUris != null) { |
| 125 | if (taskStatus.contentObserverJobInstance.mChangedUris == null) { |
| 126 | taskStatus.contentObserverJobInstance.mChangedUris = new ArraySet<>(); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 127 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 128 | for (Uri uri : taskStatus.changedUris) { |
| 129 | taskStatus.contentObserverJobInstance.mChangedUris.add(uri); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 130 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 131 | } |
| 132 | taskStatus.changedAuthorities = null; |
| 133 | taskStatus.changedUris = null; |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 134 | } |
| 135 | taskStatus.changedAuthorities = null; |
| 136 | taskStatus.changedUris = null; |
| 137 | taskStatus.setContentTriggerConstraintSatisfied(havePendingUris); |
| 138 | } |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 139 | if (lastJob != null && lastJob.contentObserverJobInstance != null) { |
| 140 | // And now we can detach the instance state from the last job. |
| 141 | lastJob.contentObserverJobInstance.detachLocked(); |
| 142 | lastJob.contentObserverJobInstance = null; |
| 143 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void prepareForExecutionLocked(JobStatus taskStatus) { |
| 148 | if (taskStatus.hasContentTriggerConstraint()) { |
| 149 | if (taskStatus.contentObserverJobInstance != null) { |
| 150 | taskStatus.changedUris = taskStatus.contentObserverJobInstance.mChangedUris; |
| 151 | taskStatus.changedAuthorities |
| 152 | = taskStatus.contentObserverJobInstance.mChangedAuthorities; |
| 153 | taskStatus.contentObserverJobInstance.mChangedUris = null; |
| 154 | taskStatus.contentObserverJobInstance.mChangedAuthorities = null; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | @Override |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 160 | public void maybeStopTrackingJobLocked(JobStatus taskStatus, JobStatus incomingJob, |
| 161 | boolean forUpdate) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 162 | if (taskStatus.clearTrackingController(JobStatus.TRACKING_CONTENT)) { |
| 163 | mTrackedTasks.remove(taskStatus); |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 164 | if (taskStatus.contentObserverJobInstance != null) { |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 165 | taskStatus.contentObserverJobInstance.unscheduleLocked(); |
| 166 | if (incomingJob != null) { |
| 167 | if (taskStatus.contentObserverJobInstance != null |
| 168 | && taskStatus.contentObserverJobInstance.mChangedAuthorities != null) { |
| 169 | // We are stopping this job, but it is going to be replaced by this given |
| 170 | // incoming job. We want to propagate our state over to it, so we don't |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 171 | // lose any content changes that had happened since the last one started. |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 172 | // If there is a previous job associated with the new job, propagate over |
| 173 | // any pending content URI trigger reports. |
| 174 | if (incomingJob.contentObserverJobInstance == null) { |
| 175 | incomingJob.contentObserverJobInstance = new JobInstance(incomingJob); |
| 176 | } |
| 177 | incomingJob.contentObserverJobInstance.mChangedAuthorities |
| 178 | = taskStatus.contentObserverJobInstance.mChangedAuthorities; |
| 179 | incomingJob.contentObserverJobInstance.mChangedUris |
| 180 | = taskStatus.contentObserverJobInstance.mChangedUris; |
| 181 | taskStatus.contentObserverJobInstance.mChangedAuthorities = null; |
| 182 | taskStatus.contentObserverJobInstance.mChangedUris = null; |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 183 | } |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 184 | // We won't detach the content observers here, because we want to |
| 185 | // allow them to continue monitoring so we don't miss anything... and |
| 186 | // since we are giving an incomingJob here, we know this will be |
| 187 | // immediately followed by a start tracking of that job. |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 188 | } else { |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 189 | // But here there is no incomingJob, so nothing coming up, so time to detach. |
| 190 | taskStatus.contentObserverJobInstance.detachLocked(); |
| 191 | taskStatus.contentObserverJobInstance = null; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
Christopher Tate | 4b42571 | 2016-06-27 16:12:41 -0700 | [diff] [blame] | 194 | if (DEBUG) { |
| 195 | Slog.i(TAG, "No longer tracking job " + taskStatus); |
| 196 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | @Override |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 201 | public void rescheduleForFailureLocked(JobStatus newJob, JobStatus failureToReschedule) { |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 202 | if (failureToReschedule.hasContentTriggerConstraint() |
| 203 | && newJob.hasContentTriggerConstraint()) { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 204 | // Our job has failed, and we are scheduling a new job for it. |
| 205 | // Copy the last reported content changes in to the new job, so when |
| 206 | // we schedule the new one we will pick them up and report them again. |
| 207 | newJob.changedAuthorities = failureToReschedule.changedAuthorities; |
| 208 | newJob.changedUris = failureToReschedule.changedUris; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 212 | final class ObserverInstance extends ContentObserver { |
Christopher Tate | 4b42571 | 2016-06-27 16:12:41 -0700 | [diff] [blame] | 213 | final JobInfo.TriggerContentUri mUri; |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 214 | final @UserIdInt int mUserId; |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 215 | final ArraySet<JobInstance> mJobs = new ArraySet<>(); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 216 | |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 217 | public ObserverInstance(Handler handler, JobInfo.TriggerContentUri uri, |
| 218 | @UserIdInt int userId) { |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 219 | super(handler); |
| 220 | mUri = uri; |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 221 | mUserId = userId; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void onChange(boolean selfChange, Uri uri) { |
Christopher Tate | 4b42571 | 2016-06-27 16:12:41 -0700 | [diff] [blame] | 226 | if (DEBUG) { |
| 227 | Slog.i(TAG, "onChange(self=" + selfChange + ") for " + uri |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 228 | + " when mUri=" + mUri + " mUserId=" + mUserId); |
Christopher Tate | 4b42571 | 2016-06-27 16:12:41 -0700 | [diff] [blame] | 229 | } |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 230 | synchronized (mLock) { |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 231 | final int N = mJobs.size(); |
| 232 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 233 | JobInstance inst = mJobs.valueAt(i); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 234 | if (inst.mChangedUris == null) { |
| 235 | inst.mChangedUris = new ArraySet<>(); |
| 236 | } |
| 237 | if (inst.mChangedUris.size() < MAX_URIS_REPORTED) { |
| 238 | inst.mChangedUris.add(uri); |
| 239 | } |
| 240 | if (inst.mChangedAuthorities == null) { |
| 241 | inst.mChangedAuthorities = new ArraySet<>(); |
| 242 | } |
| 243 | inst.mChangedAuthorities.add(uri.getAuthority()); |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 244 | inst.scheduleLocked(); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 250 | static final class TriggerRunnable implements Runnable { |
| 251 | final JobInstance mInstance; |
| 252 | |
| 253 | TriggerRunnable(JobInstance instance) { |
| 254 | mInstance = instance; |
| 255 | } |
| 256 | |
| 257 | @Override public void run() { |
| 258 | mInstance.trigger(); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | final class JobInstance { |
| 263 | final ArrayList<ObserverInstance> mMyObservers = new ArrayList<>(); |
| 264 | final JobStatus mJobStatus; |
| 265 | final Runnable mExecuteRunner; |
| 266 | final Runnable mTimeoutRunner; |
| 267 | ArraySet<Uri> mChangedUris; |
| 268 | ArraySet<String> mChangedAuthorities; |
| 269 | |
| 270 | boolean mTriggerPending; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 271 | |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 272 | // This constructor must be called with the master job scheduler lock held. |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 273 | JobInstance(JobStatus jobStatus) { |
| 274 | mJobStatus = jobStatus; |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 275 | mExecuteRunner = new TriggerRunnable(this); |
| 276 | mTimeoutRunner = new TriggerRunnable(this); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 277 | final JobInfo.TriggerContentUri[] uris = jobStatus.getJob().getTriggerContentUris(); |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 278 | final int sourceUserId = jobStatus.getSourceUserId(); |
| 279 | ArrayMap<JobInfo.TriggerContentUri, ObserverInstance> observersOfUser = |
| 280 | mObservers.get(sourceUserId); |
| 281 | if (observersOfUser == null) { |
| 282 | observersOfUser = new ArrayMap<>(); |
| 283 | mObservers.put(sourceUserId, observersOfUser); |
| 284 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 285 | if (uris != null) { |
| 286 | for (JobInfo.TriggerContentUri uri : uris) { |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 287 | ObserverInstance obs = observersOfUser.get(uri); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 288 | if (obs == null) { |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 289 | obs = new ObserverInstance(mHandler, uri, jobStatus.getSourceUserId()); |
| 290 | observersOfUser.put(uri, obs); |
Christopher Tate | 4b42571 | 2016-06-27 16:12:41 -0700 | [diff] [blame] | 291 | final boolean andDescendants = (uri.getFlags() & |
| 292 | JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS) != 0; |
| 293 | if (DEBUG) { |
| 294 | Slog.v(TAG, "New observer " + obs + " for " + uri.getUri() |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 295 | + " andDescendants=" + andDescendants |
| 296 | + " sourceUserId=" + sourceUserId); |
Christopher Tate | 4b42571 | 2016-06-27 16:12:41 -0700 | [diff] [blame] | 297 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 298 | mContext.getContentResolver().registerContentObserver( |
| 299 | uri.getUri(), |
Christopher Tate | 4b42571 | 2016-06-27 16:12:41 -0700 | [diff] [blame] | 300 | andDescendants, |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 301 | obs, |
| 302 | sourceUserId |
| 303 | ); |
Christopher Tate | 4b42571 | 2016-06-27 16:12:41 -0700 | [diff] [blame] | 304 | } else { |
| 305 | if (DEBUG) { |
| 306 | final boolean andDescendants = (uri.getFlags() & |
| 307 | JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS) != 0; |
| 308 | Slog.v(TAG, "Reusing existing observer " + obs + " for " + uri.getUri() |
| 309 | + " andDescendants=" + andDescendants); |
| 310 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 311 | } |
| 312 | obs.mJobs.add(this); |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 313 | mMyObservers.add(obs); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 318 | void trigger() { |
| 319 | boolean reportChange = false; |
| 320 | synchronized (mLock) { |
| 321 | if (mTriggerPending) { |
| 322 | if (mJobStatus.setContentTriggerConstraintSatisfied(true)) { |
| 323 | reportChange = true; |
| 324 | } |
| 325 | unscheduleLocked(); |
| 326 | } |
| 327 | } |
| 328 | // Let the scheduler know that state has changed. This may or may not result in an |
| 329 | // execution. |
| 330 | if (reportChange) { |
| 331 | mStateChangedListener.onControllerStateChanged(); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void scheduleLocked() { |
| 336 | if (!mTriggerPending) { |
| 337 | mTriggerPending = true; |
| 338 | mHandler.postDelayed(mTimeoutRunner, mJobStatus.getTriggerContentMaxDelay()); |
| 339 | } |
| 340 | mHandler.removeCallbacks(mExecuteRunner); |
| 341 | if (mChangedUris.size() >= URIS_URGENT_THRESHOLD) { |
| 342 | // If we start getting near the limit, GO NOW! |
| 343 | mHandler.post(mExecuteRunner); |
| 344 | } else { |
| 345 | mHandler.postDelayed(mExecuteRunner, mJobStatus.getTriggerContentUpdateDelay()); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | void unscheduleLocked() { |
| 350 | if (mTriggerPending) { |
| 351 | mHandler.removeCallbacks(mExecuteRunner); |
| 352 | mHandler.removeCallbacks(mTimeoutRunner); |
| 353 | mTriggerPending = false; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | void detachLocked() { |
| 358 | final int N = mMyObservers.size(); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 359 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 360 | final ObserverInstance obs = mMyObservers.get(i); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 361 | obs.mJobs.remove(this); |
| 362 | if (obs.mJobs.size() == 0) { |
Christopher Tate | 4b42571 | 2016-06-27 16:12:41 -0700 | [diff] [blame] | 363 | if (DEBUG) { |
| 364 | Slog.i(TAG, "Unregistering observer " + obs + " for " + obs.mUri.getUri()); |
| 365 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 366 | mContext.getContentResolver().unregisterContentObserver(obs); |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 367 | ArrayMap<JobInfo.TriggerContentUri, ObserverInstance> observerOfUser = |
| 368 | mObservers.get(obs.mUserId); |
| 369 | if (observerOfUser != null) { |
| 370 | observerOfUser.remove(obs.mUri); |
| 371 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | @Override |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 378 | public void dumpControllerStateLocked(PrintWriter pw, int filterUid) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 379 | pw.println("Content:"); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 380 | for (int i = 0; i < mTrackedTasks.size(); i++) { |
| 381 | JobStatus js = mTrackedTasks.valueAt(i); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 382 | if (!js.shouldDump(filterUid)) { |
| 383 | continue; |
| 384 | } |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 385 | pw.print(" #"); |
| 386 | js.printUniqueId(pw); |
| 387 | pw.print(" from "); |
| 388 | UserHandle.formatUid(pw, js.getSourceUid()); |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 389 | pw.println(); |
| 390 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 391 | int N = mObservers.size(); |
| 392 | if (N > 0) { |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 393 | pw.println(" Observers:"); |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 394 | for (int userIdx = 0; userIdx < N; userIdx++) { |
| 395 | final int userId = mObservers.keyAt(userIdx); |
| 396 | ArrayMap<JobInfo.TriggerContentUri, ObserverInstance> observersOfUser = |
| 397 | mObservers.get(userId); |
| 398 | int numbOfObserversPerUser = observersOfUser.size(); |
| 399 | for (int observerIdx = 0 ; observerIdx < numbOfObserversPerUser; observerIdx++) { |
| 400 | ObserverInstance obs = observersOfUser.valueAt(observerIdx); |
| 401 | int M = obs.mJobs.size(); |
| 402 | boolean shouldDump = false; |
| 403 | for (int j = 0; j < M; j++) { |
| 404 | JobInstance inst = obs.mJobs.valueAt(j); |
| 405 | if (inst.mJobStatus.shouldDump(filterUid)) { |
| 406 | shouldDump = true; |
| 407 | break; |
| 408 | } |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 409 | } |
Tony Mak | e46393e | 2016-07-29 11:30:47 +0100 | [diff] [blame] | 410 | if (!shouldDump) { |
| 411 | continue; |
| 412 | } |
| 413 | pw.print(" "); |
| 414 | JobInfo.TriggerContentUri trigger = observersOfUser.keyAt(observerIdx); |
| 415 | pw.print(trigger.getUri()); |
| 416 | pw.print(" 0x"); |
| 417 | pw.print(Integer.toHexString(trigger.getFlags())); |
| 418 | pw.print(" ("); |
| 419 | pw.print(System.identityHashCode(obs)); |
| 420 | pw.println("):"); |
| 421 | pw.println(" Jobs:"); |
| 422 | for (int j = 0; j < M; j++) { |
| 423 | JobInstance inst = obs.mJobs.valueAt(j); |
| 424 | pw.print(" #"); |
| 425 | inst.mJobStatus.printUniqueId(pw); |
| 426 | pw.print(" from "); |
| 427 | UserHandle.formatUid(pw, inst.mJobStatus.getSourceUid()); |
| 428 | if (inst.mChangedAuthorities != null) { |
| 429 | pw.println(":"); |
| 430 | if (inst.mTriggerPending) { |
| 431 | pw.print(" Trigger pending: update="); |
| 432 | TimeUtils.formatDuration( |
| 433 | inst.mJobStatus.getTriggerContentUpdateDelay(), pw); |
| 434 | pw.print(", max="); |
| 435 | TimeUtils.formatDuration( |
| 436 | inst.mJobStatus.getTriggerContentMaxDelay(), pw); |
| 437 | pw.println(); |
| 438 | } |
| 439 | pw.println(" Changed Authorities:"); |
| 440 | for (int k = 0; k < inst.mChangedAuthorities.size(); k++) { |
| 441 | pw.print(" "); |
| 442 | pw.println(inst.mChangedAuthorities.valueAt(k)); |
| 443 | } |
| 444 | if (inst.mChangedUris != null) { |
| 445 | pw.println(" Changed URIs:"); |
| 446 | for (int k = 0; k < inst.mChangedUris.size(); k++) { |
| 447 | pw.print(" "); |
| 448 | pw.println(inst.mChangedUris.valueAt(k)); |
| 449 | } |
| 450 | } |
| 451 | } else { |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 452 | pw.println(); |
| 453 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 459 | |
| 460 | @Override |
| 461 | public void dumpControllerStateLocked(ProtoOutputStream proto, long fieldId, int filterUid) { |
| 462 | final long token = proto.start(fieldId); |
| 463 | final long mToken = proto.start(StateControllerProto.CONTENT_OBSERVER); |
| 464 | |
| 465 | for (int i = 0; i < mTrackedTasks.size(); i++) { |
| 466 | JobStatus js = mTrackedTasks.valueAt(i); |
| 467 | if (!js.shouldDump(filterUid)) { |
| 468 | continue; |
| 469 | } |
| 470 | final long jsToken = |
| 471 | proto.start(StateControllerProto.ContentObserverController.TRACKED_JOBS); |
| 472 | js.writeToShortProto(proto, |
| 473 | StateControllerProto.ContentObserverController.TrackedJob.INFO); |
| 474 | proto.write(StateControllerProto.ContentObserverController.TrackedJob.SOURCE_UID, |
| 475 | js.getSourceUid()); |
| 476 | proto.end(jsToken); |
| 477 | } |
| 478 | |
| 479 | final int n = mObservers.size(); |
| 480 | for (int userIdx = 0; userIdx < n; userIdx++) { |
| 481 | final long oToken = |
| 482 | proto.start(StateControllerProto.ContentObserverController.OBSERVERS); |
| 483 | final int userId = mObservers.keyAt(userIdx); |
| 484 | |
| 485 | proto.write(StateControllerProto.ContentObserverController.Observer.USER_ID, userId); |
| 486 | |
| 487 | ArrayMap<JobInfo.TriggerContentUri, ObserverInstance> observersOfUser = |
| 488 | mObservers.get(userId); |
| 489 | int numbOfObserversPerUser = observersOfUser.size(); |
| 490 | for (int observerIdx = 0 ; observerIdx < numbOfObserversPerUser; observerIdx++) { |
| 491 | ObserverInstance obs = observersOfUser.valueAt(observerIdx); |
| 492 | int m = obs.mJobs.size(); |
| 493 | boolean shouldDump = false; |
| 494 | for (int j = 0; j < m; j++) { |
| 495 | JobInstance inst = obs.mJobs.valueAt(j); |
| 496 | if (inst.mJobStatus.shouldDump(filterUid)) { |
| 497 | shouldDump = true; |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | if (!shouldDump) { |
| 502 | continue; |
| 503 | } |
| 504 | final long tToken = proto.start( |
| 505 | StateControllerProto.ContentObserverController.Observer.TRIGGERS); |
| 506 | |
| 507 | JobInfo.TriggerContentUri trigger = observersOfUser.keyAt(observerIdx); |
| 508 | Uri u = trigger.getUri(); |
| 509 | if (u != null) { |
| 510 | proto.write(TriggerContentData.URI, u.toString()); |
| 511 | } |
| 512 | proto.write(TriggerContentData.FLAGS, trigger.getFlags()); |
| 513 | |
| 514 | for (int j = 0; j < m; j++) { |
| 515 | final long jToken = proto.start(TriggerContentData.JOBS); |
| 516 | JobInstance inst = obs.mJobs.valueAt(j); |
| 517 | |
| 518 | inst.mJobStatus.writeToShortProto(proto, TriggerContentData.JobInstance.INFO); |
| 519 | proto.write(TriggerContentData.JobInstance.SOURCE_UID, |
| 520 | inst.mJobStatus.getSourceUid()); |
| 521 | |
| 522 | if (inst.mChangedAuthorities == null) { |
| 523 | proto.end(jToken); |
| 524 | continue; |
| 525 | } |
| 526 | if (inst.mTriggerPending) { |
| 527 | proto.write(TriggerContentData.JobInstance.TRIGGER_CONTENT_UPDATE_DELAY_MS, |
| 528 | inst.mJobStatus.getTriggerContentUpdateDelay()); |
| 529 | proto.write(TriggerContentData.JobInstance.TRIGGER_CONTENT_MAX_DELAY_MS, |
| 530 | inst.mJobStatus.getTriggerContentMaxDelay()); |
| 531 | } |
| 532 | for (int k = 0; k < inst.mChangedAuthorities.size(); k++) { |
| 533 | proto.write(TriggerContentData.JobInstance.CHANGED_AUTHORITIES, |
| 534 | inst.mChangedAuthorities.valueAt(k)); |
| 535 | } |
| 536 | if (inst.mChangedUris != null) { |
| 537 | for (int k = 0; k < inst.mChangedUris.size(); k++) { |
| 538 | u = inst.mChangedUris.valueAt(k); |
| 539 | if (u != null) { |
| 540 | proto.write(TriggerContentData.JobInstance.CHANGED_URIS, |
| 541 | u.toString()); |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | proto.end(jToken); |
| 547 | } |
| 548 | |
| 549 | proto.end(tToken); |
| 550 | } |
| 551 | |
| 552 | proto.end(oToken); |
| 553 | } |
| 554 | |
| 555 | proto.end(mToken); |
| 556 | proto.end(token); |
| 557 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 558 | } |