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;
18 
19 import android.text.TextUtils;
20 
21 import com.android.server.display.brightness.BrightnessReason;
22 
23 import java.util.Objects;
24 
25 /**
26  * A state class representing a set of brightness related entities that are decided at runtime by
27  * the DisplayBrightnessModeStrategies when updating the brightness.
28  */
29 public final class DisplayBrightnessState {
30     private final float mBrightness;
31     private final float mSdrBrightness;
32     private final BrightnessReason mBrightnessReason;
33     private final String mDisplayBrightnessStrategyName;
34     private final boolean mShouldUseAutoBrightness;
35 
36     private final boolean mIsSlowChange;
37 
DisplayBrightnessState(Builder builder)38     private DisplayBrightnessState(Builder builder) {
39         mBrightness = builder.getBrightness();
40         mSdrBrightness = builder.getSdrBrightness();
41         mBrightnessReason = builder.getBrightnessReason();
42         mDisplayBrightnessStrategyName = builder.getDisplayBrightnessStrategyName();
43         mShouldUseAutoBrightness = builder.getShouldUseAutoBrightness();
44         mIsSlowChange = builder.isSlowChange();
45     }
46 
47     /**
48      * Gets the brightness
49      */
getBrightness()50     public float getBrightness() {
51         return mBrightness;
52     }
53 
54     /**
55      * Gets the sdr brightness
56      */
getSdrBrightness()57     public float getSdrBrightness() {
58         return mSdrBrightness;
59     }
60 
61     /**
62      * Gets the {@link BrightnessReason}
63      */
getBrightnessReason()64     public BrightnessReason getBrightnessReason() {
65         return mBrightnessReason;
66     }
67 
68     /**
69      * Gets the {@link com.android.server.display.brightness.strategy.DisplayBrightnessStrategy}
70      * name
71      */
getDisplayBrightnessStrategyName()72     public String getDisplayBrightnessStrategyName() {
73         return mDisplayBrightnessStrategyName;
74     }
75 
76     /**
77      * @return {@code true} if the device is set up to run auto-brightness.
78      */
getShouldUseAutoBrightness()79     public boolean getShouldUseAutoBrightness() {
80         return mShouldUseAutoBrightness;
81     }
82 
83     /**
84      * @return {@code true} if the should transit to new state slowly
85      */
isSlowChange()86     public boolean isSlowChange() {
87         return mIsSlowChange;
88     }
89 
90     @Override
toString()91     public String toString() {
92         StringBuilder stringBuilder = new StringBuilder("DisplayBrightnessState:");
93         stringBuilder.append("\n    brightness:");
94         stringBuilder.append(getBrightness());
95         stringBuilder.append("\n    sdrBrightness:");
96         stringBuilder.append(getSdrBrightness());
97         stringBuilder.append("\n    brightnessReason:");
98         stringBuilder.append(getBrightnessReason());
99         stringBuilder.append("\n    shouldUseAutoBrightness:");
100         stringBuilder.append(getShouldUseAutoBrightness());
101         stringBuilder.append("\n    isSlowChange:");
102         stringBuilder.append(mIsSlowChange);
103         return stringBuilder.toString();
104     }
105 
106     /**
107      * Checks whether the two objects have the same values.
108      */
109     @Override
equals(Object other)110     public boolean equals(Object other) {
111         if (other == this) {
112             return true;
113         }
114 
115         if (!(other instanceof DisplayBrightnessState)) {
116             return false;
117         }
118 
119         DisplayBrightnessState otherState = (DisplayBrightnessState) other;
120 
121         return mBrightness == otherState.getBrightness()
122                 && mSdrBrightness == otherState.getSdrBrightness()
123                 && mBrightnessReason.equals(otherState.getBrightnessReason())
124                 && TextUtils.equals(mDisplayBrightnessStrategyName,
125                         otherState.getDisplayBrightnessStrategyName())
126                 && mShouldUseAutoBrightness == otherState.getShouldUseAutoBrightness()
127                 && mIsSlowChange == otherState.isSlowChange();
128     }
129 
130     @Override
hashCode()131     public int hashCode() {
132         return Objects.hash(mBrightness, mSdrBrightness, mBrightnessReason,
133                 mShouldUseAutoBrightness, mIsSlowChange);
134     }
135 
136     /**
137      * Helper methods to create builder
138      */
builder()139     public static Builder builder() {
140         return new Builder();
141     }
142 
143     /**
144      * A DisplayBrightnessState's builder class.
145      */
146     public static class Builder {
147         private float mBrightness;
148         private float mSdrBrightness;
149         private BrightnessReason mBrightnessReason = new BrightnessReason();
150         private String mDisplayBrightnessStrategyName;
151         private boolean mShouldUseAutoBrightness;
152         private boolean mIsSlowChange;
153 
154         /**
155          * Create a builder starting with the values from the specified {@link
156          * DisplayBrightnessState}.
157          *
158          * @param state The state from which to initialize.
159          */
from(DisplayBrightnessState state)160         public static Builder from(DisplayBrightnessState state) {
161             Builder builder = new Builder();
162             builder.setBrightness(state.getBrightness());
163             builder.setSdrBrightness(state.getSdrBrightness());
164             builder.setBrightnessReason(state.getBrightnessReason());
165             builder.setDisplayBrightnessStrategyName(state.getDisplayBrightnessStrategyName());
166             builder.setShouldUseAutoBrightness(state.getShouldUseAutoBrightness());
167             builder.setIsSlowChange(state.isSlowChange());
168             return builder;
169         }
170 
171         /**
172          * Gets the brightness
173          */
getBrightness()174         public float getBrightness() {
175             return mBrightness;
176         }
177 
178         /**
179          * Sets the brightness
180          *
181          * @param brightness The brightness to be associated with DisplayBrightnessState's
182          *                   builder
183          */
setBrightness(float brightness)184         public Builder setBrightness(float brightness) {
185             this.mBrightness = brightness;
186             return this;
187         }
188 
189         /**
190          * Gets the sdr brightness
191          */
getSdrBrightness()192         public float getSdrBrightness() {
193             return mSdrBrightness;
194         }
195 
196         /**
197          * Sets the sdr brightness
198          *
199          * @param sdrBrightness The sdr brightness to be associated with DisplayBrightnessState's
200          *                      builder
201          */
setSdrBrightness(float sdrBrightness)202         public Builder setSdrBrightness(float sdrBrightness) {
203             this.mSdrBrightness = sdrBrightness;
204             return this;
205         }
206 
207         /**
208          * Gets the {@link BrightnessReason}
209          */
getBrightnessReason()210         public BrightnessReason getBrightnessReason() {
211             return mBrightnessReason;
212         }
213 
214         /**
215          * Sets the {@link BrightnessReason}
216          *
217          * @param brightnessReason The brightness reason {@link BrightnessReason} to be
218          *                         associated with the builder
219          */
setBrightnessReason(BrightnessReason brightnessReason)220         public Builder setBrightnessReason(BrightnessReason brightnessReason) {
221             this.mBrightnessReason = brightnessReason;
222             return this;
223         }
224 
225         /**
226          * Gets the {@link com.android.server.display.brightness.strategy.DisplayBrightnessStrategy}
227          * name
228          */
getDisplayBrightnessStrategyName()229         public String getDisplayBrightnessStrategyName() {
230             return mDisplayBrightnessStrategyName;
231         }
232 
233         /**
234          * Sets the
235          * {@link com.android.server.display.brightness.strategy.DisplayBrightnessStrategy}'s name
236          *
237          * @param displayBrightnessStrategyName The name of the
238          * {@link com.android.server.display.brightness.strategy.DisplayBrightnessStrategy} being
239          *                                      used.
240          */
setDisplayBrightnessStrategyName(String displayBrightnessStrategyName)241         public Builder setDisplayBrightnessStrategyName(String displayBrightnessStrategyName) {
242             this.mDisplayBrightnessStrategyName = displayBrightnessStrategyName;
243             return this;
244         }
245 
246         /**
247          * See {@link DisplayBrightnessState#getShouldUseAutoBrightness}.
248          */
setShouldUseAutoBrightness(boolean shouldUseAutoBrightness)249         public Builder setShouldUseAutoBrightness(boolean shouldUseAutoBrightness) {
250             this.mShouldUseAutoBrightness = shouldUseAutoBrightness;
251             return this;
252         }
253 
254         /**
255          * See {@link DisplayBrightnessState#getShouldUseAutoBrightness}.
256          */
getShouldUseAutoBrightness()257         public boolean getShouldUseAutoBrightness() {
258             return mShouldUseAutoBrightness;
259         }
260 
261         /**
262          * See {@link DisplayBrightnessState#isSlowChange()}.
263          */
setIsSlowChange(boolean shouldUseAutoBrightness)264         public Builder setIsSlowChange(boolean shouldUseAutoBrightness) {
265             this.mIsSlowChange = shouldUseAutoBrightness;
266             return this;
267         }
268 
269         /**
270          * See {@link DisplayBrightnessState#isSlowChange()}.
271          */
isSlowChange()272         public boolean isSlowChange() {
273             return mIsSlowChange;
274         }
275 
276         /**
277          * This is used to construct an immutable DisplayBrightnessState object from its builder
278          */
build()279         public DisplayBrightnessState build() {
280             return new DisplayBrightnessState(this);
281         }
282     }
283 }
284