Loading services/core/java/com/android/server/power/AttentionDetector.java +28 −6 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ package com.android.server.power; import static android.provider.DeviceConfig.NAMESPACE_ATTENTION_MANAGER_SERVICE; import static android.provider.Settings.Secure.ADAPTIVE_SLEEP; import android.Manifest; import android.app.ActivityManager; Loading Loading @@ -75,6 +76,12 @@ public class AttentionDetector { /** Default value in absence of {@link DeviceConfig} override. */ static final long DEFAULT_POST_DIM_CHECK_DURATION_MILLIS = 0; /** * DeviceConfig flag name, describes the limit of how long the device can remain unlocked due to * attention checking. */ static final String KEY_MAX_EXTENSION_MILLIS = "post_dim_check_duration_millis"; private Context mContext; private boolean mIsSettingEnabled; Loading @@ -85,11 +92,11 @@ public class AttentionDetector { private final Runnable mOnUserAttention; /** * The maximum time, in millis, that the phone can stay unlocked because of attention events, * triggered by any user. * The default value for the maximum time, in millis, that the phone can stay unlocked because * of attention events, triggered by any user. */ @VisibleForTesting protected long mMaximumExtensionMillis; protected long mDefaultMaximumExtensionMillis; private final Object mLock; Loading Loading @@ -162,7 +169,7 @@ public class AttentionDetector { mContentResolver = context.getContentResolver(); mAttentionManager = LocalServices.getService(AttentionManagerInternal.class); mWindowManager = LocalServices.getService(WindowManagerInternal.class); mMaximumExtensionMillis = context.getResources().getInteger( mDefaultMaximumExtensionMillis = context.getResources().getInteger( com.android.internal.R.integer.config_attentionMaximumExtension); try { Loading Loading @@ -196,7 +203,7 @@ public class AttentionDetector { final long now = SystemClock.uptimeMillis(); final long whenToCheck = nextScreenDimming - getPreDimCheckDurationMillis(); final long whenToStopExtending = mLastUserActivityTime + mMaximumExtensionMillis; final long whenToStopExtending = mLastUserActivityTime + getMaxExtensionMillis(); if (now < whenToCheck) { if (DEBUG) { Slog.d(TAG, "Do not check for attention yet, wait " + (whenToCheck - now)); Loading Loading @@ -309,7 +316,7 @@ public class AttentionDetector { public void dump(PrintWriter pw) { pw.println("AttentionDetector:"); pw.println(" mIsSettingEnabled=" + mIsSettingEnabled); pw.println(" mMaximumExtensionMillis=" + mMaximumExtensionMillis); pw.println(" mMaxExtensionMillis=" + getMaxExtensionMillis()); pw.println(" preDimCheckDurationMillis=" + getPreDimCheckDurationMillis()); pw.println(" postDimCheckDurationMillis=" + mLastPostDimTimeout); pw.println(" mLastUserActivityTime(excludingAttention)=" + mLastUserActivityTime); Loading Loading @@ -348,6 +355,21 @@ public class AttentionDetector { return mLastPostDimTimeout; } /** How long the device can remain unlocked due to attention checking. */ @VisibleForTesting protected long getMaxExtensionMillis() { final long millis = DeviceConfig.getLong(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, mDefaultMaximumExtensionMillis); if (millis < 0 || millis > 60 * 60 * 1000) { // 1 hour Slog.w(TAG, "Bad flag value supplied for: " + KEY_MAX_EXTENSION_MILLIS); return mDefaultMaximumExtensionMillis; } return millis; } @VisibleForTesting final class AttentionCallbackInternalImpl extends AttentionCallbackInternal { private final int mId; Loading services/tests/servicestests/src/com/android/server/power/AttentionDetectorTest.java +45 −1 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ import static android.provider.DeviceConfig.NAMESPACE_ATTENTION_MANAGER_SERVICE; import static com.android.server.power.AttentionDetector.DEFAULT_POST_DIM_CHECK_DURATION_MILLIS; import static com.android.server.power.AttentionDetector.DEFAULT_PRE_DIM_CHECK_DURATION_MILLIS; import static com.android.server.power.AttentionDetector.KEY_MAX_EXTENSION_MILLIS; import static com.android.server.power.AttentionDetector.KEY_POST_DIM_CHECK_DURATION_MILLIS; import static com.android.server.power.AttentionDetector.KEY_PRE_DIM_CHECK_DURATION_MILLIS; Loading Loading @@ -87,6 +88,7 @@ public class AttentionDetectorTest extends AndroidTestCase { when(mWindowManagerInternal.isKeyguardShowingAndNotOccluded()).thenReturn(false); mAttentionDetector = new TestableAttentionDetector(); mRealAttentionDetector = new AttentionDetector(mOnUserAttention, new Object()); mRealAttentionDetector.mDefaultMaximumExtensionMillis = 900_000L; mAttentionDetector.onWakefulnessChangeStarted(PowerManagerInternal.WAKEFULNESS_AWAKE); mAttentionDetector.setAttentionServiceSupported(true); mNextDimming = SystemClock.uptimeMillis() + 3000L; Loading @@ -98,6 +100,10 @@ public class AttentionDetectorTest extends AndroidTestCase { Settings.Secure.putIntForUser(getContext().getContentResolver(), Settings.Secure.ADAPTIVE_SLEEP, 1, UserHandle.USER_CURRENT); mAttentionDetector.updateEnabledFromSettings(getContext()); DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, Long.toString(10_000L), false); } @After Loading @@ -111,6 +117,9 @@ public class AttentionDetectorTest extends AndroidTestCase { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_POST_DIM_CHECK_DURATION_MILLIS, Long.toString(DEFAULT_POST_DIM_CHECK_DURATION_MILLIS), false); DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, Long.toString(mRealAttentionDetector.mDefaultMaximumExtensionMillis), false); } @Test Loading Loading @@ -393,6 +402,42 @@ public class AttentionDetectorTest extends AndroidTestCase { DEFAULT_POST_DIM_CHECK_DURATION_MILLIS); } @Test public void testGetMaxExtensionMillis_handlesGoodFlagValue() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, "123", false); assertThat(mRealAttentionDetector.getMaxExtensionMillis()).isEqualTo(123); } @Test public void testGetMaxExtensionMillis_rejectsNegativeValue() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, "-50", false); assertThat(mRealAttentionDetector.getMaxExtensionMillis()).isEqualTo( mRealAttentionDetector.mDefaultMaximumExtensionMillis); } @Test public void testGetMaxExtensionMillis_rejectsTooBigValue() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, "9900000", false); assertThat(mRealAttentionDetector.getMaxExtensionMillis()).isEqualTo( mRealAttentionDetector.mDefaultMaximumExtensionMillis); } @Test public void testGetMaxExtensionMillis_handlesBadFlagValue() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, "20000k", false); assertThat(mRealAttentionDetector.getMaxExtensionMillis()).isEqualTo( mRealAttentionDetector.mDefaultMaximumExtensionMillis); DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, "0.25", false); assertThat(mRealAttentionDetector.getMaxExtensionMillis()).isEqualTo( mRealAttentionDetector.mDefaultMaximumExtensionMillis); } private long registerAttention() { mPreDimCheckDuration = 4000L; mAttentionDetector.onUserActivity(SystemClock.uptimeMillis(), Loading @@ -409,7 +454,6 @@ public class AttentionDetectorTest extends AndroidTestCase { mWindowManager = mWindowManagerInternal; mPackageManager = AttentionDetectorTest.this.mPackageManager; mContentResolver = getContext().getContentResolver(); mMaximumExtensionMillis = 10000L; } void setAttentionServiceSupported(boolean supported) { Loading Loading
services/core/java/com/android/server/power/AttentionDetector.java +28 −6 Original line number Diff line number Diff line Loading @@ -17,6 +17,7 @@ package com.android.server.power; import static android.provider.DeviceConfig.NAMESPACE_ATTENTION_MANAGER_SERVICE; import static android.provider.Settings.Secure.ADAPTIVE_SLEEP; import android.Manifest; import android.app.ActivityManager; Loading Loading @@ -75,6 +76,12 @@ public class AttentionDetector { /** Default value in absence of {@link DeviceConfig} override. */ static final long DEFAULT_POST_DIM_CHECK_DURATION_MILLIS = 0; /** * DeviceConfig flag name, describes the limit of how long the device can remain unlocked due to * attention checking. */ static final String KEY_MAX_EXTENSION_MILLIS = "post_dim_check_duration_millis"; private Context mContext; private boolean mIsSettingEnabled; Loading @@ -85,11 +92,11 @@ public class AttentionDetector { private final Runnable mOnUserAttention; /** * The maximum time, in millis, that the phone can stay unlocked because of attention events, * triggered by any user. * The default value for the maximum time, in millis, that the phone can stay unlocked because * of attention events, triggered by any user. */ @VisibleForTesting protected long mMaximumExtensionMillis; protected long mDefaultMaximumExtensionMillis; private final Object mLock; Loading Loading @@ -162,7 +169,7 @@ public class AttentionDetector { mContentResolver = context.getContentResolver(); mAttentionManager = LocalServices.getService(AttentionManagerInternal.class); mWindowManager = LocalServices.getService(WindowManagerInternal.class); mMaximumExtensionMillis = context.getResources().getInteger( mDefaultMaximumExtensionMillis = context.getResources().getInteger( com.android.internal.R.integer.config_attentionMaximumExtension); try { Loading Loading @@ -196,7 +203,7 @@ public class AttentionDetector { final long now = SystemClock.uptimeMillis(); final long whenToCheck = nextScreenDimming - getPreDimCheckDurationMillis(); final long whenToStopExtending = mLastUserActivityTime + mMaximumExtensionMillis; final long whenToStopExtending = mLastUserActivityTime + getMaxExtensionMillis(); if (now < whenToCheck) { if (DEBUG) { Slog.d(TAG, "Do not check for attention yet, wait " + (whenToCheck - now)); Loading Loading @@ -309,7 +316,7 @@ public class AttentionDetector { public void dump(PrintWriter pw) { pw.println("AttentionDetector:"); pw.println(" mIsSettingEnabled=" + mIsSettingEnabled); pw.println(" mMaximumExtensionMillis=" + mMaximumExtensionMillis); pw.println(" mMaxExtensionMillis=" + getMaxExtensionMillis()); pw.println(" preDimCheckDurationMillis=" + getPreDimCheckDurationMillis()); pw.println(" postDimCheckDurationMillis=" + mLastPostDimTimeout); pw.println(" mLastUserActivityTime(excludingAttention)=" + mLastUserActivityTime); Loading Loading @@ -348,6 +355,21 @@ public class AttentionDetector { return mLastPostDimTimeout; } /** How long the device can remain unlocked due to attention checking. */ @VisibleForTesting protected long getMaxExtensionMillis() { final long millis = DeviceConfig.getLong(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, mDefaultMaximumExtensionMillis); if (millis < 0 || millis > 60 * 60 * 1000) { // 1 hour Slog.w(TAG, "Bad flag value supplied for: " + KEY_MAX_EXTENSION_MILLIS); return mDefaultMaximumExtensionMillis; } return millis; } @VisibleForTesting final class AttentionCallbackInternalImpl extends AttentionCallbackInternal { private final int mId; Loading
services/tests/servicestests/src/com/android/server/power/AttentionDetectorTest.java +45 −1 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ import static android.provider.DeviceConfig.NAMESPACE_ATTENTION_MANAGER_SERVICE; import static com.android.server.power.AttentionDetector.DEFAULT_POST_DIM_CHECK_DURATION_MILLIS; import static com.android.server.power.AttentionDetector.DEFAULT_PRE_DIM_CHECK_DURATION_MILLIS; import static com.android.server.power.AttentionDetector.KEY_MAX_EXTENSION_MILLIS; import static com.android.server.power.AttentionDetector.KEY_POST_DIM_CHECK_DURATION_MILLIS; import static com.android.server.power.AttentionDetector.KEY_PRE_DIM_CHECK_DURATION_MILLIS; Loading Loading @@ -87,6 +88,7 @@ public class AttentionDetectorTest extends AndroidTestCase { when(mWindowManagerInternal.isKeyguardShowingAndNotOccluded()).thenReturn(false); mAttentionDetector = new TestableAttentionDetector(); mRealAttentionDetector = new AttentionDetector(mOnUserAttention, new Object()); mRealAttentionDetector.mDefaultMaximumExtensionMillis = 900_000L; mAttentionDetector.onWakefulnessChangeStarted(PowerManagerInternal.WAKEFULNESS_AWAKE); mAttentionDetector.setAttentionServiceSupported(true); mNextDimming = SystemClock.uptimeMillis() + 3000L; Loading @@ -98,6 +100,10 @@ public class AttentionDetectorTest extends AndroidTestCase { Settings.Secure.putIntForUser(getContext().getContentResolver(), Settings.Secure.ADAPTIVE_SLEEP, 1, UserHandle.USER_CURRENT); mAttentionDetector.updateEnabledFromSettings(getContext()); DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, Long.toString(10_000L), false); } @After Loading @@ -111,6 +117,9 @@ public class AttentionDetectorTest extends AndroidTestCase { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_POST_DIM_CHECK_DURATION_MILLIS, Long.toString(DEFAULT_POST_DIM_CHECK_DURATION_MILLIS), false); DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, Long.toString(mRealAttentionDetector.mDefaultMaximumExtensionMillis), false); } @Test Loading Loading @@ -393,6 +402,42 @@ public class AttentionDetectorTest extends AndroidTestCase { DEFAULT_POST_DIM_CHECK_DURATION_MILLIS); } @Test public void testGetMaxExtensionMillis_handlesGoodFlagValue() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, "123", false); assertThat(mRealAttentionDetector.getMaxExtensionMillis()).isEqualTo(123); } @Test public void testGetMaxExtensionMillis_rejectsNegativeValue() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, "-50", false); assertThat(mRealAttentionDetector.getMaxExtensionMillis()).isEqualTo( mRealAttentionDetector.mDefaultMaximumExtensionMillis); } @Test public void testGetMaxExtensionMillis_rejectsTooBigValue() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, "9900000", false); assertThat(mRealAttentionDetector.getMaxExtensionMillis()).isEqualTo( mRealAttentionDetector.mDefaultMaximumExtensionMillis); } @Test public void testGetMaxExtensionMillis_handlesBadFlagValue() { DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, "20000k", false); assertThat(mRealAttentionDetector.getMaxExtensionMillis()).isEqualTo( mRealAttentionDetector.mDefaultMaximumExtensionMillis); DeviceConfig.setProperty(NAMESPACE_ATTENTION_MANAGER_SERVICE, KEY_MAX_EXTENSION_MILLIS, "0.25", false); assertThat(mRealAttentionDetector.getMaxExtensionMillis()).isEqualTo( mRealAttentionDetector.mDefaultMaximumExtensionMillis); } private long registerAttention() { mPreDimCheckDuration = 4000L; mAttentionDetector.onUserActivity(SystemClock.uptimeMillis(), Loading @@ -409,7 +454,6 @@ public class AttentionDetectorTest extends AndroidTestCase { mWindowManager = mWindowManagerInternal; mPackageManager = AttentionDetectorTest.this.mPackageManager; mContentResolver = getContext().getContentResolver(); mMaximumExtensionMillis = 10000L; } void setAttentionServiceSupported(boolean supported) { Loading