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

Commit 23258c2b authored by Isaac Chai's avatar Isaac Chai
Browse files

fix(Color correction): Display default value in slider to match the radio button

The default value for display in the radio buttons were hard-coded and was not
being reflected to the slider correctly.

Bug: 359379399
Test: locally tested + unit tests added
Flag: com.android.server.accessibility.enable_color_correction_saturation

Change-Id: I95bd0ae1d32561dbbf6b4e87efe70595c9566de8
parent 1dfc1874
Loading
Loading
Loading
Loading
+59 −0
Original line number Diff line number Diff line
/*
 * 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.accessibility;

import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;

import android.content.ContentResolver;
import android.provider.Settings;
import android.view.accessibility.AccessibilityManager;

import com.google.common.primitives.Ints;

/**
 * Utility class for retrieving accessibility daltonizer related values in secure settings.
 */
public class DaltonizerPreferenceUtil {

    /**
     * Return the daltonizer display mode stored in
     * {@link Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER}.
     * By default it returns {@link DALTONIZER_CORRECT_DEUTERANOMALY}.
     */
    public static int getSecureAccessibilityDaltonizerValue(ContentResolver resolver) {
        final String daltonizerStringValue = Settings.Secure.getString(
                resolver, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER);
        if (daltonizerStringValue == null) {
            return AccessibilityManager.DALTONIZER_CORRECT_DEUTERANOMALY;
        }
        final Integer daltonizerIntValue = Ints.tryParse(daltonizerStringValue);
        return daltonizerIntValue == null ? AccessibilityManager.DALTONIZER_CORRECT_DEUTERANOMALY
                : daltonizerIntValue;
    }

    /**
     * Returns the daltonizer enabled value in
     * {@link Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED}.
     * By default it returns false.
     */
    public static boolean isSecureAccessibilityDaltonizerEnabled(ContentResolver resolver) {
        return Settings.Secure.getInt(
                resolver,
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED,
                OFF) == ON;
    }
}
+2 −15
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.view.View;
import android.view.accessibility.AccessibilityManager;

import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
@@ -36,8 +35,6 @@ import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.widget.SelectorWithWidgetPreference;

import com.google.common.primitives.Ints;

import java.util.HashMap;
import java.util.Map;

@@ -70,17 +67,6 @@ public class DaltonizerRadioButtonPreferenceController extends BasePreferenceCon
        };
    }

    protected static int getSecureAccessibilityDaltonizerValue(ContentResolver resolver) {
        final String daltonizerStringValue = Settings.Secure.getString(
                resolver, DALTONIZER_TYPE_SETTINGS_KEY);
        if (daltonizerStringValue == null) {
            return AccessibilityManager.DALTONIZER_CORRECT_DEUTERANOMALY;
        }
        final Integer daltonizerIntValue = Ints.tryParse(daltonizerStringValue);
        return daltonizerIntValue == null ? AccessibilityManager.DALTONIZER_CORRECT_DEUTERANOMALY
                : daltonizerIntValue;
    }

    private Map<String, Integer> getDaltonizerValueToKeyMap() {
        if (mAccessibilityDaltonizerKeyToValueMap.isEmpty()) {

@@ -123,7 +109,8 @@ public class DaltonizerRadioButtonPreferenceController extends BasePreferenceCon
    }

    private int getAccessibilityDaltonizerValue() {
        final int daltonizerValue = getSecureAccessibilityDaltonizerValue(mContentResolver);
        final int daltonizerValue =
                DaltonizerPreferenceUtil.getSecureAccessibilityDaltonizerValue(mContentResolver);
        return daltonizerValue;
    }

+6 −6
Original line number Diff line number Diff line
@@ -15,6 +15,9 @@
 */
package com.android.settings.accessibility;

import static com.android.settings.accessibility.DaltonizerPreferenceUtil.isSecureAccessibilityDaltonizerEnabled;
import static com.android.settings.accessibility.DaltonizerPreferenceUtil.getSecureAccessibilityDaltonizerValue;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
@@ -158,14 +161,11 @@ public class DaltonizerSaturationSeekbarPreferenceController
    }

    private boolean shouldSeekBarEnabled() {
        int enabled = Settings.Secure.getInt(
                mContentResolver, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0);
        int mode = Settings.Secure.getInt(
                mContentResolver, Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, -1);
        boolean enabled = isSecureAccessibilityDaltonizerEnabled(mContentResolver);
        int mode = getSecureAccessibilityDaltonizerValue(mContentResolver);

        // enabled == 0 is disabled and also default.
        // mode == 0 is gray scale where saturation level isn't applicable.
        // mode == -1 is disabled and also default.
        return enabled != 0 && mode != -1 && mode != 0;
        return enabled && mode != -1 && mode != 0;
    }
}
+3 −1
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import static com.android.internal.accessibility.AccessibilityShortcutController
import static com.android.settings.accessibility.AccessibilityStatsLogUtils.logAccessibilityServiceEnabled;
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
import static com.android.settings.accessibility.DaltonizerPreferenceUtil.isSecureAccessibilityDaltonizerEnabled;

