private static final String TAG = "NightDisplayController";
private static final boolean DEBUG = false;
- /** @hide */
@Retention(RetentionPolicy.SOURCE)
@IntDef({ AUTO_MODE_DISABLED, AUTO_MODE_CUSTOM, AUTO_MODE_TWILIGHT })
public @interface AutoMode {}
Secure.NIGHT_DISPLAY_CUSTOM_END_TIME, endTime.toMillis(), mUserId);
}
+ /**
+ * Returns the color temperature (in Kelvin) to tint the display when activated.
+ */
+ public int getColorTemperature() {
+ int colorTemperature = Secure.getIntForUser(mContext.getContentResolver(),
+ Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE, -1, mUserId);
+ if (colorTemperature == -1) {
+ if (DEBUG) {
+ Slog.d(TAG, "Using default value for setting: "
+ + Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE);
+ }
+ colorTemperature = getDefaultColorTemperature();
+ }
+ final int minimumTemperature = getMinimumColorTemperature();
+ final int maximumTemperature = getMaximumColorTemperature();
+ if (colorTemperature < minimumTemperature) {
+ colorTemperature = minimumTemperature;
+ } else if (colorTemperature > maximumTemperature) {
+ colorTemperature = maximumTemperature;
+ }
+
+ return colorTemperature;
+ }
+
+ /**
+ * Sets the current temperature.
+ *
+ * @param colorTemperature the temperature, in Kelvin.
+ * @return {@code true} if new temperature was set successfully.
+ */
+ public boolean setColorTemperature(int colorTemperature) {
+ return Secure.putIntForUser(mContext.getContentResolver(),
+ Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE, colorTemperature, mUserId);
+ }
+
+ /**
+ * Returns the minimum allowed color temperature (in Kelvin) to tint the display when activated.
+ */
+ public int getMinimumColorTemperature() {
+ return mContext.getResources().getInteger(
+ R.integer.config_nightDisplayColorTemperatureMin);
+ }
+
+ /**
+ * Returns the maximum allowed color temperature (in Kelvin) to tint the display when activated.
+ */
+ public int getMaximumColorTemperature() {
+ return mContext.getResources().getInteger(
+ R.integer.config_nightDisplayColorTemperatureMax);
+ }
+
+ /**
+ * Returns the default color temperature (in Kelvin) to tint the display when activated.
+ */
+ public int getDefaultColorTemperature() {
+ return mContext.getResources().getInteger(
+ R.integer.config_nightDisplayColorTemperatureDefault);
+ }
+
private void onSettingChanged(@NonNull String setting) {
if (DEBUG) {
Slog.d(TAG, "onSettingChanged: " + setting);
case Secure.NIGHT_DISPLAY_CUSTOM_END_TIME:
mCallback.onCustomEndTimeChanged(getCustomEndTime());
break;
+ case Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE:
+ mCallback.onColorTemperatureChanged(getColorTemperature());
+ break;
}
}
}
false /* notifyForDescendants */, mContentObserver, mUserId);
cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_CUSTOM_END_TIME),
false /* notifyForDescendants */, mContentObserver, mUserId);
+ cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE),
+ false /* notifyForDescendants */, mContentObserver, mUserId);
}
}
}
* @param endTime the local time to automatically deactivate Night display
*/
default void onCustomEndTimeChanged(LocalTime endTime) {}
+
+ /**
+ * Callback invoked when the color temperature changes.
+ *
+ * @param colorTemperature the color temperature to tint the screen
+ */
+ default void onColorTemperatureChanged(int colorTemperature) {}
}
}
private static final boolean DEBUG = false;
/**
- * Night display ~= 3400 K.
- */
- private static final float[] MATRIX_NIGHT = new float[] {
- 1, 0, 0, 0,
- 0, 0.754f, 0, 0,
- 0, 0, 0.516f, 0,
- 0, 0, 0, 1
- };
-
- /**
* The transition time, in milliseconds, for Night Display to turn on/off.
*/
private static final long TRANSITION_DURATION = 3000L;
if (enabled) {
dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, MATRIX_IDENTITY);
} else if (mController != null && mController.isActivated()) {
- dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, MATRIX_NIGHT);
+ setMatrix(mController.getColorTemperature(), mMatrixNight);
+ dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, mMatrixNight);
}
}
});
}
};
+ private float[] mMatrixNight = new float[16];
+
+ /**
+ * These coefficients were generated by an LLS quadratic regression fitted to the
+ * overdetermined system based on experimental readings (and subsequent conversion from xy
+ * chromaticity coordinates to gamma-corrected RGB values): { (temperature, R, G, B) } ->
+ * { (7304, 1.0, 1.0, 1.0), (4082, 1.0, 0.857, 0.719), (2850, 1.0, .754, .516),
+ * (2596, 1.0, 0.722, 0.454) }. The 3x3 matrix is formatted like so:
+ * <table>
+ * <tr><td>R: a coefficient</td><td>G: a coefficient</td><td>B: a coefficient</td></tr>
+ * <tr><td>R: b coefficient</td><td>G: b coefficient</td><td>B: b coefficient</td></tr>
+ * <tr><td>R: y-intercept</td><td>G: y-intercept</td><td>B: y-intercept</td></tr>
+ * </table>
+ */
+ private static final float[] mColorTempCoefficients = new float[] {
+ 0.0f, -0.00000000962353339f, -0.0000000189359041f,
+ 0.0f, 0.000153045476f, 0.000302412211f,
+ 1.0f, 0.390782778f, -0.198650895f
+ };
+
private int mCurrentUser = UserHandle.USER_NULL;
private ContentObserver mUserSetupObserver;
private boolean mBootCompleted;
mController = new NightDisplayController(getContext(), mCurrentUser);
mController.setListener(this);
+ // Prepare color transformation matrix.
+ setMatrix(mController.getColorTemperature(), mMatrixNight);
+
// Initialize the current auto mode.
onAutoModeChanged(mController.getAutoMode());
if (mIsActivated == null) {
onActivated(mController.isActivated());
}
+
+ // Transition the screen to the current temperature.
+ applyTint(false);
}
private void tearDown() {
mIsActivated = activated;
- // Cancel the old animator if still running.
- if (mColorMatrixAnimator != null) {
- mColorMatrixAnimator.cancel();
- }
-
- // Don't do any color matrix change animations if we are ignoring them anyway.
- if (mIgnoreAllColorMatrixChanges.get()) {
- return;
- }
-
- final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class);
- final float[] from = dtm.getColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY);
- final float[] to = mIsActivated ? MATRIX_NIGHT : null;
-
- mColorMatrixAnimator = ValueAnimator.ofObject(COLOR_MATRIX_EVALUATOR,
- from == null ? MATRIX_IDENTITY : from, to == null ? MATRIX_IDENTITY : to);
- mColorMatrixAnimator.setDuration(TRANSITION_DURATION);
- mColorMatrixAnimator.setInterpolator(AnimationUtils.loadInterpolator(
- getContext(), android.R.interpolator.fast_out_slow_in));
- mColorMatrixAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
- @Override
- public void onAnimationUpdate(ValueAnimator animator) {
- final float[] value = (float[]) animator.getAnimatedValue();
- dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, value);
- }
- });
- mColorMatrixAnimator.addListener(new AnimatorListenerAdapter() {
-
- private boolean mIsCancelled;
-
- @Override
- public void onAnimationCancel(Animator animator) {
- mIsCancelled = true;
- }
-
- @Override
- public void onAnimationEnd(Animator animator) {
- if (!mIsCancelled) {
- // Ensure final color matrix is set at the end of the animation. If the
- // animation is cancelled then don't set the final color matrix so the new
- // animator can pick up from where this one left off.
- dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, to);
- }
- mColorMatrixAnimator = null;
- }
- });
- mColorMatrixAnimator.start();
+ applyTint(false);
}
}
}
}
+ @Override
+ public void onColorTemperatureChanged(int colorTemperature) {
+ setMatrix(colorTemperature, mMatrixNight);
+ applyTint(true);
+ }
+
+ /**
+ * Applies current color temperature matrix, or removes it if deactivated.
+ *
+ * @param immediate {@code true} skips transition animation
+ */
+ private void applyTint(boolean immediate) {
+ // Cancel the old animator if still running.
+ if (mColorMatrixAnimator != null) {
+ mColorMatrixAnimator.cancel();
+ }
+
+ // Don't do any color matrix change animations if we are ignoring them anyway.
+ if (mIgnoreAllColorMatrixChanges.get()) {
+ return;
+ }
+
+ final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class);
+ final float[] from = dtm.getColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY);
+ final float[] to = mIsActivated ? mMatrixNight : MATRIX_IDENTITY;
+
+ if (immediate) {
+ dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, to);
+ } else {
+ mColorMatrixAnimator = ValueAnimator.ofObject(COLOR_MATRIX_EVALUATOR,
+ from == null ? MATRIX_IDENTITY : from, to);
+ mColorMatrixAnimator.setDuration(TRANSITION_DURATION);
+ mColorMatrixAnimator.setInterpolator(AnimationUtils.loadInterpolator(
+ getContext(), android.R.interpolator.fast_out_slow_in));
+ mColorMatrixAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
+ @Override
+ public void onAnimationUpdate(ValueAnimator animator) {
+ final float[] value = (float[]) animator.getAnimatedValue();
+ dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, value);
+ }
+ });
+ mColorMatrixAnimator.addListener(new AnimatorListenerAdapter() {
+
+ private boolean mIsCancelled;
+
+ @Override
+ public void onAnimationCancel(Animator animator) {
+ mIsCancelled = true;
+ }
+
+ @Override
+ public void onAnimationEnd(Animator animator) {
+ if (!mIsCancelled) {
+ // Ensure final color matrix is set at the end of the animation. If the
+ // animation is cancelled then don't set the final color matrix so the new
+ // animator can pick up from where this one left off.
+ dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, to);
+ }
+ mColorMatrixAnimator = null;
+ }
+ });
+ mColorMatrixAnimator.start();
+ }
+ }
+
+ /**
+ * Set the color transformation {@code MATRIX_NIGHT} to the given color temperature.
+ *
+ * @param colorTemperature color temperature in Kelvin
+ * @param outTemp the 4x4 display transformation matrix for that color temperature
+ */
+ private void setMatrix(int colorTemperature, float[] outTemp) {
+ if (outTemp.length != 16) {
+ Slog.d(TAG, "The display transformation matrix must be 4x4");
+ return;
+ }
+
+ Matrix.setIdentityM(mMatrixNight, 0);
+
+ final float squareTemperature = colorTemperature * colorTemperature;
+ final float red = squareTemperature * mColorTempCoefficients[0]
+ + colorTemperature * mColorTempCoefficients[3] + mColorTempCoefficients[6];
+ final float green = squareTemperature * mColorTempCoefficients[1]
+ + colorTemperature * mColorTempCoefficients[4] + mColorTempCoefficients[7];
+ final float blue = squareTemperature * mColorTempCoefficients[2]
+ + colorTemperature * mColorTempCoefficients[5] + mColorTempCoefficients[8];
+ outTemp[0] = red;
+ outTemp[5] = green;
+ outTemp[10] = blue;
+ }
+
private abstract class AutoMode implements NightDisplayController.Callback {
public abstract void onStart();
public abstract void onStop();