android4.2 增添永不休眠选项

时间:2022-05-26 23:59:19

android4.2 增添永不休眠选项

感谢原创:http://www.csdn123.com/html/exception/626/626185_626187_626186.htm
设置 > 显示 > 休眠

a.
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>

<span style="color:#339966;"><item>Never </item></span>

</string-array>

<!-- AnsonCode 2012.7.4

+ <item>Never </item>

-->

<!-- 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>

<span style="color:#339966;"><item>-1</item></span>

</string-array>

<span style="color:#339966;"><!-- AnsonCode

-1 的值在表示永不休眠。
+ <item>-1</item>

-->
</span>

b.
在其它有定义<string-array name="screen_timeout_entries">
的地方添加一行,用于显示不同语言文字的内容,这是为了解决在不同语言时,如果没有定义,会出现异常退出的情况。大概有50个arrays.xml文件需要修改。
如:packages/apps/Settings/res/values-zh-rCN/arrays.xml中(此路径中绿色表示国家语言)
<string-array name="screen_timeout_entries">

<item msgid="3342301044271143016">"15 秒"</item>

<item msgid="8881760709354815449">"30 秒"</item>

<item msgid="7589406073232279088">"1 分钟"</item>

<item msgid="7001195990902244174">"2 分钟"</item>

<item msgid="7489864775127957179">"5 分钟"</item>

<item msgid="2314124409517439288">"10 分钟"</item>

<item msgid="6864027152847611413">"30 分钟"</item>

<item msgid="6864027952847611413">"从不"</item>

</string-array>


c.
在作出以上修改后,主界面的显示会不正常,需要在代码中加以下处理:
找到如下方法:packages/apps/Settings/src/com/android/settings/DisplaySettings.java

private void updateTimeoutPreferenceDescription(long currentTimeout) {

ListPreference preference = mScreenTimeoutPreference;

String summary;

if (currentTimeout < -1) {

//if (currentTimeout < 0) { AnsonCode2012.7.4

// Unsupported value

summary = "";

} else {

final CharSequence[] entries = preference.getEntries();

final CharSequence[] values = preference.getEntryValues();

int best = 0;

for (int i = 0; i < values.length; i++) {

long timeout = Long.parseLong(values[i].toString());

if (currentTimeout == timeout) {

best = i;

break;////AnsonCode2012.7.4

}

}

summary = preference.getContext().getString(R.string.screen_timeout_summary,

entries[best]);

if(currentTimeout < 0)summary = entries[best].toString();//AnsonCode2012.7.4

}

preference.setSummary(summary);

}


//注意与原来代码的区别:
//1) 修改 0 改为-1, 为保证,在界面中,显示的文字不为空。
//2)将 >=改为 ==是作一个准确判断,找到所设定的值便返回,退出循环;
//3)不加此句内容,在界面上会显示: 无操作 从不 后, 加了以后则显示: 从不

//在4.2上稍微有些不同, 主要是修改了对时间值的判断。
private static final int DEFAULT_SCREEN_OFF_TIMEOUT = 15 * 1000;
private static final int MINIMUM_SCREEN_OFF_TIMEOUT = 10 * 1000;
private int <span style="color:#339966;">getScreenOffTimeoutLocked</span>() {
int timeout = mScreenOffTimeoutSetting;
if (isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) {
timeout = Math.min(timeout, mMaximumScreenOffTimeoutFromDeviceAdmin);
}
if (mUserActivityTimeoutOverrideFromWindowManager >= 0) {
timeout = (int)Math.min(timeout, mUserActivityTimeoutOverrideFromWindowManager);
}
<span style="color:#339966;">return Math.max(timeout, MINIMUM_SCREEN_OFF_TIMEOUT);</span>
}


/**
* Updates the value of mUserActivitySummary to summarize the user requested
* state of the system such as whether the screen should be bright or dim.
* Note that user activity is ignored when the system is asleep.
*
* This function must have no other side-effects.
*/
private void updateUserActivitySummaryLocked(long now, int dirty) {
// Update the status of the user activity timeout timer.
if ((dirty & (DIRTY_USER_ACTIVITY | DIRTY_WAKEFULNESS | DIRTY_SETTINGS)) != 0) {
mHandler.removeMessages(MSG_USER_ACTIVITY_TIMEOUT);

long nextTimeout = 0;
if (mWakefulness != WAKEFULNESS_ASLEEP) {
final int screenOffTimeout = <span style="color:#339966;">getScreenOffTimeoutLocked</span>();
final int screenDimDuration = getScreenDimDurationLocked(screenOffTimeout);

mUserActivitySummary = 0;
if (mLastUserActivityTime >= mLastWakeTime) {
nextTimeout = mLastUserActivityTime
+ screenOffTimeout - screenDimDuration;
if (now < nextTimeout) {
mUserActivitySummary |= USER_ACTIVITY_SCREEN_BRIGHT;
} else {
nextTimeout = mLastUserActivityTime + screenOffTimeout;
if (now < nextTimeout) {
mUserActivitySummary |= USER_ACTIVITY_SCREEN_DIM;
}
}
}
if (mUserActivitySummary == 0
&& mLastUserActivityTimeNoChangeLights >= mLastWakeTime) {
nextTimeout = mLastUserActivityTimeNoChangeLights + screenOffTimeout;
if (now < nextTimeout
&& mDisplayPowerRequest.screenState
!= DisplayPowerRequest.SCREEN_STATE_OFF) {
mUserActivitySummary = mDisplayPowerRequest.screenState
== DisplayPowerRequest.SCREEN_STATE_BRIGHT ?
USER_ACTIVITY_SCREEN_BRIGHT : USER_ACTIVITY_SCREEN_DIM;
}
}
if (mUserActivitySummary != 0) {
Message msg = mHandler.obtainMessage(MSG_USER_ACTIVITY_TIMEOUT);
msg.setAsynchronous(true);
///mHandler.sendMessageAtTime(msg, nextTimeout);
if(mScreenOffTimeoutSetting > 0)mHandler.sendMessageAtTime(msg, nextTimeout);<span style="color:#339966;">//添加了一行判断, 如果值为-1则不发送广播</span>
}
} else {
mUserActivitySummary = 0;
}

if (DEBUG_SPEW) {
Slog.d(TAG, "updateUserActivitySummaryLocked: mWakefulness="
+ wakefulnessToString(mWakefulness)
+ ", mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary)
+ ", nextTimeout=" + TimeUtils.formatUptime(nextTimeout));
}
}
}
还需要改个地方:
frameworks/base/services/java/com/android/server/power/PowerManagerService.java

private void updateUserActivitySummaryLocked(long now, int dirty) {

.....

//change code here ==============

Slog.d(TAG,"mScreenOffTimeoutSetting ="+mScreenOffTimeoutSetting);

//if (mUserActivitySummary != 0 ) {

if (mUserActivitySummary != 0 && mScreenOffTimeoutSetting > 0) {

//change code here end ==============

Message msg = mHandler.obtainMessage(MSG_USER_ACTIVITY_TIMEOUT);

msg.setAsynchronous(true);

mHandler.sendMessageAtTime(msg, nextTimeout);

}

 最后,设置默认休眠时间值的路径:<strong>frameworks\base\packages\SettingsProvider\res\values\defaults.xml</strong>中设置def_screen_off_timeout的值就可以!