android 夜间模式

自7.0开始谷歌提供了夜间模式,用于将屏幕色彩调成较为柔和的琥珀色,但因该模式需要硬件的支持,所以不是所有设备上都可用,也不常见。

相关代码:
\frameworks\base\services\core\java\com\android\server\display\NightDisplayService.java
\frameworks\base\core\java\com\android\internal\app\NightDisplayController.java
\frameworks\base\core\res\res\values\config.xml

设置中相关代码:
\packages\apps\Settings\src\com\android\settings\display\NightDisplaySettings.java

夜间模式的开启和关闭,相关方法代码的调用,可完全参考设置中的夜间模式,config中默认配置如下,从配置中可见,夜间模式是否能够生效,主要依赖硬件:是否拥有HWC引入功能,能够在不增加耗电、性能、兼容性的情况下,实现颜色矩阵转换操作。

//要修改夜间模式,需要有权限:
<permission android:name="android.permission.MODIFY_DAY_NIGHT_MODE"
                android:protectionLevel="signature|privileged" />
<bool name="config_lockDayNightMode">false</bool>

    <!-- Control the default night mode to use when there is no other mode override set.
         One of the following values (see UiModeManager.java):
             0 - MODE_NIGHT_AUTO
             1 - MODE_NIGHT_NO
             2 - MODE_NIGHT_YES
    -->
    <integer name="config_defaultNightMode">1</integer>

    <!-- Boolean indicating whether the HWC setColorTransform function can be performed efficiently
         in hardware. -->
    <bool name="config_setColorTransformAccelerated">false</bool>

    <!-- Control whether Night display is available. This should only be enabled on devices
         that have a HWC implementation that can apply the matrix passed to setColorTransform
         without impacting power, performance, and app compatibility (e.g. protected content). -->
    <bool name="config_nightDisplayAvailable">@bool/config_setColorTransformAccelerated</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>

    <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>

设置中夜间模式的所有设置和操作,都是在NightDisplayController中实现,其中主要方法如下:

//开启和关闭
public boolean setActivated(boolean activated) {
        if (isActivated() != activated) {
            Secure.putStringForUser(mContext.getContentResolver(),
                    Secure.NIGHT_DISPLAY_LAST_ACTIVATED_TIME,
                    LocalDateTime.now().toString(),
                    mUserId);
        }
        return Secure.putIntForUser(mContext.getContentResolver(),
                Secure.NIGHT_DISPLAY_ACTIVATED, activated ? 1 : 0, mUserId);
    }

//设置模式,共三种,不定时,定时开启和关闭,在日落到日出的时间开启
public @AutoMode int getAutoMode() {
        int autoMode = Secure.getIntForUser(mContext.getContentResolver(),
                Secure.NIGHT_DISPLAY_AUTO_MODE, -1, mUserId);
        if (autoMode == -1) {
            if (DEBUG) {
                Slog.d(TAG, "Using default value for setting: " + Secure.NIGHT_DISPLAY_AUTO_MODE);
            }
            autoMode = mContext.getResources().getInteger(
                    R.integer.config_defaultNightDisplayAutoMode);
        }

        if (autoMode != AUTO_MODE_DISABLED
                && autoMode != AUTO_MODE_CUSTOM
                && autoMode != AUTO_MODE_TWILIGHT) {
            Slog.e(TAG, "Invalid autoMode: " + autoMode);
            autoMode = AUTO_MODE_DISABLED;
        }

        return autoMode;
    }
//设置色温,如config中配置,最大4082,最小2596
public boolean setColorTemperature(int colorTemperature) {
        return Secure.putIntForUser(mContext.getContentResolver(),
            Secure.NIGHT_DISPLAY_COLOR_TEMPERATURE, colorTemperature, mUserId);
    }
//

猜你喜欢

转载自blog.csdn.net/thh159/article/details/88289467