Merge "Call ColorDisplayManager for Extra Dim/RBC state" into sc-dev
diff --git a/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceController.java b/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceController.java
index ddf00e5..d886a59 100644
--- a/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceController.java
+++ b/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceController.java
@@ -42,6 +42,7 @@
     private ContentObserver mSettingsContentObserver;
     private PrimarySwitchPreference mPreference;
     private final Context mContext;
+    private final ColorDisplayManager mColorDisplayManager;
 
     public ReduceBrightColorsPreferenceController(Context context,
             String preferenceKey) {
@@ -56,21 +57,17 @@
                 }
             }
         };
+        mColorDisplayManager = mContext.getSystemService(ColorDisplayManager.class);
     }
 
     @Override
     public boolean isChecked() {
-        return Settings.Secure.getIntForUser(mContext.getContentResolver(),
-                Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED,
-                0,
-                UserHandle.USER_CURRENT) == 1;
+        return mColorDisplayManager.isReduceBrightColorsActivated();
     }
 
     @Override
     public boolean setChecked(boolean isChecked) {
-        return Settings.Secure.putIntForUser(mContext.getContentResolver(),
-                Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, isChecked ? 1 : 0,
-                UserHandle.USER_CURRENT);
+        return mColorDisplayManager.setReduceBrightColorsActivated(isChecked);
     }
 
     @Override
diff --git a/src/com/android/settings/accessibility/ToggleReduceBrightColorsPreferenceFragment.java b/src/com/android/settings/accessibility/ToggleReduceBrightColorsPreferenceFragment.java
index 4bc086e..b629eaa 100644
--- a/src/com/android/settings/accessibility/ToggleReduceBrightColorsPreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleReduceBrightColorsPreferenceFragment.java
@@ -16,9 +16,6 @@
 
 package com.android.settings.accessibility;
 
-import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
-import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
-
 import android.app.settings.SettingsEnums;
 import android.content.ContentResolver;
 import android.content.Context;
@@ -57,6 +54,7 @@
     private SettingsContentObserver mSettingsContentObserver;
     private ReduceBrightColorsIntensityPreferenceController mRbcIntensityPreferenceController;
     private ReduceBrightColorsPersistencePreferenceController mRbcPersistencePreferenceController;
+    private ColorDisplayManager mColorDisplayManager;
 
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -83,7 +81,7 @@
                 updateSwitchBarToggleSwitch();
             }
         };
-
+        mColorDisplayManager = getContext().getSystemService(ColorDisplayManager.class);
         final View view = super.onCreateView(inflater, container, savedInstanceState);
         // Parent sets the title when creating the view, so set it after calling super
         mToggleServiceSwitchPreference.setTitle(R.string.reduce_bright_colors_switch_title);
@@ -141,8 +139,7 @@
     @Override
     protected void onPreferenceToggled(String preferenceKey, boolean enabled) {
         AccessibilityStatsLogUtils.logAccessibilityServiceEnabled(mComponentName, enabled);
-        Settings.Secure.putInt(getContentResolver(),
-                REDUCE_BRIGHT_COLORS_ACTIVATED_KEY, enabled ? ON : OFF);
+        mColorDisplayManager.setReduceBrightColorsActivated(enabled);
     }
 
     @Override
