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

Commit b3eea625 authored by Riley Jones's avatar Riley Jones
Browse files

Caption settings cleanup

Bug: 353757664
Test: atest com.android.settings.accessibility
Test: Manually verify conditions described in bug
Flag: com.android.settings.accessibility.fix_a11y_settings_search
Change-Id: Iff25702843e5dd3e7ebcca1a13f190d48cf83e7a
parent cc8e87b0
Loading
Loading
Loading
Loading
+24 −15
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settings.accessibility;

import android.annotation.Nullable;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Handler;
@@ -39,6 +40,7 @@ import java.util.List;
public class CaptioningCustomController extends BasePreferenceController
        implements LifecycleObserver, OnStart, OnStop {

    @Nullable
    private Preference mCustom;
    private final CaptionHelper mCaptionHelper;
    private final ContentResolver mContentResolver;
@@ -50,32 +52,41 @@ public class CaptioningCustomController extends BasePreferenceController
    );

    public CaptioningCustomController(Context context, String preferenceKey) {
        super(context, preferenceKey);
        mCaptionHelper = new CaptionHelper(context);
        mContentResolver = context.getContentResolver();
        mSettingsContentObserver = new AccessibilitySettingsContentObserver(
                new Handler(Looper.getMainLooper()));
        mSettingsContentObserver.registerKeysToObserverCallback(CAPTIONING_FEATURE_KEYS,
                key -> refreshShowingCustom());
        this(context, preferenceKey, new CaptionHelper(context),
                new AccessibilitySettingsContentObserver(new Handler(Looper.getMainLooper())));
    }

    @VisibleForTesting
    CaptioningCustomController(Context context, String preferenceKey,
    CaptioningCustomController(
            Context context, String preferenceKey, CaptionHelper captionHelper,
            AccessibilitySettingsContentObserver contentObserver) {
        this(context, preferenceKey);
        super(context, preferenceKey);
        mCaptionHelper = new CaptionHelper(context);
        mContentResolver = context.getContentResolver();
        mSettingsContentObserver = contentObserver;
        mSettingsContentObserver.registerKeysToObserverCallback(CAPTIONING_FEATURE_KEYS, key -> {
            if (mCustom != null) {
                mCustom.setVisible(shouldShowPreference());
            }
        });
    }

    @Override
    public int getAvailabilityStatus() {
        if (com.android.settings.accessibility.Flags.fixA11ySettingsSearch()) {
            return (shouldShowPreference()) ? AVAILABLE : AVAILABLE_UNSEARCHABLE;
        } else {
            return AVAILABLE;
        }
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mCustom = screen.findPreference(getPreferenceKey());
        refreshShowingCustom();
        if (mCustom != null) {
            mCustom.setVisible(shouldShowPreference());
        }
    }

    @Override
@@ -88,9 +99,7 @@ public class CaptioningCustomController extends BasePreferenceController
        mSettingsContentObserver.unregister(mContentResolver);
    }

    private void refreshShowingCustom() {
        final boolean isCustomPreset =
                mCaptionHelper.getRawUserStyle() == CaptioningManager.CaptionStyle.PRESET_CUSTOM;
        mCustom.setVisible(isCustomPreset);
    private boolean shouldShowPreference() {
        return mCaptionHelper.getRawUserStyle() == CaptioningManager.CaptionStyle.PRESET_CUSTOM;
    }
}
+17 −3
Original line number Diff line number Diff line
@@ -19,10 +19,12 @@ package com.android.settings.accessibility;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.accessibility.CaptioningManager;
import android.view.accessibility.CaptioningManager.CaptionStyle;

import androidx.preference.PreferenceScreen;

