Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -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.controllers; |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -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; |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 20 | |
| 21 | import android.app.AlarmManager; |
| 22 | import android.app.PendingIntent; |
| 23 | import android.content.BroadcastReceiver; |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 27 | import android.os.UserHandle; |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 28 | import android.util.ArraySet; |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 29 | import android.util.Slog; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 30 | import android.util.proto.ProtoOutputStream; |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 31 | |
Christopher Tate | 27d92e4 | 2016-05-06 11:25:11 -0700 | [diff] [blame] | 32 | import com.android.server.am.ActivityManagerService; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 33 | import com.android.server.job.JobSchedulerService; |
| 34 | import com.android.server.job.StateChangedListener; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 35 | import com.android.server.job.StateControllerProto; |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 36 | |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 37 | import java.io.PrintWriter; |
| 38 | |
Dianne Hackborn | 6466c1c | 2017-06-13 10:33:19 -0700 | [diff] [blame] | 39 | public final class IdleController extends StateController { |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 40 | private static final String TAG = "IdleController"; |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 41 | |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 42 | // Policy: we decide that we're "idle" if the device has been unused / |
| 43 | // screen off or dreaming for at least this long |
| 44 | private long mInactivityIdleThreshold; |
| 45 | private long mIdleWindowSlop; |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 46 | final ArraySet<JobStatus> mTrackedTasks = new ArraySet<>(); |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 47 | IdlenessTracker mIdleTracker; |
| 48 | |
| 49 | // Singleton factory |
| 50 | private static Object sCreationLock = new Object(); |
| 51 | private static volatile IdleController sController; |
| 52 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 53 | public static IdleController get(JobSchedulerService service) { |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 54 | synchronized (sCreationLock) { |
| 55 | if (sController == null) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 56 | sController = new IdleController(service, service.getContext(), service.getLock()); |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 57 | } |
| 58 | return sController; |
| 59 | } |
| 60 | } |
| 61 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 62 | private IdleController(StateChangedListener stateChangedListener, Context context, |
| 63 | Object lock) { |
| 64 | super(stateChangedListener, context, lock); |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 65 | initIdleStateTracking(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * StateController interface |
| 70 | */ |
| 71 | @Override |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 72 | public void maybeStartTrackingJobLocked(JobStatus taskStatus, JobStatus lastJob) { |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 73 | if (taskStatus.hasIdleConstraint()) { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 74 | mTrackedTasks.add(taskStatus); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 75 | taskStatus.setTrackingController(JobStatus.TRACKING_IDLE); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 76 | taskStatus.setIdleConstraintSatisfied(mIdleTracker.isIdle()); |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | @Override |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 81 | public void maybeStopTrackingJobLocked(JobStatus taskStatus, JobStatus incomingJob, |
| 82 | boolean forUpdate) { |
| 83 | if (taskStatus.clearTrackingController(JobStatus.TRACKING_IDLE)) { |
| 84 | mTrackedTasks.remove(taskStatus); |
| 85 | } |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Interaction with the task manager service |
| 90 | */ |
| 91 | void reportNewIdleState(boolean isIdle) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 92 | synchronized (mLock) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 93 | for (int i = mTrackedTasks.size()-1; i >= 0; i--) { |
| 94 | mTrackedTasks.valueAt(i).setIdleConstraintSatisfied(isIdle); |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 97 | mStateChangedListener.onControllerStateChanged(); |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Idle state tracking, and messaging with the task manager when |
| 102 | * significant state changes occur |
| 103 | */ |
| 104 | private void initIdleStateTracking() { |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 105 | mInactivityIdleThreshold = mContext.getResources().getInteger( |
| 106 | com.android.internal.R.integer.config_jobSchedulerInactivityIdleThreshold); |
| 107 | mIdleWindowSlop = mContext.getResources().getInteger( |
| 108 | com.android.internal.R.integer.config_jobSchedulerIdleWindowSlop); |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 109 | mIdleTracker = new IdlenessTracker(); |
| 110 | mIdleTracker.startTracking(); |
| 111 | } |
| 112 | |
Dianne Hackborn | 6466c1c | 2017-06-13 10:33:19 -0700 | [diff] [blame] | 113 | final class IdlenessTracker extends BroadcastReceiver { |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 114 | private AlarmManager mAlarm; |
| 115 | private PendingIntent mIdleTriggerIntent; |
| 116 | boolean mIdle; |
Kevin Zhu | ccfe873 | 2015-06-29 16:06:49 -0700 | [diff] [blame] | 117 | boolean mScreenOn; |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 118 | |
| 119 | public IdlenessTracker() { |
| 120 | mAlarm = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
| 121 | |
Christopher Tate | 27d92e4 | 2016-05-06 11:25:11 -0700 | [diff] [blame] | 122 | Intent intent = new Intent(ActivityManagerService.ACTION_TRIGGER_IDLE) |
Matthew Williams | be0c417 | 2014-08-06 18:14:16 -0700 | [diff] [blame] | 123 | .setPackage("android") |
| 124 | .setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 125 | mIdleTriggerIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
| 126 | |
Matthew Williams | be0c417 | 2014-08-06 18:14:16 -0700 | [diff] [blame] | 127 | // At boot we presume that the user has just "interacted" with the |
| 128 | // device in some meaningful way. |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 129 | mIdle = false; |
Kevin Zhu | ccfe873 | 2015-06-29 16:06:49 -0700 | [diff] [blame] | 130 | mScreenOn = true; |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | public boolean isIdle() { |
| 134 | return mIdle; |
| 135 | } |
| 136 | |
| 137 | public void startTracking() { |
| 138 | IntentFilter filter = new IntentFilter(); |
| 139 | |
| 140 | // Screen state |
| 141 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 142 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 143 | |
| 144 | // Dreaming state |
| 145 | filter.addAction(Intent.ACTION_DREAMING_STARTED); |
| 146 | filter.addAction(Intent.ACTION_DREAMING_STOPPED); |
| 147 | |
Christopher Tate | b9583c9 | 2014-07-24 17:03:22 -0700 | [diff] [blame] | 148 | // Debugging/instrumentation |
Christopher Tate | 27d92e4 | 2016-05-06 11:25:11 -0700 | [diff] [blame] | 149 | filter.addAction(ActivityManagerService.ACTION_TRIGGER_IDLE); |
Christopher Tate | b9583c9 | 2014-07-24 17:03:22 -0700 | [diff] [blame] | 150 | |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 151 | mContext.registerReceiver(this, filter); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void onReceive(Context context, Intent intent) { |
| 156 | final String action = intent.getAction(); |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 157 | if (action.equals(Intent.ACTION_SCREEN_ON) |
| 158 | || action.equals(Intent.ACTION_DREAMING_STOPPED)) { |
Kevin Zhu | ccfe873 | 2015-06-29 16:06:49 -0700 | [diff] [blame] | 159 | if (DEBUG) { |
| 160 | Slog.v(TAG,"exiting idle : " + action); |
| 161 | } |
| 162 | mScreenOn = true; |
| 163 | //cancel the alarm |
| 164 | mAlarm.cancel(mIdleTriggerIntent); |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 165 | if (mIdle) { |
Kevin Zhu | ccfe873 | 2015-06-29 16:06:49 -0700 | [diff] [blame] | 166 | // possible transition to not-idle |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 167 | mIdle = false; |
| 168 | reportNewIdleState(mIdle); |
| 169 | } |
| 170 | } else if (action.equals(Intent.ACTION_SCREEN_OFF) |
| 171 | || action.equals(Intent.ACTION_DREAMING_STARTED)) { |
| 172 | // when the screen goes off or dreaming starts, we schedule the |
| 173 | // alarm that will tell us when we have decided the device is |
| 174 | // truly idle. |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 175 | final long nowElapsed = sElapsedRealtimeClock.millis(); |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 176 | final long when = nowElapsed + mInactivityIdleThreshold; |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 177 | if (DEBUG) { |
Matthew Williams | be0c417 | 2014-08-06 18:14:16 -0700 | [diff] [blame] | 178 | Slog.v(TAG, "Scheduling idle : " + action + " now:" + nowElapsed + " when=" |
| 179 | + when); |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 180 | } |
Kevin Zhu | ccfe873 | 2015-06-29 16:06:49 -0700 | [diff] [blame] | 181 | mScreenOn = false; |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 182 | mAlarm.setWindow(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Yao Chen | ca5edbb | 2016-01-13 14:44:36 -0800 | [diff] [blame] | 183 | when, mIdleWindowSlop, mIdleTriggerIntent); |
Christopher Tate | 27d92e4 | 2016-05-06 11:25:11 -0700 | [diff] [blame] | 184 | } else if (action.equals(ActivityManagerService.ACTION_TRIGGER_IDLE)) { |
Kevin Zhu | ccfe873 | 2015-06-29 16:06:49 -0700 | [diff] [blame] | 185 | // idle time starts now. Do not set mIdle if screen is on. |
| 186 | if (!mIdle && !mScreenOn) { |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 187 | if (DEBUG) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 188 | Slog.v(TAG, "Idle trigger fired @ " + sElapsedRealtimeClock.millis()); |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 189 | } |
| 190 | mIdle = true; |
| 191 | reportNewIdleState(mIdle); |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 192 | } else { |
| 193 | if (DEBUG) { |
| 194 | Slog.v(TAG, "TRIGGER_IDLE received but not changing state; idle=" |
| 195 | + mIdle + " screen=" + mScreenOn); |
| 196 | } |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 201 | |
| 202 | @Override |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 203 | public void dumpControllerStateLocked(PrintWriter pw, int filterUid) { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 204 | pw.print("Idle: "); |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 205 | pw.println(mIdleTracker.isIdle()); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 206 | pw.print("Tracking "); |
| 207 | pw.print(mTrackedTasks.size()); |
| 208 | pw.println(":"); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 209 | for (int i = 0; i < mTrackedTasks.size(); i++) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 210 | final JobStatus js = mTrackedTasks.valueAt(i); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 211 | if (!js.shouldDump(filterUid)) { |
| 212 | continue; |
| 213 | } |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 214 | pw.print(" #"); |
| 215 | js.printUniqueId(pw); |
| 216 | pw.print(" from "); |
| 217 | UserHandle.formatUid(pw, js.getSourceUid()); |
| 218 | pw.println(); |
Christopher Tate | 5eeb59c | 2014-07-22 10:50:22 -0700 | [diff] [blame] | 219 | } |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 220 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 221 | |
| 222 | @Override |
| 223 | public void dumpControllerStateLocked(ProtoOutputStream proto, long fieldId, int filterUid) { |
| 224 | final long token = proto.start(fieldId); |
| 225 | final long mToken = proto.start(StateControllerProto.IDLE); |
| 226 | |
| 227 | proto.write(StateControllerProto.IdleController.IS_IDLE, mIdleTracker.isIdle()); |
| 228 | |
| 229 | for (int i = 0; i < mTrackedTasks.size(); i++) { |
| 230 | final JobStatus js = mTrackedTasks.valueAt(i); |
| 231 | if (!js.shouldDump(filterUid)) { |
| 232 | continue; |
| 233 | } |
| 234 | final long jsToken = proto.start(StateControllerProto.IdleController.TRACKED_JOBS); |
| 235 | js.writeToShortProto(proto, StateControllerProto.IdleController.TrackedJob.INFO); |
| 236 | proto.write(StateControllerProto.IdleController.TrackedJob.SOURCE_UID, |
| 237 | js.getSourceUid()); |
| 238 | proto.end(jsToken); |
| 239 | } |
| 240 | |
| 241 | proto.end(mToken); |
| 242 | proto.end(token); |
| 243 | } |
Christopher Tate | 851f3d51 | 2014-05-14 17:01:58 -0700 | [diff] [blame] | 244 | } |