Polish humanize strings for Caption preferences page

- Add intro for usage
- Update footer description on limitations
- Update the default caption size to medium
- Update the caption size and style dynamically

Bug: 218409087
Test: make RunSettingsRoboTests
ROBOTEST_FILTER=CaptionAppearancePreferenceControllerTest

Change-Id: I6526250894cc6a3fd02e3d8c675280855b307a68
diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index a3e53f5..5ebaeed 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -816,20 +816,11 @@
     <string-array name="captioning_font_size_selector_titles">
         <item>Very small</item>
         <item>Small</item>
-        <item>Default</item>
+        <item>Medium</item>
         <item>Large</item>
         <item>Very large</item>
     </string-array>
 
-    <!-- Summary for Captions settings, explaining important settings under it. [CHAR LIMIT=NONE] -->
-    <string-array name="captioning_font_size_selector_summaries">
-        <item>Very small text size</item>
-        <item>Small text size</item>
-        <item>Default text size</item>
-        <item>Large text size</item>
-        <item>Very large text size</item>
-    </string-array>
-
     <!-- Values for captioning font size preference. -->
     <string-array name="captioning_font_size_selector_values" translatable="false" >
         <item>0.25</item>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 0107358..501de83 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -5527,8 +5527,10 @@
     <string name="captioning_caption_appearance_summary"><xliff:g id="accessibility_font_size" example="Large">%1$s</xliff:g> text size</string>
     <!-- Title for Caption preference settings screen for configuring language. [CHAR LIMIT=NONE] -->
     <string name="captioning_more_options_title">More options</string>
-    <!-- Used in the Captions preference to tell users that the setting doesn't support all apps. [CHAR LIMIT=NONE] -->
-    <string name="accessibility_caption_preference_summary">Not all apps support these caption preferences</string>
+    <!-- Introduction for the captions preference page. [CHAR LIMIT=NONE] -->
+    <string name="accessibility_caption_preference_intro">Customize caption size and style to make them easier to read</string>
+    <!-- Summary for the captions preference page. [CHAR LIMIT=NONE] -->
+    <string name="accessibility_caption_preference_summary">These caption preferences aren\u2019t supported by all media apps</string>
     <!-- Summary for accessibility shortcut preference for software shortcut type. [CHAR LIMIT=NONE] -->
     <string name="accessibility_shortcut_type_software">Accessibility button</string>
     <!-- Summary for accessibility shortcut preference for software shortcut type when gesture mode is on. [CHAR LIMIT=NONE] -->
diff --git a/res/xml/captioning_settings.xml b/res/xml/captioning_settings.xml
index f5059e5..f9e9948 100644
--- a/res/xml/captioning_settings.xml
+++ b/res/xml/captioning_settings.xml
@@ -21,6 +21,11 @@
     android:persistent="false"
     android:title="@string/accessibility_captioning_title">
 
+    <com.android.settingslib.widget.TopIntroPreference
+        android:key="captions_intro"
+        android:persistent="false"
+        android:title="@string/accessibility_caption_preference_intro"/>
+
     <com.android.settingslib.widget.IllustrationPreference
         android:key="captions_preview"
         android:persistent="false"
@@ -38,7 +43,8 @@
         android:fragment="com.android.settings.accessibility.CaptionAppearanceFragment"
         android:key="captioning_caption_appearance"
         android:persistent="false"
-        android:title="@string/captioning_caption_appearance_title" />
+        android:title="@string/captioning_caption_appearance_title"
+        settings:controller="com.android.settings.accessibility.CaptionAppearancePreferenceController" />
 
     <Preference
         android:fragment="com.android.settings.accessibility.CaptionMoreOptionsFragment"
