[Reduce Bright Colors] Use a switch preference
Test: atest ReduceBrightColorsPreferenceControllerTest, manual
Bug: 128465252
Change-Id: I824af24e545d38bad74613147328cf51108a9127
diff --git a/res/xml/accessibility_settings.xml b/res/xml/accessibility_settings.xml
index bebb336..ac51a82 100644
--- a/res/xml/accessibility_settings.xml
+++ b/res/xml/accessibility_settings.xml
@@ -44,7 +44,7 @@
settings:searchable="true"/>
<!--TODO(b/170973645): Get icon-->
- <Preference
+ <com.android.settings.widget.PrimarySwitchPreference
android:fragment="com.android.settings.accessibility.ToggleReduceBrightColorsPreferenceFragment"
android:key="reduce_bright_colors_preference"
android:persistent="false"
diff --git a/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceController.java b/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceController.java
index bfe61b7..037969d 100644
--- a/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceController.java
+++ b/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceController.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2020 The Android Open Source Project
+ * 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.
@@ -17,17 +17,60 @@
package com.android.settings.accessibility;
import android.content.Context;
+import android.database.ContentObserver;
import android.hardware.display.ColorDisplayManager;
+import android.net.Uri;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.UserHandle;
+import android.provider.Settings;
+import android.text.TextUtils;
+
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
import com.android.settings.R;
-import com.android.settings.core.BasePreferenceController;
+import com.android.settings.core.TogglePreferenceController;
+import com.android.settings.widget.PrimarySwitchPreference;
+import com.android.settingslib.core.lifecycle.LifecycleObserver;
+import com.android.settingslib.core.lifecycle.events.OnStart;
+import com.android.settingslib.core.lifecycle.events.OnStop;
/** PreferenceController that shows the Reduce Bright Colors summary */
-public class ReduceBrightColorsPreferenceController extends BasePreferenceController {
+public class ReduceBrightColorsPreferenceController extends TogglePreferenceController
+ implements LifecycleObserver, OnStart, OnStop {
+ private ContentObserver mSettingsContentObserver;
+ private PrimarySwitchPreference mPreference;
+ private final Context mContext;
public ReduceBrightColorsPreferenceController(Context context,
String preferenceKey) {
super(context, preferenceKey);
+ mContext = context;
+ mSettingsContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())){
+ @Override
+ public void onChange(boolean selfChange, Uri uri) {
+ final String path = uri == null ? null : uri.getLastPathSegment();
+ if (TextUtils.equals(path, Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED)) {
+ updateState(mPreference);
+ }
+ }
+ };
+ }
+
+ @Override
+ public boolean isChecked() {
+ return Settings.Secure.getIntForUser(mContext.getContentResolver(),
+ Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED,
+ 0,
+ UserHandle.USER_CURRENT) == 1;
+ }
+
+ @Override
+ public boolean setChecked(boolean isChecked) {
+ return Settings.Secure.putIntForUser(mContext.getContentResolver(),
+ Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, isChecked ? 1 : 0,
+ UserHandle.USER_CURRENT);
}
@Override
@@ -37,8 +80,31 @@
}
@Override
+ public void updateState(Preference preference) {
+ super.updateState(preference);
+ refreshSummary(preference);
+ }
+
+ @Override
public int getAvailabilityStatus() {
return ColorDisplayManager.isColorTransformAccelerated(mContext) ? AVAILABLE
: UNSUPPORTED_ON_DEVICE;
}
+
+ @Override
+ public void displayPreference(PreferenceScreen screen) {
+ super.displayPreference(screen);
+ mPreference = screen.findPreference(getPreferenceKey());
+ }
+
+ @Override
+ public void onStart() {
+ mContext.getContentResolver().registerContentObserver(Settings.Secure.getUriFor(
+ Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED),
+ false, mSettingsContentObserver, UserHandle.USER_CURRENT);
+ }
+ @Override
+ public void onStop() {
+ mContext.getContentResolver().unregisterContentObserver(mSettingsContentObserver);
+ }
}
diff --git a/tests/robotests/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java
index 01942f0..325e6a8 100644
--- a/tests/robotests/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/accessibility/ReduceBrightColorsPreferenceControllerTest.java
@@ -19,6 +19,7 @@
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;
@@ -31,7 +32,6 @@
@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);
@@ -41,4 +41,18 @@
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();
+ }
}