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 the value will be null.
83      *
84      * The buckets of the histogram are evenly weighted, the number of buckets is device specific.
85      * The units are in pixels * milliseconds, with 1 pixel millisecond being 1 pixel displayed
86      * for 1 millisecond.
87      * For example if we had {100, 50, 30, 20}, value component was onscreen for 100 pixel
88      * milliseconds in range 0x00->0x3F, 30 pixel milliseconds in range 0x40->0x7F, etc.
89      *
90      * {@see #colorSampleDuration}
91      */
92     @Nullable
93     public final long[] colorValueBuckets;
94 
95     /**
96      * How many milliseconds of data are contained in the colorValueBuckets, if the device does
97      * not support color sampling the value will be 0L.
98      *
99      * {@see #colorValueBuckets}
100      */
101     public final long colorSampleDuration;
102 
103 
104     /** @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)105     private BrightnessChangeEvent(float brightness, long timeStamp, String packageName,
106             int userId, float[] luxValues, long[] luxTimestamps, float batteryLevel,
107             float powerBrightnessFactor, boolean nightMode, int colorTemperature,
108             float lastBrightness, boolean isDefaultBrightnessConfig, boolean isUserSetBrightness,
109             long[] colorValueBuckets, long colorSampleDuration) {
110         this.brightness = brightness;
111         this.timeStamp = timeStamp;
112         this.packageName = packageName;
113         this.userId = userId;
114         this.luxValues = luxValues;
115         this.luxTimestamps = luxTimestamps;
116         this.batteryLevel = batteryLevel;
117         this.powerBrightnessFactor = powerBrightnessFactor;
118         this.nightMode = nightMode;
119         this.colorTemperature = colorTemperature;
120         this.lastBrightness = lastBrightness;
121         this.isDefaultBrightnessConfig = isDefaultBrightnessConfig;
122         this.isUserSetBrightness = isUserSetBrightness;
123         this.colorValueBuckets = colorValueBuckets;
124         this.colorSampleDuration = colorSampleDuration;
125     }
126 
127     /** @hide */
BrightnessChangeEvent(BrightnessChangeEvent other, boolean redactPackage)128     public BrightnessChangeEvent(BrightnessChangeEvent other, boolean redactPackage) {
129         this.brightness = other.brightness;
130         this.timeStamp = other.timeStamp;
131         this.packageName = redactPackage ? null : other.packageName;
132         this.userId = other.userId;
133         this.luxValues = other.luxValues;
134         this.luxTimestamps = other.luxTimestamps;
135         this.batteryLevel = other.batteryLevel;
136         this.powerBrightnessFactor = other.powerBrightnessFactor;
137         this.nightMode = other.nightMode;
138         this.colorTemperature = other.colorTemperature;
139         this.lastBrightness = other.lastBrightness;
140         this.isDefaultBrightnessConfig = other.isDefaultBrightnessConfig;
141         this.isUserSetBrightness = other.isUserSetBrightness;
142         this.colorValueBuckets = other.colorValueBuckets;
143         this.colorSampleDuration = other.colorSampleDuration;
144     }
145 
BrightnessChangeEvent(Parcel source)146     private BrightnessChangeEvent(Parcel source) {
147         brightness = source.readFloat();
148         timeStamp = source.readLong();
149         packageName = source.readString();
150         userId = source.readInt();
151         luxValues = source.createFloatArray();
152         luxTimestamps = source.createLongArray();
153         batteryLevel = source.readFloat();
154         powerBrightnessFactor = source.readFloat();
155         nightMode = source.readBoolean();
156         colorTemperature = source.readInt();
157         lastBrightness = source.readFloat();
158         isDefaultBrightnessConfig = source.readBoolean();
159         isUserSetBrightness = source.readBoolean();
160         colorValueBuckets = source.createLongArray();
161         colorSampleDuration = source.readLong();
162     }
163 
164     public static final @android.annotation.NonNull Creator<BrightnessChangeEvent> CREATOR =
165             new Creator<BrightnessChangeEvent>() {
166                 public BrightnessChangeEvent createFromParcel(Parcel source) {
167                     return new BrightnessChangeEvent(source);
168                 }
169                 public BrightnessChangeEvent[] newArray(int size) {
170                     return new BrightnessChangeEvent[size];
171                 }
172             };
173 
174     @Override
describeContents()175     public int describeContents() {
176         return 0;
177     }
178 
179     @Override
writeToParcel(Parcel dest, int flags)180     public void writeToParcel(Parcel dest, int flags) {
181         dest.writeFloat(brightness);
182         dest.writeLong(timeStamp);
183         dest.writeString(packageName);
184         dest.writeInt(userId);
185         dest.writeFloatArray(luxValues);
186         dest.writeLongArray(luxTimestamps);
187         dest.writeFloat(batteryLevel);
188         dest.writeFloat(powerBrightnessFactor);
189         dest.writeBoolean(nightMode);
190         dest.writeInt(colorTemperature);
191         dest.writeFloat(lastBrightness);
192         dest.writeBoolean(isDefaultBrightnessConfig);
193         dest.writeBoolean(isUserSetBrightness);
194         dest.writeLongArray(colorValueBuckets);
195         dest.writeLong(colorSampleDuration);
196     }
197 
198     /** @hide */
199     public static class Builder {
200         private float mBrightness;
201         private long mTimeStamp;
202         private String mPackageName;
203         private int mUserId;
204         private float[] mLuxValues;
205         private long[] mLuxTimestamps;
206         private float mBatteryLevel;
207         private float mPowerBrightnessFactor;
208         private boolean mNightMode;
209         private int mColorTemperature;
210         private float mLastBrightness;
211         private boolean mIsDefaultBrightnessConfig;
212         private boolean mIsUserSetBrightness;
213         private long[] mColorValueBuckets;
214         private long mColorSampleDuration;
215 
216         /** {@see BrightnessChangeEvent#brightness} */
setBrightness(float brightness)217         public Builder setBrightness(float brightness) {
218             mBrightness = brightness;
219             return this;
220         }
221 
222         /** {@see BrightnessChangeEvent#timeStamp} */
setTimeStamp(long timeStamp)223         public Builder setTimeStamp(long timeStamp) {
224             mTimeStamp = timeStamp;
225             return this;
226         }
227 
228         /** {@see BrightnessChangeEvent#packageName} */
setPackageName(String packageName)229         public Builder setPackageName(String packageName) {
230             mPackageName = packageName;
231             return this;
232         }
233 
234         /** {@see BrightnessChangeEvent#userId} */
setUserId(int userId)235         public Builder setUserId(int userId) {
236             mUserId = userId;
237             return this;
238         }
239 
240         /** {@see BrightnessChangeEvent#luxValues} */
setLuxValues(float[] luxValues)241         public Builder setLuxValues(float[] luxValues) {
242             mLuxValues = luxValues;
243             return this;
244         }
245 
246         /** {@see BrightnessChangeEvent#luxTimestamps} */
setLuxTimestamps(long[] luxTimestamps)247         public Builder setLuxTimestamps(long[] luxTimestamps) {
248             mLuxTimestamps = luxTimestamps;
249             return this;
250         }
251 
252         /** {@see BrightnessChangeEvent#batteryLevel} */
setBatteryLevel(float batteryLevel)253         public Builder setBatteryLevel(float batteryLevel) {
254             mBatteryLevel = batteryLevel;
255             return this;
256         }
257 
258         /** {@see BrightnessChangeEvent#powerSaveBrightness} */
setPowerBrightnessFactor(float powerBrightnessFactor)259         public Builder setPowerBrightnessFactor(float powerBrightnessFactor) {
260             mPowerBrightnessFactor = powerBrightnessFactor;
261             return this;
262         }
263 
264         /** {@see BrightnessChangeEvent#nightMode} */
setNightMode(boolean nightMode)265         public Builder setNightMode(boolean nightMode) {
266             mNightMode = nightMode;
267             return this;
268         }
269 
270         /** {@see BrightnessChangeEvent#colorTemperature} */
setColorTemperature(int colorTemperature)271         public Builder setColorTemperature(int colorTemperature) {
272             mColorTemperature = colorTemperature;
273             return this;
274         }
275 
276         /** {@see BrightnessChangeEvent#lastBrightness} */
setLastBrightness(float lastBrightness)277         public Builder setLastBrightness(float lastBrightness) {
278             mLastBrightness = lastBrightness;
279             return this;
280         }
281 
282         /** {@see BrightnessChangeEvent#isDefaultBrightnessConfig} */
setIsDefaultBrightnessConfig(boolean isDefaultBrightnessConfig)283         public Builder setIsDefaultBrightnessConfig(boolean isDefaultBrightnessConfig) {
284             mIsDefaultBrightnessConfig = isDefaultBrightnessConfig;
285             return this;
286         }
287 
288         /** {@see BrightnessChangeEvent#userBrightnessPoint} */
setUserBrightnessPoint(boolean isUserSetBrightness)289         public Builder setUserBrightnessPoint(boolean isUserSetBrightness) {
290             mIsUserSetBrightness = isUserSetBrightness;
291             return this;
292         }
293 
294         /** {@see BrightnessChangeEvent#colorValueBuckets}
295          *  {@see BrightnessChangeEvent#colorSampleDuration} */
setColorValues(@onNull long[] colorValueBuckets, long colorSampleDuration)296         public Builder setColorValues(@NonNull long[] colorValueBuckets, long colorSampleDuration) {
297             Objects.requireNonNull(colorValueBuckets);
298             mColorValueBuckets = colorValueBuckets;
299             mColorSampleDuration = colorSampleDuration;
300             return this;
301         }
302 
303         /** Builds a BrightnessChangeEvent */
build()304         public BrightnessChangeEvent build() {
305             return new BrightnessChangeEvent(mBrightness, mTimeStamp,
306                     mPackageName, mUserId, mLuxValues, mLuxTimestamps, mBatteryLevel,
307                     mPowerBrightnessFactor, mNightMode, mColorTemperature, mLastBrightness,
308                     mIsDefaultBrightnessConfig, mIsUserSetBrightness, mColorValueBuckets,
309                     mColorSampleDuration);
310         }
311     }
312 }
313