1 /*
2  * Copyright (C) 2021 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 android.hardware.display;
18 
19 import android.annotation.IntDef;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import java.lang.annotation.Retention;
24 import java.lang.annotation.RetentionPolicy;
25 
26 /**
27  * Data about the current brightness state.
28  * {@see android.view.Display.getBrightnessInfo()}
29  *
30  * @hide
31  */
32 public final class BrightnessInfo implements Parcelable {
33 
34     @IntDef(prefix = {"HIGH_BRIGHTNESS_MODE_"}, value = {
35             HIGH_BRIGHTNESS_MODE_OFF,
36             HIGH_BRIGHTNESS_MODE_SUNLIGHT,
37             HIGH_BRIGHTNESS_MODE_HDR
38     })
39     @Retention(RetentionPolicy.SOURCE)
40     public @interface HighBrightnessMode {}
41 
42     /**
43      * High brightness mode is OFF. The high brightness range is not currently accessible to the
44      * user.
45      */
46     public static final int HIGH_BRIGHTNESS_MODE_OFF = 0;
47 
48     /**
49      * High brightness mode is ON due to high ambient light (sunlight). The high brightness range is
50      * currently accessible to the user.
51      */
52     public static final int HIGH_BRIGHTNESS_MODE_SUNLIGHT = 1;
53 
54     /**
55      * High brightness mode is ON due to high ambient light (sunlight). The high brightness range is
56      * currently accessible to the user.
57      */
58     public static final int HIGH_BRIGHTNESS_MODE_HDR = 2;
59 
60     @IntDef(prefix = {"BRIGHTNESS_MAX_REASON_"}, value = {
61             BRIGHTNESS_MAX_REASON_NONE,
62             BRIGHTNESS_MAX_REASON_THERMAL
63     })
64     @Retention(RetentionPolicy.SOURCE)
65     public @interface BrightnessMaxReason {}
66 
67     /**
68      * Maximum brightness is unrestricted.
69      */
70     public static final int BRIGHTNESS_MAX_REASON_NONE = 0;
71 
72     /**
73      * Maximum brightness is restricted due to thermal throttling.
74      */
75     public static final int BRIGHTNESS_MAX_REASON_THERMAL = 1;
76 
77     /** Brightness */
78     public final float brightness;
79 
80     /** Brightness after {@link DisplayPowerController} adjustments */
81     public final float adjustedBrightness;
82 
83     /** Current minimum supported brightness. */
84     public final float brightnessMinimum;
85 
86     /** Current maximum supported brightness. */
87     public final float brightnessMaximum;
88 
89     /** Brightness values greater than this point are only used in High Brightness Mode. */
90     public final float highBrightnessTransitionPoint;
91 
92     /**
93      * Current state of high brightness mode.
94      * Can be any of HIGH_BRIGHTNESS_MODE_* values.
95      */
96     public final int highBrightnessMode;
97 
98     /**
99      * The current reason for restricting maximum brightness.
100      * Can be any of BRIGHTNESS_MAX_REASON_* values.
101      */
102     public final int brightnessMaxReason;
103 
BrightnessInfo(float brightness, float brightnessMinimum, float brightnessMaximum, @HighBrightnessMode int highBrightnessMode, float highBrightnessTransitionPoint, @BrightnessMaxReason int brightnessMaxReason)104     public BrightnessInfo(float brightness, float brightnessMinimum, float brightnessMaximum,
105             @HighBrightnessMode int highBrightnessMode, float highBrightnessTransitionPoint,
106             @BrightnessMaxReason int brightnessMaxReason) {
107         this(brightness, brightness, brightnessMinimum, brightnessMaximum, highBrightnessMode,
108                 highBrightnessTransitionPoint, brightnessMaxReason);
109     }
110 
BrightnessInfo(float brightness, float adjustedBrightness, float brightnessMinimum, float brightnessMaximum, @HighBrightnessMode int highBrightnessMode, float highBrightnessTransitionPoint, @BrightnessMaxReason int brightnessMaxReason)111     public BrightnessInfo(float brightness, float adjustedBrightness, float brightnessMinimum,
112             float brightnessMaximum, @HighBrightnessMode int highBrightnessMode,
113             float highBrightnessTransitionPoint, @BrightnessMaxReason int brightnessMaxReason) {
114         this.brightness = brightness;
115         this.adjustedBrightness = adjustedBrightness;
116         this.brightnessMinimum = brightnessMinimum;
117         this.brightnessMaximum = brightnessMaximum;
118         this.highBrightnessMode = highBrightnessMode;
119         this.highBrightnessTransitionPoint = highBrightnessTransitionPoint;
120         this.brightnessMaxReason =  brightnessMaxReason;
121     }
122 
123     /**
124      * @return User-friendly string for specified {@link HighBrightnessMode} parameter.
125      */
hbmToString(@ighBrightnessMode int highBrightnessMode)126     public static String hbmToString(@HighBrightnessMode int highBrightnessMode) {
127         switch (highBrightnessMode) {
128             case HIGH_BRIGHTNESS_MODE_OFF:
129                 return "off";
130             case HIGH_BRIGHTNESS_MODE_HDR:
131                 return "hdr";
132             case HIGH_BRIGHTNESS_MODE_SUNLIGHT:
133                 return "sunlight";
134         }
135         return "invalid";
136     }
137 
138     /**
139      * @return User-friendly string for specified {@link BrightnessMaxReason} parameter.
140      */
briMaxReasonToString(@rightnessMaxReason int reason)141     public static String briMaxReasonToString(@BrightnessMaxReason int reason) {
142         switch (reason) {
143             case BRIGHTNESS_MAX_REASON_NONE:
144                 return "none";
145             case BRIGHTNESS_MAX_REASON_THERMAL:
146                 return "thermal";
147         }
148         return "invalid";
149     }
150 
151     @Override
describeContents()152     public int describeContents() {
153         return 0;
154     }
155 
156     @Override
writeToParcel(Parcel dest, int flags)157     public void writeToParcel(Parcel dest, int flags) {
158         dest.writeFloat(brightness);
159         dest.writeFloat(adjustedBrightness);
160         dest.writeFloat(brightnessMinimum);
161         dest.writeFloat(brightnessMaximum);
162         dest.writeInt(highBrightnessMode);
163         dest.writeFloat(highBrightnessTransitionPoint);
164         dest.writeInt(brightnessMaxReason);
165     }
166 
167     public static final @android.annotation.NonNull Creator<BrightnessInfo> CREATOR =
168             new Creator<BrightnessInfo>() {
169                 @Override
170                 public BrightnessInfo createFromParcel(Parcel source) {
171                     return new BrightnessInfo(source);
172                 }
173 
174                 @Override
175                 public BrightnessInfo[] newArray(int size) {
176                     return new BrightnessInfo[size];
177                 }
178             };
179 
BrightnessInfo(Parcel source)180     private BrightnessInfo(Parcel source) {
181         brightness = source.readFloat();
182         adjustedBrightness = source.readFloat();
183         brightnessMinimum = source.readFloat();
184         brightnessMaximum = source.readFloat();
185         highBrightnessMode = source.readInt();
186         highBrightnessTransitionPoint = source.readFloat();
187         brightnessMaxReason = source.readInt();
188     }
189 
190 }
191