Android P 屏保和休眠相关知识

Android P添加屏保功能,如果休眠时间设定大于屏保时间,则先进入屏保,达到休眠时间后再进入休眠

需求:

  1. 添加屏幕互保开关,默认关闭。
  2. 只保留时钟,可设定指针和数字、夜间模式。
  3. 启用时间改多长时间无操作进入屏保,可设定1、5、10、15、30分钟。
  4. 如果休眠时间设定小于屏保时间,则优先进入休眠,如果休眠时间设定大于屏保时间,则先进入屏保,达到休眠时间后再进入休眠

修改后的屏保:

 

 分析与实现:

1.修改进入屏保的条件

由于Android原生系统默认进入屏保的条件是充电或插入基座时,但到达休眠时才进入屏保。所以需要把进入屏保的条件修改为任何时候都能进入。

frameworks\base\core\res\res\values\config.xml
把"config_dreamsEnabledOnBattery"修改为true即实现任何时候都能进入屏保<!-- Are we allowed to dream while not plugged in? --><bool name="config_dreamsEnabledOnBattery">true</bool>

2.在设置–>显示–>屏保中添加“屏幕互保”选项开关

2.1添加系统自定义全局变量,用来保存“屏幕互保”选项开关的状态值

frameworks/base/core/java/android/provider/Settings.java+        /** @hide */
+        public static final String SCREENSAVER_SWITCH= "seewo.screensavers.switch";
public static final class Secure extends NameValueTable {
/** @hide */
public static final String SCREENSAVER_TIMEOUT= “seewo.screensavers.timeout”;

2.2 vendor/mediatek/proprietary/packages/apps/MtkSettings/res/values-zh-rCN/strings.xml

<string name="screensaver_switch_title">屏幕互保</string>

2.3 vendor/mediatek/proprietary/packages/apps/MtkSettings/res/xml/dream_fragment_overview.xml

<PreferenceScreenxmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:settings="http://schemas.android.com/apk/res-auto"android:key="dream_overview_screen"
<SwitchPreferenceandroid:key="screensaver_switch"android:title="@string/screensaver_switch_title"/>

2.4添加vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/dream/ScreenSaverSwitchPreferenceController.java文件

/** Copyright (C) 2017 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file* except in compliance with the License. You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software distributed under the* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY* KIND, either express or implied. See the License for the specific language governing* permissions and limitations under the License.*/
package com.android.settings.dream;import static android.provider.Settings.Secure.SCREENSAVER_SWITCH;import android.content.Context;
import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;public class ScreenSaverSwitchPreferenceController extends AbstractPreferenceControllerimplements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {private static final String KEY_SCREENSAVER_SWITCH_NAME = "screensaver_switch";public ScreenSaverSwitchPreferenceController(Context context) {super(context);}@Overridepublic boolean isAvailable() {return true;}@Overridepublic String getPreferenceKey() {return KEY_SCREENSAVER_SWITCH_NAME;}@Overridepublic boolean onPreferenceChange(Preference preference, Object newValue) {boolean value = (Boolean) newValue;Settings.Secure.putInt(mContext.getContentResolver(),KEY_SCREENSAVER_SWITCH_NAME, value ? 1 : 0);if (value) {Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.SCREENSAVER_SWITCH,1);} else {Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.SCREENSAVER_SWITCH,0);}return true;}@Overridepublic void updateState(Preference preference) {int value = Settings.Secure.getInt(mContext.getContentResolver(),KEY_SCREENSAVER_SWITCH_NAME, 0);((SwitchPreference) preference).setChecked(value == 1);}
}

2.5 vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/dream/DreamSettings.java

