Disable screen attention when DISALLOW_CONFIG_SCREEN_TIMEOUT

Test: atest AdaptiveSleepDetailPreferenceControllerTest
Bug: 161938926
Change-Id: I4ed96ad36efe67091a26cbfe2cde16c116f7c8fb
diff --git a/src/com/android/settings/display/AdaptiveSleepDetailPreferenceController.java b/src/com/android/settings/display/AdaptiveSleepDetailPreferenceController.java
index d6eabec..d0fc5d7 100644
--- a/src/com/android/settings/display/AdaptiveSleepDetailPreferenceController.java
+++ b/src/com/android/settings/display/AdaptiveSleepDetailPreferenceController.java
@@ -17,13 +17,25 @@
 package com.android.settings.display;
 
 import android.content.Context;
+import android.os.UserManager;
 
 import androidx.preference.Preference;
 
+import com.android.settings.bluetooth.RestrictionUtils;
+import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
+import com.android.settingslib.RestrictedSwitchPreference;
+
 public class AdaptiveSleepDetailPreferenceController extends AdaptiveSleepPreferenceController {
+    private RestrictionUtils mRestrictionUtils;
+
+    public AdaptiveSleepDetailPreferenceController(Context context, String key,
+            RestrictionUtils restrictionUtils) {
+        super(context, key);
+        mRestrictionUtils = restrictionUtils;
+    }
 
     public AdaptiveSleepDetailPreferenceController(Context context, String key) {
-        super(context, key);
+        this(context, key, new RestrictionUtils());
     }
 
     @Override
@@ -43,6 +55,12 @@
     @Override
     public void updateState(Preference preference) {
         super.updateState(preference);
-        preference.setEnabled(hasSufficientPermission(mContext.getPackageManager()));
+        final EnforcedAdmin enforcedAdmin = mRestrictionUtils.checkIfRestrictionEnforced(mContext,
+                UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT);
+        if (enforcedAdmin != null) {
+            ((RestrictedSwitchPreference) preference).setDisabledByAdmin(enforcedAdmin);
+        } else {
+            preference.setEnabled(hasSufficientPermission(mContext.getPackageManager()));
+        }
     }
 }
\ No newline at end of file
diff --git a/tests/robotests/src/com/android/settings/display/AdaptiveSleepDetailPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/AdaptiveSleepDetailPreferenceControllerTest.java
index 981942d..fe0e812 100644
--- a/tests/robotests/src/com/android/settings/display/AdaptiveSleepDetailPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/display/AdaptiveSleepDetailPreferenceControllerTest.java
@@ -21,15 +21,26 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.pm.PackageManager;
+import android.os.UserHandle;
+import android.os.UserManager;
 
 import com.android.internal.R;
+import com.android.settings.bluetooth.RestrictionUtils;
 import com.android.settings.testutils.shadow.SettingsShadowResources;
+import com.android.settingslib.RestrictedLockUtils;
+import com.android.settingslib.RestrictedSwitchPreference;
 
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
@@ -42,17 +53,32 @@
 @RunWith(RobolectricTestRunner.class)
 @Config(shadows = {SettingsShadowResources.class})
 public class AdaptiveSleepDetailPreferenceControllerTest {
+    private static RestrictedLockUtils.EnforcedAdmin sFakeEnforcedAdmin;
+
+    @BeforeClass
+    public static void beforeClass() {
+        sFakeEnforcedAdmin = new RestrictedLockUtils.EnforcedAdmin(
+                new ComponentName("test.package", "test.Class"),
+                UserHandle.of(10));
+    }
 
     private AdaptiveSleepDetailPreferenceController mController;
     @Mock
     private PackageManager mPackageManager;
+    @Mock
+    private RestrictionUtils mRestrictionUtils;
+    @Mock
+    private RestrictedSwitchPreference mPreference;
+
+    private Context mContext;
 
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
-        Context context = Mockito.spy(RuntimeEnvironment.application);
-        doReturn(mPackageManager).when(context).getPackageManager();
-        mController = new AdaptiveSleepDetailPreferenceController(context, "test_key");
+        mContext = Mockito.spy(RuntimeEnvironment.application);
+        doReturn(mPackageManager).when(mContext).getPackageManager();
+        mController = new AdaptiveSleepDetailPreferenceController(mContext, "test_key",
+                mRestrictionUtils);
     }
 
     @Test
@@ -77,4 +103,25 @@
         SettingsShadowResources.overrideResource(R.bool.config_adaptive_sleep_available, false);
         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
     }
+
+    @Test
+    public void updateState_noRestriction_allowScreenAttentionSet() {
+        when(mRestrictionUtils.checkIfRestrictionEnforced(mContext,
+                UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT)).thenReturn(null);
+
+        mController.updateState(mPreference);
+
+        verify(mPreference, never()).setDisabledByAdmin(
+                any(RestrictedLockUtils.EnforcedAdmin.class));
+    }
+
+    @Test
+    public void updateState_enforceRestrictions_disallowScreenAttentionSet() {
+        when(mRestrictionUtils.checkIfRestrictionEnforced(mContext,
+                UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT)).thenReturn(sFakeEnforcedAdmin);
+
+        mController.updateState(mPreference);
+
+        verify(mPreference).setDisabledByAdmin(sFakeEnforcedAdmin);
+    }
 }