1 /*
2  * Copyright (C) 2022 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.server.display.state;
18 
19 import android.hardware.display.DisplayManagerInternal;
20 import android.util.IndentingPrintWriter;
21 import android.util.Pair;
22 import android.view.Display;
23 
24 import com.android.server.display.DisplayPowerProximityStateController;
25 
26 import java.io.PrintWriter;
27 
28 /**
29  * Maintains the DisplayState of the system.
30  * Internally, this accounts for the proximity changes, and notifying the system
31  * clients about the changes
32  */
33 public class DisplayStateController {
34     private DisplayPowerProximityStateController mDisplayPowerProximityStateController;
35     private boolean mPerformScreenOffTransition = false;
36     private int mDozeStateOverride = Display.STATE_UNKNOWN;
37     private int mDozeStateOverrideReason = Display.STATE_REASON_UNKNOWN;
38 
DisplayStateController(DisplayPowerProximityStateController displayPowerProximityStateController)39     public DisplayStateController(DisplayPowerProximityStateController
40             displayPowerProximityStateController) {
41         this.mDisplayPowerProximityStateController = displayPowerProximityStateController;
42     }
43 
44     /**
45      * Updates the DisplayState and notifies the system. Also accounts for the
46      * events being emitted by the proximity sensors
47      *
48      * @param displayPowerRequest   The request to update the display state
49      * @param isDisplayEnabled      A boolean flag representing if the display is enabled
50      * @param isDisplayInTransition A boolean flag representing if the display is undergoing the
51      *                              transition phase
52      * @return a {@link Pair} of integers, the first being the updated display state, and the second
53      *                              being the reason behind the new display state.
54      */
updateDisplayState( DisplayManagerInternal.DisplayPowerRequest displayPowerRequest, boolean isDisplayEnabled, boolean isDisplayInTransition)55     public Pair<Integer, Integer> updateDisplayState(
56             DisplayManagerInternal.DisplayPowerRequest displayPowerRequest,
57             boolean isDisplayEnabled,
58             boolean isDisplayInTransition) {
59         mPerformScreenOffTransition = false;
60         // Compute the basic display state using the policy.
61         // We might override this below based on other factors.
62         // Initialise brightness as invalid.
63         int state;
64         int reason = Display.STATE_REASON_DEFAULT_POLICY;
65         switch (displayPowerRequest.policy) {
66             case DisplayManagerInternal.DisplayPowerRequest.POLICY_OFF:
67                 state = Display.STATE_OFF;
68                 mPerformScreenOffTransition = true;
69                 break;
70             case DisplayManagerInternal.DisplayPowerRequest.POLICY_DOZE:
71                 if (mDozeStateOverride != Display.STATE_UNKNOWN) {
72                     state = mDozeStateOverride;
73                     reason = mDozeStateOverrideReason;
74                 } else if (displayPowerRequest.dozeScreenState != Display.STATE_UNKNOWN) {
75                     state = displayPowerRequest.dozeScreenState;
76                     reason = displayPowerRequest.dozeScreenStateReason;
77                 } else {
78                     state = Display.STATE_DOZE;
79                 }
80                 break;
81             case DisplayManagerInternal.DisplayPowerRequest.POLICY_DIM:
82             case DisplayManagerInternal.DisplayPowerRequest.POLICY_BRIGHT:
83             default:
84                 state = Display.STATE_ON;
85                 break;
86         }
87         assert (state != Display.STATE_UNKNOWN);
88 
89         mDisplayPowerProximityStateController.updateProximityState(displayPowerRequest, state);
90 
91         if (!isDisplayEnabled || isDisplayInTransition
92                 || mDisplayPowerProximityStateController.isScreenOffBecauseOfProximity()) {
93             state = Display.STATE_OFF;
94         }
95 
96         return new Pair(state, reason);
97     }
98 
99     /** Overrides the doze screen state with a given reason. */
overrideDozeScreenState(int displayState, @Display.StateReason int reason)100     public void overrideDozeScreenState(int displayState, @Display.StateReason int reason) {
101         mDozeStateOverride = displayState;
102         mDozeStateOverrideReason = reason;
103     }
104 
105     /**
106      * Checks if the screen off transition is to be performed or not.
107      */
shouldPerformScreenOffTransition()108     public boolean shouldPerformScreenOffTransition() {
109         return mPerformScreenOffTransition;
110     }
111 
112     /**
113      * Used to dump the state.
114      *
115      * @param pw The PrintWriter used to dump the state.
116      */
dumpsys(PrintWriter pw)117     public void dumpsys(PrintWriter pw) {
118         pw.println();
119         pw.println("DisplayStateController:");
120         pw.println("  mPerformScreenOffTransition:" + mPerformScreenOffTransition);
121         pw.println("  mDozeStateOverride=" + mDozeStateOverride);
122 
123         IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
124         if (mDisplayPowerProximityStateController != null) {
125             mDisplayPowerProximityStateController.dumpLocal(ipw);
126         }
127     }
128 }
129