OSDN Git Service

merge in klp-release history after reset to klp-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / accessibility / ToggleCaptioningPreferenceFragment.java
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.settings.accessibility;
18
19 import android.app.ActionBar;
20 import android.app.Activity;
21 import android.app.Fragment;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.os.Bundle;
25 import android.preference.PreferenceFrameLayout;
26 import android.provider.Settings;
27 import android.view.Gravity;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.view.accessibility.CaptioningManager;
32 import android.view.accessibility.CaptioningManager.CaptionStyle;
33
34 import com.android.settings.R;
35 import com.android.settings.accessibility.ToggleSwitch.OnBeforeCheckedChangeListener;
36
37 import java.util.Locale;
38
39 public class ToggleCaptioningPreferenceFragment extends Fragment {
40     private CaptionPropertiesFragment mPropsFragment;
41     private CaptioningTextView mPreviewText;
42
43     @Override
44     public View onCreateView(
45             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
46         final View rootView = inflater.inflate(R.layout.captioning_preview, container, false);
47
48         // We have to do this now because PreferenceFrameLayout looks at it
49         // only when the view is added.
50         if (container instanceof PreferenceFrameLayout) {
51             ((PreferenceFrameLayout.LayoutParams) rootView.getLayoutParams()).removeBorders = true;
52         }
53
54         return rootView;
55     }
56
57     @Override
58     public void onViewCreated(View view, Bundle savedInstanceState) {
59         super.onViewCreated(view, savedInstanceState);
60
61         mPropsFragment = ((CaptionPropertiesFragment) getFragmentManager()
62                 .findFragmentById(R.id.properties_fragment));
63         mPropsFragment.setParent(this);
64
65         mPreviewText = (CaptioningTextView) view.findViewById(R.id.preview_text);
66
67         installActionBarToggleSwitch();
68         refreshPreviewText();
69     }
70
71     public void refreshPreviewText() {
72         final CaptioningTextView preview = mPreviewText;
73         if (preview != null) {
74             final Activity activity = getActivity();
75             final ContentResolver cr = activity.getContentResolver();
76             final int styleId = CaptionStyle.getRawPreset(cr);
77             applyCaptionProperties(preview, styleId);
78
79             final Locale locale = CaptioningManager.getLocale(cr);
80             if (locale != null) {
81                 final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
82                         activity, locale, R.string.captioning_preview_text);
83                 preview.setText(localizedText);
84             }
85         }
86     }
87
88     public static void applyCaptionProperties(CaptioningTextView previewText, int styleId) {
89         previewText.setStyle(styleId);
90
91         final Context context = previewText.getContext();
92         final ContentResolver cr = context.getContentResolver();
93         final float fontSize = CaptioningManager.getFontSize(cr);
94         previewText.setTextSize(fontSize);
95
96         final Locale locale = CaptioningManager.getLocale(cr);
97         if (locale != null) {
98             final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
99                     context, locale, R.string.captioning_preview_characters);
100             previewText.setText(localizedText);
101         }
102     }
103
104     private void installActionBarToggleSwitch() {
105         final Activity activity = getActivity();
106         final ToggleSwitch toggleSwitch = new ToggleSwitch(activity);
107
108         final int padding = getResources().getDimensionPixelSize(
109                 R.dimen.action_bar_switch_padding);
110         toggleSwitch.setPaddingRelative(0, 0, padding, 0);
111
112         final ActionBar actionBar = activity.getActionBar();
113         actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
114
115         final ActionBar.LayoutParams params = new ActionBar.LayoutParams(
116                 ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT,
117                         Gravity.CENTER_VERTICAL | Gravity.END);
118         actionBar.setCustomView(toggleSwitch, params);
119
120         final boolean enabled = CaptioningManager.isEnabled(getActivity().getContentResolver());
121         mPropsFragment.getPreferenceScreen().setEnabled(enabled);
122         mPreviewText.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
123         toggleSwitch.setCheckedInternal(enabled);
124         toggleSwitch.setOnBeforeCheckedChangeListener(new OnBeforeCheckedChangeListener() {
125             @Override
126             public boolean onBeforeCheckedChanged(ToggleSwitch toggleSwitch, boolean checked) {
127                 toggleSwitch.setCheckedInternal(checked);
128                 Settings.Secure.putInt(getActivity().getContentResolver(),
129                         Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, checked ? 1 : 0);
130                 mPropsFragment.getPreferenceScreen().setEnabled(checked);
131                 mPreviewText.setVisibility(checked ? View.VISIBLE : View.INVISIBLE);
132                 return false;
133             }
134         });
135     }
136 }