[Accessibility] Add contrast control screen UI and setup entry point

Bug: 241805782
Test: atest, local raven device
Screenshot: attached on bug
Flag: aconfig
com.android.settings.accessibility.enable_color_contrast_control

Change-Id: I7fcc01b822b460c6585c41a5831575ba0ffbcc93
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index 63ce331..92e3efd 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -117,6 +117,8 @@
     public static class TextReadingSettingsActivity extends SettingsActivity { /* empty */ }
     /** Activity for text color and motion settings. */
     public static class ColorAndMotionActivity extends SettingsActivity { /* empty */ }
+    /** Activity for color contrast settings. */
+    public static class ColorContrastActivity extends SettingsActivity { /* empty */ }
     /** Activity for the security dashboard. */
     public static class SecurityDashboardActivity extends SettingsActivity {
 
diff --git a/src/com/android/settings/accessibility/ColorContrastFragment.java b/src/com/android/settings/accessibility/ColorContrastFragment.java
new file mode 100644
index 0000000..a572657
--- /dev/null
+++ b/src/com/android/settings/accessibility/ColorContrastFragment.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2024 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 com.android.settings.R;
+import com.android.settings.dashboard.DashboardFragment;
+import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settingslib.search.SearchIndexable;
+
+/** Accessibility settings for color contrast. */
+@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
+public class ColorContrastFragment extends DashboardFragment {
+
+    private static final String TAG = "ColorContrastFragment";
+
+
+    @Override
+    protected int getPreferenceScreenResId() {
+        return R.xml.accessibility_color_contrast;
+    }
+
+    @Override
+    protected String getLogTag() {
+        return TAG;
+    }
+
+    @Override
+    public int getMetricsCategory() {
+        // TODO(b/326539398): Add metrics tracking for color contrast.
+        return 0;
+    }
+
+    public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+            new BaseSearchIndexProvider(R.xml.accessibility_color_contrast);
+}
diff --git a/src/com/android/settings/accessibility/ContrastPreferenceController.java b/src/com/android/settings/accessibility/ContrastPreferenceController.java
new file mode 100644
index 0000000..4e9a9ec
--- /dev/null
+++ b/src/com/android/settings/accessibility/ContrastPreferenceController.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2024 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 android.content.Context;
+
+import androidx.annotation.NonNull;
+
+import com.android.settings.core.BasePreferenceController;
+
+/**
+ * Controller for {@link ColorContrastFragment}.
+ */
+public class ContrastPreferenceController extends BasePreferenceController {
+
+    public ContrastPreferenceController(@NonNull Context context, @NonNull String preferenceKey) {
+        super(context, preferenceKey);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return Flags.enableColorContrastControl() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
+    }
+}
diff --git a/src/com/android/settings/accessibility/ContrastSelectorPreferenceController.java b/src/com/android/settings/accessibility/ContrastSelectorPreferenceController.java
new file mode 100644
index 0000000..b99680f
--- /dev/null
+++ b/src/com/android/settings/accessibility/ContrastSelectorPreferenceController.java
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2024 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 android.app.UiModeManager.ContrastUtils.CONTRAST_LEVEL_HIGH;
+import static android.app.UiModeManager.ContrastUtils.CONTRAST_LEVEL_MEDIUM;
+import static android.app.UiModeManager.ContrastUtils.CONTRAST_LEVEL_STANDARD;
+import static android.app.UiModeManager.ContrastUtils.fromContrastLevel;
+import static android.app.UiModeManager.ContrastUtils.toContrastLevel;
+
+import android.app.UiModeManager;
+import android.content.Context;
+import android.provider.Settings;
+import android.view.View;
+import android.widget.FrameLayout;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+import com.android.settingslib.core.lifecycle.LifecycleObserver;
+import com.android.settingslib.core.lifecycle.events.OnStart;
+import com.android.settingslib.core.lifecycle.events.OnStop;
+import com.android.settingslib.widget.LayoutPreference;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.Executor;
+
+/**
+ * Controller for contrast selector.
+ */
+public class ContrastSelectorPreferenceController extends BasePreferenceController
+        implements LifecycleObserver, OnStart, OnStop, UiModeManager.ContrastChangeListener {
+
+    private static final String KEY_COLOR_CONTRAST_SELECTOR = "color_contrast_selector";
+
+    private final Executor mMainExecutor;
+    private final UiModeManager mUiModeManager;
+    private Map<Integer, FrameLayout> mContrastButtons = new HashMap<>();
+
+    public ContrastSelectorPreferenceController(@NonNull Context context,
+            @NonNull String preferenceKey) {
+        super(context, preferenceKey);
+
+        mMainExecutor = mContext.getMainExecutor();
+        mUiModeManager = mContext.getSystemService(UiModeManager.class);
+    }
+
+    @Override
+    public void displayPreference(@NonNull PreferenceScreen screen) {
+        super.displayPreference(screen);
+
+        final LayoutPreference mLayoutPreference =
+                screen.findPreference(KEY_COLOR_CONTRAST_SELECTOR);
+
+        mContrastButtons = Map.ofEntries(
+                Map.entry(CONTRAST_LEVEL_STANDARD,
+                        mLayoutPreference.findViewById(R.id.contrast_button_default)),
+                Map.entry(CONTRAST_LEVEL_MEDIUM,
+                        mLayoutPreference.findViewById(R.id.contrast_button_medium)),
+                Map.entry(CONTRAST_LEVEL_HIGH,
+                        mLayoutPreference.findViewById(R.id.contrast_button_high))
+        );
+
+        mContrastButtons.forEach((contrastLevel, contrastButton) -> {
+            contrastButton.setOnClickListener(new View.OnClickListener() {
+                @Override
+                public void onClick(@Nullable View v) {
+                    Settings.Secure.putFloat(mContext.getContentResolver(),
+                            Settings.Secure.CONTRAST_LEVEL,
+                            fromContrastLevel(contrastLevel));
+                }
+            });
+        });
+
+        highlightContrast(toContrastLevel(mUiModeManager.getContrast()));
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        // The main preferences screen is feature guarded, so this always returns AVAILABLE.
+        return AVAILABLE;
+    }
+
+    @Override
+    public void onStart() {
+        mUiModeManager.addContrastChangeListener(mMainExecutor, this);
+    }
+
+    @Override
+    public void onStop() {
+        mUiModeManager.removeContrastChangeListener(this);
+    }
+
+    @Override
+    public void onContrastChanged(float contrast) {
+        highlightContrast(toContrastLevel(contrast));
+    }
+
+    private void highlightContrast(int contrast) {
+        mContrastButtons.forEach((level, button) -> {
+            button.setSelected(level == contrast);
+        });
+    }
+}
diff --git a/src/com/android/settings/core/gateway/SettingsGateway.java b/src/com/android/settings/core/gateway/SettingsGateway.java
index e3131f7..1b6c231 100644
--- a/src/com/android/settings/core/gateway/SettingsGateway.java
+++ b/src/com/android/settings/core/gateway/SettingsGateway.java
@@ -29,6 +29,7 @@
 import com.android.settings.accessibility.AccessibilitySettingsForSetupWizard;
 import com.android.settings.accessibility.CaptioningPropertiesFragment;
 import com.android.settings.accessibility.ColorAndMotionFragment;
+import com.android.settings.accessibility.ColorContrastFragment;
 import com.android.settings.accessibility.TextReadingPreferenceFragment;
 import com.android.settings.accessibility.TextReadingPreferenceFragmentForSetupWizard;
 import com.android.settings.accessibility.ToggleColorInversionPreferenceFragment;
@@ -381,6 +382,7 @@
             TurnScreenOnDetails.class.getName(),
             NfcAndPaymentFragment.class.getName(),
             ColorAndMotionFragment.class.getName(),
+            ColorContrastFragment.class.getName(),
             LongBackgroundTasksDetails.class.getName(),
             RegionalPreferencesEntriesFragment.class.getName(),
             BatteryInfoFragment.class.getName(),