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