Merge changes I5409c1e8,I8ed80b44

* changes:
  Refactor CaptionAppearanceFragment to improve maintainability (4/n)
  Refactor CaptionAppearanceFragment to improve maintainability (3/n)
diff --git a/res/xml/captioning_appearance.xml b/res/xml/captioning_appearance.xml
index 6710ec6..b29d5a7 100644
--- a/res/xml/captioning_appearance.xml
+++ b/res/xml/captioning_appearance.xml
@@ -38,7 +38,8 @@
 
     <com.android.settings.accessibility.PresetPreference
         android:key="captioning_preset"
-        android:title="@string/captioning_preset"/>
+        android:title="@string/captioning_preset"
+        settings:controller="com.android.settings.accessibility.CaptionPresetController"/>
 
     <PreferenceCategory
         android:key="custom"
@@ -54,40 +55,47 @@
 
         <com.android.settings.accessibility.ColorPreference
             android:key="captioning_foreground_color"
-            android:title="@string/captioning_foreground_color"/>
+            android:title="@string/captioning_foreground_color"
+            settings:controller="com.android.settings.accessibility.CaptionForegroundColorController"/>
 
         <com.android.settings.accessibility.ColorPreference
             android:dependency="captioning_foreground_color"
             android:key="captioning_foreground_opacity"
-            android:title="@string/captioning_foreground_opacity"/>
+            android:title="@string/captioning_foreground_opacity"
+            settings:controller="com.android.settings.accessibility.CaptionForegroundOpacityController"/>
 
         <com.android.settings.accessibility.EdgeTypePreference
             android:key="captioning_edge_type"
-            android:title="@string/captioning_edge_type"/>
+            android:title="@string/captioning_edge_type"
+            settings:controller="com.android.settings.accessibility.CaptionEdgeTypeController"/>
 
         <com.android.settings.accessibility.ColorPreference
             android:dependency="captioning_edge_type"
             android:key="captioning_edge_color"
-            android:title="@string/captioning_edge_color"/>
+            android:title="@string/captioning_edge_color"
+            settings:controller="com.android.settings.accessibility.CaptionEdgeColorController"/>
 
         <com.android.settings.accessibility.ColorPreference
             android:key="captioning_background_color"
-            android:title="@string/captioning_background_color"/>
+            android:title="@string/captioning_background_color"
+            settings:controller="com.android.settings.accessibility.CaptionBackgroundColorController"/>
 
         <com.android.settings.accessibility.ColorPreference
             android:dependency="captioning_background_color"
             android:key="captioning_background_opacity"
-            android:title="@string/captioning_background_opacity"/>
+            android:title="@string/captioning_background_opacity"
+            settings:controller="com.android.settings.accessibility.CaptionBackgroundOpacityController"/>
 
         <com.android.settings.accessibility.ColorPreference
             android:key="captioning_window_color"
-            android:title="@string/captioning_window_color"/>
+            android:title="@string/captioning_window_color"
+            settings:controller="com.android.settings.accessibility.CaptionWindowColorController"/>
 
         <com.android.settings.accessibility.ColorPreference
             android:dependency="captioning_window_color"
             android:key="captioning_window_opacity"
-            android:title="@string/captioning_window_opacity"/>
-
+            android:title="@string/captioning_window_opacity"
+            settings:controller="com.android.settings.accessibility.CaptionWindowOpacityController"/>
     </PreferenceCategory>
 
     <com.android.settings.accessibility.AccessibilityFooterPreference
diff --git a/src/com/android/settings/accessibility/CaptionAppearanceFragment.java b/src/com/android/settings/accessibility/CaptionAppearanceFragment.java
index da529d3..b0f9138 100644
--- a/src/com/android/settings/accessibility/CaptionAppearanceFragment.java
+++ b/src/com/android/settings/accessibility/CaptionAppearanceFragment.java
@@ -17,58 +17,42 @@
 package com.android.settings.accessibility;
 
 import android.app.settings.SettingsEnums;
-import android.content.ContentResolver;
-import android.content.Context;
-import android.content.res.Resources;
-import android.graphics.Color;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
 import android.provider.Settings;
 import android.view.accessibility.CaptioningManager;
+import android.view.accessibility.CaptioningManager.CaptionStyle;
 
+import androidx.annotation.VisibleForTesting;
 import androidx.preference.PreferenceCategory;
 
 import com.android.settings.R;
-import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settingslib.search.SearchIndexable;
 
+import java.util.Arrays;
+import java.util.List;
+
 /** Settings fragment containing font style of captioning properties. */
 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
