android4.4 锁屏灭屏时间的设置

屏幕自动灭屏的时间和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;

而这个变量就是在KeyguardViewManager.java中设置的,因此到这里我们就知道这个值就是在Keyguard窗口起来的时候设置的,而由于这个值默认值是-1,其他窗口起来时,也把窗口的userActivityTimeout设置到PowerManagerService中去了,这时mUserActivityTimeoutOverrideFromWindowManager为-1。也就是说只有在keyguard的时候这个值才不是-1.

    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;
    }

而我们修改锁屏的时间,就是先修改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;
    }


你可能感兴趣的:(android电源)