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