-public class CaptionAppearanceFragment extends DashboardFragment
-        implements OnValueChangedListener {
+public class CaptionAppearanceFragment extends DashboardFragment {
 
     private static final String TAG = "CaptionAppearanceFragment";
-    private static final String PREF_BACKGROUND_COLOR = "captioning_background_color";
-    private static final String PREF_BACKGROUND_OPACITY = "captioning_background_opacity";
-    private static final String PREF_FOREGROUND_COLOR = "captioning_foreground_color";
-    private static final String PREF_FOREGROUND_OPACITY = "captioning_foreground_opacity";
-    private static final String PREF_WINDOW_COLOR = "captioning_window_color";
-    private static final String PREF_WINDOW_OPACITY = "captioning_window_opacity";
-    private static final String PREF_EDGE_COLOR = "captioning_edge_color";
-    private static final String PREF_EDGE_TYPE = "captioning_edge_type";
-    private static final String PREF_PRESET = "captioning_preset";
-    private static final String PREF_CUSTOM = "custom";
+    @VisibleForTesting
+    static final String PREF_CUSTOM = "custom";
+    @VisibleForTesting
+    static final List<String> CAPTIONING_FEATURE_KEYS = Arrays.asList(
+            Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET
+    );
 
+    private final Handler mHandler = new Handler(Looper.getMainLooper());
+    @VisibleForTesting
+    AccessibilitySettingsContentObserver mSettingsContentObserver;
     private CaptioningManager mCaptioningManager;
-    private CaptionHelper mCaptionHelper;
-
-    // Standard options.
-    private PresetPreference mPreset;
-
-    // Custom options.
-    private ColorPreference mForegroundColor;
-    private ColorPreference mForegroundOpacity;
-    private EdgeTypePreference mEdgeType;
-    private ColorPreference mEdgeColor;
-    private ColorPreference mBackgroundColor;
-    private ColorPreference mBackgroundOpacity;
-    private ColorPreference mWindowColor;
-    private ColorPreference mWindowOpacity;
     private PreferenceCategory mCustom;
 
-    private boolean mShowingCustom;
-
     @Override
     public int getMetricsCategory() {
         return SettingsEnums.ACCESSIBILITY_CAPTION_APPEARANCE;
@@ -77,14 +61,24 @@
     @Override
     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
         super.onCreatePreferences(savedInstanceState, rootKey);
-
-        mCaptioningManager = (CaptioningManager) getSystemService(Context.CAPTIONING_SERVICE);
-        mCaptionHelper = new CaptionHelper(getContext());
-
-        initializeAllPreferences();
-        updateAllPreferences();
+        mCaptioningManager = getContext().getSystemService(CaptioningManager.class);
+        mSettingsContentObserver = new AccessibilitySettingsContentObserver(mHandler);
+        mSettingsContentObserver.registerKeysToObserverCallback(CAPTIONING_FEATURE_KEYS,
+                key -> refreshShowingCustom());
+        mCustom = findPreference(PREF_CUSTOM);
         refreshShowingCustom();
-        installUpdateListeners();
+    }
+
+    @Override
+    public void onStart() {
+        super.onStart();
+        mSettingsContentObserver.register(getContext().getContentResolver());
+    }
+
+    @Override
+    public void onStop() {
+        super.onStop();
+        getContext().getContentResolver().unregisterContentObserver(mSettingsContentObserver);
     }
 
     @Override
@@ -97,186 +91,17 @@
         return TAG;
     }
 
-    private void initializeAllPreferences() {
-
-        final Resources res = getResources();
-        final int[] presetValues = res.getIntArray(R.array.captioning_preset_selector_values);
-        final String[] presetTitles = res.getStringArray(R.array.captioning_preset_selector_titles);
-        mPreset = (PresetPreference) findPreference(PREF_PRESET);
-        mPreset.setValues(presetValues);
-        mPreset.setTitles(presetTitles);
-
-        mCustom = (PreferenceCategory) findPreference(PREF_CUSTOM);
-        mShowingCustom = true;
-
-        final int[] colorValues = res.getIntArray(R.array.captioning_color_selector_values);
-        final String[] colorTitles = res.getStringArray(R.array.captioning_color_selector_titles);
-        mForegroundColor = (ColorPreference) mCustom.findPreference(PREF_FOREGROUND_COLOR);
-        mForegroundColor.setTitles(colorTitles);
-        mForegroundColor.setValues(colorValues);
-
-        final int[] opacityValues = res.getIntArray(R.array.captioning_opacity_selector_values);
-        final String[] opacityTitles = res.getStringArray(
-                R.array.captioning_opacity_selector_titles);
-        mForegroundOpacity = (ColorPreference) mCustom.findPreference(PREF_FOREGROUND_OPACITY);
-        mForegroundOpacity.setTitles(opacityTitles);
-        mForegroundOpacity.setValues(opacityValues);
-
-        mEdgeColor = (ColorPreference) mCustom.findPreference(PREF_EDGE_COLOR);
-        mEdgeColor.setTitles(colorTitles);
-        mEdgeColor.setValues(colorValues);
-
-        // Add "none" as an additional option for backgrounds.
-        final int[] bgColorValues = new int[colorValues.length + 1];
-        final String[] bgColorTitles = new String[colorTitles.length + 1];
-        System.arraycopy(colorValues, 0, bgColorValues, 1, colorValues.length);
-        System.arraycopy(colorTitles, 0, bgColorTitles, 1, colorTitles.length);
-        bgColorValues[0] = Color.TRANSPARENT;
-        bgColorTitles[0] = getString(R.string.color_none);
-        mBackgroundColor = (ColorPreference) mCustom.findPreference(PREF_BACKGROUND_COLOR);
-        mBackgroundColor.setTitles(bgColorTitles);
-        mBackgroundColor.setValues(bgColorValues);
-
-        mBackgroundOpacity = (ColorPreference) mCustom.findPreference(PREF_BACKGROUND_OPACITY);
-        mBackgroundOpacity.setTitles(opacityTitles);
-        mBackgroundOpacity.setValues(opacityValues);
-
-        mWindowColor = (ColorPreference) mCustom.findPreference(PREF_WINDOW_COLOR);
-        mWindowColor.setTitles(bgColorTitles);
-        mWindowColor.setValues(bgColorValues);
-
-        mWindowOpacity = (ColorPreference) mCustom.findPreference(PREF_WINDOW_OPACITY);
-        mWindowOpacity.setTitles(opacityTitles);
-        mWindowOpacity.setValues(opacityValues);
-
-        mEdgeType = (EdgeTypePreference) mCustom.findPreference(PREF_EDGE_TYPE);
-    }
-
-    private void installUpdateListeners() {
-        mPreset.setOnValueChangedListener(this);
-        mForegroundColor.setOnValueChangedListener(this);
-        mForegroundOpacity.setOnValueChangedListener(this);
-        mEdgeColor.setOnValueChangedListener(this);
-        mBackgroundColor.setOnValueChangedListener(this);
-        mBackgroundOpacity.setOnValueChangedListener(this);
-        mWindowColor.setOnValueChangedListener(this);
-        mWindowOpacity.setOnValueChangedListener(this);
-        mEdgeType.setOnValueChangedListener(this);
-    }
-
-    private void updateAllPreferences() {
-        final int preset = mCaptioningManager.getRawUserStyle();
-        mPreset.setValue(preset);
-
-        final ContentResolver cr = getContentResolver();
-        final CaptioningManager.CaptionStyle attrs = CaptioningManager.CaptionStyle.getCustomStyle(
-                cr);
-        mEdgeType.setValue(attrs.edgeType);
-        mEdgeColor.setValue(attrs.edgeColor);
-
-        final int foregroundColor = attrs.hasForegroundColor() ? attrs.foregroundColor
-                : CaptioningManager.CaptionStyle.COLOR_UNSPECIFIED;
-        parseColorOpacity(mForegroundColor, mForegroundOpacity, foregroundColor);
-
-        final int backgroundColor = attrs.hasBackgroundColor() ? attrs.backgroundColor
-                : CaptioningManager.CaptionStyle.COLOR_UNSPECIFIED;
-        parseColorOpacity(mBackgroundColor, mBackgroundOpacity, backgroundColor);
-
-        final int windowColor = attrs.hasWindowColor() ? attrs.windowColor
-                : CaptioningManager.CaptionStyle.COLOR_UNSPECIFIED;
-        parseColorOpacity(mWindowColor, mWindowOpacity, windowColor);
-    }
-
-    /**
-     * Unpacks the specified color value and update the preferences.
-     *
-     * @param color   color preference
-     * @param opacity opacity preference
-     * @param value   packed value
-     */
-    private void parseColorOpacity(ColorPreference color, ColorPreference opacity, int value) {
-        final int colorValue;
-        final int opacityValue;
-        if (!CaptioningManager.CaptionStyle.hasColor(value)) {
-            // "Default" color with variable alpha.
-            colorValue = CaptioningManager.CaptionStyle.COLOR_UNSPECIFIED;
-            opacityValue = (value & 0xFF) << 24;
-        } else if ((value >>> 24) == 0) {
-            // "None" color with variable alpha.
-            colorValue = Color.TRANSPARENT;
-            opacityValue = (value & 0xFF) << 24;
-        } else {
-            // Normal color.
-            colorValue = value | 0xFF000000;
-            opacityValue = value & 0xFF000000;
-        }
-
-        // Opacity value is always white.
-        opacity.setValue(opacityValue | 0xFFFFFF);
-        color.setValue(colorValue);
-    }
-
-    private int mergeColorOpacity(ColorPreference color, ColorPreference opacity) {
-        final int colorValue = color.getValue();
-        final int opacityValue = opacity.getValue();
-        final int value;
-        // "Default" is 0x00FFFFFF or, for legacy support, 0x00000100.
-        if (!CaptioningManager.CaptionStyle.hasColor(colorValue)) {
-            // Encode "default" as 0x00FFFFaa.
-            value = 0x00FFFF00 | Color.alpha(opacityValue);
-        } else if (colorValue == Color.TRANSPARENT) {
-            // Encode "none" as 0x000000aa.
-            value = Color.alpha(opacityValue);
-        } else {
-            // Encode custom color normally.
-            value = colorValue & 0x00FFFFFF | opacityValue & 0xFF000000;
-        }
-        return value;
-    }
-
-    private void refreshShowingCustom() {
-        final boolean customPreset =
-                mPreset.getValue() == CaptioningManager.CaptionStyle.PRESET_CUSTOM;
-        if (!customPreset && mShowingCustom) {
-            getPreferenceScreen().removePreference(mCustom);
-            mShowingCustom = false;
-        } else if (customPreset && !mShowingCustom) {
-            getPreferenceScreen().addPreference(mCustom);
-            mShowingCustom = true;
-        }
-    }
-
-    @Override
-    public void onValueChanged(ListDialogPreference preference, int value) {
-        final ContentResolver cr = getActivity().getContentResolver();
-        if (mForegroundColor == preference || mForegroundOpacity == preference) {
-            final int merged = mergeColorOpacity(mForegroundColor, mForegroundOpacity);
-            Settings.Secure.putInt(
-                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, merged);
-        } else if (mBackgroundColor == preference || mBackgroundOpacity == preference) {
-            final int merged = mergeColorOpacity(mBackgroundColor, mBackgroundOpacity);
-            Settings.Secure.putInt(
-                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, merged);
-        } else if (mWindowColor == preference || mWindowOpacity == preference) {
-            final int merged = mergeColorOpacity(mWindowColor, mWindowOpacity);
-            Settings.Secure.putInt(
-                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, merged);
-        } else if (mEdgeColor == preference) {
-            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_COLOR, value);
-        } else if (mPreset == preference) {
-            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, value);
-            refreshShowingCustom();
-        } else if (mEdgeType == preference) {
-            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_TYPE, value);
-        }
-        mCaptionHelper.setEnabled(true);
-    }
-
     @Override
     public int getHelpResource() {
         return R.string.help_url_caption;
     }
 
+    private void refreshShowingCustom() {
+        final boolean isCustomPreset =
+                mCaptioningManager.getRawUserStyle() == CaptionStyle.PRESET_CUSTOM;
+        mCustom.setVisible(isCustomPreset);
+    }
+
     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
             new BaseSearchIndexProvider(R.xml.captioning_appearance);
 }
