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