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