diff --git a/src/com/android/settings/accessibility/CaptionBackgroundColorController.java b/src/com/android/settings/accessibility/CaptionBackgroundColorController.java
new file mode 100644
index 0000000..f477c9c
--- /dev/null
+++ b/src/com/android/settings/accessibility/CaptionBackgroundColorController.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2022 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 android.content.Context;
+import android.content.res.Resources;
+import android.graphics.Color;
+
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.R;
+import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
+import com.android.settings.core.BasePreferenceController;
+
+/** Preference controller for caption background color. */
+public class CaptionBackgroundColorController extends BasePreferenceController
+        implements OnValueChangedListener {
+
+    private final CaptionHelper mCaptionHelper;
+
+    public CaptionBackgroundColorController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+        mCaptionHelper = new CaptionHelper(context);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AVAILABLE;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        final ColorPreference preference = screen.findPreference(getPreferenceKey());
+        final Resources res = mContext.getResources();
+        final int[] colorValues = res.getIntArray(R.array.captioning_color_selector_values);
+        final String[] colorTitles = res.getStringArray(
+                R.array.captioning_color_selector_titles);
+        // Add "none" as an additional option for backgrounds.
+        final int[] backgroundColorValues = new int[colorValues.length + 1];
+        final String[] backgroundColorTitles = new String[colorTitles.length + 1];
+        System.arraycopy(colorValues, 0, backgroundColorValues, 1, colorValues.length);
+        System.arraycopy(colorTitles, 0, backgroundColorTitles, 1, colorTitles.length);
+        backgroundColorValues[0] = Color.TRANSPARENT;
+        backgroundColorTitles[0] = mContext.getString(R.string.color_none);
+        preference.setTitles(backgroundColorTitles);
+        preference.setValues(backgroundColorValues);
+        final int backBackgroundColor = mCaptionHelper.getBackgroundColor();
+        final int color = CaptionUtils.parseColor(backBackgroundColor);
+        preference.setValue(color);
+        preference.setOnValueChangedListener(this);
+    }
+
+    @Override
+    public void onValueChanged(ListDialogPreference preference, int value) {
+        final int backBackgroundColor = mCaptionHelper.getBackgroundColor();
+        final int opacity = CaptionUtils.parseOpacity(backBackgroundColor);
+        final int merged = CaptionUtils.mergeColorOpacity(value, opacity);
+        mCaptionHelper.setBackgroundColor(merged);
+        mCaptionHelper.setEnabled(true);
+    }
+}
diff --git a/src/com/android/settings/accessibility/CaptionBackgroundOpacityController.java b/src/com/android/settings/accessibility/CaptionBackgroundOpacityController.java
new file mode 100644
index 0000000..5aeb1dd
--- /dev/null
+++ b/src/com/android/settings/accessibility/CaptionBackgroundOpacityController.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2022 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 android.content.Context;
+import android.content.res.Resources;
+
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.R;
+import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
+import com.android.settings.core.BasePreferenceController;
+
+/** Preference controller for caption background opacity. */
+public class CaptionBackgroundOpacityController extends BasePreferenceController
+        implements OnValueChangedListener {
+
+    private final CaptionHelper mCaptionHelper;
+
+    public CaptionBackgroundOpacityController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+        mCaptionHelper = new CaptionHelper(context);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AVAILABLE;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        final ColorPreference preference = screen.findPreference(getPreferenceKey());
+        final Resources res = mContext.getResources();
+        final int[] opacityValues = res.getIntArray(R.array.captioning_opacity_selector_values);
+        final String[] opacityTitles = res.getStringArray(
+                R.array.captioning_opacity_selector_titles);
+        preference.setTitles(opacityTitles);
+        preference.setValues(opacityValues);
+        final int backBackgroundColor = mCaptionHelper.getBackgroundColor();
+        final int opacity = CaptionUtils.parseOpacity(backBackgroundColor);
+        preference.setValue(opacity);
+        preference.setOnValueChangedListener(this);
+    }
+
+    @Override
+    public void onValueChanged(ListDialogPreference preference, int value) {
+        final int backBackgroundColor = mCaptionHelper.getBackgroundColor();
+        final int color = CaptionUtils.parseColor(backBackgroundColor);
+        final int merged = CaptionUtils.mergeColorOpacity(color, value);
+        mCaptionHelper.setBackgroundColor(merged);
+        mCaptionHelper.setEnabled(true);
+    }
+}
diff --git a/src/com/android/settings/accessibility/CaptionEdgeColorController.java b/src/com/android/settings/accessibility/CaptionEdgeColorController.java
new file mode 100644
index 0000000..2235994
--- /dev/null
+++ b/src/com/android/settings/accessibility/CaptionEdgeColorController.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2022 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 android.content.Context;
+import android.content.res.Resources;
+
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.R;
+import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
+import com.android.settings.core.BasePreferenceController;
+
+/** Preference controller for caption edge color. */
+public class CaptionEdgeColorController extends BasePreferenceController
+        implements OnValueChangedListener {
+
+    private final CaptionHelper mCaptionHelper;
+
+    public CaptionEdgeColorController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+        mCaptionHelper = new CaptionHelper(context);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AVAILABLE;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        final ColorPreference preference = screen.findPreference(getPreferenceKey());
+        final Resources res = mContext.getResources();
+        final int[] colorValues = res.getIntArray(R.array.captioning_color_selector_values);
+        final String[] colorTitles = res.getStringArray(
+                R.array.captioning_color_selector_titles);
+        preference.setTitles(colorTitles);
+        preference.setValues(colorValues);
+        preference.setValue(mCaptionHelper.getEdgeColor());
+        preference.setOnValueChangedListener(this);
+    }
+
+    @Override
+    public void onValueChanged(ListDialogPreference preference, int value) {
+        mCaptionHelper.setEdgeColor(value);
+        mCaptionHelper.setEnabled(true);
+    }
+}
diff --git a/src/com/android/settings/accessibility/CaptionEdgeTypeController.java b/src/com/android/settings/accessibility/CaptionEdgeTypeController.java
new file mode 100644
index 0000000..031dfaa
--- /dev/null
+++ b/src/com/android/settings/accessibility/CaptionEdgeTypeController.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2022 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 android.content.Context;
+
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
+import com.android.settings.core.BasePreferenceController;
+
+/** Preference controller for caption edge type. */
+public class CaptionEdgeTypeController extends BasePreferenceController
+        implements OnValueChangedListener {
+
+    private final CaptionHelper mCaptionHelper;
+
+    public CaptionEdgeTypeController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+        mCaptionHelper = new CaptionHelper(context);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AVAILABLE;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        final EdgeTypePreference preference = screen.findPreference(getPreferenceKey());
+        preference.setValue(mCaptionHelper.getEdgeType());
+        preference.setOnValueChangedListener(this);
+    }
+
+    @Override
+    public void onValueChanged(ListDialogPreference preference, int value) {
+        mCaptionHelper.setEdgeType(value);
+        mCaptionHelper.setEnabled(true);
+    }
+}
diff --git a/src/com/android/settings/accessibility/CaptionForegroundColorController.java b/src/com/android/settings/accessibility/CaptionForegroundColorController.java
new file mode 100644
index 0000000..b8618f3
--- /dev/null
+++ b/src/com/android/settings/accessibility/CaptionForegroundColorController.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2022 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 android.content.Context;
+import android.content.res.Resources;
+
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.R;
+import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
+import com.android.settings.core.BasePreferenceController;
+
+/** Preference controller for caption foreground color. */
+public class CaptionForegroundColorController extends BasePreferenceController
+        implements OnValueChangedListener {
+
+    private final CaptionHelper mCaptionHelper;
+
+    public CaptionForegroundColorController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+        mCaptionHelper = new CaptionHelper(context);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AVAILABLE;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        final ColorPreference preference = screen.findPreference(getPreferenceKey());
+        final Resources res = mContext.getResources();
+        final int[] colorValues = res.getIntArray(R.array.captioning_color_selector_values);
+        final String[] colorTitles = res.getStringArray(
+                R.array.captioning_color_selector_titles);
+        preference.setTitles(colorTitles);
+        preference.setValues(colorValues);
+        final int foregroundColor = mCaptionHelper.getForegroundColor();
+        final int color = CaptionUtils.parseColor(foregroundColor);
+        preference.setValue(color);
+        preference.setOnValueChangedListener(this);
+    }
+
+    @Override
+    public void onValueChanged(ListDialogPreference preference, int value) {
+        final int foregroundColor = mCaptionHelper.getForegroundColor();
+        final int opacity = CaptionUtils.parseOpacity(foregroundColor);
+        final int merged = CaptionUtils.mergeColorOpacity(value, opacity);
+        mCaptionHelper.setForegroundColor(merged);
+        mCaptionHelper.setEnabled(true);
+    }
+}
diff --git a/src/com/android/settings/accessibility/CaptionForegroundOpacityController.java b/src/com/android/settings/accessibility/CaptionForegroundOpacityController.java
new file mode 100644
index 0000000..7f31704
--- /dev/null
+++ b/src/com/android/settings/accessibility/CaptionForegroundOpacityController.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2022 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 android.content.Context;
+import android.content.res.Resources;
+
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.R;
+import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
+import com.android.settings.core.BasePreferenceController;
+
+/** Preference controller for caption foreground opacity. */
+public class CaptionForegroundOpacityController extends BasePreferenceController
+        implements OnValueChangedListener {
+
+    private final CaptionHelper mCaptionHelper;
+
+    public CaptionForegroundOpacityController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+        mCaptionHelper = new CaptionHelper(context);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AVAILABLE;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        final ColorPreference preference = screen.findPreference(getPreferenceKey());
+        final Resources res = mContext.getResources();
+        final int[] opacityValues = res.getIntArray(R.array.captioning_opacity_selector_values);
+        final String[] opacityTitles = res.getStringArray(
+                R.array.captioning_opacity_selector_titles);
+        preference.setTitles(opacityTitles);
+        preference.setValues(opacityValues);
+        final int foregroundColor = mCaptionHelper.getForegroundColor();
+        final int opacity = CaptionUtils.parseOpacity(foregroundColor);
+        preference.setValue(opacity);
+        preference.setOnValueChangedListener(this);
+    }
+
+    @Override
+    public void onValueChanged(ListDialogPreference preference, int value) {
+        final int foregroundColor = mCaptionHelper.getForegroundColor();
+        final int color = CaptionUtils.parseColor(foregroundColor);
+        final int merged = CaptionUtils.mergeColorOpacity(color, value);
+        mCaptionHelper.setForegroundColor(merged);
+        mCaptionHelper.setEnabled(true);
+    }
+}
diff --git a/src/com/android/settings/accessibility/CaptionHelper.java b/src/com/android/settings/accessibility/CaptionHelper.java
index c833272..eb76b6d 100644
--- a/src/com/android/settings/accessibility/CaptionHelper.java
+++ b/src/com/android/settings/accessibility/CaptionHelper.java
@@ -19,10 +19,12 @@
 import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
 import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
 
