blob: 23117760f45f4c84c29b9e950ca37af1455a1457 [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
Christine Franks0ada2772019-02-25 13:54:57 -080017package com.android.server.display.color;
Justin Klaassen911e8892016-06-21 18:24:24 -070018
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 Franks0ada2772019-02-25 13:54:57 -080027import static com.android.server.display.color.DisplayTransformManager.LEVEL_COLOR_MATRIX_DISPLAY_WHITE_BALANCE;
28import static com.android.server.display.color.DisplayTransformManager.LEVEL_COLOR_MATRIX_NIGHT_DISPLAY;
29import static com.android.server.display.color.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
Long Ling1d3f1892019-02-06 12:34:02 -0800132 @VisibleForTesting
133 final DisplayWhiteBalanceTintController mDisplayWhiteBalanceTintController =
134 new DisplayWhiteBalanceTintController();
Christine Franks245ffd42018-11-16 13:45:14 -0800135
Christine Franks09c229e2018-12-14 10:37:40 -0800136 private final TintController mGlobalSaturationTintController = new TintController() {
137
138 private float[] mMatrixGlobalSaturation = new float[16];
139
140 @Override
141 public void setUp(Context context, boolean needsLinear) {
142 }
143
144 @Override
145 public float[] getMatrix() {
146 return Arrays.copyOf(mMatrixGlobalSaturation, mMatrixGlobalSaturation.length);
147 }
148
149 @Override
150 public void setMatrix(int saturationLevel) {
151 if (saturationLevel < 0) {
152 saturationLevel = 0;
153 } else if (saturationLevel > 100) {
154 saturationLevel = 100;
155 }
156 Slog.d(TAG, "Setting saturation level: " + saturationLevel);
157
158 if (saturationLevel == 100) {
Christine Franks6d21d342019-02-07 15:09:03 -0800159 setActivated(false);
Christine Franks09c229e2018-12-14 10:37:40 -0800160 Matrix.setIdentityM(mMatrixGlobalSaturation, 0);
161 } else {
Christine Franks6d21d342019-02-07 15:09:03 -0800162 setActivated(true);
Christine Franks09c229e2018-12-14 10:37:40 -0800163 float saturation = saturationLevel * 0.1f;
164 float desaturation = 1.0f - saturation;
165 float[] luminance = {0.231f * desaturation, 0.715f * desaturation,
Christine Franks83cc5412018-07-03 14:46:07 -0700166 0.072f * desaturation};
Christine Franks09c229e2018-12-14 10:37:40 -0800167 mMatrixGlobalSaturation[0] = luminance[0] + saturation;
168 mMatrixGlobalSaturation[1] = luminance[0];
169 mMatrixGlobalSaturation[2] = luminance[0];
170 mMatrixGlobalSaturation[4] = luminance[1];
171 mMatrixGlobalSaturation[5] = luminance[1] + saturation;
172 mMatrixGlobalSaturation[6] = luminance[1];
173 mMatrixGlobalSaturation[8] = luminance[2];
174 mMatrixGlobalSaturation[9] = luminance[2];
175 mMatrixGlobalSaturation[10] = luminance[2] + saturation;
176 }
177 }
178
179 @Override
180 public int getLevel() {
181 return LEVEL_COLOR_MATRIX_SATURATION;
182 }
Christine Franksa4ed3762019-01-24 15:37:04 -0800183
184 @Override
185 public boolean isAvailable(Context context) {
186 return ColorDisplayManager.isColorTransformAccelerated(context);
187 }
Christine Franks09c229e2018-12-14 10:37:40 -0800188 };
189
Christine Franks9114f462019-01-04 11:27:30 -0800190 /**
191 * Matrix and offset used for converting color to grayscale.
192 */
193 private static final float[] MATRIX_GRAYSCALE = new float[]{
194 .2126f, .2126f, .2126f, 0f,
195 .7152f, .7152f, .7152f, 0f,
196 .0722f, .0722f, .0722f, 0f,
197 0f, 0f, 0f, 1f
198 };
199
200 /**
201 * Matrix and offset used for luminance inversion. Represents a transform from RGB to YIQ color
202 * space, rotation around the Y axis by 180 degrees, transform back to RGB color space, and
203 * subtraction from 1. The last row represents a non-multiplied addition, see surfaceflinger's
204 * ProgramCache for full implementation details.
205 */
Christine Franksd154fe52019-01-04 17:17:45 -0800206 private static final float[] MATRIX_INVERT_COLOR = new float[]{
Christine Franks9114f462019-01-04 11:27:30 -0800207 0.402f, -0.598f, -0.599f, 0f,
208 -1.174f, -0.174f, -1.175f, 0f,
209 -0.228f, -0.228f, 0.772f, 0f,
210 1f, 1f, 1f, 1f
211 };
212
Justin Klaassen2696d992016-07-11 21:26:46 -0700213 private final Handler mHandler;
214
Christine Franksf3529b22019-01-03 13:20:17 -0800215 private final AppSaturationController mAppSaturationController = new AppSaturationController();
216
Justin Klaassen911e8892016-06-21 18:24:24 -0700217 private int mCurrentUser = UserHandle.USER_NULL;
Justin Klaassen2696d992016-07-11 21:26:46 -0700218 private ContentObserver mUserSetupObserver;
Justin Klaassen911e8892016-06-21 18:24:24 -0700219 private boolean mBootCompleted;
220
Christine Franks57fdde82018-07-03 14:46:07 -0700221 private ContentObserver mContentObserver;
Christine Franks57fdde82018-07-03 14:46:07 -0700222
Christine Franks245ffd42018-11-16 13:45:14 -0800223 private DisplayWhiteBalanceListener mDisplayWhiteBalanceListener;
224
Christine Franks57fdde82018-07-03 14:46:07 -0700225 private NightDisplayAutoMode mNightDisplayAutoMode;
Justin Klaassen911e8892016-06-21 18:24:24 -0700226
Christine Franks5397f032017-11-01 18:35:16 -0700227 public ColorDisplayService(Context context) {
Justin Klaassen911e8892016-06-21 18:24:24 -0700228 super(context);
Christine Franks83cc5412018-07-03 14:46:07 -0700229 mHandler = new TintHandler(DisplayThread.get().getLooper());
Justin Klaassen911e8892016-06-21 18:24:24 -0700230 }
231
232 @Override
233 public void onStart() {
Christine Franks39b03112018-07-03 14:46:07 -0700234 publishBinderService(Context.COLOR_DISPLAY_SERVICE, new BinderService());
Christine Franks245ffd42018-11-16 13:45:14 -0800235 publishLocalService(ColorDisplayServiceInternal.class, new ColorDisplayServiceInternal());
Christine Franks0ada2772019-02-25 13:54:57 -0800236 publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
Justin Klaassen911e8892016-06-21 18:24:24 -0700237 }
238
239 @Override
Justin Klaassen2696d992016-07-11 21:26:46 -0700240 public void onBootPhase(int phase) {
Christine Frankse5bb03e2017-02-10 17:36:10 -0800241 if (phase >= PHASE_BOOT_COMPLETED) {
Justin Klaassen2696d992016-07-11 21:26:46 -0700242 mBootCompleted = true;
243
244 // Register listeners now that boot is complete.
245 if (mCurrentUser != UserHandle.USER_NULL && mUserSetupObserver == null) {
246 setUp();
247 }
248 }
249 }
250
251 @Override
Justin Klaassen911e8892016-06-21 18:24:24 -0700252 public void onStartUser(int userHandle) {
253 super.onStartUser(userHandle);
254
Justin Klaassen911e8892016-06-21 18:24:24 -0700255 if (mCurrentUser == UserHandle.USER_NULL) {
Justin Klaassen2696d992016-07-11 21:26:46 -0700256 onUserChanged(userHandle);
Justin Klaassen911e8892016-06-21 18:24:24 -0700257 }
258 }
259
260 @Override
261 public void onSwitchUser(int userHandle) {
262 super.onSwitchUser(userHandle);
263
Justin Klaassen2696d992016-07-11 21:26:46 -0700264 onUserChanged(userHandle);
Justin Klaassen911e8892016-06-21 18:24:24 -0700265 }
266
267 @Override
268 public void onStopUser(int userHandle) {
269 super.onStopUser(userHandle);
270
Justin Klaassen911e8892016-06-21 18:24:24 -0700271 if (mCurrentUser == userHandle) {
Justin Klaassen2696d992016-07-11 21:26:46 -0700272 onUserChanged(UserHandle.USER_NULL);
Justin Klaassen911e8892016-06-21 18:24:24 -0700273 }
274 }
275
Justin Klaassen2696d992016-07-11 21:26:46 -0700276 private void onUserChanged(int userHandle) {
277 final ContentResolver cr = getContext().getContentResolver();
Justin Klaassen911e8892016-06-21 18:24:24 -0700278
Justin Klaassen2696d992016-07-11 21:26:46 -0700279 if (mCurrentUser != UserHandle.USER_NULL) {
280 if (mUserSetupObserver != null) {
281 cr.unregisterContentObserver(mUserSetupObserver);
282 mUserSetupObserver = null;
283 } else if (mBootCompleted) {
284 tearDown();
285 }
286 }
287
288 mCurrentUser = userHandle;
289
290 if (mCurrentUser != UserHandle.USER_NULL) {
291 if (!isUserSetupCompleted(cr, mCurrentUser)) {
292 mUserSetupObserver = new ContentObserver(mHandler) {
293 @Override
294 public void onChange(boolean selfChange, Uri uri) {
295 if (isUserSetupCompleted(cr, mCurrentUser)) {
296 cr.unregisterContentObserver(this);
297 mUserSetupObserver = null;
298
299 if (mBootCompleted) {
300 setUp();
301 }
302 }
303 }
304 };
305 cr.registerContentObserver(Secure.getUriFor(Secure.USER_SETUP_COMPLETE),
Christine Franks39b03112018-07-03 14:46:07 -0700306 false /* notifyForDescendants */, mUserSetupObserver, mCurrentUser);
Justin Klaassen2696d992016-07-11 21:26:46 -0700307 } else if (mBootCompleted) {
308 setUp();
Justin Klaassen911e8892016-06-21 18:24:24 -0700309 }
310 }
311 }
312
Justin Klaassen2696d992016-07-11 21:26:46 -0700313 private static boolean isUserSetupCompleted(ContentResolver cr, int userHandle) {
314 return Secure.getIntForUser(cr, Secure.USER_SETUP_COMPLETE, 0, userHandle) == 1;
315 }
316
317 private void setUp() {
Justin Klaassenec8837a2016-08-23 12:04:42 -0700318 Slog.d(TAG, "setUp: currentUser=" + mCurrentUser);
319
Christine Franks57fdde82018-07-03 14:46:07 -0700320 // Listen for external changes to any of the settings.
321 if (mContentObserver == null) {
322 mContentObserver = new ContentObserver(new Handler(DisplayThread.get().getLooper())) {
323 @Override
324 public void onChange(boolean selfChange, Uri uri) {
325 super.onChange(selfChange, uri);
326
327 final String setting = uri == null ? null : uri.getLastPathSegment();
328 if (setting != null) {
329 switch (setting) {
330 case Secure.NIGHT_DISPLAY_ACTIVATED:
Christine Franks78a4dd42019-02-08 11:09:30 -0800331 final boolean activated = mNightDisplayTintController
332 .isActivatedSetting();
Christine Franks83cc5412018-07-03 14:46:07 -0700333 if (mNightDisplayTintController.isActivatedStateNotSet()
334 || mNightDisplayTintController.isActivated() != activated) {
Christine Franks78a4dd42019-02-08 11:09:30 -0800335 mNightDisplayTintController.setActivated(activated);
Christine Franks83cc5412018-07-03 14:46:07 -0700336 }
Christine Franks57fdde82018-07-03 14:46:07 -0700337 break;
338 case Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE:
Christine Franks78a4dd42019-02-08 11:09:30 -0800339 final int temperature = mNightDisplayTintController
340 .getColorTemperatureSetting();
Christine Franks83cc5412018-07-03 14:46:07 -0700341 if (mNightDisplayTintController.getColorTemperature()
342 != temperature) {
343 mNightDisplayTintController
344 .onColorTemperatureChanged(temperature);
345 }
Christine Franks57fdde82018-07-03 14:46:07 -0700346 break;
347 case Secure.NIGHT_DISPLAY_AUTO_MODE:
Christine Franks83cc5412018-07-03 14:46:07 -0700348 onNightDisplayAutoModeChanged(getNightDisplayAutoModeInternal());
Christine Franks57fdde82018-07-03 14:46:07 -0700349 break;
350 case Secure.NIGHT_DISPLAY_CUSTOM_START_TIME:
351 onNightDisplayCustomStartTimeChanged(
Christine Franks83cc5412018-07-03 14:46:07 -0700352 getNightDisplayCustomStartTimeInternal().getLocalTime());
Christine Franks57fdde82018-07-03 14:46:07 -0700353 break;
354 case Secure.NIGHT_DISPLAY_CUSTOM_END_TIME:
355 onNightDisplayCustomEndTimeChanged(
Christine Franks83cc5412018-07-03 14:46:07 -0700356 getNightDisplayCustomEndTimeInternal().getLocalTime());
Christine Franks57fdde82018-07-03 14:46:07 -0700357 break;
358 case System.DISPLAY_COLOR_MODE:
Christine Franks71e003e2019-01-24 14:40:20 -0800359 onDisplayColorModeChanged(getColorModeInternal());
Christine Franks57fdde82018-07-03 14:46:07 -0700360 break;
Christine Franks57fdde82018-07-03 14:46:07 -0700361 case Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED:
Christine Franks9114f462019-01-04 11:27:30 -0800362 onAccessibilityInversionChanged();
363 onAccessibilityActivated();
364 break;
365 case Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED:
366 onAccessibilityDaltonizerChanged();
367 onAccessibilityActivated();
368 break;
369 case Secure.ACCESSIBILITY_DISPLAY_DALTONIZER:
370 onAccessibilityDaltonizerChanged();
Christine Franks57fdde82018-07-03 14:46:07 -0700371 break;
Christine Franks245ffd42018-11-16 13:45:14 -0800372 case Secure.DISPLAY_WHITE_BALANCE_ENABLED:
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800373 updateDisplayWhiteBalanceStatus();
Christine Franks245ffd42018-11-16 13:45:14 -0800374 break;
Christine Franks57fdde82018-07-03 14:46:07 -0700375 }
376 }
377 }
378 };
379 }
380 final ContentResolver cr = getContext().getContentResolver();
381 cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_ACTIVATED),
382 false /* notifyForDescendants */, mContentObserver, mCurrentUser);
383 cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE),
384 false /* notifyForDescendants */, mContentObserver, mCurrentUser);
385 cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_AUTO_MODE),
386 false /* notifyForDescendants */, mContentObserver, mCurrentUser);
387 cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_CUSTOM_START_TIME),
388 false /* notifyForDescendants */, mContentObserver, mCurrentUser);
389 cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_CUSTOM_END_TIME),
390 false /* notifyForDescendants */, mContentObserver, mCurrentUser);
391 cr.registerContentObserver(System.getUriFor(System.DISPLAY_COLOR_MODE),
392 false /* notifyForDescendants */, mContentObserver, mCurrentUser);
393 cr.registerContentObserver(
394 Secure.getUriFor(Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED),
395 false /* notifyForDescendants */, mContentObserver, mCurrentUser);
396 cr.registerContentObserver(
397 Secure.getUriFor(Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED),
398 false /* notifyForDescendants */, mContentObserver, mCurrentUser);
Christine Franks9114f462019-01-04 11:27:30 -0800399 cr.registerContentObserver(
400 Secure.getUriFor(Secure.ACCESSIBILITY_DISPLAY_DALTONIZER),
401 false /* notifyForDescendants */, mContentObserver, mCurrentUser);
Christine Franks245ffd42018-11-16 13:45:14 -0800402 cr.registerContentObserver(Secure.getUriFor(Secure.DISPLAY_WHITE_BALANCE_ENABLED),
403 false /* notifyForDescendants */, mContentObserver, mCurrentUser);
Justin Klaassen911e8892016-06-21 18:24:24 -0700404
Christine Frankscf388c22018-05-15 15:48:10 -0700405 // Set the color mode, if valid, and immediately apply the updated tint matrix based on the
406 // existing activated state. This ensures consistency of tint across the color mode change.
Christine Franks71e003e2019-01-24 14:40:20 -0800407 onDisplayColorModeChanged(getColorModeInternal());
Christine Frankscf388c22018-05-15 15:48:10 -0700408
Christine Franksa4ed3762019-01-24 15:37:04 -0800409 if (mNightDisplayTintController.isAvailable(getContext())) {
Christine Franks245ffd42018-11-16 13:45:14 -0800410 // Reset the activated state.
411 mNightDisplayTintController.setActivated(null);
Christine Frankscf388c22018-05-15 15:48:10 -0700412
Christine Franks245ffd42018-11-16 13:45:14 -0800413 // Prepare the night display color transformation matrix.
414 mNightDisplayTintController
415 .setUp(getContext(), DisplayTransformManager.needsLinearColorMatrix());
Christine Franks78a4dd42019-02-08 11:09:30 -0800416 mNightDisplayTintController
417 .setMatrix(mNightDisplayTintController.getColorTemperatureSetting());
Christine Franks8ad71492017-10-24 19:04:22 -0700418
Christine Franks245ffd42018-11-16 13:45:14 -0800419 // Initialize the current auto mode.
Christine Franks83cc5412018-07-03 14:46:07 -0700420 onNightDisplayAutoModeChanged(getNightDisplayAutoModeInternal());
Christine Franks6418d0b2017-02-13 09:48:00 -0800421
Christine Franks83cc5412018-07-03 14:46:07 -0700422 // Force the initialization of the current saved activation state.
Christine Franks245ffd42018-11-16 13:45:14 -0800423 if (mNightDisplayTintController.isActivatedStateNotSet()) {
Christine Franks78a4dd42019-02-08 11:09:30 -0800424 mNightDisplayTintController
425 .setActivated(mNightDisplayTintController.isActivatedSetting());
Christine Franks245ffd42018-11-16 13:45:14 -0800426 }
427 }
Justin Klaassen911e8892016-06-21 18:24:24 -0700428
Christine Franksa4ed3762019-01-24 15:37:04 -0800429 if (mDisplayWhiteBalanceTintController.isAvailable(getContext())) {
Christine Franks245ffd42018-11-16 13:45:14 -0800430 // Prepare the display white balance transform matrix.
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800431 mDisplayWhiteBalanceTintController.setUp(getContext(), true /* needsLinear */);
Christine Franks245ffd42018-11-16 13:45:14 -0800432
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800433 updateDisplayWhiteBalanceStatus();
Justin Klaassen911e8892016-06-21 18:24:24 -0700434 }
435 }
436
Justin Klaassen2696d992016-07-11 21:26:46 -0700437 private void tearDown() {
Justin Klaassenec8837a2016-08-23 12:04:42 -0700438 Slog.d(TAG, "tearDown: currentUser=" + mCurrentUser);
439
Christine Franks57fdde82018-07-03 14:46:07 -0700440 getContext().getContentResolver().unregisterContentObserver(mContentObserver);
441
Christine Franksa4ed3762019-01-24 15:37:04 -0800442 if (mNightDisplayTintController.isAvailable(getContext())) {
Christine Franks245ffd42018-11-16 13:45:14 -0800443 if (mNightDisplayAutoMode != null) {
444 mNightDisplayAutoMode.onStop();
445 mNightDisplayAutoMode = null;
446 }
447 mNightDisplayTintController.endAnimator();
Justin Klaassen911e8892016-06-21 18:24:24 -0700448 }
449
Christine Franksa4ed3762019-01-24 15:37:04 -0800450 if (mDisplayWhiteBalanceTintController.isAvailable(getContext())) {
Christine Franks245ffd42018-11-16 13:45:14 -0800451 mDisplayWhiteBalanceTintController.endAnimator();
Justin Klaassen639214e2016-07-14 21:00:06 -0700452 }
Christine Franks6d21d342019-02-07 15:09:03 -0800453
454 if (mGlobalSaturationTintController.isAvailable(getContext())) {
455 mGlobalSaturationTintController.setActivated(null);
456 }
Justin Klaassen911e8892016-06-21 18:24:24 -0700457 }
458
Christine Franks57fdde82018-07-03 14:46:07 -0700459 private void onNightDisplayAutoModeChanged(int autoMode) {
460 Slog.d(TAG, "onNightDisplayAutoModeChanged: autoMode=" + autoMode);
Justin Klaassenec8837a2016-08-23 12:04:42 -0700461
Christine Franks57fdde82018-07-03 14:46:07 -0700462 if (mNightDisplayAutoMode != null) {
463 mNightDisplayAutoMode.onStop();
464 mNightDisplayAutoMode = null;
Justin Klaassen911e8892016-06-21 18:24:24 -0700465 }
466
Christine Franks83cc5412018-07-03 14:46:07 -0700467 if (autoMode == AUTO_MODE_CUSTOM_TIME) {
Christine Franks57fdde82018-07-03 14:46:07 -0700468 mNightDisplayAutoMode = new CustomNightDisplayAutoMode();
Christine Franks83cc5412018-07-03 14:46:07 -0700469 } else if (autoMode == AUTO_MODE_TWILIGHT) {
Christine Franks57fdde82018-07-03 14:46:07 -0700470 mNightDisplayAutoMode = new TwilightNightDisplayAutoMode();
Justin Klaassen911e8892016-06-21 18:24:24 -0700471 }
472
Christine Franks57fdde82018-07-03 14:46:07 -0700473 if (mNightDisplayAutoMode != null) {
474 mNightDisplayAutoMode.onStart();
Justin Klaassen911e8892016-06-21 18:24:24 -0700475 }
476 }
477
Christine Franks57fdde82018-07-03 14:46:07 -0700478 private void onNightDisplayCustomStartTimeChanged(LocalTime startTime) {
479 Slog.d(TAG, "onNightDisplayCustomStartTimeChanged: startTime=" + startTime);
Justin Klaassenec8837a2016-08-23 12:04:42 -0700480
Christine Franks57fdde82018-07-03 14:46:07 -0700481 if (mNightDisplayAutoMode != null) {
482 mNightDisplayAutoMode.onCustomStartTimeChanged(startTime);
Justin Klaassen911e8892016-06-21 18:24:24 -0700483 }
484 }
485
Christine Franks57fdde82018-07-03 14:46:07 -0700486 private void onNightDisplayCustomEndTimeChanged(LocalTime endTime) {
487 Slog.d(TAG, "onNightDisplayCustomEndTimeChanged: endTime=" + endTime);
Justin Klaassenec8837a2016-08-23 12:04:42 -0700488
Christine Franks57fdde82018-07-03 14:46:07 -0700489 if (mNightDisplayAutoMode != null) {
490 mNightDisplayAutoMode.onCustomEndTimeChanged(endTime);
Justin Klaassen911e8892016-06-21 18:24:24 -0700491 }
492 }
493
Christine Franks57fdde82018-07-03 14:46:07 -0700494 private void onDisplayColorModeChanged(int mode) {
Christine Franks83cc5412018-07-03 14:46:07 -0700495 if (mode == NOT_SET) {
Christine Frankscf388c22018-05-15 15:48:10 -0700496 return;
497 }
498
Christine Franks245ffd42018-11-16 13:45:14 -0800499 mNightDisplayTintController.cancelAnimator();
500 mDisplayWhiteBalanceTintController.cancelAnimator();
501
Christine Franksa4ed3762019-01-24 15:37:04 -0800502 if (mNightDisplayTintController.isAvailable(getContext())) {
503 mNightDisplayTintController
504 .setUp(getContext(), DisplayTransformManager.needsLinearColorMatrix(mode));
Christine Franks78a4dd42019-02-08 11:09:30 -0800505 mNightDisplayTintController
506 .setMatrix(mNightDisplayTintController.getColorTemperatureSetting());
Christine Franksa4ed3762019-01-24 15:37:04 -0800507 }
Christine Franks245ffd42018-11-16 13:45:14 -0800508
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800509 updateDisplayWhiteBalanceStatus();
Christine Franks8ad71492017-10-24 19:04:22 -0700510
Christine Franks218e6562017-11-27 10:20:14 -0800511 final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class);
Christine Franks245ffd42018-11-16 13:45:14 -0800512 dtm.setColorMode(mode, mNightDisplayTintController.getMatrix());
Christine Franks8ad71492017-10-24 19:04:22 -0700513 }
514
Christine Franks9114f462019-01-04 11:27:30 -0800515 private void onAccessibilityActivated() {
Christine Franks71e003e2019-01-24 14:40:20 -0800516 onDisplayColorModeChanged(getColorModeInternal());
Daniel Solomon317a3572018-03-30 18:36:37 -0700517 }
518
Christine Franks9114f462019-01-04 11:27:30 -0800519 /**
520 * Apply the accessibility daltonizer transform based on the settings value.
521 */
522 private void onAccessibilityDaltonizerChanged() {
523 final boolean enabled = Secure.getIntForUser(getContext().getContentResolver(),
524 Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0, mCurrentUser) != 0;
525 final int daltonizerMode = enabled ? Secure.getIntForUser(getContext().getContentResolver(),
526 Secure.ACCESSIBILITY_DISPLAY_DALTONIZER,
527 AccessibilityManager.DALTONIZER_CORRECT_DEUTERANOMALY, mCurrentUser)
528 : AccessibilityManager.DALTONIZER_DISABLED;
529
530 final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class);
531 if (daltonizerMode == AccessibilityManager.DALTONIZER_SIMULATE_MONOCHROMACY) {
532 // Monochromacy isn't supported by the native Daltonizer implementation; use grayscale.
533 dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_GRAYSCALE,
534 MATRIX_GRAYSCALE);
535 dtm.setDaltonizerMode(AccessibilityManager.DALTONIZER_DISABLED);
536 } else {
537 dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_GRAYSCALE, null);
538 dtm.setDaltonizerMode(daltonizerMode);
539 }
540 }
541
542 /**
543 * Apply the accessibility inversion transform based on the settings value.
544 */
545 private void onAccessibilityInversionChanged() {
546 final boolean enabled = Secure.getIntForUser(getContext().getContentResolver(),
547 Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, 0, mCurrentUser) != 0;
548 final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class);
549 dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_INVERT_COLOR,
550 enabled ? MATRIX_INVERT_COLOR : null);
551 }
Christine Franks8ad71492017-10-24 19:04:22 -0700552
Christine Franks6418d0b2017-02-13 09:48:00 -0800553 /**
554 * Applies current color temperature matrix, or removes it if deactivated.
555 *
556 * @param immediate {@code true} skips transition animation
557 */
Christine Franks245ffd42018-11-16 13:45:14 -0800558 private void applyTint(TintController tintController, boolean immediate) {
559 tintController.cancelAnimator();
Christine Franks6418d0b2017-02-13 09:48:00 -0800560
Christine Franks6418d0b2017-02-13 09:48:00 -0800561 final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class);
Christine Franks245ffd42018-11-16 13:45:14 -0800562 final float[] from = dtm.getColorMatrix(tintController.getLevel());
563 final float[] to = tintController.getMatrix();
Christine Franks6418d0b2017-02-13 09:48:00 -0800564
565 if (immediate) {
Christine Franks245ffd42018-11-16 13:45:14 -0800566 dtm.setColorMatrix(tintController.getLevel(), to);
Christine Franks6418d0b2017-02-13 09:48:00 -0800567 } else {
Christine Franks245ffd42018-11-16 13:45:14 -0800568 tintController.setAnimator(ValueAnimator.ofObject(COLOR_MATRIX_EVALUATOR,
569 from == null ? MATRIX_IDENTITY : from, to));
570 tintController.getAnimator().setDuration(TRANSITION_DURATION);
571 tintController.getAnimator().setInterpolator(AnimationUtils.loadInterpolator(
Christine Franks6418d0b2017-02-13 09:48:00 -0800572 getContext(), android.R.interpolator.fast_out_slow_in));
Christine Franks245ffd42018-11-16 13:45:14 -0800573 tintController.getAnimator().addUpdateListener((ValueAnimator animator) -> {
574 final float[] value = (float[]) animator.getAnimatedValue();
575 dtm.setColorMatrix(tintController.getLevel(), value);
Christine Franks6418d0b2017-02-13 09:48:00 -0800576 });
Christine Franks245ffd42018-11-16 13:45:14 -0800577 tintController.getAnimator().addListener(new AnimatorListenerAdapter() {
Christine Franks6418d0b2017-02-13 09:48:00 -0800578
579 private boolean mIsCancelled;
580
581 @Override
582 public void onAnimationCancel(Animator animator) {
583 mIsCancelled = true;
584 }
585
586 @Override
587 public void onAnimationEnd(Animator animator) {
588 if (!mIsCancelled) {
589 // Ensure final color matrix is set at the end of the animation. If the
590 // animation is cancelled then don't set the final color matrix so the new
591 // animator can pick up from where this one left off.
Christine Franks245ffd42018-11-16 13:45:14 -0800592 dtm.setColorMatrix(tintController.getLevel(), to);
Christine Franks6418d0b2017-02-13 09:48:00 -0800593 }
Christine Franks245ffd42018-11-16 13:45:14 -0800594 tintController.setAnimator(null);
Christine Franks6418d0b2017-02-13 09:48:00 -0800595 }
596 });
Christine Franks245ffd42018-11-16 13:45:14 -0800597 tintController.getAnimator().start();
Christine Franks6418d0b2017-02-13 09:48:00 -0800598 }
599 }
600
601 /**
Christine Franks39b03112018-07-03 14:46:07 -0700602 * Returns the first date time corresponding to the local time that occurs before the provided
603 * date time.
Christine Franks03213462017-08-25 13:57:26 -0700604 *
605 * @param compareTime the LocalDateTime to compare against
606 * @return the prior LocalDateTime corresponding to this local time
607 */
Christine Franks57fdde82018-07-03 14:46:07 -0700608 @VisibleForTesting
609 static LocalDateTime getDateTimeBefore(LocalTime localTime, LocalDateTime compareTime) {
Christine Franks03213462017-08-25 13:57:26 -0700610 final LocalDateTime ldt = LocalDateTime.of(compareTime.getYear(), compareTime.getMonth(),
611 compareTime.getDayOfMonth(), localTime.getHour(), localTime.getMinute());
612
613 // Check if the local time has passed, if so return the same time yesterday.
614 return ldt.isAfter(compareTime) ? ldt.minusDays(1) : ldt;
615 }
616
617 /**
Christine Franks39b03112018-07-03 14:46:07 -0700618 * Returns the first date time corresponding to this local time that occurs after the provided
619 * date time.
Christine Franks03213462017-08-25 13:57:26 -0700620 *
621 * @param compareTime the LocalDateTime to compare against
622 * @return the next LocalDateTime corresponding to this local time
623 */
Christine Franks57fdde82018-07-03 14:46:07 -0700624 @VisibleForTesting
625 static LocalDateTime getDateTimeAfter(LocalTime localTime, LocalDateTime compareTime) {
Christine Franks03213462017-08-25 13:57:26 -0700626 final LocalDateTime ldt = LocalDateTime.of(compareTime.getYear(), compareTime.getMonth(),
627 compareTime.getDayOfMonth(), localTime.getHour(), localTime.getMinute());
628
629 // Check if the local time has passed, if so return the same time tomorrow.
630 return ldt.isBefore(compareTime) ? ldt.plusDays(1) : ldt;
631 }
632
Long Ling1d3f1892019-02-06 12:34:02 -0800633 @VisibleForTesting
634 void updateDisplayWhiteBalanceStatus() {
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800635 boolean oldActivated = mDisplayWhiteBalanceTintController.isActivated();
Christine Franks0ada2772019-02-25 13:54:57 -0800636 mDisplayWhiteBalanceTintController.setActivated(isDisplayWhiteBalanceSettingEnabled()
637 && !mNightDisplayTintController.isActivated()
638 && DisplayTransformManager.needsLinearColorMatrix());
Daniel Solomon8b72c5b2018-11-25 11:07:13 -0800639 boolean activated = mDisplayWhiteBalanceTintController.isActivated();
640
641 if (mDisplayWhiteBalanceListener != null && oldActivated != activated) {
642 mDisplayWhiteBalanceListener.onDisplayWhiteBalanceStatusChanged(activated);
Christine Franks245ffd42018-11-16 13:45:14 -0800643 }
Daniel Solomon86508f82019-01-18 19:01:02 -0800644
645 // If disabled, clear the tint. If enabled, do nothing more here and let the next
646 // temperature update set the correct tint.
647 if (!activated) {
Christine Franksc7fb9452019-02-04 08:45:33 -0800648 mHandler.sendEmptyMessage(MSG_APPLY_DISPLAY_WHITE_BALANCE);
Daniel Solomon86508f82019-01-18 19:01:02 -0800649 }
Christine Franks245ffd42018-11-16 13:45:14 -0800650 }
651
652 private boolean isDisplayWhiteBalanceSettingEnabled() {
653 return Secure.getIntForUser(getContext().getContentResolver(),
654 Secure.DISPLAY_WHITE_BALANCE_ENABLED, 0, mCurrentUser) == 1;
655 }
656
Christine Franks39b03112018-07-03 14:46:07 -0700657 private boolean isDeviceColorManagedInternal() {
658 final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class);
659 return dtm.isDeviceColorManaged();
660 }
661
Christine Franks55194dc2019-01-15 13:47:06 -0800662 private int getTransformCapabilitiesInternal() {
663 int availabilityFlags = ColorDisplayManager.CAPABILITY_NONE;
664 if (SurfaceControl.getProtectedContentSupport()) {
665 availabilityFlags |= ColorDisplayManager.CAPABILITY_PROTECTED_CONTENT;
666 }
667 final Resources res = getContext().getResources();
668 if (res.getBoolean(R.bool.config_setColorTransformAccelerated)) {
669 availabilityFlags |= ColorDisplayManager.CAPABILITY_HARDWARE_ACCELERATION_GLOBAL;
670 }
671 if (res.getBoolean(R.bool.config_setColorTransformAcceleratedPerLayer)) {
672 availabilityFlags |= ColorDisplayManager.CAPABILITY_HARDWARE_ACCELERATION_PER_APP;
673 }
674 return availabilityFlags;
675 }
676
Christine Franks83cc5412018-07-03 14:46:07 -0700677 private boolean setNightDisplayAutoModeInternal(@AutoMode int autoMode) {
678 if (getNightDisplayAutoModeInternal() != autoMode) {
679 Secure.putStringForUser(getContext().getContentResolver(),
680 Secure.NIGHT_DISPLAY_LAST_ACTIVATED_TIME,
681 null,
682 mCurrentUser);
683 }
684 return Secure.putIntForUser(getContext().getContentResolver(),
685 Secure.NIGHT_DISPLAY_AUTO_MODE, autoMode, mCurrentUser);
686 }
687
688 private int getNightDisplayAutoModeInternal() {
689 int autoMode = getNightDisplayAutoModeRawInternal();
690 if (autoMode == NOT_SET) {
691 autoMode = getContext().getResources().getInteger(
692 R.integer.config_defaultNightDisplayAutoMode);
693 }
694 if (autoMode != AUTO_MODE_DISABLED
695 && autoMode != AUTO_MODE_CUSTOM_TIME
696 && autoMode != AUTO_MODE_TWILIGHT) {
697 Slog.e(TAG, "Invalid autoMode: " + autoMode);
698 autoMode = AUTO_MODE_DISABLED;
699 }
700 return autoMode;
701 }
702
703 private int getNightDisplayAutoModeRawInternal() {
704 return Secure
705 .getIntForUser(getContext().getContentResolver(), Secure.NIGHT_DISPLAY_AUTO_MODE,
706 NOT_SET, mCurrentUser);
707 }
708
709 private Time getNightDisplayCustomStartTimeInternal() {
710 int startTimeValue = Secure.getIntForUser(getContext().getContentResolver(),
711 Secure.NIGHT_DISPLAY_CUSTOM_START_TIME, NOT_SET, mCurrentUser);
712 if (startTimeValue == NOT_SET) {
713 startTimeValue = getContext().getResources().getInteger(
714 R.integer.config_defaultNightDisplayCustomStartTime);
715 }
716 return new Time(LocalTime.ofSecondOfDay(startTimeValue / 1000));
717 }
718
719 private boolean setNightDisplayCustomStartTimeInternal(Time startTime) {
720 return Secure.putIntForUser(getContext().getContentResolver(),
721 Secure.NIGHT_DISPLAY_CUSTOM_START_TIME,
722 startTime.getLocalTime().toSecondOfDay() * 1000,
723 mCurrentUser);
724 }
725
726 private Time getNightDisplayCustomEndTimeInternal() {
727 int endTimeValue = Secure.getIntForUser(getContext().getContentResolver(),
728 Secure.NIGHT_DISPLAY_CUSTOM_END_TIME, NOT_SET, mCurrentUser);
729 if (endTimeValue == NOT_SET) {
730 endTimeValue = getContext().getResources().getInteger(
731 R.integer.config_defaultNightDisplayCustomEndTime);
732 }
733 return new Time(LocalTime.ofSecondOfDay(endTimeValue / 1000));
734 }
735
736 private boolean setNightDisplayCustomEndTimeInternal(Time endTime) {
737 return Secure.putIntForUser(getContext().getContentResolver(),
738 Secure.NIGHT_DISPLAY_CUSTOM_END_TIME, endTime.getLocalTime().toSecondOfDay() * 1000,
739 mCurrentUser);
740 }
741
Christine Franks57fdde82018-07-03 14:46:07 -0700742 /**
743 * Returns the last time the night display transform activation state was changed, or {@link
744 * LocalDateTime#MIN} if night display has never been activated.
745 */
Christine Franks245ffd42018-11-16 13:45:14 -0800746 private LocalDateTime getNightDisplayLastActivatedTimeSetting() {
Christine Franks57fdde82018-07-03 14:46:07 -0700747 final ContentResolver cr = getContext().getContentResolver();
748 final String lastActivatedTime = Secure.getStringForUser(
749 cr, Secure.NIGHT_DISPLAY_LAST_ACTIVATED_TIME, getContext().getUserId());
750 if (lastActivatedTime != null) {
751 try {
752 return LocalDateTime.parse(lastActivatedTime);
753 } catch (DateTimeParseException ignored) {
754 }
755 // Uses the old epoch time.
756 try {
757 return LocalDateTime.ofInstant(
758 Instant.ofEpochMilli(Long.parseLong(lastActivatedTime)),
759 ZoneId.systemDefault());
760 } catch (DateTimeException | NumberFormatException ignored) {
761 }
762 }
763 return LocalDateTime.MIN;
764 }
765
Christine Franksf3529b22019-01-03 13:20:17 -0800766 private boolean setAppSaturationLevelInternal(String packageName, int saturationLevel) {
767 return mAppSaturationController
768 .setSaturationLevel(packageName, mCurrentUser, saturationLevel);
769 }
770
Christine Franksd154fe52019-01-04 17:17:45 -0800771 private void setColorModeInternal(@ColorMode int colorMode) {
772 if (!isColorModeAvailable(colorMode)) {
773 throw new IllegalArgumentException("Invalid colorMode: " + colorMode);
774 }
775 System.putIntForUser(getContext().getContentResolver(), System.DISPLAY_COLOR_MODE,
776 colorMode,
777 mCurrentUser);
778 }
779
Christine Franks71e003e2019-01-24 14:40:20 -0800780 private @ColorMode int getColorModeInternal() {
Christine Franksd154fe52019-01-04 17:17:45 -0800781 final ContentResolver cr = getContext().getContentResolver();
782 if (Secure.getIntForUser(cr, Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED,
783 0, mCurrentUser) == 1
784 || Secure.getIntForUser(cr, Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED,
785 0, mCurrentUser) == 1) {
786 // There are restrictions on the available color modes combined with a11y transforms.
787 if (isColorModeAvailable(COLOR_MODE_SATURATED)) {
788 return COLOR_MODE_SATURATED;
789 } else if (isColorModeAvailable(COLOR_MODE_AUTOMATIC)) {
790 return COLOR_MODE_AUTOMATIC;
791 }
792 }
793
794 int colorMode = System.getIntForUser(cr, System.DISPLAY_COLOR_MODE, -1, mCurrentUser);
795 if (colorMode == -1) {
796 // There might be a system property controlling color mode that we need to respect; if
797 // not, this will set a suitable default.
798 colorMode = getCurrentColorModeFromSystemProperties();
799 }
800
801 // This happens when a color mode is no longer available (e.g., after system update or B&R)
802 // or the device does not support any color mode.
803 if (!isColorModeAvailable(colorMode)) {
804 if (colorMode == COLOR_MODE_BOOSTED && isColorModeAvailable(COLOR_MODE_NATURAL)) {
805 colorMode = COLOR_MODE_NATURAL;
806 } else if (colorMode == COLOR_MODE_SATURATED
807 && isColorModeAvailable(COLOR_MODE_AUTOMATIC)) {
808 colorMode = COLOR_MODE_AUTOMATIC;
809 } else if (colorMode == COLOR_MODE_AUTOMATIC
810 && isColorModeAvailable(COLOR_MODE_SATURATED)) {
811 colorMode = COLOR_MODE_SATURATED;
812 } else {
813 colorMode = -1;
814 }
815 }
816
817 return colorMode;
818 }
819
820 /**
821 * Get the current color mode from system properties, or return -1 if invalid.
822 *
Christine Franks0ada2772019-02-25 13:54:57 -0800823 * See {@link DisplayTransformManager}
Christine Franksd154fe52019-01-04 17:17:45 -0800824 */
Christine Franks78a4dd42019-02-08 11:09:30 -0800825 private @ColorMode int getCurrentColorModeFromSystemProperties() {
Christine Franksd154fe52019-01-04 17:17:45 -0800826 final int displayColorSetting = SystemProperties.getInt("persist.sys.sf.native_mode", 0);
827 if (displayColorSetting == 0) {
828 return "1.0".equals(SystemProperties.get("persist.sys.sf.color_saturation"))
829 ? COLOR_MODE_NATURAL : COLOR_MODE_BOOSTED;
830 } else if (displayColorSetting == 1) {
831 return COLOR_MODE_SATURATED;
832 } else if (displayColorSetting == 2) {
833 return COLOR_MODE_AUTOMATIC;
834 } else {
835 return -1;
836 }
837 }
838
839 private boolean isColorModeAvailable(@ColorMode int colorMode) {
840 final int[] availableColorModes = getContext().getResources().getIntArray(
841 R.array.config_availableColorModes);
842 if (availableColorModes != null) {
843 for (int mode : availableColorModes) {
844 if (mode == colorMode) {
845 return true;
846 }
847 }
848 }
849 return false;
850 }
851
Christine Franksf3529b22019-01-03 13:20:17 -0800852 private void dumpInternal(PrintWriter pw) {
853 pw.println("COLOR DISPLAY MANAGER dumpsys (color_display)");
Christine Franksa4ed3762019-01-24 15:37:04 -0800854
Christine Franks0ada2772019-02-25 13:54:57 -0800855 pw.println("Night display:");
Christine Franksa4ed3762019-01-24 15:37:04 -0800856 if (mNightDisplayTintController.isAvailable(getContext())) {
Christine Franksf3529b22019-01-03 13:20:17 -0800857 pw.println(" Activated: " + mNightDisplayTintController.isActivated());
Christine Franksa4ed3762019-01-24 15:37:04 -0800858 pw.println(" Color temp: " + mNightDisplayTintController.getColorTemperature());
Christine Franksf3529b22019-01-03 13:20:17 -0800859 } else {
860 pw.println(" Not available");
861 }
Christine Franksa4ed3762019-01-24 15:37:04 -0800862
863 pw.println("Global saturation:");
864 if (mGlobalSaturationTintController.isAvailable(getContext())) {
865 pw.println(" Activated: " + mGlobalSaturationTintController.isActivated());
866 } else {
867 pw.println(" Not available");
868 }
869
Christine Franksf3529b22019-01-03 13:20:17 -0800870 mAppSaturationController.dump(pw);
Christine Franksa4ed3762019-01-24 15:37:04 -0800871
872 pw.println("Display white balance:");
873 if (mDisplayWhiteBalanceTintController.isAvailable(getContext())) {
874 pw.println(" Activated: " + mDisplayWhiteBalanceTintController.isActivated());
Long Ling1d3f1892019-02-06 12:34:02 -0800875 mDisplayWhiteBalanceTintController.dump(pw);
Christine Franksa4ed3762019-01-24 15:37:04 -0800876 } else {
877 pw.println(" Not available");
878 }
879
880 pw.println("Color mode: " + getColorModeInternal());
Christine Franksf3529b22019-01-03 13:20:17 -0800881 }
882
Christine Franks57fdde82018-07-03 14:46:07 -0700883 private abstract class NightDisplayAutoMode {
884
885 public abstract void onActivated(boolean activated);
886
Justin Klaassen911e8892016-06-21 18:24:24 -0700887 public abstract void onStart();
Christine Frankse5bb03e2017-02-10 17:36:10 -0800888
Justin Klaassen911e8892016-06-21 18:24:24 -0700889 public abstract void onStop();
Christine Franks57fdde82018-07-03 14:46:07 -0700890
891 public void onCustomStartTimeChanged(LocalTime startTime) {
892 }
893
894 public void onCustomEndTimeChanged(LocalTime endTime) {
895 }
Justin Klaassen911e8892016-06-21 18:24:24 -0700896 }
897
Christine Franks57fdde82018-07-03 14:46:07 -0700898 private final class CustomNightDisplayAutoMode extends NightDisplayAutoMode implements
899 AlarmManager.OnAlarmListener {
Justin Klaassen911e8892016-06-21 18:24:24 -0700900
901 private final AlarmManager mAlarmManager;
902 private final BroadcastReceiver mTimeChangedReceiver;
903
Christine Franks03213462017-08-25 13:57:26 -0700904 private LocalTime mStartTime;
905 private LocalTime mEndTime;
Justin Klaassen911e8892016-06-21 18:24:24 -0700906
Christine Franks03213462017-08-25 13:57:26 -0700907 private LocalDateTime mLastActivatedTime;
Justin Klaassen911e8892016-06-21 18:24:24 -0700908
Christine Franks57fdde82018-07-03 14:46:07 -0700909 CustomNightDisplayAutoMode() {
Justin Klaassen911e8892016-06-21 18:24:24 -0700910 mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
911 mTimeChangedReceiver = new BroadcastReceiver() {
912 @Override
913 public void onReceive(Context context, Intent intent) {
914 updateActivated();
915 }
916 };
917 }
918
919 private void updateActivated() {
Christine Franks03213462017-08-25 13:57:26 -0700920 final LocalDateTime now = LocalDateTime.now();
921 final LocalDateTime start = getDateTimeBefore(mStartTime, now);
922 final LocalDateTime end = getDateTimeAfter(mEndTime, start);
923 boolean activate = now.isBefore(end);
Justin Klaassen911e8892016-06-21 18:24:24 -0700924
Christine Frankse5bb03e2017-02-10 17:36:10 -0800925 if (mLastActivatedTime != null) {
Christine Frankse5bb03e2017-02-10 17:36:10 -0800926 // Maintain the existing activated state if within the current period.
Christine Franks0ada2772019-02-25 13:54:57 -0800927 if (mLastActivatedTime.isBefore(now)
928 && mLastActivatedTime.isAfter(start)
Christine Franks03213462017-08-25 13:57:26 -0700929 && (mLastActivatedTime.isAfter(end) || now.isBefore(end))) {
Christine Franks78a4dd42019-02-08 11:09:30 -0800930 activate = mNightDisplayTintController.isActivatedSetting();
Justin Klaassen911e8892016-06-21 18:24:24 -0700931 }
932 }
933
Christine Franks0ada2772019-02-25 13:54:57 -0800934 if (mNightDisplayTintController.isActivatedStateNotSet()
935 || (mNightDisplayTintController.isActivated() != activate)) {
Christine Franks83cc5412018-07-03 14:46:07 -0700936 mNightDisplayTintController.setActivated(activate);
Justin Klaassen911e8892016-06-21 18:24:24 -0700937 }
Christine Franks03213462017-08-25 13:57:26 -0700938
Christine Franks245ffd42018-11-16 13:45:14 -0800939 updateNextAlarm(mNightDisplayTintController.isActivated(), now);
Justin Klaassen911e8892016-06-21 18:24:24 -0700940 }
941
Christine Franks03213462017-08-25 13:57:26 -0700942 private void updateNextAlarm(@Nullable Boolean activated, @NonNull LocalDateTime now) {
Justin Klaassen4346f632016-08-08 15:01:47 -0700943 if (activated != null) {
Christine Franks03213462017-08-25 13:57:26 -0700944 final LocalDateTime next = activated ? getDateTimeAfter(mEndTime, now)
945 : getDateTimeAfter(mStartTime, now);
946 final long millis = next.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
947 mAlarmManager.setExact(AlarmManager.RTC, millis, TAG, this, null);
Justin Klaassen911e8892016-06-21 18:24:24 -0700948 }
949 }
950
951 @Override
952 public void onStart() {
953 final IntentFilter intentFilter = new IntentFilter(Intent.ACTION_TIME_CHANGED);
954 intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
955 getContext().registerReceiver(mTimeChangedReceiver, intentFilter);
956
Christine Franks83cc5412018-07-03 14:46:07 -0700957 mStartTime = getNightDisplayCustomStartTimeInternal().getLocalTime();
958 mEndTime = getNightDisplayCustomEndTimeInternal().getLocalTime();
Justin Klaassen911e8892016-06-21 18:24:24 -0700959
Christine Franks57fdde82018-07-03 14:46:07 -0700960 mLastActivatedTime = getNightDisplayLastActivatedTimeSetting();
Christine Frankse5bb03e2017-02-10 17:36:10 -0800961
Justin Klaassen911e8892016-06-21 18:24:24 -0700962 // Force an update to initialize state.
963 updateActivated();
964 }
965
966 @Override
967 public void onStop() {
968 getContext().unregisterReceiver(mTimeChangedReceiver);
969
970 mAlarmManager.cancel(this);
971 mLastActivatedTime = null;
972 }
973
974 @Override
975 public void onActivated(boolean activated) {
Christine Franks57fdde82018-07-03 14:46:07 -0700976 mLastActivatedTime = getNightDisplayLastActivatedTimeSetting();
Christine Franks03213462017-08-25 13:57:26 -0700977 updateNextAlarm(activated, LocalDateTime.now());
Justin Klaassen911e8892016-06-21 18:24:24 -0700978 }
979
980 @Override
Christine Franks03213462017-08-25 13:57:26 -0700981 public void onCustomStartTimeChanged(LocalTime startTime) {
Justin Klaassen911e8892016-06-21 18:24:24 -0700982 mStartTime = startTime;
983 mLastActivatedTime = null;
984 updateActivated();
985 }
986
987 @Override
Christine Franks03213462017-08-25 13:57:26 -0700988 public void onCustomEndTimeChanged(LocalTime endTime) {
Justin Klaassen911e8892016-06-21 18:24:24 -0700989 mEndTime = endTime;
990 mLastActivatedTime = null;
991 updateActivated();
992 }
993
994 @Override
995 public void onAlarm() {
Justin Klaassenec8837a2016-08-23 12:04:42 -0700996 Slog.d(TAG, "onAlarm");
Justin Klaassen911e8892016-06-21 18:24:24 -0700997 updateActivated();
998 }
999 }
1000
Christine Franks57fdde82018-07-03 14:46:07 -07001001 private final class TwilightNightDisplayAutoMode extends NightDisplayAutoMode implements
1002 TwilightListener {
Justin Klaassen911e8892016-06-21 18:24:24 -07001003
1004 private final TwilightManager mTwilightManager;
Christine Franks57fdde82018-07-03 14:46:07 -07001005 private LocalDateTime mLastActivatedTime;
Justin Klaassen911e8892016-06-21 18:24:24 -07001006
Christine Franks57fdde82018-07-03 14:46:07 -07001007 TwilightNightDisplayAutoMode() {
Justin Klaassen911e8892016-06-21 18:24:24 -07001008 mTwilightManager = getLocalService(TwilightManager.class);
Justin Klaassen911e8892016-06-21 18:24:24 -07001009 }
1010
Justin Klaassen908b86c2016-08-08 09:18:42 -07001011 private void updateActivated(TwilightState state) {
Justin Klaassen3da4c442017-05-05 15:19:33 -07001012 if (state == null) {
1013 // If there isn't a valid TwilightState then just keep the current activated
1014 // state.
1015 return;
1016 }
1017
1018 boolean activate = state.isNight();
Christine Franks57fdde82018-07-03 14:46:07 -07001019 if (mLastActivatedTime != null) {
Christine Franks03213462017-08-25 13:57:26 -07001020 final LocalDateTime now = LocalDateTime.now();
1021 final LocalDateTime sunrise = state.sunrise();
1022 final LocalDateTime sunset = state.sunset();
Christine Frankse5bb03e2017-02-10 17:36:10 -08001023 // Maintain the existing activated state if within the current period.
Christine Franks57fdde82018-07-03 14:46:07 -07001024 if (mLastActivatedTime.isBefore(now) && (mLastActivatedTime.isBefore(sunrise)
1025 ^ mLastActivatedTime.isBefore(sunset))) {
Christine Franks78a4dd42019-02-08 11:09:30 -08001026 activate = mNightDisplayTintController.isActivatedSetting();
Justin Klaassen911e8892016-06-21 18:24:24 -07001027 }
1028 }
Justin Klaassen908b86c2016-08-08 09:18:42 -07001029
Christine Franks245ffd42018-11-16 13:45:14 -08001030 if (mNightDisplayTintController.isActivatedStateNotSet() || (
1031 mNightDisplayTintController.isActivated() != activate)) {
Christine Franks83cc5412018-07-03 14:46:07 -07001032 mNightDisplayTintController.setActivated(activate);
Justin Klaassen908b86c2016-08-08 09:18:42 -07001033 }
Justin Klaassen911e8892016-06-21 18:24:24 -07001034 }
1035
1036 @Override
Christine Franks57fdde82018-07-03 14:46:07 -07001037 public void onActivated(boolean activated) {
1038 mLastActivatedTime = getNightDisplayLastActivatedTimeSetting();
1039 }
1040
1041 @Override
Justin Klaassen911e8892016-06-21 18:24:24 -07001042 public void onStart() {
1043 mTwilightManager.registerListener(this, mHandler);
Christine Franks57fdde82018-07-03 14:46:07 -07001044 mLastActivatedTime = getNightDisplayLastActivatedTimeSetting();
Justin Klaassen911e8892016-06-21 18:24:24 -07001045
1046 // Force an update to initialize state.
Justin Klaassen908b86c2016-08-08 09:18:42 -07001047 updateActivated(mTwilightManager.getLastTwilightState());
Justin Klaassen911e8892016-06-21 18:24:24 -07001048 }
1049
1050 @Override
1051 public void onStop() {
1052 mTwilightManager.unregisterListener(this);
Christine Franks57fdde82018-07-03 14:46:07 -07001053 mLastActivatedTime = null;
Justin Klaassen908b86c2016-08-08 09:18:42 -07001054 }
1055
1056 @Override
1057 public void onTwilightStateChanged(@Nullable TwilightState state) {
Justin Klaassenec8837a2016-08-23 12:04:42 -07001058 Slog.d(TAG, "onTwilightStateChanged: isNight="
1059 + (state == null ? null : state.isNight()));
Justin Klaassen908b86c2016-08-08 09:18:42 -07001060 updateActivated(state);
Justin Klaassen911e8892016-06-21 18:24:24 -07001061 }
1062 }
Justin Klaassen639214e2016-07-14 21:00:06 -07001063
1064 /**
1065 * Interpolates between two 4x4 color transform matrices (in column-major order).
1066 */
1067 private static class ColorMatrixEvaluator implements TypeEvaluator<float[]> {
1068
1069 /**
1070 * Result matrix returned by {@link #evaluate(float, float[], float[])}.
1071 */
1072 private final float[] mResultMatrix = new float[16];
1073
1074 @Override
1075 public float[] evaluate(float fraction, float[] startValue, float[] endValue) {
1076 for (int i = 0; i < mResultMatrix.length; i++) {
1077 mResultMatrix[i] = MathUtils.lerp(startValue[i], endValue[i], fraction);
1078 }
1079 return mResultMatrix;
1080 }
1081 }
Christine Franks39b03112018-07-03 14:46:07 -07001082
Christine Franks245ffd42018-11-16 13:45:14 -08001083 private abstract static class TintController {
1084
1085 private ValueAnimator mAnimator;
1086 private Boolean mIsActivated;
1087
1088 public ValueAnimator getAnimator() {
1089 return mAnimator;
1090 }
1091
1092 public void setAnimator(ValueAnimator animator) {
1093 mAnimator = animator;
1094 }
1095
1096 /**
1097 * Cancel the animator if it's still running.
1098 */
1099 public void cancelAnimator() {
1100 if (mAnimator != null) {
1101 mAnimator.cancel();
1102 }
1103 }
1104
1105 /**
1106 * End the animator if it's still running, jumping to the end state.
1107 */
1108 public void endAnimator() {
1109 if (mAnimator != null) {
1110 mAnimator.end();
1111 mAnimator = null;
1112 }
1113 }
1114
1115 public void setActivated(Boolean isActivated) {
1116 mIsActivated = isActivated;
1117 }
1118
1119 public boolean isActivated() {
1120 return mIsActivated != null && mIsActivated;
1121 }
1122
1123 public boolean isActivatedStateNotSet() {
1124 return mIsActivated == null;
1125 }
1126
1127 /**
Daniel Solomon8b72c5b2018-11-25 11:07:13 -08001128 * Dump debug information.
1129 */
1130 public void dump(PrintWriter pw) {
1131 }
1132
1133 /**
Christine Franks245ffd42018-11-16 13:45:14 -08001134 * Set up any constants needed for computing the matrix.
1135 */
1136 public abstract void setUp(Context context, boolean needsLinear);
1137
1138 /**
1139 * Sets the 4x4 matrix to apply.
1140 */
1141 public abstract void setMatrix(int value);
1142
1143 /**
1144 * Get the 4x4 matrix to apply.
1145 */
1146 public abstract float[] getMatrix();
1147
1148 /**
1149 * Get the color transform level to apply the matrix.
1150 */
1151 public abstract int getLevel();
Christine Franksa4ed3762019-01-24 15:37:04 -08001152
1153 /**
1154 * Returns whether or not this transform type is available on this device.
1155 */
1156 public abstract boolean isAvailable(Context context);
Christine Franks245ffd42018-11-16 13:45:14 -08001157 }
1158
Christine Franks83cc5412018-07-03 14:46:07 -07001159 private final class NightDisplayTintController extends TintController {
1160
Christine Franksa4ed3762019-01-24 15:37:04 -08001161 private final float[] mMatrix = new float[16];
Christine Franks83cc5412018-07-03 14:46:07 -07001162 private final float[] mColorTempCoefficients = new float[9];
Christine Franksa4ed3762019-01-24 15:37:04 -08001163
1164 private Boolean mIsAvailable;
Christine Franks83cc5412018-07-03 14:46:07 -07001165 private Integer mColorTemp;
1166
1167 /**
1168 * Set coefficients based on whether the color matrix is linear or not.
1169 */
1170 @Override
1171 public void setUp(Context context, boolean needsLinear) {
1172 final String[] coefficients = context.getResources().getStringArray(needsLinear
1173 ? R.array.config_nightDisplayColorTemperatureCoefficients
1174 : R.array.config_nightDisplayColorTemperatureCoefficientsNative);
1175 for (int i = 0; i < 9 && i < coefficients.length; i++) {
1176 mColorTempCoefficients[i] = Float.parseFloat(coefficients[i]);
1177 }
1178 }
1179
1180 @Override
1181 public void setMatrix(int cct) {
1182 if (mMatrix.length != 16) {
1183 Slog.d(TAG, "The display transformation matrix must be 4x4");
1184 return;
1185 }
1186
1187 Matrix.setIdentityM(mMatrix, 0);
1188
1189 final float squareTemperature = cct * cct;
1190 final float red = squareTemperature * mColorTempCoefficients[0]
1191 + cct * mColorTempCoefficients[1] + mColorTempCoefficients[2];
1192 final float green = squareTemperature * mColorTempCoefficients[3]
1193 + cct * mColorTempCoefficients[4] + mColorTempCoefficients[5];
1194 final float blue = squareTemperature * mColorTempCoefficients[6]
1195 + cct * mColorTempCoefficients[7] + mColorTempCoefficients[8];
1196 mMatrix[0] = red;
1197 mMatrix[5] = green;
1198 mMatrix[10] = blue;
1199 }
1200
1201 @Override
1202 public float[] getMatrix() {
1203 return isActivated() ? mMatrix : MATRIX_IDENTITY;
1204 }
1205
1206 @Override
1207 public void setActivated(Boolean activated) {
1208 if (activated == null) {
1209 super.setActivated(null);
1210 return;
1211 }
1212
1213 boolean activationStateChanged = activated != isActivated();
1214
1215 if (!isActivatedStateNotSet() && activationStateChanged) {
1216 // This is a true state change, so set this as the last activation time.
1217 Secure.putStringForUser(getContext().getContentResolver(),
1218 Secure.NIGHT_DISPLAY_LAST_ACTIVATED_TIME,
1219 LocalDateTime.now().toString(),
1220 mCurrentUser);
1221 }
1222
1223 if (isActivatedStateNotSet() || activationStateChanged) {
1224 super.setActivated(activated);
Christine Franks78a4dd42019-02-08 11:09:30 -08001225 if (isActivatedSetting() != activated) {
1226 Secure.putIntForUser(getContext().getContentResolver(),
1227 Secure.NIGHT_DISPLAY_ACTIVATED,
1228 activated ? 1 : 0, mCurrentUser);
1229 }
Christine Franks83cc5412018-07-03 14:46:07 -07001230 onActivated(activated);
1231 }
1232 }
1233
1234 @Override
1235 public int getLevel() {
1236 return LEVEL_COLOR_MATRIX_NIGHT_DISPLAY;
1237 }
1238
Christine Franksa4ed3762019-01-24 15:37:04 -08001239 @Override
1240 public boolean isAvailable(Context context) {
1241 if (mIsAvailable == null) {
1242 mIsAvailable = ColorDisplayManager.isNightDisplayAvailable(context);
1243 }
1244 return mIsAvailable;
1245 }
1246
Christine Franks78a4dd42019-02-08 11:09:30 -08001247 private void onActivated(boolean activated) {
Christine Franks83cc5412018-07-03 14:46:07 -07001248 Slog.i(TAG, activated ? "Turning on night display" : "Turning off night display");
1249 if (mNightDisplayAutoMode != null) {
1250 mNightDisplayAutoMode.onActivated(activated);
1251 }
1252
Christine Franksa4ed3762019-01-24 15:37:04 -08001253 if (mDisplayWhiteBalanceTintController.isAvailable(getContext())) {
Christine Franks83cc5412018-07-03 14:46:07 -07001254 updateDisplayWhiteBalanceStatus();
1255 }
1256
1257 mHandler.sendEmptyMessage(MSG_APPLY_NIGHT_DISPLAY_ANIMATED);
1258 }
1259
1260 int getColorTemperature() {
1261 return mColorTemp != null ? clampNightDisplayColorTemperature(mColorTemp)
Christine Franks78a4dd42019-02-08 11:09:30 -08001262 : getColorTemperatureSetting();
Christine Franks83cc5412018-07-03 14:46:07 -07001263 }
1264
1265 boolean setColorTemperature(int temperature) {
1266 mColorTemp = temperature;
1267 final boolean success = Secure.putIntForUser(getContext().getContentResolver(),
1268 Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE, temperature, mCurrentUser);
1269 onColorTemperatureChanged(temperature);
1270 return success;
1271 }
1272
1273 void onColorTemperatureChanged(int temperature) {
1274 setMatrix(temperature);
1275 mHandler.sendEmptyMessage(MSG_APPLY_NIGHT_DISPLAY_IMMEDIATE);
1276 }
Christine Franks78a4dd42019-02-08 11:09:30 -08001277
1278 boolean isActivatedSetting() {
Christine Franks44782612019-03-07 17:25:39 -08001279 if (mCurrentUser == UserHandle.USER_NULL) {
1280 return false;
1281 }
Christine Franks78a4dd42019-02-08 11:09:30 -08001282 return Secure.getIntForUser(getContext().getContentResolver(),
1283 Secure.NIGHT_DISPLAY_ACTIVATED, 0, mCurrentUser) == 1;
1284 }
1285
1286 int getColorTemperatureSetting() {
Christine Franks44782612019-03-07 17:25:39 -08001287 if (mCurrentUser == UserHandle.USER_NULL) {
1288 return NOT_SET;
1289 }
Christine Franks78a4dd42019-02-08 11:09:30 -08001290 return clampNightDisplayColorTemperature(Secure.getIntForUser(
1291 getContext().getContentResolver(), Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE,
1292 NOT_SET,
1293 mCurrentUser));
1294 }
1295
1296 private int clampNightDisplayColorTemperature(int colorTemperature) {
1297 if (colorTemperature == NOT_SET) {
1298 colorTemperature = getContext().getResources().getInteger(
1299 R.integer.config_nightDisplayColorTemperatureDefault);
1300 }
1301 final int minimumTemperature = ColorDisplayManager
1302 .getMinimumColorTemperature(getContext());
1303 final int maximumTemperature = ColorDisplayManager
1304 .getMaximumColorTemperature(getContext());
1305 if (colorTemperature < minimumTemperature) {
1306 colorTemperature = minimumTemperature;
1307 } else if (colorTemperature > maximumTemperature) {
1308 colorTemperature = maximumTemperature;
1309 }
1310
1311 return colorTemperature;
1312 }
Christine Franks83cc5412018-07-03 14:46:07 -07001313 }
1314
Long Ling1d3f1892019-02-06 12:34:02 -08001315 final class DisplayWhiteBalanceTintController extends TintController {
Christine Franks0ada2772019-02-25 13:54:57 -08001316
Long Ling1d3f1892019-02-06 12:34:02 -08001317 // Three chromaticity coordinates per color: X, Y, and Z
Christine Franks0ada2772019-02-25 13:54:57 -08001318 private static final int NUM_VALUES_PER_PRIMARY = 3;
Long Ling1d3f1892019-02-06 12:34:02 -08001319 // Four colors: red, green, blue, and white
Christine Franks0ada2772019-02-25 13:54:57 -08001320 private static final int NUM_DISPLAY_PRIMARIES_VALS = 4 * NUM_VALUES_PER_PRIMARY;
Long Ling1d3f1892019-02-06 12:34:02 -08001321
1322 private final Object mLock = new Object();
1323 @VisibleForTesting
1324 int mTemperatureMin;
1325 @VisibleForTesting
1326 int mTemperatureMax;
1327 private int mTemperatureDefault;
1328 private float[] mDisplayNominalWhiteXYZ = new float[NUM_VALUES_PER_PRIMARY];
1329 @VisibleForTesting
1330 ColorSpace.Rgb mDisplayColorSpaceRGB;
1331 private float[] mChromaticAdaptationMatrix;
1332 @VisibleForTesting
1333 int mCurrentColorTemperature;
1334 private float[] mCurrentColorTemperatureXYZ;
1335 private boolean mSetUp = false;
1336 private float[] mMatrixDisplayWhiteBalance = new float[16];
1337 private Boolean mIsAvailable;
1338
1339 @Override
1340 public void setUp(Context context, boolean needsLinear) {
1341 mSetUp = false;
1342 final Resources res = context.getResources();
1343
1344 ColorSpace.Rgb displayColorSpaceRGB = getDisplayColorSpaceFromSurfaceControl();
1345 if (displayColorSpaceRGB == null) {
1346 Slog.w(TAG, "Failed to get display color space from SurfaceControl, trying res");
1347 displayColorSpaceRGB = getDisplayColorSpaceFromResources(res);
1348 if (displayColorSpaceRGB == null) {
1349 Slog.e(TAG, "Failed to get display color space from resources");
1350 return;
1351 }
1352 }
1353
1354 final String[] nominalWhiteValues = res.getStringArray(
1355 R.array.config_displayWhiteBalanceDisplayNominalWhite);
1356 float[] displayNominalWhiteXYZ = new float[NUM_VALUES_PER_PRIMARY];
1357 for (int i = 0; i < nominalWhiteValues.length; i++) {
1358 displayNominalWhiteXYZ[i] = Float.parseFloat(nominalWhiteValues[i]);
1359 }
1360
1361 final int colorTemperatureMin = res.getInteger(
1362 R.integer.config_displayWhiteBalanceColorTemperatureMin);
1363 if (colorTemperatureMin <= 0) {
1364 Slog.e(TAG, "Display white balance minimum temperature must be greater than 0");
1365 return;
1366 }
1367
1368 final int colorTemperatureMax = res.getInteger(
1369 R.integer.config_displayWhiteBalanceColorTemperatureMax);
1370 if (colorTemperatureMax < colorTemperatureMin) {
1371 Slog.e(TAG, "Display white balance max temp must be greater or equal to min");
1372 return;
1373 }
1374
1375 final int colorTemperature = res.getInteger(
1376 R.integer.config_displayWhiteBalanceColorTemperatureDefault);
1377
1378 synchronized (mLock) {
1379 mDisplayColorSpaceRGB = displayColorSpaceRGB;
1380 mDisplayNominalWhiteXYZ = displayNominalWhiteXYZ;
1381 mTemperatureMin = colorTemperatureMin;
1382 mTemperatureMax = colorTemperatureMax;
1383 mTemperatureDefault = colorTemperature;
1384 mSetUp = true;
1385 }
1386
1387 setMatrix(mTemperatureDefault);
1388 }
1389
1390 @Override
1391 public float[] getMatrix() {
1392 return mSetUp && isActivated() ? mMatrixDisplayWhiteBalance : MATRIX_IDENTITY;
1393 }
1394
1395 @Override
1396 public void setMatrix(int cct) {
1397 if (!mSetUp) {
1398 Slog.w(TAG, "Can't set display white balance temperature: uninitialized");
1399 return;
1400 }
1401
1402 if (cct < mTemperatureMin) {
1403 Slog.w(TAG, "Requested display color temperature is below allowed minimum");
1404 cct = mTemperatureMin;
1405 } else if (cct > mTemperatureMax) {
1406 Slog.w(TAG, "Requested display color temperature is above allowed maximum");
1407 cct = mTemperatureMax;
1408 }
1409
1410 Slog.d(TAG, "setDisplayWhiteBalanceTemperatureMatrix: cct = " + cct);
1411
1412 synchronized (mLock) {
1413 mCurrentColorTemperature = cct;
1414
1415 // Adapt the display's nominal white point to match the requested CCT value
1416 mCurrentColorTemperatureXYZ = ColorSpace.cctToIlluminantdXyz(cct);
1417
1418 mChromaticAdaptationMatrix =
1419 ColorSpace.chromaticAdaptation(ColorSpace.Adaptation.BRADFORD,
1420 mDisplayNominalWhiteXYZ, mCurrentColorTemperatureXYZ);
1421
1422 // Convert the adaptation matrix to RGB space
1423 float[] result = ColorSpace.mul3x3(mChromaticAdaptationMatrix,
1424 mDisplayColorSpaceRGB.getTransform());
1425 result = ColorSpace.mul3x3(mDisplayColorSpaceRGB.getInverseTransform(), result);
1426
1427 // Normalize the transform matrix to peak white value in RGB space
1428 final float adaptedMaxR = result[0] + result[3] + result[6];
1429 final float adaptedMaxG = result[1] + result[4] + result[7];
1430 final float adaptedMaxB = result[2] + result[5] + result[8];
1431 final float denum = Math.max(Math.max(adaptedMaxR, adaptedMaxG), adaptedMaxB);
1432 for (int i = 0; i < result.length; i++) {
1433 result[i] /= denum;
1434 }
1435
1436 Matrix.setIdentityM(mMatrixDisplayWhiteBalance, 0);
1437 java.lang.System.arraycopy(result, 0, mMatrixDisplayWhiteBalance, 0, 3);
1438 java.lang.System.arraycopy(result, 3, mMatrixDisplayWhiteBalance, 4, 3);
1439 java.lang.System.arraycopy(result, 6, mMatrixDisplayWhiteBalance, 8, 3);
1440 }
1441 }
1442
1443 @Override
1444 public int getLevel() {
1445 return LEVEL_COLOR_MATRIX_DISPLAY_WHITE_BALANCE;
1446 }
1447
1448 @Override
1449 public boolean isAvailable(Context context) {
1450 if (mIsAvailable == null) {
1451 mIsAvailable = ColorDisplayManager.isDisplayWhiteBalanceAvailable(context);
1452 }
1453 return mIsAvailable;
1454 }
1455
1456 /**
1457 * Format a given matrix into a string.
1458 *
1459 * @param matrix the matrix to format
1460 * @param cols number of columns in the matrix
1461 */
1462 private String matrixToString(float[] matrix, int cols) {
1463 if (matrix == null || cols <= 0) {
1464 Slog.e(TAG, "Invalid arguments when formatting matrix to string");
1465 return "";
1466 }
1467
1468 StringBuilder sb = new StringBuilder("");
1469 for (int i = 0; i < matrix.length; i++) {
1470 if (i % cols == 0) {
1471 sb.append("\n ");
1472 }
1473 sb.append(String.format("%9.6f ", matrix[i]));
1474 }
1475 return sb.toString();
1476 }
1477
1478 @Override
1479 public void dump(PrintWriter pw) {
1480 synchronized (mLock) {
1481 pw.println(" mSetUp = " + mSetUp);
1482 if (!mSetUp) {
1483 return;
1484 }
1485
1486 pw.println(" mTemperatureMin = " + mTemperatureMin);
1487 pw.println(" mTemperatureMax = " + mTemperatureMax);
1488 pw.println(" mTemperatureDefault = " + mTemperatureDefault);
1489 pw.println(" mCurrentColorTemperature = " + mCurrentColorTemperature);
Christine Franks0ada2772019-02-25 13:54:57 -08001490 pw.println(" mCurrentColorTemperatureXYZ = "
1491 + matrixToString(mCurrentColorTemperatureXYZ, 3));
1492 pw.println(" mDisplayColorSpaceRGB RGB-to-XYZ = "
1493 + matrixToString(mDisplayColorSpaceRGB.getTransform(), 3));
1494 pw.println(" mChromaticAdaptationMatrix = "
1495 + matrixToString(mChromaticAdaptationMatrix, 3));
1496 pw.println(" mDisplayColorSpaceRGB XYZ-to-RGB = "
1497 + matrixToString(mDisplayColorSpaceRGB.getInverseTransform(), 3));
1498 pw.println(" mMatrixDisplayWhiteBalance = "
1499 + matrixToString(mMatrixDisplayWhiteBalance, 4));
Long Ling1d3f1892019-02-06 12:34:02 -08001500 }
1501 }
1502
1503 private ColorSpace.Rgb makeRgbColorSpaceFromXYZ(float[] redGreenBlueXYZ, float[] whiteXYZ) {
1504 return new ColorSpace.Rgb(
Christine Franks0ada2772019-02-25 13:54:57 -08001505 "Display Color Space",
1506 redGreenBlueXYZ,
1507 whiteXYZ,
1508 2.2f // gamma, unused for display white balance
Long Ling1d3f1892019-02-06 12:34:02 -08001509 );
1510 }
1511
1512 private ColorSpace.Rgb getDisplayColorSpaceFromSurfaceControl() {
1513 final IBinder displayToken = SurfaceControl.getInternalDisplayToken();
1514 if (displayToken == null) {
1515 return null;
1516 }
1517
1518 DisplayPrimaries primaries = SurfaceControl.getDisplayNativePrimaries(displayToken);
Christine Franks0ada2772019-02-25 13:54:57 -08001519 if (primaries == null || primaries.red == null || primaries.green == null
1520 || primaries.blue == null || primaries.white == null) {
Long Ling1d3f1892019-02-06 12:34:02 -08001521 return null;
1522 }
1523
1524 return makeRgbColorSpaceFromXYZ(
Christine Franks0ada2772019-02-25 13:54:57 -08001525 new float[]{
1526 primaries.red.X, primaries.red.Y, primaries.red.Z,
1527 primaries.green.X, primaries.green.Y, primaries.green.Z,
1528 primaries.blue.X, primaries.blue.Y, primaries.blue.Z,
Long Ling1d3f1892019-02-06 12:34:02 -08001529 },
Christine Franks0ada2772019-02-25 13:54:57 -08001530 new float[]{primaries.white.X, primaries.white.Y, primaries.white.Z}
1531 );
Long Ling1d3f1892019-02-06 12:34:02 -08001532 }
1533
1534 private ColorSpace.Rgb getDisplayColorSpaceFromResources(Resources res) {
1535 final String[] displayPrimariesValues = res.getStringArray(
1536 R.array.config_displayWhiteBalanceDisplayPrimaries);
1537 float[] displayRedGreenBlueXYZ =
1538 new float[NUM_DISPLAY_PRIMARIES_VALS - NUM_VALUES_PER_PRIMARY];
1539 float[] displayWhiteXYZ = new float[NUM_VALUES_PER_PRIMARY];
1540
1541 for (int i = 0; i < displayRedGreenBlueXYZ.length; i++) {
1542 displayRedGreenBlueXYZ[i] = Float.parseFloat(displayPrimariesValues[i]);
1543 }
1544
1545 for (int i = 0; i < displayWhiteXYZ.length; i++) {
1546 displayWhiteXYZ[i] = Float.parseFloat(
1547 displayPrimariesValues[displayRedGreenBlueXYZ.length + i]);
1548 }
1549
1550 return makeRgbColorSpaceFromXYZ(displayRedGreenBlueXYZ, displayWhiteXYZ);
1551 }
Christine Franks0ada2772019-02-25 13:54:57 -08001552 }
Long Ling1d3f1892019-02-06 12:34:02 -08001553
Christine Franks245ffd42018-11-16 13:45:14 -08001554 /**
1555 * Local service that allows color transforms to be enabled from other system services.
1556 */
1557 public final class ColorDisplayServiceInternal {
1558
1559 /**
1560 * Set the current CCT value for the display white balance transform, and if the transform
1561 * is enabled, apply it.
1562 *
1563 * @param cct the color temperature in Kelvin.
1564 */
1565 public boolean setDisplayWhiteBalanceColorTemperature(int cct) {
1566 // Update the transform matrix even if it can't be applied.
Christine Franks245ffd42018-11-16 13:45:14 -08001567 mDisplayWhiteBalanceTintController.setMatrix(cct);
1568
1569 if (mDisplayWhiteBalanceTintController.isActivated()) {
Christine Franksc7fb9452019-02-04 08:45:33 -08001570 mHandler.sendEmptyMessage(MSG_APPLY_DISPLAY_WHITE_BALANCE);
Christine Franks245ffd42018-11-16 13:45:14 -08001571 return true;
1572 }
1573 return false;
1574 }
1575
1576 /**
1577 * Sets the listener and returns whether display white balance is currently enabled.
1578 */
1579 public boolean setDisplayWhiteBalanceListener(DisplayWhiteBalanceListener listener) {
1580 mDisplayWhiteBalanceListener = listener;
1581 return mDisplayWhiteBalanceTintController.isActivated();
1582 }
Daniel Solomon8b72c5b2018-11-25 11:07:13 -08001583
Christine Franksf3529b22019-01-03 13:20:17 -08001584 /**
1585 * Adds a {@link WeakReference<ColorTransformController>} for a newly started activity, and
1586 * invokes {@link ColorTransformController#applyAppSaturation(float[], float[])} if needed.
1587 */
Christine Franks55194dc2019-01-15 13:47:06 -08001588 public boolean attachColorTransformController(String packageName, @UserIdInt int userId,
Christine Franksf3529b22019-01-03 13:20:17 -08001589 WeakReference<ColorTransformController> controller) {
1590 return mAppSaturationController
Christine Franks55194dc2019-01-15 13:47:06 -08001591 .addColorTransformController(packageName, userId, controller);
Christine Franksf3529b22019-01-03 13:20:17 -08001592 }
Christine Franks245ffd42018-11-16 13:45:14 -08001593 }
1594
1595 /**
1596 * Listener for changes in display white balance status.
1597 */
1598 public interface DisplayWhiteBalanceListener {
1599
1600 /**
1601 * Notify that the display white balance status has changed, either due to preemption by
1602 * another transform or the feature being turned off.
1603 */
1604 void onDisplayWhiteBalanceStatusChanged(boolean enabled);
1605 }
1606
Christine Franks09c229e2018-12-14 10:37:40 -08001607 private final class TintHandler extends Handler {
1608
Christine Franks83cc5412018-07-03 14:46:07 -07001609 private TintHandler(Looper looper) {
Christine Franks09c229e2018-12-14 10:37:40 -08001610 super(looper, null, true /* async */);
1611 }
1612
1613 @Override
1614 public void handleMessage(Message msg) {
1615 switch (msg.what) {
1616 case MSG_APPLY_GLOBAL_SATURATION:
1617 mGlobalSaturationTintController.setMatrix(msg.arg1);
1618 applyTint(mGlobalSaturationTintController, false);
1619 break;
Christine Franks83cc5412018-07-03 14:46:07 -07001620 case MSG_APPLY_NIGHT_DISPLAY_IMMEDIATE:
1621 applyTint(mNightDisplayTintController, true);
1622 break;
1623 case MSG_APPLY_NIGHT_DISPLAY_ANIMATED:
1624 applyTint(mNightDisplayTintController, false);
1625 break;
Christine Franksc7fb9452019-02-04 08:45:33 -08001626 case MSG_APPLY_DISPLAY_WHITE_BALANCE:
1627 applyTint(mDisplayWhiteBalanceTintController, false);
1628 break;
Christine Franks09c229e2018-12-14 10:37:40 -08001629 }
1630 }
1631 }
1632
Christine Franksf3529b22019-01-03 13:20:17 -08001633 /**
1634 * Interface for applying transforms to a given AppWindow.
1635 */
1636 public interface ColorTransformController {
1637
Christine Franksd154fe52019-01-04 17:17:45 -08001638 /**
1639 * Apply the given saturation (grayscale) matrix to the associated AppWindow.
1640 */
Christine Franksf3529b22019-01-03 13:20:17 -08001641 void applyAppSaturation(@Size(9) float[] matrix, @Size(3) float[] translation);
1642 }
1643
Christine Franks83cc5412018-07-03 14:46:07 -07001644 @VisibleForTesting
1645 final class BinderService extends IColorDisplayManager.Stub {
Christine Franks57fdde82018-07-03 14:46:07 -07001646
Christine Franks39b03112018-07-03 14:46:07 -07001647 @Override
Christine Franksd154fe52019-01-04 17:17:45 -08001648 public void setColorMode(int colorMode) {
1649 getContext().enforceCallingOrSelfPermission(
1650 Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS,
1651 "Permission required to set display color mode");
1652 final long token = Binder.clearCallingIdentity();
1653 try {
1654 setColorModeInternal(colorMode);
1655 } finally {
1656 Binder.restoreCallingIdentity(token);
1657 }
1658 }
1659
1660 @Override
1661 public int getColorMode() {
1662 final long token = Binder.clearCallingIdentity();
1663 try {
1664 return getColorModeInternal();
1665 } finally {
1666 Binder.restoreCallingIdentity(token);
1667 }
1668 }
1669
1670 @Override
Christine Franks39b03112018-07-03 14:46:07 -07001671 public boolean isDeviceColorManaged() {
1672 final long token = Binder.clearCallingIdentity();
1673 try {
1674 return isDeviceColorManagedInternal();
1675 } finally {
1676 Binder.restoreCallingIdentity(token);
1677 }
1678 }
Christine Franks09c229e2018-12-14 10:37:40 -08001679
1680 @Override
1681 public boolean setSaturationLevel(int level) {
1682 final boolean hasTransformsPermission = getContext()
1683 .checkCallingPermission(Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS)
1684 == PackageManager.PERMISSION_GRANTED;
1685 final boolean hasLegacyPermission = getContext()
1686 .checkCallingPermission(Manifest.permission.CONTROL_DISPLAY_SATURATION)
1687 == PackageManager.PERMISSION_GRANTED;
1688 if (!hasTransformsPermission && !hasLegacyPermission) {
1689 throw new SecurityException("Permission required to set display saturation level");
1690 }
1691 final long token = Binder.clearCallingIdentity();
1692 try {
1693 final Message message = mHandler.obtainMessage(MSG_APPLY_GLOBAL_SATURATION);
1694 message.arg1 = level;
1695 mHandler.sendMessage(message);
1696 } finally {
1697 Binder.restoreCallingIdentity(token);
1698 }
1699 return true;
1700 }
Christine Franksf3529b22019-01-03 13:20:17 -08001701
1702 @Override
Christine Franks6d21d342019-02-07 15:09:03 -08001703 public boolean isSaturationActivated() {
1704 getContext().enforceCallingPermission(
1705 Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS,
1706 "Permission required to get display saturation level");
1707 final long token = Binder.clearCallingIdentity();
1708 try {
1709 return !mGlobalSaturationTintController.isActivatedStateNotSet()
1710 && mGlobalSaturationTintController.isActivated();
1711 } finally {
1712 Binder.restoreCallingIdentity(token);
1713 }
1714 }
1715
1716 @Override
Christine Franksf3529b22019-01-03 13:20:17 -08001717 public boolean setAppSaturationLevel(String packageName, int level) {
1718 getContext().enforceCallingPermission(
1719 Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS,
1720 "Permission required to set display saturation level");
1721 final long token = Binder.clearCallingIdentity();
1722 try {
1723 return setAppSaturationLevelInternal(packageName, level);
1724 } finally {
1725 Binder.restoreCallingIdentity(token);
1726 }
1727 }
1728
Christine Franks55194dc2019-01-15 13:47:06 -08001729 public int getTransformCapabilities() {
1730 getContext().enforceCallingPermission(
1731 Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS,
1732 "Permission required to query transform capabilities");
1733 final long token = Binder.clearCallingIdentity();
1734 try {
1735 return getTransformCapabilitiesInternal();
1736 } finally {
1737 Binder.restoreCallingIdentity(token);
1738 }
1739 }
1740
Christine Franksf3529b22019-01-03 13:20:17 -08001741 @Override
Christine Franks83cc5412018-07-03 14:46:07 -07001742 public boolean setNightDisplayActivated(boolean activated) {
1743 getContext().enforceCallingOrSelfPermission(
1744 Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS,
1745 "Permission required to set night display activated");
1746 final long token = Binder.clearCallingIdentity();
1747 try {
1748 mNightDisplayTintController.setActivated(activated);
1749 return true;
1750 } finally {
1751 Binder.restoreCallingIdentity(token);
1752 }
1753 }
1754
1755 @Override
1756 public boolean isNightDisplayActivated() {
1757 final long token = Binder.clearCallingIdentity();
1758 try {
1759 return mNightDisplayTintController.isActivated();
1760 } finally {
1761 Binder.restoreCallingIdentity(token);
1762 }
1763 }
1764
1765 @Override
1766 public boolean setNightDisplayColorTemperature(int temperature) {
1767 getContext().enforceCallingOrSelfPermission(
1768 Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS,
1769 "Permission required to set night display temperature");
1770 final long token = Binder.clearCallingIdentity();
1771 try {
1772 return mNightDisplayTintController.setColorTemperature(temperature);
1773 } finally {
1774 Binder.restoreCallingIdentity(token);
1775 }
1776 }
1777
1778 @Override
1779 public int getNightDisplayColorTemperature() {
1780 final long token = Binder.clearCallingIdentity();
1781 try {
1782 return mNightDisplayTintController.getColorTemperature();
1783 } finally {
1784 Binder.restoreCallingIdentity(token);
1785 }
1786 }
1787
1788 @Override
1789 public boolean setNightDisplayAutoMode(int autoMode) {
1790 getContext().enforceCallingOrSelfPermission(
1791 Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS,
1792 "Permission required to set night display auto mode");
1793 final long token = Binder.clearCallingIdentity();
1794 try {
1795 return setNightDisplayAutoModeInternal(autoMode);
1796 } finally {
1797 Binder.restoreCallingIdentity(token);
1798 }
1799 }
1800
1801 @Override
1802 public int getNightDisplayAutoMode() {
1803 getContext().enforceCallingOrSelfPermission(
1804 Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS,
1805 "Permission required to get night display auto mode");
1806 final long token = Binder.clearCallingIdentity();
1807 try {
1808 return getNightDisplayAutoModeInternal();
1809 } finally {
1810 Binder.restoreCallingIdentity(token);
1811 }
1812 }
1813
1814 @Override
1815 public int getNightDisplayAutoModeRaw() {
1816 final long token = Binder.clearCallingIdentity();
1817 try {
1818 return getNightDisplayAutoModeRawInternal();
1819 } finally {
1820 Binder.restoreCallingIdentity(token);
1821 }
1822 }
1823
1824 @Override
1825 public boolean setNightDisplayCustomStartTime(Time startTime) {
1826 getContext().enforceCallingOrSelfPermission(
1827 Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS,
1828 "Permission required to set night display custom start time");
1829 final long token = Binder.clearCallingIdentity();
1830 try {
1831 return setNightDisplayCustomStartTimeInternal(startTime);
1832 } finally {
1833 Binder.restoreCallingIdentity(token);
1834 }
1835 }
1836
1837 @Override
1838 public Time getNightDisplayCustomStartTime() {
1839 final long token = Binder.clearCallingIdentity();
1840 try {
1841 return getNightDisplayCustomStartTimeInternal();
1842 } finally {
1843 Binder.restoreCallingIdentity(token);
1844 }
1845 }
1846
1847 @Override
1848 public boolean setNightDisplayCustomEndTime(Time endTime) {
1849 getContext().enforceCallingOrSelfPermission(
1850 Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS,
1851 "Permission required to set night display custom end time");
1852 final long token = Binder.clearCallingIdentity();
1853 try {
1854 return setNightDisplayCustomEndTimeInternal(endTime);
1855 } finally {
1856 Binder.restoreCallingIdentity(token);
1857 }
1858 }
1859
1860 @Override
1861 public Time getNightDisplayCustomEndTime() {
1862 final long token = Binder.clearCallingIdentity();
1863 try {
1864 return getNightDisplayCustomEndTimeInternal();
1865 } finally {
1866 Binder.restoreCallingIdentity(token);
1867 }
1868 }
1869
1870 @Override
Christine Franksf3529b22019-01-03 13:20:17 -08001871 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Christine Franksd154fe52019-01-04 17:17:45 -08001872 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
1873 return;
1874 }
Christine Franksf3529b22019-01-03 13:20:17 -08001875
1876 final long token = Binder.clearCallingIdentity();
1877 try {
1878 dumpInternal(pw);
1879 } finally {
1880 Binder.restoreCallingIdentity(token);
1881 }
1882 }
Christine Franks39b03112018-07-03 14:46:07 -07001883 }
Justin Klaassen911e8892016-06-21 18:24:24 -07001884}