OSDN Git Service

Merge "docs: Add documentation for equals() method" into qt-dev am: 732a127636
[android-x86/frameworks-base.git] / packages / SystemUI / src / com / android / systemui / doze / DozeScreenState.java
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.systemui.doze;
18
19 import android.os.Handler;
20 import android.util.Log;
21 import android.view.Display;
22
23 import com.android.systemui.statusbar.phone.DozeParameters;
24 import com.android.systemui.util.wakelock.SettableWakeLock;
25 import com.android.systemui.util.wakelock.WakeLock;
26
27 /**
28  * Controls the screen when dozing.
29  */
30 public class DozeScreenState implements DozeMachine.Part {
31
32     private static final boolean DEBUG = DozeService.DEBUG;
33     private static final String TAG = "DozeScreenState";
34
35     /**
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 = 4000;
40     /**
41      * Hide wallpaper earlier when entering low power mode. The gap between
42      * hiding the wallpaper and changing the display mode is necessary to hide
43      * the black frame that's inherent to hardware specs.
44      */
45     public static final int ENTER_DOZE_HIDE_WALLPAPER_DELAY = 2500;
46
47     private final DozeMachine.Service mDozeService;
48     private final Handler mHandler;
49     private final Runnable mApplyPendingScreenState = this::applyPendingScreenState;
50     private final DozeParameters mParameters;
51
52     private int mPendingScreenState = Display.STATE_UNKNOWN;
53     private SettableWakeLock mWakeLock;
54
55     public DozeScreenState(DozeMachine.Service service, Handler handler,
56             DozeParameters parameters, WakeLock wakeLock) {
57         mDozeService = service;
58         mHandler = handler;
59         mParameters = parameters;
60         mWakeLock = new SettableWakeLock(wakeLock, TAG);
61     }
62
63     @Override
64     public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
65         int screenState = newState.screenState(mParameters);
66
67         if (newState == DozeMachine.State.FINISH) {
68             // Make sure not to apply the screen state after DozeService was destroyed.
69             mPendingScreenState = Display.STATE_UNKNOWN;
70             mHandler.removeCallbacks(mApplyPendingScreenState);
71
72             applyScreenState(screenState);
73             mWakeLock.setAcquired(false);
74             return;
75         }
76
77         if (screenState == Display.STATE_UNKNOWN) {
78             // We'll keep it in the existing state
79             return;
80         }
81
82         boolean messagePending = mHandler.hasCallbacks(mApplyPendingScreenState);
83         boolean pulseEnding = oldState  == DozeMachine.State.DOZE_PULSE_DONE
84                 && newState == DozeMachine.State.DOZE_AOD;
85         boolean turningOn = (oldState == DozeMachine.State.DOZE_AOD_PAUSED
86                 || oldState  == DozeMachine.State.DOZE) && newState == DozeMachine.State.DOZE_AOD;
87         boolean justInitialized = oldState == DozeMachine.State.INITIALIZED;
88         if (messagePending || justInitialized || pulseEnding || turningOn) {
89             // During initialization, we hide the navigation bar. That is however only applied after
90             // a traversal; setting the screen state here is immediate however, so it can happen
91             // that the screen turns on again before the navigation bar is hidden. To work around
92             // that, wait for a traversal to happen before applying the initial screen state.
93             mPendingScreenState = screenState;
94
95             // Delay screen state transitions even longer while animations are running.
96             boolean shouldDelayTransition = newState == DozeMachine.State.DOZE_AOD
97                     && mParameters.shouldControlScreenOff() && !turningOn;
98
99             if (shouldDelayTransition) {
100                 mWakeLock.setAcquired(true);
101             }
102
103             if (!messagePending) {
104                 if (DEBUG) {
105                     Log.d(TAG, "Display state changed to " + screenState + " delayed by "
106                             + (shouldDelayTransition ? ENTER_DOZE_DELAY : 1));
107                 }
108
109                 if (shouldDelayTransition) {
110                     mHandler.postDelayed(mApplyPendingScreenState, ENTER_DOZE_DELAY);
111                 } else {
112                     mHandler.post(mApplyPendingScreenState);
113                 }
114             } else if (DEBUG) {
115                 Log.d(TAG, "Pending display state change to " + screenState);
116             }
117         } else {
118             applyScreenState(screenState);
119         }
120     }
121
122     private void applyPendingScreenState() {
123         applyScreenState(mPendingScreenState);
124         mPendingScreenState = Display.STATE_UNKNOWN;
125     }
126
127     private void applyScreenState(int screenState) {
128         if (screenState != Display.STATE_UNKNOWN) {
129             if (DEBUG) Log.d(TAG, "setDozeScreenState(" + screenState + ")");
130             mDozeService.setDozeScreenState(screenState);
131             mPendingScreenState = Display.STATE_UNKNOWN;
132             mWakeLock.setAcquired(false);
133         }
134     }
135 }