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.BrightnessEvent;
22 import com.android.server.display.brightness.BrightnessReason;
23 
24 import java.util.Objects;
25 
26 /**
27  * A state class representing a set of brightness related entities that are decided at runtime by
28  * the DisplayBrightnessModeStrategies when updating the brightness.
29  */
30 public final class DisplayBrightnessState {
31     public static final float CUSTOM_ANIMATION_RATE_NOT_SET = -1f;
32 
33     private final float mBrightness;
34     private final float mSdrBrightness;
35 
36     private final float mMaxBrightness;
37     private final float mMinBrightness;
38     private final BrightnessReason mBrightnessReason;
39     private final String mDisplayBrightnessStrategyName;
40     private final boolean mShouldUseAutoBrightness;
41 
42     private final boolean mIsSlowChange;
43     private final boolean mShouldUpdateScreenBrightnessSetting;
44 
45     private final float mCustomAnimationRate;
46 
47     private final BrightnessEvent mBrightnessEvent;
48     private final int mBrightnessAdjustmentFlag;
49 
50     private final boolean mIsUserInitiatedChange;
51 
DisplayBrightnessState(Builder builder)52     private DisplayBrightnessState(Builder builder) {
53         mBrightness = builder.getBrightness();
54         mSdrBrightness = builder.getSdrBrightness();
55         mBrightnessReason = builder.getBrightnessReason();
56         mDisplayBrightnessStrategyName = builder.getDisplayBrightnessStrategyName();
57         mShouldUseAutoBrightness = builder.getShouldUseAutoBrightness();
58         mIsSlowChange = builder.isSlowChange();
59         mMaxBrightness = builder.getMaxBrightness();
60         mMinBrightness = builder.getMinBrightness();
61         mCustomAnimationRate = builder.getCustomAnimationRate();
62         mShouldUpdateScreenBrightnessSetting = builder.shouldUpdateScreenBrightnessSetting();
63         mBrightnessEvent = builder.getBrightnessEvent();
64         mBrightnessAdjustmentFlag = builder.getBrightnessAdjustmentFlag();
65         mIsUserInitiatedChange = builder.isUserInitiatedChange();
66     }
67 
68     /**
69      * Gets the brightness
70      */
getBrightness()71     public float getBrightness() {
72         return mBrightness;
73     }
74 
75     /**
76      * Gets the sdr brightness
77      */
getSdrBrightness()78     public float getSdrBrightness() {
79         return mSdrBrightness;
80     }
81 
82     /**
83      * Gets the {@link BrightnessReason}
84      */
getBrightnessReason()85     public BrightnessReason getBrightnessReason() {
86         return mBrightnessReason;
87     }
88 
89     /**
90      * Gets the {@link com.android.server.display.brightness.strategy.DisplayBrightnessStrategy}
91      * name
92      */
getDisplayBrightnessStrategyName()93     public String getDisplayBrightnessStrategyName() {
94         return mDisplayBrightnessStrategyName;
95     }
96 
97     /**
98      * @return {@code true} if the device is set up to run auto-brightness.
99      */
getShouldUseAutoBrightness()100     public boolean getShouldUseAutoBrightness() {
101         return mShouldUseAutoBrightness;
102     }
103 
104     /**
105      * @return {@code true} if the should transit to new state slowly
106      */
isSlowChange()107     public boolean isSlowChange() {
108         return mIsSlowChange;
109     }
110 
111     /**
112      * @return maximum allowed brightness
113      */
getMaxBrightness()114     public float getMaxBrightness() {
115         return mMaxBrightness;
116     }
117 
118     /**
119      * @return minimum allowed brightness
120      */
getMinBrightness()121     public float getMinBrightness() {
122         return mMinBrightness;
123     }
124 
125     /**
126      * @return custom animation rate
127      */
getCustomAnimationRate()128     public float getCustomAnimationRate() {
129         return mCustomAnimationRate;
130     }
131 
132     /**
133      * @return {@code true} if the screen brightness setting should be updated
134      */
shouldUpdateScreenBrightnessSetting()135     public boolean shouldUpdateScreenBrightnessSetting() {
136         return mShouldUpdateScreenBrightnessSetting;
137     }
138 
139     /**
140      * @return The BrightnessEvent object
141      */
getBrightnessEvent()142     public BrightnessEvent getBrightnessEvent() {
143         return mBrightnessEvent;
144     }
145 
146     /**
147      * Gets the flag representing the reason for the brightness adjustment. This can be
148      * automatic(e.g. because of the change in the lux), or user initiated(e.g. moving the slider)
149      */
getBrightnessAdjustmentFlag()150     public int getBrightnessAdjustmentFlag() {
151         return mBrightnessAdjustmentFlag;
152     }
153 
154     /**
155      * Gets if the current brightness changes are because of a user initiated change
156      */
isUserInitiatedChange()157     public boolean isUserInitiatedChange() {
158         return mIsUserInitiatedChange;
159     }
160 
161     @Override
toString()162     public String toString() {
163         StringBuilder stringBuilder = new StringBuilder("DisplayBrightnessState:");
164         stringBuilder.append("\n    brightness:");
165         stringBuilder.append(getBrightness());
166         stringBuilder.append("\n    sdrBrightness:");
167         stringBuilder.append(getSdrBrightness());
168         stringBuilder.append("\n    brightnessReason:");
169         stringBuilder.append(getBrightnessReason());
170         stringBuilder.append("\n    shouldUseAutoBrightness:");
171         stringBuilder.append(getShouldUseAutoBrightness());
172         stringBuilder.append("\n    isSlowChange:").append(mIsSlowChange);
173         stringBuilder.append("\n    maxBrightness:").append(mMaxBrightness);
174         stringBuilder.append("\n    minBrightness:").append(mMinBrightness);
175         stringBuilder.append("\n    customAnimationRate:").append(mCustomAnimationRate);
176         stringBuilder.append("\n    shouldUpdateScreenBrightnessSetting:")
177                 .append(mShouldUpdateScreenBrightnessSetting);
178         stringBuilder.append("\n    mBrightnessEvent:")
179                 .append(Objects.toString(mBrightnessEvent, "null"));
180         stringBuilder.append("\n    mBrightnessAdjustmentFlag:").append(mBrightnessAdjustmentFlag);
181         stringBuilder.append("\n    mIsUserInitiatedChange:").append(mIsUserInitiatedChange);
182         return stringBuilder.toString();
183     }
184 
185     /**
186      * Checks whether the two objects have the same values.
187      */
188     @Override
equals(Object other)189     public boolean equals(Object other) {
190         if (other == this) {
191             return true;
192         }
193 
194         if (!(other instanceof DisplayBrightnessState)) {
195             return false;
196         }
197 
198         DisplayBrightnessState otherState = (DisplayBrightnessState) other;
199 
200         return mBrightness == otherState.getBrightness()
201                 && mSdrBrightness == otherState.getSdrBrightness()
202                 && mBrightnessReason.equals(otherState.getBrightnessReason())
203                 && TextUtils.equals(mDisplayBrightnessStrategyName,
204                         otherState.getDisplayBrightnessStrategyName())
205                 && mShouldUseAutoBrightness == otherState.getShouldUseAutoBrightness()
206                 && mIsSlowChange == otherState.isSlowChange()
207                 && mMaxBrightness == otherState.getMaxBrightness()
208                 && mMinBrightness == otherState.getMinBrightness()
209                 && mCustomAnimationRate == otherState.getCustomAnimationRate()
210                 && mShouldUpdateScreenBrightnessSetting
211                     == otherState.shouldUpdateScreenBrightnessSetting()
212                 && Objects.equals(mBrightnessEvent, otherState.getBrightnessEvent())
213                 && mBrightnessAdjustmentFlag == otherState.getBrightnessAdjustmentFlag()
214                 && mIsUserInitiatedChange == otherState.isUserInitiatedChange();
215     }
216 
217     @Override
hashCode()218     public int hashCode() {
219         return Objects.hash(mBrightness, mSdrBrightness, mBrightnessReason,
220                 mShouldUseAutoBrightness, mIsSlowChange, mMaxBrightness, mMinBrightness,
221                 mCustomAnimationRate,
222                 mShouldUpdateScreenBrightnessSetting, mBrightnessEvent, mBrightnessAdjustmentFlag,
223                 mIsUserInitiatedChange);
224     }
225 
226     /**
227      * Helper methods to create builder
228      */
builder()229     public static Builder builder() {
230         return new Builder();
231     }
232 
233     /**
234      * A DisplayBrightnessState's builder class.
235      */
236     public static class Builder {
237         private float mBrightness;
238         private float mSdrBrightness;
239         private BrightnessReason mBrightnessReason = new BrightnessReason();
240         private String mDisplayBrightnessStrategyName;
241         private boolean mShouldUseAutoBrightness;
242         private boolean mIsSlowChange;
243         private float mMaxBrightness;
244         private float mMinBrightness;
245         private float mCustomAnimationRate = CUSTOM_ANIMATION_RATE_NOT_SET;
246         private boolean mShouldUpdateScreenBrightnessSetting;
247 
248         private BrightnessEvent mBrightnessEvent;
249 
250         public int mBrightnessAdjustmentFlag = 0;
251 
252         private boolean mIsUserInitiatedChange;
253 
254         /**
255          * Create a builder starting with the values from the specified {@link
256          * DisplayBrightnessState}.
257          *
258          * @param state The state from which to initialize.
259          */
from(DisplayBrightnessState state)260         public static Builder from(DisplayBrightnessState state) {
261             Builder builder = new Builder();
262             builder.setBrightness(state.getBrightness());
263             builder.setSdrBrightness(state.getSdrBrightness());
264             builder.setBrightnessReason(state.getBrightnessReason());
265             builder.setDisplayBrightnessStrategyName(state.getDisplayBrightnessStrategyName());
266             builder.setShouldUseAutoBrightness(state.getShouldUseAutoBrightness());
267             builder.setIsSlowChange(state.isSlowChange());
268             builder.setMaxBrightness(state.getMaxBrightness());
269             builder.setMinBrightness(state.getMinBrightness());
270             builder.setCustomAnimationRate(state.getCustomAnimationRate());
271             builder.setShouldUpdateScreenBrightnessSetting(
272                     state.shouldUpdateScreenBrightnessSetting());
273             builder.setBrightnessEvent(state.getBrightnessEvent());
274             builder.setBrightnessAdjustmentFlag(state.getBrightnessAdjustmentFlag());
275             builder.setIsUserInitiatedChange(state.isUserInitiatedChange());
276             return builder;
277         }
278 
279         /**
280          * Gets the brightness
281          */
getBrightness()282         public float getBrightness() {
283             return mBrightness;
284         }
285 
286         /**
287          * Sets the brightness
288          *
289          * @param brightness The brightness to be associated with DisplayBrightnessState's
290          *                   builder
291          */
setBrightness(float brightness)292         public Builder setBrightness(float brightness) {
293             this.mBrightness = brightness;
294             return this;
295         }
296 
297         /**
298          * Gets the sdr brightness
299          */
getSdrBrightness()300         public float getSdrBrightness() {
301             return mSdrBrightness;
302         }
303 
304         /**
305          * Sets the sdr brightness
306          *
307          * @param sdrBrightness The sdr brightness to be associated with DisplayBrightnessState's
308          *                      builder
309          */
setSdrBrightness(float sdrBrightness)310         public Builder setSdrBrightness(float sdrBrightness) {
311             this.mSdrBrightness = sdrBrightness;
312             return this;
313         }
314 
315         /**
316          * Gets the {@link BrightnessReason}
317          */
getBrightnessReason()318         public BrightnessReason getBrightnessReason() {
319             return mBrightnessReason;
320         }
321 
322         /**
323          * Sets the {@link BrightnessReason}
324          *
325          * @param brightnessReason The brightness reason {@link BrightnessReason} to be
326          *                         associated with the builder
327          */
setBrightnessReason(BrightnessReason brightnessReason)328         public Builder setBrightnessReason(BrightnessReason brightnessReason) {
329             this.mBrightnessReason = brightnessReason;
330             return this;
331         }
332 
333         /**
334          * Gets the {@link com.android.server.display.brightness.strategy.DisplayBrightnessStrategy}
335          * name
336          */
getDisplayBrightnessStrategyName()337         public String getDisplayBrightnessStrategyName() {
338             return mDisplayBrightnessStrategyName;
339         }
340 
341         /**
342          * Sets the
343          * {@link com.android.server.display.brightness.strategy.DisplayBrightnessStrategy}'s name
344          *
345          * @param displayBrightnessStrategyName The name of the
346          * {@link com.android.server.display.brightness.strategy.DisplayBrightnessStrategy} being
347          *                                      used.
348          */
setDisplayBrightnessStrategyName(String displayBrightnessStrategyName)349         public Builder setDisplayBrightnessStrategyName(String displayBrightnessStrategyName) {
350             this.mDisplayBrightnessStrategyName = displayBrightnessStrategyName;
351             return this;
352         }
353 
354         /**
355          * See {@link DisplayBrightnessState#getShouldUseAutoBrightness}.
356          */
setShouldUseAutoBrightness(boolean shouldUseAutoBrightness)357         public Builder setShouldUseAutoBrightness(boolean shouldUseAutoBrightness) {
358             this.mShouldUseAutoBrightness = shouldUseAutoBrightness;
359             return this;
360         }
361 
362         /**
363          * See {@link DisplayBrightnessState#getShouldUseAutoBrightness}.
364          */
getShouldUseAutoBrightness()365         public boolean getShouldUseAutoBrightness() {
366             return mShouldUseAutoBrightness;
367         }
368 
369         /**
370          * See {@link DisplayBrightnessState#isSlowChange()}.
371          */
setIsSlowChange(boolean isSlowChange)372         public Builder setIsSlowChange(boolean isSlowChange) {
373             this.mIsSlowChange = isSlowChange;
374             return this;
375         }
376 
377         /**
378          * See {@link DisplayBrightnessState#isSlowChange()}.
379          */
isSlowChange()380         public boolean isSlowChange() {
381             return mIsSlowChange;
382         }
383 
384         /**
385          * See {@link DisplayBrightnessState#getMaxBrightness()}.
386          */
setMaxBrightness(float maxBrightness)387         public Builder setMaxBrightness(float maxBrightness) {
388             this.mMaxBrightness = maxBrightness;
389             return this;
390         }
391 
392         /**
393          * See {@link DisplayBrightnessState#getMaxBrightness()}.
394          */
getMaxBrightness()395         public float getMaxBrightness() {
396             return mMaxBrightness;
397         }
398 
399         /**
400          * See {@link DisplayBrightnessState#getMinBrightness()}.
401          */
setMinBrightness(float minBrightness)402         public Builder setMinBrightness(float minBrightness) {
403             this.mMinBrightness = minBrightness;
404             return this;
405         }
406 
407         /**
408          * See {@link DisplayBrightnessState#getMinBrightness()}.
409          */
getMinBrightness()410         public float getMinBrightness() {
411             return mMinBrightness;
412         }
413 
414         /**
415          * See {@link DisplayBrightnessState#getCustomAnimationRate()}.
416          */
setCustomAnimationRate(float animationRate)417         public Builder setCustomAnimationRate(float animationRate) {
418             this.mCustomAnimationRate = animationRate;
419             return this;
420         }
421 
422         /**
423          * See {@link DisplayBrightnessState#getCustomAnimationRate()}.
424          */
getCustomAnimationRate()425         public float getCustomAnimationRate() {
426             return mCustomAnimationRate;
427         }
428 
429         /**
430          * See {@link DisplayBrightnessState#shouldUpdateScreenBrightnessSetting()}.
431          */
shouldUpdateScreenBrightnessSetting()432         public boolean shouldUpdateScreenBrightnessSetting() {
433             return mShouldUpdateScreenBrightnessSetting;
434         }
435 
436         /**
437          * See {@link DisplayBrightnessState#shouldUpdateScreenBrightnessSetting()}.
438          */
setShouldUpdateScreenBrightnessSetting( boolean shouldUpdateScreenBrightnessSetting)439         public Builder setShouldUpdateScreenBrightnessSetting(
440                 boolean shouldUpdateScreenBrightnessSetting) {
441             mShouldUpdateScreenBrightnessSetting = shouldUpdateScreenBrightnessSetting;
442             return this;
443         }
444 
445         /**
446          * This is used to construct an immutable DisplayBrightnessState object from its builder
447          */
build()448         public DisplayBrightnessState build() {
449             return new DisplayBrightnessState(this);
450         }
451 
452         /**
453          * This is used to get the BrightnessEvent object from its builder
454          */
getBrightnessEvent()455         public BrightnessEvent getBrightnessEvent() {
456             return mBrightnessEvent;
457         }
458 
459 
460         /**
461          * This is used to set the BrightnessEvent object
462          */
setBrightnessEvent(BrightnessEvent brightnessEvent)463         public Builder setBrightnessEvent(BrightnessEvent brightnessEvent) {
464             mBrightnessEvent = brightnessEvent;
465             return this;
466         }
467 
468         /**
469          * This is used to get the brightness adjustment flag from its builder
470          */
getBrightnessAdjustmentFlag()471         public int getBrightnessAdjustmentFlag() {
472             return mBrightnessAdjustmentFlag;
473         }
474 
475 
476         /**
477          * This is used to set the brightness adjustment flag
478          */
setBrightnessAdjustmentFlag(int brightnessAdjustmentFlag)479         public Builder setBrightnessAdjustmentFlag(int brightnessAdjustmentFlag) {
480             mBrightnessAdjustmentFlag = brightnessAdjustmentFlag;
481             return this;
482         }
483 
484         /**
485          * Gets if the current change is a user initiated change
486          */
isUserInitiatedChange()487         public boolean isUserInitiatedChange() {
488             return mIsUserInitiatedChange;
489         }
490 
491         /**
492          * This is used to set if the current change is a user initiated change
493          */
setIsUserInitiatedChange(boolean isUserInitiatedChange)494         public Builder setIsUserInitiatedChange(boolean isUserInitiatedChange) {
495             mIsUserInitiatedChange = isUserInitiatedChange;
496             return this;
497         }
498     }
499 }
500