Flag the falsing threshold for attention

Bug: 157102507
Test: atest AttentionDetectorTest
Change-Id: I703a0edeb0964dd6dcf1aca04f7c9d37522f753c
diff --git a/services/core/java/com/android/server/power/AttentionDetector.java b/services/core/java/com/android/server/power/AttentionDetector.java
index b69c450..5ebeb4a 100644
--- a/services/core/java/com/android/server/power/AttentionDetector.java
+++ b/services/core/java/com/android/server/power/AttentionDetector.java
@@ -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;
@@ -75,6 +76,12 @@
     /** 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;
@@ -85,11 +92,11 @@
     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;
 
@@ -162,7 +169,7 @@
         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 {
@@ -196,7 +203,7 @@
 
         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));
@@ -309,7 +316,7 @@
     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);
@@ -348,6 +355,21 @@
         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;
diff --git a/services/tests/servicestests/src/com/android/server/power/AttentionDetectorTest.java b/services/tests/servicestests/src/com/android/server/power/AttentionDetectorTest.java
index e7e8aca..4381bfd 100644
--- a/services/tests/servicestests/src/com/android/server/power/AttentionDetectorTest.java
+++ b/services/tests/servicestests/src/com/android/server/power/AttentionDetectorTest.java
@@ -21,6 +21,7 @@
 
 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;
 
@@ -87,6 +88,7 @@
         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;
@@ -98,6 +100,10 @@
         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
@@ -111,6 +117,9 @@
         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
@@ -393,6 +402,42 @@
                 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(),
@@ -409,7 +454,6 @@
             mWindowManager = mWindowManagerInternal;
             mPackageManager = AttentionDetectorTest.this.mPackageManager;
             mContentResolver = getContext().getContentResolver();
-            mMaximumExtensionMillis = 10000L;
         }
 
         void setAttentionServiceSupported(boolean supported) {