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.brightness.strategy;
18 
19 import android.os.PowerManager;
20 
21 import com.android.server.display.DisplayBrightnessState;
22 import com.android.server.display.brightness.BrightnessReason;
23 import com.android.server.display.brightness.BrightnessUtils;
24 import com.android.server.display.brightness.StrategyExecutionRequest;
25 import com.android.server.display.brightness.StrategySelectionNotifyRequest;
26 
27 import java.io.PrintWriter;
28 
29 /**
30  * Manages the brightness of the display when the system brightness is temporary
31  */
32 public class TemporaryBrightnessStrategy implements DisplayBrightnessStrategy {
33     // The temporary screen brightness. Typically set when a user is interacting with the
34     // brightness slider but hasn't settled on a choice yet. Set to
35     // PowerManager.BRIGHTNESS_INVALID_FLOAT when there's no temporary brightness set.
36     private float mTemporaryScreenBrightness;
37 
TemporaryBrightnessStrategy()38     public TemporaryBrightnessStrategy() {
39         mTemporaryScreenBrightness = PowerManager.BRIGHTNESS_INVALID_FLOAT;
40     }
41 
42     // Use the temporary screen brightness if there isn't an override, either from
43     // WindowManager or based on the display state.
44     @Override
updateBrightness( StrategyExecutionRequest strategyExecutionRequest)45     public DisplayBrightnessState updateBrightness(
46             StrategyExecutionRequest strategyExecutionRequest) {
47         // Todo(b/241308599): Introduce a validator class and add validations before setting
48         // the brightness
49         DisplayBrightnessState displayBrightnessState =
50                 BrightnessUtils.constructDisplayBrightnessState(BrightnessReason.REASON_TEMPORARY,
51                         mTemporaryScreenBrightness,
52                         mTemporaryScreenBrightness, getName());
53         return displayBrightnessState;
54     }
55 
56     @Override
getName()57     public String getName() {
58         return "TemporaryBrightnessStrategy";
59     }
60 
getTemporaryScreenBrightness()61     public float getTemporaryScreenBrightness() {
62         return mTemporaryScreenBrightness;
63     }
64 
setTemporaryScreenBrightness(float temporaryScreenBrightness)65     public void setTemporaryScreenBrightness(float temporaryScreenBrightness) {
66         mTemporaryScreenBrightness = temporaryScreenBrightness;
67     }
68 
69     /**
70      * Dumps the state of this class.
71      */
72     @Override
dump(PrintWriter writer)73     public void dump(PrintWriter writer) {
74         writer.println("TemporaryBrightnessStrategy:");
75         writer.println("  mTemporaryScreenBrightness:" + mTemporaryScreenBrightness);
76     }
77 
78     @Override
strategySelectionPostProcessor( StrategySelectionNotifyRequest strategySelectionNotifyRequest)79     public void strategySelectionPostProcessor(
80             StrategySelectionNotifyRequest strategySelectionNotifyRequest) {
81         // DO NOTHING
82     }
83 
84     @Override
getReason()85     public int getReason() {
86         return BrightnessReason.REASON_TEMPORARY;
87     }
88 }
89