1 /*
2  * Copyright (C) 2016 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.tv.settings.system;
18 
19 import android.app.Activity;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.os.Bundle;
25 import android.support.annotation.NonNull;
26 import android.support.v4.content.LocalBroadcastManager;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.view.accessibility.CaptioningManager;
31 
32 import com.android.internal.widget.SubtitleView;
33 import com.android.settingslib.accessibility.AccessibilityUtils;
34 import com.android.tv.settings.BaseSettingsFragment;
35 import com.android.tv.settings.R;
36 
37 import java.util.Locale;
38 
39 public class CaptionSettingsFragment extends BaseSettingsFragment {
40 
41     public static final String ACTION_REFRESH_CAPTIONS_PREVIEW = "CaptionSettingsFragment.refresh";
42 
43     private int mDefaultFontSize;
44 
45     private SubtitleView mPreviewText;
46     private View mPreviewWindow;
47     private CaptioningManager mCaptioningManager;
48     private final CaptioningManager.CaptioningChangeListener mCaptionChangeListener =
49             new CaptioningManager.CaptioningChangeListener() {
50 
51                 @Override
52                 public void onEnabledChanged(boolean enabled) {
53                     refreshPreviewText();
54                 }
55 
56                 @Override
57                 public void onUserStyleChanged(@NonNull CaptioningManager.CaptionStyle userStyle) {
58                     loadCaptionSettings();
59                     refreshPreviewText();
60                 }
61 
62                 @Override
63                 public void onLocaleChanged(Locale locale) {
64                     loadCaptionSettings();
65                     refreshPreviewText();
66                 }
67 
68                 @Override
69                 public void onFontScaleChanged(float fontScale) {
70                     loadCaptionSettings();
71                     refreshPreviewText();
72                 }
73             };
74     private final BroadcastReceiver mRefreshReceiver = new BroadcastReceiver() {
75         @Override
76         public void onReceive(Context context, Intent intent) {
77             refreshPreviewText();
78         }
79     };
80 
81     private float mFontScale;
82     private int mStyleId;
83     private Locale mLocale;
84 
newInstance()85     public static CaptionSettingsFragment newInstance() {
86         return new CaptionSettingsFragment();
87     }
88 
89     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)90     public View onCreateView(LayoutInflater inflater, ViewGroup container,
91             Bundle savedInstanceState) {
92         final ViewGroup v = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState);
93         if (v == null) {
94             throw new IllegalStateException("Unexpectedly null view from super");
95         }
96         inflater.inflate(R.layout.captioning_preview, v, true);
97         return v;
98     }
99 
100     @Override
onCreate(Bundle savedInstanceState)101     public void onCreate(Bundle savedInstanceState) {
102         super.onCreate(savedInstanceState);
103 
104         mCaptioningManager =
105                 (CaptioningManager) getActivity().getSystemService(Context.CAPTIONING_SERVICE);
106 
107         mDefaultFontSize =
108                 getResources().getInteger(R.integer.captioning_preview_default_font_size);
109 
110         loadCaptionSettings();
111     }
112 
113     @Override
onViewCreated(View view, Bundle savedInstanceState)114     public void onViewCreated(View view, Bundle savedInstanceState) {
115         super.onViewCreated(view, savedInstanceState);
116         mPreviewText = (SubtitleView) view.findViewById(R.id.preview_text);
117         mPreviewWindow = view.findViewById(R.id.preview_window);
118     }
119 
120     @Override
onPreferenceStartInitialScreen()121     public void onPreferenceStartInitialScreen() {
122         startPreferenceFragment(CaptionFragment.newInstance());
123     }
124 
125     @Override
onStart()126     public void onStart() {
127         super.onStart();
128         mCaptioningManager.addCaptioningChangeListener (mCaptionChangeListener);
129         LocalBroadcastManager.getInstance(getContext()).registerReceiver(mRefreshReceiver,
130                 new IntentFilter(ACTION_REFRESH_CAPTIONS_PREVIEW));
131         refreshPreviewText();
132     }
133 
134     @Override
onStop()135     public void onStop() {
136         super.onStop();
137         mCaptioningManager.removeCaptioningChangeListener (mCaptionChangeListener);
138         LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mRefreshReceiver);
139     }
140 
loadCaptionSettings()141     private void loadCaptionSettings() {
142         mFontScale = mCaptioningManager.getFontScale();
143         mStyleId = mCaptioningManager.getRawUserStyle();
144         mLocale = mCaptioningManager.getLocale();
145     }
146 
refreshPreviewText()147     private void refreshPreviewText() {
148         if (mPreviewText != null) {
149             boolean enabled = mCaptioningManager.isEnabled();
150             if (enabled) {
151                 mPreviewText.setVisibility(View.VISIBLE);
152                 Activity activity = getActivity();
153                 mPreviewText.setStyle(mStyleId);
154                 mPreviewText.setTextSize(mFontScale * mDefaultFontSize);
155                 if (mLocale != null) {
156                     CharSequence localizedText = AccessibilityUtils.getTextForLocale(
157                             activity, mLocale, R.string.captioning_preview_text);
158                     mPreviewText.setText(localizedText);
159                 } else {
160                     mPreviewText.setText(getResources()
161                             .getString(R.string.captioning_preview_text));
162                 }
163 
164                 final CaptioningManager.CaptionStyle style = mCaptioningManager.getUserStyle();
165                 if (style.hasWindowColor()) {
166                     mPreviewWindow.setBackgroundColor(style.windowColor);
167                 } else {
168                     final CaptioningManager.CaptionStyle defStyle =
169                             CaptioningManager.CaptionStyle.DEFAULT;
170                     mPreviewWindow.setBackgroundColor(defStyle.windowColor);
171                 }
172 
173                 mPreviewText.invalidate();
174             } else {
175                 mPreviewText.setVisibility(View.INVISIBLE);
176             }
177         }
178     }
179 }
180