diff --git a/src/com/android/settings/accessibility/CaptionAppearancePreferenceController.java b/src/com/android/settings/accessibility/CaptionAppearancePreferenceController.java
new file mode 100644
index 0000000..4bcdceb
--- /dev/null
+++ b/src/com/android/settings/accessibility/CaptionAppearancePreferenceController.java
@@ -0,0 +1,78 @@
+/*
+ * 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.view.accessibility.CaptioningManager;
+
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+
+import com.google.common.primitives.Floats;
+import com.google.common.primitives.Ints;
+
+/** Controller that shows the caption scale and style summary. */
+public class CaptionAppearancePreferenceController extends BasePreferenceController {
+
+    private final CaptioningManager mCaptioningManager;
+
+    public CaptionAppearancePreferenceController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+        mCaptioningManager = context.getSystemService(CaptioningManager.class);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AVAILABLE;
+    }
+
+    @Override
+    public CharSequence getSummary() {
+        return mContext.getString(R.string.preference_summary_default_combination,
+                geFontScaleSummary(), getPresetSummary());
+    }
+
+    private float[] getFontScaleValuesArray() {
+        final String[] fontScaleValuesStrArray = mContext.getResources().getStringArray(
+                R.array.captioning_font_size_selector_values);
+        final int length = fontScaleValuesStrArray.length;
+        final float[] fontScaleValuesArray = new float[length];
+        for (int i = 0; i < length; ++i) {
+            fontScaleValuesArray[i] = Float.parseFloat(fontScaleValuesStrArray[i]);
+        }
+        return fontScaleValuesArray;
+    }
+
+    private CharSequence geFontScaleSummary() {
+        final float[] fontScaleValuesArray = getFontScaleValuesArray();
+        final String[] fontScaleSummaries = mContext.getResources().getStringArray(
+                R.array.captioning_font_size_selector_titles);
+        final float fontScale = mCaptioningManager.getFontScale();
+        final int idx = Floats.indexOf(fontScaleValuesArray, fontScale);
+        return fontScaleSummaries[idx == /* not exist */ -1 ? 0 : idx];
+    }
+
+    private CharSequence getPresetSummary() {
+        final int[] presetValuesArray = mContext.getResources().getIntArray(
+                R.array.captioning_preset_selector_values);
+        final String[] presetSummaries = mContext.getResources().getStringArray(
+                R.array.captioning_preset_selector_titles);
+        final int preset = mCaptioningManager.getRawUserStyle();
+        final int idx = Ints.indexOf(presetValuesArray, preset);
+        return presetSummaries[idx];
+    }
+}
diff --git a/src/com/android/settings/accessibility/CaptionPropertiesFragment.java b/src/com/android/settings/accessibility/CaptionPropertiesFragment.java
index bcdba11..2d4bb11 100644
--- a/src/com/android/settings/accessibility/CaptionPropertiesFragment.java
+++ b/src/com/android/settings/accessibility/CaptionPropertiesFragment.java
@@ -34,8 +34,6 @@
 import com.android.settingslib.search.SearchIndexable;
 import com.android.settingslib.widget.OnMainSwitchChangeListener;
 
-import com.google.common.primitives.Floats;
-
 import java.util.ArrayList;
 import java.util.List;
 
@@ -56,7 +54,6 @@
     private Preference mMoreOptions;
 
     private final List<Preference> mPreferenceList = new ArrayList<>();
-    private float[] mFontSizeValuesArray;
 
     @Override
     public int getMetricsCategory() {
@@ -71,13 +68,12 @@
 
         initializeAllPreferences();
         installUpdateListeners();
-        initFontSizeValuesArray();
     }
 
     @Override
     public void onResume() {
         super.onResume();
-        updateAllPreferences();
+        mSwitch.setChecked(mCaptioningManager.isEnabled());
     }
 
     @Override
@@ -105,21 +101,6 @@
 
     }
 
-    private void initFontSizeValuesArray() {
-        final String[] fontSizeValuesStrArray = getPrefContext().getResources().getStringArray(
-                R.array.captioning_font_size_selector_values);
-        final int length = fontSizeValuesStrArray.length;
-        mFontSizeValuesArray = new float[length];
-        for (int i = 0; i < length; ++i) {
-            mFontSizeValuesArray[i] = Float.parseFloat(fontSizeValuesStrArray[i]);
-        }
-    }
-
-    private void updateAllPreferences() {
-        mSwitch.setChecked(mCaptioningManager.isEnabled());
-        mTextAppearance.setSummary(geTextAppearanceSummary(getPrefContext()));
-    }
-
     @Override
     public boolean onPreferenceChange(Preference preference, Object value) {
         final ContentResolver cr = getActivity().getContentResolver();
@@ -136,16 +117,6 @@
         return R.string.help_url_caption;
     }
 
-    private CharSequence geTextAppearanceSummary(Context context) {
-        final String[] fontSizeSummaries = context.getResources().getStringArray(
-                R.array.captioning_font_size_selector_summaries);
-
-        final float fontSize = mCaptioningManager.getFontScale();
-        final int idx = Floats.indexOf(mFontSizeValuesArray, fontSize);
-
-        return fontSizeSummaries[idx == /* not exist */ -1 ? 0 : idx];
-    }
-
     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
             new BaseSearchIndexProvider(R.xml.captioning_settings);
 