import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
import com.android.settings.core.BasePreferenceController;
@@ -34,15 +36,27 @@ public class CaptioningWindowColorController extends BasePreferenceController
    private final CaptionHelper mCaptionHelper;
    private int mCachedNonDefaultOpacity = CaptionStyle.COLOR_UNSPECIFIED;

    public CaptioningWindowColorController(Context context, String preferenceKey) {
    @VisibleForTesting
    CaptioningWindowColorController(Context context, String preferenceKey,
            CaptionHelper captionHelper) {
        super(context, preferenceKey);
        mCaptionHelper = new CaptionHelper(context);
        mCaptionHelper = captionHelper;
    }

    public CaptioningWindowColorController(Context context, String preferenceKey) {
        this(context, preferenceKey, new CaptionHelper(context));
    }

    @Override
    public int getAvailabilityStatus() {
        if (com.android.settings.accessibility.Flags.fixA11ySettingsSearch()) {
            return (mCaptionHelper.getRawUserStyle()
                    == CaptioningManager.CaptionStyle.PRESET_CUSTOM)
                    ? AVAILABLE : AVAILABLE_UNSEARCHABLE;
        } else {
            return AVAILABLE;
        }
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
+17 −3
Original line number Diff line number Diff line
@@ -18,9 +18,11 @@ package com.android.settings.accessibility;

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

import androidx.preference.PreferenceScreen;

import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
import com.android.settings.core.BasePreferenceController;
@@ -31,15 +33,27 @@ public class CaptioningWindowOpacityController extends BasePreferenceController

    private final CaptionHelper mCaptionHelper;

    public CaptioningWindowOpacityController(Context context, String preferenceKey) {
    @VisibleForTesting
    CaptioningWindowOpacityController(Context context, String preferenceKey,
            CaptionHelper captionHelper) {
        super(context, preferenceKey);
        mCaptionHelper = new CaptionHelper(context);
        mCaptionHelper = captionHelper;
    }

    public CaptioningWindowOpacityController(Context context, String preferenceKey) {
        this(context, preferenceKey, new CaptionHelper(context));
    }

    @Override
    public int getAvailabilityStatus() {
        if (com.android.settings.accessibility.Flags.fixA11ySettingsSearch()) {
            return (mCaptionHelper.getRawUserStyle()
                    == CaptioningManager.CaptionStyle.PRESET_CUSTOM)
                    ? AVAILABLE : AVAILABLE_UNSEARCHABLE;
        } else {
            return AVAILABLE;
        }
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
+27 −1
Original line number Diff line number Diff line
@@ -23,6 +23,9 @@ import static org.mockito.Mockito.when;

import android.content.ContentResolver;
import android.content.Context;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.view.accessibility.CaptioningManager.CaptionStyle;

@@ -49,6 +52,8 @@ public class CaptioningCustomControllerTest {

    @Rule
    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
    @Mock
    private PreferenceScreen mScreen;
    @Mock
@@ -57,22 +62,43 @@ public class CaptioningCustomControllerTest {
    private ContentResolver mContentResolver;
    private CaptioningCustomController mController;
    private Preference mPreference;
    private CaptionHelper mCaptionHelper;

    @Before
    public void setUp() {
        mContentResolver = mContext.getContentResolver();
        mController = new CaptioningCustomController(mContext, PREF_KEY,
        mCaptionHelper = new CaptionHelper(mContext);
        mController = new CaptioningCustomController(mContext, PREF_KEY, mCaptionHelper,
                mAccessibilitySettingsContentObserver);
        mPreference = new Preference(mContext);
        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
    }

    @Test
    @DisableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH)
    public void getAvailabilityStatus_shouldReturnAvailable() {
        assertThat(mController.getAvailabilityStatus())
                .isEqualTo(BasePreferenceController.AVAILABLE);
    }

    @Test
    @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH)
    public void getAvailabilityStatus_customCaption_shouldReturnAvailable() {
        mCaptionHelper.setRawUserStyle(CaptionStyle.PRESET_CUSTOM);

        assertThat(mController.getAvailabilityStatus())
                .isEqualTo(BasePreferenceController.AVAILABLE);
    }

    @Test
    @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH)
    public void getAvailabilityStatus_notCustom_shouldReturnUnsearchable() {
        mCaptionHelper.setRawUserStyle(0);

        assertThat(mController.getAvailabilityStatus())
                .isEqualTo(BasePreferenceController.AVAILABLE_UNSEARCHABLE);
    }


    @Test
    public void displayPreference_byDefault_shouldIsInvisible() {
+28 −1
Original line number Diff line number Diff line
@@ -24,6 +24,9 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.accessibility.CaptioningManager;
@@ -53,16 +56,21 @@ public class CaptioningWindowColorControllerTest {

    @Rule
    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
    @Mock
    private PreferenceScreen mScreen;
    private final Context mContext = ApplicationProvider.getApplicationContext();
    private CaptioningWindowColorController mController;
    private ColorPreference mPreference;
    private ShadowCaptioningManager mShadowCaptioningManager;
    private CaptionHelper mCaptionHelper;

    @Before
    public void setUp() {
        mController = new CaptioningWindowColorController(mContext, "captioning_window_color");
        mCaptionHelper = new CaptionHelper(mContext);
        mController = new CaptioningWindowColorController(
                mContext, "captioning_window_color", mCaptionHelper);
        final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
        mPreference = new ColorPreference(mContext, attributeSet);
        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
@@ -71,11 +79,30 @@ public class CaptioningWindowColorControllerTest {
    }

    @Test
    @DisableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH)
    public void getAvailabilityStatus_shouldReturnAvailable() {
        assertThat(mController.getAvailabilityStatus())
                .isEqualTo(BasePreferenceController.AVAILABLE);
    }

    @Test
    @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH)
    public void getAvailabilityStatus_customCaption_shouldReturnAvailable() {
        mCaptionHelper.setRawUserStyle(CaptionStyle.PRESET_CUSTOM);

        assertThat(mController.getAvailabilityStatus())
                .isEqualTo(BasePreferenceController.AVAILABLE);
    }

    @Test
    @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH)
    public void getAvailabilityStatus_noCustom_shouldReturnUnsearchable() {
        mCaptionHelper.setRawUserStyle(0);

        assertThat(mController.getAvailabilityStatus())
                .isEqualTo(BasePreferenceController.AVAILABLE_UNSEARCHABLE);
    }

    @Test
    public void getSummary_defaultValue_shouldReturnNone() {
        mController.displayPreference(mScreen);
Loading