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.text.TextUtils; 22 import android.view.KeyEvent; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 import android.view.ViewGroup; 26 import com.android.tv.R; 27 import com.android.tv.util.CaptionSettings; 28 import java.util.ArrayList; 29 import java.util.List; 30 import java.util.Locale; 31 32 public class ClosedCaptionFragment extends SideFragment { 33 private static final String TRACKER_LABEL = "closed caption"; 34 private boolean mResetClosedCaption; 35 private int mClosedCaptionOption; 36 private String mClosedCaptionLanguage; 37 private String mClosedCaptionTrackId; 38 private ClosedCaptionOptionItem mSelectedItem; 39 ClosedCaptionFragment()40 public ClosedCaptionFragment() { 41 super(KeyEvent.KEYCODE_CAPTIONS, KeyEvent.KEYCODE_S); 42 } 43 44 @Override getTitle()45 protected String getTitle() { 46 return getString(R.string.side_panel_title_closed_caption); 47 } 48 49 @Override getTrackerLabel()50 public String getTrackerLabel() { 51 return TRACKER_LABEL; 52 } 53 54 @Override getItemList()55 protected List<Item> getItemList() { 56 CaptionSettings captionSettings = getMainActivity().getCaptionSettings(); 57 mResetClosedCaption = true; 58 mClosedCaptionOption = captionSettings.getEnableOption(); 59 mClosedCaptionLanguage = captionSettings.getLanguage(); 60 mClosedCaptionTrackId = captionSettings.getTrackId(); 61 62 List<Item> items = new ArrayList<>(); 63 mSelectedItem = null; 64 65 List<TvTrackInfo> tracks = getMainActivity().getTracks(TvTrackInfo.TYPE_SUBTITLE); 66 if (tracks != null && !tracks.isEmpty()) { 67 String selectedTrackId = 68 captionSettings.isEnabled() 69 ? getMainActivity().getSelectedTrack(TvTrackInfo.TYPE_SUBTITLE) 70 : null; 71 ClosedCaptionOptionItem item = new ClosedCaptionOptionItem(null, null); 72 items.add(item); 73 if (selectedTrackId == null) { 74 mSelectedItem = item; 75 item.setChecked(true); 76 setSelectedPosition(0); 77 } 78 for (int i = 0; i < tracks.size(); i++) { 79 item = new ClosedCaptionOptionItem(tracks.get(i), i); 80 if (TextUtils.equals(selectedTrackId, tracks.get(i).getId())) { 81 mSelectedItem = item; 82 item.setChecked(true); 83 setSelectedPosition(i + 1); 84 } 85 items.add(item); 86 } 87 } 88 if (getMainActivity().hasCaptioningSettingsActivity()) { 89 items.add( 90 new ActionItem( 91 getString(R.string.closed_caption_system_settings), 92 getString(R.string.closed_caption_system_settings_description)) { 93 @Override 94 protected void onSelected() { 95 getMainActivity().startSystemCaptioningSettingsActivity(); 96 } 97 98 @Override 99 protected void onFocused() { 100 super.onFocused(); 101 if (mSelectedItem != null) { 102 getMainActivity() 103 .selectSubtitleTrack( 104 mSelectedItem.mOption, mSelectedItem.mTrackId); 105 } 106 } 107 }); 108 } 109 return items; 110 } 111 112 @Override onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)113 public View onCreateView( 114 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 115 return super.onCreateView(inflater, container, savedInstanceState); 116 } 117 118 @Override onDestroyView()119 public void onDestroyView() { 120 if (mResetClosedCaption) { 121 getMainActivity() 122 .selectSubtitleLanguage( 123 mClosedCaptionOption, mClosedCaptionLanguage, mClosedCaptionTrackId); 124 } 125 super.onDestroyView(); 126 } 127 getLabel(TvTrackInfo track, Integer trackIndex)128 private String getLabel(TvTrackInfo track, Integer trackIndex) { 129 if (track == null) { 130 return getString(R.string.closed_caption_option_item_off); 131 } else if (track.getLanguage() != null) { 132 return new Locale(track.getLanguage()).getDisplayName(); 133 } 134 return getString(R.string.closed_caption_unknown_language, trackIndex + 1); 135 } 136 137 private class ClosedCaptionOptionItem extends RadioButtonItem { 138 private final int mOption; 139 private final String mTrackId; 140 ClosedCaptionOptionItem(TvTrackInfo track, Integer trackIndex)141 private ClosedCaptionOptionItem(TvTrackInfo track, Integer trackIndex) { 142 super(getLabel(track, trackIndex)); 143 if (track == null) { 144 mOption = CaptionSettings.OPTION_OFF; 145 mTrackId = null; 146 } else { 147 mOption = CaptionSettings.OPTION_ON; 148 mTrackId = track.getId(); 149 } 150 } 151 152 @Override onSelected()153 protected void onSelected() { 154 super.onSelected(); 155 mSelectedItem = this; 156 getMainActivity().selectSubtitleTrack(mOption, mTrackId); 157 mResetClosedCaption = false; 158 closeFragment(); 159 } 160 161 @Override onFocused()162 protected void onFocused() { 163 super.onFocused(); 164 getMainActivity().selectSubtitleTrack(mOption, mTrackId); 165 } 166 } 167 } 168