1 /* 2 * Copyright (C) 2015 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.ui.sidepanel; 18 19 import android.media.tv.TvTrackInfo; 20 import android.os.Bundle; 21 import android.view.KeyEvent; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.ViewGroup; 25 26 import com.android.tv.R; 27 import com.android.tv.util.CaptionSettings; 28 import com.android.tv.util.Utils; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 import java.util.Locale; 33 34 public class ClosedCaptionFragment extends SideFragment { 35 private static final String TRACKER_LABEL ="closed caption" ; 36 private boolean mResetClosedCaption; 37 private int mClosedCaptionOption; 38 private String mClosedCaptionLanguage; 39 private String mClosedCaptionTrackId; 40 private ClosedCaptionOptionItem mSelectedItem; 41 private List<Item> mItems; 42 private boolean mPaused; 43 ClosedCaptionFragment()44 public ClosedCaptionFragment() { 45 super(KeyEvent.KEYCODE_CAPTIONS, KeyEvent.KEYCODE_S); 46 } 47 48 @Override getTitle()49 protected String getTitle() { 50 return getString(R.string.side_panel_title_closed_caption); 51 } 52 53 @Override getTrackerLabel()54 public String getTrackerLabel() { 55 return TRACKER_LABEL; 56 } 57 58 @Override getItemList()59 protected List<Item> getItemList() { 60 CaptionSettings captionSettings = getMainActivity().getCaptionSettings(); 61 mResetClosedCaption = true; 62 mClosedCaptionOption = captionSettings.getEnableOption(); 63 mClosedCaptionLanguage = captionSettings.getLanguage(); 64 mClosedCaptionTrackId = captionSettings.getTrackId(); 65 66 mItems = new ArrayList<>(); 67 mSelectedItem = null; 68 69 List<TvTrackInfo> tracks = getMainActivity().getTracks(TvTrackInfo.TYPE_SUBTITLE); 70 if (tracks != null && !tracks.isEmpty()) { 71 String trackId = captionSettings.isEnabled() ? 72 getMainActivity().getSelectedTrack(TvTrackInfo.TYPE_SUBTITLE) : null; 73 boolean isEnabled = trackId != null; 74 75 ClosedCaptionOptionItem item = new ClosedCaptionOptionItem( 76 getString(R.string.closed_caption_option_item_off), 77 CaptionSettings.OPTION_OFF, null, null); 78 // Pick 'Off' as default because we may fail to find the matching language. 79 mSelectedItem = item; 80 if (!isEnabled) { 81 item.setChecked(true); 82 } 83 mItems.add(item); 84 85 for (final TvTrackInfo track : tracks) { 86 item = new ClosedCaptionOptionItem(getLabel(track), 87 CaptionSettings.OPTION_ON, track.getId(), track.getLanguage()); 88 if (isEnabled && track.getId().equals(trackId)) { 89 item.setChecked(true); 90 mSelectedItem = item; 91 } 92 mItems.add(item); 93 } 94 } 95 if (getMainActivity().hasCaptioningSettingsActivity()) { 96 mItems.add(new ActionItem(getString(R.string.closed_caption_system_settings), 97 getString(R.string.closed_caption_system_settings_description)) { 98 @Override 99 protected void onSelected() { 100 getMainActivity().startSystemCaptioningSettingsActivity(); 101 } 102 103 @Override 104 protected void onFocused() { 105 super.onFocused(); 106 if (!mPaused && mSelectedItem != null) { 107 getMainActivity().selectSubtitleTrack( 108 mSelectedItem.mOption, mSelectedItem.mTrackId); 109 } 110 } 111 }); 112 } 113 return mItems; 114 } 115 116 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)117 public View onCreateView(LayoutInflater inflater, ViewGroup container, 118 Bundle savedInstanceState) { 119 return super.onCreateView(inflater, container, savedInstanceState); 120 } 121 122 @Override onResume()123 public void onResume() { 124 super.onResume(); 125 if (mPaused) { 126 // Apply system's closed caption settings to the UI. 127 CaptionSettings captionSettings = getMainActivity().getCaptionSettings(); 128 mClosedCaptionOption = CaptionSettings.OPTION_SYSTEM; 129 mClosedCaptionLanguage = captionSettings.getSystemLanguage(); 130 ClosedCaptionOptionItem selectedItem = null; 131 if (captionSettings.isSystemSettingEnabled()) { 132 for (Item item : mItems) { 133 if (!(item instanceof ClosedCaptionOptionItem)) { 134 continue; 135 } 136 ClosedCaptionOptionItem captionItem = (ClosedCaptionOptionItem) item; 137 if (Utils.isEqualLanguage(captionItem.mLanguage, mClosedCaptionLanguage)) { 138 selectedItem = captionItem; 139 break; 140 } 141 } 142 } 143 if (mSelectedItem != null) { 144 mSelectedItem.setChecked(false); 145 } 146 if (selectedItem == null && mItems.get(0) instanceof ClosedCaptionOptionItem) { 147 selectedItem = (ClosedCaptionOptionItem) mItems.get(0); 148 } 149 if (selectedItem != null) { 150 selectedItem.setChecked(true); 151 } 152 // We shouldn't call MainActivity.selectSubtitleTrack() here because 153 // 1. Tracks are not available because video is just started at this moment. 154 // 2. MainActivity will apply system settings when video's tracks are available. 155 mSelectedItem = selectedItem; 156 } 157 mPaused = false; 158 } 159 160 @Override onPause()161 public void onPause() { 162 super.onPause(); 163 mPaused = true; 164 } 165 166 @Override onDestroyView()167 public void onDestroyView() { 168 if (mResetClosedCaption) { 169 getMainActivity().selectSubtitleLanguage(mClosedCaptionOption, mClosedCaptionLanguage, 170 mClosedCaptionTrackId); 171 } 172 super.onDestroyView(); 173 } 174 getLabel(TvTrackInfo track)175 private String getLabel(TvTrackInfo track) { 176 if (track.getLanguage() != null) { 177 return new Locale(track.getLanguage()).getDisplayName(); 178 } 179 return getString(R.string.default_language); 180 } 181 182 private class ClosedCaptionOptionItem extends RadioButtonItem { 183 private final int mOption; 184 private final String mTrackId; 185 private final String mLanguage; 186 ClosedCaptionOptionItem(String title, int option, String trackId, String language)187 private ClosedCaptionOptionItem(String title, int option, String trackId, String language) { 188 super(title); 189 mOption = option; 190 mTrackId = trackId; 191 mLanguage = language; 192 } 193 194 @Override onSelected()195 protected void onSelected() { 196 super.onSelected(); 197 mSelectedItem = this; 198 getMainActivity().selectSubtitleTrack(mOption, mTrackId); 199 mResetClosedCaption = false; 200 closeFragment(); 201 } 202 203 @Override onFocused()204 protected void onFocused() { 205 super.onFocused(); 206 getMainActivity().selectSubtitleTrack(mOption, mTrackId); 207 } 208 } 209 } 210