blob: 83f0ef13bd2f03bee43fe6feaa10f600302c00a1 [file] [log] [blame]
Justin Klaassen911e8892016-06-21 18:24:24 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.display;
18
Christine Franks83cc5412018-07-03 14:46:07 -070019import static android.hardware.display.ColorDisplayManager.AUTO_MODE_CUSTOM_TIME;
20import static android.hardware.display.ColorDisplayManager.AUTO_MODE_DISABLED;
21import static android.hardware.display.ColorDisplayManager.AUTO_MODE_TWILIGHT;
Christine Franksd154fe52019-01-04 17:17:45 -080022import static android.hardware.display.ColorDisplayManager.COLOR_MODE_AUTOMATIC;
23import static android.hardware.display.ColorDisplayManager.COLOR_MODE_BOOSTED;
24import static android.hardware.display.ColorDisplayManager.COLOR_MODE_NATURAL;
25import static android.hardware.display.ColorDisplayManager.COLOR_MODE_SATURATED;
Christine Franksa4ed3762019-01-24 15:37:04 -080026
Christine Franks245ffd42018-11-16 13:45:14 -080027import static com.android.server.display.DisplayTransformManager.LEVEL_COLOR_MATRIX_DISPLAY_WHITE_BALANCE;
Christine Franks39b03112018-07-03 14:46:07 -070028import static com.android.server.display.DisplayTransformManager.LEVEL_COLOR_MATRIX_NIGHT_DISPLAY;
Christine Franks09c229e2018-12-14 10:37:40 -080029import static com.android.server.display.DisplayTransformManager.LEVEL_COLOR_MATRIX_SATURATION;
Christine Franks39b03112018-07-03 14:46:07 -070030
Christine Franks09c229e2018-12-14 10:37:40 -080031import android.Manifest;
Justin Klaassen639214e2016-07-14 21:00:06 -070032import android.animation.Animator;
33import android.animation.AnimatorListenerAdapter;
34import android.animation.TypeEvaluator;
35import android.animation.ValueAnimator;
Justin Klaassen4346f632016-08-08 15:01:47 -070036import android.annotation.NonNull;
Justin Klaassen908b86c2016-08-08 09:18:42 -070037import android.annotation.Nullable;
Christine Franksf3529b22019-01-03 13:20:17 -080038import android.annotation.Size;
Christine Franks55194dc2019-01-15 13:47:06 -080039import android.annotation.UserIdInt;
Justin Klaassen911e8892016-06-21 18:24:24 -070040import android.app.AlarmManager;
41import android.content.BroadcastReceiver;
42import android.content.ContentResolver;
43import android.content.Context;
44import android.content.Intent;
45import android.content.IntentFilter;
Christine Franks09c229e2018-12-14 10:37:40 -080046import android.content.pm.PackageManager;
Daniel Solomon8b72c5b2018-11-25 11:07:13 -080047import android.content.res.Resources;
Justin Klaassen2696d992016-07-11 21:26:46 -070048import android.database.ContentObserver;
Daniel Solomon8b72c5b2018-11-25 11:07:13 -080049import android.graphics.ColorSpace;
Christine Franksf3529b22019-01-03 13:20:17 -080050import android.hardware.display.ColorDisplayManager;
Christine Franks83cc5412018-07-03 14:46:07 -070051import android.hardware.display.ColorDisplayManager.AutoMode;
Christine Franksd154fe52019-01-04 17:17:45 -080052import android.hardware.display.ColorDisplayManager.ColorMode;
Christine Franks39b03112018-07-03 14:46:07 -070053import android.hardware.display.IColorDisplayManager;
Christine Franks83cc5412018-07-03 14:46:07 -070054import android.hardware.display.Time;
Justin Klaassen2696d992016-07-11 21:26:46 -070055import android.net.Uri;
Justin Klaassen639214e2016-07-14 21:00:06 -070056import android.opengl.Matrix;
Christine Franks39b03112018-07-03 14:46:07 -070057import android.os.Binder;
Justin Klaassen911e8892016-06-21 18:24:24 -070058import android.os.Handler;
Daniel Solomona4ab5672019-01-22 19:35:55 -080059import android.os.IBinder;
Justin Klaassen911e8892016-06-21 18:24:24 -070060import android.os.Looper;
Christine Franks09c229e2018-12-14 10:37:40 -080061import android.os.Message;
Christine Franksd154fe52019-01-04 17:17:45 -080062import android.os.SystemProperties;
Justin Klaassen911e8892016-06-21 18:24:24 -070063import android.os.UserHandle;
64import android.provider.Settings.Secure;
Christine Franks57fdde82018-07-03 14:46:07 -070065import android.provider.Settings.System;
Justin Klaassen639214e2016-07-14 21:00:06 -070066import android.util.MathUtils;
Justin Klaassen911e8892016-06-21 18:24:24 -070067import android.util.Slog;
Christine Franks55194dc2019-01-15 13:47:06 -080068import android.view.SurfaceControl;
Christine Franksa4ed3762019-01-24 15:37:04 -080069import android.view.SurfaceControl.DisplayPrimaries;
Christine Franks9114f462019-01-04 11:27:30 -080070import android.view.accessibility.AccessibilityManager;
Justin Klaassen639214e2016-07-14 21:00:06 -070071import android.view.animation.AnimationUtils;
Daniel Solomona4ab5672019-01-22 19:35:55 -080072
Christine Franks39b03112018-07-03 14:46:07 -070073import com.android.internal.R;
Christine Franks57fdde82018-07-03 14:46:07 -070074import com.android.internal.annotations.VisibleForTesting;
Christine Franksf3529b22019-01-03 13:20:17 -080075import com.android.internal.util.DumpUtils;
Christine Franks57fdde82018-07-03 14:46:07 -070076import com.android.server.DisplayThread;
Justin Klaassen911e8892016-06-21 18:24:24 -070077import com.android.server.SystemService;
78import com.android.server.twilight.TwilightListener;
79import com.android.server.twilight.TwilightManager;
80import com.android.server.twilight.TwilightState;
Christine Franksa4ed3762019-01-24 15:37:04 -080081
Christine Franksf3529b22019-01-03 13:20:17 -080082import java.io.FileDescriptor;
Daniel Solomon8b72c5b2018-11-25 11:07:13 -080083import java.io.PrintWriter;
Christine Franksf3529b22019-01-03 13:20:17 -080084import java.lang.ref.WeakReference;
Christine Franks57fdde82018-07-03 14:46:07 -070085import java.time.DateTimeException;
86import java.time.Instant;
Christine Franks03213462017-08-25 13:57:26 -070087import java.time.LocalDateTime;
88import java.time.LocalTime;
89import java.time.ZoneId;
Christine Franks57fdde82018-07-03 14:46:07 -070090import java.time.format.DateTimeParseException;
Christine Franks09c229e2018-12-14 10:37:40 -080091import java.util.Arrays;
Christine Franks8ad71492017-10-24 19:04:22 -070092
Justin Klaassen911e8892016-06-21 18:24:24 -070093/**
Christine Franks39b03112018-07-03 14:46:07 -070094 * Controls the display's color transforms.
Justin Klaassen911e8892016-06-21 18:24:24 -070095 */
Christine Franks57fdde82018-07-03 14:46:07 -070096public final class ColorDisplayService extends SystemService {
Justin Klaassen911e8892016-06-21 18:24:24 -070097
Christine Franks5397f032017-11-01 18:35:16 -070098 private static final String TAG = "ColorDisplayService";
Justin Klaassen911e8892016-06-21 18:24:24 -070099
100 /**
Christine Franks7b83b4282017-01-18 14:55:00 -0800101 * The transition time, in milliseconds, for Night Display to turn on/off.
102 */
103 private static final long TRANSITION_DURATION = 3000L;
104
105 /**
Justin Klaassen639214e2016-07-14 21:00:06 -0700106 * 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 Franks57fdde82018-07-03 14:46:07 -0700109
Justin Klaassen639214e2016-07-14 21:00:06 -0700110 static {
111 Matrix.setIdentityM(MATRIX_IDENTITY, 0);
112 }
113
Christine Franks09c229e2018-12-14 10:37:40 -0800114 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 Franksc7fb9452019-02-04 08:45:33 -0800117 private static final int MSG_APPLY_DISPLAY_WHITE_BALANCE = 3;
Christine Franks09c229e2018-12-14 10:37:40 -0800118
Justin Klaassen639214e2016-07-14 21:00:06 -0700119 /**
Christine Franks83cc5412018-07-03 14:46:07 -0700120 * Return value if a setting has not been set.
121 */
122 private static final int NOT_SET = -1;
123
124 /**
Justin Klaassen639214e2016-07-14 21:00:06 -0700125 * Evaluator used to animate color matrix transitions.
126 */
127 private static final ColorMatrixEvaluator COLOR_MATRIX_EVALUATOR = new ColorMatrixEvaluator();
128
Christine Franks83cc5412018-07-03 14:46:07 -0700129 private final NightDisplayTintController mNightDisplayTintController =
130 new NightDisplayTintController();
Christine Franks245ffd42018-11-16 13:45:14 -0800131
132 private final TintController mDisplayWhiteBalanceTintController = new TintController() {
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800133 // 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 Franks245ffd42018-11-16 13:45:14 -0800137
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800138 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 Franks245ffd42018-11-16 13:45:14 -0800148 private float[] mMatrixDisplayWhiteBalance = new float[16];
Christine Franksa4ed3762019-01-24 15:37:04 -0800149 private Boolean mIsAvailable;
Christine Franks245ffd42018-11-16 13:45:14 -0800150
151 @Override
152 public void setUp(Context context, boolean needsLinear) {
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800153 mSetUp = false;
Daniel Solomona4ab5672019-01-22 19:35:55 -0800154 final Resources res = context.getResources();
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800155
Daniel Solomona4ab5672019-01-22 19:35:55 -0800156 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 Solomon8b72c5b2018-11-25 11:07:13 -0800166 final String[] nominalWhiteValues = res.getStringArray(
167 R.array.config_displayWhiteBalanceDisplayNominalWhite);
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800168 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 Solomona4ab5672019-01-22 19:35:55 -0800176 Slog.e(TAG, "Display white balance minimum temperature must be greater than 0");
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800177 return;
178 }
179
180 final int colorTemperatureMax = res.getInteger(
181 R.integer.config_displayWhiteBalanceColorTemperatureMax);
182 if (colorTemperatureMax < colorTemperatureMin) {
Daniel Solomona4ab5672019-01-22 19:35:55 -0800183 Slog.e(TAG, "Display white balance max temp must be greater or equal to min");
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800184 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 Franks245ffd42018-11-16 13:45:14 -0800200 }
201
202 @Override
203 public float[] getMatrix() {
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800204 return mSetUp && isActivated() ? mMatrixDisplayWhiteBalance : MATRIX_IDENTITY;
Christine Franks245ffd42018-11-16 13:45:14 -0800205 }
206
207 @Override
208 public void setMatrix(int cct) {
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800209 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 Franksd154fe52019-01-04 17:17:45 -0800231 ColorSpace.chromaticAdaptation(ColorSpace.Adaptation.BRADFORD,
232 mDisplayNominalWhiteXYZ, mCurrentColorTemperatureXYZ);
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800233
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 Franks245ffd42018-11-16 13:45:14 -0800253 }
254
255 @Override
256 public int getLevel() {
257 return LEVEL_COLOR_MATRIX_DISPLAY_WHITE_BALANCE;
258 }
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800259
Christine Franksa4ed3762019-01-24 15:37:04 -0800260 @Override
261 public boolean isAvailable(Context context) {
262 if (mIsAvailable == null) {
263 mIsAvailable = ColorDisplayManager.isDisplayWhiteBalanceAvailable(context);
264 }
265 return mIsAvailable;
266 }
267
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800268 /**
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 Solomon8b72c5b2018-11-25 11:07:13 -0800293 pw.println(" mSetUp = " + mSetUp);
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800294 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 Solomona4ab5672019-01-22 19:35:55 -0800315
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 Laskowski3316a0a2019-01-25 02:56:41 -0800326 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
Daniel Solomona4ab5672019-01-22 19:35:55 -0800327 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 Franks245ffd42018-11-16 13:45:14 -0800365 };
366
Christine Franks09c229e2018-12-14 10:37:40 -0800367 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 Franks83cc5412018-07-03 14:46:07 -0700395 0.072f * desaturation};
Christine Franks09c229e2018-12-14 10:37:40 -0800396 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 Franksa4ed3762019-01-24 15:37:04 -0800412
413 @Override
414 public boolean isAvailable(Context context) {
415 return ColorDisplayManager.isColorTransformAccelerated(context);
416 }
Christine Franks09c229e2018-12-14 10:37:40 -0800417 };
418
Christine Franks9114f462019-01-04 11:27:30 -0800419 /**
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 Franksd154fe52019-01-04 17:17:45 -0800435 private static final float[] MATRIX_INVERT_COLOR = new float[]{
Christine Franks9114f462019-01-04 11:27:30 -0800436 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 Klaassen2696d992016-07-11 21:26:46 -0700442 private final Handler mHandler;
443
Christine Franksf3529b22019-01-03 13:20:17 -0800444 private final AppSaturationController mAppSaturationController = new AppSaturationController();
445
Justin Klaassen911e8892016-06-21 18:24:24 -0700446 private int mCurrentUser = UserHandle.USER_NULL;
Justin Klaassen2696d992016-07-11 21:26:46 -0700447 private ContentObserver mUserSetupObserver;
Justin Klaassen911e8892016-06-21 18:24:24 -0700448 private boolean mBootCompleted;
449
Christine Franks57fdde82018-07-03 14:46:07 -0700450 private ContentObserver mContentObserver;
Christine Franks57fdde82018-07-03 14:46:07 -0700451
Christine Franks245ffd42018-11-16 13:45:14 -0800452 private DisplayWhiteBalanceListener mDisplayWhiteBalanceListener;
453
Christine Franks57fdde82018-07-03 14:46:07 -0700454 private NightDisplayAutoMode mNightDisplayAutoMode;
Justin Klaassen911e8892016-06-21 18:24:24 -0700455
Christine Franks5397f032017-11-01 18:35:16 -0700456 public ColorDisplayService(Context context) {
Justin Klaassen911e8892016-06-21 18:24:24 -0700457 super(context);
Christine Franks83cc5412018-07-03 14:46:07 -0700458 mHandler = new TintHandler(DisplayThread.get().getLooper());
Justin Klaassen911e8892016-06-21 18:24:24 -0700459 }
460
461 @Override
462 public void onStart() {
Christine Franks39b03112018-07-03 14:46:07 -0700463 publishBinderService(Context.COLOR_DISPLAY_SERVICE, new BinderService());
Christine Franks245ffd42018-11-16 13:45:14 -0800464 publishLocalService(ColorDisplayServiceInternal.class, new ColorDisplayServiceInternal());
Justin Klaassen911e8892016-06-21 18:24:24 -0700465 }
466
467 @Override
Justin Klaassen2696d992016-07-11 21:26:46 -0700468 public void onBootPhase(int phase) {
Christine Frankse5bb03e2017-02-10 17:36:10 -0800469 if (phase >= PHASE_BOOT_COMPLETED) {
Justin Klaassen2696d992016-07-11 21:26:46 -0700470 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 Klaassen911e8892016-06-21 18:24:24 -0700480 public void onStartUser(int userHandle) {
481 super.onStartUser(userHandle);
482
Justin Klaassen911e8892016-06-21 18:24:24 -0700483 if (mCurrentUser == UserHandle.USER_NULL) {
Justin Klaassen2696d992016-07-11 21:26:46 -0700484 onUserChanged(userHandle);
Justin Klaassen911e8892016-06-21 18:24:24 -0700485 }
486 }
487
488 @Override
489 public void onSwitchUser(int userHandle) {
490 super.onSwitchUser(userHandle);
491
Justin Klaassen2696d992016-07-11 21:26:46 -0700492 onUserChanged(userHandle);
Justin Klaassen911e8892016-06-21 18:24:24 -0700493 }
494
495 @Override
496 public void onStopUser(int userHandle) {
497 super.onStopUser(userHandle);
498
Justin Klaassen911e8892016-06-21 18:24:24 -0700499 if (mCurrentUser == userHandle) {
Justin Klaassen2696d992016-07-11 21:26:46 -0700500 onUserChanged(UserHandle.USER_NULL);
Justin Klaassen911e8892016-06-21 18:24:24 -0700501 }
502 }
503
Justin Klaassen2696d992016-07-11 21:26:46 -0700504 private void onUserChanged(int userHandle) {
505 final ContentResolver cr = getContext().getContentResolver();
Justin Klaassen911e8892016-06-21 18:24:24 -0700506
Justin Klaassen2696d992016-07-11 21:26:46 -0700507 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 Franks39b03112018-07-03 14:46:07 -0700534 false /* notifyForDescendants */, mUserSetupObserver, mCurrentUser);
Justin Klaassen2696d992016-07-11 21:26:46 -0700535 } else if (mBootCompleted) {
536 setUp();
Justin Klaassen911e8892016-06-21 18:24:24 -0700537 }
538 }
539 }
540
Justin Klaassen2696d992016-07-11 21:26:46 -0700541 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 Klaassenec8837a2016-08-23 12:04:42 -0700546 Slog.d(TAG, "setUp: currentUser=" + mCurrentUser);
547
Christine Franks57fdde82018-07-03 14:46:07 -0700548 // 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 Franks78a4dd42019-02-08 11:09:30 -0800559 final boolean activated = mNightDisplayTintController
560 .isActivatedSetting();
Christine Franks83cc5412018-07-03 14:46:07 -0700561 if (mNightDisplayTintController.isActivatedStateNotSet()
562 || mNightDisplayTintController.isActivated() != activated) {
Christine Franks78a4dd42019-02-08 11:09:30 -0800563 mNightDisplayTintController.setActivated(activated);
Christine Franks83cc5412018-07-03 14:46:07 -0700564 }
Christine Franks57fdde82018-07-03 14:46:07 -0700565 break;
566 case Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE:
Christine Franks78a4dd42019-02-08 11:09:30 -0800567 final int temperature = mNightDisplayTintController
568 .getColorTemperatureSetting();
Christine Franks83cc5412018-07-03 14:46:07 -0700569 if (mNightDisplayTintController.getColorTemperature()
570 != temperature) {
571 mNightDisplayTintController
572 .onColorTemperatureChanged(temperature);
573 }
Christine Franks57fdde82018-07-03 14:46:07 -0700574 break;
575 case Secure.NIGHT_DISPLAY_AUTO_MODE:
Christine Franks83cc5412018-07-03 14:46:07 -0700576 onNightDisplayAutoModeChanged(getNightDisplayAutoModeInternal());
Christine Franks57fdde82018-07-03 14:46:07 -0700577 break;
578 case Secure.NIGHT_DISPLAY_CUSTOM_START_TIME:
579 onNightDisplayCustomStartTimeChanged(
Christine Franks83cc5412018-07-03 14:46:07 -0700580 getNightDisplayCustomStartTimeInternal().getLocalTime());
Christine Franks57fdde82018-07-03 14:46:07 -0700581 break;
582 case Secure.NIGHT_DISPLAY_CUSTOM_END_TIME:
583 onNightDisplayCustomEndTimeChanged(
Christine Franks83cc5412018-07-03 14:46:07 -0700584 getNightDisplayCustomEndTimeInternal().getLocalTime());
Christine Franks57fdde82018-07-03 14:46:07 -0700585 break;
586 case System.DISPLAY_COLOR_MODE:
Christine Franks71e003e2019-01-24 14:40:20 -0800587 onDisplayColorModeChanged(getColorModeInternal());
Christine Franks57fdde82018-07-03 14:46:07 -0700588 break;
Christine Franks57fdde82018-07-03 14:46:07 -0700589 case Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED:
Christine Franks9114f462019-01-04 11:27:30 -0800590 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 Franks57fdde82018-07-03 14:46:07 -0700599 break;
Christine Franks245ffd42018-11-16 13:45:14 -0800600 case Secure.DISPLAY_WHITE_BALANCE_ENABLED:
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800601 updateDisplayWhiteBalanceStatus();
Christine Franks245ffd42018-11-16 13:45:14 -0800602 break;
Christine Franks57fdde82018-07-03 14:46:07 -0700603 }
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 Franks9114f462019-01-04 11:27:30 -0800627 cr.registerContentObserver(
628 Secure.getUriFor(Secure.ACCESSIBILITY_DISPLAY_DALTONIZER),
629 false /* notifyForDescendants */, mContentObserver, mCurrentUser);
Christine Franks245ffd42018-11-16 13:45:14 -0800630 cr.registerContentObserver(Secure.getUriFor(Secure.DISPLAY_WHITE_BALANCE_ENABLED),
631 false /* notifyForDescendants */, mContentObserver, mCurrentUser);
Justin Klaassen911e8892016-06-21 18:24:24 -0700632
Christine Frankscf388c22018-05-15 15:48:10 -0700633 // 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 Franks71e003e2019-01-24 14:40:20 -0800635 onDisplayColorModeChanged(getColorModeInternal());
Christine Frankscf388c22018-05-15 15:48:10 -0700636
Christine Franksa4ed3762019-01-24 15:37:04 -0800637 if (mNightDisplayTintController.isAvailable(getContext())) {
Christine Franks245ffd42018-11-16 13:45:14 -0800638 // Reset the activated state.
639 mNightDisplayTintController.setActivated(null);
Christine Frankscf388c22018-05-15 15:48:10 -0700640
Christine Franks245ffd42018-11-16 13:45:14 -0800641 // Prepare the night display color transformation matrix.
642 mNightDisplayTintController
643 .setUp(getContext(), DisplayTransformManager.needsLinearColorMatrix());
Christine Franks78a4dd42019-02-08 11:09:30 -0800644 mNightDisplayTintController
645 .setMatrix(mNightDisplayTintController.getColorTemperatureSetting());
Christine Franks8ad71492017-10-24 19:04:22 -0700646
Christine Franks245ffd42018-11-16 13:45:14 -0800647 // Initialize the current auto mode.
Christine Franks83cc5412018-07-03 14:46:07 -0700648 onNightDisplayAutoModeChanged(getNightDisplayAutoModeInternal());
Christine Franks6418d0b2017-02-13 09:48:00 -0800649
Christine Franks83cc5412018-07-03 14:46:07 -0700650 // Force the initialization of the current saved activation state.
Christine Franks245ffd42018-11-16 13:45:14 -0800651 if (mNightDisplayTintController.isActivatedStateNotSet()) {
Christine Franks78a4dd42019-02-08 11:09:30 -0800652 mNightDisplayTintController
653 .setActivated(mNightDisplayTintController.isActivatedSetting());
Christine Franks245ffd42018-11-16 13:45:14 -0800654 }
655 }
Justin Klaassen911e8892016-06-21 18:24:24 -0700656
Christine Franksa4ed3762019-01-24 15:37:04 -0800657 if (mDisplayWhiteBalanceTintController.isAvailable(getContext())) {
Christine Franks245ffd42018-11-16 13:45:14 -0800658 // Prepare the display white balance transform matrix.
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800659 mDisplayWhiteBalanceTintController.setUp(getContext(), true /* needsLinear */);
Christine Franks245ffd42018-11-16 13:45:14 -0800660
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800661 updateDisplayWhiteBalanceStatus();
Justin Klaassen911e8892016-06-21 18:24:24 -0700662 }
663 }
664
Justin Klaassen2696d992016-07-11 21:26:46 -0700665 private void tearDown() {
Justin Klaassenec8837a2016-08-23 12:04:42 -0700666 Slog.d(TAG, "tearDown: currentUser=" + mCurrentUser);
667
Christine Franks57fdde82018-07-03 14:46:07 -0700668 getContext().getContentResolver().unregisterContentObserver(mContentObserver);
669
Christine Franksa4ed3762019-01-24 15:37:04 -0800670 if (mNightDisplayTintController.isAvailable(getContext())) {
Christine Franks245ffd42018-11-16 13:45:14 -0800671 if (mNightDisplayAutoMode != null) {
672 mNightDisplayAutoMode.onStop();
673 mNightDisplayAutoMode = null;
674 }
675 mNightDisplayTintController.endAnimator();
Justin Klaassen911e8892016-06-21 18:24:24 -0700676 }
677
Christine Franksa4ed3762019-01-24 15:37:04 -0800678 if (mDisplayWhiteBalanceTintController.isAvailable(getContext())) {
Christine Franks245ffd42018-11-16 13:45:14 -0800679 mDisplayWhiteBalanceTintController.endAnimator();
Justin Klaassen639214e2016-07-14 21:00:06 -0700680 }
Justin Klaassen911e8892016-06-21 18:24:24 -0700681 }
682
Christine Franks57fdde82018-07-03 14:46:07 -0700683 private void onNightDisplayAutoModeChanged(int autoMode) {
684 Slog.d(TAG, "onNightDisplayAutoModeChanged: autoMode=" + autoMode);
Justin Klaassenec8837a2016-08-23 12:04:42 -0700685
Christine Franks57fdde82018-07-03 14:46:07 -0700686 if (mNightDisplayAutoMode != null) {
687 mNightDisplayAutoMode.onStop();
688 mNightDisplayAutoMode = null;
Justin Klaassen911e8892016-06-21 18:24:24 -0700689 }
690
Christine Franks83cc5412018-07-03 14:46:07 -0700691 if (autoMode == AUTO_MODE_CUSTOM_TIME) {
Christine Franks57fdde82018-07-03 14:46:07 -0700692 mNightDisplayAutoMode = new CustomNightDisplayAutoMode();
Christine Franks83cc5412018-07-03 14:46:07 -0700693 } else if (autoMode == AUTO_MODE_TWILIGHT) {
Christine Franks57fdde82018-07-03 14:46:07 -0700694 mNightDisplayAutoMode = new TwilightNightDisplayAutoMode();
Justin Klaassen911e8892016-06-21 18:24:24 -0700695 }
696
Christine Franks57fdde82018-07-03 14:46:07 -0700697 if (mNightDisplayAutoMode != null) {
698 mNightDisplayAutoMode.onStart();
Justin Klaassen911e8892016-06-21 18:24:24 -0700699 }
700 }
701
Christine Franks57fdde82018-07-03 14:46:07 -0700702 private void onNightDisplayCustomStartTimeChanged(LocalTime startTime) {
703 Slog.d(TAG, "onNightDisplayCustomStartTimeChanged: startTime=" + startTime);
Justin Klaassenec8837a2016-08-23 12:04:42 -0700704
Christine Franks57fdde82018-07-03 14:46:07 -0700705 if (mNightDisplayAutoMode != null) {
706 mNightDisplayAutoMode.onCustomStartTimeChanged(startTime);
Justin Klaassen911e8892016-06-21 18:24:24 -0700707 }
708 }
709
Christine Franks57fdde82018-07-03 14:46:07 -0700710 private void onNightDisplayCustomEndTimeChanged(LocalTime endTime) {
711 Slog.d(TAG, "onNightDisplayCustomEndTimeChanged: endTime=" + endTime);
Justin Klaassenec8837a2016-08-23 12:04:42 -0700712
Christine Franks57fdde82018-07-03 14:46:07 -0700713 if (mNightDisplayAutoMode != null) {
714 mNightDisplayAutoMode.onCustomEndTimeChanged(endTime);
Justin Klaassen911e8892016-06-21 18:24:24 -0700715 }
716 }
717
Christine Franks57fdde82018-07-03 14:46:07 -0700718 private void onDisplayColorModeChanged(int mode) {
Christine Franks83cc5412018-07-03 14:46:07 -0700719 if (mode == NOT_SET) {
Christine Frankscf388c22018-05-15 15:48:10 -0700720 return;
721 }
722
Christine Franks245ffd42018-11-16 13:45:14 -0800723 mNightDisplayTintController.cancelAnimator();
724 mDisplayWhiteBalanceTintController.cancelAnimator();
725
Christine Franksa4ed3762019-01-24 15:37:04 -0800726 if (mNightDisplayTintController.isAvailable(getContext())) {
727 mNightDisplayTintController
728 .setUp(getContext(), DisplayTransformManager.needsLinearColorMatrix(mode));
Christine Franks78a4dd42019-02-08 11:09:30 -0800729 mNightDisplayTintController
730 .setMatrix(mNightDisplayTintController.getColorTemperatureSetting());
Christine Franksa4ed3762019-01-24 15:37:04 -0800731 }
Christine Franks245ffd42018-11-16 13:45:14 -0800732
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800733 updateDisplayWhiteBalanceStatus();
Christine Franks8ad71492017-10-24 19:04:22 -0700734
Christine Franks218e6562017-11-27 10:20:14 -0800735 final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class);
Christine Franks245ffd42018-11-16 13:45:14 -0800736 dtm.setColorMode(mode, mNightDisplayTintController.getMatrix());
Christine Franks8ad71492017-10-24 19:04:22 -0700737 }
738
Christine Franks9114f462019-01-04 11:27:30 -0800739 private void onAccessibilityActivated() {
Christine Franks71e003e2019-01-24 14:40:20 -0800740 onDisplayColorModeChanged(getColorModeInternal());
Daniel Solomon317a3572018-03-30 18:36:37 -0700741 }
742
Christine Franks9114f462019-01-04 11:27:30 -0800743 /**
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 Franks8ad71492017-10-24 19:04:22 -0700776
Christine Franks6418d0b2017-02-13 09:48:00 -0800777 /**
778 * Applies current color temperature matrix, or removes it if deactivated.
779 *
780 * @param immediate {@code true} skips transition animation
781 */
Christine Franks245ffd42018-11-16 13:45:14 -0800782 private void applyTint(TintController tintController, boolean immediate) {
783 tintController.cancelAnimator();
Christine Franks6418d0b2017-02-13 09:48:00 -0800784
Christine Franks6418d0b2017-02-13 09:48:00 -0800785 final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class);
Christine Franks245ffd42018-11-16 13:45:14 -0800786 final float[] from = dtm.getColorMatrix(tintController.getLevel());
787 final float[] to = tintController.getMatrix();
Christine Franks6418d0b2017-02-13 09:48:00 -0800788
789 if (immediate) {
Christine Franks245ffd42018-11-16 13:45:14 -0800790 dtm.setColorMatrix(tintController.getLevel(), to);
Christine Franks6418d0b2017-02-13 09:48:00 -0800791 } else {
Christine Franks245ffd42018-11-16 13:45:14 -0800792 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 Franks6418d0b2017-02-13 09:48:00 -0800796 getContext(), android.R.interpolator.fast_out_slow_in));
Christine Franks245ffd42018-11-16 13:45:14 -0800797 tintController.getAnimator().addUpdateListener((ValueAnimator animator) -> {
798 final float[] value = (float[]) animator.getAnimatedValue();
799 dtm.setColorMatrix(tintController.getLevel(), value);
Christine Franks6418d0b2017-02-13 09:48:00 -0800800 });
Christine Franks245ffd42018-11-16 13:45:14 -0800801 tintController.getAnimator().addListener(new AnimatorListenerAdapter() {
Christine Franks6418d0b2017-02-13 09:48:00 -0800802
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 Franks245ffd42018-11-16 13:45:14 -0800816 dtm.setColorMatrix(tintController.getLevel(), to);
Christine Franks6418d0b2017-02-13 09:48:00 -0800817 }
Christine Franks245ffd42018-11-16 13:45:14 -0800818 tintController.setAnimator(null);
Christine Franks6418d0b2017-02-13 09:48:00 -0800819 }
820 });
Christine Franks245ffd42018-11-16 13:45:14 -0800821 tintController.getAnimator().start();
Christine Franks6418d0b2017-02-13 09:48:00 -0800822 }
823 }
824
825 /**
Christine Franks39b03112018-07-03 14:46:07 -0700826 * Returns the first date time corresponding to the local time that occurs before the provided
827 * date time.
Christine Franks03213462017-08-25 13:57:26 -0700828 *
829 * @param compareTime the LocalDateTime to compare against
830 * @return the prior LocalDateTime corresponding to this local time
831 */
Christine Franks57fdde82018-07-03 14:46:07 -0700832 @VisibleForTesting
833 static LocalDateTime getDateTimeBefore(LocalTime localTime, LocalDateTime compareTime) {
Christine Franks03213462017-08-25 13:57:26 -0700834 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 Franks39b03112018-07-03 14:46:07 -0700842 * Returns the first date time corresponding to this local time that occurs after the provided
843 * date time.
Christine Franks03213462017-08-25 13:57:26 -0700844 *
845 * @param compareTime the LocalDateTime to compare against
846 * @return the next LocalDateTime corresponding to this local time
847 */
Christine Franks57fdde82018-07-03 14:46:07 -0700848 @VisibleForTesting
849 static LocalDateTime getDateTimeAfter(LocalTime localTime, LocalDateTime compareTime) {
Christine Franks03213462017-08-25 13:57:26 -0700850 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 Solomon8b72c5b2018-11-25 11:07:13 -0800857 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 Franks245ffd42018-11-16 13:45:14 -0800866 }
Daniel Solomon86508f82019-01-18 19:01:02 -0800867
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 Franksc7fb9452019-02-04 08:45:33 -0800871 mHandler.sendEmptyMessage(MSG_APPLY_DISPLAY_WHITE_BALANCE);
Daniel Solomon86508f82019-01-18 19:01:02 -0800872 }
Christine Franks245ffd42018-11-16 13:45:14 -0800873 }
874
875 private boolean isDisplayWhiteBalanceSettingEnabled() {
876 return Secure.getIntForUser(getContext().getContentResolver(),
877 Secure.DISPLAY_WHITE_BALANCE_ENABLED, 0, mCurrentUser) == 1;
878 }
879
Christine Franks39b03112018-07-03 14:46:07 -0700880 private boolean isDeviceColorManagedInternal() {
881 final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class);
882 return dtm.isDeviceColorManaged();
883 }
884
Christine Franks55194dc2019-01-15 13:47:06 -0800885 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 Franks83cc5412018-07-03 14:46:07 -0700900 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 Franks57fdde82018-07-03 14:46:07 -0700965 /**
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 Franks245ffd42018-11-16 13:45:14 -0800969 private LocalDateTime getNightDisplayLastActivatedTimeSetting() {
Christine Franks57fdde82018-07-03 14:46:07 -0700970 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 Franksf3529b22019-01-03 13:20:17 -0800989 private boolean setAppSaturationLevelInternal(String packageName, int saturationLevel) {
990 return mAppSaturationController
991 .setSaturationLevel(packageName, mCurrentUser, saturationLevel);
992 }
993
Christine Franksd154fe52019-01-04 17:17:45 -0800994 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 Franks71e003e2019-01-24 14:40:20 -08001003 private @ColorMode int getColorModeInternal() {
Christine Franksd154fe52019-01-04 17:17:45 -08001004 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 Franks78a4dd42019-02-08 11:09:30 -08001048 private @ColorMode int getCurrentColorModeFromSystemProperties() {
Christine Franksd154fe52019-01-04 17:17:45 -08001049 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 Franksf3529b22019-01-03 13:20:17 -08001075 private void dumpInternal(PrintWriter pw) {
1076 pw.println("COLOR DISPLAY MANAGER dumpsys (color_display)");
Christine Franksa4ed3762019-01-24 15:37:04 -08001077
Christine Franksf3529b22019-01-03 13:20:17 -08001078 pw.println("Night Display:");
Christine Franksa4ed3762019-01-24 15:37:04 -08001079 if (mNightDisplayTintController.isAvailable(getContext())) {
Christine Franksf3529b22019-01-03 13:20:17 -08001080 pw.println(" Activated: " + mNightDisplayTintController.isActivated());
Christine Franksa4ed3762019-01-24 15:37:04 -08001081 pw.println(" Color temp: " + mNightDisplayTintController.getColorTemperature());
Christine Franksf3529b22019-01-03 13:20:17 -08001082 } else {
1083 pw.println(" Not available");
1084 }
Christine Franksa4ed3762019-01-24 15:37:04 -08001085
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 Franksf3529b22019-01-03 13:20:17 -08001093 mAppSaturationController.dump(pw);
Christine Franksa4ed3762019-01-24 15:37:04 -08001094
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 Franksf3529b22019-01-03 13:20:17 -08001103 }
1104
Christine Franks57fdde82018-07-03 14:46:07 -07001105 private abstract class NightDisplayAutoMode {
1106
1107 public abstract void onActivated(boolean activated);
1108
Justin Klaassen911e8892016-06-21 18:24:24 -07001109 public abstract void onStart();
Christine Frankse5bb03e2017-02-10 17:36:10 -08001110
Justin Klaassen911e8892016-06-21 18:24:24 -07001111 public abstract void onStop();
Christine Franks57fdde82018-07-03 14:46:07 -07001112
1113 public void onCustomStartTimeChanged(LocalTime startTime) {
1114 }
1115
1116 public void onCustomEndTimeChanged(LocalTime endTime) {
1117 }
Justin Klaassen911e8892016-06-21 18:24:24 -07001118 }
1119
Christine Franks57fdde82018-07-03 14:46:07 -07001120 private final class CustomNightDisplayAutoMode extends NightDisplayAutoMode implements
1121 AlarmManager.OnAlarmListener {
Justin Klaassen911e8892016-06-21 18:24:24 -07001122
1123 private final AlarmManager mAlarmManager;
1124 private final BroadcastReceiver mTimeChangedReceiver;
1125
Christine Franks03213462017-08-25 13:57:26 -07001126 private LocalTime mStartTime;
1127 private LocalTime mEndTime;
Justin Klaassen911e8892016-06-21 18:24:24 -07001128
Christine Franks03213462017-08-25 13:57:26 -07001129 private LocalDateTime mLastActivatedTime;
Justin Klaassen911e8892016-06-21 18:24:24 -07001130
Christine Franks57fdde82018-07-03 14:46:07 -07001131 CustomNightDisplayAutoMode() {
Justin Klaassen911e8892016-06-21 18:24:24 -07001132 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 Franks03213462017-08-25 13:57:26 -07001142 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 Klaassen911e8892016-06-21 18:24:24 -07001146
Christine Frankse5bb03e2017-02-10 17:36:10 -08001147 if (mLastActivatedTime != null) {
Christine Frankse5bb03e2017-02-10 17:36:10 -08001148 // Maintain the existing activated state if within the current period.
Christine Franks03213462017-08-25 13:57:26 -07001149 if (mLastActivatedTime.isBefore(now) && mLastActivatedTime.isAfter(start)
1150 && (mLastActivatedTime.isAfter(end) || now.isBefore(end))) {
Christine Franks78a4dd42019-02-08 11:09:30 -08001151 activate = mNightDisplayTintController.isActivatedSetting();
Justin Klaassen911e8892016-06-21 18:24:24 -07001152 }
1153 }
1154
Christine Franks245ffd42018-11-16 13:45:14 -08001155 if (mNightDisplayTintController.isActivatedStateNotSet() || (
1156 mNightDisplayTintController.isActivated() != activate)) {
Christine Franks83cc5412018-07-03 14:46:07 -07001157 mNightDisplayTintController.setActivated(activate);
Justin Klaassen911e8892016-06-21 18:24:24 -07001158 }
Christine Franks03213462017-08-25 13:57:26 -07001159
Christine Franks245ffd42018-11-16 13:45:14 -08001160 updateNextAlarm(mNightDisplayTintController.isActivated(), now);
Justin Klaassen911e8892016-06-21 18:24:24 -07001161 }
1162
Christine Franks03213462017-08-25 13:57:26 -07001163 private void updateNextAlarm(@Nullable Boolean activated, @NonNull LocalDateTime now) {
Justin Klaassen4346f632016-08-08 15:01:47 -07001164 if (activated != null) {
Christine Franks03213462017-08-25 13:57:26 -07001165 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 Klaassen911e8892016-06-21 18:24:24 -07001169 }
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 Franks83cc5412018-07-03 14:46:07 -07001178 mStartTime = getNightDisplayCustomStartTimeInternal().getLocalTime();
1179 mEndTime = getNightDisplayCustomEndTimeInternal().getLocalTime();
Justin Klaassen911e8892016-06-21 18:24:24 -07001180
Christine Franks57fdde82018-07-03 14:46:07 -07001181 mLastActivatedTime = getNightDisplayLastActivatedTimeSetting();
Christine Frankse5bb03e2017-02-10 17:36:10 -08001182
Justin Klaassen911e8892016-06-21 18:24:24 -07001183 // 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 Franks57fdde82018-07-03 14:46:07 -07001197 mLastActivatedTime = getNightDisplayLastActivatedTimeSetting();
Christine Franks03213462017-08-25 13:57:26 -07001198 updateNextAlarm(activated, LocalDateTime.now());
Justin Klaassen911e8892016-06-21 18:24:24 -07001199 }
1200
1201 @Override
Christine Franks03213462017-08-25 13:57:26 -07001202 public void onCustomStartTimeChanged(LocalTime startTime) {
Justin Klaassen911e8892016-06-21 18:24:24 -07001203 mStartTime = startTime;
1204 mLastActivatedTime = null;
1205 updateActivated();
1206 }
1207
1208 @Override
Christine Franks03213462017-08-25 13:57:26 -07001209 public void onCustomEndTimeChanged(LocalTime endTime) {
Justin Klaassen911e8892016-06-21 18:24:24 -07001210 mEndTime = endTime;
1211 mLastActivatedTime = null;
1212 updateActivated();
1213 }
1214
1215 @Override
1216 public void onAlarm() {
Justin Klaassenec8837a2016-08-23 12:04:42 -07001217 Slog.d(TAG, "onAlarm");
Justin Klaassen911e8892016-06-21 18:24:24 -07001218 updateActivated();
1219 }
1220 }
1221
Christine Franks57fdde82018-07-03 14:46:07 -07001222 private final class TwilightNightDisplayAutoMode extends NightDisplayAutoMode implements
1223 TwilightListener {
Justin Klaassen911e8892016-06-21 18:24:24 -07001224
1225 private final TwilightManager mTwilightManager;
Christine Franks57fdde82018-07-03 14:46:07 -07001226 private LocalDateTime mLastActivatedTime;
Justin Klaassen911e8892016-06-21 18:24:24 -07001227
Christine Franks57fdde82018-07-03 14:46:07 -07001228 TwilightNightDisplayAutoMode() {
Justin Klaassen911e8892016-06-21 18:24:24 -07001229 mTwilightManager = getLocalService(TwilightManager.class);
Justin Klaassen911e8892016-06-21 18:24:24 -07001230 }
1231
Justin Klaassen908b86c2016-08-08 09:18:42 -07001232 private void updateActivated(TwilightState state) {
Justin Klaassen3da4c442017-05-05 15:19:33 -07001233 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 Franks57fdde82018-07-03 14:46:07 -07001240 if (mLastActivatedTime != null) {
Christine Franks03213462017-08-25 13:57:26 -07001241 final LocalDateTime now = LocalDateTime.now();
1242 final LocalDateTime sunrise = state.sunrise();
1243 final LocalDateTime sunset = state.sunset();
Christine Frankse5bb03e2017-02-10 17:36:10 -08001244 // Maintain the existing activated state if within the current period.
Christine Franks57fdde82018-07-03 14:46:07 -07001245 if (mLastActivatedTime.isBefore(now) && (mLastActivatedTime.isBefore(sunrise)
1246 ^ mLastActivatedTime.isBefore(sunset))) {
Christine Franks78a4dd42019-02-08 11:09:30 -08001247 activate = mNightDisplayTintController.isActivatedSetting();
Justin Klaassen911e8892016-06-21 18:24:24 -07001248 }
1249 }
Justin Klaassen908b86c2016-08-08 09:18:42 -07001250
Christine Franks245ffd42018-11-16 13:45:14 -08001251 if (mNightDisplayTintController.isActivatedStateNotSet() || (
1252 mNightDisplayTintController.isActivated() != activate)) {
Christine Franks83cc5412018-07-03 14:46:07 -07001253 mNightDisplayTintController.setActivated(activate);
Justin Klaassen908b86c2016-08-08 09:18:42 -07001254 }
Justin Klaassen911e8892016-06-21 18:24:24 -07001255 }
1256
1257 @Override
Christine Franks57fdde82018-07-03 14:46:07 -07001258 public void onActivated(boolean activated) {
1259 mLastActivatedTime = getNightDisplayLastActivatedTimeSetting();
1260 }
1261
1262 @Override
Justin Klaassen911e8892016-06-21 18:24:24 -07001263 public void onStart() {
1264 mTwilightManager.registerListener(this, mHandler);
Christine Franks57fdde82018-07-03 14:46:07 -07001265 mLastActivatedTime = getNightDisplayLastActivatedTimeSetting();
Justin Klaassen911e8892016-06-21 18:24:24 -07001266
1267 // Force an update to initialize state.
Justin Klaassen908b86c2016-08-08 09:18:42 -07001268 updateActivated(mTwilightManager.getLastTwilightState());
Justin Klaassen911e8892016-06-21 18:24:24 -07001269 }
1270
1271 @Override
1272 public void onStop() {
1273 mTwilightManager.unregisterListener(this);
Christine Franks57fdde82018-07-03 14:46:07 -07001274 mLastActivatedTime = null;
Justin Klaassen908b86c2016-08-08 09:18:42 -07001275 }
1276
1277 @Override
1278 public void onTwilightStateChanged(@Nullable TwilightState state) {
Justin Klaassenec8837a2016-08-23 12:04:42 -07001279 Slog.d(TAG, "onTwilightStateChanged: isNight="
1280 + (state == null ? null : state.isNight()));
Justin Klaassen908b86c2016-08-08 09:18:42 -07001281 updateActivated(state);
Justin Klaassen911e8892016-06-21 18:24:24 -07001282 }
1283 }
Justin Klaassen639214e2016-07-14 21:00:06 -07001284
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 Franks39b03112018-07-03 14:46:07 -07001303
Christine Franks245ffd42018-11-16 13:45:14 -08001304 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 Solomon8b72c5b2018-11-25 11:07:13 -08001349 * Dump debug information.
1350 */
1351 public void dump(PrintWriter pw) {
1352 }
1353
1354 /**
Christine Franks245ffd42018-11-16 13:45:14 -08001355 * 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 Franksa4ed3762019-01-24 15:37:04 -08001373
1374 /**
1375 * Returns whether or not this transform type is available on this device.
1376 */
1377 public abstract boolean isAvailable(Context context);
Christine Franks245ffd42018-11-16 13:45:14 -08001378 }
1379
Christine Franks83cc5412018-07-03 14:46:07 -07001380 private final class NightDisplayTintController extends TintController {
1381
Christine Franksa4ed3762019-01-24 15:37:04 -08001382 private final float[] mMatrix = new float[16];
Christine Franks83cc5412018-07-03 14:46:07 -07001383 private final float[] mColorTempCoefficients = new float[9];
Christine Franksa4ed3762019-01-24 15:37:04 -08001384
1385 private Boolean mIsAvailable;
Christine Franks83cc5412018-07-03 14:46:07 -07001386 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 Franks78a4dd42019-02-08 11:09:30 -08001446 if (isActivatedSetting() != activated) {
1447 Secure.putIntForUser(getContext().getContentResolver(),
1448 Secure.NIGHT_DISPLAY_ACTIVATED,
1449 activated ? 1 : 0, mCurrentUser);
1450 }
Christine Franks83cc5412018-07-03 14:46:07 -07001451 onActivated(activated);
1452 }
1453 }
1454
1455 @Override
1456 public int getLevel() {
1457 return LEVEL_COLOR_MATRIX_NIGHT_DISPLAY;
1458 }
1459
Christine Franksa4ed3762019-01-24 15:37:04 -08001460 @Override
1461 public boolean isAvailable(Context context) {
1462 if (mIsAvailable == null) {
1463 mIsAvailable = ColorDisplayManager.isNightDisplayAvailable(context);
1464 }
1465 return mIsAvailable;
1466 }
1467
Christine Franks78a4dd42019-02-08 11:09:30 -08001468 private void onActivated(boolean activated) {
Christine Franks83cc5412018-07-03 14:46:07 -07001469 Slog.i(TAG, activated ? "Turning on night display" : "Turning off night display");
1470 if (mNightDisplayAutoMode != null) {
1471 mNightDisplayAutoMode.onActivated(activated);
1472 }
1473
Christine Franksa4ed3762019-01-24 15:37:04 -08001474 if (mDisplayWhiteBalanceTintController.isAvailable(getContext())) {
Christine Franks83cc5412018-07-03 14:46:07 -07001475 updateDisplayWhiteBalanceStatus();
1476 }
1477
1478 mHandler.sendEmptyMessage(MSG_APPLY_NIGHT_DISPLAY_ANIMATED);
1479 }
1480
1481 int getColorTemperature() {
1482 return mColorTemp != null ? clampNightDisplayColorTemperature(mColorTemp)
Christine Franks78a4dd42019-02-08 11:09:30 -08001483 : getColorTemperatureSetting();
Christine Franks83cc5412018-07-03 14:46:07 -07001484 }
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 Franks78a4dd42019-02-08 11:09:30 -08001498
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 Franks83cc5412018-07-03 14:46:07 -07001528 }
1529
Christine Franks245ffd42018-11-16 13:45:14 -08001530 /**
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 Franks245ffd42018-11-16 13:45:14 -08001543 mDisplayWhiteBalanceTintController.setMatrix(cct);
1544
1545 if (mDisplayWhiteBalanceTintController.isActivated()) {
Christine Franksc7fb9452019-02-04 08:45:33 -08001546 mHandler.sendEmptyMessage(MSG_APPLY_DISPLAY_WHITE_BALANCE);
Christine Franks245ffd42018-11-16 13:45:14 -08001547 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 Solomon8b72c5b2018-11-25 11:07:13 -08001559
1560 public void dump(PrintWriter pw) {
1561 mDisplayWhiteBalanceTintController.dump(pw);
1562 }
Christine Franksf3529b22019-01-03 13:20:17 -08001563
1564 /**
1565 * Adds a {@link WeakReference<ColorTransformController>} for a newly started activity, and
1566 * invokes {@link ColorTransformController#applyAppSaturation(float[], float[])} if needed.
1567 */
Christine Franks55194dc2019-01-15 13:47:06 -08001568 public boolean attachColorTransformController(String packageName, @UserIdInt int userId,
Christine Franksf3529b22019-01-03 13:20:17 -08001569 WeakReference<ColorTransformController> controller) {
1570 return mAppSaturationController
Christine Franks55194dc2019-01-15 13:47:06 -08001571 .addColorTransformController(packageName, userId, controller);
Christine Franksf3529b22019-01-03 13:20:17 -08001572 }
Christine Franks245ffd42018-11-16 13:45:14 -08001573 }
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 Franks09c229e2018-12-14 10:37:40 -08001587 private final class TintHandler extends Handler {
1588
Christine Franks83cc5412018-07-03 14:46:07 -07001589 private TintHandler(Looper looper) {
Christine Franks09c229e2018-12-14 10:37:40 -08001590 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 Franks83cc5412018-07-03 14:46:07 -07001600 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 Franksc7fb9452019-02-04 08:45:33 -08001606 case MSG_APPLY_DISPLAY_WHITE_BALANCE:
1607 applyTint(mDisplayWhiteBalanceTintController, false);
1608 break;
Christine Franks09c229e2018-12-14 10:37:40 -08001609 }
1610 }
1611 }
1612
Christine Franksf3529b22019-01-03 13:20:17 -08001613 /**
1614 * Interface for applying transforms to a given AppWindow.
1615 */
1616 public interface ColorTransformController {
1617
Christine Franksd154fe52019-01-04 17:17:45 -08001618 /**
1619 * Apply the given saturation (grayscale) matrix to the associated AppWindow.
1620 */
Christine Franksf3529b22019-01-03 13:20:17 -08001621 void applyAppSaturation(@Size(9) float[] matrix, @Size(3) float[] translation);
1622 }
1623
Christine Franks83cc5412018-07-03 14:46:07 -07001624 @VisibleForTesting
1625 final class BinderService extends IColorDisplayManager.Stub {
Christine Franks57fdde82018-07-03 14:46:07 -07001626
Christine Franks39b03112018-07-03 14:46:07 -07001627 @Override
Christine Franksd154fe52019-01-04 17:17:45 -08001628 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 Franks39b03112018-07-03 14:46:07 -07001651 public boolean isDeviceColorManaged() {
1652 final long token = Binder.clearCallingIdentity();
1653 try {
1654 return isDeviceColorManagedInternal();
1655 } finally {
1656 Binder.restoreCallingIdentity(token);
1657 }
1658 }
Christine Franks09c229e2018-12-14 10:37:40 -08001659
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 Franksf3529b22019-01-03 13:20:17 -08001681
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 Franks55194dc2019-01-15 13:47:06 -08001695 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 Franksf3529b22019-01-03 13:20:17 -08001707 @Override
Christine Franks83cc5412018-07-03 14:46:07 -07001708 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 Franksf3529b22019-01-03 13:20:17 -08001837 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Christine Franksd154fe52019-01-04 17:17:45 -08001838 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
1839 return;
1840 }
Christine Franksf3529b22019-01-03 13:20:17 -08001841
1842 final long token = Binder.clearCallingIdentity();
1843 try {
1844 dumpInternal(pw);
1845 } finally {
1846 Binder.restoreCallingIdentity(token);
1847 }
1848 }
Christine Franks39b03112018-07-03 14:46:07 -07001849 }
Justin Klaassen911e8892016-06-21 18:24:24 -07001850}