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