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

Commit d7da1cd6 authored by Fiona Campbell's avatar Fiona Campbell
Browse files

Make Extra Dim Setting as percentage of device

- Extra dim setting now operates in percentage of the device's
  capabilities
- ie "100" now means the maximum possible dimming on the current device,
  if restored to another device, this means the max possible dimming on
  that device as well.

Bug: 337351445
Flag: EXEMPT bugfix
Test: atest DisplayServiceTests
Change-Id: I50c837a57d7aabf255685935539914723a38cb75
parent 7650b0f3
Loading
Loading
Loading
Loading
+22 −6
Original line number Diff line number Diff line
@@ -131,6 +131,12 @@ public final class ColorDisplayService extends SystemService {
     */
    private static final int NOT_SET = -1;

    /**
     * Min and Max values for percentage of RBC setting.
     */
    private static final int PERCENTAGE_MIN = 0;
    private static final int PERCENTAGE_MAX = 100;

    /**
     * Evaluator used to animate color matrix transitions.
     */
@@ -696,15 +702,25 @@ public final class ColorDisplayService extends SystemService {
        if (mCurrentUser == UserHandle.USER_NULL) {
            return;
        }
        int strength = Secure.getIntForUser(getContext().getContentResolver(),

        int percentage = Secure.getIntForUser(getContext().getContentResolver(),
                Secure.REDUCE_BRIGHT_COLORS_LEVEL, NOT_SET, mCurrentUser);
        if (strength == NOT_SET) {
            strength = getContext().getResources().getInteger(
        final int deviceRange;

        if (percentage == NOT_SET) {
            deviceRange = getContext().getResources().getInteger(
                    R.integer.config_reduceBrightColorsStrengthDefault);
        }
        mReduceBrightColorsTintController.setMatrix(strength);
        } else {
            final int deviceMin = getContext().getResources().getInteger(
                    R.integer.config_reduceBrightColorsStrengthMin);
            final int deviceMax = getContext().getResources().getInteger(
                    R.integer.config_reduceBrightColorsStrengthMax);
            deviceRange = (int) MathUtils.constrainedMap(
                    deviceMin, deviceMax, PERCENTAGE_MIN, PERCENTAGE_MAX, percentage);
        }
        mReduceBrightColorsTintController.setMatrix(deviceRange);
        if (mReduceBrightColorsListener != null) {
            mReduceBrightColorsListener.onReduceBrightColorsStrengthChanged(strength);
            mReduceBrightColorsListener.onReduceBrightColorsStrengthChanged(deviceRange);
        }
    }

+61 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.display.color;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
@@ -1194,6 +1195,66 @@ public class ColorDisplayServiceTest {
        verify(mRbcSpy, times(1)).setUp(eq(mContext), anyBoolean());
    }

    @Test
    public void sliderScalesWithinRange() {
        // setup
        startService();
        reset(mRbcSpy);
        doReturn(true).when(mRbcSpy).isAvailable(mContext);
        when(mContext.getResources().getInteger(
                R.integer.config_reduceBrightColorsStrengthMax)).thenReturn(85);
        when(mContext.getResources().getInteger(
                R.integer.config_reduceBrightColorsStrengthMin)).thenReturn(10);
        when(mContext.getResources().getInteger(
                R.integer.config_reduceBrightColorsStrengthDefault)).thenReturn(44);

        // Valid value test //
        // set on, and to 90% of range
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 1);
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.REDUCE_BRIGHT_COLORS_LEVEL, 90);
        // update
        mCds.mHandler.runWithScissors(
                () -> mCds.onDisplayColorModeChanged(ColorDisplayManager.COLOR_MODE_NATURAL),
                1000);
        // verify this:
        assertEquals(90, Settings.Secure.getInt(mContext.getContentResolver(),
                        Settings.Secure.REDUCE_BRIGHT_COLORS_LEVEL, 0));
        assertEquals(/* 85 - 10 * 0.90f + 10 = */ 77, mRbcSpy.getStrength());

        // Out of range test //
        // set on, and to 101% of range
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 1);
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.REDUCE_BRIGHT_COLORS_LEVEL, 101);
        // update
        mCds.mHandler.runWithScissors(
                () -> mCds.onDisplayColorModeChanged(ColorDisplayManager.COLOR_MODE_NATURAL),
                1000);
        // verify this:
        assertEquals(101, Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.REDUCE_BRIGHT_COLORS_LEVEL, 0));
        assertEquals(/* 85 - 10 * 1.0f + 10 = */ 85, mRbcSpy.getStrength());

        // Invalid value test //
        // set on, and to an invalid value
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.REDUCE_BRIGHT_COLORS_ACTIVATED, 1);
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.REDUCE_BRIGHT_COLORS_LEVEL, -1);
        // update
        mCds.mHandler.runWithScissors(
                () -> mCds.onDisplayColorModeChanged(ColorDisplayManager.COLOR_MODE_NATURAL),
                1000);
        // verify this, setting will stay the same, strength should set to default?:
        assertEquals(-1, Settings.Secure.getInt(mContext.getContentResolver(),
                Settings.Secure.REDUCE_BRIGHT_COLORS_LEVEL, 0));
        // default value is set instead!
        assertEquals(/* default = */ 44, mRbcSpy.getStrength());
    }

    /**
     * Configures Night display to use a custom schedule.
     *