Merge "Create EvenDimmerPreferenceController" into main
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 73a3b55..fc0ee78 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -2836,6 +2836,10 @@
     <string name="dark_ui_bedtime_footer_summary">Dark theme is currently following your Bedtime mode schedule</string>
     <!-- Dark UI screen footer action text shown when the when Dark theme turns on/off automatically according to a user bedtime schedule. [CHAR LIMIT=NONE] -->
     <string name="dark_ui_bedtime_footer_action">Bedtime mode settings</string>
+    <!-- Even Dimmer setting title. Allows device to reduce brightness even further than standard range. [CHAR LIMIT=NONE] -->
+    <string name="even_dimmer_display_title">Even Dimmer</string>
+    <!-- Even Dimmer setting summary. [CHAR LIMIT=NONE] -->
+    <string name="even_dimmer_display_summary">Allow device to go dimmer than usual</string>
 
 
     <!-- Sound & display settings screen, setting option name to change screen timeout -->
diff --git a/res/xml/display_settings.xml b/res/xml/display_settings.xml
index 2df360d..5b4bee8 100644
--- a/res/xml/display_settings.xml
+++ b/res/xml/display_settings.xml
@@ -36,6 +36,11 @@
             android:title="@string/auto_brightness_title"
             android:fragment="com.android.settings.display.AutoBrightnessSettings"
             settings:controller="com.android.settings.display.AutoBrightnessPreferenceController"/>
+        <SwitchPreferenceCompat
+            android:key="even_dimmer_activated"
+            android:title="@string/even_dimmer_display_title"
+            android:summary="@string/even_dimmer_display_summary"
+            settings:controller="com.android.settings.display.EvenDimmerPreferenceController"/>
     </PreferenceCategory>
 
     <PreferenceCategory
diff --git a/src/com/android/settings/display/EvenDimmerPreferenceController.java b/src/com/android/settings/display/EvenDimmerPreferenceController.java
new file mode 100644
index 0000000..b86c845
--- /dev/null
+++ b/src/com/android/settings/display/EvenDimmerPreferenceController.java
@@ -0,0 +1,76 @@
+/*
+ * 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.display;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.provider.Settings;
+import android.util.Log;
+
+import androidx.annotation.NonNull;
+
+import com.android.server.display.feature.flags.Flags;
+import com.android.settings.R;
+import com.android.settings.core.TogglePreferenceController;
+
+/**
+ * Controller for the settings toggle which allows screen brightness to go even dimmer than usual.
+ *
+ */
+public class EvenDimmerPreferenceController extends TogglePreferenceController {
+
+    private static final String TAG = "EvenDimmerPreferenceController";
+
+    private final Resources mResources;
+
+    public EvenDimmerPreferenceController(@NonNull Context context, @NonNull String key) {
+        super(context, key);
+        mResources = context.getResources();
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        // enable based on flag and config.xml
+        final boolean enabledInConfig = mResources.getBoolean(
+                com.android.internal.R.bool.config_evenDimmerEnabled);
+        return (Flags.evenDimmer() && enabledInConfig) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
+    }
+
+    @Override
+    public boolean isChecked() {
+        return getEvenDimmerActivated();
+    }
+
+    @Override
+    public boolean setChecked(boolean isChecked) {
+        final float enabled = getAvailabilityStatus() == AVAILABLE && isChecked ? 1 : 0;
+        Log.i(TAG, "setChecked to : " + enabled);
+
+        return Settings.Secure.putFloat(
+                mContext.getContentResolver(), Settings.Secure.EVEN_DIMMER_ACTIVATED, enabled);
+    }
+
+    @Override
+    public int getSliceHighlightMenuRes() {
+        return R.string.menu_key_display;
+    }
+
+    private boolean getEvenDimmerActivated() {
+        return Settings.Secure.getFloat(mContext.getContentResolver(),
+                Settings.Secure.EVEN_DIMMER_ACTIVATED, 0) == 1;
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/display/EvenDimmerPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/EvenDimmerPreferenceControllerTest.java
new file mode 100644
index 0000000..a3cf151
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/display/EvenDimmerPreferenceControllerTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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.display;
+
+
+import static com.android.settings.core.BasePreferenceController.AVAILABLE;
+import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.platform.test.annotations.RequiresFlagsDisabled;
+import android.platform.test.annotations.RequiresFlagsEnabled;
+import android.platform.test.flag.junit.CheckFlagsRule;
+import android.platform.test.flag.junit.DeviceFlagsValueProvider;
+import android.provider.Settings;
+
+import com.android.server.display.feature.flags.Flags;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+
+@RunWith(RobolectricTestRunner.class)
+public class EvenDimmerPreferenceControllerTest {
+
+    private EvenDimmerPreferenceController mController;
+    @Mock
+    private Context mContext;
+    @Mock
+    private Resources mResources;
+
+    @Rule
+    public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        when(mContext.getResources()).thenReturn(mResources);
+        mController = new EvenDimmerPreferenceController(mContext, "key");
+    }
+
+    @RequiresFlagsDisabled(Flags.FLAG_EVEN_DIMMER)
+    @Test
+    public void testGetAvailabilityStatus_flagOffconfigTrue() {
+        when(mContext.getResources()).thenReturn(mResources);
+        when(mResources.getBoolean(
+                com.android.internal.R.bool.config_evenDimmerEnabled)).thenReturn(true);
+        // setup
+        mController = new EvenDimmerPreferenceController(mContext, "key");
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
+    }
+
+    @RequiresFlagsDisabled(Flags.FLAG_EVEN_DIMMER)
+    @Test
+    public void testGetCheckedStatus_setTrue() throws Settings.SettingNotFoundException {
+        // setup
+        mController = new EvenDimmerPreferenceController(mContext, "key");
+        mController.setChecked(true);
+
+        assertThat(Settings.Secure.getFloat(mContext.getContentResolver(),
+                Settings.Secure.EVEN_DIMMER_ACTIVATED)).isEqualTo(0.0f); // false
+    }
+
+    @RequiresFlagsEnabled(Flags.FLAG_EVEN_DIMMER)
+    @Test
+    public void testGetAvailabilityStatus_flagOnConfigTrue() {
+        when(mContext.getResources()).thenReturn(mResources);
+        when(mResources.getBoolean(
+                com.android.internal.R.bool.config_evenDimmerEnabled)).thenReturn(true);
+        // setup
+        mController = new EvenDimmerPreferenceController(mContext, "key");
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
+    }
+
+    @Test
+    @RequiresFlagsEnabled(Flags.FLAG_EVEN_DIMMER)
+    public void testSetChecked_enable() throws Settings.SettingNotFoundException {
+        mController.setChecked(true);
+        assertThat(Settings.Secure.getFloat(mContext.getContentResolver(),
+                Settings.Secure.EVEN_DIMMER_ACTIVATED)).isEqualTo(1.0f); // true
+    }
+
+    @Test
+    @RequiresFlagsEnabled(Flags.FLAG_EVEN_DIMMER)
+    public void testSetChecked_disable() throws Settings.SettingNotFoundException {
+        mController.setChecked(false);
+        assertThat(Settings.Secure.getFloat(mContext.getContentResolver(),
+                Settings.Secure.EVEN_DIMMER_ACTIVATED)).isEqualTo(0.0f); // false
+    }
+}