Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [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.systemui.doze; |
| 18 | |
Lucas Dupin | bb83b89 | 2019-07-19 11:45:16 -0700 | [diff] [blame] | 19 | import static com.android.systemui.doze.DozeMachine.State.DOZE; |
Lucas Dupin | 4df92c3 | 2018-01-08 10:21:52 -0800 | [diff] [blame] | 20 | import static com.android.systemui.doze.DozeMachine.State.DOZE_AOD_PAUSED; |
| 21 | |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 22 | import android.app.AlarmManager; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 24 | import android.os.Handler; |
| 25 | import android.os.SystemClock; |
Adrian Roos | e4b564c | 2017-04-14 14:48:05 -0700 | [diff] [blame] | 26 | import android.text.format.Formatter; |
| 27 | import android.util.Log; |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 28 | |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 29 | import com.android.internal.annotations.VisibleForTesting; |
| 30 | import com.android.keyguard.KeyguardUpdateMonitor; |
| 31 | import com.android.keyguard.KeyguardUpdateMonitorCallback; |
Lucas Dupin | 43d0d73 | 2017-11-16 11:23:49 -0800 | [diff] [blame] | 32 | import com.android.systemui.statusbar.phone.DozeParameters; |
Adrian Roos | ef5cf2b | 2017-06-28 15:46:03 +0200 | [diff] [blame] | 33 | import com.android.systemui.util.AlarmTimeout; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 34 | import com.android.systemui.util.wakelock.WakeLock; |
| 35 | |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 36 | import java.util.Calendar; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * The policy controlling doze. |
| 40 | */ |
| 41 | public class DozeUi implements DozeMachine.Part { |
| 42 | |
Adrian Roos | e4b564c | 2017-04-14 14:48:05 -0700 | [diff] [blame] | 43 | private static final long TIME_TICK_DEADLINE_MILLIS = 90 * 1000; // 1.5min |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 44 | private final Context mContext; |
| 45 | private final DozeHost mHost; |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 46 | private final Handler mHandler; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 47 | private final WakeLock mWakeLock; |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 48 | private final DozeMachine mMachine; |
Adrian Roos | ef5cf2b | 2017-06-28 15:46:03 +0200 | [diff] [blame] | 49 | private final AlarmTimeout mTimeTicker; |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 50 | private final boolean mCanAnimateTransition; |
| 51 | private final DozeParameters mDozeParameters; |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 52 | private final DozeLog mDozeLog; |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 53 | |
| 54 | private boolean mKeyguardShowing; |
| 55 | private final KeyguardUpdateMonitorCallback mKeyguardVisibilityCallback = |
| 56 | new KeyguardUpdateMonitorCallback() { |
| 57 | |
| 58 | @Override |
| 59 | public void onKeyguardVisibilityChanged(boolean showing) { |
| 60 | mKeyguardShowing = showing; |
| 61 | updateAnimateScreenOff(); |
| 62 | } |
| 63 | }; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 64 | |
Adrian Roos | e4b564c | 2017-04-14 14:48:05 -0700 | [diff] [blame] | 65 | private long mLastTimeTickElapsed = 0; |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 66 | |
| 67 | public DozeUi(Context context, AlarmManager alarmManager, DozeMachine machine, |
Lucas Dupin | 43d0d73 | 2017-11-16 11:23:49 -0800 | [diff] [blame] | 68 | WakeLock wakeLock, DozeHost host, Handler handler, |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 69 | DozeParameters params, KeyguardUpdateMonitor keyguardUpdateMonitor, |
| 70 | DozeLog dozeLog) { |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 71 | mContext = context; |
| 72 | mMachine = machine; |
| 73 | mWakeLock = wakeLock; |
| 74 | mHost = host; |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 75 | mHandler = handler; |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 76 | mCanAnimateTransition = !params.getDisplayNeedsBlanking(); |
| 77 | mDozeParameters = params; |
Adrian Roos | ef5cf2b | 2017-06-28 15:46:03 +0200 | [diff] [blame] | 78 | mTimeTicker = new AlarmTimeout(alarmManager, this::onTimeTick, "doze_time_tick", handler); |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 79 | keyguardUpdateMonitor.registerCallback(mKeyguardVisibilityCallback); |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 80 | mDozeLog = dozeLog; |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Decide if we're taking over the screen-off animation |
| 85 | * when the device was configured to skip doze after screen off. |
| 86 | */ |
| 87 | private void updateAnimateScreenOff() { |
| 88 | if (mCanAnimateTransition) { |
Lucas Dupin | bd7366d | 2019-09-25 13:39:21 -0700 | [diff] [blame] | 89 | final boolean controlScreenOff = mDozeParameters.getAlwaysOn() && mKeyguardShowing |
| 90 | && !mHost.isPowerSaveActive(); |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 91 | mDozeParameters.setControlScreenOffAnimation(controlScreenOff); |
| 92 | mHost.setAnimateScreenOff(controlScreenOff); |
| 93 | } |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | private void pulseWhileDozing(int reason) { |
| 97 | mHost.pulseWhileDozing( |
| 98 | new DozeHost.PulseCallback() { |
| 99 | @Override |
| 100 | public void onPulseStarted() { |
Lucas Dupin | 09f8527 | 2019-03-08 15:04:06 -0800 | [diff] [blame] | 101 | try { |
Lucas Dupin | 5f00fa5 | 2019-03-27 22:46:53 -0700 | [diff] [blame] | 102 | mMachine.requestState( |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 103 | reason == DozeEvent.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN |
Lucas Dupin | 5f00fa5 | 2019-03-27 22:46:53 -0700 | [diff] [blame] | 104 | ? DozeMachine.State.DOZE_PULSING_BRIGHT |
| 105 | : DozeMachine.State.DOZE_PULSING); |
Lucas Dupin | 09f8527 | 2019-03-08 15:04:06 -0800 | [diff] [blame] | 106 | } catch (IllegalStateException e) { |
| 107 | // It's possible that the pulse was asynchronously cancelled while |
| 108 | // we were waiting for it to start (under stress conditions.) |
| 109 | // In those cases we should just ignore it. b/127657926 |
| 110 | } |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void onPulseFinished() { |
| 115 | mMachine.requestState(DozeMachine.State.DOZE_PULSE_DONE); |
| 116 | } |
| 117 | }, reason); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) { |
| 122 | switch (newState) { |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 123 | case DOZE_AOD: |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 124 | case DOZE_AOD_DOCKED: |
Lucas Dupin | bb83b89 | 2019-07-19 11:45:16 -0700 | [diff] [blame] | 125 | if (oldState == DOZE_AOD_PAUSED || oldState == DOZE) { |
Lucas Dupin | f9583c4 | 2018-04-05 22:20:44 -0700 | [diff] [blame] | 126 | // Whenever turning on the display, it's necessary to push a new frame. |
| 127 | // The display buffers will be empty and need to be filled. |
Lucas Dupin | 4df92c3 | 2018-01-08 10:21:52 -0800 | [diff] [blame] | 128 | mHost.dozeTimeTick(); |
Lucas Dupin | f9583c4 | 2018-04-05 22:20:44 -0700 | [diff] [blame] | 129 | // The first frame may arrive when the display isn't ready yet. |
Lucas Dupin | a11298b | 2018-06-26 15:09:08 -0700 | [diff] [blame] | 130 | mHandler.postDelayed(mWakeLock.wrap(mHost::dozeTimeTick), 500); |
Lucas Dupin | 4df92c3 | 2018-01-08 10:21:52 -0800 | [diff] [blame] | 131 | } |
| 132 | scheduleTimeTick(); |
| 133 | break; |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 134 | case DOZE_AOD_PAUSING: |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 135 | scheduleTimeTick(); |
| 136 | break; |
| 137 | case DOZE: |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 138 | case DOZE_AOD_PAUSED: |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 139 | unscheduleTimeTick(); |
| 140 | break; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 141 | case DOZE_REQUEST_PULSE: |
TYM Tsai | 25c0549 | 2019-01-04 16:59:07 +0800 | [diff] [blame] | 142 | scheduleTimeTick(); |
Adrian Roos | d7b9d10 | 2017-04-28 15:42:58 -0700 | [diff] [blame] | 143 | pulseWhileDozing(mMachine.getPulseReason()); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 144 | break; |
| 145 | case INITIALIZED: |
| 146 | mHost.startDozing(); |
| 147 | break; |
| 148 | case FINISH: |
| 149 | mHost.stopDozing(); |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 150 | unscheduleTimeTick(); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 151 | break; |
| 152 | } |
Adrian Roos | 013fc4c | 2017-08-02 19:32:28 +0200 | [diff] [blame] | 153 | updateAnimateWakeup(newState); |
Adrian Roos | 28f90c7 | 2017-05-08 17:24:26 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Adrian Roos | 013fc4c | 2017-08-02 19:32:28 +0200 | [diff] [blame] | 156 | private void updateAnimateWakeup(DozeMachine.State state) { |
Adrian Roos | 28f90c7 | 2017-05-08 17:24:26 -0700 | [diff] [blame] | 157 | switch (state) { |
Adrian Roos | 28f90c7 | 2017-05-08 17:24:26 -0700 | [diff] [blame] | 158 | case DOZE_REQUEST_PULSE: |
| 159 | case DOZE_PULSING: |
Lucas Dupin | 5f00fa5 | 2019-03-27 22:46:53 -0700 | [diff] [blame] | 160 | case DOZE_PULSING_BRIGHT: |
Adrian Roos | 28f90c7 | 2017-05-08 17:24:26 -0700 | [diff] [blame] | 161 | case DOZE_PULSE_DONE: |
Adrian Roos | 013fc4c | 2017-08-02 19:32:28 +0200 | [diff] [blame] | 162 | mHost.setAnimateWakeup(true); |
| 163 | break; |
| 164 | case FINISH: |
| 165 | // Keep current state. |
| 166 | break; |
Adrian Roos | 28f90c7 | 2017-05-08 17:24:26 -0700 | [diff] [blame] | 167 | default: |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 168 | mHost.setAnimateWakeup(mCanAnimateTransition && mDozeParameters.getAlwaysOn()); |
Adrian Roos | 013fc4c | 2017-08-02 19:32:28 +0200 | [diff] [blame] | 169 | break; |
Adrian Roos | 28f90c7 | 2017-05-08 17:24:26 -0700 | [diff] [blame] | 170 | } |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 171 | } |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 172 | |
| 173 | private void scheduleTimeTick() { |
Adrian Roos | ef5cf2b | 2017-06-28 15:46:03 +0200 | [diff] [blame] | 174 | if (mTimeTicker.isScheduled()) { |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
Lucas Dupin | 69d4b57 | 2018-09-06 10:36:08 -0700 | [diff] [blame] | 178 | long time = System.currentTimeMillis(); |
| 179 | long delta = roundToNextMinute(time) - System.currentTimeMillis(); |
| 180 | boolean scheduled = mTimeTicker.schedule(delta, AlarmTimeout.MODE_IGNORE_IF_SCHEDULED); |
| 181 | if (scheduled) { |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 182 | mDozeLog.traceTimeTickScheduled(time, time + delta); |
Lucas Dupin | 69d4b57 | 2018-09-06 10:36:08 -0700 | [diff] [blame] | 183 | } |
Adrian Roos | e4b564c | 2017-04-14 14:48:05 -0700 | [diff] [blame] | 184 | mLastTimeTickElapsed = SystemClock.elapsedRealtime(); |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | private void unscheduleTimeTick() { |
Adrian Roos | ef5cf2b | 2017-06-28 15:46:03 +0200 | [diff] [blame] | 188 | if (!mTimeTicker.isScheduled()) { |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 189 | return; |
| 190 | } |
Adrian Roos | e4b564c | 2017-04-14 14:48:05 -0700 | [diff] [blame] | 191 | verifyLastTimeTick(); |
Adrian Roos | ef5cf2b | 2017-06-28 15:46:03 +0200 | [diff] [blame] | 192 | mTimeTicker.cancel(); |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Adrian Roos | e4b564c | 2017-04-14 14:48:05 -0700 | [diff] [blame] | 195 | private void verifyLastTimeTick() { |
| 196 | long millisSinceLastTick = SystemClock.elapsedRealtime() - mLastTimeTickElapsed; |
| 197 | if (millisSinceLastTick > TIME_TICK_DEADLINE_MILLIS) { |
| 198 | String delay = Formatter.formatShortElapsedTime(mContext, millisSinceLastTick); |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 199 | mDozeLog.traceMissedTick(delay); |
Adrian Roos | e4b564c | 2017-04-14 14:48:05 -0700 | [diff] [blame] | 200 | Log.e(DozeMachine.TAG, "Missed AOD time tick by " + delay); |
| 201 | } |
| 202 | } |
| 203 | |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 204 | private long roundToNextMinute(long timeInMillis) { |
Neil Fuller | 9c610f7 | 2018-07-04 16:41:14 +0100 | [diff] [blame] | 205 | Calendar calendar = Calendar.getInstance(); |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 206 | calendar.setTimeInMillis(timeInMillis); |
| 207 | calendar.set(Calendar.MILLISECOND, 0); |
| 208 | calendar.set(Calendar.SECOND, 0); |
| 209 | calendar.add(Calendar.MINUTE, 1); |
| 210 | |
| 211 | return calendar.getTimeInMillis(); |
| 212 | } |
| 213 | |
| 214 | private void onTimeTick() { |
Adrian Roos | e4b564c | 2017-04-14 14:48:05 -0700 | [diff] [blame] | 215 | verifyLastTimeTick(); |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 216 | |
| 217 | mHost.dozeTimeTick(); |
| 218 | |
| 219 | // Keep wakelock until a frame has been pushed. |
| 220 | mHandler.post(mWakeLock.wrap(() -> {})); |
| 221 | |
Adrian Roos | fe54aa0 | 2016-11-07 14:14:25 -0800 | [diff] [blame] | 222 | scheduleTimeTick(); |
| 223 | } |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 224 | |
| 225 | @VisibleForTesting |
| 226 | KeyguardUpdateMonitorCallback getKeyguardCallback() { |
| 227 | return mKeyguardVisibilityCallback; |
| 228 | } |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 229 | } |