屏幕自动灭屏的时间和userActivity事件相关,而这个灭屏时间的获取是在PowerManagerService中getScreenOffTimeoutLocked函数。
private int getScreenOffTimeoutLocked() {
int timeout = mScreenOffTimeoutSetting;
if (isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) {
timeout = Math.min(timeout, mMaximumScreenOffTimeoutFromDeviceAdmin);
}
if (mUserActivityTimeoutOverrideFromWindowManager >= 0) {
timeout = (int)Math.min(timeout, mUserActivityTimeoutOverrideFromWindowManager);
}
return Math.max(timeout, MINIMUM_SCREEN_OFF_TIMEOUT);
}
我们先来看下mScreenOffTimeoutSetting这个变量,如果没有设置的话,默认是DEFAULT_SCREEN_OFF_TIMEOUT常量为15秒。
mScreenOffTimeoutSetting = Settings.System.getIntForUser(resolver,
Settings.System.SCREEN_OFF_TIMEOUT, DEFAULT_SCREEN_OFF_TIMEOUT,
UserHandle.USER_CURRENT);
而keyguard的灭屏时间和mUserActivityTimeoutOverrideFromWindowManager值有关。
下面我们来看看这个值是哪里来的?
我在WMS的performLayoutAndPlaceSurfacesLockedInner函数中,会调用如下函数。
mPowerManager.setUserActivityTimeoutOverrideFromWindowManager(
mInnerFields.mUserActivityTimeout);
这个函数最终就会设置mUserActivityTimeoutOverrideFromWindowManager。
public void setUserActivityTimeoutOverrideFromWindowManager(long timeoutMillis) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
final long ident = Binder.clearCallingIdentity();
try {
setUserActivityTimeoutOverrideFromWindowManagerInternal(timeoutMillis);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
private void setUserActivityTimeoutOverrideFromWindowManagerInternal(long timeoutMillis) {
synchronized (mLock) {
if (mUserActivityTimeoutOverrideFromWindowManager != timeoutMillis) {
mUserActivityTimeoutOverrideFromWindowManager = timeoutMillis;
mDirty |= DIRTY_SETTINGS;
updatePowerStateLocked();
}
}
}
而在WMS调用PowerManagerService的setUserActivityTimeoutOverrideFromWindowManager的参数mInnerFields.mUserActivityTimeout其实也是WindowState的mAttrs(WindowManager.LayoutParams)的userActivityTimeout变量。
mInnerFields.mUserActivityTimeout = w.mAttrs.userActivityTimeout;
private void updateUserActivityTimeoutInWindowLayoutParams() {
// Use the user activity timeout requested by the keyguard view, if any.
if (mKeyguardView != null) {
long timeout = mKeyguardView.getUserActivityTimeout();
if (timeout >= 0) {
mWindowLayoutParams.userActivityTimeout = timeout;
return;
}
}
// Otherwise, use the default timeout.
mWindowLayoutParams.userActivityTimeout = KeyguardViewMediator.AWAKE_INTERVAL_DEFAULT_MS;
}
然后在PowerManagerService中的getScreenOffTimeoutLocked函数中,当这个时候在锁屏界面时,因为是keyguard界面,这时候WMS会调用PowerManagerService的setUserActivityTimeoutOverrideFromWindowManager函数来设置mUserActivityTimeoutOverrideFromWindowManager,因此这个时候mUserActivityTimeoutOverrideFromWindowManager不为-1.这个时候我们可以将timeout直接设置为mUserActivityTimeoutOverrideFromWindowManager(Keyguard设置的屏幕锁屏时间),而不用和PowerManagerService现在的timeout对比,最后在锁屏的时候灭屏的timeout就会变成我们设置的时间了。
private int getScreenOffTimeoutLocked() {
int timeout = mScreenOffTimeoutSetting;
if (isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) {
timeout = Math.min(timeout, mMaximumScreenOffTimeoutFromDeviceAdmin);
}
if (mUserActivityTimeoutOverrideFromWindowManager >= 0) {
timeout = (int)mUserActivityTimeoutOverrideFromWindowManager;//设置为keyguard设置的timeout
}
int lockedtimeout = Math.max(timeout, MINIMUM_SCREEN_OFF_TIMEOUT);
return lockedtimeout;
}