+import android.content.ContentResolver;
 import android.content.Context;
 import android.provider.Settings;
 import android.view.View;
 import android.view.accessibility.CaptioningManager;
+import android.view.accessibility.CaptioningManager.CaptionStyle;
 
 import com.android.internal.widget.SubtitleView;
 import com.android.settings.R;
@@ -32,9 +34,7 @@
 
 import java.util.Locale;
 
-/**
- * Helper class for caption.
- */
+/** Helper class for caption. */
 public class CaptionHelper {
 
     /* WebVtt specifies line height as 5.3% of the viewport height. */
@@ -42,10 +42,12 @@
     static final float LINE_HEIGHT_RATIO = 0.0533f;
 
     private final Context mContext;
+    private final ContentResolver mContentResolver;
     private final CaptioningManager mCaptioningManager;
 
     public CaptionHelper(Context context) {
         mContext = context;
+        mContentResolver = mContext.getContentResolver();
         mCaptioningManager = context.getSystemService(CaptioningManager.class);
     }
 
@@ -104,4 +106,99 @@
             previewText.setText(R.string.captioning_preview_characters);
         }
     }
+
+    /**
+     * Sets the user's preferred captioning background color.
+     *
+     * @param color The captioning background color
+     */
+    public void setBackgroundColor(int color) {
+        Settings.Secure.putInt(mContentResolver,
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, color);
+    }
+
+    /** Returns the captioning background color.*/
+    public int getBackgroundColor() {
+        final CaptionStyle attrs = CaptionStyle.getCustomStyle(mContentResolver);
+        return attrs.hasBackgroundColor() ? attrs.backgroundColor : CaptionStyle.COLOR_UNSPECIFIED;
+    }
+
+    /**
+     * Sets the user's preferred captioning foreground color.
+     *
+     * @param color The captioning foreground color
+     */
+    public void setForegroundColor(int color) {
+        Settings.Secure.putInt(mContentResolver,
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, color);
+    }
+
+    /** Returns the captioning foreground color.*/
+    public int getForegroundColor() {
+        final CaptionStyle attrs = CaptionStyle.getCustomStyle(mContentResolver);
+        return attrs.hasForegroundColor() ? attrs.foregroundColor : CaptionStyle.COLOR_UNSPECIFIED;
+    }
+
+    /**
+     * Sets the user's preferred captioning window color.
+     *
+     * @param color The captioning window color
+     */
+    public void setWindowColor(int color) {
+        Settings.Secure.putInt(mContentResolver,
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, color);
+    }
+
+    /** Returns the captioning window color.*/
+    public int getWindowColor() {
+        final CaptionStyle attrs = CaptionStyle.getCustomStyle(mContentResolver);
+        return attrs.hasWindowColor() ? attrs.windowColor : CaptionStyle.COLOR_UNSPECIFIED;
+    }
+
+    /**
+     * Sets the user's preferred captioning edge color.
+     *
+     * @param color The captioning edge color
+     */
+    public void setEdgeColor(int color) {
+        Settings.Secure.putInt(mContentResolver,
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_COLOR, color);
+    }
+
+    /** Returns the captioning edge color.*/
+    public int getEdgeColor() {
+        final CaptionStyle attrs = CaptionStyle.getCustomStyle(mContentResolver);
+        return attrs.edgeColor;
+    }
+
+    /**
+     * Sets the user's preferred captioning edge type.
+     *
+     * @param type The captioning edge type
+     */
+    public void setEdgeType(int type) {
+        Settings.Secure.putInt(mContentResolver,
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_TYPE, type);
+    }
+
+    /** Returns the captioning edge type.*/
+    public int getEdgeType() {
+        final CaptionStyle attrs = CaptionStyle.getCustomStyle(mContentResolver);
+        return attrs.edgeType;
+    }
+
+    /**
+     * Sets the caption raw user style.
+     *
+     * @param type The caption raw user style
+     */
+    public void setRawUserStyle(int type) {
+        Settings.Secure.putInt(mContentResolver,
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, type);
+    }
+
+    /** Returns the caption raw user style.*/
+    public int getRawUserStyle() {
+        return mCaptioningManager.getRawUserStyle();
+    }
 }
