Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.display; |
| 18 | |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.TypeEvaluator; |
| 22 | import android.animation.ValueAnimator; |
Justin Klaassen | 4346f63 | 2016-08-08 15:01:47 -0700 | [diff] [blame] | 23 | import android.annotation.NonNull; |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 24 | import android.annotation.Nullable; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 25 | import android.app.AlarmManager; |
| 26 | import android.content.BroadcastReceiver; |
| 27 | import android.content.ContentResolver; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.IntentFilter; |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 31 | import android.database.ContentObserver; |
| 32 | import android.net.Uri; |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 33 | import android.opengl.Matrix; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 34 | import android.os.Handler; |
| 35 | import android.os.Looper; |
Ruben Brunk | de003ae | 2016-08-30 13:14:33 -0700 | [diff] [blame] | 36 | import android.os.RemoteException; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 37 | import android.os.UserHandle; |
| 38 | import android.provider.Settings.Secure; |
Ruben Brunk | de003ae | 2016-08-30 13:14:33 -0700 | [diff] [blame] | 39 | import android.service.vr.IVrManager; |
| 40 | import android.service.vr.IVrStateCallbacks; |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 41 | import android.util.MathUtils; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 42 | import android.util.Slog; |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 43 | import android.view.animation.AnimationUtils; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 44 | |
| 45 | import com.android.internal.app.NightDisplayController; |
| 46 | import com.android.server.SystemService; |
| 47 | import com.android.server.twilight.TwilightListener; |
| 48 | import com.android.server.twilight.TwilightManager; |
| 49 | import com.android.server.twilight.TwilightState; |
| 50 | |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 51 | import java.time.LocalDateTime; |
| 52 | import java.time.LocalTime; |
| 53 | import java.time.ZoneId; |
Ruben Brunk | de003ae | 2016-08-30 13:14:33 -0700 | [diff] [blame] | 54 | import java.util.concurrent.atomic.AtomicBoolean; |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame^] | 55 | |
| 56 | import com.android.internal.R; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 57 | |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 58 | import static com.android.server.display.DisplayTransformManager.LEVEL_COLOR_MATRIX_NIGHT_DISPLAY; |
| 59 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Tints the display at night. |
| 62 | */ |
| 63 | public final class NightDisplayService extends SystemService |
| 64 | implements NightDisplayController.Callback { |
| 65 | |
| 66 | private static final String TAG = "NightDisplayService"; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 67 | |
| 68 | /** |
Christine Franks | 7b83b428 | 2017-01-18 14:55:00 -0800 | [diff] [blame] | 69 | * The transition time, in milliseconds, for Night Display to turn on/off. |
| 70 | */ |
| 71 | private static final long TRANSITION_DURATION = 3000L; |
| 72 | |
| 73 | /** |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 74 | * The identity matrix, used if one of the given matrices is {@code null}. |
| 75 | */ |
| 76 | private static final float[] MATRIX_IDENTITY = new float[16]; |
| 77 | static { |
| 78 | Matrix.setIdentityM(MATRIX_IDENTITY, 0); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Evaluator used to animate color matrix transitions. |
| 83 | */ |
| 84 | private static final ColorMatrixEvaluator COLOR_MATRIX_EVALUATOR = new ColorMatrixEvaluator(); |
| 85 | |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 86 | private final Handler mHandler; |
Ruben Brunk | de003ae | 2016-08-30 13:14:33 -0700 | [diff] [blame] | 87 | private final AtomicBoolean mIgnoreAllColorMatrixChanges = new AtomicBoolean(); |
| 88 | private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() { |
| 89 | @Override |
| 90 | public void onVrStateChanged(final boolean enabled) { |
| 91 | // Turn off all night mode display stuff while device is in VR mode. |
| 92 | mIgnoreAllColorMatrixChanges.set(enabled); |
| 93 | mHandler.post(new Runnable() { |
| 94 | @Override |
| 95 | public void run() { |
| 96 | // Cancel in-progress animations |
| 97 | if (mColorMatrixAnimator != null) { |
| 98 | mColorMatrixAnimator.cancel(); |
| 99 | } |
| 100 | |
| 101 | final DisplayTransformManager dtm = |
| 102 | getLocalService(DisplayTransformManager.class); |
| 103 | if (enabled) { |
| 104 | dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, MATRIX_IDENTITY); |
Steven Thomas | 1e9b4b0 | 2017-01-10 16:54:42 -0800 | [diff] [blame] | 105 | } else if (mController != null && mController.isActivated()) { |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 106 | setMatrix(mController.getColorTemperature(), mMatrixNight); |
| 107 | dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, mMatrixNight); |
Ruben Brunk | de003ae | 2016-08-30 13:14:33 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | }); |
| 111 | } |
| 112 | }; |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 113 | |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 114 | private float[] mMatrixNight = new float[16]; |
| 115 | |
Christine Franks | 2a12547 | 2017-07-26 17:51:16 -0700 | [diff] [blame] | 116 | private final float[] mColorTempCoefficients = new float[9]; |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 117 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 118 | private int mCurrentUser = UserHandle.USER_NULL; |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 119 | private ContentObserver mUserSetupObserver; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 120 | private boolean mBootCompleted; |
| 121 | |
| 122 | private NightDisplayController mController; |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 123 | private ValueAnimator mColorMatrixAnimator; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 124 | private Boolean mIsActivated; |
| 125 | private AutoMode mAutoMode; |
| 126 | |
| 127 | public NightDisplayService(Context context) { |
| 128 | super(context); |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 129 | mHandler = new Handler(Looper.getMainLooper()); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void onStart() { |
| 134 | // Nothing to publish. |
| 135 | } |
| 136 | |
| 137 | @Override |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 138 | public void onBootPhase(int phase) { |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 139 | if (phase >= PHASE_SYSTEM_SERVICES_READY) { |
| 140 | final IVrManager vrManager = IVrManager.Stub.asInterface( |
| 141 | getBinderService(Context.VR_SERVICE)); |
Ruben Brunk | de003ae | 2016-08-30 13:14:33 -0700 | [diff] [blame] | 142 | if (vrManager != null) { |
| 143 | try { |
| 144 | vrManager.registerListener(mVrStateCallbacks); |
| 145 | } catch (RemoteException e) { |
| 146 | Slog.e(TAG, "Failed to register VR mode state listener: " + e); |
| 147 | } |
| 148 | } |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | if (phase >= PHASE_BOOT_COMPLETED) { |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 152 | mBootCompleted = true; |
| 153 | |
| 154 | // Register listeners now that boot is complete. |
| 155 | if (mCurrentUser != UserHandle.USER_NULL && mUserSetupObserver == null) { |
| 156 | setUp(); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | @Override |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 162 | public void onStartUser(int userHandle) { |
| 163 | super.onStartUser(userHandle); |
| 164 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 165 | if (mCurrentUser == UserHandle.USER_NULL) { |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 166 | onUserChanged(userHandle); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void onSwitchUser(int userHandle) { |
| 172 | super.onSwitchUser(userHandle); |
| 173 | |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 174 | onUserChanged(userHandle); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void onStopUser(int userHandle) { |
| 179 | super.onStopUser(userHandle); |
| 180 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 181 | if (mCurrentUser == userHandle) { |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 182 | onUserChanged(UserHandle.USER_NULL); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 186 | private void onUserChanged(int userHandle) { |
| 187 | final ContentResolver cr = getContext().getContentResolver(); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 188 | |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 189 | if (mCurrentUser != UserHandle.USER_NULL) { |
| 190 | if (mUserSetupObserver != null) { |
| 191 | cr.unregisterContentObserver(mUserSetupObserver); |
| 192 | mUserSetupObserver = null; |
| 193 | } else if (mBootCompleted) { |
| 194 | tearDown(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | mCurrentUser = userHandle; |
| 199 | |
| 200 | if (mCurrentUser != UserHandle.USER_NULL) { |
| 201 | if (!isUserSetupCompleted(cr, mCurrentUser)) { |
| 202 | mUserSetupObserver = new ContentObserver(mHandler) { |
| 203 | @Override |
| 204 | public void onChange(boolean selfChange, Uri uri) { |
| 205 | if (isUserSetupCompleted(cr, mCurrentUser)) { |
| 206 | cr.unregisterContentObserver(this); |
| 207 | mUserSetupObserver = null; |
| 208 | |
| 209 | if (mBootCompleted) { |
| 210 | setUp(); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | }; |
| 215 | cr.registerContentObserver(Secure.getUriFor(Secure.USER_SETUP_COMPLETE), |
| 216 | false /* notifyForDescendents */, mUserSetupObserver, mCurrentUser); |
| 217 | } else if (mBootCompleted) { |
| 218 | setUp(); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 223 | private static boolean isUserSetupCompleted(ContentResolver cr, int userHandle) { |
| 224 | return Secure.getIntForUser(cr, Secure.USER_SETUP_COMPLETE, 0, userHandle) == 1; |
| 225 | } |
| 226 | |
| 227 | private void setUp() { |
Justin Klaassen | ec8837a | 2016-08-23 12:04:42 -0700 | [diff] [blame] | 228 | Slog.d(TAG, "setUp: currentUser=" + mCurrentUser); |
| 229 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 230 | // Create a new controller for the current user and start listening for changes. |
| 231 | mController = new NightDisplayController(getContext(), mCurrentUser); |
| 232 | mController.setListener(this); |
| 233 | |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame^] | 234 | setCoefficientMatrix(getContext()); |
| 235 | |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 236 | // Prepare color transformation matrix. |
| 237 | setMatrix(mController.getColorTemperature(), mMatrixNight); |
| 238 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 239 | // Initialize the current auto mode. |
| 240 | onAutoModeChanged(mController.getAutoMode()); |
| 241 | |
| 242 | // Force the initialization current activated state. |
| 243 | if (mIsActivated == null) { |
| 244 | onActivated(mController.isActivated()); |
| 245 | } |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 246 | |
| 247 | // Transition the screen to the current temperature. |
| 248 | applyTint(false); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 251 | private void tearDown() { |
Justin Klaassen | ec8837a | 2016-08-23 12:04:42 -0700 | [diff] [blame] | 252 | Slog.d(TAG, "tearDown: currentUser=" + mCurrentUser); |
| 253 | |
Justin Klaassen | 2696d99 | 2016-07-11 21:26:46 -0700 | [diff] [blame] | 254 | if (mController != null) { |
| 255 | mController.setListener(null); |
| 256 | mController = null; |
| 257 | } |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 258 | |
| 259 | if (mAutoMode != null) { |
| 260 | mAutoMode.onStop(); |
| 261 | mAutoMode = null; |
| 262 | } |
| 263 | |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 264 | if (mColorMatrixAnimator != null) { |
| 265 | mColorMatrixAnimator.end(); |
| 266 | mColorMatrixAnimator = null; |
| 267 | } |
| 268 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 269 | mIsActivated = null; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public void onActivated(boolean activated) { |
| 274 | if (mIsActivated == null || mIsActivated != activated) { |
| 275 | Slog.i(TAG, activated ? "Turning on night display" : "Turning off night display"); |
| 276 | |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 277 | mIsActivated = activated; |
| 278 | |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 279 | if (mAutoMode != null) { |
| 280 | mAutoMode.onActivated(activated); |
| 281 | } |
| 282 | |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 283 | applyTint(false); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public void onAutoModeChanged(int autoMode) { |
Justin Klaassen | ec8837a | 2016-08-23 12:04:42 -0700 | [diff] [blame] | 289 | Slog.d(TAG, "onAutoModeChanged: autoMode=" + autoMode); |
| 290 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 291 | if (mAutoMode != null) { |
| 292 | mAutoMode.onStop(); |
| 293 | mAutoMode = null; |
| 294 | } |
| 295 | |
| 296 | if (autoMode == NightDisplayController.AUTO_MODE_CUSTOM) { |
| 297 | mAutoMode = new CustomAutoMode(); |
| 298 | } else if (autoMode == NightDisplayController.AUTO_MODE_TWILIGHT) { |
| 299 | mAutoMode = new TwilightAutoMode(); |
| 300 | } |
| 301 | |
| 302 | if (mAutoMode != null) { |
| 303 | mAutoMode.onStart(); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | @Override |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 308 | public void onCustomStartTimeChanged(LocalTime startTime) { |
Justin Klaassen | ec8837a | 2016-08-23 12:04:42 -0700 | [diff] [blame] | 309 | Slog.d(TAG, "onCustomStartTimeChanged: startTime=" + startTime); |
| 310 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 311 | if (mAutoMode != null) { |
| 312 | mAutoMode.onCustomStartTimeChanged(startTime); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | @Override |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 317 | public void onCustomEndTimeChanged(LocalTime endTime) { |
Justin Klaassen | ec8837a | 2016-08-23 12:04:42 -0700 | [diff] [blame] | 318 | Slog.d(TAG, "onCustomEndTimeChanged: endTime=" + endTime); |
| 319 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 320 | if (mAutoMode != null) { |
| 321 | mAutoMode.onCustomEndTimeChanged(endTime); |
| 322 | } |
| 323 | } |
| 324 | |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 325 | @Override |
| 326 | public void onColorTemperatureChanged(int colorTemperature) { |
| 327 | setMatrix(colorTemperature, mMatrixNight); |
| 328 | applyTint(true); |
| 329 | } |
| 330 | |
Christine Franks | 8ad7149 | 2017-10-24 19:04:22 -0700 | [diff] [blame^] | 331 | @Override |
| 332 | public void onDisplayColorModeChanged(int colorMode) { |
| 333 | final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class); |
| 334 | dtm.setColorMode(colorMode); |
| 335 | |
| 336 | setCoefficientMatrix(getContext()); |
| 337 | setMatrix(mController.getColorTemperature(), mMatrixNight); |
| 338 | applyTint(true); |
| 339 | } |
| 340 | |
| 341 | private void setCoefficientMatrix(Context context) { |
| 342 | final boolean isNative = DisplayTransformManager.isNativeModeEnabled(); |
| 343 | final String[] coefficients = context.getResources().getStringArray(isNative |
| 344 | ? R.array.config_nightDisplayColorTemperatureCoefficientsNative |
| 345 | : R.array.config_nightDisplayColorTemperatureCoefficients); |
| 346 | for (int i = 0; i < 9 && i < coefficients.length; i++) { |
| 347 | mColorTempCoefficients[i] = Float.parseFloat(coefficients[i]); |
| 348 | } |
| 349 | } |
| 350 | |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 351 | /** |
| 352 | * Applies current color temperature matrix, or removes it if deactivated. |
| 353 | * |
| 354 | * @param immediate {@code true} skips transition animation |
| 355 | */ |
| 356 | private void applyTint(boolean immediate) { |
| 357 | // Cancel the old animator if still running. |
| 358 | if (mColorMatrixAnimator != null) { |
| 359 | mColorMatrixAnimator.cancel(); |
| 360 | } |
| 361 | |
| 362 | // Don't do any color matrix change animations if we are ignoring them anyway. |
| 363 | if (mIgnoreAllColorMatrixChanges.get()) { |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class); |
| 368 | final float[] from = dtm.getColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY); |
| 369 | final float[] to = mIsActivated ? mMatrixNight : MATRIX_IDENTITY; |
| 370 | |
| 371 | if (immediate) { |
| 372 | dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, to); |
| 373 | } else { |
| 374 | mColorMatrixAnimator = ValueAnimator.ofObject(COLOR_MATRIX_EVALUATOR, |
| 375 | from == null ? MATRIX_IDENTITY : from, to); |
| 376 | mColorMatrixAnimator.setDuration(TRANSITION_DURATION); |
| 377 | mColorMatrixAnimator.setInterpolator(AnimationUtils.loadInterpolator( |
| 378 | getContext(), android.R.interpolator.fast_out_slow_in)); |
| 379 | mColorMatrixAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 380 | @Override |
| 381 | public void onAnimationUpdate(ValueAnimator animator) { |
| 382 | final float[] value = (float[]) animator.getAnimatedValue(); |
| 383 | dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, value); |
| 384 | } |
| 385 | }); |
| 386 | mColorMatrixAnimator.addListener(new AnimatorListenerAdapter() { |
| 387 | |
| 388 | private boolean mIsCancelled; |
| 389 | |
| 390 | @Override |
| 391 | public void onAnimationCancel(Animator animator) { |
| 392 | mIsCancelled = true; |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public void onAnimationEnd(Animator animator) { |
| 397 | if (!mIsCancelled) { |
| 398 | // Ensure final color matrix is set at the end of the animation. If the |
| 399 | // animation is cancelled then don't set the final color matrix so the new |
| 400 | // animator can pick up from where this one left off. |
| 401 | dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, to); |
| 402 | } |
| 403 | mColorMatrixAnimator = null; |
| 404 | } |
| 405 | }); |
| 406 | mColorMatrixAnimator.start(); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Set the color transformation {@code MATRIX_NIGHT} to the given color temperature. |
| 412 | * |
| 413 | * @param colorTemperature color temperature in Kelvin |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 414 | * @param outTemp the 4x4 display transformation matrix for that color temperature |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 415 | */ |
| 416 | private void setMatrix(int colorTemperature, float[] outTemp) { |
| 417 | if (outTemp.length != 16) { |
| 418 | Slog.d(TAG, "The display transformation matrix must be 4x4"); |
| 419 | return; |
| 420 | } |
| 421 | |
| 422 | Matrix.setIdentityM(mMatrixNight, 0); |
| 423 | |
| 424 | final float squareTemperature = colorTemperature * colorTemperature; |
| 425 | final float red = squareTemperature * mColorTempCoefficients[0] |
Christine Franks | 2a12547 | 2017-07-26 17:51:16 -0700 | [diff] [blame] | 426 | + colorTemperature * mColorTempCoefficients[1] + mColorTempCoefficients[2]; |
| 427 | final float green = squareTemperature * mColorTempCoefficients[3] |
| 428 | + colorTemperature * mColorTempCoefficients[4] + mColorTempCoefficients[5]; |
| 429 | final float blue = squareTemperature * mColorTempCoefficients[6] |
| 430 | + colorTemperature * mColorTempCoefficients[7] + mColorTempCoefficients[8]; |
Christine Franks | 6418d0b | 2017-02-13 09:48:00 -0800 | [diff] [blame] | 431 | outTemp[0] = red; |
| 432 | outTemp[5] = green; |
| 433 | outTemp[10] = blue; |
| 434 | } |
| 435 | |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 436 | /** |
| 437 | * Returns the first date time corresponding to the local time that occurs before the |
| 438 | * provided date time. |
| 439 | * |
| 440 | * @param compareTime the LocalDateTime to compare against |
| 441 | * @return the prior LocalDateTime corresponding to this local time |
| 442 | */ |
| 443 | public static LocalDateTime getDateTimeBefore(LocalTime localTime, LocalDateTime compareTime) { |
| 444 | final LocalDateTime ldt = LocalDateTime.of(compareTime.getYear(), compareTime.getMonth(), |
| 445 | compareTime.getDayOfMonth(), localTime.getHour(), localTime.getMinute()); |
| 446 | |
| 447 | // Check if the local time has passed, if so return the same time yesterday. |
| 448 | return ldt.isAfter(compareTime) ? ldt.minusDays(1) : ldt; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * Returns the first date time corresponding to this local time that occurs after the |
| 453 | * provided date time. |
| 454 | * |
| 455 | * @param compareTime the LocalDateTime to compare against |
| 456 | * @return the next LocalDateTime corresponding to this local time |
| 457 | */ |
| 458 | public static LocalDateTime getDateTimeAfter(LocalTime localTime, LocalDateTime compareTime) { |
| 459 | final LocalDateTime ldt = LocalDateTime.of(compareTime.getYear(), compareTime.getMonth(), |
| 460 | compareTime.getDayOfMonth(), localTime.getHour(), localTime.getMinute()); |
| 461 | |
| 462 | // Check if the local time has passed, if so return the same time tomorrow. |
| 463 | return ldt.isBefore(compareTime) ? ldt.plusDays(1) : ldt; |
| 464 | } |
| 465 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 466 | private abstract class AutoMode implements NightDisplayController.Callback { |
| 467 | public abstract void onStart(); |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 468 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 469 | public abstract void onStop(); |
| 470 | } |
| 471 | |
| 472 | private class CustomAutoMode extends AutoMode implements AlarmManager.OnAlarmListener { |
| 473 | |
| 474 | private final AlarmManager mAlarmManager; |
| 475 | private final BroadcastReceiver mTimeChangedReceiver; |
| 476 | |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 477 | private LocalTime mStartTime; |
| 478 | private LocalTime mEndTime; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 479 | |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 480 | private LocalDateTime mLastActivatedTime; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 481 | |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 482 | CustomAutoMode() { |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 483 | mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); |
| 484 | mTimeChangedReceiver = new BroadcastReceiver() { |
| 485 | @Override |
| 486 | public void onReceive(Context context, Intent intent) { |
| 487 | updateActivated(); |
| 488 | } |
| 489 | }; |
| 490 | } |
| 491 | |
| 492 | private void updateActivated() { |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 493 | final LocalDateTime now = LocalDateTime.now(); |
| 494 | final LocalDateTime start = getDateTimeBefore(mStartTime, now); |
| 495 | final LocalDateTime end = getDateTimeAfter(mEndTime, start); |
| 496 | boolean activate = now.isBefore(end); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 497 | |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 498 | if (mLastActivatedTime != null) { |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 499 | // Maintain the existing activated state if within the current period. |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 500 | if (mLastActivatedTime.isBefore(now) && mLastActivatedTime.isAfter(start) |
| 501 | && (mLastActivatedTime.isAfter(end) || now.isBefore(end))) { |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 502 | activate = mController.isActivated(); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 506 | if (mIsActivated == null || mIsActivated != activate) { |
| 507 | mController.setActivated(activate); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 508 | } |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 509 | |
Justin Klaassen | 4346f63 | 2016-08-08 15:01:47 -0700 | [diff] [blame] | 510 | updateNextAlarm(mIsActivated, now); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 513 | private void updateNextAlarm(@Nullable Boolean activated, @NonNull LocalDateTime now) { |
Justin Klaassen | 4346f63 | 2016-08-08 15:01:47 -0700 | [diff] [blame] | 514 | if (activated != null) { |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 515 | final LocalDateTime next = activated ? getDateTimeAfter(mEndTime, now) |
| 516 | : getDateTimeAfter(mStartTime, now); |
| 517 | final long millis = next.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); |
| 518 | mAlarmManager.setExact(AlarmManager.RTC, millis, TAG, this, null); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
| 522 | @Override |
| 523 | public void onStart() { |
| 524 | final IntentFilter intentFilter = new IntentFilter(Intent.ACTION_TIME_CHANGED); |
| 525 | intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED); |
| 526 | getContext().registerReceiver(mTimeChangedReceiver, intentFilter); |
| 527 | |
| 528 | mStartTime = mController.getCustomStartTime(); |
| 529 | mEndTime = mController.getCustomEndTime(); |
| 530 | |
Christine Franks | 1454eae | 2017-05-31 10:52:22 -0700 | [diff] [blame] | 531 | mLastActivatedTime = mController.getLastActivatedTime(); |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 532 | |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 533 | // Force an update to initialize state. |
| 534 | updateActivated(); |
| 535 | } |
| 536 | |
| 537 | @Override |
| 538 | public void onStop() { |
| 539 | getContext().unregisterReceiver(mTimeChangedReceiver); |
| 540 | |
| 541 | mAlarmManager.cancel(this); |
| 542 | mLastActivatedTime = null; |
| 543 | } |
| 544 | |
| 545 | @Override |
| 546 | public void onActivated(boolean activated) { |
Christine Franks | 1454eae | 2017-05-31 10:52:22 -0700 | [diff] [blame] | 547 | mLastActivatedTime = mController.getLastActivatedTime(); |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 548 | updateNextAlarm(activated, LocalDateTime.now()); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | @Override |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 552 | public void onCustomStartTimeChanged(LocalTime startTime) { |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 553 | mStartTime = startTime; |
| 554 | mLastActivatedTime = null; |
| 555 | updateActivated(); |
| 556 | } |
| 557 | |
| 558 | @Override |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 559 | public void onCustomEndTimeChanged(LocalTime endTime) { |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 560 | mEndTime = endTime; |
| 561 | mLastActivatedTime = null; |
| 562 | updateActivated(); |
| 563 | } |
| 564 | |
| 565 | @Override |
| 566 | public void onAlarm() { |
Justin Klaassen | ec8837a | 2016-08-23 12:04:42 -0700 | [diff] [blame] | 567 | Slog.d(TAG, "onAlarm"); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 568 | updateActivated(); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | private class TwilightAutoMode extends AutoMode implements TwilightListener { |
| 573 | |
| 574 | private final TwilightManager mTwilightManager; |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 575 | |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 576 | TwilightAutoMode() { |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 577 | mTwilightManager = getLocalService(TwilightManager.class); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 580 | private void updateActivated(TwilightState state) { |
Justin Klaassen | 3da4c44 | 2017-05-05 15:19:33 -0700 | [diff] [blame] | 581 | if (state == null) { |
| 582 | // If there isn't a valid TwilightState then just keep the current activated |
| 583 | // state. |
| 584 | return; |
| 585 | } |
| 586 | |
| 587 | boolean activate = state.isNight(); |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 588 | final LocalDateTime lastActivatedTime = mController.getLastActivatedTime(); |
Justin Klaassen | 3da4c44 | 2017-05-05 15:19:33 -0700 | [diff] [blame] | 589 | if (lastActivatedTime != null) { |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 590 | final LocalDateTime now = LocalDateTime.now(); |
| 591 | final LocalDateTime sunrise = state.sunrise(); |
| 592 | final LocalDateTime sunset = state.sunset(); |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 593 | // Maintain the existing activated state if within the current period. |
Christine Franks | 0321346 | 2017-08-25 13:57:26 -0700 | [diff] [blame] | 594 | if (lastActivatedTime.isBefore(now) && (lastActivatedTime.isBefore(sunrise) |
| 595 | ^ lastActivatedTime.isBefore(sunset))) { |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 596 | activate = mController.isActivated(); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 597 | } |
| 598 | } |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 599 | |
Christine Franks | e5bb03e | 2017-02-10 17:36:10 -0800 | [diff] [blame] | 600 | if (mIsActivated == null || mIsActivated != activate) { |
| 601 | mController.setActivated(activate); |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 602 | } |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | @Override |
| 606 | public void onStart() { |
| 607 | mTwilightManager.registerListener(this, mHandler); |
| 608 | |
| 609 | // Force an update to initialize state. |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 610 | updateActivated(mTwilightManager.getLastTwilightState()); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | @Override |
| 614 | public void onStop() { |
| 615 | mTwilightManager.unregisterListener(this); |
| 616 | } |
| 617 | |
| 618 | @Override |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 619 | public void onActivated(boolean activated) { |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | @Override |
| 623 | public void onTwilightStateChanged(@Nullable TwilightState state) { |
Justin Klaassen | ec8837a | 2016-08-23 12:04:42 -0700 | [diff] [blame] | 624 | Slog.d(TAG, "onTwilightStateChanged: isNight=" |
| 625 | + (state == null ? null : state.isNight())); |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 626 | updateActivated(state); |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
Justin Klaassen | 639214e | 2016-07-14 21:00:06 -0700 | [diff] [blame] | 629 | |
| 630 | /** |
| 631 | * Interpolates between two 4x4 color transform matrices (in column-major order). |
| 632 | */ |
| 633 | private static class ColorMatrixEvaluator implements TypeEvaluator<float[]> { |
| 634 | |
| 635 | /** |
| 636 | * Result matrix returned by {@link #evaluate(float, float[], float[])}. |
| 637 | */ |
| 638 | private final float[] mResultMatrix = new float[16]; |
| 639 | |
| 640 | @Override |
| 641 | public float[] evaluate(float fraction, float[] startValue, float[] endValue) { |
| 642 | for (int i = 0; i < mResultMatrix.length; i++) { |
| 643 | mResultMatrix[i] = MathUtils.lerp(startValue[i], endValue[i], fraction); |
| 644 | } |
| 645 | return mResultMatrix; |
| 646 | } |
| 647 | } |
Justin Klaassen | 911e889 | 2016-06-21 18:24:24 -0700 | [diff] [blame] | 648 | } |