美文网首页
Android11 setting中增加永不息屏

Android11 setting中增加永不息屏

作者: 梧叶已秋声 | 来源:发表于2021-01-29 09:56 被阅读0次

首先修改android:entries和 android:entryValues属性。默认的是screen_timeout_entries和screen_timeout_values,是没有永不息屏的选项的,但是array有定义dream_timeout_entries和dream_timeout_values,这里有永不息屏。

//vendor\mediatek\proprietary\packages\apps\MtkSettings\res\xml\display_settings.xml
   <com.android.settings.display.TimeoutListPreference
        android:key="screen_timeout"
        android:title="@string/screen_timeout"
        android:summary="@string/summary_placeholder"
        android:entries="@array/screen_timeout_entries"
        android:entryValues="@array/screen_timeout_values"
        settings:keywords="@string/keywords_screen_timeout" />

修改后如下。

    <com.android.settings.display.TimeoutListPreference
        android:key="screen_timeout"
        android:title="@string/screen_timeout"
        android:summary="@string/summary_placeholder"
        android:entries="@array/dream_timeout_entries"
        android:entryValues="@array/dream_timeout_values"
        settings:keywords="@string/keywords_screen_timeout" />

但是这样只是setting中对应的dialog界面会增加never这个选项,如果选择never的话,还是会息屏。never对应的时间是0.
我尝试在PowerManagerService增加判断,这里虽然可以实现永不息屏,但是会导致新的问题,锁屏后无法亮屏。

      if(mScreenOffTimeoutSetting == 0){
            return mMaximumScreenOffTimeoutFromDeviceAdmin;
        }

具体如下(这样修改在Android11中会导致新的问题)。

//frameworks\base\services\core\java\com\android\server\power\PowerManagerService.java
    private long getScreenOffTimeoutLocked(long sleepTimeout, long attentiveTimeout) {
        long timeout = mScreenOffTimeoutSetting;
        if (isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) {
            timeout = Math.min(timeout, mMaximumScreenOffTimeoutFromDeviceAdmin);
        }
        if (mUserActivityTimeoutOverrideFromWindowManager >= 0) {
            timeout = Math.min(timeout, mUserActivityTimeoutOverrideFromWindowManager);
        }
        if (sleepTimeout >= 0) {
            timeout = Math.min(timeout, sleepTimeout);
        }
        if (attentiveTimeout >= 0) {
            timeout = Math.min(timeout, attentiveTimeout);
        }
        if(mScreenOffTimeoutSetting == 0){
            return mMaximumScreenOffTimeoutFromDeviceAdmin;
        }
        return Math.max(timeout, mMinimumScreenOffTimeoutConfig);
    }

最终修改如下。
1.给mHandler.postDelayed(mScreenLockTimeout, mLockScreenTimeout);加个判断。

//frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager.java
    private void updateLockScreenTimeout() {
        synchronized (mScreenLockTimeout) {
            final boolean enable = !mAllowLockscreenWhenOnDisplays.isEmpty()
                    && mDefaultDisplayPolicy.isAwake()
                    && mKeyguardDelegate != null && mKeyguardDelegate.isSecure(mCurrentUserId);
            if (mLockScreenTimerActive != enable) {
                if (enable) {
                    if (localLOGV) Log.v(TAG, "setting lockscreen timer");
                    mHandler.removeCallbacks(mScreenLockTimeout); // remove any pending requests
                    //mHandler.postDelayed(mScreenLockTimeout, mLockScreenTimeout);
                    if (mLockScreenTimeout != 0) {
                        mHandler.postDelayed(mScreenLockTimeout, mLockScreenTimeout);
                    }
                } else {
                    if (localLOGV) Log.v(TAG, "clearing lockscreen timer");
                    mHandler.removeCallbacks(mScreenLockTimeout);
                }
                mLockScreenTimerActive = enable;
            }
        }
    }

2.修改updateUserActivitySummaryLocked中部分逻辑,增加判断条件。
最终如下。

//frameworks\base\services\core\java\com\android\server\power\PowerManagerService.java
    private void updateUserActivitySummaryLocked(long now, int dirty) {
        // Update the status of the user activity timeout timer.
       ................

               /* if ((mUserActivitySummary & USER_ACTIVITY_SCREEN_BRIGHT) != 0
                        && (mWakeLockSummary & WAKE_LOCK_STAY_AWAKE) == 0) {
                    nextTimeout = mAttentionDetector.updateUserActivity(nextTimeout,
                            screenDimDuration);
                }

                if (nextProfileTimeout > 0) {
                    nextTimeout = Math.min(nextTimeout, nextProfileTimeout);
                }

                if (mUserActivitySummary != 0 && nextTimeout >= 0) {
                    scheduleUserInactivityTimeout(nextTimeout);
                }*/

                if (mScreenOffTimeoutSetting != 0) {
                    if ((mUserActivitySummary & USER_ACTIVITY_SCREEN_BRIGHT) != 0
                            && (mWakeLockSummary & WAKE_LOCK_STAY_AWAKE) == 0) {
                        nextTimeout = mAttentionDetector.updateUserActivity(nextTimeout,screenDimDuration);
                    }
                    if (nextProfileTimeout > 0) {
                        nextTimeout = Math.min(nextTimeout, nextProfileTimeout);
                    }
                    if (mUserActivitySummary != 0 && nextTimeout >= 0) {
                        scheduleUserInactivityTimeout(nextTimeout);
                    }
                } else {
                    mUserActivitySummary |= USER_ACTIVITY_SCREEN_BRIGHT;
                }
            } else {
                mUserActivitySummary = 0;
            }

            if (DEBUG_SPEW) {
                Slog.d(TAG, "updateUserActivitySummaryLocked: mWakefulness="
                        + PowerManagerInternal.wakefulnessToString(getWakefulnessLocked())
                        + ", mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary)
                        + ", nextTimeout=" + TimeUtils.formatUptime(nextTimeout));
            }
        }
    }

参考链接:
展讯平台记录下工作中一些修改
Android7.1 Settings加入永不休眠选项
安卓7.0显示设置界面休眠菜单添加“永不灭屏”选项
android设置永不灭屏

相关文章

网友评论

      本文标题:Android11 setting中增加永不息屏

      本文链接:https://www.haomeiwen.com/subject/knbmzktx.html