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
19cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.content.ContentResolver;
205ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viveretteimport android.content.Context;
21cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.content.res.Resources;
22cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.graphics.Color;
23cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.os.Bundle;
244098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport android.preference.PreferenceFrameLayout;
25cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.provider.Settings;
2639b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport android.support.v7.preference.ListPreference;
2739b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport android.support.v7.preference.Preference;
2839b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport android.support.v7.preference.Preference.OnPreferenceChangeListener;
2939b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport android.support.v7.preference.PreferenceCategory;
304098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport android.view.LayoutInflater;
314098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport android.view.View;
32a83a532dde44341319810958d67aabb02ff7aea6Alan Viveretteimport android.view.View.OnLayoutChangeListener;
334098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport android.view.ViewGroup;
344098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport android.view.ViewGroup.LayoutParams;
35cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.view.accessibility.CaptioningManager;
36cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.view.accessibility.CaptioningManager.CaptionStyle;
37cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
389d1bfd1e8de6e46137a9571507c03526880d6a46Chris Wrenimport com.android.internal.logging.MetricsProto.MetricsEvent;
394098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport com.android.internal.widget.SubtitleView;
40cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport com.android.settings.R;
416220275d6dec8ffc6f1df2cbfbf42934d09d41f0Fabrice Di Meglioimport com.android.settings.SettingsActivity;
42cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport com.android.settings.SettingsPreferenceFragment;
43cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport com.android.settings.accessibility.ListDialogPreference.OnValueChangedListener;
446220275d6dec8ffc6f1df2cbfbf42934d09d41f0Fabrice Di Meglioimport com.android.settings.widget.SwitchBar;
455160536df91bb9daa6cac9a878ce406deda791c6Fabrice Di Meglioimport com.android.settings.widget.ToggleSwitch;
465160536df91bb9daa6cac9a878ce406deda791c6Fabrice Di Meglioimport com.android.settings.widget.ToggleSwitch.OnBeforeCheckedChangeListener;
47351321c2b5c263c183c52716c5b7b25ff635b0aaTony Mantlerimport com.android.settingslib.accessibility.AccessibilityUtils;
484098dba2535453a89ba74022de47578f2556bd41Alan Viverette
494098dba2535453a89ba74022de47578f2556bd41Alan Viveretteimport java.util.Locale;
50cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
51cc0e782871eb6b946ded880e391866f27953654bAlan Viverette/**
52cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * Settings fragment containing captioning properties.
53cc0e782871eb6b946ded880e391866f27953654bAlan Viverette */
54cc0e782871eb6b946ded880e391866f27953654bAlan Viverettepublic class CaptionPropertiesFragment extends SettingsPreferenceFragment
55cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        implements OnPreferenceChangeListener, OnValueChangedListener {
56205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_BACKGROUND_COLOR = "captioning_background_color";
57205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_BACKGROUND_OPACITY = "captioning_background_opacity";
58205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_FOREGROUND_COLOR = "captioning_foreground_color";
59205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_FOREGROUND_OPACITY = "captioning_foreground_opacity";
60d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette    private static final String PREF_WINDOW_COLOR = "captioning_window_color";
61d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette    private static final String PREF_WINDOW_OPACITY = "captioning_window_opacity";
62205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_EDGE_COLOR = "captioning_edge_color";
63205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_EDGE_TYPE = "captioning_edge_type";
64205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_FONT_SIZE = "captioning_font_size";
65205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_TYPEFACE = "captioning_typeface";
66205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_LOCALE = "captioning_locale";
67205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_PRESET = "captioning_preset";
68205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private static final String PREF_CUSTOM = "custom";
69205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette
70a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette    /** WebVtt specifies line height as 5.3% of the viewport height. */
71a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette    private static final float LINE_HEIGHT_RATIO = 0.0533f;
724098dba2535453a89ba74022de47578f2556bd41Alan Viverette
735ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette    private CaptioningManager mCaptioningManager;
744098dba2535453a89ba74022de47578f2556bd41Alan Viverette    private SubtitleView mPreviewText;
75d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette    private View mPreviewWindow;
76a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette    private View mPreviewViewport;
776220275d6dec8ffc6f1df2cbfbf42934d09d41f0Fabrice Di Meglio    private SwitchBar mSwitchBar;
78061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette    private ToggleSwitch mToggleSwitch;
79cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
80cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    // Standard options.
81cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private LocalePreference mLocale;
82cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private ListPreference mFontSize;
83cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private PresetPreference mPreset;
84cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
85cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    // Custom options.
86cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private ListPreference mTypeface;
87cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private ColorPreference mForegroundColor;
88205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private ColorPreference mForegroundOpacity;
89cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private EdgeTypePreference mEdgeType;
90cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private ColorPreference mEdgeColor;
91cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private ColorPreference mBackgroundColor;
92cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private ColorPreference mBackgroundOpacity;
93d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette    private ColorPreference mWindowColor;
94d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette    private ColorPreference mWindowOpacity;
95660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette    private PreferenceCategory mCustom;
96660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette
97660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette    private boolean mShowingCustom;
98cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
99cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    @Override
1008a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    protected int getMetricsCategory() {
1019d1bfd1e8de6e46137a9571507c03526880d6a46Chris Wren        return MetricsEvent.ACCESSIBILITY_CAPTION_PROPERTIES;
1028a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    }
1038a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren
1048a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    @Override
105cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    public void onCreate(Bundle icicle) {
106cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        super.onCreate(icicle);
1075ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette
1085ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette        mCaptioningManager = (CaptioningManager) getSystemService(Context.CAPTIONING_SERVICE);
1095ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette
110cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        addPreferencesFromResource(R.xml.captioning_settings);
111cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        initializeAllPreferences();
112cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        updateAllPreferences();
113660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        refreshShowingCustom();
114cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        installUpdateListeners();
115cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
116cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
1174098dba2535453a89ba74022de47578f2556bd41Alan Viverette    @Override
1184098dba2535453a89ba74022de47578f2556bd41Alan Viverette    public View onCreateView(
1194098dba2535453a89ba74022de47578f2556bd41Alan Viverette            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
1204098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final View rootView = inflater.inflate(R.layout.captioning_preview, container, false);
1214098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1224098dba2535453a89ba74022de47578f2556bd41Alan Viverette        // We have to do this now because PreferenceFrameLayout looks at it
1234098dba2535453a89ba74022de47578f2556bd41Alan Viverette        // only when the view is added.
1244098dba2535453a89ba74022de47578f2556bd41Alan Viverette        if (container instanceof PreferenceFrameLayout) {
1254098dba2535453a89ba74022de47578f2556bd41Alan Viverette            ((PreferenceFrameLayout.LayoutParams) rootView.getLayoutParams()).removeBorders = true;
1264098dba2535453a89ba74022de47578f2556bd41Alan Viverette        }
1274098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1284098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final View content = super.onCreateView(inflater, container, savedInstanceState);
1294098dba2535453a89ba74022de47578f2556bd41Alan Viverette        ((ViewGroup) rootView.findViewById(R.id.properties_fragment)).addView(
1304098dba2535453a89ba74022de47578f2556bd41Alan Viverette                content, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
1314098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1324098dba2535453a89ba74022de47578f2556bd41Alan Viverette        return rootView;
1334098dba2535453a89ba74022de47578f2556bd41Alan Viverette    }
1344098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1354098dba2535453a89ba74022de47578f2556bd41Alan Viverette    @Override
1364098dba2535453a89ba74022de47578f2556bd41Alan Viverette    public void onViewCreated(View view, Bundle savedInstanceState) {
1374098dba2535453a89ba74022de47578f2556bd41Alan Viverette        super.onViewCreated(view, savedInstanceState);
1384098dba2535453a89ba74022de47578f2556bd41Alan Viverette
139061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette        final boolean enabled = mCaptioningManager.isEnabled();
1404098dba2535453a89ba74022de47578f2556bd41Alan Viverette        mPreviewText = (SubtitleView) view.findViewById(R.id.preview_text);
141061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette        mPreviewText.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
142061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette
143a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette        mPreviewWindow = view.findViewById(R.id.preview_window);
144a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette        mPreviewViewport = view.findViewById(R.id.preview_viewport);
145a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette        mPreviewViewport.addOnLayoutChangeListener(new OnLayoutChangeListener() {
146a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette            @Override
147a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette            public void onLayoutChange(View v, int left, int top, int right, int bottom,
148a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette                    int oldLeft, int oldTop, int oldRight, int oldBottom) {
149a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette                refreshPreviewText();
150a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette            }
151a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette        });
152138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio    }
153138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio
154138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio    @Override
155138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio    public void onActivityCreated(Bundle savedInstanceState) {
156138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio        super.onActivityCreated(savedInstanceState);
157a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette
158138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio        final boolean enabled = mCaptioningManager.isEnabled();
1596220275d6dec8ffc6f1df2cbfbf42934d09d41f0Fabrice Di Meglio        SettingsActivity activity = (SettingsActivity) getActivity();
1606220275d6dec8ffc6f1df2cbfbf42934d09d41f0Fabrice Di Meglio        mSwitchBar = activity.getSwitchBar();
161006b2cca1311e61472a8b66fb0c50854fc36d2e7Fabrice Di Meglio        mSwitchBar.setCheckedInternal(enabled);
1626220275d6dec8ffc6f1df2cbfbf42934d09d41f0Fabrice Di Meglio        mToggleSwitch = mSwitchBar.getSwitch();
163061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette
164061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette        getPreferenceScreen().setEnabled(enabled);
165061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette
1664098dba2535453a89ba74022de47578f2556bd41Alan Viverette        refreshPreviewText();
1674098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1686220275d6dec8ffc6f1df2cbfbf42934d09d41f0Fabrice Di Meglio        installSwitchBarToggleSwitch();
169061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette    }
170061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette
171061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette    @Override
172138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio    public void onDestroyView() {
173138ff8c0457c6e1345015973668d652fa17c7c3bFabrice Di Meglio        super.onDestroyView();
1746220275d6dec8ffc6f1df2cbfbf42934d09d41f0Fabrice Di Meglio        removeSwitchBarToggleSwitch();
175061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette    }
176061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette
1774098dba2535453a89ba74022de47578f2556bd41Alan Viverette    private void refreshPreviewText() {
1784098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final Context context = getActivity();
1794098dba2535453a89ba74022de47578f2556bd41Alan Viverette        if (context == null) {
1804098dba2535453a89ba74022de47578f2556bd41Alan Viverette            // We've been destroyed, abort!
1814098dba2535453a89ba74022de47578f2556bd41Alan Viverette            return;
1824098dba2535453a89ba74022de47578f2556bd41Alan Viverette        }
1834098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1844098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final SubtitleView preview = mPreviewText;
1854098dba2535453a89ba74022de47578f2556bd41Alan Viverette        if (preview != null) {
1864098dba2535453a89ba74022de47578f2556bd41Alan Viverette            final int styleId = mCaptioningManager.getRawUserStyle();
187a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette            applyCaptionProperties(mCaptioningManager, preview, mPreviewViewport, styleId);
1884098dba2535453a89ba74022de47578f2556bd41Alan Viverette
1894098dba2535453a89ba74022de47578f2556bd41Alan Viverette            final Locale locale = mCaptioningManager.getLocale();
1904098dba2535453a89ba74022de47578f2556bd41Alan Viverette            if (locale != null) {
1914098dba2535453a89ba74022de47578f2556bd41Alan Viverette                final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
1924098dba2535453a89ba74022de47578f2556bd41Alan Viverette                        context, locale, R.string.captioning_preview_text);
1934098dba2535453a89ba74022de47578f2556bd41Alan Viverette                preview.setText(localizedText);
1944098dba2535453a89ba74022de47578f2556bd41Alan Viverette            } else {
1954098dba2535453a89ba74022de47578f2556bd41Alan Viverette                preview.setText(R.string.captioning_preview_text);
1964098dba2535453a89ba74022de47578f2556bd41Alan Viverette            }
197d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette
198d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette            final CaptionStyle style = mCaptioningManager.getUserStyle();
199a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette            if (style.hasWindowColor()) {
200a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette                mPreviewWindow.setBackgroundColor(style.windowColor);
201a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette            } else {
202a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette                final CaptionStyle defStyle = CaptionStyle.DEFAULT;
203a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette                mPreviewWindow.setBackgroundColor(defStyle.windowColor);
204a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette            }
2054098dba2535453a89ba74022de47578f2556bd41Alan Viverette        }
2064098dba2535453a89ba74022de47578f2556bd41Alan Viverette    }
2074098dba2535453a89ba74022de47578f2556bd41Alan Viverette
208a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette    public static void applyCaptionProperties(CaptioningManager manager, SubtitleView previewText,
209a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette            View previewWindow, int styleId) {
2104098dba2535453a89ba74022de47578f2556bd41Alan Viverette        previewText.setStyle(styleId);
2114098dba2535453a89ba74022de47578f2556bd41Alan Viverette
2124098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final Context context = previewText.getContext();
2134098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final ContentResolver cr = context.getContentResolver();
2144098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final float fontScale = manager.getFontScale();
215a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette        if (previewWindow != null) {
216389287e5f8a1fe137de959034de39608830878d1Alan Viverette            // Assume the viewport is clipped with a 16:9 aspect ratio.
217389287e5f8a1fe137de959034de39608830878d1Alan Viverette            final float virtualHeight = Math.max(9 * previewWindow.getWidth(),
218389287e5f8a1fe137de959034de39608830878d1Alan Viverette                    16 * previewWindow.getHeight()) / 16.0f;
219389287e5f8a1fe137de959034de39608830878d1Alan Viverette            previewText.setTextSize(virtualHeight * LINE_HEIGHT_RATIO * fontScale);
220a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette        } else {
221a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette            final float textSize = context.getResources().getDimension(
222a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette                    R.dimen.caption_preview_text_size);
223a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette            previewText.setTextSize(textSize * fontScale);
224a83a532dde44341319810958d67aabb02ff7aea6Alan Viverette        }
2254098dba2535453a89ba74022de47578f2556bd41Alan Viverette
2264098dba2535453a89ba74022de47578f2556bd41Alan Viverette        final Locale locale = manager.getLocale();
2274098dba2535453a89ba74022de47578f2556bd41Alan Viverette        if (locale != null) {
2284098dba2535453a89ba74022de47578f2556bd41Alan Viverette            final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
2294098dba2535453a89ba74022de47578f2556bd41Alan Viverette                    context, locale, R.string.captioning_preview_characters);
2304098dba2535453a89ba74022de47578f2556bd41Alan Viverette            previewText.setText(localizedText);
2314098dba2535453a89ba74022de47578f2556bd41Alan Viverette        } else {
2324098dba2535453a89ba74022de47578f2556bd41Alan Viverette            previewText.setText(R.string.captioning_preview_characters);
2334098dba2535453a89ba74022de47578f2556bd41Alan Viverette        }
2344098dba2535453a89ba74022de47578f2556bd41Alan Viverette    }
2354098dba2535453a89ba74022de47578f2556bd41Alan Viverette
2366220275d6dec8ffc6f1df2cbfbf42934d09d41f0Fabrice Di Meglio    protected void onInstallSwitchBarToggleSwitch() {
237061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette        mToggleSwitch.setOnBeforeCheckedChangeListener(new OnBeforeCheckedChangeListener() {
2384098dba2535453a89ba74022de47578f2556bd41Alan Viverette            @Override
2394098dba2535453a89ba74022de47578f2556bd41Alan Viverette            public boolean onBeforeCheckedChanged(ToggleSwitch toggleSwitch, boolean checked) {
240006b2cca1311e61472a8b66fb0c50854fc36d2e7Fabrice Di Meglio                mSwitchBar.setCheckedInternal(checked);
2414098dba2535453a89ba74022de47578f2556bd41Alan Viverette                Settings.Secure.putInt(getActivity().getContentResolver(),
2424098dba2535453a89ba74022de47578f2556bd41Alan Viverette                        Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, checked ? 1 : 0);
2434098dba2535453a89ba74022de47578f2556bd41Alan Viverette                getPreferenceScreen().setEnabled(checked);
244061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette                if (mPreviewText != null) {
245061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette                    mPreviewText.setVisibility(checked ? View.VISIBLE : View.INVISIBLE);
246061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette                }
2474098dba2535453a89ba74022de47578f2556bd41Alan Viverette                return false;
2484098dba2535453a89ba74022de47578f2556bd41Alan Viverette            }
2494098dba2535453a89ba74022de47578f2556bd41Alan Viverette        });
250cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
251cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
2526220275d6dec8ffc6f1df2cbfbf42934d09d41f0Fabrice Di Meglio    private void installSwitchBarToggleSwitch() {
2536220275d6dec8ffc6f1df2cbfbf42934d09d41f0Fabrice Di Meglio        onInstallSwitchBarToggleSwitch();
2546220275d6dec8ffc6f1df2cbfbf42934d09d41f0Fabrice Di Meglio        mSwitchBar.show();
255061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette    }
256061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette
2576220275d6dec8ffc6f1df2cbfbf42934d09d41f0Fabrice Di Meglio    private void removeSwitchBarToggleSwitch() {
2586220275d6dec8ffc6f1df2cbfbf42934d09d41f0Fabrice Di Meglio        mSwitchBar.hide();
259061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette        mToggleSwitch.setOnBeforeCheckedChangeListener(null);
260061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette    }
261061333c26bbb848b8982b04d6043061cb2451a90Alan Viverette
262cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private void initializeAllPreferences() {
263205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mLocale = (LocalePreference) findPreference(PREF_LOCALE);
264205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mFontSize = (ListPreference) findPreference(PREF_FONT_SIZE);
265660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette
266cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final Resources res = getResources();
267cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final int[] presetValues = res.getIntArray(R.array.captioning_preset_selector_values);
268cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final String[] presetTitles = res.getStringArray(R.array.captioning_preset_selector_titles);
269205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mPreset = (PresetPreference) findPreference(PREF_PRESET);
270cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mPreset.setValues(presetValues);
271cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mPreset.setTitles(presetTitles);
272cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
273205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mCustom = (PreferenceCategory) findPreference(PREF_CUSTOM);
274660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        mShowingCustom = true;
275660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette
276cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final int[] colorValues = res.getIntArray(R.array.captioning_color_selector_values);
277cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final String[] colorTitles = res.getStringArray(R.array.captioning_color_selector_titles);
278205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mForegroundColor = (ColorPreference) mCustom.findPreference(PREF_FOREGROUND_COLOR);
279cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mForegroundColor.setTitles(colorTitles);
280cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mForegroundColor.setValues(colorValues);
281205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette
282205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        final int[] opacityValues = res.getIntArray(R.array.captioning_opacity_selector_values);
283205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        final String[] opacityTitles = res.getStringArray(
284205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette                R.array.captioning_opacity_selector_titles);
285205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mForegroundOpacity = (ColorPreference) mCustom.findPreference(PREF_FOREGROUND_OPACITY);
286205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mForegroundOpacity.setTitles(opacityTitles);
287205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mForegroundOpacity.setValues(opacityValues);
288205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette
289205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mEdgeColor = (ColorPreference) mCustom.findPreference(PREF_EDGE_COLOR);
290cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mEdgeColor.setTitles(colorTitles);
291cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mEdgeColor.setValues(colorValues);
292cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
293660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        // Add "none" as an additional option for backgrounds.
294660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        final int[] bgColorValues = new int[colorValues.length + 1];
295660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        final String[] bgColorTitles = new String[colorTitles.length + 1];
296660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        System.arraycopy(colorValues, 0, bgColorValues, 1, colorValues.length);
297660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        System.arraycopy(colorTitles, 0, bgColorTitles, 1, colorTitles.length);
298660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        bgColorValues[0] = Color.TRANSPARENT;
299660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        bgColorTitles[0] = getString(R.string.color_none);
300205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mBackgroundColor = (ColorPreference) mCustom.findPreference(PREF_BACKGROUND_COLOR);
301cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mBackgroundColor.setTitles(bgColorTitles);
302cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mBackgroundColor.setValues(bgColorValues);
303cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
304205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mBackgroundOpacity = (ColorPreference) mCustom.findPreference(PREF_BACKGROUND_OPACITY);
305cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mBackgroundOpacity.setTitles(opacityTitles);
306cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mBackgroundOpacity.setValues(opacityValues);
307cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
308d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowColor = (ColorPreference) mCustom.findPreference(PREF_WINDOW_COLOR);
309d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowColor.setTitles(bgColorTitles);
310d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowColor.setValues(bgColorValues);
311d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette
312d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowOpacity = (ColorPreference) mCustom.findPreference(PREF_WINDOW_OPACITY);
313d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowOpacity.setTitles(opacityTitles);
314d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowOpacity.setValues(opacityValues);
315d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette
316205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mEdgeType = (EdgeTypePreference) mCustom.findPreference(PREF_EDGE_TYPE);
317205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mTypeface = (ListPreference) mCustom.findPreference(PREF_TYPEFACE);
318cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
319cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
320cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private void installUpdateListeners() {
321cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mPreset.setOnValueChangedListener(this);
322cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mForegroundColor.setOnValueChangedListener(this);
323205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        mForegroundOpacity.setOnValueChangedListener(this);
324cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mEdgeColor.setOnValueChangedListener(this);
325cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mBackgroundColor.setOnValueChangedListener(this);
326cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mBackgroundOpacity.setOnValueChangedListener(this);
327d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowColor.setOnValueChangedListener(this);
328d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        mWindowOpacity.setOnValueChangedListener(this);
329cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mEdgeType.setOnValueChangedListener(this);
330cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
331cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mTypeface.setOnPreferenceChangeListener(this);
332cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mFontSize.setOnPreferenceChangeListener(this);
333cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mLocale.setOnPreferenceChangeListener(this);
334cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
335cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
336cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private void updateAllPreferences() {
3375ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette        final int preset = mCaptioningManager.getRawUserStyle();
338cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mPreset.setValue(preset);
339cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
3405ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette        final float fontSize = mCaptioningManager.getFontScale();
341cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mFontSize.setValue(Float.toString(fontSize));
342cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
3435ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette        final ContentResolver cr = getContentResolver();
344cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final CaptionStyle attrs = CaptionStyle.getCustomStyle(cr);
345cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mEdgeType.setValue(attrs.edgeType);
346cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mEdgeColor.setValue(attrs.edgeColor);
347cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
348751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette        final int foregroundColor = attrs.hasForegroundColor() ?
349751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette                attrs.foregroundColor : CaptionStyle.COLOR_UNSPECIFIED;
350751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette        parseColorOpacity(mForegroundColor, mForegroundOpacity, foregroundColor);
351751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette
352751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette        final int backgroundColor = attrs.hasBackgroundColor() ?
353751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette                attrs.backgroundColor : CaptionStyle.COLOR_UNSPECIFIED;
354751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette        parseColorOpacity(mBackgroundColor, mBackgroundOpacity, backgroundColor);
355751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette
356751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette        final int windowColor = attrs.hasWindowColor() ?
357751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette                attrs.windowColor : CaptionStyle.COLOR_UNSPECIFIED;
358751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette        parseColorOpacity(mWindowColor, mWindowOpacity, windowColor);
359cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
360cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final String rawTypeface = attrs.mRawTypeface;
361cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mTypeface.setValue(rawTypeface == null ? "" : rawTypeface);
362cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
3635ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette        final String rawLocale = mCaptioningManager.getRawLocale();
364cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mLocale.setValue(rawLocale == null ? "" : rawLocale);
365cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
366cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
367751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette    /**
368751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette     * Unpack the specified color value and update the preferences.
369751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette     *
370751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette     * @param color color preference
371751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette     * @param opacity opacity preference
372751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette     * @param value packed value
373751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette     */
374205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private void parseColorOpacity(ColorPreference color, ColorPreference opacity, int value) {
375205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        final int colorValue;
376205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        final int opacityValue;
377751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette        if (!CaptionStyle.hasColor(value)) {
378751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette            // "Default" color with variable alpha.
379751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette            colorValue = CaptionStyle.COLOR_UNSPECIFIED;
380751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette            opacityValue = (value & 0xFF) << 24;
381751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette        } else if ((value >>> 24) == 0) {
382751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette            // "None" color with variable alpha.
383205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette            colorValue = Color.TRANSPARENT;
384205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette            opacityValue = (value & 0xFF) << 24;
385205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        } else {
386751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette            // Normal color.
387205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette            colorValue = value | 0xFF000000;
388205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette            opacityValue = value & 0xFF000000;
389205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        }
390751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette
391751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette        // Opacity value is always white.
392205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        opacity.setValue(opacityValue | 0xFFFFFF);
393751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette        color.setValue(colorValue);
394205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    }
395205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette
396205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    private int mergeColorOpacity(ColorPreference color, ColorPreference opacity) {
397205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        final int colorValue = color.getValue();
398205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        final int opacityValue = opacity.getValue();
399205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        final int value;
400751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette        // "Default" is 0x00FFFFFF or, for legacy support, 0x00000100.
401751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette        if (!CaptionStyle.hasColor(colorValue)) {
402751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette            // Encode "default" as 0x00FFFFaa.
403751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette            value = 0x00FFFF00 | Color.alpha(opacityValue);
404751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette        } else if (colorValue == Color.TRANSPARENT) {
405751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette            // Encode "none" as 0x000000aa.
406751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette            value = Color.alpha(opacityValue);
407205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        } else {
408751625e9a20cdd11073cdadb06b2c18464e9701cAlan Viverette            // Encode custom color normally.
409205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette            value = colorValue & 0x00FFFFFF | opacityValue & 0xFF000000;
410205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        }
411205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        return value;
412205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette    }
413205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette
414660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette    private void refreshShowingCustom() {
415660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        final boolean customPreset = mPreset.getValue() == CaptionStyle.PRESET_CUSTOM;
416660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        if (!customPreset && mShowingCustom) {
417660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette            getPreferenceScreen().removePreference(mCustom);
418660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette            mShowingCustom = false;
419660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        } else if (customPreset && !mShowingCustom) {
420660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette            getPreferenceScreen().addPreference(mCustom);
421660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette            mShowingCustom = true;
422660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette        }
423660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette    }
424660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette
425cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    @Override
426cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    public void onValueChanged(ListDialogPreference preference, int value) {
427cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final ContentResolver cr = getActivity().getContentResolver();
428205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette        if (mForegroundColor == preference || mForegroundOpacity == preference) {
429205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette            final int merged = mergeColorOpacity(mForegroundColor, mForegroundOpacity);
430cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putInt(
431205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, merged);
432cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        } else if (mBackgroundColor == preference || mBackgroundOpacity == preference) {
433205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette            final int merged = mergeColorOpacity(mBackgroundColor, mBackgroundOpacity);
434cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putInt(
435205cee4c1c0d0088c3a8e16093a28fa8f680d7faAlan Viverette                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, merged);
436d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette        } else if (mWindowColor == preference || mWindowOpacity == preference) {
437d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette            final int merged = mergeColorOpacity(mWindowColor, mWindowOpacity);
438d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette            Settings.Secure.putInt(
439d07c16107d7cda495a8a2655cdcb0ad552d25a84Alan Viverette                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, merged);
440cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        } else if (mEdgeColor == preference) {
441cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_COLOR, value);
442cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        } else if (mPreset == preference) {
443cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, value);
444660223ff430022aeabc7fb5947d1e1a5116f57d7Alan Viverette            refreshShowingCustom();
445cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        } else if (mEdgeType == preference) {
446cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_EDGE_TYPE, value);
447cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        }
448cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
449cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        refreshPreviewText();
450cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
451cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
452cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    @Override
453cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    public boolean onPreferenceChange(Preference preference, Object value) {
454cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final ContentResolver cr = getActivity().getContentResolver();
455cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        if (mTypeface == preference) {
456cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putString(
457cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_TYPEFACE, (String) value);
458cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        } else if (mFontSize == preference) {
459cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putFloat(
4605ea751e48fc3d7c25cf4e44ef5926e8e70979b5aAlan Viverette                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE,
461cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                    Float.parseFloat((String) value));
462cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        } else if (mLocale == preference) {
463cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            Settings.Secure.putString(
464cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                    cr, Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE, (String) value);
465cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        }
466cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
467cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        refreshPreviewText();
468cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        return true;
469cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
470cc0e782871eb6b946ded880e391866f27953654bAlan Viverette}
471