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

Commit 26d0f62b authored by Vadym Omelnytskyi's avatar Vadym Omelnytskyi Committed by Android (Google) Code Review
Browse files

Merge changes from topic "shadow-color-mode-manager-extend" into main

* changes:
  Display: make Colors settings entry preference reactive
  Display: refactor Color Mode settings
parents 57da8021 cc96950d
Loading
Loading
Loading
Loading
+61 −7
Original line number Diff line number Diff line
@@ -14,15 +14,39 @@
package com.android.settings.display;

import android.content.Context;
import android.database.ContentObserver;
import android.hardware.display.ColorDisplayManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;

import androidx.annotation.VisibleForTesting;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.core.BasePreferenceController;

public class ColorModePreferenceController extends BasePreferenceController {
public class ColorModePreferenceController extends BasePreferenceController
        implements LifecycleObserver {

    public ColorModePreferenceController(Context context, String key) {
    private Preference mPreference;

    private final ContentObserver mContentObserver = new ContentObserver(
            new Handler(Looper.getMainLooper())) {
        @Override
        public void onChange(boolean selfChange, @Nullable Uri uri) {
            if (mPreference != null) {
                updateState(mPreference);
            }
        }
    };

    public ColorModePreferenceController(@NonNull Context context, @NonNull String key) {
        super(context, key);
    }

@@ -34,13 +58,43 @@ public class ColorModePreferenceController extends BasePreferenceController {
                AVAILABLE : DISABLED_FOR_USER;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume() {
        mContext.getContentResolver().registerContentObserver(
                Settings.System.getUriFor(Settings.System.DISPLAY_COLOR_MODE),
                /* notifyForDescendants= */ false,
                mContentObserver);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause() {
        mContext.getContentResolver().unregisterContentObserver(mContentObserver);
    }

    @Override
    public CharSequence getSummary() {
        return ColorModeUtils.getColorModeMapping(mContext.getResources()).get(getColorMode());
        return getColorModeName();
    }

    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mPreference = screen.findPreference(getPreferenceKey());
        if (mPreference != null) {
            updateState(mPreference);
        }
    }

    @Override
    public void updateState(@Nullable Preference preference) {
        if (preference == null) {
            return;
        }
        super.updateState(preference);
        preference.setSummary(getSummary());
    }

    @VisibleForTesting
    public int getColorMode() {
        return mContext.getSystemService(ColorDisplayManager.class).getColorMode();
    @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
+139 −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.ContentResolver
import android.content.Context
import android.database.ContentObserver
import android.hardware.display.ColorDisplayManager
import android.provider.Settings

import androidx.preference.Preference
import androidx.preference.PreferenceScreen
import androidx.preference.PreferenceManager
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
import org.robolectric.shadows.ShadowContentResolver

@RunWith(RobolectricTestRunner::class)
@Config(shadows = [ShadowColorDisplayManager::class, ShadowContentResolver::class])
class ColorModePreferenceControllerTest {
    private lateinit var context: Context
    private lateinit var preference: Preference
    private lateinit var controller: ColorModePreferenceController
    private lateinit var shadowColorDisplayManager: ShadowColorDisplayManager
    private lateinit var shadowContentResolver: ShadowContentResolver

    @Before
    fun setup() {
        context = ApplicationProvider.getApplicationContext()

        controller = ColorModePreferenceController(context, "test")
        preference = Preference(context)
        val preferenceManager = PreferenceManager(context)
        val preferenceScreen = preferenceManager.createPreferenceScreen(context)
        preference.setKey(controller.getPreferenceKey());
        preferenceScreen.addPreference(preference)

        shadowColorDisplayManager = Shadow.extract(
            context.getSystemService(ColorDisplayManager::class.java))
        val contentResolver = context.getContentResolver();
        shadowContentResolver = Shadow.extract(contentResolver)

        controller.displayPreference(preferenceScreen)
    }

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

    @Test
    fun onResume_verifyRegisterColorModeObserver() {
        controller.onResume()
        assertThat(shadowContentResolver.getContentObservers(
            Settings.System.getUriFor(Settings.System.DISPLAY_COLOR_MODE)))
            .hasSize(1)
    }

    @Test
    fun onPause_verifyUnregisterColorModeObserver() {
        controller.onResume()
        controller.onPause()
        assertThat(shadowContentResolver.getContentObservers(
            Settings.System.getUriFor(Settings.System.DISPLAY_COLOR_MODE)))
            .isEmpty()
    }

    @Test
    fun contentObserver_onChange_updatesPreferenceSummary() {
        controller.onResume()
        assertThat(shadowContentResolver.getContentObservers(
            Settings.System.getUriFor(Settings.System.DISPLAY_COLOR_MODE)))
            .hasSize(1)

        shadowColorDisplayManager.setColorMode(ColorDisplayManager.COLOR_MODE_NATURAL)
        triggerOnChangeListener()
        assertThat(preference.summary).isEqualTo(context.getString(R.string.color_mode_option_natural))

        shadowColorDisplayManager.setColorMode(ColorDisplayManager.COLOR_MODE_AUTOMATIC)
        triggerOnChangeListener()
        assertThat(preference.summary).isEqualTo(context.getString(R.string.color_mode_option_automatic))
    }

    private fun triggerOnChangeListener() {
        shadowContentResolver.getContentObservers(
            Settings.System.getUriFor(Settings.System.DISPLAY_COLOR_MODE))
            .forEach {it.onChange(false, null)};
    }
}
 No newline at end of file
Loading