blob: 1c02dd1fcdf4c87f051f1dfb200bb8f9ed730eb3 [file] [log] [blame]
Julius D'souza428aed02016-08-07 19:08:30 -07001/*
2 * Copyright (C) 2016 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
17package com.android.server.display;
18
19import android.util.Slog;
20
Michael Wright2155bc22018-05-01 00:38:32 +010021import java.io.PrintWriter;
22import java.util.Arrays;
23
Julius D'souza428aed02016-08-07 19:08:30 -070024/**
25 * A helper class for handling access to illuminance hysteresis level values.
26 */
27final class HysteresisLevels {
28 private static final String TAG = "HysteresisLevels";
29
30 // Default hysteresis constraints for brightening or darkening.
31 // The recent lux must have changed by at least this fraction relative to the
32 // current ambient lux before a change will be considered.
33 private static final float DEFAULT_BRIGHTENING_HYSTERESIS = 0.10f;
34 private static final float DEFAULT_DARKENING_HYSTERESIS = 0.20f;
35
36 private static final boolean DEBUG = false;
37
38 private final float[] mBrightLevels;
39 private final float[] mDarkLevels;
40 private final float[] mLuxLevels;
41
42 /**
43 * Creates a {@code HysteresisLevels} object with the given equal-length
44 * integer arrays.
45 * @param brightLevels an array of brightening hysteresis constraint constants
46 * @param darkLevels an array of darkening hysteresis constraint constants
47 * @param luxLevels a monotonically increasing array of illuminance
48 * thresholds in units of lux
49 */
50 public HysteresisLevels(int[] brightLevels, int[] darkLevels, int[] luxLevels) {
51 if (brightLevels.length != darkLevels.length || darkLevels.length != luxLevels.length + 1) {
52 throw new IllegalArgumentException("Mismatch between hysteresis array lengths.");
53 }
54 mBrightLevels = setArrayFormat(brightLevels, 1000.0f);
55 mDarkLevels = setArrayFormat(darkLevels, 1000.0f);
56 mLuxLevels = setArrayFormat(luxLevels, 1.0f);
57 }
58
59 /**
60 * Return the brightening hysteresis threshold for the given lux level.
61 */
62 public float getBrighteningThreshold(float lux) {
63 float brightConstant = getReferenceLevel(lux, mBrightLevels);
64 float brightThreshold = lux * (1.0f + brightConstant);
65 if (DEBUG) {
66 Slog.d(TAG, "bright hysteresis constant=: " + brightConstant + ", threshold="
67 + brightThreshold + ", lux=" + lux);
68 }
69 return brightThreshold;
70 }
71
72 /**
73 * Return the darkening hysteresis threshold for the given lux level.
74 */
75 public float getDarkeningThreshold(float lux) {
76 float darkConstant = getReferenceLevel(lux, mDarkLevels);
77 float darkThreshold = lux * (1.0f - darkConstant);
78 if (DEBUG) {
79 Slog.d(TAG, "dark hysteresis constant=: " + darkConstant + ", threshold="
80 + darkThreshold + ", lux=" + lux);
81 }
82 return darkThreshold;
83 }
84
85 /**
86 * Return the hysteresis constant for the closest lux threshold value to the
87 * current illuminance from the given array.
88 */
89 private float getReferenceLevel(float lux, float[] referenceLevels) {
90 int index = 0;
91 while (mLuxLevels.length > index && lux >= mLuxLevels[index]) {
92 ++index;
93 }
94 return referenceLevels[index];
95 }
96
97 /**
98 * Return a float array where each i-th element equals {@code configArray[i]/divideFactor}.
99 */
100 private float[] setArrayFormat(int[] configArray, float divideFactor) {
101 float[] levelArray = new float[configArray.length];
102 for (int index = 0; levelArray.length > index; ++index) {
103 levelArray[index] = (float)configArray[index] / divideFactor;
104 }
105 return levelArray;
106 }
Michael Wright2155bc22018-05-01 00:38:32 +0100107
108 public void dump(PrintWriter pw) {
109 pw.println("HysteresisLevels");
110 pw.println(" mBrightLevels=" + Arrays.toString(mBrightLevels));
111 pw.println(" mDarkLevels=" + Arrays.toString(mDarkLevels));
112 pw.println(" mLuxLevels=" + Arrays.toString(mLuxLevels));
113 }
Julius D'souza428aed02016-08-07 19:08:30 -0700114}