Create EvenDimmerPreferenceController

- Create preference controller
- linked to even dimmer activated setting

Bug: 179428400
Test: EvenDimmerPreferenceControllerTest
Change-Id: I3ac6501c3d45399caeda96fe6a7dd4164439d1bd
This commit is contained in:
Fiona Campbell
2024-01-29 17:27:33 +00:00
parent 62fa76f719
commit 836018788d
4 changed files with 199 additions and 0 deletions

View File

@@ -2824,6 +2824,10 @@
<string name="dark_ui_bedtime_footer_summary">Dark theme is currently following your Bedtime mode schedule</string> <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] --> <!-- 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> <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 --> <!-- Sound & display settings screen, setting option name to change screen timeout -->

View File

@@ -36,6 +36,11 @@
android:title="@string/auto_brightness_title" android:title="@string/auto_brightness_title"
android:fragment="com.android.settings.display.AutoBrightnessSettings" android:fragment="com.android.settings.display.AutoBrightnessSettings"
settings:controller="com.android.settings.display.AutoBrightnessPreferenceController"/> 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>
<PreferenceCategory <PreferenceCategory

View File

@@ -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;
}
}

View File

@@ -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
}
}