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