2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.accessibility;
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;
34 import com.android.settings.R;
35 import com.android.settings.accessibility.ToggleSwitch.OnBeforeCheckedChangeListener;
37 import java.util.Locale;
39 public class ToggleCaptioningPreferenceFragment extends Fragment {
40 private CaptionPropertiesFragment mPropsFragment;
41 private CaptioningTextView mPreviewText;
44 public View onCreateView(
45 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
46 final View rootView = inflater.inflate(R.layout.captioning_preview, container, false);
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;
58 public void onViewCreated(View view, Bundle savedInstanceState) {
59 super.onViewCreated(view, savedInstanceState);
61 mPropsFragment = ((CaptionPropertiesFragment) getFragmentManager()
62 .findFragmentById(R.id.properties_fragment));
63 mPropsFragment.setParent(this);
65 mPreviewText = (CaptioningTextView) view.findViewById(R.id.preview_text);
67 installActionBarToggleSwitch();
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);
79 final Locale locale = CaptioningManager.getLocale(cr);
81 final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
82 activity, locale, R.string.captioning_preview_text);
83 preview.setText(localizedText);
88 public static void applyCaptionProperties(CaptioningTextView previewText, int styleId) {
89 previewText.setStyle(styleId);
91 final Context context = previewText.getContext();
92 final ContentResolver cr = context.getContentResolver();
93 final float fontSize = CaptioningManager.getFontSize(cr);
94 previewText.setTextSize(fontSize);
96 final Locale locale = CaptioningManager.getLocale(cr);
98 final CharSequence localizedText = AccessibilityUtils.getTextForLocale(
99 context, locale, R.string.captioning_preview_characters);
100 previewText.setText(localizedText);
104 private void installActionBarToggleSwitch() {
105 final Activity activity = getActivity();
106 final ToggleSwitch toggleSwitch = new ToggleSwitch(activity);
108 final int padding = getResources().getDimensionPixelSize(
109 R.dimen.action_bar_switch_padding);
110 toggleSwitch.setPaddingRelative(0, 0, padding, 0);
112 final ActionBar actionBar = activity.getActionBar();
113 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
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);
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() {
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);