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