android13 settings-display-screen timeout

301 阅读2分钟

1.简介

主要是随便设置了值以后,display页面显示的summary有点奇怪,如下,显示个after null of inactivity,所以简单看下

image.png

2.display页

2.1.display_settings.xml

  • 可以看到由ScreenTimeoutPreferenceController控制的
  • 跳转的fragment是ScreenTimeoutSettings
    <PreferenceCategory
        android:title="@string/category_name_lock_display">

        <Preference
            android:key="lockscreen_from_display_settings"
            android:title="@string/lockscreen_settings_title"
            android:fragment="com.android.settings.security.LockscreenDashboardFragment"
            settings:keywords="@string/keywords_ambient_display_screen"
            settings:controller="com.android.settings.security.screenlock.LockScreenPreferenceController"/>

        <com.android.settingslib.RestrictedPreference
            android:key="screen_timeout"
            android:title="@string/screen_timeout"
            android:summary="@string/summary_placeholder"
            android:fragment="com.android.settings.display.ScreenTimeoutSettings"
            settings:controller="com.android.settings.display.ScreenTimeoutPreferenceController"/>
    </PreferenceCategory>

2.2.ScreenTimeoutPreferenceController

>getTimeoutSummary

    private CharSequence getTimeoutSummary(long maxTimeout) {
    //获取设置的时间或者默认值
        final long currentTimeout = getCurrentScreenTimeout();
        //maxTimeout没有特殊设置的话就是LONG.MAX_VALUE
        final CharSequence description = getTimeoutDescription(currentTimeout, maxTimeout);
        return mContext.getString(R.string.screen_timeout_summary, description);
    }

>getCurrentScreenTimeout

这个就是用户设置的值,没有的话就是默认值

    private long getCurrentScreenTimeout() {
        return Settings.System.getLong(mContext.getContentResolver(),
                SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE);
    }

>getTimeoutDescription

    private CharSequence getTimeoutDescription(long currentTimeout, long maxTimeout) {
    //异常情况返回空,设置的时间为负的,条目为空或entries和values数量不一样
        if (currentTimeout < 0 || mTimeoutEntries == null || mTimeoutValues == null
                || mTimeoutValues.length != mTimeoutEntries.length) {
            return null;
        }
        //设置的时间比支持的最大值还大,就取条目的最后一个
        if (currentTimeout > maxTimeout) {
            // The selected time out value is longer than the max timeout allowed by the admin.
            // Select the largest value from the list by default.
            return getLargestTimeout(maxTimeout);
        } else {
            return getCurrentTimeout(currentTimeout);
        }
    }
    //我们的走这里了
    private CharSequence getCurrentTimeout(long currentTimeout) {
        for (int i = 0; i < mTimeoutValues.length; i++) {
            if (currentTimeout == Long.parseLong(mTimeoutValues[i].toString())) {
                return mTimeoutEntries[i];
            }
        }
        //我们设置的值不是系统提供的条目里,所以返回了null
        return null;
    }

3.ScreenTimeoutSettings

3.1.onAttach

    public static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000;

    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
        mDevicePolicyManager = mContext.getSystemService(DevicePolicyManager.class);
        mInitialEntries = getResources().getStringArray(R.array.screen_timeout_entries);
        mInitialValues = getResources().getStringArray(R.array.screen_timeout_values);

>screen_timeout_entries

默认支持的条目

