Implement night light

Research suggests that blue light from screens can have a negative impact on sleep. Android 7.1.1 introduced a feature called Night Light that reduces the amount of blue light emitted by the device display to better match the natural light of the user's time of day and location. Android 8.0 introduced an additional feature that gives users more control over the intensity of the Night Light effect. Android 10 introduces the COLOR_DISPLAY_SERVICE system service, with a system API surface to give the system, Settings, and System UI more control over all color transforms, including Night Light.

Night Light requires a Hardware Composer HAL 2.0 (HWC 2) implementation that can apply the matrix passed to setColorTransform to perform tinting without impacting power, performance, and app compatibility.

Implementation

Device manufacturers can enable the default implementation of the feature by using the following flags defined in frameworks/base/core/res/res/values/config.xml

 <!-- Control whether Night display is available. This should only be enabled
      on devices with HWC 2 color transform support. -->
 <bool name="config_nightDisplayAvailable">false</bool>
 <!-- Default mode to control how Night display is automatically activated.
      One of the following values (see NightDisplayController.java):
          0 - AUTO_MODE_DISABLED
          1 - AUTO_MODE_CUSTOM
          2 - AUTO_MODE_TWILIGHT
 -->
 <integer name="config_defaultNightDisplayAutoMode">0</integer>
 <!-- Default time when Night display is automatically activated.
      Represented as milliseconds from midnight (e.g. 79200000 == 10pm). -->
 <integer name="config_defaultNightDisplayCustomStartTime">79200000</integer>
 <!-- Default time when Night display is automatically deactivated.
      Represented as milliseconds from midnight (e.g. 21600000 == 6am). -->
 <integer name="config_defaultNightDisplayCustomEndTime">21600000</integer>

 <!-- Minimum color temperature, in Kelvin, supported by Night display. -->
 <integer name="config_nightDisplayColorTemperatureMin">2596</integer>
 <!-- Default color temperature, in Kelvin, to tint the screen when Night display is
      activated. -->
 <integer name="config_nightDisplayColorTemperatureDefault">2850</integer>
 <!-- Maximum color temperature, in Kelvin, supported by Night display. -->
 <integer name="config_nightDisplayColorTemperatureMax">4082</integer>

The code is divided between framework, system services, System UI, and Settings. The core functionality is controlled by ColorDisplayManager (backed by ColorDisplayService).

Device manufacturers should customize the color ramp based on the characteristics of the device's display panel, including white point, gamut, and desired color. You can change the color ramp without changing the base implementation by using a configuration overlay. This configuration is expressed as a quadratic equation for each of red, green, and blue, in the form vres = vat2 + vbt + vy-int where t is the temperature input in Kelvin, as specified in the range between config_nightDisplayColorTemperatureMin and config_nightDisplayColorTemperatureMax (as described in the previous section), and va, vb, and vy-int are the a-coefficient, b-coefficient, and y-intercept, respectively, for the given primary's curve, as indicated below.

    <string-array name="config_nightDisplayColorTemperatureCoefficientsNative">
        <!-- R a-coefficient --> <item>0.0</item>
        <!-- R b-coefficient --> <item>0.0</item>
        <!-- R y-intercept --> <item>1.0</item>
        <!-- G a-coefficient --> <item>-0.00000000962353339</item>
        <!-- G b-coefficient --> <item>0.000153045476</item>
        <!-- G y-intercept --> <item>0.390782778</item>
        <!-- B a-coefficient --> <item>-0.0000000189359041</item>
        <!-- B b-coefficient --> <item>0.000302412211</item>
        <!-- B y-intercept --> <item>-0.198650895</item>
    </string-array>

    <string-array name="config_nightDisplayColorTemperatureCoefficients">
        <!-- R a-coefficient --> <item>0.0</item>
        <!-- R b-coefficient --> <item>0.0</item>
        <!-- R y-intercept --> <item>1.0</item>
        <!-- G a-coefficient --> <item>-0.00000000962353339</item>
        <!-- G b-coefficient --> <item>0.000153045476</item>
        <!-- G y-intercept --> <item>0.390782778</item>
        <!-- B a-coefficient --> <item>-0.0000000189359041</item>
        <!-- B b-coefficient --> <item>0.000302412211</item>
        <!-- B y-intercept --> <item>-0.198650895</item>
    </string-array>

UI features

Because Night Light is a user-facing feature, users need to be able to control it. There is a full implementation of the settings in the Android Open Source Project (AOSP) packages/apps/Settings project that device manufacturers can reference for their Settings implementation. Implementers must handle the Settings.ACTION_NIGHT_DISPLAY_SETTINGS intent to expose this setting.

Settings

The settings for Night Light are in Settings > Display > Night Light. From there, users can learn about Night Light, set its schedule, and turn it on or off.

  • Turn On Automatically
    • Never: Night Light will never turn on automatically and must be activated with the manual On / Off toggle.
    • Custom schedule: Night Light turns on at a specified Start time [default: 10:30 p.m.] and off at a specified End time [default: 6:30 a.m.].
    • Sunset to sunrise: Night Light turns on at sunset and off at sunrise. The time for sunrise and sunset depends on the device location and the time of year.
  • On / Off: Toggle that controls the current state of Night Light. This state respects existing automatic rules. For example, if Night Light is toggled on at 5:30 p.m. (before the automatic rule would turn it on at 10:30 p.m.), Night Light will still turn off at 6:30 a.m. And if Night Light is toggled off at 5:30 a.m. (before it turns off at 6:30 a.m.), it will still turn on at 10:30 p.m.
  • Intensity: Seekbar that controls the tint level by sliding from warm to cool. The seekbar can be disabled when Night Light is not activated.
  • Informational text: Teaches the user what Night Light does and why.

Settings conditional

Visible at the top of Settings when Night Light is on.

Quick Settings tile

The Quick Settings tile behaves identically to the On / Off toggle in Settings > Display > Night Light.