Display: refactor Color Mode settings
1. Refactor Color Mode by moving color summary functionality to the ColorModeUtils class. 2. Migrated `ColorModeUtils` from Java to Kotlin. 3. Changed ColorModePreferenceControllerTest according to changes Bug: 390644464 Flag: EXEMPT refactoring Test: atest com.android.settings.display Test: atest -c packages/apps/Settings/tests/robotests/src/com/android/settings/display/colors/ColorModePreferenceControllerTest.kt Test: atest -c packages/apps/Settings/tests/unit/src/com/android/settings/display/ColorModePreferenceFragmentTest.java Change-Id: I55ac6129b93e4e35bd58f0313215b711ce954c0a
This commit is contained in:
@@ -16,13 +16,15 @@ package com.android.settings.display;
|
||||
import android.content.Context;
|
||||
import android.hardware.display.ColorDisplayManager;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
|
||||
public class ColorModePreferenceController extends BasePreferenceController {
|
||||
|
||||
public ColorModePreferenceController(Context context, String key) {
|
||||
public ColorModePreferenceController(@NonNull Context context, @NonNull String key) {
|
||||
super(context, key);
|
||||
}
|
||||
|
||||
@@ -36,11 +38,20 @@ public class ColorModePreferenceController extends BasePreferenceController {
|
||||
|
||||
@Override
|
||||
public CharSequence getSummary() {
|
||||
return ColorModeUtils.getColorModeMapping(mContext.getResources()).get(getColorMode());
|
||||
return getColorModeName();
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public int getColorMode() {
|
||||
return mContext.getSystemService(ColorDisplayManager.class).getColorMode();
|
||||
@Override
|
||||
public void updateState(@Nullable Preference preference) {
|
||||
if (preference == null) {
|
||||
return;
|
||||
}
|
||||
super.updateState(preference);
|
||||
preference.setSummary(getSummary());
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private String getColorModeName() {
|
||||
return ColorModeUtils.getActiveColorModeName(mContext);
|
||||
}
|
||||
}
|
||||
|
@@ -213,8 +213,7 @@ public class ColorModePreferenceFragment extends RadioButtonPickerFragment {
|
||||
final Map<Integer, String> colorModesToSummaries =
|
||||
ColorModeUtils.getColorModeMapping(mResources);
|
||||
final List<ColorModeCandidateInfo> candidates = new ArrayList<>();
|
||||
for (int colorMode : mResources.getIntArray(
|
||||
com.android.internal.R.array.config_availableColorModes)) {
|
||||
for (int colorMode : ColorModeUtils.getAvailableColorModes(getContext())) {
|
||||
candidates.add(new ColorModeCandidateInfo(
|
||||
colorModesToSummaries.get(colorMode),
|
||||
getKeyForColorMode(colorMode),
|
||||
@@ -390,8 +389,8 @@ public class ColorModePreferenceFragment extends RadioButtonPickerFragment {
|
||||
|
||||
@Override
|
||||
protected boolean isPageSearchEnabled(Context context) {
|
||||
final int[] availableColorModes = context.getResources().getIntArray(
|
||||
com.android.internal.R.array.config_availableColorModes);
|
||||
final int[] availableColorModes =
|
||||
ColorModeUtils.getAvailableColorModes(context);
|
||||
return availableColorModes != null && availableColorModes.length > 0
|
||||
&& !ColorDisplayManager.areAccessibilityTransformsEnabled(context);
|
||||
}
|
||||
|
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* 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.display;
|
||||
|
||||
import static android.hardware.display.ColorDisplayManager.COLOR_MODE_AUTOMATIC;
|
||||
import static android.hardware.display.ColorDisplayManager.COLOR_MODE_BOOSTED;
|
||||
import static android.hardware.display.ColorDisplayManager.COLOR_MODE_NATURAL;
|
||||
import static android.hardware.display.ColorDisplayManager.COLOR_MODE_SATURATED;
|
||||
import static android.hardware.display.ColorDisplayManager.VENDOR_COLOR_MODE_RANGE_MAX;
|
||||
import static android.hardware.display.ColorDisplayManager.VENDOR_COLOR_MODE_RANGE_MIN;
|
||||
|
||||
import android.content.res.Resources;
|
||||
import android.util.ArrayMap;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
final class ColorModeUtils {
|
||||
|
||||
private ColorModeUtils() {
|
||||
// Do not instantiate.
|
||||
}
|
||||
|
||||
static Map<Integer, String> getColorModeMapping(Resources resources) {
|
||||
final String[] colorModeOptionsStrings = resources.getStringArray(
|
||||
R.array.config_color_mode_options_strings);
|
||||
final int[] colorModeOptionsValues = resources.getIntArray(
|
||||
R.array.config_color_mode_options_values);
|
||||
if (colorModeOptionsStrings.length != colorModeOptionsValues.length) {
|
||||
throw new RuntimeException("Color mode options of unequal length");
|
||||
}
|
||||
|
||||
final Map<Integer, String> colorModesToSummaries = new ArrayMap<>();
|
||||
for (int i = 0; i < colorModeOptionsValues.length; i++) {
|
||||
final int colorMode = colorModeOptionsValues[i];
|
||||
if (colorMode == COLOR_MODE_NATURAL
|
||||
|| colorMode == COLOR_MODE_BOOSTED
|
||||
|| colorMode == COLOR_MODE_SATURATED
|
||||
|| colorMode == COLOR_MODE_AUTOMATIC
|
||||
|| (colorMode >= VENDOR_COLOR_MODE_RANGE_MIN
|
||||
&& colorMode <= VENDOR_COLOR_MODE_RANGE_MAX)) {
|
||||
colorModesToSummaries.put(colorMode, colorModeOptionsStrings[i]);
|
||||
}
|
||||
}
|
||||
return colorModesToSummaries;
|
||||
}
|
||||
}
|
65
src/com/android/settings/display/ColorModeUtils.kt
Normal file
65
src/com/android/settings/display/ColorModeUtils.kt
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (C) 2025 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.hardware.display.ColorDisplayManager
|
||||
import android.hardware.display.ColorDisplayManager.*
|
||||
import android.util.Log
|
||||
|
||||
import com.android.settings.R
|
||||
|
||||
object ColorModeUtils {
|
||||
|
||||
private val TAG = "ColorModeUtils"
|
||||
|
||||
@JvmStatic
|
||||
fun getColorModeMapping(resources: Resources): Map<Int, String> {
|
||||
val colorModeOptionsStrings = resources.getStringArray(
|
||||
R.array.config_color_mode_options_strings
|
||||
)
|
||||
val colorModeOptionsValues = resources.getIntArray(
|
||||
R.array.config_color_mode_options_values
|
||||
)
|
||||
if (colorModeOptionsStrings.size!= colorModeOptionsValues.size) {
|
||||
throw RuntimeException("Color mode options of unequal length")
|
||||
}
|
||||
|
||||
val colorModesToSummaries = colorModeOptionsValues.zip(colorModeOptionsStrings).toMap().filterKeys { colorMode ->
|
||||
colorMode == COLOR_MODE_NATURAL ||
|
||||
colorMode == COLOR_MODE_BOOSTED ||
|
||||
colorMode == COLOR_MODE_SATURATED ||
|
||||
colorMode == COLOR_MODE_AUTOMATIC ||
|
||||
(colorMode >= VENDOR_COLOR_MODE_RANGE_MIN &&
|
||||
colorMode <= VENDOR_COLOR_MODE_RANGE_MAX)
|
||||
}
|
||||
|
||||
return colorModesToSummaries
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getColorMode(context: Context): Int =
|
||||
context.getSystemService(ColorDisplayManager::class.java).colorMode
|
||||
|
||||
@JvmStatic
|
||||
fun getActiveColorModeName(context: Context): String =
|
||||
getColorModeMapping(context.resources)[getColorMode(context)] ?: ""
|
||||
|
||||
@JvmStatic
|
||||
fun getAvailableColorModes(context: Context): IntArray =
|
||||
context.getResources().getIntArray(com.android.internal.R.array.config_availableColorModes)
|
||||
}
|
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (C) 2025 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.hardware.display.ColorDisplayManager
|
||||
|
||||
import androidx.preference.Preference
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
|
||||
import com.android.settingslib.testutils.shadow.ShadowColorDisplayManager
|
||||
import com.android.settings.R
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.shadow.api.Shadow
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(shadows = [ShadowColorDisplayManager::class])
|
||||
class ColorModePreferenceControllerTest {
|
||||
private lateinit var context: Context
|
||||
private lateinit var preference: Preference
|
||||
private lateinit var controller: ColorModePreferenceController
|
||||
private lateinit var shadowColorDisplayManager: ShadowColorDisplayManager
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
context = ApplicationProvider.getApplicationContext()
|
||||
controller = ColorModePreferenceController(context, "test")
|
||||
preference = Preference(context)
|
||||
shadowColorDisplayManager = Shadow.extract(
|
||||
context.getSystemService(ColorDisplayManager::class.java));
|
||||
}
|
||||
|
||||
@Test
|
||||
fun updateState_colorModeAutomatic_shouldSetSummaryToAutomatic() {
|
||||
shadowColorDisplayManager.setColorMode(ColorDisplayManager.COLOR_MODE_AUTOMATIC)
|
||||
controller.updateState(preference)
|
||||
val automaticColorModeName = context.getString(R.string.color_mode_option_automatic)
|
||||
assertThat(preference.summary.toString()).isEqualTo(automaticColorModeName)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun updateState_colorModeSaturated_shouldSetSummaryToSaturated() {
|
||||
shadowColorDisplayManager.setColorMode(ColorDisplayManager.COLOR_MODE_SATURATED)
|
||||
controller.updateState(preference)
|
||||
val saturatedColorModeName = context.getString(R.string.color_mode_option_saturated)
|
||||
assertThat(preference.summary.toString()).isEqualTo(saturatedColorModeName)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun updateState_colorModeBoosted_shouldSetSummaryToBoosted() {
|
||||
shadowColorDisplayManager.setColorMode(ColorDisplayManager.COLOR_MODE_BOOSTED)
|
||||
controller.updateState(preference)
|
||||
val boostedColorModeName = context.getString(R.string.color_mode_option_boosted)
|
||||
assertThat(preference.summary.toString()).isEqualTo(boostedColorModeName)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun updateState_colorModeNatural_shouldSetSummaryToNatural() {
|
||||
shadowColorDisplayManager.setColorMode(ColorDisplayManager.COLOR_MODE_NATURAL)
|
||||
controller.updateState(preference)
|
||||
val naturalColorModeName = context.getString(R.string.color_mode_option_natural)
|
||||
assertThat(preference.summary.toString()).isEqualTo(naturalColorModeName)
|
||||
}
|
||||
}
|
@@ -1,97 +0,0 @@
|
||||
/*
|
||||
* 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.display;
|
||||
|
||||
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.hardware.display.ColorDisplayManager;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.test.annotation.UiThreadTest;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ColorModePreferenceControllerTest {
|
||||
|
||||
private Preference mPreference;
|
||||
private ColorModePreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
final Context context = spy(ApplicationProvider.getApplicationContext());
|
||||
mController = spy(new ColorModePreferenceController(context, "test"));
|
||||
mPreference = new Preference(context);
|
||||
final Resources res = spy(context.getResources());
|
||||
when(res.getIntArray(com.android.internal.R.array.config_availableColorModes)).thenReturn(
|
||||
new int[]{
|
||||
ColorDisplayManager.COLOR_MODE_NATURAL,
|
||||
ColorDisplayManager.COLOR_MODE_BOOSTED,
|
||||
ColorDisplayManager.COLOR_MODE_SATURATED,
|
||||
ColorDisplayManager.COLOR_MODE_AUTOMATIC
|
||||
});
|
||||
doReturn(res).when(context).getResources();
|
||||
}
|
||||
|
||||
@Test
|
||||
@UiThreadTest
|
||||
public void updateState_colorModeAutomatic_shouldSetSummaryToAutomatic() {
|
||||
doReturn(ColorDisplayManager.COLOR_MODE_AUTOMATIC).when(mController).getColorMode();
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.getSummary()).isEqualTo("Adaptive");
|
||||
}
|
||||
|
||||
@Test
|
||||
@UiThreadTest
|
||||
public void updateState_colorModeSaturated_shouldSetSummaryToSaturated() {
|
||||
doReturn(ColorDisplayManager.COLOR_MODE_SATURATED).when(mController).getColorMode();
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.getSummary()).isEqualTo("Saturated");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_colorModeBoosted_shouldSetSummaryToBoosted() {
|
||||
doReturn(ColorDisplayManager.COLOR_MODE_BOOSTED).when(mController).getColorMode();
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.getSummary()).isEqualTo("Boosted");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_colorModeNatural_shouldSetSummaryToNatural() {
|
||||
doReturn(ColorDisplayManager.COLOR_MODE_NATURAL).when(mController).getColorMode();
|
||||
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.getSummary()).isEqualTo("Natural");
|
||||
}
|
||||
}
|
@@ -76,6 +76,7 @@ public class ColorModePreferenceFragmentTest {
|
||||
});
|
||||
doReturn(res).when(mContext).getResources();
|
||||
mFragment.onAttach(mContext);
|
||||
doReturn(mContext).when(mFragment).getContext();
|
||||
|
||||
final List<? extends CandidateInfo> candidates = mFragment.getCandidates();
|
||||
|
||||
@@ -99,6 +100,7 @@ public class ColorModePreferenceFragmentTest {
|
||||
});
|
||||
doReturn(res).when(mContext).getResources();
|
||||
mFragment.onAttach(mContext);
|
||||
doReturn(mContext).when(mFragment).getContext();
|
||||
|
||||
List<? extends CandidateInfo> candidates = mFragment.getCandidates();
|
||||
|
||||
@@ -116,6 +118,7 @@ public class ColorModePreferenceFragmentTest {
|
||||
});
|
||||
doReturn(res).when(mContext).getResources();
|
||||
mFragment.onAttach(mContext);
|
||||
doReturn(mContext).when(mFragment).getContext();
|
||||
|
||||
List<? extends CandidateInfo> candidates = mFragment.getCandidates();
|
||||
|
||||
@@ -138,6 +141,7 @@ public class ColorModePreferenceFragmentTest {
|
||||
});
|
||||
doReturn(res).when(mContext).getResources();
|
||||
mFragment.onAttach(mContext);
|
||||
doReturn(mContext).when(mFragment).getContext();
|
||||
|
||||
List<? extends CandidateInfo> candidates = mFragment.getCandidates();
|
||||
|
||||
|
Reference in New Issue
Block a user