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