1 /*
2  * Copyright 2017 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.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.annotation.TestApi;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import java.util.Objects;
27 
28 /**
29  * Data about a brightness settings change.
30  *
31  * {@see DisplayManager.getBrightnessEvents()}
32  * @hide
33  */
34 @SystemApi
35 @TestApi
36 public final class BrightnessChangeEvent implements Parcelable {
37     /** Brightness in nits */
38     public final float brightness;
39 
40     /** Timestamp of the change {@see System.currentTimeMillis()} */
41     public final long timeStamp;
42 
43     /** Package name of focused activity when brightness was changed.
44      *  This will be null if the caller of {@see DisplayManager.getBrightnessEvents()}
45      *  does not have access to usage stats {@see UsageStatsManager} */
46     public final String packageName;
47 
48     /** User id of of the user running when brightness was changed.
49      * @hide */
50     public final int userId;
51 
52     /** Lux values of recent sensor data */
53     public final float[] luxValues;
54 
55     /** Timestamps of the lux sensor readings {@see System.currentTimeMillis()} */
56     public final long[] luxTimestamps;
57 
58     /** Most recent battery level when brightness was changed or Float.NaN */
59     public final float batteryLevel;
60 
61     /** Factor applied to brightness due to battery level, 0.0-1.0 */
62     public final float powerBrightnessFactor;
63 
64     /** Color filter active to provide night mode */
65     public final boolean nightMode;
66 
67     /** If night mode color filter is active this will be the temperature in kelvin */
68     public final int colorTemperature;
69 
70     /** Brightness level before slider adjustment */
71     public final float lastBrightness;
72 
73     /** Whether brightness configuration is default version */
74     public final boolean isDefaultBrightnessConfig;
75 
76     /** Whether brightness curve includes a user brightness point */
77     public final boolean isUserSetBrightness;
78 
79     /**
80      * Histogram counting how many times a pixel of a given value was displayed onscreen for the
81      * Value component of HSV if the device supports color sampling, if the device does not support
82      * color sampling or {@link BrightnessConfiguration#shouldCollectColorSamples()} is false the
83      * value will be null.
84      *
85      * The buckets of the histogram are evenly weighted, the number of buckets is device specific.
86      * The units are in pixels * milliseconds, with 1 pixel millisecond being 1 pixel displayed
87      * for 1 millisecond.
88      * For example if we had {100, 50, 30, 20}, value component was onscreen for 100 pixel
89      * milliseconds in range 0x00->0x3F, 30 pixel milliseconds in range 0x40->0x7F, etc.
90      *
91      * {@see #colorSampleDuration}
92      */
93     @Nullable
94     public final long[] colorValueBuckets;
95 
96     /**
97      * How many milliseconds of data are contained in the colorValueBuckets, if the device does
98      * not support color sampling or {@link BrightnessConfiguration#shouldCollectColorSamples()} is
99      * false the value will be 0L.
100      *
101      * {@see #colorValueBuckets}
102      */
103     public final long colorSampleDuration;
104 
105 
106     /** @hide */
BrightnessChangeEvent(float brightness, long timeStamp, String packageName, int userId, float[] luxValues, long[] luxTimestamps, float batteryLevel, float powerBrightnessFactor, boolean nightMode, int colorTemperature, float lastBrightness, boolean isDefaultBrightnessConfig, boolean isUserSetBrightness, long[] colorValueBuckets, long colorSampleDuration)107     private BrightnessChangeEvent(float brightness, long timeStamp, String packageName,
108             int userId, float[] luxValues, long[] luxTimestamps, float batteryLevel,
109             float powerBrightnessFactor, boolean nightMode, int colorTemperature,
110             float lastBrightness, boolean isDefaultBrightnessConfig, boolean isUserSetBrightness,
111             long[] colorValueBuckets, long colorSampleDuration) {
112         this.brightness = brightness;
113         this.timeStamp = timeStamp;
114         this.packageName = packageName;
115         this.userId = userId;
116         this.luxValues = luxValues;
117         this.luxTimestamps = luxTimestamps;
118         this.batteryLevel = batteryLevel;
119         this.powerBrightnessFactor = powerBrightnessFactor;
120         this.nightMode = nightMode;
121         this.colorTemperature = colorTemperature;
122         this.lastBrightness = lastBrightness;
123         this.isDefaultBrightnessConfig = isDefaultBrightnessConfig;
124         this.isUserSetBrightness = isUserSetBrightness;
125         this.colorValueBuckets = colorValueBuckets;
126         this.colorSampleDuration = colorSampleDuration;
127     }
128 
129     /** @hide */
BrightnessChangeEvent(BrightnessChangeEvent other, boolean redactPackage)130     public BrightnessChangeEvent(BrightnessChangeEvent other, boolean redactPackage) {
131         this.brightness = other.brightness;
132         this.timeStamp = other.timeStamp;
133         this.packageName = redactPackage ? null : other.packageName;
134         this.userId = other.userId;
135         this.luxValues = other.luxValues;
136         this.luxTimestamps = other.luxTimestamps;
137         this.batteryLevel = other.batteryLevel;
138         this.powerBrightnessFactor = other.powerBrightnessFactor;
139         this.nightMode = other.nightMode;
140         this.colorTemperature = other.colorTemperature;
141         this.lastBrightness = other.lastBrightness;
142         this.isDefaultBrightnessConfig = other.isDefaultBrightnessConfig;
143         this.isUserSetBrightness = other.isUserSetBrightness;
144         this.colorValueBuckets = other.colorValueBuckets;
145         this.colorSampleDuration = other.colorSampleDuration;
146     }
147 
BrightnessChangeEvent(Parcel source)148     private BrightnessChangeEvent(Parcel source) {
149         brightness = source.readFloat();
150         timeStamp = source.readLong();
151         packageName = source.readString();
152         userId = source.readInt();
153         luxValues = source.createFloatArray();
154         luxTimestamps = source.createLongArray();
155         batteryLevel = source.readFloat();
156         powerBrightnessFactor = source.readFloat();
157         nightMode = source.readBoolean();
158         colorTemperature = source.readInt();
159         lastBrightness = source.readFloat();
160         isDefaultBrightnessConfig = source.readBoolean();
161         isUserSetBrightness = source.readBoolean();
162         colorValueBuckets = source.createLongArray();
163         colorSampleDuration = source.readLong();
164     }
165 
166     public static final @android.annotation.NonNull Creator<BrightnessChangeEvent> CREATOR =
167             new Creator<BrightnessChangeEvent>() {
168                 public BrightnessChangeEvent createFromParcel(Parcel source) {
169                     return new BrightnessChangeEvent(source);
170                 }
171                 public BrightnessChangeEvent[] newArray(int size) {
172                     return new BrightnessChangeEvent[size];
173                 }
174             };
175 
176     @Override
describeContents()177     public int describeContents() {
178         return 0;
179     }
180 
181     @Override
writeToParcel(Parcel dest, int flags)182     public void writeToParcel(Parcel dest, int flags) {
183         dest.writeFloat(brightness);
184         dest.writeLong(timeStamp);
185         dest.writeString(packageName);
186         dest.writeInt(userId);
187         dest.writeFloatArray(luxValues);
188         dest.writeLongArray(luxTimestamps);
189         dest.writeFloat(batteryLevel);
190         dest.writeFloat(powerBrightnessFactor);
191         dest.writeBoolean(nightMode);
192         dest.writeInt(colorTemperature);
193         dest.writeFloat(lastBrightness);
194         dest.writeBoolean(isDefaultBrightnessConfig);
195         dest.writeBoolean(isUserSetBrightness);
196         dest.writeLongArray(colorValueBuckets);
197         dest.writeLong(colorSampleDuration);
198     }
199 
200     /** @hide */
201     public static class Builder {
202         private float mBrightness;
203         private long mTimeStamp;
204         private String mPackageName;
205         private int mUserId;
206         private float[] mLuxValues;
207         private long[] mLuxTimestamps;
208         private float mBatteryLevel;
209         private float mPowerBrightnessFactor;
210         private boolean mNightMode;
211         private int mColorTemperature;
212         private float mLastBrightness;
213         private boolean mIsDefaultBrightnessConfig;
214         private boolean mIsUserSetBrightness;
215         private long[] mColorValueBuckets;
216         private long mColorSampleDuration;
217 
218         /** {@see BrightnessChangeEvent#brightness} */
setBrightness(float brightness)219         public Builder setBrightness(float brightness) {
220             mBrightness = brightness;
221             return this;
222         }
223 
224         /** {@see BrightnessChangeEvent#timeStamp} */
setTimeStamp(long timeStamp)225         public Builder setTimeStamp(long timeStamp) {
226             mTimeStamp = timeStamp;
227             return this;
228         }
229 
230         /** {@see BrightnessChangeEvent#packageName} */
setPackageName(String packageName)231         public Builder setPackageName(String packageName) {
232             mPackageName = packageName;
233             return this;
234         }
235 
236         /** {@see BrightnessChangeEvent#userId} */
setUserId(int userId)237         public Builder setUserId(int userId) {
238             mUserId = userId;
239             return this;
240         }
241 
242         /** {@see BrightnessChangeEvent#luxValues} */
setLuxValues(float[] luxValues)243         public Builder setLuxValues(float[] luxValues) {
244             mLuxValues = luxValues;
245             return this;
246         }
247 
248         /** {@see BrightnessChangeEvent#luxTimestamps} */
setLuxTimestamps(long[] luxTimestamps)249         public Builder setLuxTimestamps(long[] luxTimestamps) {
250             mLuxTimestamps = luxTimestamps;
251             return this;
252         }
253 
254         /** {@see BrightnessChangeEvent#batteryLevel} */
setBatteryLevel(float batteryLevel)255         public Builder setBatteryLevel(float batteryLevel) {
256             mBatteryLevel = batteryLevel;
257             return this;
258         }
259 
260         /** {@see BrightnessChangeEvent#powerSaveBrightness} */
setPowerBrightnessFactor(float powerBrightnessFactor)261         public Builder setPowerBrightnessFactor(float powerBrightnessFactor) {
262             mPowerBrightnessFactor = powerBrightnessFactor;
263             return this;
264         }
265 
266         /** {@see BrightnessChangeEvent#nightMode} */
setNightMode(boolean nightMode)267         public Builder setNightMode(boolean nightMode) {
268             mNightMode = nightMode;
269             return this;
270         }
271 
272         /** {@see BrightnessChangeEvent#colorTemperature} */
setColorTemperature(int colorTemperature)273         public Builder setColorTemperature(int colorTemperature) {
274             mColorTemperature = colorTemperature;
275             return this;
276         }
277 
278         /** {@see BrightnessChangeEvent#lastBrightness} */
setLastBrightness(float lastBrightness)279         public Builder setLastBrightness(float lastBrightness) {
280             mLastBrightness = lastBrightness;
281             return this;
282         }
283 
284         /** {@see BrightnessChangeEvent#isDefaultBrightnessConfig} */
setIsDefaultBrightnessConfig(boolean isDefaultBrightnessConfig)285         public Builder setIsDefaultBrightnessConfig(boolean isDefaultBrightnessConfig) {
286             mIsDefaultBrightnessConfig = isDefaultBrightnessConfig;
287             return this;
288         }
289 
290         /** {@see BrightnessChangeEvent#userBrightnessPoint} */
setUserBrightnessPoint(boolean isUserSetBrightness)291         public Builder setUserBrightnessPoint(boolean isUserSetBrightness) {
292             mIsUserSetBrightness = isUserSetBrightness;
293             return this;
294         }
295 
296         /** {@see BrightnessChangeEvent#colorValueBuckets}
297          *  {@see BrightnessChangeEvent#colorSampleDuration} */
setColorValues(@onNull long[] colorValueBuckets, long colorSampleDuration)298         public Builder setColorValues(@NonNull long[] colorValueBuckets, long colorSampleDuration) {
299             Objects.requireNonNull(colorValueBuckets);
300             mColorValueBuckets = colorValueBuckets;
301             mColorSampleDuration = colorSampleDuration;
302             return this;
303         }
304 
305         /** Builds a BrightnessChangeEvent */
build()306         public BrightnessChangeEvent build() {
307             return new BrightnessChangeEvent(mBrightness, mTimeStamp,
308                     mPackageName, mUserId, mLuxValues, mLuxTimestamps, mBatteryLevel,
309                     mPowerBrightnessFactor, mNightMode, mColorTemperature, mLastBrightness,
310                     mIsDefaultBrightnessConfig, mIsUserSetBrightness, mColorValueBuckets,
311                     mColorSampleDuration);
312         }
313     }
314 }
315