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