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