CaptionPropertiesFragment.java revision d07c16107d7cda495a8a2655cdcb0ad552d25a84
1cc0e782871eb6b946ded880e391866f27953654bAlan Viverette/*
2cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * Copyright (C) 2013 The Android Open Source Project
3cc0e782871eb6b946ded880e391866f27953654bAlan Viverette *
4cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * Licensed under the Apache License, Version 2.0 (the "License");
5cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * you may not use this file except in compliance with the License.
6cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * You may obtain a copy of the License at
7cc0e782871eb6b946ded880e391866f27953654bAlan Viverette *
8cc0e782871eb6b946ded880e391866f27953654bAlan Viverette *      http://www.apache.org/licenses/LICENSE-2.0
9cc0e782871eb6b946ded880e391866f27953654bAlan Viverette *
10cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * Unless required by applicable law or agreed to in writing, software
11cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * distributed under the License is distributed on an "AS IS" BASIS,
12cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * See the License for the specific language governing permissions and
14cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * limitations under the License.
15cc0e782871eb6b946ded880e391866f27953654bAlan Viverette */
16cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
17cc0e782871eb6b946ded880e391866f27953654bAlan Viverettepackage com.android.settings.accessibility;
18cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
194098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport android.app.ActionBar;
204098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport android.app.Activity;
21cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.content.ContentResolver;
225ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viveretteimport android.content.Context;
23cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.content.res.Resources;
24cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.graphics.Color;
25cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.os.Bundle;
26cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.preference.ListPreference;
27cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.preference.Preference;
28660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viveretteimport android.preference.PreferenceCategory;
294098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport android.preference.PreferenceFrameLayout;
30cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.preference.Preference.OnPreferenceChangeListener;
31cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.provider.Settings;
324098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport android.view.Gravity;
334098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport android.view.LayoutInflater;
344098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport android.view.View;
354098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport android.view.ViewGroup;
364098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport android.view.ViewGroup.LayoutParams;
37cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.view.accessibility.CaptioningManager;
38cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.view.accessibility.CaptioningManager.CaptionStyle;
39cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
404098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport com.android.internal.widget.SubtitleView;
41cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport com.android.settings.R;
42cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport com.android.settings.SettingsPreferenceFragment;
43cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
444098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport com.android.settings.accessibility.ToggleSwitch.OnBeforeCheckedChangeListener;
454098dba2535453a89ba74022de47578f2556bd41Alan Viverette
464098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport java.util.Locale;
47cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
48cc0e782871eb6b946ded880e391866f27953654bAlan Viverette/**
49cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * Settings fragment containing captioning properties.
50cc0e782871eb6b946ded880e391866f27953654bAlan Viverette */
51cc0e782871eb6b946ded880e391866f27953654bAlan Viverettepublic class CaptionPropertiesFragment extends SettingsPreferenceFragment
52cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        implements OnPreferenceChangeListener, OnValueChangedListener {
53205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_BACKGROUND_COLOR = "captioning_background_color";
54205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_BACKGROUND_OPACITY = "captioning_background_opacity";
55205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_FOREGROUND_COLOR = "captioning_foreground_color";
56205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_FOREGROUND_OPACITY = "captioning_foreground_opacity";
57d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette    private static final String PREF_WINDOW_COLOR = "captioning_window_color";
58d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette    private static final String PREF_WINDOW_OPACITY = "captioning_window_opacity";
59205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_EDGE_COLOR = "captioning_edge_color";
60205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_EDGE_TYPE = "captioning_edge_type";
61205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_FONT_SIZE = "captioning_font_size";
62205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_TYPEFACE = "captioning_typeface";
63205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_LOCALE = "captioning_locale";
64205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_PRESET = "captioning_preset";
65205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_CUSTOM = "custom";
66205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette
674098dba2535453a89ba74022de47578f2556bd41Alan Viverette    private static final float DEFAULT_FONT_SIZE = 48f;
684098dba2535453a89ba74022de47578f2556bd41Alan Viverette
695ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette    private CaptioningManager mCaptioningManager;
704098dba2535453a89ba74022de47578f2556bd41Alan Viverette    private SubtitleView mPreviewText;
71d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette    private View mPreviewWindow;
72cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
73cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    // Standard options.
74cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private LocalePreference mLocale;
75cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private ListPreference mFontSize;
76cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private PresetPreference mPreset;
77cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
78cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    // Custom options.
79cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private ListPreference mTypeface;
80cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private ColorPreference mForegroundColor;
81205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private ColorPreference mForegroundOpacity;
82cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private EdgeTypePreference mEdgeType;
83cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private ColorPreference mEdgeColor;
84cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private ColorPreference mBackgroundColor;
85cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private ColorPreference mBackgroundOpacity;
86d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette    private ColorPreference mWindowColor;
87d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette    private ColorPreference mWindowOpacity;
88660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette    private PreferenceCategory mCustom;
89660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette
90660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette    private boolean mShowingCustom;
91cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
92cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    @Override
93cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    public void onCreate(Bundle icicle) {
94cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        super.onCreate(icicle);
955ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette
965ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette        mCaptioningManager = (CaptioningManager) getSystemService(Context.CAPTIONING_SERVICE);
975ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette
98cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        addPreferencesFromResource(R.xml.captioning_settings);
99cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        initializeAllPreferences();
100cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        updateAllPreferences();
101660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        refreshShowingCustom();
102cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        installUpdateListeners();
103cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
104cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
1054098dba2535453a89ba74022de47578f2556bd41Alan Viverette    @Override
1064098dba2535453a89ba74022de47578f2556bd41Alan Viverette    public View onCreateView(
1074098dba2535453a89ba74022de47578f2556bd41Alan Viverette            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
1084098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final View rootView = inflater.inflate(R.layout.captioning_preview, container, false);
1094098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1104098dba2535453a89ba74022de47578f2556bd41Alan Viverette        // We have to do this now because PreferenceFrameLayout looks at it
1114098dba2535453a89ba74022de47578f2556bd41Alan Viverette        // only when the view is added.
1124098dba2535453a89ba74022de47578f2556bd41Alan Viverette        if (container instanceof PreferenceFrameLayout) {
1134098dba2535453a89ba74022de47578f2556bd41Alan Viverette            ((PreferenceFrameLayout.LayoutParams) rootView.getLayoutParams()).removeBorders = true;
1144098dba2535453a89ba74022de47578f2556bd41Alan Viverette        }
1154098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1164098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final View content = super.onCreateView(inflater, container, savedInstanceState);
1174098dba2535453a89ba74022de47578f2556bd41Alan Viverette        ((ViewGroup) rootView.findViewById(R.id.properties_fragment)).addView(
1184098dba2535453a89ba74022de47578f2556bd41Alan Viverette                content, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
1194098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1204098dba2535453a89ba74022de47578f2556bd41Alan Viverette        return rootView;
1214098dba2535453a89ba74022de47578f2556bd41Alan Viverette    }
1224098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1234098dba2535453a89ba74022de47578f2556bd41Alan Viverette    @Override
1244098dba2535453a89ba74022de47578f2556bd41Alan Viverette    public void onViewCreated(View view, Bundle savedInstanceState) {
1254098dba2535453a89ba74022de47578f2556bd41Alan Viverette        super.onViewCreated(view, savedInstanceState);
1264098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1274098dba2535453a89ba74022de47578f2556bd41Alan Viverette        mPreviewText = (SubtitleView) view.findViewById(R.id.preview_text);
128d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mPreviewWindow = view.findViewById(R.id.preview_window);
1294098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1304098dba2535453a89ba74022de47578f2556bd41Alan Viverette        installActionBarToggleSwitch();
1314098dba2535453a89ba74022de47578f2556bd41Alan Viverette        refreshPreviewText();
1324098dba2535453a89ba74022de47578f2556bd41Alan Viverette    }
1334098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1344098dba2535453a89ba74022de47578f2556bd41Alan Viverette    private void refreshPreviewText() {
1354098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final Context context = getActivity();
1364098dba2535453a89ba74022de47578f2556bd41Alan Viverette        if (context == null) {
1374098dba2535453a89ba74022de47578f2556bd41Alan Viverette            // We've been destroyed, abort!
1384098dba2535453a89ba74022de47578f2556bd41Alan Viverette            return;
1394098dba2535453a89ba74022de47578f2556bd41Alan Viverette        }
1404098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1414098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final SubtitleView preview = mPreviewText;
1424098dba2535453a89ba74022de47578f2556bd41Alan Viverette        if (preview != null) {
1434098dba2535453a89ba74022de47578f2556bd41Alan Viverette            final int styleId = mCaptioningManager.getRawUserStyle();
1444098dba2535453a89ba74022de47578f2556bd41Alan Viverette            applyCaptionProperties(mCaptioningManager, preview, styleId);
1454098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1464098dba2535453a89ba74022de47578f2556bd41Alan Viverette            final Locale locale = mCaptioningManager.getLocale();
1474098dba2535453a89ba74022de47578f2556bd41Alan Viverette            if (locale != null) {
1484098dba2535453a89ba74022de47578f2556bd41Alan Viverette                final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
1494098dba2535453a89ba74022de47578f2556bd41Alan Viverette                        context, locale, R.string.captioning_preview_text);
1504098dba2535453a89ba74022de47578f2556bd41Alan Viverette                preview.setText(localizedText);
1514098dba2535453a89ba74022de47578f2556bd41Alan Viverette            } else {
1524098dba2535453a89ba74022de47578f2556bd41Alan Viverette                preview.setText(R.string.captioning_preview_text);
1534098dba2535453a89ba74022de47578f2556bd41Alan Viverette            }
154d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette
155d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette            final CaptionStyle style = mCaptioningManager.getUserStyle();
156d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette            mPreviewWindow.setBackgroundColor(style.windowColor);
1574098dba2535453a89ba74022de47578f2556bd41Alan Viverette        }
1584098dba2535453a89ba74022de47578f2556bd41Alan Viverette    }
1594098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1604098dba2535453a89ba74022de47578f2556bd41Alan Viverette    public static void applyCaptionProperties(
1614098dba2535453a89ba74022de47578f2556bd41Alan Viverette            CaptioningManager manager, SubtitleView previewText, int styleId) {
1624098dba2535453a89ba74022de47578f2556bd41Alan Viverette        previewText.setStyle(styleId);
1634098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1644098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final Context context = previewText.getContext();
1654098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final ContentResolver cr = context.getContentResolver();
1664098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final float fontScale = manager.getFontScale();
1674098dba2535453a89ba74022de47578f2556bd41Alan Viverette        previewText.setTextSize(fontScale * DEFAULT_FONT_SIZE);
1684098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1694098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final Locale locale = manager.getLocale();
1704098dba2535453a89ba74022de47578f2556bd41Alan Viverette        if (locale != null) {
1714098dba2535453a89ba74022de47578f2556bd41Alan Viverette            final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
1724098dba2535453a89ba74022de47578f2556bd41Alan Viverette                    context, locale, R.string.captioning_preview_characters);
1734098dba2535453a89ba74022de47578f2556bd41Alan Viverette            previewText.setText(localizedText);
1744098dba2535453a89ba74022de47578f2556bd41Alan Viverette        } else {
1754098dba2535453a89ba74022de47578f2556bd41Alan Viverette            previewText.setText(R.string.captioning_preview_characters);
1764098dba2535453a89ba74022de47578f2556bd41Alan Viverette        }
1774098dba2535453a89ba74022de47578f2556bd41Alan Viverette    }
1784098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1794098dba2535453a89ba74022de47578f2556bd41Alan Viverette    private void installActionBarToggleSwitch() {
1804098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final Activity activity = getActivity();
1814098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final ToggleSwitch toggleSwitch = new ToggleSwitch(activity);
1824098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1834098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final int padding = getResources().getDimensionPixelSize(
1844098dba2535453a89ba74022de47578f2556bd41Alan Viverette                R.dimen.action_bar_switch_padding);
1854098dba2535453a89ba74022de47578f2556bd41Alan Viverette        toggleSwitch.setPaddingRelative(0, 0, padding, 0);
1864098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1874098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final ActionBar actionBar = activity.getActionBar();
1884098dba2535453a89ba74022de47578f2556bd41Alan Viverette        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
1894098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1904098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final ActionBar.LayoutParams params = new ActionBar.LayoutParams(
1914098dba2535453a89ba74022de47578f2556bd41Alan Viverette                ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT,
1924098dba2535453a89ba74022de47578f2556bd41Alan Viverette                        Gravity.CENTER_VERTICAL | Gravity.END);
1934098dba2535453a89ba74022de47578f2556bd41Alan Viverette        actionBar.setCustomView(toggleSwitch, params);
1944098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1954098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final boolean enabled = mCaptioningManager.isEnabled();
1964098dba2535453a89ba74022de47578f2556bd41Alan Viverette        getPreferenceScreen().setEnabled(enabled);
1974098dba2535453a89ba74022de47578f2556bd41Alan Viverette        mPreviewText.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
1984098dba2535453a89ba74022de47578f2556bd41Alan Viverette        toggleSwitch.setCheckedInternal(enabled);
1994098dba2535453a89ba74022de47578f2556bd41Alan Viverette        toggleSwitch.setOnBeforeCheckedChangeListener(new OnBeforeCheckedChangeListener() {
2004098dba2535453a89ba74022de47578f2556bd41Alan Viverette            @Override
2014098dba2535453a89ba74022de47578f2556bd41Alan Viverette            public boolean onBeforeCheckedChanged(ToggleSwitch toggleSwitch, boolean checked) {
2024098dba2535453a89ba74022de47578f2556bd41Alan Viverette                toggleSwitch.setCheckedInternal(checked);
2034098dba2535453a89ba74022de47578f2556bd41Alan Viverette                Settings.Secure.putInt(getActivity().getContentResolver(),
2044098dba2535453a89ba74022de47578f2556bd41Alan Viverette                        Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, checked ? 1 : 0);
2054098dba2535453a89ba74022de47578f2556bd41Alan Viverette                getPreferenceScreen().setEnabled(checked);
2064098dba2535453a89ba74022de47578f2556bd41Alan Viverette                mPreviewText.setVisibility(checked ? View.VISIBLE : View.INVISIBLE);
2074098dba2535453a89ba74022de47578f2556bd41Alan Viverette                return false;
2084098dba2535453a89ba74022de47578f2556bd41Alan Viverette            }
2094098dba2535453a89ba74022de47578f2556bd41Alan Viverette        });
210cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
211cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
212cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private void initializeAllPreferences() {
213205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mLocale = (LocalePreference) findPreference(PREF_LOCALE);
214205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mFontSize = (ListPreference) findPreference(PREF_FONT_SIZE);
215660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette
216cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final Resources res = getResources();
217cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final int[] presetValues = res.getIntArray(R.array.captioning_preset_selector_values);
218cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final String[] presetTitles = res.getStringArray(R.array.captioning_preset_selector_titles);
219205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mPreset = (PresetPreference) findPreference(PREF_PRESET);
220cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mPreset.setValues(presetValues);
221cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mPreset.setTitles(presetTitles);
222cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
223205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mCustom = (PreferenceCategory) findPreference(PREF_CUSTOM);
224660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        mShowingCustom = true;
225660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette
226cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final int[] colorValues = res.getIntArray(R.array.captioning_color_selector_values);
227cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final String[] colorTitles = res.getStringArray(R.array.captioning_color_selector_titles);
228205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mForegroundColor = (ColorPreference) mCustom.findPreference(PREF_FOREGROUND_COLOR);
229cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mForegroundColor.setTitles(colorTitles);
230cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mForegroundColor.setValues(colorValues);
231205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette
232205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        final int[] opacityValues = res.getIntArray(R.array.captioning_opacity_selector_values);
233205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        final String[] opacityTitles = res.getStringArray(
234205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette                R.array.captioning_opacity_selector_titles);
235205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mForegroundOpacity = (ColorPreference) mCustom.findPreference(PREF_FOREGROUND_OPACITY);
236205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mForegroundOpacity.setTitles(opacityTitles);
237205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mForegroundOpacity.setValues(opacityValues);
238205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette
239205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mEdgeColor = (ColorPreference) mCustom.findPreference(PREF_EDGE_COLOR);
240cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mEdgeColor.setTitles(colorTitles);
241cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mEdgeColor.setValues(colorValues);
242cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
243660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        // Add "none" as an additional option for backgrounds.
244660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        final int[] bgColorValues = new int[colorValues.length + 1];
245660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        final String[] bgColorTitles = new String[colorTitles.length + 1];
246660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        System.arraycopy(colorValues, 0, bgColorValues, 1, colorValues.length);
247660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        System.arraycopy(colorTitles, 0, bgColorTitles, 1, colorTitles.length);
248660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        bgColorValues[0] = Color.TRANSPARENT;
249660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        bgColorTitles[0] = getString(R.string.color_none);
250205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mBackgroundColor = (ColorPreference) mCustom.findPreference(PREF_BACKGROUND_COLOR);
251cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mBackgroundColor.setTitles(bgColorTitles);
252cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mBackgroundColor.setValues(bgColorValues);
253cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
254205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mBackgroundOpacity = (ColorPreference) mCustom.findPreference(PREF_BACKGROUND_OPACITY);
255cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mBackgroundOpacity.setTitles(opacityTitles);
256cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mBackgroundOpacity.setValues(opacityValues);
257cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
258d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowColor = (ColorPreference) mCustom.findPreference(PREF_WINDOW_COLOR);
259d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowColor.setTitles(bgColorTitles);
260d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowColor.setValues(bgColorValues);
261d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette
262d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowOpacity = (ColorPreference) mCustom.findPreference(PREF_WINDOW_OPACITY);
263d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowOpacity.setTitles(opacityTitles);
264d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowOpacity.setValues(opacityValues);
265d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette
266205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mEdgeType = (EdgeTypePreference) mCustom.findPreference(PREF_EDGE_TYPE);
267205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mTypeface = (ListPreference) mCustom.findPreference(PREF_TYPEFACE);
268cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
269cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
270cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private void installUpdateListeners() {
271cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mPreset.setOnValueChangedListener(this);
272cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mForegroundColor.setOnValueChangedListener(this);
273205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mForegroundOpacity.setOnValueChangedListener(this);
274cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mEdgeColor.setOnValueChangedListener(this);
275cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mBackgroundColor.setOnValueChangedListener(this);
276cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mBackgroundOpacity.setOnValueChangedListener(this);
277d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowColor.setOnValueChangedListener(this);
278d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowOpacity.setOnValueChangedListener(this);
279cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mEdgeType.setOnValueChangedListener(this);
280cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
281cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mTypeface.setOnPreferenceChangeListener(this);
282cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mFontSize.setOnPreferenceChangeListener(this);
283cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mLocale.setOnPreferenceChangeListener(this);
284cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
285cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
286cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private void updateAllPreferences() {
2875ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette        final int preset = mCaptioningManager.getRawUserStyle();
288cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mPreset.setValue(preset);
289cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
2905ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette        final float fontSize = mCaptioningManager.getFontScale();
291cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mFontSize.setValue(Float.toString(fontSize));
292cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
2935ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette        final ContentResolver cr = getContentResolver();
294cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final CaptionStyle attrs = CaptionStyle.getCustomStyle(cr);
295cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mEdgeType.setValue(attrs.edgeType);
296cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mEdgeColor.setValue(attrs.edgeColor);
297cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
298205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        parseColorOpacity(mForegroundColor, mForegroundOpacity, attrs.foregroundColor);
299205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        parseColorOpacity(mBackgroundColor, mBackgroundOpacity, attrs.backgroundColor);
300d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        parseColorOpacity(mWindowColor, mWindowOpacity, attrs.windowColor);
301cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
302cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final String rawTypeface = attrs.mRawTypeface;
303cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mTypeface.setValue(rawTypeface == null ? "" : rawTypeface);
304cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
3055ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette        final String rawLocale = mCaptioningManager.getRawLocale();
306cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mLocale.setValue(rawLocale == null ? "" : rawLocale);
307cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
308cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
309205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private void parseColorOpacity(ColorPreference color, ColorPreference opacity, int value) {
310205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        final int colorValue;
311205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        final int opacityValue;
312205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        if (Color.alpha(value) == 0) {
313205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette            colorValue = Color.TRANSPARENT;
314205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette            opacityValue = (value & 0xFF) << 24;
315205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        } else {
316205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette            colorValue = value | 0xFF000000;
317205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette            opacityValue = value & 0xFF000000;
318205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        }
319205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        color.setValue(colorValue);
320205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        opacity.setValue(opacityValue | 0xFFFFFF);
321205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    }
322205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette
323205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private int mergeColorOpacity(ColorPreference color, ColorPreference opacity) {
324205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        final int colorValue = color.getValue();
325205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        final int opacityValue = opacity.getValue();
326205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        final int value;
327205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        if (Color.alpha(colorValue) == 0) {
328205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette            value = Color.alpha(opacityValue);
329205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        } else {
330205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette            value = colorValue & 0x00FFFFFF | opacityValue & 0xFF000000;
331205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        }
332205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        return value;
333205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    }
334205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette
335660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette    private void refreshShowingCustom() {
336660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        final boolean customPreset = mPreset.getValue() == CaptionStyle.PRESET_CUSTOM;
337660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        if (!customPreset && mShowingCustom) {
338660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette            getPreferenceScreen().removePreference(mCustom);
339660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette            mShowingCustom = false;
340660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        } else if (customPreset && !mShowingCustom) {
341660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette            getPreferenceScreen().addPreference(mCustom);
342660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette            mShowingCustom = true;
343660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        }
344660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette    }
345660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette
346cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    @Override
347cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    public void onValueChanged(ListDialogPreference preference, int value) {
348cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final ContentResolver cr = getActivity().getContentResolver();
349205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        if (mForegroundColor == preference || mForegroundOpacity == preference) {
350205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette            final int merged = mergeColorOpacity(mForegroundColor, mForegroundOpacity);
351cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putInt(
352205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, merged);
353cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        } else if (mBackgroundColor == preference || mBackgroundOpacity == preference) {
354205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette            final int merged = mergeColorOpacity(mBackgroundColor, mBackgroundOpacity);
355cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putInt(
356205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, merged);
357d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        } else if (mWindowColor == preference || mWindowOpacity == preference) {
358d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette            final int merged = mergeColorOpacity(mWindowColor, mWindowOpacity);
359d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette            Settings.Secure.putInt(
360d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, merged);
361cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        } else if (mEdgeColor == preference) {
362cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_COLOR, value);
363cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        } else if (mPreset == preference) {
364cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, value);
365660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette            refreshShowingCustom();
366cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        } else if (mEdgeType == preference) {
367cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_TYPE, value);
368cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        }
369cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
370cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        refreshPreviewText();
371cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
372cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
373cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    @Override
374cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    public boolean onPreferenceChange(Preference preference, Object value) {
375cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final ContentResolver cr = getActivity().getContentResolver();
376cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        if (mTypeface == preference) {
377cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putString(
378cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_TYPEFACE, (String) value);
379cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        } else if (mFontSize == preference) {
380cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putFloat(
3815ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE,
382cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                    Float.parseFloat((String) value));
383cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        } else if (mLocale == preference) {
384cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putString(
385cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE, (String) value);
386cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        }
387cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
388cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        refreshPreviewText();
389cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        return true;
390cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
391cc0e782871eb6b946ded880e391866f27953654bAlan Viverette}
392