Merge "Cancel enrollment when back or negative buttons pressed" into sc-dev
diff --git a/src/com/android/settings/display/AdaptiveSleepPermissionPreferenceController.java b/src/com/android/settings/display/AdaptiveSleepPermissionPreferenceController.java
index 8e4db0d..0d21e9c 100644
--- a/src/com/android/settings/display/AdaptiveSleepPermissionPreferenceController.java
+++ b/src/com/android/settings/display/AdaptiveSleepPermissionPreferenceController.java
@@ -37,18 +37,26 @@
     @VisibleForTesting
     BannerMessagePreference mPreference;
     private final PackageManager mPackageManager;
-    private final Context mContext;
 
     public AdaptiveSleepPermissionPreferenceController(Context context) {
+        final String packageName = context.getPackageManager().getAttentionServicePackageName();
         mPackageManager = context.getPackageManager();
-        mContext = context;
+        final Intent intent = new Intent(
+                android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
+        intent.setData(Uri.parse("package:" + packageName));
+        mPreference = new BannerMessagePreference(context);
+        mPreference.setTitle(R.string.adaptive_sleep_title_no_permission);
+        mPreference.setSummary(R.string.adaptive_sleep_summary_no_permission);
+        mPreference.setPositiveButtonText(R.string.adaptive_sleep_manage_permission_button);
+        mPreference.setPositiveButtonOnClickListener(p -> {
+            context.startActivity(intent);
+        });
     }
 
     /**
      * Adds the controlled preference to the provided preference screen.
      */
     public void addToScreen(PreferenceScreen screen) {
-        initializePreference();
         if (!hasSufficientPermission(mPackageManager)) {
             screen.addPreference(mPreference);
         }
@@ -60,19 +68,4 @@
     public void updateVisibility() {
         mPreference.setVisible(!hasSufficientPermission(mPackageManager));
     }
-
-    private void initializePreference() {
-        final String packageName = mContext.getPackageManager().getAttentionServicePackageName();
-        final Intent intent = new Intent(
-                android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
-        intent.setData(Uri.parse("package:" + packageName));
-        mPreference = new BannerMessagePreference(mContext);
-        mPreference.setTitle(R.string.adaptive_sleep_title_no_permission);
-        mPreference.setSummary(R.string.adaptive_sleep_summary_no_permission);
-        mPreference.setPositiveButtonText(R.string.adaptive_sleep_manage_permission_button);
-        mPreference.setPositiveButtonOnClickListener(p -> {
-            mContext.startActivity(intent);
-        });
-    }
-
 }
diff --git a/src/com/android/settings/display/AdaptiveSleepPreferenceController.java b/src/com/android/settings/display/AdaptiveSleepPreferenceController.java
index aa02ce5..70d8a79 100644
--- a/src/com/android/settings/display/AdaptiveSleepPreferenceController.java
+++ b/src/com/android/settings/display/AdaptiveSleepPreferenceController.java
@@ -49,10 +49,10 @@
     public static final String PREFERENCE_KEY = "adaptive_sleep";
     private static final int DEFAULT_VALUE = 0;
     private final SensorPrivacyManager mPrivacyManager;
-    private final RestrictionUtils mRestrictionUtils;
-    private final PackageManager mPackageManager;
-    private final Context mContext;
-    private final MetricsFeatureProvider mMetricsFeatureProvider;
+    private RestrictionUtils mRestrictionUtils;
+    private PackageManager mPackageManager;
+    private Context mContext;
+    private MetricsFeatureProvider mMetricsFeatureProvider;
 
     @VisibleForTesting
     RestrictedSwitchPreference mPreference;
@@ -62,6 +62,19 @@
         mRestrictionUtils = restrictionUtils;
         mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
         mPrivacyManager = SensorPrivacyManager.getInstance(context);
+        mPreference = new RestrictedSwitchPreference(context);
+        mPreference.setTitle(R.string.adaptive_sleep_title);
+        mPreference.setSummary(R.string.adaptive_sleep_description);
+        mPreference.setChecked(isChecked());
+        mPreference.setKey(PREFERENCE_KEY);
+        mPreference.setOnPreferenceClickListener(preference -> {
+            final boolean isChecked = ((RestrictedSwitchPreference) preference).isChecked();
+            mMetricsFeatureProvider.action(context, SettingsEnums.ACTION_SCREEN_ATTENTION_CHANGED,
+                    isChecked);
+            Settings.Secure.putInt(context.getContentResolver(),
+                    Settings.Secure.ADAPTIVE_SLEEP, isChecked ? 1 : DEFAULT_VALUE);
+            return true;
+        });
         mPackageManager = context.getPackageManager();
     }
 
@@ -73,7 +86,6 @@
      * Adds the controlled preference to the provided preference screen.
      */
     public void addToScreen(PreferenceScreen screen) {
-        initializePreference();
         updatePreference();
         screen.addPreference(mPreference);
     }
@@ -92,23 +104,6 @@
     }
 
     @VisibleForTesting
