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

Commit 0ed28e9f authored by menghanli's avatar menghanli
Browse files

[Captioning preferences] Fix wrong color if default color selected

Root cause: When the default captioning color was selected, the opacity became 100% and made the opacity preference is disabled. After changing to non-default color, the opacity preference becomes enabled, but it keeps showing cached opacity in opacity preference and the preview still shows the wrong color.

Solution: Cache the latest opacity if default captioning color was selected to show the correct opacity later.

Bug: 241308551
Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.accessibility
Change-Id: I7712fb25d622da62d7fb2d017e33f94ef258941b
parent aef0cde4
Loading
Loading
Loading
Loading
+20 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.settings.accessibility;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.accessibility.CaptioningManager.CaptionStyle;

import androidx.preference.PreferenceScreen;

@@ -31,6 +32,7 @@ public class CaptioningBackgroundColorController extends BasePreferenceControlle
        implements OnValueChangedListener {

    private final CaptionHelper mCaptionHelper;
    private int mCachedNonDefaultOpacity = CaptionStyle.COLOR_UNSPECIFIED;

    public CaptioningBackgroundColorController(Context context, String preferenceKey) {
        super(context, preferenceKey);
@@ -67,10 +69,26 @@ public class CaptioningBackgroundColorController extends BasePreferenceControlle

    @Override
    public void onValueChanged(ListDialogPreference preference, int value) {
        final int backBackgroundColor = mCaptionHelper.getBackgroundColor();
        final int opacity = CaptionUtils.parseOpacity(backBackgroundColor);
        final boolean isNonDefaultColor = CaptionStyle.hasColor(value);
        final int opacity = getNonDefaultOpacity(isNonDefaultColor);
        final int merged = CaptionUtils.mergeColorOpacity(value, opacity);
        mCaptionHelper.setBackgroundColor(merged);
        mCaptionHelper.setEnabled(true);
    }

    private int getNonDefaultOpacity(boolean isNonDefaultColor) {
        final int backBackgroundColor = mCaptionHelper.getBackgroundColor();
        final int opacity = CaptionUtils.parseOpacity(backBackgroundColor);
        if (isNonDefaultColor) {
            final int lastOpacity = mCachedNonDefaultOpacity != CaptionStyle.COLOR_UNSPECIFIED
                    ? mCachedNonDefaultOpacity : opacity;
            // Reset cached opacity to use current color opacity to merge color.
            mCachedNonDefaultOpacity = CaptionStyle.COLOR_UNSPECIFIED;
            return lastOpacity;
        }
        // When default captioning color was selected, the opacity become 100% and make opacity
        // preference disable. Cache the latest opacity to show the correct opacity later.
        mCachedNonDefaultOpacity = opacity;
        return opacity;
    }
}
+20 −2
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settings.accessibility;

import android.content.Context;
import android.content.res.Resources;
import android.view.accessibility.CaptioningManager.CaptionStyle;

import androidx.preference.PreferenceScreen;

@@ -30,6 +31,7 @@ public class CaptioningForegroundColorController extends BasePreferenceControlle
        implements OnValueChangedListener {

    private final CaptionHelper mCaptionHelper;
    private int mCachedNonDefaultOpacity = CaptionStyle.COLOR_UNSPECIFIED;

    public CaptioningForegroundColorController(Context context, String preferenceKey) {
        super(context, preferenceKey);
@@ -59,10 +61,26 @@ public class CaptioningForegroundColorController extends BasePreferenceControlle

    @Override
    public void onValueChanged(ListDialogPreference preference, int value) {
        final int foregroundColor = mCaptionHelper.getForegroundColor();
        final int opacity = CaptionUtils.parseOpacity(foregroundColor);
        final boolean isNonDefaultColor = CaptionStyle.hasColor(value);
        final int opacity = getNonDefaultOpacity(isNonDefaultColor);
        final int merged = CaptionUtils.mergeColorOpacity(value, opacity);
        mCaptionHelper.setForegroundColor(merged);
        mCaptionHelper.setEnabled(true);
    }

    private int getNonDefaultOpacity(boolean isNonDefaultColor) {
        final int foregroundColor = mCaptionHelper.getForegroundColor();
        final int opacity = CaptionUtils.parseOpacity(foregroundColor);
        if (isNonDefaultColor) {
            final int lastOpacity = mCachedNonDefaultOpacity != CaptionStyle.COLOR_UNSPECIFIED
                    ? mCachedNonDefaultOpacity : opacity;
            // Reset cached opacity to use current color opacity to merge color.
            mCachedNonDefaultOpacity = CaptionStyle.COLOR_UNSPECIFIED;
            return lastOpacity;
        }
        // When default captioning color was selected, the opacity become 100% and make opacity
        // preference disable. Cache the latest opacity to show the correct opacity later.
        mCachedNonDefaultOpacity = opacity;
        return opacity;
    }
}
+20 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.settings.accessibility;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.accessibility.CaptioningManager.CaptionStyle;

import androidx.preference.PreferenceScreen;

@@ -31,6 +32,7 @@ public class CaptioningWindowColorController extends BasePreferenceController
        implements OnValueChangedListener {

    private final CaptionHelper mCaptionHelper;
    private int mCachedNonDefaultOpacity = CaptionStyle.COLOR_UNSPECIFIED;

    public CaptioningWindowColorController(Context context, String preferenceKey) {
        super(context, preferenceKey);
@@ -67,10 +69,26 @@ public class CaptioningWindowColorController extends BasePreferenceController

    @Override
    public void onValueChanged(ListDialogPreference preference, int value) {
        final int windowColor = mCaptionHelper.getWindowColor();
        final int opacity = CaptionUtils.parseOpacity(windowColor);
        final boolean isNonDefaultColor = CaptionStyle.hasColor(value);
        final int opacity = getNonDefaultOpacity(isNonDefaultColor);
        final int merged = CaptionUtils.mergeColorOpacity(value, opacity);
        mCaptionHelper.setWindowColor(merged);
        mCaptionHelper.setEnabled(true);
    }

    private int getNonDefaultOpacity(boolean isNonDefaultColor) {
        final int windowColor = mCaptionHelper.getWindowColor();
        final int opacity = CaptionUtils.parseOpacity(windowColor);
        if (isNonDefaultColor) {
            final int lastOpacity = mCachedNonDefaultOpacity != CaptionStyle.COLOR_UNSPECIFIED
                    ? mCachedNonDefaultOpacity : opacity;
            // Reset cached opacity to use current color opacity to merge color.
            mCachedNonDefaultOpacity = CaptionStyle.COLOR_UNSPECIFIED;
            return lastOpacity;
        }
        // When default captioning color was selected, the opacity become 100% and make opacity
        // preference disable. Cache the latest opacity to show the correct opacity later.
        mCachedNonDefaultOpacity = opacity;
        return opacity;
    }
}
+24 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.content.Context;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.accessibility.CaptioningManager;
import android.view.accessibility.CaptioningManager.CaptionStyle;

import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
@@ -101,6 +102,29 @@ public class CaptioningBackgroundColorControllerTest {
        assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
    }

    @Test
    public void setNoneColorValue_shouldNotHaveColor() {
        final CaptionHelper captionHelper = new CaptionHelper(mContext);
        captionHelper.setBackgroundColor(0xFFFF0000);
        mController.displayPreference(mScreen);

        mPreference.setValue(0x00FFFFFF);

        assertThat(CaptionStyle.hasColor(captionHelper.getBackgroundColor())).isFalse();
    }

    @Test
    public void setRedValueFromNoneValue_halfOpacityRedColor_shouldReturnExpectedColor() {
        final CaptionHelper captionHelper = new CaptionHelper(mContext);
        captionHelper.setBackgroundColor(0x80FF0000);
        mController.displayPreference(mScreen);
        mPreference.setValue(0x00FFFFFF);

        mPreference.setValue(0xFFFF0000);

        assertThat(captionHelper.getBackgroundColor()).isEqualTo(0x80FF0000);
    }

    @Test
    public void onValueChanged_shouldSetCaptionEnabled() {
        mShadowCaptioningManager.setEnabled(false);
+24 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.content.Context;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.accessibility.CaptioningManager;
import android.view.accessibility.CaptioningManager.CaptionStyle;

import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
@@ -101,6 +102,29 @@ public class CaptioningForegroundColorControllerTest {
        assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
    }

    @Test
    public void setNoneColorValue_shouldNotHaveColor() {
        final CaptionHelper captionHelper = new CaptionHelper(mContext);
        captionHelper.setForegroundColor(0xFFFF0000);
        mController.displayPreference(mScreen);

        mPreference.setValue(0x00FFFFFF);

        assertThat(CaptionStyle.hasColor(captionHelper.getForegroundColor())).isFalse();
    }

    @Test
    public void setRedValueFromNoneValue_halfOpacityRedColor_shouldReturnExpectedColor() {
        final CaptionHelper captionHelper = new CaptionHelper(mContext);
        captionHelper.setForegroundColor(0x80FF0000);
        mController.displayPreference(mScreen);
        mPreference.setValue(0x00FFFFFF);

        mPreference.setValue(0xFFFF0000);

        assertThat(captionHelper.getForegroundColor()).isEqualTo(0x80FF0000);
    }

    @Test
    public void onValueChanged_shouldSetCaptionEnabled() {
        mShadowCaptioningManager.setEnabled(false);
Loading