@@ -165,8 +162,7 @@
 
     @Override
     protected void updateSwitchBarToggleSwitch() {
-        final boolean checked = Settings.Secure.getInt(getContentResolver(),
-                REDUCE_BRIGHT_COLORS_ACTIVATED_KEY, OFF) == ON;
+        final boolean checked = mColorDisplayManager.isReduceBrightColorsActivated();
         mRbcIntensityPreferenceController.updateState(getPreferenceScreen()
                 .findPreference(KEY_INTENSITY));
         mRbcPersistencePreferenceController.updateState(getPreferenceScreen()
diff --git a/tests/robotests/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java
deleted file mode 100644
index 325e6a8..0000000
--- a/tests/robotests/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (C) 2020 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.settings.accessibility;
-
-import static com.google.common.truth.Truth.assertThat;
-
-import android.content.Context;
-import android.provider.Settings;
-
-import androidx.test.core.app.ApplicationProvider;
-import androidx.test.ext.junit.runners.AndroidJUnit4;
-
-import com.android.settings.R;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-@RunWith(AndroidJUnit4.class)
-public class ReduceBrightColorsPreferenceControllerTest {
-    private static final String PREF_KEY = "rbc_preference";
-    private final Context mContext = ApplicationProvider.getApplicationContext();
-    private final ReduceBrightColorsPreferenceController mController =
-            new ReduceBrightColorsPreferenceController(mContext, PREF_KEY);
-
-    @Test
-    public void getSummary_returnSummary() {
-        assertThat(mController.getSummary().toString().contains(
-                mContext.getText(R.string.reduce_bright_colors_preference_summary))).isTrue();
-    }
-
-    @Test
-    public void isChecked_reduceBrightColorsIsActivated_returnTrue() {
-        Settings.Secure.putInt(mContext.getContentResolver(),
-                Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 1);
-        assertThat(mController.isChecked()).isTrue();
-    }
-
-    @Test
-    public void isChecked_reduceBrightColorsIsNotActivated_returnFalse() {
-        Settings.Secure.putInt(mContext.getContentResolver(),
-                Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 0);
-        assertThat(mController.isChecked()).isFalse();
-    }
-}
diff --git a/tests/unit/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java b/tests/unit/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java
new file mode 100644
index 0000000..e1c0277
--- /dev/null
+++ b/tests/unit/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.accessibility;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.provider.Settings;
+
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import com.android.internal.R;
+
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(AndroidJUnit4.class)
+public class ReduceBrightColorsPreferenceControllerTest {
+    private static final String PREF_KEY = "rbc_preference";
+
+    private Context mContext;
+    private Resources mResources;;
+    private ReduceBrightColorsPreferenceController mController;
+
+    @Before
+    public void setUp() {
+        mContext = spy(ApplicationProvider.getApplicationContext());
+        mResources = spy(mContext.getResources());
+        when(mContext.getResources()).thenReturn(mResources);
+        mController = new ReduceBrightColorsPreferenceController(mContext,
+                PREF_KEY);
+    }
+
+    @Test
+    public void getSummary_returnSummary() {
+        assertThat(mController.getSummary().toString().contains(
+                resourceString("reduce_bright_colors_preference_summary"))).isTrue();
+    }
+
+    @Ignore("ColorDisplayManager runs in a different thread which results in a race condition")
+    @Test
+    public void isChecked_reduceBrightColorsIsActivated_returnTrue() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 1);
+        assertThat(mController.isChecked()).isTrue();
+    }
+
+    @Ignore("ColorDisplayManager runs in a different thread which results in a race condition")
+    @Test
+    public void isChecked_reduceBrightColorsIsNotActivated_returnFalse() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 0);
+        assertThat(mController.isChecked()).isFalse();
+    }
+
+    @Test
+    public void isAvailable_configuredRbcAvailable_shouldReturnTrue() {
+        doReturn(true).when(mResources).getBoolean(
+                R.bool.config_reduceBrightColorsAvailable);
+        assertThat(mController.isAvailable()).isTrue();
+    }
+    @Test
+    public void isAvailable_configuredRbcUnAvailable_shouldReturnFalse() {
+        doReturn(false).when(mResources).getBoolean(
+                R.bool.config_reduceBrightColorsAvailable);
+        assertThat(mController.isAvailable()).isFalse();
+    }
+
+    private int resourceId(String type, String name) {
+        return mContext.getResources().getIdentifier(name, type, mContext.getPackageName());
+    }
+
+    private String resourceString(String name) {
+        return mContext.getResources().getString(resourceId("string", name));
+    }
+}