Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 20 | import android.os.IDeviceIdleController; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 21 | import android.os.ServiceManager; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 22 | import android.os.SystemClock; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 23 | import android.os.UserHandle; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 24 | import android.util.Slog; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 25 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 26 | import com.android.internal.util.ArrayUtils; |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 27 | import com.android.server.ForceAppStandbyTracker; |
| 28 | import com.android.server.ForceAppStandbyTracker.Listener; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 29 | import com.android.server.job.JobSchedulerService; |
| 30 | import com.android.server.job.JobStore; |
| 31 | |
| 32 | import java.io.PrintWriter; |
| 33 | |
| 34 | public final class BackgroundJobsController extends StateController { |
| 35 | |
| 36 | private static final String LOG_TAG = "BackgroundJobsController"; |
| 37 | private static final boolean DEBUG = JobSchedulerService.DEBUG; |
| 38 | |
| 39 | // Singleton factory |
| 40 | private static final Object sCreationLock = new Object(); |
| 41 | private static volatile BackgroundJobsController sController; |
| 42 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 43 | private final JobSchedulerService mJobSchedulerService; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 44 | private final IDeviceIdleController mDeviceIdleController; |
| 45 | |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 46 | private final ForceAppStandbyTracker mForceAppStandbyTracker; |
| 47 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 48 | |
| 49 | public static BackgroundJobsController get(JobSchedulerService service) { |
| 50 | synchronized (sCreationLock) { |
| 51 | if (sController == null) { |
| 52 | sController = new BackgroundJobsController(service, service.getContext(), |
| 53 | service.getLock()); |
| 54 | } |
| 55 | return sController; |
| 56 | } |
| 57 | } |
| 58 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 59 | private BackgroundJobsController(JobSchedulerService service, Context context, Object lock) { |
| 60 | super(service, context, lock); |
| 61 | mJobSchedulerService = service; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 62 | mDeviceIdleController = IDeviceIdleController.Stub.asInterface( |
| 63 | ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER)); |
| 64 | |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 65 | mForceAppStandbyTracker = ForceAppStandbyTracker.getInstance(context); |
| 66 | |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 67 | mForceAppStandbyTracker.addListener(mForceAppStandbyListener); |
| 68 | mForceAppStandbyTracker.start(); |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void maybeStartTrackingJobLocked(JobStatus jobStatus, JobStatus lastJob) { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 73 | updateSingleJobRestrictionLocked(jobStatus); |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void maybeStopTrackingJobLocked(JobStatus jobStatus, JobStatus incomingJob, |
| 78 | boolean forUpdate) { |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void dumpControllerStateLocked(final PrintWriter pw, final int filterUid) { |
Suprabh Shukla | a78acfd | 2017-10-13 19:29:36 -0700 | [diff] [blame] | 83 | pw.println("BackgroundJobsController"); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 84 | |
Makoto Onuki | 2206af39 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 85 | mForceAppStandbyTracker.dump(pw, ""); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 86 | |
| 87 | pw.println("Job state:"); |
| 88 | mJobSchedulerService.getJobStore().forEachJob((jobStatus) -> { |
| 89 | if (!jobStatus.shouldDump(filterUid)) { |
| 90 | return; |
| 91 | } |
| 92 | final int uid = jobStatus.getSourceUid(); |
| 93 | pw.print(" #"); |
| 94 | jobStatus.printUniqueId(pw); |
| 95 | pw.print(" from "); |
| 96 | UserHandle.formatUid(pw, uid); |
| 97 | pw.print(mForceAppStandbyTracker.isInForeground(uid) ? " foreground" : " background"); |
Makoto Onuki | 2206af39 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 98 | if (mForceAppStandbyTracker.isUidPowerSaveWhitelisted(uid) || |
| 99 | mForceAppStandbyTracker.isUidTempPowerSaveWhitelisted(uid)) { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 100 | pw.print(", whitelisted"); |
| 101 | } |
| 102 | pw.print(": "); |
| 103 | pw.print(jobStatus.getSourcePackageName()); |
| 104 | |
Makoto Onuki | 2206af39 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 105 | pw.print(" [RUN_ANY_IN_BACKGROUND "); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 106 | pw.print(mForceAppStandbyTracker.isRunAnyInBackgroundAppOpsAllowed( |
Makoto Onuki | 2206af39 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 107 | jobStatus.getSourceUid(), jobStatus.getSourcePackageName()) |
| 108 | ? "allowed]" : "disallowed]"); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 109 | |
| 110 | if ((jobStatus.satisfiedConstraints |
| 111 | & JobStatus.CONSTRAINT_BACKGROUND_NOT_RESTRICTED) != 0) { |
| 112 | pw.println(" RUNNABLE"); |
| 113 | } else { |
| 114 | pw.println(" WAITING"); |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 115 | } |
| 116 | }); |
| 117 | } |
| 118 | |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 119 | private void updateAllJobRestrictionsLocked() { |
| 120 | updateJobRestrictionsLocked(/*filterUid=*/ -1); |
| 121 | } |
| 122 | |
| 123 | private void updateJobRestrictionsForUidLocked(int uid) { |
| 124 | |
| 125 | // TODO Use forEachJobForSourceUid() once we have it. |
| 126 | |
| 127 | updateJobRestrictionsLocked(/*filterUid=*/ uid); |
| 128 | } |
| 129 | |
| 130 | private void updateJobRestrictionsLocked(int filterUid) { |
| 131 | final UpdateJobFunctor updateTrackedJobs = |
| 132 | new UpdateJobFunctor(filterUid); |
| 133 | |
| 134 | final long start = DEBUG ? SystemClock.elapsedRealtimeNanos() : 0; |
| 135 | |
| 136 | mJobSchedulerService.getJobStore().forEachJob(updateTrackedJobs); |
| 137 | |
| 138 | final long time = DEBUG ? (SystemClock.elapsedRealtimeNanos() - start) : 0; |
| 139 | if (DEBUG) { |
| 140 | Slog.d(LOG_TAG, String.format( |
| 141 | "Job status updated: %d/%d checked/total jobs, %d us", |
| 142 | updateTrackedJobs.mCheckedCount, |
| 143 | updateTrackedJobs.mTotalCount, |
| 144 | (time / 1000) |
| 145 | )); |
| 146 | } |
| 147 | |
| 148 | if (updateTrackedJobs.mChanged) { |
| 149 | mStateChangedListener.onControllerStateChanged(); |
| 150 | } |
| 151 | } |
| 152 | |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 153 | boolean updateSingleJobRestrictionLocked(JobStatus jobStatus) { |
| 154 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 155 | final int uid = jobStatus.getSourceUid(); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 156 | final String packageName = jobStatus.getSourcePackageName(); |
| 157 | |
Makoto Onuki | 2206af39 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 158 | final boolean canRun = !mForceAppStandbyTracker.areJobsRestricted(uid, packageName); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 159 | |
| 160 | return jobStatus.setBackgroundNotRestrictedConstraintSatisfied(canRun); |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 163 | private final class UpdateJobFunctor implements JobStore.JobStatusFunctor { |
| 164 | private final int mFilterUid; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 165 | |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 166 | boolean mChanged = false; |
| 167 | int mTotalCount = 0; |
| 168 | int mCheckedCount = 0; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 169 | |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 170 | UpdateJobFunctor(int filterUid) { |
| 171 | mFilterUid = filterUid; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void process(JobStatus jobStatus) { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 176 | mTotalCount++; |
| 177 | if ((mFilterUid > 0) && (mFilterUid != jobStatus.getSourceUid())) { |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 178 | return; |
| 179 | } |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 180 | mCheckedCount++; |
| 181 | if (updateSingleJobRestrictionLocked(jobStatus)) { |
| 182 | mChanged = true; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 186 | |
| 187 | private final Listener mForceAppStandbyListener = new Listener() { |
| 188 | @Override |
Makoto Onuki | 2206af39 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 189 | public void updateAllJobs() { |
| 190 | updateAllJobRestrictionsLocked(); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void updateJobsForUid(int uid) { |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 195 | updateJobRestrictionsForUidLocked(uid); |
| 196 | } |
| 197 | |
| 198 | @Override |
Makoto Onuki | 2206af39 | 2017-11-21 16:25:35 -0800 | [diff] [blame] | 199 | public void updateJobsForUidPackage(int uid, String packageName) { |
| 200 | updateJobRestrictionsForUidLocked(uid); |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 201 | } |
| 202 | }; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 203 | } |