diff --git a/src/com/android/settings/accessibility/CaptionPresetController.java b/src/com/android/settings/accessibility/CaptionPresetController.java
new file mode 100644
index 0000000..db21f0b
--- /dev/null
+++ b/src/com/android/settings/accessibility/CaptionPresetController.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2022 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 android.content.Context;
+import android.content.res.Resources;
+
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+
+/** Preference controller for caption preset. */
+public class CaptionPresetController extends BasePreferenceController
+        implements ListDialogPreference.OnValueChangedListener {
+
+    private final CaptionHelper mCaptionHelper;
+
+    public CaptionPresetController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+        mCaptionHelper = new CaptionHelper(context);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AVAILABLE;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        final PresetPreference preference = screen.findPreference(getPreferenceKey());
+        final Resources res = mContext.getResources();
+        final int[] presetValues = res.getIntArray(R.array.captioning_preset_selector_values);
+        final String[] presetTitles = res.getStringArray(R.array.captioning_preset_selector_titles);
+        preference.setTitles(presetTitles);
+        preference.setValues(presetValues);
+        final int preset = mCaptionHelper.getRawUserStyle();
+        preference.setValue(preset);
+        preference.setOnValueChangedListener(this);
+    }
+
+    @Override
+    public void onValueChanged(ListDialogPreference preference, int value) {
+        mCaptionHelper.setRawUserStyle(value);
+        mCaptionHelper.setEnabled(true);
+    }
+}
diff --git a/src/com/android/settings/accessibility/CaptionUtils.java b/src/com/android/settings/accessibility/CaptionUtils.java
new file mode 100644
index 0000000..3a80133
--- /dev/null
+++ b/src/com/android/settings/accessibility/CaptionUtils.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2022 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 android.graphics.Color;
+import android.view.accessibility.CaptioningManager.CaptionStyle;
+
+/** Provides utility methods related caption. */
+public final class CaptionUtils {
+
+    /**
+     * Unpacks the specified color value to get the color value.
+     *
+     * @param value the specified color value.
+     */
+    public static int parseColor(int value) {
+        final int colorValue;
+        if (!CaptionStyle.hasColor(value)) {
+            // "Default" color with variable alpha.
+            colorValue = CaptionStyle.COLOR_UNSPECIFIED;
+        } else if ((value >>> 24) == 0) {
+            // "None" color with variable alpha.
+            colorValue = Color.TRANSPARENT;
+        } else {
+            // Normal color.
+            colorValue = value | 0xFF000000;
+        }
+        return colorValue;
+    }
+
+    /**
+     * Unpacks the specified color value to get the opacity value.
+     *
+     * @param value the specified color value.
+     */
+    public static int parseOpacity(int value) {
+        final int opacityValue;
+        if (!CaptionStyle.hasColor(value)) {
+            // "Default" color with variable alpha.
+            opacityValue = (value & 0xFF) << 24;
+        } else if ((value >>> 24) == 0) {
+            // "None" color with variable alpha.
+            opacityValue = (value & 0xFF) << 24;
+        } else {
+            // Normal color.
+            opacityValue = value & 0xFF000000;
+        }
+
+        // Opacity value is always white.
+        return opacityValue | 0xFFFFFF;
+    }
+
+    /**
+     * Packs the specified color value and specified opacity value into merged color value.
+     *
+     * @param colorValue the color value.
+     * @param opacityValue the opacity value.
+     */
+    public static int mergeColorOpacity(int colorValue, int opacityValue) {
+        final int value;
+        // "Default" is 0x00FFFFFF or, for legacy support, 0x00000100.
+        if (!CaptionStyle.hasColor(colorValue)) {
+            // Encode "default" as 0x00FFFFaa.
+            value = 0x00FFFF00 | Color.alpha(opacityValue);
+        } else if (colorValue == Color.TRANSPARENT) {
+            // Encode "none" as 0x000000aa.
+            value = Color.alpha(opacityValue);
+        } else {
+            // Encode custom color normally.
+            value = (colorValue & 0x00FFFFFF) | (opacityValue & 0xFF000000);
+        }
+        return value;
+    }
+}
diff --git a/src/com/android/settings/accessibility/CaptionWindowColorController.java b/src/com/android/settings/accessibility/CaptionWindowColorController.java
new file mode 100644
index 0000000..f13a347
--- /dev/null
+++ b/src/com/android/settings/accessibility/CaptionWindowColorController.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2022 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 android.content.Context;
+import android.content.res.Resources;
+import android.graphics.Color;
+
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.R;
+import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
+import com.android.settings.core.BasePreferenceController;
+
+/** Preference controller for caption window color. */
+public class CaptionWindowColorController extends BasePreferenceController
+        implements OnValueChangedListener {
+
+    private final CaptionHelper mCaptionHelper;
+
+    public CaptionWindowColorController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+        mCaptionHelper = new CaptionHelper(context);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AVAILABLE;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        final ColorPreference preference = screen.findPreference(getPreferenceKey());
+        final Resources res = mContext.getResources();
+        final int[] colorValues = res.getIntArray(R.array.captioning_color_selector_values);
+        final String[] colorTitles = res.getStringArray(
+                R.array.captioning_color_selector_titles);
+        // Add "none" as an additional option for window backgrounds.
+        final int[] backgroundColorValues = new int[colorValues.length + 1];
+        final String[] backgroundColorTitles = new String[colorTitles.length + 1];
+        System.arraycopy(colorValues, 0, backgroundColorValues, 1, colorValues.length);
+        System.arraycopy(colorTitles, 0, backgroundColorTitles, 1, colorTitles.length);
+        backgroundColorValues[0] = Color.TRANSPARENT;
+        backgroundColorTitles[0] = mContext.getString(R.string.color_none);
+        preference.setTitles(backgroundColorTitles);
+        preference.setValues(backgroundColorValues);
+        final int windowColor = mCaptionHelper.getWindowColor();
+        final int color = CaptionUtils.parseColor(windowColor);
+        preference.setValue(color);
+        preference.setOnValueChangedListener(this);
+    }
+
+    @Override
+    public void onValueChanged(ListDialogPreference preference, int value) {
+        final int windowColor = mCaptionHelper.getWindowColor();
+        final int opacity = CaptionUtils.parseOpacity(windowColor);
+        final int merged = CaptionUtils.mergeColorOpacity(value, opacity);
+        mCaptionHelper.setWindowColor(merged);
+        mCaptionHelper.setEnabled(true);
+    }
+}
diff --git a/src/com/android/settings/accessibility/CaptionWindowOpacityController.java b/src/com/android/settings/accessibility/CaptionWindowOpacityController.java
new file mode 100644
index 0000000..8076a48
--- /dev/null
+++ b/src/com/android/settings/accessibility/CaptionWindowOpacityController.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2022 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 android.content.Context;
+import android.content.res.Resources;
+
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.R;
+import com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
+import com.android.settings.core.BasePreferenceController;
+
+/** Preference controller for caption window opacity. */
+public class  CaptionWindowOpacityController extends BasePreferenceController
+        implements OnValueChangedListener {
+
+    private final CaptionHelper mCaptionHelper;
+
+    public CaptionWindowOpacityController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+        mCaptionHelper = new CaptionHelper(context);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AVAILABLE;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        final ColorPreference preference = screen.findPreference(getPreferenceKey());
+        final Resources res = mContext.getResources();
+        final int[] opacityValues = res.getIntArray(R.array.captioning_opacity_selector_values);
+        final String[] opacityTitles = res.getStringArray(
+                R.array.captioning_opacity_selector_titles);
+        preference.setTitles(opacityTitles);
+        preference.setValues(opacityValues);
+        final int windowColor = mCaptionHelper.getWindowColor();
+        final int opacity = CaptionUtils.parseOpacity(windowColor);
+        preference.setValue(opacity);
+        preference.setOnValueChangedListener(this);
+    }
+
+    @Override
+    public void onValueChanged(ListDialogPreference preference, int value) {
+        final int windowColor = mCaptionHelper.getWindowColor();
+        final int color = CaptionUtils.parseColor(windowColor);
+        final int merged = CaptionUtils.mergeColorOpacity(color, value);
+        mCaptionHelper.setWindowColor(merged);
+        mCaptionHelper.setEnabled(true);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptionAppearanceFragmentTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptionAppearanceFragmentTest.java
new file mode 100644
index 0000000..3c320e2
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/CaptionAppearanceFragmentTest.java
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2022 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.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.app.settings.SettingsEnums;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.os.Bundle;
+import android.provider.Settings;
+import android.view.accessibility.CaptioningManager.CaptionStyle;
+
+import androidx.preference.PreferenceCategory;
+import androidx.preference.PreferenceManager;
+import androidx.preference.PreferenceScreen;
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.settings.R;
+import com.android.settings.SettingsActivity;
+import com.android.settings.testutils.XmlTestUtils;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Spy;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.util.ReflectionHelpers;
+
+import java.util.List;
+
+/** Tests for {@link CaptionAppearanceFragment}. */
+@RunWith(RobolectricTestRunner.class)
+public class CaptionAppearanceFragmentTest {
+
+    @Rule
+    public MockitoRule mMockitoRule = MockitoJUnit.rule();
+    @Mock
+    private SettingsActivity mActivity;
+    @Mock
+    private PreferenceScreen mScreen;
+    @Mock
+    private PreferenceManager mPreferenceManager;
+    @Mock
+    private ContentResolver mContentResolver;
+    @Mock
+    private PreferenceCategory mCustomPref;
+    @Spy
+    private Context mContext = ApplicationProvider.getApplicationContext();
+    private TestCaptionAppearanceFragment mFragment;
+
+    @Before
+    public void setUp() {
+        mFragment = spy(new TestCaptionAppearanceFragment());
+        doReturn(mActivity).when(mFragment).getActivity();
+        doReturn(mContext).when(mFragment).getContext();
+        doReturn(mCustomPref).when(mFragment).findPreference(mFragment.PREF_CUSTOM);
+        when(mPreferenceManager.getPreferenceScreen()).thenReturn(mScreen);
+        ReflectionHelpers.setField(mFragment, "mPreferenceManager", mPreferenceManager);
+    }
+
+    @Test
+    public void onCreatePreferences_shouldPreferenceIsInvisible() {
+        mFragment.onAttach(mContext);
+
+        mFragment.onCreatePreferences(Bundle.EMPTY, /* rootKey */ null);
+
+        verify(mCustomPref).setVisible(false);
+    }
+
+    @Test
+    public void onCreatePreferences_customValue_shouldPreferenceIsVisible() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, CaptionStyle.PRESET_CUSTOM);
+        mFragment.onAttach(mContext);
+
+        mFragment.onCreatePreferences(Bundle.EMPTY, /* rootKey */ null);
+
+        verify(mCustomPref).setVisible(true);
+    }
+
+    @Test
+    public void onStart_registerSpecificContentObserverForSpecificKeys() {
+        when(mContext.getContentResolver()).thenReturn(mContentResolver);
+        mFragment.onAttach(mContext);
+        mFragment.onCreatePreferences(Bundle.EMPTY, /* rootKey */ null);
+
+        mFragment.onStart();
+
+        for (String key : mFragment.CAPTIONING_FEATURE_KEYS) {
+            verify(mContentResolver).registerContentObserver(Settings.Secure.getUriFor(key),
+                    /* notifyForDescendants= */ false, mFragment.mSettingsContentObserver);
+        }
+    }
+
+    @Test
+    public void onStop_unregisterContentObserver() {
+        when(mContext.getContentResolver()).thenReturn(mContentResolver);
+        mFragment.onAttach(mContext);
+        mFragment.onCreatePreferences(Bundle.EMPTY, /* rootKey */ null);
+        mFragment.onStart();
+
+        mFragment.onStop();
+
+        verify(mContentResolver).unregisterContentObserver(mFragment.mSettingsContentObserver);
+    }
+
+    @Test
+    public void getMetricsCategory_returnsCorrectCategory() {
+        assertThat(mFragment.getMetricsCategory()).isEqualTo(
+                SettingsEnums.ACCESSIBILITY_CAPTION_APPEARANCE);
+    }
+
+    @Test
+    public void getLogTag_returnsCorrectTag() {
+        assertThat(mFragment.getLogTag()).isEqualTo("CaptionAppearanceFragment");
+    }
+
+    @Test
+    public void getNonIndexableKeys_existInXmlLayout() {
+        final List<String> niks = CaptionAppearanceFragment.SEARCH_INDEX_DATA_PROVIDER
+                .getNonIndexableKeys(mContext);
+        final List<String> keys = XmlTestUtils.getKeysFromPreferenceXml(mContext,
+                R.xml.captioning_appearance);
+
+        assertThat(keys).containsAtLeastElementsIn(niks);
+    }
+
+    private static class TestCaptionAppearanceFragment extends CaptionAppearanceFragment {
+
+        @Override
+        public int getPreferenceScreenResId() {
+            return R.xml.placeholder_prefs;
+        }
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptionBackgroundColorControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptionBackgroundColorControllerTest.java
new file mode 100644
index 0000000..964e240
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/CaptionBackgroundColorControllerTest.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2022 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 static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.util.AttributeSet;
+import android.view.accessibility.CaptioningManager;
+
+import androidx.preference.PreferenceScreen;
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.settings.core.BasePreferenceController;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowCaptioningManager;
+
+/** Tests for {@link CaptionBackgroundColorController}. */
+@RunWith(RobolectricTestRunner.class)
+public class CaptionBackgroundColorControllerTest {
+
+    @Rule
+    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
+    @Mock
+    private PreferenceScreen mScreen;
+    private final Context mContext = ApplicationProvider.getApplicationContext();
+    private CaptionBackgroundColorController mController;
+    private ColorPreference mPreference;
+    private ShadowCaptioningManager mShadowCaptioningManager;
+
+    @Before
+    public void setUp() {
+        mController = new CaptionBackgroundColorController(mContext, "captioning_background_color");
+        final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
+        mPreference = new ColorPreference(mContext, attributeSet);
+        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+        CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
+        mShadowCaptioningManager = Shadow.extract(captioningManager);
+    }
+
+    @Test
+    public void getAvailabilityStatus_shouldReturnAvailable() {
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getSummary_defaultValue_shouldReturnBlack() {
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("Black");
+    }
+
+    @Test
+    public void getSummary_redValue_shouldReturnRed() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, 0xFFFF0000);
+
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
+    }
+
+    @Test
+    public void setRedValue_shouldReturnRed() {
+        mController.displayPreference(mScreen);
+
+        mPreference.setValue(0xFFFF0000);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
+    }
+
+    @Test
+    public void onValueChanged_shouldSetCaptionEnabled() {
+        mShadowCaptioningManager.setEnabled(false);
+        mController.displayPreference(mScreen);
+
+        mController.onValueChanged(mPreference, 0xFFFF0000);
+
+        final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
+        assertThat(isCaptionEnabled).isTrue();
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptionBackgroundOpacityControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptionBackgroundOpacityControllerTest.java
new file mode 100644
index 0000000..3c5e247
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/CaptionBackgroundOpacityControllerTest.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2022 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 static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.util.AttributeSet;
+import android.view.accessibility.CaptioningManager;
+
+import androidx.preference.PreferenceScreen;
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.settings.core.BasePreferenceController;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowCaptioningManager;
+
+/** Tests for {@link CaptionBackgroundOpacityController}. */
+@RunWith(RobolectricTestRunner.class)
+public class CaptionBackgroundOpacityControllerTest {
+
+    @Rule
+    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
+    @Mock
+    private PreferenceScreen mScreen;
+    private final Context mContext = ApplicationProvider.getApplicationContext();
+    private CaptionBackgroundOpacityController mController;
+    private ColorPreference mPreference;
+    private ShadowCaptioningManager mShadowCaptioningManager;
+
+    @Before
+    public void setUp() {
+        mController =
+                new CaptionBackgroundOpacityController(mContext, "captioning_background_opacity");
+        final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
+        mPreference = new ColorPreference(mContext, attributeSet);
+        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+        CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
+        mShadowCaptioningManager = Shadow.extract(captioningManager);
+    }
+
+    @Test
+    public void getAvailabilityStatus_shouldReturnAvailable() {
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getSummary_defaultValue_shouldReturnNonTransparent() {
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("100%");
+    }
+
+    @Test
+    public void getSummary_halfTransparentValue_shouldReturnHalfTransparent() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, 0x80FFFFFF);
+
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
+    }
+
+    @Test
+    public void setHalfTransparentValue_shouldReturnHalfTransparent() {
+        mController.displayPreference(mScreen);
+
+        mPreference.setValue(0x80FFFFFF);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
+    }
+
+    @Test
+    public void onValueChanged_shouldSetCaptionEnabled() {
+        mShadowCaptioningManager.setEnabled(false);
+        mController.displayPreference(mScreen);
+
+        mController.onValueChanged(mPreference, 0x80FFFFFF);
+
+        final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
+        assertThat(isCaptionEnabled).isTrue();
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptionEdgeColorControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptionEdgeColorControllerTest.java
new file mode 100644
index 0000000..fd4e55a
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/CaptionEdgeColorControllerTest.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright (C) 2022 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 static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.util.AttributeSet;
+import android.view.accessibility.CaptioningManager;
+
+import androidx.preference.PreferenceScreen;
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.settings.core.BasePreferenceController;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowCaptioningManager;
+
+/** Tests for {@link CaptionEdgeColorController}. */
+@RunWith(RobolectricTestRunner.class)
+public class CaptionEdgeColorControllerTest {
+    @Rule
+    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
+    @Mock
+    private PreferenceScreen mScreen;
+    private final Context mContext = ApplicationProvider.getApplicationContext();
+    private CaptionEdgeColorController mController;
+    private ColorPreference mPreference;
+    private ShadowCaptioningManager mShadowCaptioningManager;
+
+    @Before
+    public void setUp() {
+        mController = new CaptionEdgeColorController(mContext, "captioning_edge_color");
+        final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
+        mPreference = new ColorPreference(mContext, attributeSet);
+        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+        CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
+        mShadowCaptioningManager = Shadow.extract(captioningManager);
+    }
+
+    @Test
+    public void getAvailabilityStatus_shouldReturnAvailable() {
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getSummary_defaultValue_shouldReturnBlack() {
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("Black");
+    }
+
+    @Test
+    public void getSummary_redValue_shouldReturnRed() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_COLOR, 0xFFFF0000);
+
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
+    }
+
+    @Test
+    public void setRedValue_shouldReturnRed() {
+        mController.displayPreference(mScreen);
+
+        mPreference.setValue(0xFFFF0000);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
+    }
+
+    @Test
+    public void onValueChanged_shouldSetCaptionEnabled() {
+        mShadowCaptioningManager.setEnabled(false);
+        mController.displayPreference(mScreen);
+
+        mController.onValueChanged(mPreference, 0xFFFF0000);
+
+        final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
+        assertThat(isCaptionEnabled).isTrue();
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptionEdgeTypeControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptionEdgeTypeControllerTest.java
new file mode 100644
index 0000000..fba7afa
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/CaptionEdgeTypeControllerTest.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2022 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 static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+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;
+
+import com.android.settings.core.BasePreferenceController;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowCaptioningManager;
+
+/** Tests for {@link CaptionEdgeTypeController}. */
+@RunWith(RobolectricTestRunner.class)
+public class CaptionEdgeTypeControllerTest {
+
+    @Rule
+    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
+    @Mock
+    private PreferenceScreen mScreen;
+    private final Context mContext = ApplicationProvider.getApplicationContext();
+    private CaptionEdgeTypeController mController;
+    private EdgeTypePreference mPreference;
+    private ShadowCaptioningManager mShadowCaptioningManager;
+
+    @Before
+    public void setUp() {
+        mController = new CaptionEdgeTypeController(mContext, "captioning_edge_type");
+        final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
+        mPreference = new EdgeTypePreference(mContext, attributeSet);
+        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+        CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
+        mShadowCaptioningManager = Shadow.extract(captioningManager);
+    }
+
+    @Test
+    public void getAvailabilityStatus_shouldReturnAvailable() {
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getSummary_defaultValue_shouldReturnNone() {
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("None");
+    }
+
+    @Test
+    public void getSummary_outlineValue_shouldReturnOutline() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_TYPE, CaptionStyle.EDGE_TYPE_OUTLINE);
+
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("Outline");
+    }
+
+    @Test
+    public void setOutlineValue_shouldReturnOutline() {
+        mController.displayPreference(mScreen);
+
+        mPreference.setValue(CaptionStyle.EDGE_TYPE_OUTLINE);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("Outline");
+    }
+
+    @Test
+    public void onValueChanged_shouldSetCaptionEnabled() {
+        mShadowCaptioningManager.setEnabled(false);
+        mController.displayPreference(mScreen);
+
+        mController.onValueChanged(mPreference, CaptionStyle.EDGE_TYPE_OUTLINE);
+
+        final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
+        assertThat(isCaptionEnabled).isTrue();
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptionForegroundColorControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptionForegroundColorControllerTest.java
new file mode 100644
index 0000000..50ce90d
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/CaptionForegroundColorControllerTest.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2022 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 static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.util.AttributeSet;
+import android.view.accessibility.CaptioningManager;
+
+import androidx.preference.PreferenceScreen;
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.settings.core.BasePreferenceController;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowCaptioningManager;
+
+/** Tests for {@link CaptionForegroundColorController}. */
+@RunWith(RobolectricTestRunner.class)
+public class CaptionForegroundColorControllerTest {
+
+    @Rule
+    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
+    @Mock
+    private PreferenceScreen mScreen;
+    private final Context mContext = ApplicationProvider.getApplicationContext();
+    private CaptionForegroundColorController mController;
+    private ColorPreference mPreference;
+    private ShadowCaptioningManager mShadowCaptioningManager;
+
+    @Before
+    public void setUp() {
+        mController = new CaptionForegroundColorController(mContext, "captioning_foreground_color");
+        final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
+        mPreference = new ColorPreference(mContext, attributeSet);
+        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+        CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
+        mShadowCaptioningManager = Shadow.extract(captioningManager);
+    }
+
+    @Test
+    public void getAvailabilityStatus_shouldReturnAvailable() {
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getSummary_defaultValue_shouldReturnWhite() {
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("White");
+    }
+
+    @Test
+    public void getSummary_redValue_shouldReturnRed() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, 0xFFFF0000);
+
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
+    }
+
+    @Test
+    public void setRedValue_shouldReturnRed() {
+        mController.displayPreference(mScreen);
+
+        mPreference.setValue(0xFFFF0000);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
+    }
+
+    @Test
+    public void onValueChanged_shouldSetCaptionEnabled() {
+        mShadowCaptioningManager.setEnabled(false);
+        mController.displayPreference(mScreen);
+
+        mController.onValueChanged(mPreference, 0xFFFF0000);
+
+        final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
+        assertThat(isCaptionEnabled).isTrue();
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptionForegroundOpacityControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptionForegroundOpacityControllerTest.java
new file mode 100644
index 0000000..38cecd6
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/CaptionForegroundOpacityControllerTest.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2022 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 static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.util.AttributeSet;
+import android.view.accessibility.CaptioningManager;
+
+import androidx.preference.PreferenceScreen;
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.settings.core.BasePreferenceController;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowCaptioningManager;
+
+/** Tests for {@link CaptionForegroundOpacityController}. */
+@RunWith(RobolectricTestRunner.class)
+public class CaptionForegroundOpacityControllerTest {
+
+    @Rule
+    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
+    @Mock
+    private PreferenceScreen mScreen;
+    private final Context mContext = ApplicationProvider.getApplicationContext();
+    private CaptionForegroundOpacityController mController;
+    private ColorPreference mPreference;
+    private ShadowCaptioningManager mShadowCaptioningManager;
+
+    @Before
+    public void setUp() {
+        mController =
+                new CaptionForegroundOpacityController(mContext, "captioning_foreground_opacity");
+        final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
+        mPreference = new ColorPreference(mContext, attributeSet);
+        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+        CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
+        mShadowCaptioningManager = Shadow.extract(captioningManager);
+    }
+
+    @Test
+    public void getAvailabilityStatus_shouldReturnAvailable() {
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getSummary_defaultValue_shouldReturnNonTransparent() {
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("100%");
+    }
+
+    @Test
+    public void getSummary_halfTransparentValue_shouldReturnHalfTransparent() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, 0x80FFFFFF);
+
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
+    }
+
+    @Test
+    public void setHalfTransparentValue_shouldReturnHalfTransparent() {
+        mController.displayPreference(mScreen);
+
+        mPreference.setValue(0x80FFFFFF);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
+    }
+
+    @Test
+    public void onValueChanged_shouldSetCaptionEnabled() {
+        mShadowCaptioningManager.setEnabled(false);
+        mController.displayPreference(mScreen);
+
+        mController.onValueChanged(mPreference, 0x80FFFFFF);
+
+        final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
+        assertThat(isCaptionEnabled).isTrue();
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptionHelperTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptionHelperTest.java
index 42d9454..4700ecd 100644
--- a/tests/robotests/src/com/android/settings/accessibility/CaptionHelperTest.java
+++ b/tests/robotests/src/com/android/settings/accessibility/CaptionHelperTest.java
@@ -24,10 +24,12 @@
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import android.content.ContentResolver;
 import android.content.Context;
 import android.provider.Settings;
 import android.view.View;
 import android.view.accessibility.CaptioningManager;
+import android.view.accessibility.CaptioningManager.CaptionStyle;
 
 import androidx.test.core.app.ApplicationProvider;
 
@@ -60,12 +62,14 @@
     private View mPreviewWindow;
     @Spy
     private final Context mContext = ApplicationProvider.getApplicationContext();
+    private ContentResolver mContentResolver;
     private CaptionHelper mCaptionHelper;
 
     @Before
     public void setUp() {
         when(mContext.getSystemService(CaptioningManager.class)).thenReturn(mCaptioningManager);
         mCaptionHelper = new CaptionHelper(mContext);
+        mContentResolver = mContext.getContentResolver();
     }
 
     @Test
@@ -129,4 +133,53 @@
                 Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
         assertThat(isCaptionEnabled).isFalse();
     }
+
+    @Test
+    public void setBackgroundColor_shouldReturnSpecificColor() {
+        mCaptionHelper.setBackgroundColor(0xFFFF0000);
+
+        final int backgroundColor = mCaptionHelper.getBackgroundColor();
+        assertThat(backgroundColor).isEqualTo(0xFFFF0000);
+    }
+
+    @Test
+    public void setForegroundColor_shouldReturnSpecificColor() {
+        mCaptionHelper.setForegroundColor(0xFFFF0000);
+
+        final int foregroundColor = mCaptionHelper.getForegroundColor();
+        assertThat(foregroundColor).isEqualTo(0xFFFF0000);
+    }
+
+    @Test
+    public void setWindowColor_shouldReturnSpecificColor() {
+        mCaptionHelper.setWindowColor(0xFFFF0000);
+
+        final int windowColor = mCaptionHelper.getWindowColor();
+        assertThat(windowColor).isEqualTo(0xFFFF0000);
+    }
+
+    @Test
+    public void setEdgeColor_shouldReturnSpecificColor() {
+        mCaptionHelper.setEdgeColor(0xFFFF0000);
+
+        final int edgeColor = mCaptionHelper.getEdgeColor();
+        assertThat(edgeColor).isEqualTo(0xFFFF0000);
+    }
+
+    @Test
+    public void setEdgeType_shouldReturnSpecificType() {
+        mCaptionHelper.setEdgeType(CaptionStyle.EDGE_TYPE_OUTLINE);
+
+        final int edgeType = mCaptionHelper.getEdgeType();
+        assertThat(edgeType).isEqualTo(CaptionStyle.EDGE_TYPE_OUTLINE);
+    }
+
+    @Test
+    public void setRawUserStyle_shouldReturnSpecificStyle() {
+        mCaptionHelper.setRawUserStyle(CaptionStyle.PRESET_CUSTOM);
+
+        final int style = Settings.Secure.getInt(mContentResolver,
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, 0);
+        assertThat(style).isEqualTo(CaptionStyle.PRESET_CUSTOM);
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptionPresetControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptionPresetControllerTest.java
new file mode 100644
index 0000000..0b832d3
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/CaptionPresetControllerTest.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2022 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 static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+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;
+
+import com.android.settings.core.BasePreferenceController;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowCaptioningManager;
+
+/** Tests for {@link CaptionPresetController}. */
+@RunWith(RobolectricTestRunner.class)
+public class CaptionPresetControllerTest {
+
+    @Rule
+    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
+    @Mock
+    private PreferenceScreen mScreen;
+    private final Context mContext = ApplicationProvider.getApplicationContext();
+    private CaptionPresetController mController;
+    private PresetPreference mPreference;
+    private ShadowCaptioningManager mShadowCaptioningManager;
+
+    @Before
+    public void setUp() {
+        mController = new CaptionPresetController(mContext, "captioning_preset");
+        final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
+        mPreference = new PresetPreference(mContext, attributeSet);
+        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+        CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
+        mShadowCaptioningManager = Shadow.extract(captioningManager);
+    }
+
+    @Test
+    public void getAvailabilityStatus_shouldReturnAvailable() {
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getSummary_defaultValue_shouldReturnWhiteOnBlack() {
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("White on black");
+    }
+
+    @Test
+    public void getSummary_customValue_shouldReturnCustom() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, CaptionStyle.PRESET_CUSTOM);
+
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("Custom");
+    }
+
+    @Test
+    public void setCustomValue_shouldReturnCustom() {
+        mController.displayPreference(mScreen);
+
+        mPreference.setValue(CaptionStyle.PRESET_CUSTOM);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("Custom");
+    }
+
+    @Test
+    public void onValueChanged_shouldSetCaptionEnabled() {
+        mShadowCaptioningManager.setEnabled(false);
+        mController.displayPreference(mScreen);
+
+        mController.onValueChanged(mPreference, CaptionStyle.PRESET_CUSTOM);
+
+        final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
+        assertThat(isCaptionEnabled).isTrue();
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptionUtilsTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptionUtilsTest.java
new file mode 100644
index 0000000..1e0f155
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/CaptionUtilsTest.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2022 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.google.common.truth.Truth.assertThat;
+
+import android.graphics.Color;
+import android.view.accessibility.CaptioningManager;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+
+/** Tests for {@link CaptionUtils}. */
+@RunWith(RobolectricTestRunner.class)
+public class CaptionUtilsTest {
+
+    @Test
+    public void parseColor_defaultPackedColor_shouldReturnUnspecified() {
+        final int color = CaptionUtils.parseColor(0xFFFF00);
+
+        assertThat(color).isEqualTo(CaptioningManager.CaptionStyle.COLOR_UNSPECIFIED);
+    }
+
+    @Test
+    public void parseColor_unrecognizedColor_shouldReturnTransparent() {
+        final int color = CaptionUtils.parseColor(0x00);
+
+        assertThat(color).isEqualTo(Color.TRANSPARENT);
+    }
+
+    @Test
+    public void parseColor_redColor_shouldReturnRed() {
+        final int color = CaptionUtils.parseColor(0xFFFF0000);
+
+        assertThat(color).isEqualTo(Color.RED);
+    }
+
+    @Test
+    public void parseOpacity_defaultPackedColor_shouldReturnUnspecified() {
+        final int color = CaptionUtils.parseOpacity(0xFFFF00);
+
+        assertThat(color).isEqualTo(CaptioningManager.CaptionStyle.COLOR_UNSPECIFIED);
+    }
+
+    @Test
+    public void parseOpacity_unrecognizedColor_shouldReturnTransparent() {
+        final int color = CaptionUtils.parseOpacity(0x00);
+
+        assertThat(color).isEqualTo(0xFFFFFF);
+    }
+
+    @Test
+    public void parseOpacity_halfTransparentValue_shouldReturnHalfTransparent() {
+        final int color = CaptionUtils.parseOpacity(0x80FFFFFF);
+
+        assertThat(color).isEqualTo(0x80FFFFFF);
+    }
+
+    @Test
+    public void mergeColorOpacity_halfTransparentRedValue_shouldReturnMergeColorOpacityValue() {
+        final int color = CaptionUtils.mergeColorOpacity(0xFFFF0000, 0x80FFFFFF);
+
+        assertThat(color).isEqualTo(0x80FF0000);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptionWindowColorControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptionWindowColorControllerTest.java
new file mode 100644
index 0000000..66b1666
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/CaptionWindowColorControllerTest.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2022 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 static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.util.AttributeSet;
+import android.view.accessibility.CaptioningManager;
+
+import androidx.preference.PreferenceScreen;
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowCaptioningManager;
+
+/** Tests for {@link CaptionWindowColorController}. */
+@RunWith(RobolectricTestRunner.class)
+public class CaptionWindowColorControllerTest {
+
+    @Rule
+    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
+    @Mock
+    private PreferenceScreen mScreen;
+    private final Context mContext = ApplicationProvider.getApplicationContext();
+    private CaptionWindowColorController mController;
+    private ColorPreference mPreference;
+    private ShadowCaptioningManager mShadowCaptioningManager;
+
+    @Before
+    public void setUp() {
+        mController = new CaptionWindowColorController(mContext, "captioning_window_color");
+        final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
+        mPreference = new ColorPreference(mContext, attributeSet);
+        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+        CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
+        mShadowCaptioningManager = Shadow.extract(captioningManager);
+    }
+
+    @Test
+    public void getAvailabilityStatus_shouldReturnAvailable() {
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getSummary_defaultValue_shouldReturnNone() {
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo(
+                mContext.getString(R.string.color_none));
+    }
+
+    @Test
+    public void getSummary_redValue_shouldReturnRed() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, 0xFFFF0000);
+
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
+    }
+
+    @Test
+    public void setRedValue_shouldReturnRed() {
+        mController.displayPreference(mScreen);
+
+        mPreference.setValue(0xFFFF0000);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("Red");
+    }
+
+    @Test
+    public void onValueChanged_shouldSetCaptionEnabled() {
+        mShadowCaptioningManager.setEnabled(false);
+        mController.displayPreference(mScreen);
+
+        mController.onValueChanged(mPreference, 0xFFFF0000);
+
+        final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
+        assertThat(isCaptionEnabled).isTrue();
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptionWindowOpacityControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptionWindowOpacityControllerTest.java
new file mode 100644
index 0000000..71d3748
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/CaptionWindowOpacityControllerTest.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2022 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 static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.util.AttributeSet;
+import android.view.accessibility.CaptioningManager;
+
+import androidx.preference.PreferenceScreen;
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.settings.core.BasePreferenceController;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowCaptioningManager;
+
+/** Tests for {@link CaptionWindowOpacityController}. */
+@RunWith(RobolectricTestRunner.class)
+public class CaptionWindowOpacityControllerTest {
+
+    @Rule
+    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
+    @Mock
+    private PreferenceScreen mScreen;
+    private final Context mContext = ApplicationProvider.getApplicationContext();
+    private CaptionWindowOpacityController mController;
+    private ColorPreference mPreference;
+    private ShadowCaptioningManager mShadowCaptioningManager;
+
+    @Before
+    public void setUp() {
+        mController = new CaptionWindowOpacityController(mContext, "captioning_window_opacity");
+        final AttributeSet attributeSet = Robolectric.buildAttributeSet().build();
+        mPreference = new ColorPreference(mContext, attributeSet);
+        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+        CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
+        mShadowCaptioningManager = Shadow.extract(captioningManager);
+    }
+
+    @Test
+    public void getAvailabilityStatus_shouldReturnAvailable() {
+        assertThat(mController.getAvailabilityStatus())
+                .isEqualTo(BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getSummary_defaultValue_shouldReturnNonTransparent() {
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("100%");
+    }
+
+    @Test
+    public void getSummary_halfTransparentValue_shouldReturnHalfTransparent() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, 0x80FFFFFF);
+
+        mController.displayPreference(mScreen);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
+    }
+
+    @Test
+    public void setHalfTransparentValue_shouldReturnHalfTransparent() {
+        mController.displayPreference(mScreen);
+
+        mPreference.setValue(0x80FFFFFF);
+
+        assertThat(mPreference.getSummary().toString()).isEqualTo("50%");
+    }
+
+    @Test
+    public void onValueChanged_shouldSetCaptionEnabled() {
+        mShadowCaptioningManager.setEnabled(false);
+        mController.displayPreference(mScreen);
+
+        mController.onValueChanged(mPreference, 0x80FFFFFF);
+
+        final boolean isCaptionEnabled = Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, OFF) == ON;
+        assertThat(isCaptionEnabled).isTrue();
+    }
+}