32   <string-array name="screen_timeout_entries">
33     <item msgid="8386012403457852396">"‎‏‎‎‎‎‎‏‎‏‏‏‎‎‎‎‎‎‏‎‎‏‎‎‎‏‎‏‏‏‏‏‏‏‏‏‎‏‎‎‎‏‏‎‎‎‎‏‎‎‎‏‏‎‎‏‏‏‎‏‏‎‎‏‏‎‎‏‎‏‏‎‎‎‏‎‎‎‎‎‎‎‎‎‎‎‏‏‏‏‏‎‏‏‎‎‎15 seconds‎‏‎‎‏‎"</item>
34     <item msgid="4572123773028439079">"‎‏‎‎‎‎‎‏‎‏‏‏‎‎‎‎‎‎‏‎‎‏‎‎‎‏‎‏‏‏‏‏‎‏‏‏‏‏‏‎‏‏‏‎‎‏‏‎‏‏‏‎‎‏‎‎‏‎‏‏‎‎‎‏‏‏‏‎‎‎‎‎‏‏‏‏‎‏‎‏‎‎‏‎‏‎‎‎‎‏‎‎‏‏‏‎30 seconds‎‏‎‎‏‎"</item>
35     <item msgid="7016081293774377048">"‎‏‎‎‎‎‎‏‎‏‏‏‎‎‎‎‎‎‏‎‎‏‎‎‎‏‎‏‏‏‏‏‏‏‏‎‎‎‎‏‎‏‎‏‏‏‏‎‎‎‏‎‎‎‎‎‏‏‎‎‎‎‏‏‏‎‎‎‏‏‏‎‏‎‏‎‏‏‏‏‎‏‎‏‎‎‎‎‎‏‎‏‏‎‎‎‎1 minute‎‏‎‎‏‎"</item>
36     <item msgid="838575533670111144">"‎‏‎‎‎‎‎‏‎‏‏‏‎‎‎‎‎‎‏‎‎‏‎‎‎‏‎‏‏‏‏‎‎‏‎‏‏‏‎‏‎‎‎‏‏‎‎‏‏‏‎‎‎‎‎‎‎‎‎‎‏‎‏‎‎‎‎‎‏‏‎‏‏‏‏‎‏‏‎‎‏‎‏‏‏‏‎‏‎‏‎‎‎‎2 minutes‎‏‎‎‏‎"</item>
37     <item msgid="2693197579676214668">"‎‏‎‎‎‎‎‏‎‏‏‏‎‎‎‎‎‎‏‎‎‏‎‎‎‏‎‏‏‏‏‏‎‏‎‎‏‎‏‎‏‏‎‎‎‎‎‎‎‏‎‏‎‎‎‏‏‏‎‏‏‎‏‏‏‏‏‎‏‏‎‏‎‎‎‏‎‏‎‏‏‏‎‎‏‎‏‏‎‎‎‏‏‎‎‎5 minutes‎‏‎‎‏‎"</item>
38     <item msgid="1955784331962974678">"‎‏‎‎‎‎‎‏‎‏‏‏‎‎‎‎‎‎‏‎‎‏‎‎‎‏‎‏‏‏‏‎‏‏‏‎‏‏‎‎‏‎‎‏‎‎‎‏‎‏‎‏‏‏‎‏‏‏‏‏‎‎‏‏‎‏‏‎‏‏‏‏‏‎‏‏‎‏‏‎‎‎‎‎‎‏‏‏‎‏‎‏‏‎‎10 minutes‎‏‎‎‏‎"</item>
39     <item msgid="5578717731965793584">"‎‏‎‎‎‎‎‏‎‏‏‏‎‎‎‎‎‎‏‎‎‏‎‎‎‏‎‏‏‏‏‏‏‏‎‎‏‏‎‏‎‏‏‎‏‎‏‏‏‎‎‏‎‏‏‎‎‎‏‏‏‎‎‎‎‏‎‎‏‎‏‏‏‏‏‏‏‏‏‎‎‏‏‏‎‏‎‏‎‎‏‏‎‎‎‎‎30 minutes‎‏‎‎‏‎"</item>
40   </string-array>

3.2.updateCandidates

    public void updateCandidates() {
        final String defaultKey = getDefaultKey();
        final PreferenceScreen screen = getPreferenceScreen();
        screen.removeAll();

        final List<? extends CandidateInfo> candidateList = getCandidates();
        if (candidateList == null) {
            return;
        }
//手动添加条目
        for (CandidateInfo info : candidateList) {
            SelectorWithWidgetPreference pref =
                    new SelectorWithWidgetPreference(getPrefContext());
                    //bind的时候如果设置的值和对应条目一样话,自动设置为选中状态
            bindPreference(pref, info.getKey(), info, defaultKey);
            screen.addPreference(pref);
        }
//设置的值不在条目里,则没有选中状态
        final long selectedTimeout = Long.parseLong(defaultKey);
        final long maxTimeout = getMaxScreenTimeout(getContext());
        if (!candidateList.isEmpty() && (selectedTimeout > maxTimeout)) {
            //如果设置的值比支持的最大值还大,修改为条目的最大值
            final SelectorWithWidgetPreference preferenceWithLargestTimeout =
                    (SelectorWithWidgetPreference) screen.getPreference(candidateList.size() - 1);
            preferenceWithLargestTimeout.setChecked(true);
        }

        mPrivacyPreference = new FooterPreference(mContext);
        mPrivacyPreference.setIcon(R.drawable.ic_privacy_shield_24dp);
        mPrivacyPreference.setTitle(R.string.adaptive_sleep_privacy);
        mPrivacyPreference.setSelectable(false);
        mPrivacyPreference.setLayoutResource(R.layout.preference_footer);
//添加screen attention 选项
        if (isScreenAttentionAvailable(getContext())) {
            mAdaptiveSleepPermissionController.addToScreen(screen);
            mAdaptiveSleepCameraStatePreferenceController.addToScreen(screen);
            mAdaptiveSleepController.addToScreen(screen);
            mAdaptiveSleepBatterySaverPreferenceController.addToScreen(screen);
            screen.addPreference(mPrivacyPreference);
        }

        if (mAdmin != null) {
            setupDisabledFooterPreference();
            screen.addPreference(mDisableOptionsPreference);
        }
    }