1. Simple analysis of setting never sleep
In the " Settings " application of Android phones, you can set the phone screen timeout period. As shown in the figure below: Through the options, you can see that it can be set up to 30 minutes. If you need to never sleep, you can achieve the goal by setting the screen timeout time large enough, such as the maximum value of Int. Next, trace how it is implemented in the "Settings" application in the source code. The source path of the "Settings" application in the source code is as follows:
packages/apps/Settings
Through the keyword search and analysis of the directory, the source file for setting the screen timeout is found as follows:
packages/apps/Settings/src/com/android/settings/display/TimeoutPreferenceController.java
The key functions to modify the screen timeout in " TimeoutPreferenceController.java " are as follows:
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
try {
int value = Integer.parseInt((String) newValue);
//最终是调用这个函数实现的
Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, value);
updateTimeoutPreferenceDescription((TimeoutListPreference) preference, value);
} catch (NumberFormatException e) {
Log.e(TAG, "could not persist screen timeout setting", e);
}
return true;
}
From the code, you can see that the screen timeout time is modified by the following code:
Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT, 6000);
The following will add a "never sleep" function to the screen timeout in the settings application.
2. Documents involved in modification
//添加永不休眠以及时间选项
packages/apps/Settings/res/values/arrays.xml
//添加中文展示的永不休眠选项
packages/apps/Settings/res/values/values-zh-rCN/arrays.xml
//Settings.System.putInt方法的实现类,根据传入的特殊值-1修改为一个很大的休眠时间
/frameworks/base/core/java/android/provider/Settings.java
Three, modify the actual combat
1. Add the option of never sleep in the settings app
add the following content to packages/apps/Settings/res/values/arrays.xml :
<string-array name="screen_timeout_entries">
<item>15 seconds</item>
<item>30 seconds</item>
<item>1 minute</item>
<item>2 minutes</item>
<item>5 minutes</item>
<item>10 minutes</item>
<item>30 minutes</item>
<!-- ///ADD START 此处新增的英文语言下Never展示项-->
<item>Never</item>
<!-- ///ADD END -->
</string-array>
<!-- Do not translate. -->
<string-array name="screen_timeout_values" translatable="false">
<!-- Do not translate. -->
<item>15000</item>
<!-- Do not translate. -->
<item>30000</item>
<!-- Do not translate. -->
<item>60000</item>
<!-- Do not translate. -->
<item>120000</item>
<!-- Do not translate. -->
<item>300000</item>
<!-- Do not translate. -->
<item>600000</item>
<!-- Do not translate. -->
<item>1800000</item>
<!-- ///ADD START 此处新增的休眠时间-1-->
<item>-1</item>
<!-- ///ADD END -->
</string-array>
packages/apps/Settings/res/values/values-zh-rCN/arrays.xml add the following content:
<string-array name="screen_timeout_entries">
<item msgid="8386012403457852396">"15 秒"</item>
<item msgid="4572123773028439079">"30 秒"</item>
<item msgid="7016081293774377048">"1 分钟"</item>
<item msgid="838575533670111144">"2 分钟"</item>
<item msgid="2693197579676214668">"5 分钟"</item>
<item msgid="1955784331962974678">"10 分钟"</item>
<item msgid="5578717731965793584">"30 分钟"</item>
<!-- ///ADD START 此处新增的中文语言展示选择项-->
<item>永不睡觉</item>
<!-- ///ADD END -->
</string-array>
2. Modify the set sleep time in the Settings.java file
Find the Settings.System.putInt method in the file, and modify the associated method code as follows:
//putInt最终调用的是putIntForUser
public static boolean putInt(ContentResolver cr, String name, int value) {
return putIntForUser(cr, name, value, cr.getUserId());
}
//putIntForUser中根据传入的name和value特殊值-1进行修改设置
/** @hide */
@UnsupportedAppUsage
public static boolean putIntForUser(ContentResolver cr, String name, int value,int userHandle) {
///ADD START
if(name.equals(SCREEN_OFF_TIMEOUT))
{
//-1说明是我们在设置中添加的永不休眠的值
if(value==-1)
{
//
Log.d("Settings","change screen timeout for:"+Integer.toString(Integer.MAX_VALUE-1000));
return putStringForUser(cr, name, Integer.toString(Integer.MAX_VALUE-1000), userHandle);
}
}
///ADD END
return putStringForUser(cr, name, Integer.toString(value), userHandle);
}
Four, effect display
After the modification, compile and flash the machine, I tested the machine and hung up for a day without sleeping. Show pictures:
Android10 system source code kernel custom development from entry to pit
How to use EdXposed ART hook framework
Use adb command to install Edxposed without Magisk
Android10 system custom development modification Android source code close selinux
The big guys keep a concern before leaving, and follow-up wonderful articles continue