DO NOT MERGE Add preference for caption text opacity

BUG: 11990225
Change-Id: Ib7dd92fd3e44931a1a4a2603effb02550b4fbc59
diff --git a/res/values/strings.xml b/res/values/strings.xml
index db8b4b7..d3d392b 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -3330,6 +3330,8 @@
     <string name="captioning_background_opacity">Background opacity</string>
     <!-- Title for the preference to change video caption text color. [CHAR LIMIT=35] -->
     <string name="captioning_foreground_color">Text color</string>
+    <!-- Title for the preference to change video caption text opacity. [CHAR LIMIT=35] -->
+    <string name="captioning_foreground_opacity">Text opacity</string>
     <!-- Title for the preference to change video caption edge color. [CHAR LIMIT=35] -->
     <string name="captioning_edge_color">Edge color</string>
     <!-- Title for the preference to change video caption edge type. [CHAR LIMIT=35] -->
diff --git a/res/xml/captioning_settings.xml b/res/xml/captioning_settings.xml
index 02fe51f..d14d352 100644
--- a/res/xml/captioning_settings.xml
+++ b/res/xml/captioning_settings.xml
@@ -58,6 +58,10 @@
             android:key="captioning_foreground_color"
             android:persistent="false"
             android:title="@string/captioning_foreground_color" />
+        <com.android.settings.accessibility.ColorPreference
+            android:key="captioning_foreground_opacity"
+            android:persistent="false"
+            android:title="@string/captioning_foreground_opacity" />
 
         <com.android.settings.accessibility.EdgeTypePreference
             android:key="captioning_edge_type"
diff --git a/src/com/android/settings/accessibility/CaptionPropertiesFragment.java b/src/com/android/settings/accessibility/CaptionPropertiesFragment.java
index 07c4d84..8a08d90 100644
--- a/src/com/android/settings/accessibility/CaptionPropertiesFragment.java
+++ b/src/com/android/settings/accessibility/CaptionPropertiesFragment.java
@@ -50,6 +50,18 @@
  */
 public class CaptionPropertiesFragment extends SettingsPreferenceFragment
         implements OnPreferenceChangeListener, OnValueChangedListener {
+    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_EDGE_COLOR = "captioning_edge_color";
+    private static final String PREF_EDGE_TYPE = "captioning_edge_type";
+    private static final String PREF_FONT_SIZE = "captioning_font_size";
+    private static final String PREF_TYPEFACE = "captioning_typeface";
+    private static final String PREF_LOCALE = "captioning_locale";
+    private static final String PREF_PRESET = "captioning_preset";
+    private static final String PREF_CUSTOM = "custom";
+
     private static final float DEFAULT_FONT_SIZE = 48f;
 
     private CaptioningManager mCaptioningManager;
@@ -63,6 +75,7 @@
     // Custom options.
     private ListPreference mTypeface;
     private ColorPreference mForegroundColor;
+    private ColorPreference mForegroundOpacity;
     private EdgeTypePreference mEdgeType;
     private ColorPreference mEdgeColor;
     private ColorPreference mBackgroundColor;
@@ -188,25 +201,33 @@
     }
 
     private void initializeAllPreferences() {
-        mLocale = (LocalePreference) findPreference("captioning_locale");
-        mFontSize = (ListPreference) findPreference("captioning_font_size");
+        mLocale = (LocalePreference) findPreference(PREF_LOCALE);
+        mFontSize = (ListPreference) findPreference(PREF_FONT_SIZE);
 
         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("captioning_preset");
+        mPreset = (PresetPreference) findPreference(PREF_PRESET);
         mPreset.setValues(presetValues);
         mPreset.setTitles(presetTitles);
 
-        mCustom = (PreferenceCategory) findPreference("custom");
+        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("captioning_foreground_color");
+        mForegroundColor = (ColorPreference) mCustom.findPreference(PREF_FOREGROUND_COLOR);
         mForegroundColor.setTitles(colorTitles);
         mForegroundColor.setValues(colorValues);
-        mEdgeColor = (ColorPreference) mCustom.findPreference("captioning_edge_color");
+
+        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);
 
@@ -217,25 +238,22 @@
         System.arraycopy(colorTitles, 0, bgColorTitles, 1, colorTitles.length);
         bgColorValues[0] = Color.TRANSPARENT;
         bgColorTitles[0] = getString(R.string.color_none);
-        mBackgroundColor = (ColorPreference) mCustom.findPreference("captioning_background_color");
+        mBackgroundColor = (ColorPreference) mCustom.findPreference(PREF_BACKGROUND_COLOR);
         mBackgroundColor.setTitles(bgColorTitles);
         mBackgroundColor.setValues(bgColorValues);
 