import android.app.settings.SettingsEnums;
import android.content.ComponentName;
@@ -145,7 +146,8 @@ public class ToggleDaltonizerPreferenceFragment extends ToggleFeaturePreferenceF

    @Override
    protected void onPreferenceToggled(String preferenceKey, boolean enabled) {
        final boolean isEnabled = Settings.Secure.getInt(getContentResolver(), ENABLED, OFF) == ON;
        final boolean isEnabled =
                isSecureAccessibilityDaltonizerEnabled(getContentResolver());
        if (enabled == isEnabled) {
            return;
        }
+40 −26
Original line number Diff line number Diff line
@@ -58,8 +58,6 @@ public class DaltonizerSaturationSeekbarPreferenceControllerTest {
    private ContentResolver mContentResolver;
    private DaltonizerSaturationSeekbarPreferenceController mController;

    private int mOriginalSaturationLevel = -1;

    private PreferenceScreen mScreen;
    private LifecycleOwner mLifecycleOwner;
    private Lifecycle mLifecycle;
@@ -73,10 +71,6 @@ public class DaltonizerSaturationSeekbarPreferenceControllerTest {
    public void setup() {
        Context context = ApplicationProvider.getApplicationContext();
        mContentResolver = context.getContentResolver();
        mOriginalSaturationLevel = Settings.Secure.getInt(
                mContentResolver,
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_SATURATION_LEVEL,
                7);

        mPreference = new SeekBarPreference(context);
        mPreference.setKey(ToggleDaltonizerPreferenceFragment.KEY_SATURATION);
@@ -92,10 +86,18 @@ public class DaltonizerSaturationSeekbarPreferenceControllerTest {

    @After
    public void cleanup() {
        Settings.Secure.putInt(
        Settings.Secure.putString(
                mContentResolver,
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER,
                null);
        Settings.Secure.putString(
                mContentResolver,
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED,
                null);
        Settings.Secure.putString(
                mContentResolver,
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_SATURATION_LEVEL,
                mOriginalSaturationLevel);
                null);
    }

    @Test
@@ -111,6 +113,22 @@ public class DaltonizerSaturationSeekbarPreferenceControllerTest {
        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_COLOR_CORRECTION_SATURATION)
    public void getAvailabilityStatus_defaultSettings_unavailable() {
        // By default enabled == false.
        assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING);
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_COLOR_CORRECTION_SATURATION)
    public void getAvailabilityStatus_enabledDefaultDisplayMode_available() {
        setDaltonizerEnabled(1);

        // By default display mode is deuteranomaly.
        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_COLOR_CORRECTION_SATURATION)
    public void getAvailabilityStatus_flagEnabledProtanEnabled_available() {
@@ -306,10 +324,7 @@ public class DaltonizerSaturationSeekbarPreferenceControllerTest {
        mLifecycle.addObserver(mController);
        mLifecycle.handleLifecycleEvent(ON_RESUME);

        Settings.Secure.putInt(
                mContentResolver,
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED,
                1);
        setDaltonizerEnabled(1);
        shadowOf(Looper.getMainLooper()).idle();

        assertThat(mPreference.isEnabled()).isTrue();
@@ -324,10 +339,7 @@ public class DaltonizerSaturationSeekbarPreferenceControllerTest {
        mLifecycle.addObserver(mController);
        mLifecycle.handleLifecycleEvent(ON_RESUME);

        Settings.Secure.putInt(
                mContentResolver,
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED,
                0);
        setDaltonizerEnabled(0);
        shadowOf(Looper.getMainLooper()).idle();

        assertThat(mPreference.isEnabled()).isFalse();
@@ -342,10 +354,7 @@ public class DaltonizerSaturationSeekbarPreferenceControllerTest {
        mLifecycle.addObserver(mController);
        mLifecycle.handleLifecycleEvent(ON_RESUME);

        Settings.Secure.putInt(
                mContentResolver,
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER,
                0);
        setDaltonizerDisplay(0);
        shadowOf(Looper.getMainLooper()).idle();

        assertThat(mPreference.isEnabled()).isFalse();
@@ -361,23 +370,28 @@ public class DaltonizerSaturationSeekbarPreferenceControllerTest {
        mLifecycle.handleLifecycleEvent(ON_STOP);

        // enabled.
        Settings.Secure.putInt(
                mContentResolver,
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED,
                1);
        setDaltonizerEnabled(1);
        shadowOf(Looper.getMainLooper()).idle();

        assertThat(mPreference.isEnabled()).isFalse();
    }

    private void setDaltonizerMode(int enabled, int mode) {
        setDaltonizerEnabled(enabled);
        setDaltonizerDisplay(mode);
    }

    private void setDaltonizerEnabled(int enabled) {
        Settings.Secure.putInt(
                mContentResolver,
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED,
                enabled);
        Settings.Secure.putInt(
    }

    private void setDaltonizerDisplay(int mode) {
        Settings.Secure.putString(
                mContentResolver,
                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER,
                mode);
                Integer.toString(mode));
    }
}