@@ -132,6 +132,7 @@ public class DreamSettings extends DashboardFragment {controllers.add(new StartNowPreferenceController(context));
+        controllers.add(new ScreenSaverSwitchPreferenceController(context));return controllers;

3.隐藏“启动时间”

--- a/vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/dream/StartNowPreferenceController.java
+++ b/vendor/mediatek/proprietary/packages/apps/MtkSettings/src/com/android/settings/dream/StartNowPreferenceController.java
@@ -40,7 +40,7 @@ public class StartNowPreferenceController extends AbstractPreferenceController i@Overridepublic boolean isAvailable() {
-        return true;
+        return false;}

4.添加选择无操作多少分钟后进入屏保的ListPreference

4.1添加系统自定义全局变量,用来保存无操作多少分钟后进入屏保的时间。
frameworks/base/core/java/android/provider/Settings.java

 public static final class Secure extends NameValueTable {/** @hide */public static final String SCREENSAVER_TIMEOUT= "seewo.screensavers.timeout";

4.2设置–>显示–>屏保中添加ListPreference
vendor/mediatek/proprietary/packages/apps/MtkSettings/res/xml/dream_fragment_overview.xml

+    <ListPreference
+        android:key="screensaver_timeout"
+        android:title="@string/screensaver_timeout"
+        android:entries="@array/screensaver_timeout_entries"
+        android:entryValues="@array/screensaver_timeout_values"
+        settings:controller="com.android.settings.dream.ScreenSaverTimeoutPreferenceController" />

vendor/mediatek/proprietary/packages/apps/MtkSettings/res/values/arrays.xml

+    <!--  Titles for screensaver preference. -->
+    <string-array name="screensaver_timeout_entries" >
+        <item>@string/screensaver_timeout_1min</item>
+        <item>@string/screensaver_timeout_5mins</item>
+        <item>@string/screensaver_timeout_10mins</item>
+        <item>@string/screensaver_timeout_15mins</item>
+        <item>@string/screensaver_timeout_30mins</item>
+    </string-array>
+
+    <!--  Values for screensaver preference. -->
+    <string-array name="screensaver_timeout_values" translatable="false" >
+        <item>60000</item>
+        <item>300000</item>
+        <item>600000</item>
+        <item>900000</item>
+        <item>1800000</item>
+    </string-array>

vendor/mediatek/proprietary/packages/apps/MtkSettings/res/values-zh-rCN/strings.xml

+    <string name="screensaver_timeout">屏保</string>
+    <string name="screensaver_timeout_1min">1 分钟</string>
+    <string name="screensaver_timeout_5mins">5 分钟</string>
+    <string name="screensaver_timeout_10mins">10 分钟</string>
+    <string name="screensaver_timeout_15mins">15 分钟</string>
+    <string name="screensaver_timeout_30mins">30 分钟</string>
+    <string name="screensaver_timeout_summary">"无操作<xliff:g id="TIMEOUT_DESCRIPTION">%1$s</xliff:g>后"</string>
+    <string name="screensaver_switch_title">屏幕互保</string>

4.3添加vendor\mediatek\proprietary\packages\apps\MtkSettings\src\com\android\settings\dream\ScreenSaverTimeoutPreferenceController.java

/** Copyright (C) 2018 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License*/package com.android.settings.dream;import static android.provider.Settings.Secure.SCREENSAVER_TIMEOUT;import android.content.Context;
import android.provider.Settings;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.util.FeatureFlagUtils;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
/*** Setting where user can pick if SystemUI will be light, dark or try to match* the wallpaper colors.*/
public class ScreenSaverTimeoutPreferenceController extends BasePreferenceControllerimplements Preference.OnPreferenceChangeListener {private ListPreference mScreenSaverTimeoutPref;public ScreenSaverTimeoutPreferenceController(Context context, String preferenceKey) {super(context, preferenceKey);}@Overridepublic int getAvailabilityStatus() {return AVAILABLE;}@Overridepublic void displayPreference(PreferenceScreen screen) {super.displayPreference(screen);mScreenSaverTimeoutPref = (ListPreference) screen.findPreference(getPreferenceKey());int value = Settings.Secure.getInt(mContext.getContentResolver(), SCREENSAVER_TIMEOUT, 60000);mScreenSaverTimeoutPref.setValue(Integer.toString(value));}@Overridepublic boolean onPreferenceChange(Preference preference, Object newValue) {int value = Integer.parseInt((String) newValue);Settings.Secure.putInt(mContext.getContentResolver(), SCREENSAVER_TIMEOUT, value);refreshSummary(preference);return true;}@Overridepublic CharSequence getSummary() {int value = Settings.Secure.getInt(mContext.getContentResolver(), SCREENSAVER_TIMEOUT, 60000);int index = mScreenSaverTimeoutPref.findIndexOfValue(Integer.toString(value));return mContext.getString(R.string.screensaver_timeout_summary, mScreenSaverTimeoutPref.getEntries()[index]);}
}

5.屏保默认为原生时钟,移除“万花筒”

5.1 原生屏保默认是使用Google GMS中的时钟,因此修改为默认使用原生的时钟。
frameworks/base/core/res/res/values/config.xml

-    <string name="config_dreamsDefaultComponent" translatable="false">com.google.android.deskclock/com.android.deskclock.Screensaver</string>
+    <string name="config_dreamsDefaultComponent" translatable="false">com.android.deskclock/com.android.deskclock.Screensaver</string>

5.2移除“万花筒”
vendor/mediatek/proprietary/packages/apps/SettingsLib/src/com/android/settingslib/dream/DreamBackend.java

     public List<DreamInfo> getDreamInfos() {logd("getDreamInfos()");
+        String componentName;//del basic.ColorsComponentName activeDream = getActiveDream();PackageManager pm = mContext.getPackageManager();Intent dreamIntent = new Intent(DreamService.SERVICE_INTERFACE);
@@ -124,6 +125,12 @@ public class DreamBackend {dreamInfo.caption = resolveInfo.loadLabel(pm);dreamInfo.icon = resolveInfo.loadIcon(pm);dreamInfo.componentName = getDreamComponentName(resolveInfo);
+            componentName = dreamInfo.componentName.toString();
+            if(componentName.contains("com.android.dreams.basic.Colors")){
+                continue;
+            }dreamInfo.isActive = dreamInfo.componentName.equals(activeDream);

6.定制屏保时间

如果休眠时间设定小于屏保时间,则优先进入休眠,如果休眠时间设定大于屏保时间,则先进入屏保,达到休眠时间后再进入休眠的实现
frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java

6.1修改进入屏保的条件由Setting中的“屏幕互保”选项的开或关来决定

 private boolean updateWakefulnessLocked(int dirty) {boolean changed = false;if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY | DIRTY_BOOT_COMPLETED| DIRTY_WAKEFULNESS | DIRTY_STAY_ON | DIRTY_PROXIMITY_POSITIVE| DIRTY_DOCK_STATE)) != 0) {if (mWakefulness == WAKEFULNESS_AWAKE && isItBedTimeYetLocked()) {if (DEBUG_SPEW) {Slog.d(TAG, "updateWakefulnessLocked: Bed time...");}final long time = SystemClock.uptimeMillis();if (shouldNapAtBedTimeLocked()) {//此处为进入屏保的条件changed = napNoUpdateLocked(time, Process.SYSTEM_UID);} else {changed = goToSleepNoUpdateLocked(time,PowerManager.GO_TO_SLEEP_REASON_TIMEOUT, 0, Process.SYSTEM_UID);}}}return changed;}

shouldNapAtBedTimeLocked()为进入屏保的条件,所以需要修改shouldNapAtBedTimeLocked()的返回值。

    private boolean shouldNapAtBedTimeLocked() {
-        return mDreamsActivateOnSleepSetting
-                || (mDreamsActivateOnDockSetting
-                        && mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED);
+        return Settings.Secure.getInt(mContext.getContentResolver(),Settings.Secure.SCREENSAVER_SWITCH,0) == 1;
+        //return mDreamsActivateOnSleepSetting
+        //        || (mDreamsActivateOnDockSetting
+        //                && mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED);}

Settings.Secure.getInt(mContext.getContentResolver(),Settings.Secure.SCREENSAVER_SWITCH,0)是获取“屏幕互保”的状态值,1表示打开,0表示关闭。

6.2修改取决于什么时候进入屏保的逻辑。

 private void updateUserActivitySummaryLocked(long now, int dirty) {// Update the status of the user activity timeout timer.if ((dirty & (DIRTY_WAKE_LOCKS | DIRTY_USER_ACTIVITY| DIRTY_WAKEFULNESS | DIRTY_SETTINGS)) != 0) {mHandler.removeMessages(MSG_USER_ACTIVITY_TIMEOUT);long nextTimeout = 0;long screenSaverNextTimeout = 0;//添加计算下次进入屏保的时间boolean isScreenSaverEnabled = Settings.Secure.getInt(mContext.getContentResolver(),Settings.Secure.SCREENSAVER_SWITCH,0) == 1;//获取“屏幕互保”是否打开if (mWakefulness == WAKEFULNESS_AWAKE|| mWakefulness == WAKEFULNESS_DREAMING|| mWakefulness == WAKEFULNESS_DOZING) {final long sleepTimeout = getSleepTimeoutLocked();final long screenOffTimeout = getScreenOffTimeoutLocked(sleepTimeout);//无操作多少分钟后进入休眠的时间final long screenDimDuration = getScreenDimDurationLocked(screenOffTimeout);final boolean userInactiveOverride = mUserInactiveOverrideFromWindowManager;final long nextProfileTimeout = getNextProfileTimeoutLocked(now);final long screenSaverTimeout = Settings.Secure.getInt(mContext.getContentResolver(),Settings.Secure.SCREENSAVER_TIMEOUT,60000);//无操作多少分钟后进入屏保的时间mUserActivitySummary = 0;if (mLastUserActivityTime >= mLastWakeTime) {nextTimeout = mLastUserActivityTime+ screenOffTimeout - screenDimDuration;screenSaverNextTimeout = mLastUserActivityTime + screenSaverTimeout;//无操作后下次进入屏保的时间if (now < nextTimeout) {//当“屏幕互保”是打开的,并且无操作后进入休眠的时间 大于 无操作后进入屏保的时间,//而且当前的时间 大于 无操作后下次进入屏保的时间则进入屏保if(isScreenSaverEnabled && screenOffTimeout > screenSaverTimeout && now > screenSaverNextTimeout ){mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM;}else{//否则保持屏保亮屏mUserActivitySummary = USER_ACTIVITY_SCREEN_BRIGHT;}//Neostra dangziming end screensaver} else {nextTimeout = mLastUserActivityTime + screenOffTimeout;if (now < nextTimeout) {mUserActivitySummary = USER_ACTIVITY_SCREEN_DIM;}}}if (mUserActivitySummary == 0&& mLastUserActivityTimeNoChangeLights >= mLastWakeTime) {nextTimeout = mLastUserActivityTimeNoChangeLights + screenOffTimeout;if (now < nextTimeout) {if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_BRIGHT|| mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_VR) {mUserActivitySummary = USER_ACTIVITY_SCREEN_BRIGHT;} else if (mDisplayPowerRequest.policy == DisplayPowerRequest.POLICY_DIM) {mUserActivitySummary = USER_ACTIVITY_SCREEN_DIM;}}}if (mUserActivitySummary == 0) {if (sleepTimeout >= 0) {final long anyUserActivity = Math.max(mLastUserActivityTime,mLastUserActivityTimeNoChangeLights);if (anyUserActivity >= mLastWakeTime) {nextTimeout = anyUserActivity + sleepTimeout;if (now < nextTimeout) {mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM;}}} else {//mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM;//删除系统进入休眠后才进入屏保的逻辑nextTimeout = -1;}}if (mUserActivitySummary != USER_ACTIVITY_SCREEN_DREAM && userInactiveOverride) {if ((mUserActivitySummary &(USER_ACTIVITY_SCREEN_BRIGHT | USER_ACTIVITY_SCREEN_DIM)) != 0) {// Device is being kept awake by recent user activityif (nextTimeout >= now && mOverriddenTimeout == -1) {// Save when the next timeout would have occurredmOverriddenTimeout = nextTimeout;}}mUserActivitySummary = USER_ACTIVITY_SCREEN_DREAM;nextTimeout = -1;}if (nextProfileTimeout > 0) {nextTimeout = Math.min(nextTimeout, nextProfileTimeout);}if (mUserActivitySummary != 0 && nextTimeout >= 0) {//当“屏幕互保”是打开的,并且无操作后进入休眠的时间 大于 无操作后进入屏保的时间,//而且当前的时间 大于 无操作后下次进入屏保的时间则发送信息进入屏保if(isScreenSaverEnabled && screenOffTimeout > screenSaverTimeout && now <= screenSaverNextTimeout ){scheduleUserInactivityTimeout(screenSaverNextTimeout);}else{//否则发送进入休眠的信息scheduleUserInactivityTimeout(nextTimeout);}//Neostra dangziming end screensaver}} else {mUserActivitySummary = 0;}if (DEBUG_SPEW) {Slog.d(TAG, "updateUserActivitySummaryLocked: mWakefulness="+ PowerManagerInternal.wakefulnessToString(mWakefulness)+ ", mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary)+ ", nextTimeout=" + TimeUtils.formatUptime(nextTimeout));}}}

updateUserActivitySummaryLocked()的逻辑分析可以参考Android 8.1 PowerManagerService分析(二) ——updatePowerStateLocked()方法

如果此文章解答了您的相关疑惑,节约了您的宝贵时间,可以赏小编鸡腿哟!

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/438636.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【React教程】(3) React之表单、组件、事件处理详细代码示例

目录 事件处理示例1示例2示例3&#xff08;this 绑定问题&#xff09;示例4&#xff08;传递参数&#xff09;Class 和 Style 表单处理组件组件规则注意事项函数式组件&#xff08;无状态&#xff09;类方式组件&#xff08;有状态&#xff09;组件传值 Propsthis.props.childr…

docker-compose部署单机ES+Kibana

记录部署的操作步骤 准备工作编写docker-compose.yml启动服务验证部署结果 本次elasticsearch和kibana版本为8.2.2 使用环境&#xff1a;centos7.9 本次记录还包括&#xff1a;安装elasticsearch中文分词插件和拼音分词插件 准备工作 1、创建目录和填写配置 mkdir /home/es/s…

分表过多引起的问题/Apache ShardingSphere元数据加载慢

目录 环境 背景 探寻 元数据的加载策略 如何解决 升级版本到5.x 调大max.connections.size.per.query max.connections.size.per.query分析 服务启动阶段相关源码 服务运行阶段相关源码 受到的影响 注意事项&#xff08;重要&#xff09; 其他 环境 Spring Boot 2…

70从零开始学Java之Collection与Collections有什么区别?

作者:孙玉昌,昵称【一一哥】,另外【壹壹哥】也是我哦 CSDN博客专家、万粉博主、阿里云专家博主、掘金优质作者 前言 截止到现在,壹哥已经把Java里的List、Set和Map这三大集合都给大家讲解完毕了,不知道各位掌握了多少呢?如果你对之前的内容还没有熟练掌握,可以把壹哥前…

AI新工具(20240129) AI红包封面;Baichuan 3-超千亿参数的大语言模型;腾讯文档智能助手

AI红包封面-定制微信红包封面 AI红包封面生成器利用AI技术&#xff0c;为用户定制微信红包封面&#xff0c;生成精美作品。产品定位于为用户提供个性化、精美的微信红包封面定制服务。定价根据封面定制的复杂程度而定。 AI: Art Impostor-AI绘画多人联机休闲派对游戏 AI: Art…

计网Lesson12 - UDP客户服务器模型和UDP协议

文章目录 丢个图在这&#xff0c;实在不是很明白在讲啥&#xff0c;等学完网编的我归来狠狠拿下它

微信小程序如何搜索iBeacon设备

1.首先在utils文件夹下创建bluetooth.js和ibeacon.js 2.在 bluetooth.js文件中写入 module.exports {initBluetooth: function () {// 初始化蓝牙模块wx.openBluetoothAdapter({success: function (res) {console.log(蓝牙模块初始化成功);},fail: function (res) {console.l…

贪吃蛇项目

引言&#xff1a; 本文章使用C语言在Windows环境的控制台中模拟实现经典小游戏贪吃蛇。 实现基本功能&#xff1a; 1.贪吃蛇地图绘制。 2.蛇吃食物的功能&#xff08;上、下、左、右方向键控制蛇的动作&#xff09; 3.蛇撞墙死亡 4.蛇咬到自己死亡 5.计算得分 6.蛇加速…

Spark3内核源码与优化

文章目录 一、Spark内核原理1、Spark 内核概述1.1 简介1.2 Spark 核心组件1.3 Spark 通用运行流程概述 2、Spark 部署模式2.1 YARN Cluster 模式(重点)2.2 YARN Client 模式2.3 Standalone Cluster 模式2.4 Standalone Client 模式 3、Spark 通讯架构3.1 Spark 通信架构概述3.2…

【React教程】(2) React之JSX入门与列表渲染、条件渲染详细代码示例

目录 JSX环境配置基本语法规则在 JSX 中嵌入 JavaScript 表达式在 JavaScript 表达式中嵌入 JSXJSX 中的节点属性声明子节点JSX 自动阻止注入攻击在 JSX 中使用注释JSX 原理列表循环DOM Elements 列表渲染语法高亮 条件渲染示例1&#xff1a;示例2&#xff1a;示例3&#xff08…

Spring Security的入门案例!!!

一、导入依赖 <dependencies><!--web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--security--><dependency><groupId>…

幻兽帕鲁服务器出租,腾讯云PK阿里云怎么收费?

幻兽帕鲁服务器价格多少钱&#xff1f;4核16G服务器Palworld官方推荐配置&#xff0c;阿里云4核16G服务器32元1个月、96元3个月&#xff0c;腾讯云换手帕服务器服务器4核16G14M带宽66元一个月、277元3个月&#xff0c;8核32G22M配置115元1个月、345元3个月&#xff0c;16核64G3…