-        final int[] opacityValues = res.getIntArray(R.array.captioning_opacity_selector_values);
-        final String[] opacityTitles = res.getStringArray(
-                R.array.captioning_opacity_selector_titles);
-        mBackgroundOpacity = (ColorPreference) mCustom.findPreference(
-                "captioning_background_opacity");
+        mBackgroundOpacity = (ColorPreference) mCustom.findPreference(PREF_BACKGROUND_OPACITY);
         mBackgroundOpacity.setTitles(opacityTitles);
         mBackgroundOpacity.setValues(opacityValues);
 
-        mEdgeType = (EdgeTypePreference) mCustom.findPreference("captioning_edge_type");
-        mTypeface = (ListPreference) mCustom.findPreference("captioning_typeface");
+        mEdgeType = (EdgeTypePreference) mCustom.findPreference(PREF_EDGE_TYPE);
+        mTypeface = (ListPreference) mCustom.findPreference(PREF_TYPEFACE);
     }
 
     private void installUpdateListeners() {
         mPreset.setOnValueChangedListener(this);
         mForegroundColor.setOnValueChangedListener(this);
+        mForegroundOpacity.setOnValueChangedListener(this);
         mEdgeColor.setOnValueChangedListener(this);
         mBackgroundColor.setOnValueChangedListener(this);
         mBackgroundOpacity.setOnValueChangedListener(this);
@@ -255,22 +273,11 @@
 
         final ContentResolver cr = getContentResolver();
         final CaptionStyle attrs = CaptionStyle.getCustomStyle(cr);
-        mForegroundColor.setValue(attrs.foregroundColor);
         mEdgeType.setValue(attrs.edgeType);
         mEdgeColor.setValue(attrs.edgeColor);
 
-        final int backgroundColor = attrs.backgroundColor;
-        final int bgColor;
-        final int bgAlpha;
-        if (Color.alpha(backgroundColor) == 0) {
-            bgColor = Color.TRANSPARENT;
-            bgAlpha = (backgroundColor & 0xFF) << 24;
-        } else {
-            bgColor = backgroundColor | 0xFF000000;
-            bgAlpha = backgroundColor & 0xFF000000;
-        }
-        mBackgroundColor.setValue(bgColor);
-        mBackgroundOpacity.setValue(bgAlpha | 0xFFFFFF);
+        parseColorOpacity(mForegroundColor, mForegroundOpacity, attrs.foregroundColor);
+        parseColorOpacity(mBackgroundColor, mBackgroundOpacity, attrs.backgroundColor);
 
         final String rawTypeface = attrs.mRawTypeface;
         mTypeface.setValue(rawTypeface == null ? "" : rawTypeface);
@@ -279,6 +286,32 @@
         mLocale.setValue(rawLocale == null ? "" : rawLocale);
     }
 
+    private void parseColorOpacity(ColorPreference color, ColorPreference opacity, int value) {
+        final int colorValue;
+        final int opacityValue;
+        if (Color.alpha(value) == 0) {
+            colorValue = Color.TRANSPARENT;
+            opacityValue = (value & 0xFF) << 24;
+        } else {
+            colorValue = value | 0xFF000000;
+            opacityValue = value & 0xFF000000;
+        }
+        color.setValue(colorValue);
+        opacity.setValue(opacityValue | 0xFFFFFF);
+    }
+
+    private int mergeColorOpacity(ColorPreference color, ColorPreference opacity) {
+        final int colorValue = color.getValue();
+        final int opacityValue = opacity.getValue();
+        final int value;
+        if (Color.alpha(colorValue) == 0) {
+            value = Color.alpha(opacityValue);
+        } else {
+            value = colorValue & 0x00FFFFFF | opacityValue & 0xFF000000;
+        }
+        return value;
+    }
+
     private void refreshShowingCustom() {
         final boolean customPreset = mPreset.getValue() == CaptionStyle.PRESET_CUSTOM;
         if (!customPreset && mShowingCustom) {
@@ -293,20 +326,14 @@
     @Override
     public void onValueChanged(ListDialogPreference preference, int value) {
         final ContentResolver cr = getActivity().getContentResolver();
-        if (mForegroundColor == preference) {
+        if (mForegroundColor == preference || mForegroundOpacity == preference) {
+            final int merged = mergeColorOpacity(mForegroundColor, mForegroundOpacity);
             Settings.Secure.putInt(
-                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, value);
+                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, merged);
         } else if (mBackgroundColor == preference || mBackgroundOpacity == preference) {
-            final int bgColor = mBackgroundColor.getValue();
-            final int bgAlpha = mBackgroundOpacity.getValue();
-            final int argb;
-            if (Color.alpha(bgColor) == 0) {
-                argb = Color.alpha(bgAlpha);
-            } else {
-                argb = bgColor & 0x00FFFFFF | bgAlpha & 0xFF000000;
-            }
+            final int merged = mergeColorOpacity(mBackgroundColor, mBackgroundOpacity);
             Settings.Secure.putInt(
-                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, argb);
+                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, merged);
         } else if (mEdgeColor == preference) {
             Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_COLOR, value);
         } else if (mPreset == preference) {