Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -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 | |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 19 | import static android.hardware.display.ColorDisplayManager.AUTO_MODE_CUSTOM_TIME; |
| 20 | import static android.hardware.display.ColorDisplayManager.AUTO_MODE_DISABLED; |
| 21 | import static android.hardware.display.ColorDisplayManager.AUTO_MODE_TWILIGHT; |
Christine Franks | d154fe5 | 2019-01-04 17:17:45 -0800 | [diff] [blame] | 22 | import static android.hardware.display.ColorDisplayManager.COLOR_MODE_AUTOMATIC; |
| 23 | import static android.hardware.display.ColorDisplayManager.COLOR_MODE_BOOSTED; |
| 24 | import static android.hardware.display.ColorDisplayManager.COLOR_MODE_NATURAL; |
| 25 | import static android.hardware.display.ColorDisplayManager.COLOR_MODE_SATURATED; |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 26 | |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 27 | import static com.android.server.display.DisplayTransformManager.LEVEL_COLOR_MATRIX_DISPLAY_WHITE_BALANCE; |
Christine Franks | 39b0311 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 28 | import static com.android.server.display.DisplayTransformManager.LEVEL_COLOR_MATRIX_NIGHT_DISPLAY; |
Christine Franks | 09c229e | 2018-12-14 10:37:40 -0800 | [diff] [blame] | 29 | import static com.android.server.display.DisplayTransformManager.LEVEL_COLOR_MATRIX_SATURATION; |
Christine Franks | 39b0311 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 30 | |
Christine Franks | 09c229e | 2018-12-14 10:37:40 -0800 | [diff] [blame] | 31 | import android.Manifest; |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 32 | import android.animation.Animator; |
| 33 | import android.animation.AnimatorListenerAdapter; |
| 34 | import android.animation.TypeEvaluator; |
| 35 | import android.animation.ValueAnimator; |
Justin Klaassen | 4346f63 | 2016-08-08 15:01:47 -0700 | [diff] [blame] | 36 | import android.annotation.NonNull; |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 37 | import android.annotation.Nullable; |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 38 | import android.annotation.Size; |
Christine Franks | 55194dc | 2019-01-15 13:47:06 -0800 | [diff] [blame] | 39 | import android.annotation.UserIdInt; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 40 | import android.app.AlarmManager; |
| 41 | import android.content.BroadcastReceiver; |
| 42 | import android.content.ContentResolver; |
| 43 | import android.content.Context; |
| 44 | import android.content.Intent; |
| 45 | import android.content.IntentFilter; |
Christine Franks | 09c229e | 2018-12-14 10:37:40 -0800 | [diff] [blame] | 46 | import android.content.pm.PackageManager; |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 47 | import android.content.res.Resources; |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 48 | import android.database.ContentObserver; |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 49 | import android.graphics.ColorSpace; |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 50 | import android.hardware.display.ColorDisplayManager; |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 51 | import android.hardware.display.ColorDisplayManager.AutoMode; |
Christine Franks | d154fe5 | 2019-01-04 17:17:45 -0800 | [diff] [blame] | 52 | import android.hardware.display.ColorDisplayManager.ColorMode; |
Christine Franks | 39b0311 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 53 | import android.hardware.display.IColorDisplayManager; |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 54 | import android.hardware.display.Time; |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 55 | import android.net.Uri; |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 56 | import android.opengl.Matrix; |
Christine Franks | 39b0311 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 57 | import android.os.Binder; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 58 | import android.os.Handler; |
Daniel Solomon | a4ab567 | 2019-01-22 19:35:55 -0800 | [diff] [blame] | 59 | import android.os.IBinder; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 60 | import android.os.Looper; |
Christine Franks | 09c229e | 2018-12-14 10:37:40 -0800 | [diff] [blame] | 61 | import android.os.Message; |
Christine Franks | d154fe5 | 2019-01-04 17:17:45 -0800 | [diff] [blame] | 62 | import android.os.SystemProperties; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 63 | import android.os.UserHandle; |
| 64 | import android.provider.Settings.Secure; |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 65 | import android.provider.Settings.System; |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 66 | import android.util.MathUtils; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 67 | import android.util.Slog; |
Christine Franks | 55194dc | 2019-01-15 13:47:06 -0800 | [diff] [blame] | 68 | import android.view.SurfaceControl; |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 69 | import android.view.SurfaceControl.DisplayPrimaries; |
Christine Franks | 9114f46 | 2019-01-04 11:27:30 -0800 | [diff] [blame] | 70 | import android.view.accessibility.AccessibilityManager; |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 71 | import android.view.animation.AnimationUtils; |
Daniel Solomon | a4ab567 | 2019-01-22 19:35:55 -0800 | [diff] [blame] | 72 | |
Christine Franks | 39b0311 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 73 | import com.android.internal.R; |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 74 | import com.android.internal.annotations.VisibleForTesting; |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 75 | import com.android.internal.util.DumpUtils; |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 76 | import com.android.server.DisplayThread; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 77 | import com.android.server.SystemService; |
| 78 | import com.android.server.twilight.TwilightListener; |
| 79 | import com.android.server.twilight.TwilightManager; |
| 80 | import com.android.server.twilight.TwilightState; |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 81 | |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 82 | import java.io.FileDescriptor; |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 83 | import java.io.PrintWriter; |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 84 | import java.lang.ref.WeakReference; |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 85 | import java.time.DateTimeException; |
| 86 | import java.time.Instant; |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 87 | import java.time.LocalDateTime; |
| 88 | import java.time.LocalTime; |
| 89 | import java.time.ZoneId; |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 90 | import java.time.format.DateTimeParseException; |
Christine Franks | 09c229e | 2018-12-14 10:37:40 -0800 | [diff] [blame] | 91 | import java.util.Arrays; |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 92 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 93 | /** |
Christine Franks | 39b0311 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 94 | * Controls the display's color transforms. |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 95 | */ |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 96 | public final class ColorDisplayService extends SystemService { |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 97 | |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 98 | private static final String TAG = "ColorDisplayService"; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
Christine Franks | 7b83b428 | 2017-01-18 14:55:00 -0800 | [diff] [blame] | 101 | * The transition time, in milliseconds, for Night Display to turn on/off. |
| 102 | */ |
| 103 | private static final long TRANSITION_DURATION = 3000L; |
| 104 | |
| 105 | /** |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 106 | * The identity matrix, used if one of the given matrices is {@code null}. |
| 107 | */ |
| 108 | private static final float[] MATRIX_IDENTITY = new float[16]; |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 109 | |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 110 | static { |
| 111 | Matrix.setIdentityM(MATRIX_IDENTITY, 0); |
| 112 | } |
| 113 | |
Christine Franks | 09c229e | 2018-12-14 10:37:40 -0800 | [diff] [blame] | 114 | private static final int MSG_APPLY_NIGHT_DISPLAY_IMMEDIATE = 0; |
| 115 | private static final int MSG_APPLY_NIGHT_DISPLAY_ANIMATED = 1; |
| 116 | private static final int MSG_APPLY_GLOBAL_SATURATION = 2; |
Christine Franks | c7fb945 | 2019-02-04 08:45:33 -0800 | [diff] [blame] | 117 | private static final int MSG_APPLY_DISPLAY_WHITE_BALANCE = 3; |
Christine Franks | 09c229e | 2018-12-14 10:37:40 -0800 | [diff] [blame] | 118 | |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 119 | /** |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 120 | * Return value if a setting has not been set. |
| 121 | */ |
| 122 | private static final int NOT_SET = -1; |
| 123 | |
| 124 | /** |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 125 | * Evaluator used to animate color matrix transitions. |
| 126 | */ |
| 127 | private static final ColorMatrixEvaluator COLOR_MATRIX_EVALUATOR = new ColorMatrixEvaluator(); |
| 128 | |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 129 | private final NightDisplayTintController mNightDisplayTintController = |
| 130 | new NightDisplayTintController(); |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 131 | |
| 132 | private final TintController mDisplayWhiteBalanceTintController = new TintController() { |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 133 | // Three chromaticity coordinates per color: X, Y, and Z |
| 134 | private final int NUM_VALUES_PER_PRIMARY = 3; |
| 135 | // Four colors: red, green, blue, and white |
| 136 | private final int NUM_DISPLAY_PRIMARIES_VALS = 4 * NUM_VALUES_PER_PRIMARY; |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 137 | |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 138 | private final Object mLock = new Object(); |
| 139 | private int mTemperatureMin; |
| 140 | private int mTemperatureMax; |
| 141 | private int mTemperatureDefault; |
| 142 | private float[] mDisplayNominalWhiteXYZ = new float[NUM_VALUES_PER_PRIMARY]; |
| 143 | private ColorSpace.Rgb mDisplayColorSpaceRGB; |
| 144 | private float[] mChromaticAdaptationMatrix; |
| 145 | private int mCurrentColorTemperature; |
| 146 | private float[] mCurrentColorTemperatureXYZ; |
| 147 | private boolean mSetUp = false; |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 148 | private float[] mMatrixDisplayWhiteBalance = new float[16]; |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 149 | private Boolean mIsAvailable; |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 150 | |
| 151 | @Override |
| 152 | public void setUp(Context context, boolean needsLinear) { |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 153 | mSetUp = false; |
Daniel Solomon | a4ab567 | 2019-01-22 19:35:55 -0800 | [diff] [blame] | 154 | final Resources res = context.getResources(); |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 155 | |
Daniel Solomon | a4ab567 | 2019-01-22 19:35:55 -0800 | [diff] [blame] | 156 | ColorSpace.Rgb displayColorSpaceRGB = getDisplayColorSpaceFromSurfaceControl(); |
| 157 | if (displayColorSpaceRGB == null) { |
| 158 | Slog.w(TAG, "Failed to get display color space from SurfaceControl, trying res"); |
| 159 | displayColorSpaceRGB = getDisplayColorSpaceFromResources(res); |
| 160 | if (displayColorSpaceRGB == null) { |
| 161 | Slog.e(TAG, "Failed to get display color space from resources"); |
| 162 | return; |
| 163 | } |
| 164 | } |
| 165 | |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 166 | final String[] nominalWhiteValues = res.getStringArray( |
| 167 | R.array.config_displayWhiteBalanceDisplayNominalWhite); |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 168 | float[] displayNominalWhiteXYZ = new float[NUM_VALUES_PER_PRIMARY]; |
| 169 | for (int i = 0; i < nominalWhiteValues.length; i++) { |
| 170 | displayNominalWhiteXYZ[i] = Float.parseFloat(nominalWhiteValues[i]); |
| 171 | } |
| 172 | |
| 173 | final int colorTemperatureMin = res.getInteger( |
| 174 | R.integer.config_displayWhiteBalanceColorTemperatureMin); |
| 175 | if (colorTemperatureMin <= 0) { |
Daniel Solomon | a4ab567 | 2019-01-22 19:35:55 -0800 | [diff] [blame] | 176 | Slog.e(TAG, "Display white balance minimum temperature must be greater than 0"); |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | |
| 180 | final int colorTemperatureMax = res.getInteger( |
| 181 | R.integer.config_displayWhiteBalanceColorTemperatureMax); |
| 182 | if (colorTemperatureMax < colorTemperatureMin) { |
Daniel Solomon | a4ab567 | 2019-01-22 19:35:55 -0800 | [diff] [blame] | 183 | Slog.e(TAG, "Display white balance max temp must be greater or equal to min"); |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
| 187 | final int colorTemperature = res.getInteger( |
| 188 | R.integer.config_displayWhiteBalanceColorTemperatureDefault); |
| 189 | |
| 190 | synchronized (mLock) { |
| 191 | mDisplayColorSpaceRGB = displayColorSpaceRGB; |
| 192 | mDisplayNominalWhiteXYZ = displayNominalWhiteXYZ; |
| 193 | mTemperatureMin = colorTemperatureMin; |
| 194 | mTemperatureMax = colorTemperatureMax; |
| 195 | mTemperatureDefault = colorTemperature; |
| 196 | mSetUp = true; |
| 197 | } |
| 198 | |
| 199 | setMatrix(mTemperatureDefault); |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public float[] getMatrix() { |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 204 | return mSetUp && isActivated() ? mMatrixDisplayWhiteBalance : MATRIX_IDENTITY; |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void setMatrix(int cct) { |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 209 | if (!mSetUp) { |
| 210 | Slog.w(TAG, "Can't set display white balance temperature: uninitialized"); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | if (cct < mTemperatureMin) { |
| 215 | Slog.w(TAG, "Requested display color temperature is below allowed minimum"); |
| 216 | cct = mTemperatureMin; |
| 217 | } else if (cct > mTemperatureMax) { |
| 218 | Slog.w(TAG, "Requested display color temperature is above allowed maximum"); |
| 219 | cct = mTemperatureMax; |
| 220 | } |
| 221 | |
| 222 | Slog.d(TAG, "setDisplayWhiteBalanceTemperatureMatrix: cct = " + cct); |
| 223 | |
| 224 | synchronized (mLock) { |
| 225 | mCurrentColorTemperature = cct; |
| 226 | |
| 227 | // Adapt the display's nominal white point to match the requested CCT value |
| 228 | mCurrentColorTemperatureXYZ = ColorSpace.cctToIlluminantdXyz(cct); |
| 229 | |
| 230 | mChromaticAdaptationMatrix = |
Christine Franks | d154fe5 | 2019-01-04 17:17:45 -0800 | [diff] [blame] | 231 | ColorSpace.chromaticAdaptation(ColorSpace.Adaptation.BRADFORD, |
| 232 | mDisplayNominalWhiteXYZ, mCurrentColorTemperatureXYZ); |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 233 | |
| 234 | // Convert the adaptation matrix to RGB space |
| 235 | float[] result = ColorSpace.mul3x3(mChromaticAdaptationMatrix, |
| 236 | mDisplayColorSpaceRGB.getTransform()); |
| 237 | result = ColorSpace.mul3x3(mDisplayColorSpaceRGB.getInverseTransform(), result); |
| 238 | |
| 239 | // Normalize the transform matrix to peak white value in RGB space |
| 240 | final float adaptedMaxR = result[0] + result[3] + result[6]; |
| 241 | final float adaptedMaxG = result[1] + result[4] + result[7]; |
| 242 | final float adaptedMaxB = result[2] + result[5] + result[8]; |
| 243 | final float denum = Math.max(Math.max(adaptedMaxR, adaptedMaxG), adaptedMaxB); |
| 244 | for (int i = 0; i < result.length; i++) { |
| 245 | result[i] /= denum; |
| 246 | } |
| 247 | |
| 248 | Matrix.setIdentityM(mMatrixDisplayWhiteBalance, 0); |
| 249 | java.lang.System.arraycopy(result, 0, mMatrixDisplayWhiteBalance, 0, 3); |
| 250 | java.lang.System.arraycopy(result, 3, mMatrixDisplayWhiteBalance, 4, 3); |
| 251 | java.lang.System.arraycopy(result, 6, mMatrixDisplayWhiteBalance, 8, 3); |
| 252 | } |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public int getLevel() { |
| 257 | return LEVEL_COLOR_MATRIX_DISPLAY_WHITE_BALANCE; |
| 258 | } |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 259 | |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 260 | @Override |
| 261 | public boolean isAvailable(Context context) { |
| 262 | if (mIsAvailable == null) { |
| 263 | mIsAvailable = ColorDisplayManager.isDisplayWhiteBalanceAvailable(context); |
| 264 | } |
| 265 | return mIsAvailable; |
| 266 | } |
| 267 | |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 268 | /** |
| 269 | * Format a given matrix into a string. |
| 270 | * |
| 271 | * @param matrix the matrix to format |
| 272 | * @param cols number of columns in the matrix |
| 273 | */ |
| 274 | private String matrixToString(float[] matrix, int cols) { |
| 275 | if (matrix == null || cols <= 0) { |
| 276 | Slog.e(TAG, "Invalid arguments when formatting matrix to string"); |
| 277 | return ""; |
| 278 | } |
| 279 | |
| 280 | StringBuilder sb = new StringBuilder(""); |
| 281 | for (int i = 0; i < matrix.length; i++) { |
| 282 | if (i % cols == 0) { |
| 283 | sb.append("\n "); |
| 284 | } |
| 285 | sb.append(String.format("%9.6f ", matrix[i])); |
| 286 | } |
| 287 | return sb.toString(); |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public void dump(PrintWriter pw) { |
| 292 | synchronized (mLock) { |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 293 | pw.println(" mSetUp = " + mSetUp); |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 294 | if (!mSetUp) { |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | pw.println(" isActivated = " + isActivated()); |
| 299 | pw.println(" mTemperatureMin = " + mTemperatureMin); |
| 300 | pw.println(" mTemperatureMax = " + mTemperatureMax); |
| 301 | pw.println(" mTemperatureDefault = " + mTemperatureDefault); |
| 302 | pw.println(" mCurrentColorTemperature = " + mCurrentColorTemperature); |
| 303 | pw.println(" mCurrentColorTemperatureXYZ = " + |
| 304 | matrixToString(mCurrentColorTemperatureXYZ, 3)); |
| 305 | pw.println(" mDisplayColorSpaceRGB RGB-to-XYZ = " + |
| 306 | matrixToString(mDisplayColorSpaceRGB.getTransform(), 3)); |
| 307 | pw.println(" mChromaticAdaptationMatrix = " + |
| 308 | matrixToString(mChromaticAdaptationMatrix, 3)); |
| 309 | pw.println(" mDisplayColorSpaceRGB XYZ-to-RGB = " + |
| 310 | matrixToString(mDisplayColorSpaceRGB.getInverseTransform(), 3)); |
| 311 | pw.println(" mMatrixDisplayWhiteBalance = " + |
| 312 | matrixToString(mMatrixDisplayWhiteBalance, 4)); |
| 313 | } |
| 314 | } |
Daniel Solomon | a4ab567 | 2019-01-22 19:35:55 -0800 | [diff] [blame] | 315 | |
| 316 | private ColorSpace.Rgb makeRgbColorSpaceFromXYZ(float[] redGreenBlueXYZ, float[] whiteXYZ) { |
| 317 | return new ColorSpace.Rgb( |
| 318 | "Display Color Space", |
| 319 | redGreenBlueXYZ, |
| 320 | whiteXYZ, |
| 321 | 2.2f // gamma, unused for display white balance |
| 322 | ); |
| 323 | } |
| 324 | |
| 325 | private ColorSpace.Rgb getDisplayColorSpaceFromSurfaceControl() { |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 326 | final IBinder displayToken = SurfaceControl.getInternalDisplayToken(); |
Daniel Solomon | a4ab567 | 2019-01-22 19:35:55 -0800 | [diff] [blame] | 327 | if (displayToken == null) { |
| 328 | return null; |
| 329 | } |
| 330 | |
| 331 | DisplayPrimaries primaries = SurfaceControl.getDisplayNativePrimaries(displayToken); |
| 332 | if (primaries == null || primaries.red == null || primaries.green == null || |
| 333 | primaries.blue == null || primaries.white == null) { |
| 334 | return null; |
| 335 | } |
| 336 | |
| 337 | return makeRgbColorSpaceFromXYZ( |
| 338 | new float[] { |
| 339 | primaries.red.X, primaries.red.Y, primaries.red.Z, |
| 340 | primaries.green.X, primaries.green.Y, primaries.green.Z, |
| 341 | primaries.blue.X, primaries.blue.Y, primaries.blue.Z, |
| 342 | }, |
| 343 | new float[] { primaries.white.X, primaries.white.Y, primaries.white.Z } |
| 344 | ); |
| 345 | } |
| 346 | |
| 347 | private ColorSpace.Rgb getDisplayColorSpaceFromResources(Resources res) { |
| 348 | final String[] displayPrimariesValues = res.getStringArray( |
| 349 | R.array.config_displayWhiteBalanceDisplayPrimaries); |
| 350 | float[] displayRedGreenBlueXYZ = |
| 351 | new float[NUM_DISPLAY_PRIMARIES_VALS - NUM_VALUES_PER_PRIMARY]; |
| 352 | float[] displayWhiteXYZ = new float[NUM_VALUES_PER_PRIMARY]; |
| 353 | |
| 354 | for (int i = 0; i < displayRedGreenBlueXYZ.length; i++) { |
| 355 | displayRedGreenBlueXYZ[i] = Float.parseFloat(displayPrimariesValues[i]); |
| 356 | } |
| 357 | |
| 358 | for (int i = 0; i < displayWhiteXYZ.length; i++) { |
| 359 | displayWhiteXYZ[i] = Float.parseFloat( |
| 360 | displayPrimariesValues[displayRedGreenBlueXYZ.length + i]); |
| 361 | } |
| 362 | |
| 363 | return makeRgbColorSpaceFromXYZ(displayRedGreenBlueXYZ, displayWhiteXYZ); |
| 364 | } |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 365 | }; |
| 366 | |
Christine Franks | 09c229e | 2018-12-14 10:37:40 -0800 | [diff] [blame] | 367 | private final TintController mGlobalSaturationTintController = new TintController() { |
| 368 | |
| 369 | private float[] mMatrixGlobalSaturation = new float[16]; |
| 370 | |
| 371 | @Override |
| 372 | public void setUp(Context context, boolean needsLinear) { |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public float[] getMatrix() { |
| 377 | return Arrays.copyOf(mMatrixGlobalSaturation, mMatrixGlobalSaturation.length); |
| 378 | } |
| 379 | |
| 380 | @Override |
| 381 | public void setMatrix(int saturationLevel) { |
| 382 | if (saturationLevel < 0) { |
| 383 | saturationLevel = 0; |
| 384 | } else if (saturationLevel > 100) { |
| 385 | saturationLevel = 100; |
| 386 | } |
| 387 | Slog.d(TAG, "Setting saturation level: " + saturationLevel); |
| 388 | |
| 389 | if (saturationLevel == 100) { |
| 390 | Matrix.setIdentityM(mMatrixGlobalSaturation, 0); |
| 391 | } else { |
| 392 | float saturation = saturationLevel * 0.1f; |
| 393 | float desaturation = 1.0f - saturation; |
| 394 | float[] luminance = {0.231f * desaturation, 0.715f * desaturation, |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 395 | 0.072f * desaturation}; |
Christine Franks | 09c229e | 2018-12-14 10:37:40 -0800 | [diff] [blame] | 396 | mMatrixGlobalSaturation[0] = luminance[0] + saturation; |
| 397 | mMatrixGlobalSaturation[1] = luminance[0]; |
| 398 | mMatrixGlobalSaturation[2] = luminance[0]; |
| 399 | mMatrixGlobalSaturation[4] = luminance[1]; |
| 400 | mMatrixGlobalSaturation[5] = luminance[1] + saturation; |
| 401 | mMatrixGlobalSaturation[6] = luminance[1]; |
| 402 | mMatrixGlobalSaturation[8] = luminance[2]; |
| 403 | mMatrixGlobalSaturation[9] = luminance[2]; |
| 404 | mMatrixGlobalSaturation[10] = luminance[2] + saturation; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public int getLevel() { |
| 410 | return LEVEL_COLOR_MATRIX_SATURATION; |
| 411 | } |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 412 | |
| 413 | @Override |
| 414 | public boolean isAvailable(Context context) { |
| 415 | return ColorDisplayManager.isColorTransformAccelerated(context); |
| 416 | } |
Christine Franks | 09c229e | 2018-12-14 10:37:40 -0800 | [diff] [blame] | 417 | }; |
| 418 | |
Christine Franks | 9114f46 | 2019-01-04 11:27:30 -0800 | [diff] [blame] | 419 | /** |
| 420 | * Matrix and offset used for converting color to grayscale. |
| 421 | */ |
| 422 | private static final float[] MATRIX_GRAYSCALE = new float[]{ |
| 423 | .2126f, .2126f, .2126f, 0f, |
| 424 | .7152f, .7152f, .7152f, 0f, |
| 425 | .0722f, .0722f, .0722f, 0f, |
| 426 | 0f, 0f, 0f, 1f |
| 427 | }; |
| 428 | |
| 429 | /** |
| 430 | * Matrix and offset used for luminance inversion. Represents a transform from RGB to YIQ color |
| 431 | * space, rotation around the Y axis by 180 degrees, transform back to RGB color space, and |
| 432 | * subtraction from 1. The last row represents a non-multiplied addition, see surfaceflinger's |
| 433 | * ProgramCache for full implementation details. |
| 434 | */ |
Christine Franks | d154fe5 | 2019-01-04 17:17:45 -0800 | [diff] [blame] | 435 | private static final float[] MATRIX_INVERT_COLOR = new float[]{ |
Christine Franks | 9114f46 | 2019-01-04 11:27:30 -0800 | [diff] [blame] | 436 | 0.402f, -0.598f, -0.599f, 0f, |
| 437 | -1.174f, -0.174f, -1.175f, 0f, |
| 438 | -0.228f, -0.228f, 0.772f, 0f, |
| 439 | 1f, 1f, 1f, 1f |
| 440 | }; |
| 441 | |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 442 | private final Handler mHandler; |
| 443 | |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 444 | private final AppSaturationController mAppSaturationController = new AppSaturationController(); |
| 445 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 446 | private int mCurrentUser = UserHandle.USER_NULL; |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 447 | private ContentObserver mUserSetupObserver; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 448 | private boolean mBootCompleted; |
| 449 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 450 | private ContentObserver mContentObserver; |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 451 | |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 452 | private DisplayWhiteBalanceListener mDisplayWhiteBalanceListener; |
| 453 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 454 | private NightDisplayAutoMode mNightDisplayAutoMode; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 455 | |
Christine Franks | 5397f03 | 2017-11-01 18:35:16 -0700 | [diff] [blame] | 456 | public ColorDisplayService(Context context) { |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 457 | super(context); |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 458 | mHandler = new TintHandler(DisplayThread.get().getLooper()); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | @Override |
| 462 | public void onStart() { |
Christine Franks | 39b0311 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 463 | publishBinderService(Context.COLOR_DISPLAY_SERVICE, new BinderService()); |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 464 | publishLocalService(ColorDisplayServiceInternal.class, new ColorDisplayServiceInternal()); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | @Override |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 468 | public void onBootPhase(int phase) { |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 469 | if (phase >= PHASE_BOOT_COMPLETED) { |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 470 | mBootCompleted = true; |
| 471 | |
| 472 | // Register listeners now that boot is complete. |
| 473 | if (mCurrentUser != UserHandle.USER_NULL && mUserSetupObserver == null) { |
| 474 | setUp(); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | @Override |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 480 | public void onStartUser(int userHandle) { |
| 481 | super.onStartUser(userHandle); |
| 482 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 483 | if (mCurrentUser == UserHandle.USER_NULL) { |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 484 | onUserChanged(userHandle); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public void onSwitchUser(int userHandle) { |
| 490 | super.onSwitchUser(userHandle); |
| 491 | |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 492 | onUserChanged(userHandle); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | @Override |
| 496 | public void onStopUser(int userHandle) { |
| 497 | super.onStopUser(userHandle); |
| 498 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 499 | if (mCurrentUser == userHandle) { |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 500 | onUserChanged(UserHandle.USER_NULL); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 504 | private void onUserChanged(int userHandle) { |
| 505 | final ContentResolver cr = getContext().getContentResolver(); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 506 | |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 507 | if (mCurrentUser != UserHandle.USER_NULL) { |
| 508 | if (mUserSetupObserver != null) { |
| 509 | cr.unregisterContentObserver(mUserSetupObserver); |
| 510 | mUserSetupObserver = null; |
| 511 | } else if (mBootCompleted) { |
| 512 | tearDown(); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | mCurrentUser = userHandle; |
| 517 | |
| 518 | if (mCurrentUser != UserHandle.USER_NULL) { |
| 519 | if (!isUserSetupCompleted(cr, mCurrentUser)) { |
| 520 | mUserSetupObserver = new ContentObserver(mHandler) { |
| 521 | @Override |
| 522 | public void onChange(boolean selfChange, Uri uri) { |
| 523 | if (isUserSetupCompleted(cr, mCurrentUser)) { |
| 524 | cr.unregisterContentObserver(this); |
| 525 | mUserSetupObserver = null; |
| 526 | |
| 527 | if (mBootCompleted) { |
| 528 | setUp(); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | }; |
| 533 | cr.registerContentObserver(Secure.getUriFor(Secure.USER_SETUP_COMPLETE), |
Christine Franks | 39b0311 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 534 | false /* notifyForDescendants */, mUserSetupObserver, mCurrentUser); |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 535 | } else if (mBootCompleted) { |
| 536 | setUp(); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 541 | private static boolean isUserSetupCompleted(ContentResolver cr, int userHandle) { |
| 542 | return Secure.getIntForUser(cr, Secure.USER_SETUP_COMPLETE, 0, userHandle) == 1; |
| 543 | } |
| 544 | |
| 545 | private void setUp() { |
Justin Klaassen | ec8837a | 2016-08-23 12:04:42 -0700 | [diff] [blame] | 546 | Slog.d(TAG, "setUp: currentUser=" + mCurrentUser); |
| 547 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 548 | // Listen for external changes to any of the settings. |
| 549 | if (mContentObserver == null) { |
| 550 | mContentObserver = new ContentObserver(new Handler(DisplayThread.get().getLooper())) { |
| 551 | @Override |
| 552 | public void onChange(boolean selfChange, Uri uri) { |
| 553 | super.onChange(selfChange, uri); |
| 554 | |
| 555 | final String setting = uri == null ? null : uri.getLastPathSegment(); |
| 556 | if (setting != null) { |
| 557 | switch (setting) { |
| 558 | case Secure.NIGHT_DISPLAY_ACTIVATED: |
Christine Franks | 78a4dd4 | 2019-02-08 11:09:30 -0800 | [diff] [blame^] | 559 | final boolean activated = mNightDisplayTintController |
| 560 | .isActivatedSetting(); |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 561 | if (mNightDisplayTintController.isActivatedStateNotSet() |
| 562 | || mNightDisplayTintController.isActivated() != activated) { |
Christine Franks | 78a4dd4 | 2019-02-08 11:09:30 -0800 | [diff] [blame^] | 563 | mNightDisplayTintController.setActivated(activated); |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 564 | } |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 565 | break; |
| 566 | case Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE: |
Christine Franks | 78a4dd4 | 2019-02-08 11:09:30 -0800 | [diff] [blame^] | 567 | final int temperature = mNightDisplayTintController |
| 568 | .getColorTemperatureSetting(); |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 569 | if (mNightDisplayTintController.getColorTemperature() |
| 570 | != temperature) { |
| 571 | mNightDisplayTintController |
| 572 | .onColorTemperatureChanged(temperature); |
| 573 | } |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 574 | break; |
| 575 | case Secure.NIGHT_DISPLAY_AUTO_MODE: |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 576 | onNightDisplayAutoModeChanged(getNightDisplayAutoModeInternal()); |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 577 | break; |
| 578 | case Secure.NIGHT_DISPLAY_CUSTOM_START_TIME: |
| 579 | onNightDisplayCustomStartTimeChanged( |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 580 | getNightDisplayCustomStartTimeInternal().getLocalTime()); |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 581 | break; |
| 582 | case Secure.NIGHT_DISPLAY_CUSTOM_END_TIME: |
| 583 | onNightDisplayCustomEndTimeChanged( |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 584 | getNightDisplayCustomEndTimeInternal().getLocalTime()); |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 585 | break; |
| 586 | case System.DISPLAY_COLOR_MODE: |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 587 | onDisplayColorModeChanged(getColorModeInternal()); |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 588 | break; |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 589 | case Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED: |
Christine Franks | 9114f46 | 2019-01-04 11:27:30 -0800 | [diff] [blame] | 590 | onAccessibilityInversionChanged(); |
| 591 | onAccessibilityActivated(); |
| 592 | break; |
| 593 | case Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED: |
| 594 | onAccessibilityDaltonizerChanged(); |
| 595 | onAccessibilityActivated(); |
| 596 | break; |
| 597 | case Secure.ACCESSIBILITY_DISPLAY_DALTONIZER: |
| 598 | onAccessibilityDaltonizerChanged(); |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 599 | break; |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 600 | case Secure.DISPLAY_WHITE_BALANCE_ENABLED: |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 601 | updateDisplayWhiteBalanceStatus(); |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 602 | break; |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | } |
| 606 | }; |
| 607 | } |
| 608 | final ContentResolver cr = getContext().getContentResolver(); |
| 609 | cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_ACTIVATED), |
| 610 | false /* notifyForDescendants */, mContentObserver, mCurrentUser); |
| 611 | cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE), |
| 612 | false /* notifyForDescendants */, mContentObserver, mCurrentUser); |
| 613 | cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_AUTO_MODE), |
| 614 | false /* notifyForDescendants */, mContentObserver, mCurrentUser); |
| 615 | cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_CUSTOM_START_TIME), |
| 616 | false /* notifyForDescendants */, mContentObserver, mCurrentUser); |
| 617 | cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_CUSTOM_END_TIME), |
| 618 | false /* notifyForDescendants */, mContentObserver, mCurrentUser); |
| 619 | cr.registerContentObserver(System.getUriFor(System.DISPLAY_COLOR_MODE), |
| 620 | false /* notifyForDescendants */, mContentObserver, mCurrentUser); |
| 621 | cr.registerContentObserver( |
| 622 | Secure.getUriFor(Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED), |
| 623 | false /* notifyForDescendants */, mContentObserver, mCurrentUser); |
| 624 | cr.registerContentObserver( |
| 625 | Secure.getUriFor(Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED), |
| 626 | false /* notifyForDescendants */, mContentObserver, mCurrentUser); |
Christine Franks | 9114f46 | 2019-01-04 11:27:30 -0800 | [diff] [blame] | 627 | cr.registerContentObserver( |
| 628 | Secure.getUriFor(Secure.ACCESSIBILITY_DISPLAY_DALTONIZER), |
| 629 | false /* notifyForDescendants */, mContentObserver, mCurrentUser); |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 630 | cr.registerContentObserver(Secure.getUriFor(Secure.DISPLAY_WHITE_BALANCE_ENABLED), |
| 631 | false /* notifyForDescendants */, mContentObserver, mCurrentUser); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 632 | |
Christine Franks | cf388c2 | 2018-05-15 15:48:10 -0700 | [diff] [blame] | 633 | // Set the color mode, if valid, and immediately apply the updated tint matrix based on the |
| 634 | // existing activated state. This ensures consistency of tint across the color mode change. |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 635 | onDisplayColorModeChanged(getColorModeInternal()); |
Christine Franks | cf388c2 | 2018-05-15 15:48:10 -0700 | [diff] [blame] | 636 | |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 637 | if (mNightDisplayTintController.isAvailable(getContext())) { |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 638 | // Reset the activated state. |
| 639 | mNightDisplayTintController.setActivated(null); |
Christine Franks | cf388c2 | 2018-05-15 15:48:10 -0700 | [diff] [blame] | 640 | |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 641 | // Prepare the night display color transformation matrix. |
| 642 | mNightDisplayTintController |
| 643 | .setUp(getContext(), DisplayTransformManager.needsLinearColorMatrix()); |
Christine Franks | 78a4dd4 | 2019-02-08 11:09:30 -0800 | [diff] [blame^] | 644 | mNightDisplayTintController |
| 645 | .setMatrix(mNightDisplayTintController.getColorTemperatureSetting()); |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 646 | |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 647 | // Initialize the current auto mode. |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 648 | onNightDisplayAutoModeChanged(getNightDisplayAutoModeInternal()); |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 649 | |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 650 | // Force the initialization of the current saved activation state. |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 651 | if (mNightDisplayTintController.isActivatedStateNotSet()) { |
Christine Franks | 78a4dd4 | 2019-02-08 11:09:30 -0800 | [diff] [blame^] | 652 | mNightDisplayTintController |
| 653 | .setActivated(mNightDisplayTintController.isActivatedSetting()); |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 654 | } |
| 655 | } |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 656 | |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 657 | if (mDisplayWhiteBalanceTintController.isAvailable(getContext())) { |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 658 | // Prepare the display white balance transform matrix. |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 659 | mDisplayWhiteBalanceTintController.setUp(getContext(), true /* needsLinear */); |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 660 | |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 661 | updateDisplayWhiteBalanceStatus(); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 665 | private void tearDown() { |
Justin Klaassen | ec8837a | 2016-08-23 12:04:42 -0700 | [diff] [blame] | 666 | Slog.d(TAG, "tearDown: currentUser=" + mCurrentUser); |
| 667 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 668 | getContext().getContentResolver().unregisterContentObserver(mContentObserver); |
| 669 | |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 670 | if (mNightDisplayTintController.isAvailable(getContext())) { |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 671 | if (mNightDisplayAutoMode != null) { |
| 672 | mNightDisplayAutoMode.onStop(); |
| 673 | mNightDisplayAutoMode = null; |
| 674 | } |
| 675 | mNightDisplayTintController.endAnimator(); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 678 | if (mDisplayWhiteBalanceTintController.isAvailable(getContext())) { |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 679 | mDisplayWhiteBalanceTintController.endAnimator(); |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 680 | } |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 683 | private void onNightDisplayAutoModeChanged(int autoMode) { |
| 684 | Slog.d(TAG, "onNightDisplayAutoModeChanged: autoMode=" + autoMode); |
Justin Klaassen | ec8837a | 2016-08-23 12:04:42 -0700 | [diff] [blame] | 685 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 686 | if (mNightDisplayAutoMode != null) { |
| 687 | mNightDisplayAutoMode.onStop(); |
| 688 | mNightDisplayAutoMode = null; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 691 | if (autoMode == AUTO_MODE_CUSTOM_TIME) { |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 692 | mNightDisplayAutoMode = new CustomNightDisplayAutoMode(); |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 693 | } else if (autoMode == AUTO_MODE_TWILIGHT) { |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 694 | mNightDisplayAutoMode = new TwilightNightDisplayAutoMode(); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 697 | if (mNightDisplayAutoMode != null) { |
| 698 | mNightDisplayAutoMode.onStart(); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 702 | private void onNightDisplayCustomStartTimeChanged(LocalTime startTime) { |
| 703 | Slog.d(TAG, "onNightDisplayCustomStartTimeChanged: startTime=" + startTime); |
Justin Klaassen | ec8837a | 2016-08-23 12:04:42 -0700 | [diff] [blame] | 704 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 705 | if (mNightDisplayAutoMode != null) { |
| 706 | mNightDisplayAutoMode.onCustomStartTimeChanged(startTime); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 710 | private void onNightDisplayCustomEndTimeChanged(LocalTime endTime) { |
| 711 | Slog.d(TAG, "onNightDisplayCustomEndTimeChanged: endTime=" + endTime); |
Justin Klaassen | ec8837a | 2016-08-23 12:04:42 -0700 | [diff] [blame] | 712 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 713 | if (mNightDisplayAutoMode != null) { |
| 714 | mNightDisplayAutoMode.onCustomEndTimeChanged(endTime); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 718 | private void onDisplayColorModeChanged(int mode) { |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 719 | if (mode == NOT_SET) { |
Christine Franks | cf388c2 | 2018-05-15 15:48:10 -0700 | [diff] [blame] | 720 | return; |
| 721 | } |
| 722 | |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 723 | mNightDisplayTintController.cancelAnimator(); |
| 724 | mDisplayWhiteBalanceTintController.cancelAnimator(); |
| 725 | |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 726 | if (mNightDisplayTintController.isAvailable(getContext())) { |
| 727 | mNightDisplayTintController |
| 728 | .setUp(getContext(), DisplayTransformManager.needsLinearColorMatrix(mode)); |
Christine Franks | 78a4dd4 | 2019-02-08 11:09:30 -0800 | [diff] [blame^] | 729 | mNightDisplayTintController |
| 730 | .setMatrix(mNightDisplayTintController.getColorTemperatureSetting()); |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 731 | } |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 732 | |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 733 | updateDisplayWhiteBalanceStatus(); |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 734 | |
Christine Franks | 218e656 | 2017-11-27 10:20:14 -0800 | [diff] [blame] | 735 | final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class); |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 736 | dtm.setColorMode(mode, mNightDisplayTintController.getMatrix()); |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Christine Franks | 9114f46 | 2019-01-04 11:27:30 -0800 | [diff] [blame] | 739 | private void onAccessibilityActivated() { |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 740 | onDisplayColorModeChanged(getColorModeInternal()); |
Daniel Solomon | 317a357 | 2018-03-30 18:36:37 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Christine Franks | 9114f46 | 2019-01-04 11:27:30 -0800 | [diff] [blame] | 743 | /** |
| 744 | * Apply the accessibility daltonizer transform based on the settings value. |
| 745 | */ |
| 746 | private void onAccessibilityDaltonizerChanged() { |
| 747 | final boolean enabled = Secure.getIntForUser(getContext().getContentResolver(), |
| 748 | Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0, mCurrentUser) != 0; |
| 749 | final int daltonizerMode = enabled ? Secure.getIntForUser(getContext().getContentResolver(), |
| 750 | Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, |
| 751 | AccessibilityManager.DALTONIZER_CORRECT_DEUTERANOMALY, mCurrentUser) |
| 752 | : AccessibilityManager.DALTONIZER_DISABLED; |
| 753 | |
| 754 | final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class); |
| 755 | if (daltonizerMode == AccessibilityManager.DALTONIZER_SIMULATE_MONOCHROMACY) { |
| 756 | // Monochromacy isn't supported by the native Daltonizer implementation; use grayscale. |
| 757 | dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_GRAYSCALE, |
| 758 | MATRIX_GRAYSCALE); |
| 759 | dtm.setDaltonizerMode(AccessibilityManager.DALTONIZER_DISABLED); |
| 760 | } else { |
| 761 | dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_GRAYSCALE, null); |
| 762 | dtm.setDaltonizerMode(daltonizerMode); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * Apply the accessibility inversion transform based on the settings value. |
| 768 | */ |
| 769 | private void onAccessibilityInversionChanged() { |
| 770 | final boolean enabled = Secure.getIntForUser(getContext().getContentResolver(), |
| 771 | Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, 0, mCurrentUser) != 0; |
| 772 | final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class); |
| 773 | dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_INVERT_COLOR, |
| 774 | enabled ? MATRIX_INVERT_COLOR : null); |
| 775 | } |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame] | 776 | |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 777 | /** |
| 778 | * Applies current color temperature matrix, or removes it if deactivated. |
| 779 | * |
| 780 | * @param immediate {@code true} skips transition animation |
| 781 | */ |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 782 | private void applyTint(TintController tintController, boolean immediate) { |
| 783 | tintController.cancelAnimator(); |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 784 | |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 785 | final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class); |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 786 | final float[] from = dtm.getColorMatrix(tintController.getLevel()); |
| 787 | final float[] to = tintController.getMatrix(); |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 788 | |
| 789 | if (immediate) { |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 790 | dtm.setColorMatrix(tintController.getLevel(), to); |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 791 | } else { |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 792 | tintController.setAnimator(ValueAnimator.ofObject(COLOR_MATRIX_EVALUATOR, |
| 793 | from == null ? MATRIX_IDENTITY : from, to)); |
| 794 | tintController.getAnimator().setDuration(TRANSITION_DURATION); |
| 795 | tintController.getAnimator().setInterpolator(AnimationUtils.loadInterpolator( |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 796 | getContext(), android.R.interpolator.fast_out_slow_in)); |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 797 | tintController.getAnimator().addUpdateListener((ValueAnimator animator) -> { |
| 798 | final float[] value = (float[]) animator.getAnimatedValue(); |
| 799 | dtm.setColorMatrix(tintController.getLevel(), value); |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 800 | }); |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 801 | tintController.getAnimator().addListener(new AnimatorListenerAdapter() { |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 802 | |
| 803 | private boolean mIsCancelled; |
| 804 | |
| 805 | @Override |
| 806 | public void onAnimationCancel(Animator animator) { |
| 807 | mIsCancelled = true; |
| 808 | } |
| 809 | |
| 810 | @Override |
| 811 | public void onAnimationEnd(Animator animator) { |
| 812 | if (!mIsCancelled) { |
| 813 | // Ensure final color matrix is set at the end of the animation. If the |
| 814 | // animation is cancelled then don't set the final color matrix so the new |
| 815 | // animator can pick up from where this one left off. |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 816 | dtm.setColorMatrix(tintController.getLevel(), to); |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 817 | } |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 818 | tintController.setAnimator(null); |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 819 | } |
| 820 | }); |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 821 | tintController.getAnimator().start(); |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | |
| 825 | /** |
Christine Franks | 39b0311 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 826 | * Returns the first date time corresponding to the local time that occurs before the provided |
| 827 | * date time. |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 828 | * |
| 829 | * @param compareTime the LocalDateTime to compare against |
| 830 | * @return the prior LocalDateTime corresponding to this local time |
| 831 | */ |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 832 | @VisibleForTesting |
| 833 | static LocalDateTime getDateTimeBefore(LocalTime localTime, LocalDateTime compareTime) { |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 834 | final LocalDateTime ldt = LocalDateTime.of(compareTime.getYear(), compareTime.getMonth(), |
| 835 | compareTime.getDayOfMonth(), localTime.getHour(), localTime.getMinute()); |
| 836 | |
| 837 | // Check if the local time has passed, if so return the same time yesterday. |
| 838 | return ldt.isAfter(compareTime) ? ldt.minusDays(1) : ldt; |
| 839 | } |
| 840 | |
| 841 | /** |
Christine Franks | 39b0311 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 842 | * Returns the first date time corresponding to this local time that occurs after the provided |
| 843 | * date time. |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 844 | * |
| 845 | * @param compareTime the LocalDateTime to compare against |
| 846 | * @return the next LocalDateTime corresponding to this local time |
| 847 | */ |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 848 | @VisibleForTesting |
| 849 | static LocalDateTime getDateTimeAfter(LocalTime localTime, LocalDateTime compareTime) { |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 850 | final LocalDateTime ldt = LocalDateTime.of(compareTime.getYear(), compareTime.getMonth(), |
| 851 | compareTime.getDayOfMonth(), localTime.getHour(), localTime.getMinute()); |
| 852 | |
| 853 | // Check if the local time has passed, if so return the same time tomorrow. |
| 854 | return ldt.isBefore(compareTime) ? ldt.plusDays(1) : ldt; |
| 855 | } |
| 856 | |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 857 | private void updateDisplayWhiteBalanceStatus() { |
| 858 | boolean oldActivated = mDisplayWhiteBalanceTintController.isActivated(); |
| 859 | mDisplayWhiteBalanceTintController.setActivated(isDisplayWhiteBalanceSettingEnabled() && |
| 860 | !mNightDisplayTintController.isActivated() && |
| 861 | DisplayTransformManager.needsLinearColorMatrix()); |
| 862 | boolean activated = mDisplayWhiteBalanceTintController.isActivated(); |
| 863 | |
| 864 | if (mDisplayWhiteBalanceListener != null && oldActivated != activated) { |
| 865 | mDisplayWhiteBalanceListener.onDisplayWhiteBalanceStatusChanged(activated); |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 866 | } |
Daniel Solomon | 86508f8 | 2019-01-18 19:01:02 -0800 | [diff] [blame] | 867 | |
| 868 | // If disabled, clear the tint. If enabled, do nothing more here and let the next |
| 869 | // temperature update set the correct tint. |
| 870 | if (!activated) { |
Christine Franks | c7fb945 | 2019-02-04 08:45:33 -0800 | [diff] [blame] | 871 | mHandler.sendEmptyMessage(MSG_APPLY_DISPLAY_WHITE_BALANCE); |
Daniel Solomon | 86508f8 | 2019-01-18 19:01:02 -0800 | [diff] [blame] | 872 | } |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | private boolean isDisplayWhiteBalanceSettingEnabled() { |
| 876 | return Secure.getIntForUser(getContext().getContentResolver(), |
| 877 | Secure.DISPLAY_WHITE_BALANCE_ENABLED, 0, mCurrentUser) == 1; |
| 878 | } |
| 879 | |
Christine Franks | 39b0311 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 880 | private boolean isDeviceColorManagedInternal() { |
| 881 | final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class); |
| 882 | return dtm.isDeviceColorManaged(); |
| 883 | } |
| 884 | |
Christine Franks | 55194dc | 2019-01-15 13:47:06 -0800 | [diff] [blame] | 885 | private int getTransformCapabilitiesInternal() { |
| 886 | int availabilityFlags = ColorDisplayManager.CAPABILITY_NONE; |
| 887 | if (SurfaceControl.getProtectedContentSupport()) { |
| 888 | availabilityFlags |= ColorDisplayManager.CAPABILITY_PROTECTED_CONTENT; |
| 889 | } |
| 890 | final Resources res = getContext().getResources(); |
| 891 | if (res.getBoolean(R.bool.config_setColorTransformAccelerated)) { |
| 892 | availabilityFlags |= ColorDisplayManager.CAPABILITY_HARDWARE_ACCELERATION_GLOBAL; |
| 893 | } |
| 894 | if (res.getBoolean(R.bool.config_setColorTransformAcceleratedPerLayer)) { |
| 895 | availabilityFlags |= ColorDisplayManager.CAPABILITY_HARDWARE_ACCELERATION_PER_APP; |
| 896 | } |
| 897 | return availabilityFlags; |
| 898 | } |
| 899 | |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 900 | private boolean setNightDisplayAutoModeInternal(@AutoMode int autoMode) { |
| 901 | if (getNightDisplayAutoModeInternal() != autoMode) { |
| 902 | Secure.putStringForUser(getContext().getContentResolver(), |
| 903 | Secure.NIGHT_DISPLAY_LAST_ACTIVATED_TIME, |
| 904 | null, |
| 905 | mCurrentUser); |
| 906 | } |
| 907 | return Secure.putIntForUser(getContext().getContentResolver(), |
| 908 | Secure.NIGHT_DISPLAY_AUTO_MODE, autoMode, mCurrentUser); |
| 909 | } |
| 910 | |
| 911 | private int getNightDisplayAutoModeInternal() { |
| 912 | int autoMode = getNightDisplayAutoModeRawInternal(); |
| 913 | if (autoMode == NOT_SET) { |
| 914 | autoMode = getContext().getResources().getInteger( |
| 915 | R.integer.config_defaultNightDisplayAutoMode); |
| 916 | } |
| 917 | if (autoMode != AUTO_MODE_DISABLED |
| 918 | && autoMode != AUTO_MODE_CUSTOM_TIME |
| 919 | && autoMode != AUTO_MODE_TWILIGHT) { |
| 920 | Slog.e(TAG, "Invalid autoMode: " + autoMode); |
| 921 | autoMode = AUTO_MODE_DISABLED; |
| 922 | } |
| 923 | return autoMode; |
| 924 | } |
| 925 | |
| 926 | private int getNightDisplayAutoModeRawInternal() { |
| 927 | return Secure |
| 928 | .getIntForUser(getContext().getContentResolver(), Secure.NIGHT_DISPLAY_AUTO_MODE, |
| 929 | NOT_SET, mCurrentUser); |
| 930 | } |
| 931 | |
| 932 | private Time getNightDisplayCustomStartTimeInternal() { |
| 933 | int startTimeValue = Secure.getIntForUser(getContext().getContentResolver(), |
| 934 | Secure.NIGHT_DISPLAY_CUSTOM_START_TIME, NOT_SET, mCurrentUser); |
| 935 | if (startTimeValue == NOT_SET) { |
| 936 | startTimeValue = getContext().getResources().getInteger( |
| 937 | R.integer.config_defaultNightDisplayCustomStartTime); |
| 938 | } |
| 939 | return new Time(LocalTime.ofSecondOfDay(startTimeValue / 1000)); |
| 940 | } |
| 941 | |
| 942 | private boolean setNightDisplayCustomStartTimeInternal(Time startTime) { |
| 943 | return Secure.putIntForUser(getContext().getContentResolver(), |
| 944 | Secure.NIGHT_DISPLAY_CUSTOM_START_TIME, |
| 945 | startTime.getLocalTime().toSecondOfDay() * 1000, |
| 946 | mCurrentUser); |
| 947 | } |
| 948 | |
| 949 | private Time getNightDisplayCustomEndTimeInternal() { |
| 950 | int endTimeValue = Secure.getIntForUser(getContext().getContentResolver(), |
| 951 | Secure.NIGHT_DISPLAY_CUSTOM_END_TIME, NOT_SET, mCurrentUser); |
| 952 | if (endTimeValue == NOT_SET) { |
| 953 | endTimeValue = getContext().getResources().getInteger( |
| 954 | R.integer.config_defaultNightDisplayCustomEndTime); |
| 955 | } |
| 956 | return new Time(LocalTime.ofSecondOfDay(endTimeValue / 1000)); |
| 957 | } |
| 958 | |
| 959 | private boolean setNightDisplayCustomEndTimeInternal(Time endTime) { |
| 960 | return Secure.putIntForUser(getContext().getContentResolver(), |
| 961 | Secure.NIGHT_DISPLAY_CUSTOM_END_TIME, endTime.getLocalTime().toSecondOfDay() * 1000, |
| 962 | mCurrentUser); |
| 963 | } |
| 964 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 965 | /** |
| 966 | * Returns the last time the night display transform activation state was changed, or {@link |
| 967 | * LocalDateTime#MIN} if night display has never been activated. |
| 968 | */ |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 969 | private LocalDateTime getNightDisplayLastActivatedTimeSetting() { |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 970 | final ContentResolver cr = getContext().getContentResolver(); |
| 971 | final String lastActivatedTime = Secure.getStringForUser( |
| 972 | cr, Secure.NIGHT_DISPLAY_LAST_ACTIVATED_TIME, getContext().getUserId()); |
| 973 | if (lastActivatedTime != null) { |
| 974 | try { |
| 975 | return LocalDateTime.parse(lastActivatedTime); |
| 976 | } catch (DateTimeParseException ignored) { |
| 977 | } |
| 978 | // Uses the old epoch time. |
| 979 | try { |
| 980 | return LocalDateTime.ofInstant( |
| 981 | Instant.ofEpochMilli(Long.parseLong(lastActivatedTime)), |
| 982 | ZoneId.systemDefault()); |
| 983 | } catch (DateTimeException | NumberFormatException ignored) { |
| 984 | } |
| 985 | } |
| 986 | return LocalDateTime.MIN; |
| 987 | } |
| 988 | |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 989 | private boolean setAppSaturationLevelInternal(String packageName, int saturationLevel) { |
| 990 | return mAppSaturationController |
| 991 | .setSaturationLevel(packageName, mCurrentUser, saturationLevel); |
| 992 | } |
| 993 | |
Christine Franks | d154fe5 | 2019-01-04 17:17:45 -0800 | [diff] [blame] | 994 | private void setColorModeInternal(@ColorMode int colorMode) { |
| 995 | if (!isColorModeAvailable(colorMode)) { |
| 996 | throw new IllegalArgumentException("Invalid colorMode: " + colorMode); |
| 997 | } |
| 998 | System.putIntForUser(getContext().getContentResolver(), System.DISPLAY_COLOR_MODE, |
| 999 | colorMode, |
| 1000 | mCurrentUser); |
| 1001 | } |
| 1002 | |
Christine Franks | 71e003e | 2019-01-24 14:40:20 -0800 | [diff] [blame] | 1003 | private @ColorMode int getColorModeInternal() { |
Christine Franks | d154fe5 | 2019-01-04 17:17:45 -0800 | [diff] [blame] | 1004 | final ContentResolver cr = getContext().getContentResolver(); |
| 1005 | if (Secure.getIntForUser(cr, Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, |
| 1006 | 0, mCurrentUser) == 1 |
| 1007 | || Secure.getIntForUser(cr, Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, |
| 1008 | 0, mCurrentUser) == 1) { |
| 1009 | // There are restrictions on the available color modes combined with a11y transforms. |
| 1010 | if (isColorModeAvailable(COLOR_MODE_SATURATED)) { |
| 1011 | return COLOR_MODE_SATURATED; |
| 1012 | } else if (isColorModeAvailable(COLOR_MODE_AUTOMATIC)) { |
| 1013 | return COLOR_MODE_AUTOMATIC; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | int colorMode = System.getIntForUser(cr, System.DISPLAY_COLOR_MODE, -1, mCurrentUser); |
| 1018 | if (colorMode == -1) { |
| 1019 | // There might be a system property controlling color mode that we need to respect; if |
| 1020 | // not, this will set a suitable default. |
| 1021 | colorMode = getCurrentColorModeFromSystemProperties(); |
| 1022 | } |
| 1023 | |
| 1024 | // This happens when a color mode is no longer available (e.g., after system update or B&R) |
| 1025 | // or the device does not support any color mode. |
| 1026 | if (!isColorModeAvailable(colorMode)) { |
| 1027 | if (colorMode == COLOR_MODE_BOOSTED && isColorModeAvailable(COLOR_MODE_NATURAL)) { |
| 1028 | colorMode = COLOR_MODE_NATURAL; |
| 1029 | } else if (colorMode == COLOR_MODE_SATURATED |
| 1030 | && isColorModeAvailable(COLOR_MODE_AUTOMATIC)) { |
| 1031 | colorMode = COLOR_MODE_AUTOMATIC; |
| 1032 | } else if (colorMode == COLOR_MODE_AUTOMATIC |
| 1033 | && isColorModeAvailable(COLOR_MODE_SATURATED)) { |
| 1034 | colorMode = COLOR_MODE_SATURATED; |
| 1035 | } else { |
| 1036 | colorMode = -1; |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | return colorMode; |
| 1041 | } |
| 1042 | |
| 1043 | /** |
| 1044 | * Get the current color mode from system properties, or return -1 if invalid. |
| 1045 | * |
| 1046 | * See {@link com.android.server.display.DisplayTransformManager} |
| 1047 | */ |
Christine Franks | 78a4dd4 | 2019-02-08 11:09:30 -0800 | [diff] [blame^] | 1048 | private @ColorMode int getCurrentColorModeFromSystemProperties() { |
Christine Franks | d154fe5 | 2019-01-04 17:17:45 -0800 | [diff] [blame] | 1049 | final int displayColorSetting = SystemProperties.getInt("persist.sys.sf.native_mode", 0); |
| 1050 | if (displayColorSetting == 0) { |
| 1051 | return "1.0".equals(SystemProperties.get("persist.sys.sf.color_saturation")) |
| 1052 | ? COLOR_MODE_NATURAL : COLOR_MODE_BOOSTED; |
| 1053 | } else if (displayColorSetting == 1) { |
| 1054 | return COLOR_MODE_SATURATED; |
| 1055 | } else if (displayColorSetting == 2) { |
| 1056 | return COLOR_MODE_AUTOMATIC; |
| 1057 | } else { |
| 1058 | return -1; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | private boolean isColorModeAvailable(@ColorMode int colorMode) { |
| 1063 | final int[] availableColorModes = getContext().getResources().getIntArray( |
| 1064 | R.array.config_availableColorModes); |
| 1065 | if (availableColorModes != null) { |
| 1066 | for (int mode : availableColorModes) { |
| 1067 | if (mode == colorMode) { |
| 1068 | return true; |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | return false; |
| 1073 | } |
| 1074 | |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 1075 | private void dumpInternal(PrintWriter pw) { |
| 1076 | pw.println("COLOR DISPLAY MANAGER dumpsys (color_display)"); |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 1077 | |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 1078 | pw.println("Night Display:"); |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 1079 | if (mNightDisplayTintController.isAvailable(getContext())) { |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 1080 | pw.println(" Activated: " + mNightDisplayTintController.isActivated()); |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 1081 | pw.println(" Color temp: " + mNightDisplayTintController.getColorTemperature()); |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 1082 | } else { |
| 1083 | pw.println(" Not available"); |
| 1084 | } |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 1085 | |
| 1086 | pw.println("Global saturation:"); |
| 1087 | if (mGlobalSaturationTintController.isAvailable(getContext())) { |
| 1088 | pw.println(" Activated: " + mGlobalSaturationTintController.isActivated()); |
| 1089 | } else { |
| 1090 | pw.println(" Not available"); |
| 1091 | } |
| 1092 | |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 1093 | mAppSaturationController.dump(pw); |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 1094 | |
| 1095 | pw.println("Display white balance:"); |
| 1096 | if (mDisplayWhiteBalanceTintController.isAvailable(getContext())) { |
| 1097 | pw.println(" Activated: " + mDisplayWhiteBalanceTintController.isActivated()); |
| 1098 | } else { |
| 1099 | pw.println(" Not available"); |
| 1100 | } |
| 1101 | |
| 1102 | pw.println("Color mode: " + getColorModeInternal()); |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 1103 | } |
| 1104 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1105 | private abstract class NightDisplayAutoMode { |
| 1106 | |
| 1107 | public abstract void onActivated(boolean activated); |
| 1108 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1109 | public abstract void onStart(); |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 1110 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1111 | public abstract void onStop(); |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1112 | |
| 1113 | public void onCustomStartTimeChanged(LocalTime startTime) { |
| 1114 | } |
| 1115 | |
| 1116 | public void onCustomEndTimeChanged(LocalTime endTime) { |
| 1117 | } |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1120 | private final class CustomNightDisplayAutoMode extends NightDisplayAutoMode implements |
| 1121 | AlarmManager.OnAlarmListener { |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1122 | |
| 1123 | private final AlarmManager mAlarmManager; |
| 1124 | private final BroadcastReceiver mTimeChangedReceiver; |
| 1125 | |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 1126 | private LocalTime mStartTime; |
| 1127 | private LocalTime mEndTime; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1128 | |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 1129 | private LocalDateTime mLastActivatedTime; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1130 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1131 | CustomNightDisplayAutoMode() { |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1132 | mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); |
| 1133 | mTimeChangedReceiver = new BroadcastReceiver() { |
| 1134 | @Override |
| 1135 | public void onReceive(Context context, Intent intent) { |
| 1136 | updateActivated(); |
| 1137 | } |
| 1138 | }; |
| 1139 | } |
| 1140 | |
| 1141 | private void updateActivated() { |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 1142 | final LocalDateTime now = LocalDateTime.now(); |
| 1143 | final LocalDateTime start = getDateTimeBefore(mStartTime, now); |
| 1144 | final LocalDateTime end = getDateTimeAfter(mEndTime, start); |
| 1145 | boolean activate = now.isBefore(end); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1146 | |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 1147 | if (mLastActivatedTime != null) { |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 1148 | // Maintain the existing activated state if within the current period. |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 1149 | if (mLastActivatedTime.isBefore(now) && mLastActivatedTime.isAfter(start) |
| 1150 | && (mLastActivatedTime.isAfter(end) || now.isBefore(end))) { |
Christine Franks | 78a4dd4 | 2019-02-08 11:09:30 -0800 | [diff] [blame^] | 1151 | activate = mNightDisplayTintController.isActivatedSetting(); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 1155 | if (mNightDisplayTintController.isActivatedStateNotSet() || ( |
| 1156 | mNightDisplayTintController.isActivated() != activate)) { |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1157 | mNightDisplayTintController.setActivated(activate); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1158 | } |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 1159 | |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 1160 | updateNextAlarm(mNightDisplayTintController.isActivated(), now); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 1163 | private void updateNextAlarm(@Nullable Boolean activated, @NonNull LocalDateTime now) { |
Justin Klaassen | 4346f63 | 2016-08-08 15:01:47 -0700 | [diff] [blame] | 1164 | if (activated != null) { |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 1165 | final LocalDateTime next = activated ? getDateTimeAfter(mEndTime, now) |
| 1166 | : getDateTimeAfter(mStartTime, now); |
| 1167 | final long millis = next.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); |
| 1168 | mAlarmManager.setExact(AlarmManager.RTC, millis, TAG, this, null); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | @Override |
| 1173 | public void onStart() { |
| 1174 | final IntentFilter intentFilter = new IntentFilter(Intent.ACTION_TIME_CHANGED); |
| 1175 | intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED); |
| 1176 | getContext().registerReceiver(mTimeChangedReceiver, intentFilter); |
| 1177 | |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1178 | mStartTime = getNightDisplayCustomStartTimeInternal().getLocalTime(); |
| 1179 | mEndTime = getNightDisplayCustomEndTimeInternal().getLocalTime(); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1180 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1181 | mLastActivatedTime = getNightDisplayLastActivatedTimeSetting(); |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 1182 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1183 | // Force an update to initialize state. |
| 1184 | updateActivated(); |
| 1185 | } |
| 1186 | |
| 1187 | @Override |
| 1188 | public void onStop() { |
| 1189 | getContext().unregisterReceiver(mTimeChangedReceiver); |
| 1190 | |
| 1191 | mAlarmManager.cancel(this); |
| 1192 | mLastActivatedTime = null; |
| 1193 | } |
| 1194 | |
| 1195 | @Override |
| 1196 | public void onActivated(boolean activated) { |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1197 | mLastActivatedTime = getNightDisplayLastActivatedTimeSetting(); |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 1198 | updateNextAlarm(activated, LocalDateTime.now()); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | @Override |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 1202 | public void onCustomStartTimeChanged(LocalTime startTime) { |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1203 | mStartTime = startTime; |
| 1204 | mLastActivatedTime = null; |
| 1205 | updateActivated(); |
| 1206 | } |
| 1207 | |
| 1208 | @Override |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 1209 | public void onCustomEndTimeChanged(LocalTime endTime) { |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1210 | mEndTime = endTime; |
| 1211 | mLastActivatedTime = null; |
| 1212 | updateActivated(); |
| 1213 | } |
| 1214 | |
| 1215 | @Override |
| 1216 | public void onAlarm() { |
Justin Klaassen | ec8837a | 2016-08-23 12:04:42 -0700 | [diff] [blame] | 1217 | Slog.d(TAG, "onAlarm"); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1218 | updateActivated(); |
| 1219 | } |
| 1220 | } |
| 1221 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1222 | private final class TwilightNightDisplayAutoMode extends NightDisplayAutoMode implements |
| 1223 | TwilightListener { |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1224 | |
| 1225 | private final TwilightManager mTwilightManager; |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1226 | private LocalDateTime mLastActivatedTime; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1227 | |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1228 | TwilightNightDisplayAutoMode() { |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1229 | mTwilightManager = getLocalService(TwilightManager.class); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 1232 | private void updateActivated(TwilightState state) { |
Justin Klaassen | 3da4c44 | 2017-05-05 15:19:33 -0700 | [diff] [blame] | 1233 | if (state == null) { |
| 1234 | // If there isn't a valid TwilightState then just keep the current activated |
| 1235 | // state. |
| 1236 | return; |
| 1237 | } |
| 1238 | |
| 1239 | boolean activate = state.isNight(); |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1240 | if (mLastActivatedTime != null) { |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 1241 | final LocalDateTime now = LocalDateTime.now(); |
| 1242 | final LocalDateTime sunrise = state.sunrise(); |
| 1243 | final LocalDateTime sunset = state.sunset(); |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 1244 | // Maintain the existing activated state if within the current period. |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1245 | if (mLastActivatedTime.isBefore(now) && (mLastActivatedTime.isBefore(sunrise) |
| 1246 | ^ mLastActivatedTime.isBefore(sunset))) { |
Christine Franks | 78a4dd4 | 2019-02-08 11:09:30 -0800 | [diff] [blame^] | 1247 | activate = mNightDisplayTintController.isActivatedSetting(); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1248 | } |
| 1249 | } |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 1250 | |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 1251 | if (mNightDisplayTintController.isActivatedStateNotSet() || ( |
| 1252 | mNightDisplayTintController.isActivated() != activate)) { |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1253 | mNightDisplayTintController.setActivated(activate); |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 1254 | } |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | @Override |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1258 | public void onActivated(boolean activated) { |
| 1259 | mLastActivatedTime = getNightDisplayLastActivatedTimeSetting(); |
| 1260 | } |
| 1261 | |
| 1262 | @Override |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1263 | public void onStart() { |
| 1264 | mTwilightManager.registerListener(this, mHandler); |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1265 | mLastActivatedTime = getNightDisplayLastActivatedTimeSetting(); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1266 | |
| 1267 | // Force an update to initialize state. |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 1268 | updateActivated(mTwilightManager.getLastTwilightState()); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | @Override |
| 1272 | public void onStop() { |
| 1273 | mTwilightManager.unregisterListener(this); |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1274 | mLastActivatedTime = null; |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | @Override |
| 1278 | public void onTwilightStateChanged(@Nullable TwilightState state) { |
Justin Klaassen | ec8837a | 2016-08-23 12:04:42 -0700 | [diff] [blame] | 1279 | Slog.d(TAG, "onTwilightStateChanged: isNight=" |
| 1280 | + (state == null ? null : state.isNight())); |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 1281 | updateActivated(state); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1282 | } |
| 1283 | } |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 1284 | |
| 1285 | /** |
| 1286 | * Interpolates between two 4x4 color transform matrices (in column-major order). |
| 1287 | */ |
| 1288 | private static class ColorMatrixEvaluator implements TypeEvaluator<float[]> { |
| 1289 | |
| 1290 | /** |
| 1291 | * Result matrix returned by {@link #evaluate(float, float[], float[])}. |
| 1292 | */ |
| 1293 | private final float[] mResultMatrix = new float[16]; |
| 1294 | |
| 1295 | @Override |
| 1296 | public float[] evaluate(float fraction, float[] startValue, float[] endValue) { |
| 1297 | for (int i = 0; i < mResultMatrix.length; i++) { |
| 1298 | mResultMatrix[i] = MathUtils.lerp(startValue[i], endValue[i], fraction); |
| 1299 | } |
| 1300 | return mResultMatrix; |
| 1301 | } |
| 1302 | } |
Christine Franks | 39b0311 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1303 | |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 1304 | private abstract static class TintController { |
| 1305 | |
| 1306 | private ValueAnimator mAnimator; |
| 1307 | private Boolean mIsActivated; |
| 1308 | |
| 1309 | public ValueAnimator getAnimator() { |
| 1310 | return mAnimator; |
| 1311 | } |
| 1312 | |
| 1313 | public void setAnimator(ValueAnimator animator) { |
| 1314 | mAnimator = animator; |
| 1315 | } |
| 1316 | |
| 1317 | /** |
| 1318 | * Cancel the animator if it's still running. |
| 1319 | */ |
| 1320 | public void cancelAnimator() { |
| 1321 | if (mAnimator != null) { |
| 1322 | mAnimator.cancel(); |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | /** |
| 1327 | * End the animator if it's still running, jumping to the end state. |
| 1328 | */ |
| 1329 | public void endAnimator() { |
| 1330 | if (mAnimator != null) { |
| 1331 | mAnimator.end(); |
| 1332 | mAnimator = null; |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | public void setActivated(Boolean isActivated) { |
| 1337 | mIsActivated = isActivated; |
| 1338 | } |
| 1339 | |
| 1340 | public boolean isActivated() { |
| 1341 | return mIsActivated != null && mIsActivated; |
| 1342 | } |
| 1343 | |
| 1344 | public boolean isActivatedStateNotSet() { |
| 1345 | return mIsActivated == null; |
| 1346 | } |
| 1347 | |
| 1348 | /** |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 1349 | * Dump debug information. |
| 1350 | */ |
| 1351 | public void dump(PrintWriter pw) { |
| 1352 | } |
| 1353 | |
| 1354 | /** |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 1355 | * Set up any constants needed for computing the matrix. |
| 1356 | */ |
| 1357 | public abstract void setUp(Context context, boolean needsLinear); |
| 1358 | |
| 1359 | /** |
| 1360 | * Sets the 4x4 matrix to apply. |
| 1361 | */ |
| 1362 | public abstract void setMatrix(int value); |
| 1363 | |
| 1364 | /** |
| 1365 | * Get the 4x4 matrix to apply. |
| 1366 | */ |
| 1367 | public abstract float[] getMatrix(); |
| 1368 | |
| 1369 | /** |
| 1370 | * Get the color transform level to apply the matrix. |
| 1371 | */ |
| 1372 | public abstract int getLevel(); |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 1373 | |
| 1374 | /** |
| 1375 | * Returns whether or not this transform type is available on this device. |
| 1376 | */ |
| 1377 | public abstract boolean isAvailable(Context context); |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 1378 | } |
| 1379 | |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1380 | private final class NightDisplayTintController extends TintController { |
| 1381 | |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 1382 | private final float[] mMatrix = new float[16]; |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1383 | private final float[] mColorTempCoefficients = new float[9]; |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 1384 | |
| 1385 | private Boolean mIsAvailable; |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1386 | private Integer mColorTemp; |
| 1387 | |
| 1388 | /** |
| 1389 | * Set coefficients based on whether the color matrix is linear or not. |
| 1390 | */ |
| 1391 | @Override |
| 1392 | public void setUp(Context context, boolean needsLinear) { |
| 1393 | final String[] coefficients = context.getResources().getStringArray(needsLinear |
| 1394 | ? R.array.config_nightDisplayColorTemperatureCoefficients |
| 1395 | : R.array.config_nightDisplayColorTemperatureCoefficientsNative); |
| 1396 | for (int i = 0; i < 9 && i < coefficients.length; i++) { |
| 1397 | mColorTempCoefficients[i] = Float.parseFloat(coefficients[i]); |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | @Override |
| 1402 | public void setMatrix(int cct) { |
| 1403 | if (mMatrix.length != 16) { |
| 1404 | Slog.d(TAG, "The display transformation matrix must be 4x4"); |
| 1405 | return; |
| 1406 | } |
| 1407 | |
| 1408 | Matrix.setIdentityM(mMatrix, 0); |
| 1409 | |
| 1410 | final float squareTemperature = cct * cct; |
| 1411 | final float red = squareTemperature * mColorTempCoefficients[0] |
| 1412 | + cct * mColorTempCoefficients[1] + mColorTempCoefficients[2]; |
| 1413 | final float green = squareTemperature * mColorTempCoefficients[3] |
| 1414 | + cct * mColorTempCoefficients[4] + mColorTempCoefficients[5]; |
| 1415 | final float blue = squareTemperature * mColorTempCoefficients[6] |
| 1416 | + cct * mColorTempCoefficients[7] + mColorTempCoefficients[8]; |
| 1417 | mMatrix[0] = red; |
| 1418 | mMatrix[5] = green; |
| 1419 | mMatrix[10] = blue; |
| 1420 | } |
| 1421 | |
| 1422 | @Override |
| 1423 | public float[] getMatrix() { |
| 1424 | return isActivated() ? mMatrix : MATRIX_IDENTITY; |
| 1425 | } |
| 1426 | |
| 1427 | @Override |
| 1428 | public void setActivated(Boolean activated) { |
| 1429 | if (activated == null) { |
| 1430 | super.setActivated(null); |
| 1431 | return; |
| 1432 | } |
| 1433 | |
| 1434 | boolean activationStateChanged = activated != isActivated(); |
| 1435 | |
| 1436 | if (!isActivatedStateNotSet() && activationStateChanged) { |
| 1437 | // This is a true state change, so set this as the last activation time. |
| 1438 | Secure.putStringForUser(getContext().getContentResolver(), |
| 1439 | Secure.NIGHT_DISPLAY_LAST_ACTIVATED_TIME, |
| 1440 | LocalDateTime.now().toString(), |
| 1441 | mCurrentUser); |
| 1442 | } |
| 1443 | |
| 1444 | if (isActivatedStateNotSet() || activationStateChanged) { |
| 1445 | super.setActivated(activated); |
Christine Franks | 78a4dd4 | 2019-02-08 11:09:30 -0800 | [diff] [blame^] | 1446 | if (isActivatedSetting() != activated) { |
| 1447 | Secure.putIntForUser(getContext().getContentResolver(), |
| 1448 | Secure.NIGHT_DISPLAY_ACTIVATED, |
| 1449 | activated ? 1 : 0, mCurrentUser); |
| 1450 | } |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1451 | onActivated(activated); |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | @Override |
| 1456 | public int getLevel() { |
| 1457 | return LEVEL_COLOR_MATRIX_NIGHT_DISPLAY; |
| 1458 | } |
| 1459 | |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 1460 | @Override |
| 1461 | public boolean isAvailable(Context context) { |
| 1462 | if (mIsAvailable == null) { |
| 1463 | mIsAvailable = ColorDisplayManager.isNightDisplayAvailable(context); |
| 1464 | } |
| 1465 | return mIsAvailable; |
| 1466 | } |
| 1467 | |
Christine Franks | 78a4dd4 | 2019-02-08 11:09:30 -0800 | [diff] [blame^] | 1468 | private void onActivated(boolean activated) { |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1469 | Slog.i(TAG, activated ? "Turning on night display" : "Turning off night display"); |
| 1470 | if (mNightDisplayAutoMode != null) { |
| 1471 | mNightDisplayAutoMode.onActivated(activated); |
| 1472 | } |
| 1473 | |
Christine Franks | a4ed376 | 2019-01-24 15:37:04 -0800 | [diff] [blame] | 1474 | if (mDisplayWhiteBalanceTintController.isAvailable(getContext())) { |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1475 | updateDisplayWhiteBalanceStatus(); |
| 1476 | } |
| 1477 | |
| 1478 | mHandler.sendEmptyMessage(MSG_APPLY_NIGHT_DISPLAY_ANIMATED); |
| 1479 | } |
| 1480 | |
| 1481 | int getColorTemperature() { |
| 1482 | return mColorTemp != null ? clampNightDisplayColorTemperature(mColorTemp) |
Christine Franks | 78a4dd4 | 2019-02-08 11:09:30 -0800 | [diff] [blame^] | 1483 | : getColorTemperatureSetting(); |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | boolean setColorTemperature(int temperature) { |
| 1487 | mColorTemp = temperature; |
| 1488 | final boolean success = Secure.putIntForUser(getContext().getContentResolver(), |
| 1489 | Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE, temperature, mCurrentUser); |
| 1490 | onColorTemperatureChanged(temperature); |
| 1491 | return success; |
| 1492 | } |
| 1493 | |
| 1494 | void onColorTemperatureChanged(int temperature) { |
| 1495 | setMatrix(temperature); |
| 1496 | mHandler.sendEmptyMessage(MSG_APPLY_NIGHT_DISPLAY_IMMEDIATE); |
| 1497 | } |
Christine Franks | 78a4dd4 | 2019-02-08 11:09:30 -0800 | [diff] [blame^] | 1498 | |
| 1499 | boolean isActivatedSetting() { |
| 1500 | return Secure.getIntForUser(getContext().getContentResolver(), |
| 1501 | Secure.NIGHT_DISPLAY_ACTIVATED, 0, mCurrentUser) == 1; |
| 1502 | } |
| 1503 | |
| 1504 | int getColorTemperatureSetting() { |
| 1505 | return clampNightDisplayColorTemperature(Secure.getIntForUser( |
| 1506 | getContext().getContentResolver(), Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE, |
| 1507 | NOT_SET, |
| 1508 | mCurrentUser)); |
| 1509 | } |
| 1510 | |
| 1511 | private int clampNightDisplayColorTemperature(int colorTemperature) { |
| 1512 | if (colorTemperature == NOT_SET) { |
| 1513 | colorTemperature = getContext().getResources().getInteger( |
| 1514 | R.integer.config_nightDisplayColorTemperatureDefault); |
| 1515 | } |
| 1516 | final int minimumTemperature = ColorDisplayManager |
| 1517 | .getMinimumColorTemperature(getContext()); |
| 1518 | final int maximumTemperature = ColorDisplayManager |
| 1519 | .getMaximumColorTemperature(getContext()); |
| 1520 | if (colorTemperature < minimumTemperature) { |
| 1521 | colorTemperature = minimumTemperature; |
| 1522 | } else if (colorTemperature > maximumTemperature) { |
| 1523 | colorTemperature = maximumTemperature; |
| 1524 | } |
| 1525 | |
| 1526 | return colorTemperature; |
| 1527 | } |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 1530 | /** |
| 1531 | * Local service that allows color transforms to be enabled from other system services. |
| 1532 | */ |
| 1533 | public final class ColorDisplayServiceInternal { |
| 1534 | |
| 1535 | /** |
| 1536 | * Set the current CCT value for the display white balance transform, and if the transform |
| 1537 | * is enabled, apply it. |
| 1538 | * |
| 1539 | * @param cct the color temperature in Kelvin. |
| 1540 | */ |
| 1541 | public boolean setDisplayWhiteBalanceColorTemperature(int cct) { |
| 1542 | // Update the transform matrix even if it can't be applied. |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 1543 | mDisplayWhiteBalanceTintController.setMatrix(cct); |
| 1544 | |
| 1545 | if (mDisplayWhiteBalanceTintController.isActivated()) { |
Christine Franks | c7fb945 | 2019-02-04 08:45:33 -0800 | [diff] [blame] | 1546 | mHandler.sendEmptyMessage(MSG_APPLY_DISPLAY_WHITE_BALANCE); |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 1547 | return true; |
| 1548 | } |
| 1549 | return false; |
| 1550 | } |
| 1551 | |
| 1552 | /** |
| 1553 | * Sets the listener and returns whether display white balance is currently enabled. |
| 1554 | */ |
| 1555 | public boolean setDisplayWhiteBalanceListener(DisplayWhiteBalanceListener listener) { |
| 1556 | mDisplayWhiteBalanceListener = listener; |
| 1557 | return mDisplayWhiteBalanceTintController.isActivated(); |
| 1558 | } |
Daniel Solomon | 8b72c5b | 2018-11-25 11:07:13 -0800 | [diff] [blame] | 1559 | |
| 1560 | public void dump(PrintWriter pw) { |
| 1561 | mDisplayWhiteBalanceTintController.dump(pw); |
| 1562 | } |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 1563 | |
| 1564 | /** |
| 1565 | * Adds a {@link WeakReference<ColorTransformController>} for a newly started activity, and |
| 1566 | * invokes {@link ColorTransformController#applyAppSaturation(float[], float[])} if needed. |
| 1567 | */ |
Christine Franks | 55194dc | 2019-01-15 13:47:06 -0800 | [diff] [blame] | 1568 | public boolean attachColorTransformController(String packageName, @UserIdInt int userId, |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 1569 | WeakReference<ColorTransformController> controller) { |
| 1570 | return mAppSaturationController |
Christine Franks | 55194dc | 2019-01-15 13:47:06 -0800 | [diff] [blame] | 1571 | .addColorTransformController(packageName, userId, controller); |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 1572 | } |
Christine Franks | 245ffd4 | 2018-11-16 13:45:14 -0800 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | /** |
| 1576 | * Listener for changes in display white balance status. |
| 1577 | */ |
| 1578 | public interface DisplayWhiteBalanceListener { |
| 1579 | |
| 1580 | /** |
| 1581 | * Notify that the display white balance status has changed, either due to preemption by |
| 1582 | * another transform or the feature being turned off. |
| 1583 | */ |
| 1584 | void onDisplayWhiteBalanceStatusChanged(boolean enabled); |
| 1585 | } |
| 1586 | |
Christine Franks | 09c229e | 2018-12-14 10:37:40 -0800 | [diff] [blame] | 1587 | private final class TintHandler extends Handler { |
| 1588 | |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1589 | private TintHandler(Looper looper) { |
Christine Franks | 09c229e | 2018-12-14 10:37:40 -0800 | [diff] [blame] | 1590 | super(looper, null, true /* async */); |
| 1591 | } |
| 1592 | |
| 1593 | @Override |
| 1594 | public void handleMessage(Message msg) { |
| 1595 | switch (msg.what) { |
| 1596 | case MSG_APPLY_GLOBAL_SATURATION: |
| 1597 | mGlobalSaturationTintController.setMatrix(msg.arg1); |
| 1598 | applyTint(mGlobalSaturationTintController, false); |
| 1599 | break; |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1600 | case MSG_APPLY_NIGHT_DISPLAY_IMMEDIATE: |
| 1601 | applyTint(mNightDisplayTintController, true); |
| 1602 | break; |
| 1603 | case MSG_APPLY_NIGHT_DISPLAY_ANIMATED: |
| 1604 | applyTint(mNightDisplayTintController, false); |
| 1605 | break; |
Christine Franks | c7fb945 | 2019-02-04 08:45:33 -0800 | [diff] [blame] | 1606 | case MSG_APPLY_DISPLAY_WHITE_BALANCE: |
| 1607 | applyTint(mDisplayWhiteBalanceTintController, false); |
| 1608 | break; |
Christine Franks | 09c229e | 2018-12-14 10:37:40 -0800 | [diff] [blame] | 1609 | } |
| 1610 | } |
| 1611 | } |
| 1612 | |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 1613 | /** |
| 1614 | * Interface for applying transforms to a given AppWindow. |
| 1615 | */ |
| 1616 | public interface ColorTransformController { |
| 1617 | |
Christine Franks | d154fe5 | 2019-01-04 17:17:45 -0800 | [diff] [blame] | 1618 | /** |
| 1619 | * Apply the given saturation (grayscale) matrix to the associated AppWindow. |
| 1620 | */ |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 1621 | void applyAppSaturation(@Size(9) float[] matrix, @Size(3) float[] translation); |
| 1622 | } |
| 1623 | |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1624 | @VisibleForTesting |
| 1625 | final class BinderService extends IColorDisplayManager.Stub { |
Christine Franks | 57fdde8 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1626 | |
Christine Franks | 39b0311 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1627 | @Override |
Christine Franks | d154fe5 | 2019-01-04 17:17:45 -0800 | [diff] [blame] | 1628 | public void setColorMode(int colorMode) { |
| 1629 | getContext().enforceCallingOrSelfPermission( |
| 1630 | Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS, |
| 1631 | "Permission required to set display color mode"); |
| 1632 | final long token = Binder.clearCallingIdentity(); |
| 1633 | try { |
| 1634 | setColorModeInternal(colorMode); |
| 1635 | } finally { |
| 1636 | Binder.restoreCallingIdentity(token); |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | @Override |
| 1641 | public int getColorMode() { |
| 1642 | final long token = Binder.clearCallingIdentity(); |
| 1643 | try { |
| 1644 | return getColorModeInternal(); |
| 1645 | } finally { |
| 1646 | Binder.restoreCallingIdentity(token); |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | @Override |
Christine Franks | 39b0311 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1651 | public boolean isDeviceColorManaged() { |
| 1652 | final long token = Binder.clearCallingIdentity(); |
| 1653 | try { |
| 1654 | return isDeviceColorManagedInternal(); |
| 1655 | } finally { |
| 1656 | Binder.restoreCallingIdentity(token); |
| 1657 | } |
| 1658 | } |
Christine Franks | 09c229e | 2018-12-14 10:37:40 -0800 | [diff] [blame] | 1659 | |
| 1660 | @Override |
| 1661 | public boolean setSaturationLevel(int level) { |
| 1662 | final boolean hasTransformsPermission = getContext() |
| 1663 | .checkCallingPermission(Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS) |
| 1664 | == PackageManager.PERMISSION_GRANTED; |
| 1665 | final boolean hasLegacyPermission = getContext() |
| 1666 | .checkCallingPermission(Manifest.permission.CONTROL_DISPLAY_SATURATION) |
| 1667 | == PackageManager.PERMISSION_GRANTED; |
| 1668 | if (!hasTransformsPermission && !hasLegacyPermission) { |
| 1669 | throw new SecurityException("Permission required to set display saturation level"); |
| 1670 | } |
| 1671 | final long token = Binder.clearCallingIdentity(); |
| 1672 | try { |
| 1673 | final Message message = mHandler.obtainMessage(MSG_APPLY_GLOBAL_SATURATION); |
| 1674 | message.arg1 = level; |
| 1675 | mHandler.sendMessage(message); |
| 1676 | } finally { |
| 1677 | Binder.restoreCallingIdentity(token); |
| 1678 | } |
| 1679 | return true; |
| 1680 | } |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 1681 | |
| 1682 | @Override |
| 1683 | public boolean setAppSaturationLevel(String packageName, int level) { |
| 1684 | getContext().enforceCallingPermission( |
| 1685 | Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS, |
| 1686 | "Permission required to set display saturation level"); |
| 1687 | final long token = Binder.clearCallingIdentity(); |
| 1688 | try { |
| 1689 | return setAppSaturationLevelInternal(packageName, level); |
| 1690 | } finally { |
| 1691 | Binder.restoreCallingIdentity(token); |
| 1692 | } |
| 1693 | } |
| 1694 | |
Christine Franks | 55194dc | 2019-01-15 13:47:06 -0800 | [diff] [blame] | 1695 | public int getTransformCapabilities() { |
| 1696 | getContext().enforceCallingPermission( |
| 1697 | Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS, |
| 1698 | "Permission required to query transform capabilities"); |
| 1699 | final long token = Binder.clearCallingIdentity(); |
| 1700 | try { |
| 1701 | return getTransformCapabilitiesInternal(); |
| 1702 | } finally { |
| 1703 | Binder.restoreCallingIdentity(token); |
| 1704 | } |
| 1705 | } |
| 1706 | |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 1707 | @Override |
Christine Franks | 83cc541 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1708 | public boolean setNightDisplayActivated(boolean activated) { |
| 1709 | getContext().enforceCallingOrSelfPermission( |
| 1710 | Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS, |
| 1711 | "Permission required to set night display activated"); |
| 1712 | final long token = Binder.clearCallingIdentity(); |
| 1713 | try { |
| 1714 | mNightDisplayTintController.setActivated(activated); |
| 1715 | return true; |
| 1716 | } finally { |
| 1717 | Binder.restoreCallingIdentity(token); |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | @Override |
| 1722 | public boolean isNightDisplayActivated() { |
| 1723 | final long token = Binder.clearCallingIdentity(); |
| 1724 | try { |
| 1725 | return mNightDisplayTintController.isActivated(); |
| 1726 | } finally { |
| 1727 | Binder.restoreCallingIdentity(token); |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | @Override |
| 1732 | public boolean setNightDisplayColorTemperature(int temperature) { |
| 1733 | getContext().enforceCallingOrSelfPermission( |
| 1734 | Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS, |
| 1735 | "Permission required to set night display temperature"); |
| 1736 | final long token = Binder.clearCallingIdentity(); |
| 1737 | try { |
| 1738 | return mNightDisplayTintController.setColorTemperature(temperature); |
| 1739 | } finally { |
| 1740 | Binder.restoreCallingIdentity(token); |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | @Override |
| 1745 | public int getNightDisplayColorTemperature() { |
| 1746 | final long token = Binder.clearCallingIdentity(); |
| 1747 | try { |
| 1748 | return mNightDisplayTintController.getColorTemperature(); |
| 1749 | } finally { |
| 1750 | Binder.restoreCallingIdentity(token); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | @Override |
| 1755 | public boolean setNightDisplayAutoMode(int autoMode) { |
| 1756 | getContext().enforceCallingOrSelfPermission( |
| 1757 | Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS, |
| 1758 | "Permission required to set night display auto mode"); |
| 1759 | final long token = Binder.clearCallingIdentity(); |
| 1760 | try { |
| 1761 | return setNightDisplayAutoModeInternal(autoMode); |
| 1762 | } finally { |
| 1763 | Binder.restoreCallingIdentity(token); |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | @Override |
| 1768 | public int getNightDisplayAutoMode() { |
| 1769 | getContext().enforceCallingOrSelfPermission( |
| 1770 | Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS, |
| 1771 | "Permission required to get night display auto mode"); |
| 1772 | final long token = Binder.clearCallingIdentity(); |
| 1773 | try { |
| 1774 | return getNightDisplayAutoModeInternal(); |
| 1775 | } finally { |
| 1776 | Binder.restoreCallingIdentity(token); |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | @Override |
| 1781 | public int getNightDisplayAutoModeRaw() { |
| 1782 | final long token = Binder.clearCallingIdentity(); |
| 1783 | try { |
| 1784 | return getNightDisplayAutoModeRawInternal(); |
| 1785 | } finally { |
| 1786 | Binder.restoreCallingIdentity(token); |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | @Override |
| 1791 | public boolean setNightDisplayCustomStartTime(Time startTime) { |
| 1792 | getContext().enforceCallingOrSelfPermission( |
| 1793 | Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS, |
| 1794 | "Permission required to set night display custom start time"); |
| 1795 | final long token = Binder.clearCallingIdentity(); |
| 1796 | try { |
| 1797 | return setNightDisplayCustomStartTimeInternal(startTime); |
| 1798 | } finally { |
| 1799 | Binder.restoreCallingIdentity(token); |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | @Override |
| 1804 | public Time getNightDisplayCustomStartTime() { |
| 1805 | final long token = Binder.clearCallingIdentity(); |
| 1806 | try { |
| 1807 | return getNightDisplayCustomStartTimeInternal(); |
| 1808 | } finally { |
| 1809 | Binder.restoreCallingIdentity(token); |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | @Override |
| 1814 | public boolean setNightDisplayCustomEndTime(Time endTime) { |
| 1815 | getContext().enforceCallingOrSelfPermission( |
| 1816 | Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS, |
| 1817 | "Permission required to set night display custom end time"); |
| 1818 | final long token = Binder.clearCallingIdentity(); |
| 1819 | try { |
| 1820 | return setNightDisplayCustomEndTimeInternal(endTime); |
| 1821 | } finally { |
| 1822 | Binder.restoreCallingIdentity(token); |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | @Override |
| 1827 | public Time getNightDisplayCustomEndTime() { |
| 1828 | final long token = Binder.clearCallingIdentity(); |
| 1829 | try { |
| 1830 | return getNightDisplayCustomEndTimeInternal(); |
| 1831 | } finally { |
| 1832 | Binder.restoreCallingIdentity(token); |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | @Override |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 1837 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Christine Franks | d154fe5 | 2019-01-04 17:17:45 -0800 | [diff] [blame] | 1838 | if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) { |
| 1839 | return; |
| 1840 | } |
Christine Franks | f3529b2 | 2019-01-03 13:20:17 -0800 | [diff] [blame] | 1841 | |
| 1842 | final long token = Binder.clearCallingIdentity(); |
| 1843 | try { |
| 1844 | dumpInternal(pw); |
| 1845 | } finally { |
| 1846 | Binder.restoreCallingIdentity(token); |
| 1847 | } |
| 1848 | } |
Christine Franks | 39b0311 | 2018-07-03 14:46:07 -0700 | [diff] [blame] | 1849 | } |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1850 | } |