-    void initializePreference() {
-        mPreference = new RestrictedSwitchPreference(mContext);
-        mPreference.setTitle(R.string.adaptive_sleep_title);
-        mPreference.setSummary(R.string.adaptive_sleep_description);
-        mPreference.setChecked(isChecked());
-        mPreference.setKey(PREFERENCE_KEY);
-        mPreference.setOnPreferenceChangeListener((preference, value) -> {
-            final boolean isChecked = (Boolean) value;
-            mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_SCREEN_ATTENTION_CHANGED,
-                    isChecked);
-            Settings.Secure.putInt(mContext.getContentResolver(),
-                    Settings.Secure.ADAPTIVE_SLEEP, isChecked ? 1 : DEFAULT_VALUE);
-            return true;
-        });
-    }
-
-    @VisibleForTesting
     boolean isChecked() {
         return hasSufficientPermission(mContext.getPackageManager()) && !isCameraLocked()
                 && Settings.Secure.getInt(mContext.getContentResolver(),
diff --git a/src/com/android/settings/display/ScreenTimeoutSettings.java b/src/com/android/settings/display/ScreenTimeoutSettings.java
index 6dfb225..27e1e1b 100644
--- a/src/com/android/settings/display/ScreenTimeoutSettings.java
+++ b/src/com/android/settings/display/ScreenTimeoutSettings.java
@@ -71,15 +71,11 @@
     private CharSequence[] mInitialEntries;
     private CharSequence[] mInitialValues;
     private FooterPreference mPrivacyPreference;
-    private final MetricsFeatureProvider mMetricsFeatureProvider;
+    private MetricsFeatureProvider mMetricsFeatureProvider;
     private SensorPrivacyManager mPrivacyManager;
 
     @VisibleForTesting
-    Context mContext;
-
-    @VisibleForTesting
     RestrictedLockUtils.EnforcedAdmin mAdmin;
-
     @VisibleForTesting
     Preference mDisableOptionsPreference;
 
@@ -101,7 +97,6 @@
     @Override
     public void onAttach(Context context) {
         super.onAttach(context);
-        mContext = context;
         mInitialEntries = getResources().getStringArray(R.array.screen_timeout_entries);
         mInitialValues = getResources().getStringArray(R.array.screen_timeout_values);
         mAdaptiveSleepController = new AdaptiveSleepPreferenceController(context);
@@ -109,6 +104,11 @@
                 context);
         mAdaptiveSleepCameraStatePreferenceController =
                 new AdaptiveSleepCameraStatePreferenceController(context);
+        mPrivacyPreference = new FooterPreference(context);
+        mPrivacyPreference.setIcon(R.drawable.ic_privacy_shield_24dp);
+        mPrivacyPreference.setTitle(R.string.adaptive_sleep_privacy);
+        mPrivacyPreference.setSelectable(false);
+        mPrivacyPreference.setLayoutResource(R.layout.preference_footer);
         mPrivacyManager = SensorPrivacyManager.getInstance(context);
         mPrivacyManager.addSensorPrivacyListener(CAMERA,
                 (sensor, enabled) -> mAdaptiveSleepController.updatePreference());
@@ -167,12 +167,6 @@
             preferenceWithLargestTimeout.setChecked(true);
         }
 
-        mPrivacyPreference = new FooterPreference(mContext);
-        mPrivacyPreference.setIcon(R.drawable.ic_privacy_shield_24dp);
-        mPrivacyPreference.setTitle(R.string.adaptive_sleep_privacy);
-        mPrivacyPreference.setSelectable(false);
-        mPrivacyPreference.setLayoutResource(R.layout.preference_footer);
-
         if (isScreenAttentionAvailable(getContext())) {
             mAdaptiveSleepPermissionController.addToScreen(screen);
             mAdaptiveSleepCameraStatePreferenceController.addToScreen(screen);
diff --git a/tests/robotests/src/com/android/settings/display/AdaptiveSleepPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/AdaptiveSleepPreferenceControllerTest.java
index 880f9cc..843870d 100644
--- a/tests/robotests/src/com/android/settings/display/AdaptiveSleepPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/display/AdaptiveSleepPreferenceControllerTest.java
@@ -84,7 +84,6 @@
                 eq(UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT))).thenReturn(null);
 
         mController = new AdaptiveSleepPreferenceController(mContext, mRestrictionUtils);
-        mController.initializePreference();
         when(mController.isCameraLocked()).thenReturn(false);
     }
 
diff --git a/tests/robotests/src/com/android/settings/display/ScreenTimeoutSettingsTest.java b/tests/robotests/src/com/android/settings/display/ScreenTimeoutSettingsTest.java
index 24bcde8..c4f5580 100644
--- a/tests/robotests/src/com/android/settings/display/ScreenTimeoutSettingsTest.java
+++ b/tests/robotests/src/com/android/settings/display/ScreenTimeoutSettingsTest.java
@@ -60,6 +60,8 @@
     private ScreenTimeoutSettings mSettings;
     private Context mContext;
     private ContentResolver mContentResolver;
+
+    @Mock
     private Resources mResources;
 
     @Mock
@@ -83,9 +85,7 @@
         FakeFeatureFactory.setupForTest();
         mContext = spy(getApplicationContext());
         mSettings = spy(new ScreenTimeoutSettings());
-        mSettings.mContext = mContext;
         mContentResolver = mContext.getContentResolver();
-        mResources = spy(mContext.getResources());
 
         doReturn(TIMEOUT_ENTRIES).when(mResources).getStringArray(R.array.screen_timeout_entries);
         doReturn(TIMEOUT_VALUES).when(mResources).getStringArray(R.array.screen_timeout_entries);