blob: a0fdb9dc5b90f33d27c8fe43d561e4a236bba2e5 [file] [log] [blame]
Adrian Roos2981eb02017-05-26 18:40:09 -07001/*
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
17package com.android.systemui.doze;
18
Adrian Roos8db93112017-08-11 19:43:17 +020019import android.os.Handler;
Lucas Dupin58543842018-02-15 14:00:54 -080020import android.util.Log;
Adrian Roos2981eb02017-05-26 18:40:09 -070021import android.view.Display;
22
Lucas Dupin12663d32018-02-08 11:42:22 -080023import com.android.systemui.statusbar.phone.DozeParameters;
Adrian Roos39880be2018-04-23 17:41:17 +020024import com.android.systemui.util.wakelock.SettableWakeLock;
Lucas Dupin16cfe452018-02-08 13:14:50 -080025import com.android.systemui.util.wakelock.WakeLock;
Lucas Dupin12663d32018-02-08 11:42:22 -080026
Adrian Roos2981eb02017-05-26 18:40:09 -070027/**
28 * Controls the screen when dozing.
29 */
30public class DozeScreenState implements DozeMachine.Part {
Lucas Dupin58543842018-02-15 14:00:54 -080031
32 private static final boolean DEBUG = DozeService.DEBUG;
33 private static final String TAG = "DozeScreenState";
34
Lucas Dupin16cfe452018-02-08 13:14:50 -080035 /**
36 * Delay entering low power mode when animating to make sure that we'll have
37 * time to move all elements into their final positions while still at 60 fps.
38 */
39 private static final int ENTER_DOZE_DELAY = 3000;
40
Adrian Roos2981eb02017-05-26 18:40:09 -070041 private final DozeMachine.Service mDozeService;
Adrian Roos8db93112017-08-11 19:43:17 +020042 private final Handler mHandler;
Adrian Roosa79ad592017-08-31 15:17:17 +020043 private final Runnable mApplyPendingScreenState = this::applyPendingScreenState;
Lucas Dupin12663d32018-02-08 11:42:22 -080044 private final DozeParameters mParameters;
Adrian Roosa79ad592017-08-31 15:17:17 +020045
Adrian Roos8db93112017-08-11 19:43:17 +020046 private int mPendingScreenState = Display.STATE_UNKNOWN;
Adrian Roos39880be2018-04-23 17:41:17 +020047 private SettableWakeLock mWakeLock;
Adrian Roos2981eb02017-05-26 18:40:09 -070048
Lucas Dupin12663d32018-02-08 11:42:22 -080049 public DozeScreenState(DozeMachine.Service service, Handler handler,
Lucas Dupin16cfe452018-02-08 13:14:50 -080050 DozeParameters parameters, WakeLock wakeLock) {
Adrian Roos2981eb02017-05-26 18:40:09 -070051 mDozeService = service;
Adrian Roos8db93112017-08-11 19:43:17 +020052 mHandler = handler;
Lucas Dupin12663d32018-02-08 11:42:22 -080053 mParameters = parameters;
Adrian Roos39880be2018-04-23 17:41:17 +020054 mWakeLock = new SettableWakeLock(wakeLock);
Adrian Roos2981eb02017-05-26 18:40:09 -070055 }
56
57 @Override
58 public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
Lucas Dupin12663d32018-02-08 11:42:22 -080059 int screenState = newState.screenState(mParameters);
Adrian Roos5502b892017-08-22 15:42:12 +020060
61 if (newState == DozeMachine.State.FINISH) {
62 // Make sure not to apply the screen state after DozeService was destroyed.
63 mPendingScreenState = Display.STATE_UNKNOWN;
64 mHandler.removeCallbacks(mApplyPendingScreenState);
65
66 applyScreenState(screenState);
Adrian Roos39880be2018-04-23 17:41:17 +020067 mWakeLock.setAcquired(false);
Adrian Roos5502b892017-08-22 15:42:12 +020068 return;
69 }
70
Adrian Roos8db93112017-08-11 19:43:17 +020071 if (screenState == Display.STATE_UNKNOWN) {
72 // We'll keep it in the existing state
73 return;
74 }
Adrian Roos5502b892017-08-22 15:42:12 +020075
Adrian Roos8db93112017-08-11 19:43:17 +020076 boolean messagePending = mHandler.hasCallbacks(mApplyPendingScreenState);
77 if (messagePending || oldState == DozeMachine.State.INITIALIZED) {
78 // During initialization, we hide the navigation bar. That is however only applied after
79 // a traversal; setting the screen state here is immediate however, so it can happen
80 // that the screen turns on again before the navigation bar is hidden. To work around
81 // that, wait for a traversal to happen before applying the initial screen state.
82 mPendingScreenState = screenState;
Lucas Dupin16cfe452018-02-08 13:14:50 -080083
84 // Delay screen state transitions even longer while animations are running.
85 boolean shouldDelayTransition = newState == DozeMachine.State.DOZE_AOD
86 && mParameters.shouldControlScreenOff();
87
Adrian Roos39880be2018-04-23 17:41:17 +020088 if (shouldDelayTransition) {
89 mWakeLock.setAcquired(true);
Adrian Roos8db93112017-08-11 19:43:17 +020090 }
Lucas Dupin16cfe452018-02-08 13:14:50 -080091
92 if (!messagePending) {
93 if (DEBUG) {
94 Log.d(TAG, "Display state changed to " + screenState + " delayed by "
95 + (shouldDelayTransition ? ENTER_DOZE_DELAY : 1));
96 }
97
98 if (shouldDelayTransition) {
99 mHandler.postDelayed(mApplyPendingScreenState, ENTER_DOZE_DELAY);
100 } else {
101 mHandler.post(mApplyPendingScreenState);
102 }
103 } else if (DEBUG) {
104 Log.d(TAG, "Pending display state change to " + screenState);
105 }
106 } else {
107 applyScreenState(screenState);
Adrian Roos8db93112017-08-11 19:43:17 +0200108 }
Adrian Roos8db93112017-08-11 19:43:17 +0200109 }
110
111 private void applyPendingScreenState() {
112 applyScreenState(mPendingScreenState);
113 mPendingScreenState = Display.STATE_UNKNOWN;
114 }
115
116 private void applyScreenState(int screenState) {
Adrian Roos2981eb02017-05-26 18:40:09 -0700117 if (screenState != Display.STATE_UNKNOWN) {
Lucas Dupin58543842018-02-15 14:00:54 -0800118 if (DEBUG) Log.d(TAG, "setDozeScreenState(" + screenState + ")");
Adrian Roos2981eb02017-05-26 18:40:09 -0700119 mDozeService.setDozeScreenState(screenState);
Adrian Roos8db93112017-08-11 19:43:17 +0200120 mPendingScreenState = Display.STATE_UNKNOWN;
Adrian Roos39880be2018-04-23 17:41:17 +0200121 mWakeLock.setAcquired(false);
Adrian Roos2981eb02017-05-26 18:40:09 -0700122 }
123 }
124}