ToggleCaptioningPreferenceFragment.java revision cc0e782871eb6b946ded880e391866f27953654b
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.app.ActionBar;
20cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.app.Activity;
21cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.app.Fragment;
22cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.content.ContentResolver;
23cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.content.Context;
24cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.os.Bundle;
25cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.preference.PreferenceFrameLayout;
26cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.provider.Settings;
27cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.view.Gravity;
28cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.view.LayoutInflater;
29cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.view.View;
30cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.view.ViewGroup;
31cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.view.accessibility.CaptioningManager;
32cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.view.accessibility.CaptioningManager.CaptionStyle;
33cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
34cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport com.android.settings.R;
35cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport com.android.settings.accessibility.ToggleSwitch.OnBeforeCheckedChangeListener;
36cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
37cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport java.util.Locale;
38cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
39cc0e782871eb6b946ded880e391866f27953654bAlan Viverettepublic class ToggleCaptioningPreferenceFragment extends Fragment {
40cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private CaptionPropertiesFragment mPropsFragment;
41cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private CaptioningTextView mPreviewText;
42cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
43cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    @Override
44cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    public View onCreateView(
45cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
46cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final View rootView = inflater.inflate(R.layout.captioning_preview, container, false);
47cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
48cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        // We have to do this now because PreferenceFrameLayout looks at it
49cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        // only when the view is added.
50cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        if (container instanceof PreferenceFrameLayout) {
51cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            ((PreferenceFrameLayout.LayoutParams) rootView.getLayoutParams()).removeBorders = true;
52cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        }
53cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
54cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        return rootView;
55cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
56cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
57cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    @Override
58cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    public void onViewCreated(View view, Bundle savedInstanceState) {
59cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        super.onViewCreated(view, savedInstanceState);
60cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
61cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mPropsFragment = ((CaptionPropertiesFragment) getFragmentManager()
62cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                .findFragmentById(R.id.properties_fragment));
63cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mPropsFragment.setParent(this);
64cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
65cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mPreviewText = (CaptioningTextView) view.findViewById(R.id.preview_text);
66cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
67cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        installActionBarToggleSwitch();
68cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        refreshPreviewText();
69cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
70cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
71cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    public void refreshPreviewText() {
72cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final CaptioningTextView preview = mPreviewText;
73cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        if (preview != null) {
74cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            final Activity activity = getActivity();
75cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            final ContentResolver cr = activity.getContentResolver();
76cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            final int styleId = CaptionStyle.getRawPreset(cr);
77cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            applyCaptionProperties(preview, styleId);
78cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
79cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            final Locale locale = CaptioningManager.getLocale(cr);
80cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            if (locale != null) {
81cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
82cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                        activity, locale, R.string.captioning_preview_text);
83cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                preview.setText(localizedText);
84cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            }
85cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        }
86cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
87cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
88cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    public static void applyCaptionProperties(CaptioningTextView previewText, int styleId) {
89cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        previewText.applyStyleAndFontSize(styleId);
90cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
91cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final Context context = previewText.getContext();
92cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final ContentResolver cr = context.getContentResolver();
93cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final Locale locale = CaptioningManager.getLocale(cr);
94cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        if (locale != null) {
95cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
96cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                    context, locale, R.string.captioning_preview_characters);
97cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            previewText.setText(localizedText);
98cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        }
99cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
100cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
101cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    private void installActionBarToggleSwitch() {
102cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final Activity activity = getActivity();
103cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final ToggleSwitch toggleSwitch = new ToggleSwitch(activity);
104cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
105cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final int padding = getResources().getDimensionPixelSize(
106cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                R.dimen.action_bar_switch_padding);
107cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        toggleSwitch.setPaddingRelative(0, 0, padding, 0);
108cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
109cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final ActionBar actionBar = activity.getActionBar();
110cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
111cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
112cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final ActionBar.LayoutParams params = new ActionBar.LayoutParams(
113cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT,
114cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                        Gravity.CENTER_VERTICAL | Gravity.END);
115cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        actionBar.setCustomView(toggleSwitch, params);
116cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
117cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final boolean enabled = CaptioningManager.isEnabled(getActivity().getContentResolver());
118cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mPropsFragment.getPreferenceScreen().setEnabled(enabled);
119cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        mPreviewText.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
120cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        toggleSwitch.setCheckedInternal(enabled);
121cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        toggleSwitch.setOnBeforeCheckedChangeListener(new OnBeforeCheckedChangeListener() {
122cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            @Override
123cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            public boolean onBeforeCheckedChanged(ToggleSwitch toggleSwitch, boolean checked) {
124cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                toggleSwitch.setCheckedInternal(checked);
125cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                Settings.Secure.putInt(getActivity().getContentResolver(),
126cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                        Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, checked ? 1 : 0);
127cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                mPropsFragment.getPreferenceScreen().setEnabled(checked);
128cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                mPreviewText.setVisibility(checked ? View.VISIBLE : View.INVISIBLE);
129cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                return false;
130cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            }
131cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        });
132cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
133cc0e782871eb6b946ded880e391866f27953654bAlan Viverette}
134