diff --git a/tests/robotests/src/com/android/settings/accessibility/CaptionAppearancePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/CaptionAppearancePreferenceControllerTest.java
new file mode 100644
index 0000000..cfa683ba
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/accessibility/CaptionAppearancePreferenceControllerTest.java
@@ -0,0 +1,182 @@
+/*
+ * 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.content.Context;
+import android.provider.Settings;
+import android.view.accessibility.CaptioningManager;
+
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowCaptioningManager;
+
+/** Tests for {@link CaptionAppearancePreferenceController}. */
+@RunWith(RobolectricTestRunner.class)
+public class CaptionAppearancePreferenceControllerTest {
+
+    private static final String TEST_KEY = "test_key";
+    private static final int DEFAULT_PRESET_INDEX = 1;
+    private static final int DEFAULT_FONT_SCALE_INDEX = 2;
+
+    private final Context mContext = ApplicationProvider.getApplicationContext();
+    private CaptionAppearancePreferenceController mController;
+    private ShadowCaptioningManager mShadowCaptioningManager;
+
+    @Before
+    public void setUp() {
+        CaptioningManager captioningManager = mContext.getSystemService(CaptioningManager.class);
+        mShadowCaptioningManager = Shadow.extract(captioningManager);
+        mController = new CaptionAppearancePreferenceController(mContext, TEST_KEY);
+    }
+
+    @Test
+    public void getAvailabilityStatus_shouldReturnAvailable() {
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(
+                BasePreferenceController.AVAILABLE);
+    }
+
+    @Test
+    public void getSummary_noScale_shouldReturnDefaultSummary() {
+        final String expectedSummary =
+                getSummaryCombo(DEFAULT_FONT_SCALE_INDEX, DEFAULT_PRESET_INDEX);
+        assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
+    }
+
+    @Test
+    public void getSummary_smallestScale_shouldReturnExpectedSummary() {
+        mShadowCaptioningManager.setFontScale(0.25f);
+
+        final String expectedSummary =
+                getSummaryCombo(/* fontScaleIndex= */ 0, DEFAULT_PRESET_INDEX);
+        assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
+    }
+
+    @Test
+    public void getSummary_smallScale_shouldReturnExpectedSummary() {
+        mShadowCaptioningManager.setFontScale(0.5f);
+
+        final String expectedSummary =
+                getSummaryCombo(/* fontScaleIndex= */ 1, DEFAULT_PRESET_INDEX);
+        assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
+    }
+
+    @Test
+    public void getSummary_mediumScale_shouldReturnExpectedSummary() {
+        mShadowCaptioningManager.setFontScale(1.0f);
+
+        final String expectedSummary =
+                getSummaryCombo(/* fontScaleIndex= */ 2, DEFAULT_PRESET_INDEX);
+        assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
+    }
+
+    @Test
+    public void getSummary_largeScale_shouldReturnExpectedSummary() {
+        mShadowCaptioningManager.setFontScale(1.5f);
+
+        final String expectedSummary =
+                getSummaryCombo(/* fontScaleIndex= */ 3, DEFAULT_PRESET_INDEX);
+        assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
+    }
+
+    @Test
+    public void getSummary_largestScale_shouldReturnExpectedSummary() {
+        mShadowCaptioningManager.setFontScale(2.0f);
+
+        final String expectedSummary =
+                getSummaryCombo(/* fontScaleIndex= */ 4, DEFAULT_PRESET_INDEX);
+        assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
+    }
+
+    @Test
+    public void getSummary_setByAppPreset_shouldReturnExpectedSummary() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, 4);
+
+        final String expectedSummary =
+                getSummaryCombo(DEFAULT_FONT_SCALE_INDEX, /* presetIndex= */ 0);
+        assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
+    }
+
+    @Test
+    public void getSummary_whiteOnBlackPreset_shouldReturnExpectedSummary() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, 0);
+
+        final String expectedSummary =
+                getSummaryCombo(DEFAULT_FONT_SCALE_INDEX, /* presetIndex= */ 1);
+        assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
+    }
+
+    @Test
+    public void getSummary_blackOnWhitePreset_shouldReturnExpectedSummary() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, 1);
+
+        final String expectedSummary =
+                getSummaryCombo(DEFAULT_FONT_SCALE_INDEX, /* presetIndex= */ 2);
+        assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
+    }
+
+    @Test
+    public void getSummary_yellowOnBlackPreset_shouldReturnExpectedSummary() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, 2);
+
+        final String expectedSummary =
+                getSummaryCombo(DEFAULT_FONT_SCALE_INDEX, /* presetIndex= */ 3);
+        assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
+    }
+
+    @Test
+    public void getSummary_yellowOnBluePreset_shouldReturnExpectedSummary() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, 3);
+
+        final String expectedSummary =
+                getSummaryCombo(DEFAULT_FONT_SCALE_INDEX, /* presetIndex= */ 4);
+        assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
+    }
+
+    @Test
+    public void getSummary_customPreset_shouldReturnExpectedSummary() {
+        Settings.Secure.putInt(mContext.getContentResolver(),
+                Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, -1);
+
+        final String expectedSummary =
+                getSummaryCombo(DEFAULT_FONT_SCALE_INDEX, /* presetIndex= */ 5);
+        assertThat(mController.getSummary().toString()).isEqualTo(expectedSummary);
+    }
+
+    private String getSummaryCombo(int fontScaleIndex, int presetIndex) {
+        final String[] fontScaleArray = mContext.getResources().getStringArray(
+                R.array.captioning_font_size_selector_titles);
+        final String[] presetArray = mContext.getResources().getStringArray(
+                R.array.captioning_preset_selector_titles);
+        return mContext.getString(R.string.preference_summary_default_combination,
+                fontScaleArray[fontScaleIndex], presetArray[presetIndex]);
+    }
+}