Android 息屏显示

什么是息屏显示

息屏显示就是手机在息屏状态下,屏幕上会显示当前时间、日期信息,无需点亮手机屏幕即可查看。息屏显示的原理主要是利用了OLED屏幕像素点自发光的特性,仅显示时间的像素点发光,功耗相比LCD屏幕要低很多。
在这里插入图片描述

Android原生的主动显示

玩过Android源码的同学应该知道,在Settings里有一个开关项:
设置 - 显示 - 主动显示
在这里插入图片描述
这就是设置 - 显示下的主动显示选项,勾选了这个选项后,当设备在息屏时接到一条新通知会显示这样的效果:
在这里插入图片描述
是不是发现与息屏显示的效果一模一样,但触发条件却不一样,需要息屏后有通知才会显示出来,并且过一段时间又会自动消失回归黑屏,那么怎样才能做到息屏后就能一直都显示呢?

源码分析

既然知道主动显示开关是放在设置里面的,那不妨先从Settings的源码看起,首先找到主动显示对应的Preference

<Preference
        android:key="ambient_display"
        android:title="@string/ambient_display_screen_title"
        android:fragment="com.android.settings.display.AmbientDisplaySettings" />

然后发现在AmbientDisplaySettings里注册了一些controller

private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
        Lifecycle lifecycle, AmbientDisplayConfiguration config,
        MetricsFeatureProvider metricsFeatureProvider,
        AmbientDisplayAlwaysOnPreferenceController.OnPreferenceChangedCallback aodCallback) {
    final List<AbstractPreferenceController> controllers = new ArrayList<>();
    controllers.add(new AmbientDisplayNotificationsPreferenceController(context, config,
            metricsFeatureProvider));
    controllers.add(new AmbientDisplayAlwaysOnPreferenceController(context, config,
            aodCallback));
    controllers.add(new DoubleTapScreenPreferenceController(context, lifecycle, config,
            MY_USER_ID, KEY_AMBIENT_DISPLAY_DOUBLE_TAP));
    controllers.add(new PickupGesturePreferenceController(context, lifecycle, config,
            MY_USER_ID, KEY_AMBIENT_DISPLAY_PICK_UP));
    return controllers;
}

先关注里面的两个:AmbientDisplayNotificationsPreferenceController和AmbientDisplayAlwaysOnPreferenceController,看名字大概能知道,第一个与通知有关,应该是上文提到的息屏后来通知才显示;而第二个就是我们要找的“始终开启”。
AmbientDisplayAlwaysOnPreferenceController

public class AmbientDisplayAlwaysOnPreferenceController extends
        AbstractPreferenceController implements PreferenceControllerMixin,
        Preference.OnPreferenceChangeListener {
    private final int ON = 1;
    private final int OFF = 0;
    ...
    // 每次进入该PreferenceScreen都会调用一次刷新开关状态
    @Override
    public void updateState(Preference preference) {
        ((SwitchPreference) preference).setChecked(isAlwaysOnEnabled(mConfig));
    }

    // 通过AmbientDisplayConfiguration获得当前enable状态
    public static boolean isAlwaysOnEnabled(AmbientDisplayConfiguration config) {
        return config.alwaysOnEnabled(MY_USER);
    }

    // 每次点击后写入数据
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        int enabled = (boolean) newValue ? ON : OFF;
        Settings.Secure.putInt(
                mContext.getContentResolver(), Settings.Secure.DOZE_ALWAYS_ON, enabled);
        if (mCallback != null) {
            mCallback.onPreferenceChanged();
        }
        return true;
    }

    // 该Preference是否可用
    @Override
    public boolean isAvailable() {
        return isAvailable(mConfig);
    }

    // 通过AmbientDisplayConfiguration 获得available状态
    public static boolean isAvailable(AmbientDisplayConfiguration config) {
        return config.alwaysOnAvailableForUser(MY_USER);
    }
    ...
}

分析AmbientDisplayAlwaysOnPreferenceController的源码发现,AlwaysOn的enable和available状态都需要通过AmbientDisplayConfiguration 这个类来获得,并且这个类位于framework中。

简单介绍下AmbientDisplayConfiguration 中与alwaysOn有关的几个函数:

public boolean alwaysOnEnabled(int user) {
     return boolSettingDefaultOn(Settings.Secure.DOZE_ALWAYS_ON, user) && alwaysOnAvailable()
             && !accessibilityInversionEnabled(user);
 }

 public boolean alwaysOnAvailable() {
     return (alwaysOnDisplayDebuggingEnabled() || alwaysOnDisplayAvailable())
             && ambientDisplayAvailable();
 }

 private boolean alwaysOnDisplayAvailable() {
     return mContext.getResources().getBoolean(R.bool.config_dozeAlwaysOnDisplayAvailable);
 }

 public boolean accessibilityInversionEnabled(int user) {
     return boolSettingDefaultOff(Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, user);
 }

 private boolean ambientDisplayAvailable() {
     return !TextUtils.isEmpty(ambientDisplayComponent());
 }

 public String ambientDisplayComponent() {
     return mContext.getResources().getString(R.string.config_dozeComponent);
 }

 private boolean boolSettingDefaultOn(String name, int user) {
     return boolSetting(name, user, 1);
 }

 private boolean boolSetting(String name, int user, int def) {
     return Settings.Secure.getIntForUser(mContext.getContentResolver(), name, def, user) != 0;
 }

alwaysOnAvailable为true需要同时满足两个条件:

1. 处于debug模式,或者config_dozeAlwaysOnDisplayAvailable为true,这个值写在frameworks/base/core/res/res/values/config.xml里,默认是false; 
2. config_dozeComponent取值不为空,这个值同样写在上面讲到的config.xml里,默认是空着的。

alwaysOnEnabled为true需要同时满足三个条件:

1. DOZE_ALWAYS_ON值写入了1,即Settings里开启了开关;
2. alwaysOnAvailable为true;
3.  ACCESSIBILITY_DISPLAY_INVERSION_ENABLED值为0,即没有开启颜色反转。

原来源码里面默认把AlwaysOn功能给关闭了,如果想启用这个功能,需要修改config.xml里的两个值或者强制alwaysOnAvailable返回true,修改后设置里的主动显示一栏就会多出一项“始终开启”可以勾选,这样一来我们的设备在息屏之后就能自动开启主动显示功能了。

参考: https://www.jianshu.com/p/0c3641e83ab9

  • 17
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值