From 0ed28e9fd033b9f8f23c503d391e4a8ca55aad77 Mon Sep 17 00:00:00 2001 From: menghanli Date: Thu, 4 Aug 2022 07:50:43 +0800 Subject: [PATCH] [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 --- .../CaptioningBackgroundColorController.java | 22 +++++++++++++++-- .../CaptioningForegroundColorController.java | 22 +++++++++++++++-- .../CaptioningWindowColorController.java | 22 +++++++++++++++-- ...ptioningBackgroundColorControllerTest.java | 24 +++++++++++++++++++ ...ptioningForegroundColorControllerTest.java | 24 +++++++++++++++++++ .../CaptioningWindowColorControllerTest.java | 24 +++++++++++++++++++ 6 files changed, 132 insertions(+), 6 deletions(-) diff --git a/src/com/android/settings/accessibility/CaptioningBackgroundColorController.java b/src/com/android/settings/accessibility/CaptioningBackgroundColorController.java index ff093d5af7c..686f725e78f 100644 --- a/src/com/android/settings/accessibility/CaptioningBackgroundColorController.java +++ b/src/com/android/settings/accessibility/CaptioningBackgroundColorController.java @@ -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; + } } diff --git a/src/com/android/settings/accessibility/CaptioningForegroundColorController.java b/src/com/android/settings/accessibility/CaptioningForegroundColorController.java index c867732722f..8e9ec1879ee 100644 --- a/src/com/android/settings/accessibility/CaptioningForegroundColorController.java +++ b/src/com/android/settings/accessibility/CaptioningForegroundColorController.java @@ -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; + } } diff --git a/src/com/android/settings/accessibility/CaptioningWindowColorController.java b/src/com/android/settings/accessibility/CaptioningWindowColorController.java index ce68227ed8d..238a75a8b6a 100644 --- a/src/com/android/settings/accessibility/CaptioningWindowColorController.java +++ b/src/com/android/settings/accessibility/CaptioningWindowColorController.java @@ -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; + } } diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptioningBackgroundColorControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptioningBackgroundColorControllerTest.java index 2ecaa0f3f29..0ea7a41a61a 100644 --- a/tests/robotests/src/com/android/settings/accessibility/CaptioningBackgroundColorControllerTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/CaptioningBackgroundColorControllerTest.java @@ -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); diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptioningForegroundColorControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptioningForegroundColorControllerTest.java index 9c64130d4df..8991bad61bb 100644 --- a/tests/robotests/src/com/android/settings/accessibility/CaptioningForegroundColorControllerTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/CaptioningForegroundColorControllerTest.java @@ -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); diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptioningWindowColorControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptioningWindowColorControllerTest.java index abfa98d3f3b..f916778d9bc 100644 --- a/tests/robotests/src/com/android/settings/accessibility/CaptioningWindowColorControllerTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/CaptioningWindowColorControllerTest.java @@ -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; @@ -102,6 +103,29 @@ public class CaptioningWindowColorControllerTest { assertThat(mPreference.getSummary().toString()).isEqualTo("Red"); } + @Test + public void setNoneColorValue_shouldNotHaveColor() { + final CaptionHelper captionHelper = new CaptionHelper(mContext); + captionHelper.setWindowColor(0xFFFF0000); + mController.displayPreference(mScreen); + + mPreference.setValue(0x00FFFFFF); + + assertThat(CaptionStyle.hasColor(captionHelper.getWindowColor())).isFalse(); + } + + @Test + public void setRedValueFromNoneValue_halfOpacityRedColor_shouldReturnExpectedColor() { + final CaptionHelper captionHelper = new CaptionHelper(mContext); + captionHelper.setWindowColor(0x80FF0000); + mController.displayPreference(mScreen); + mPreference.setValue(0x00FFFFFF); + + mPreference.setValue(0xFFFF0000); + + assertThat(captionHelper.getWindowColor()).isEqualTo(0x80FF0000); + } + @Test public void onValueChanged_shouldSetCaptionEnabled() { mShadowCaptioningManager.setEnabled(false);