| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 com.android.internal.util.Preconditions; |
| 26 |
|
| 27 |
import java.time.LocalDate; |
| 28 |
import java.util.Arrays; |
| 29 |
import java.util.Objects; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
@SystemApi |
| 38 |
public final class AmbientBrightnessDayStats implements Parcelable { |
| 39 |
|
| 40 |
|
| 41 |
private final LocalDate mLocalDate; |
| 42 |
|
| 43 |
|
| 44 |
private final float[] mBucketBoundaries; |
| 45 |
|
| 46 |
|
| 47 |
private final float[] mStats; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public AmbientBrightnessDayStats(@NonNull LocalDate localDate, |
| 58 |
@NonNull float[] bucketBoundaries) { |
| 59 |
this(localDate, bucketBoundaries, null); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public AmbientBrightnessDayStats(@NonNull LocalDate localDate, |
| 71 |
@NonNull float[] bucketBoundaries, float[] stats) { |
| 72 |
Objects.requireNonNull(localDate); |
| 73 |
Objects.requireNonNull(bucketBoundaries); |
| 74 |
Preconditions.checkArrayElementsInRange(bucketBoundaries, 0, Float.MAX_VALUE, |
| 75 |
"bucketBoundaries"); |
| 76 |
if (bucketBoundaries.length < 1) { |
| 77 |
throw new IllegalArgumentException("Bucket boundaries must contain at least 1 value"); |
| 78 |
} |
| 79 |
checkSorted(bucketBoundaries); |
| 80 |
if (stats == null) { |
| 81 |
stats = new float[bucketBoundaries.length]; |
| 82 |
} else { |
| 83 |
Preconditions.checkArrayElementsInRange(stats, 0, Float.MAX_VALUE, "stats"); |
| 84 |
if (bucketBoundaries.length != stats.length) { |
| 85 |
throw new IllegalArgumentException( |
| 86 |
"Bucket boundaries and stats must be of same size."); |
| 87 |
} |
| 88 |
} |
| 89 |
mLocalDate = localDate; |
| 90 |
mBucketBoundaries = bucketBoundaries; |
| 91 |
mStats = stats; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public LocalDate getLocalDate() { |
| 98 |
return mLocalDate; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public float[] getStats() { |
| 105 |
return mStats; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public float[] getBucketBoundaries() { |
| 115 |
return mBucketBoundaries; |
| 116 |
} |
| 117 |
|
| 118 |
private AmbientBrightnessDayStats(Parcel source) { |
| 119 |
mLocalDate = LocalDate.parse(source.readString()); |
| 120 |
mBucketBoundaries = source.createFloatArray(); |
| 121 |
mStats = source.createFloatArray(); |
| 122 |
} |
| 123 |
|
| 124 |
public static final @android.annotation.NonNull Creator<AmbientBrightnessDayStats> CREATOR = |
| 125 |
new Creator<AmbientBrightnessDayStats>() { |
| 126 |
|
| 127 |
@Override |
| 128 |
public AmbientBrightnessDayStats createFromParcel(Parcel source) { |
| 129 |
return new AmbientBrightnessDayStats(source); |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public AmbientBrightnessDayStats[] newArray(int size) { |
| 134 |
return new AmbientBrightnessDayStats[size]; |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
@Override |
| 139 |
public boolean equals(@Nullable Object obj) { |
| 140 |
if (this == obj) { |
| 141 |
return true; |
| 142 |
} |
| 143 |
if (obj == null) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
if (getClass() != obj.getClass()) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
AmbientBrightnessDayStats other = (AmbientBrightnessDayStats) obj; |
| 150 |
return mLocalDate.equals(other.mLocalDate) && Arrays.equals(mBucketBoundaries, |
| 151 |
other.mBucketBoundaries) && Arrays.equals(mStats, other.mStats); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public int hashCode() { |
| 156 |
final int prime = 31; |
| 157 |
int result = 1; |
| 158 |
result = result * prime + mLocalDate.hashCode(); |
| 159 |
result = result * prime + Arrays.hashCode(mBucketBoundaries); |
| 160 |
result = result * prime + Arrays.hashCode(mStats); |
| 161 |
return result; |
| 162 |
} |
| 163 |
|
| 164 |
@NonNull |
| 165 |
@Override |
| 166 |
public String toString() { |
| 167 |
StringBuilder bucketBoundariesString = new StringBuilder(); |
| 168 |
StringBuilder statsString = new StringBuilder(); |
| 169 |
for (int i = 0; i < mBucketBoundaries.length; i++) { |
| 170 |
if (i != 0) { |
| 171 |
bucketBoundariesString.append(", "); |
| 172 |
statsString.append(", "); |
| 173 |
} |
| 174 |
bucketBoundariesString.append(mBucketBoundaries[i]); |
| 175 |
statsString.append(mStats[i]); |
| 176 |
} |
| 177 |
return new StringBuilder() |
| 178 |
.append(mLocalDate).append(" ") |
| 179 |
.append("{").append(bucketBoundariesString).append("} ") |
| 180 |
.append("{").append(statsString).append("}").toString(); |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public int describeContents() { |
| 185 |
return 0; |
| 186 |
} |
| 187 |
|
| 188 |
@Override |
| 189 |
public void writeToParcel(Parcel dest, int flags) { |
| 190 |
dest.writeString(mLocalDate.toString()); |
| 191 |
dest.writeFloatArray(mBucketBoundaries); |
| 192 |
dest.writeFloatArray(mStats); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public void log(float ambientBrightness, float durationSec) { |
| 204 |
int bucketIndex = getBucketIndex(ambientBrightness); |
| 205 |
if (bucketIndex >= 0) { |
| 206 |
mStats[bucketIndex] += durationSec; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
private int getBucketIndex(float ambientBrightness) { |
| 211 |
if (ambientBrightness < mBucketBoundaries[0] || Float.isNaN(ambientBrightness)) { |
| 212 |
return -1; |
| 213 |
} |
| 214 |
int low = 0; |
| 215 |
int high = mBucketBoundaries.length - 1; |
| 216 |
while (low < high) { |
| 217 |
int mid = (low + high) / 2; |
| 218 |
if (mBucketBoundaries[mid] <= ambientBrightness |
| 219 |
&& ambientBrightness < mBucketBoundaries[mid + 1]) { |
| 220 |
return mid; |
| 221 |
} else if (mBucketBoundaries[mid] < ambientBrightness) { |
| 222 |
low = mid + 1; |
| 223 |
} else if (mBucketBoundaries[mid] > ambientBrightness) { |
| 224 |
high = mid - 1; |
| 225 |
} |
| 226 |
} |
| 227 |
return low; |
| 228 |
} |
| 229 |
|
| 230 |
private static void checkSorted(float[] values) { |
| 231 |
if (values.length <= 1) { |
| 232 |
return; |
| 233 |
} |
| 234 |
float prevValue = values[0]; |
| 235 |
for (int i = 1; i < values.length; i++) { |
| 236 |
Preconditions.checkState(prevValue < values[i]); |
| 237 |
prevValue = values[i]; |
| 238 |
} |
| 239 |
return; |
| 240 |
} |
| 241 |
} |