Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a8f1da69 authored by Vadym Omelnytskyi's avatar Vadym Omelnytskyi
Browse files

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
parent 6baa6fee
Loading
Loading
Loading
Loading
+17 −6
Original line number Diff line number Diff line
@@ -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);
    }
}
+3 −4
Original line number Diff line number Diff line
@@ -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);
                }
+0 −62
Original line number Diff line number Diff line
/*
 * 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 −0
Original line number Diff line number Diff line
/*
 * 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)
}
 No newline at end of file
+83 −0
Original line number Diff line number Diff line
/*
 * 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)
    }
}
 No newline at end of file
Loading