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.text.TextUtils;
21 import android.view.KeyEvent;
22 
23 import com.android.tv.R;
24 import com.android.tv.util.Utils;
25 
26 import java.util.ArrayList;
27 import java.util.List;
28 
29 public class MultiAudioFragment extends SideFragment {
30     private static final String TRACKER_LABEL = "multi-audio";
31     private int mInitialSelectedPosition = INVALID_POSITION;
32     private String mSelectedTrackId;
33     private String mFocusedTrackId;
34 
MultiAudioFragment()35     public MultiAudioFragment() {
36         super(KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK, KeyEvent.KEYCODE_A);
37     }
38 
39     @Override
getTitle()40     protected String getTitle() {
41         return getString(R.string.side_panel_title_multi_audio);
42     }
43 
44     @Override
getTrackerLabel()45     public String getTrackerLabel() {
46         return TRACKER_LABEL;
47     }
48 
49     @Override
getItemList()50     protected List<Item> getItemList() {
51         List<TvTrackInfo> tracks = getMainActivity().getTracks(TvTrackInfo.TYPE_AUDIO);
52         mSelectedTrackId = getMainActivity().getSelectedTrack(TvTrackInfo.TYPE_AUDIO);
53 
54         List<Item> items = new ArrayList<>();
55         if (tracks != null) {
56             boolean needToShowSampleRate = Utils.needToShowSampleRate(getActivity(), tracks);
57             int pos = 0;
58             for (final TvTrackInfo track : tracks) {
59                 RadioButtonItem item = new MultiAudioOptionItem(
60                         Utils.getMultiAudioString(getActivity(), track, needToShowSampleRate),
61                         track.getId());
62                 if (track.getId().equals(mSelectedTrackId)) {
63                     item.setChecked(true);
64                     mInitialSelectedPosition = pos;
65                     mSelectedTrackId = mFocusedTrackId = track.getId();
66                 }
67                 items.add(item);
68                 ++pos;
69             }
70         }
71         return items;
72     }
73 
74     @Override
onResume()75     public void onResume() {
76         super.onResume();
77         if (mInitialSelectedPosition != INVALID_POSITION) {
78             setSelectedPosition(mInitialSelectedPosition);
79         }
80     }
81 
82     private class MultiAudioOptionItem extends RadioButtonItem {
83         private final String mTrackId;
84 
MultiAudioOptionItem(String title, String trackId)85         private MultiAudioOptionItem(String title, String trackId) {
86             super(title);
87             mTrackId = trackId;
88         }
89 
90         @Override
onSelected()91         protected void onSelected() {
92             super.onSelected();
93             mSelectedTrackId = mFocusedTrackId = mTrackId;
94             getMainActivity().selectAudioTrack(mTrackId);
95             closeFragment();
96         }
97 
98         @Override
onFocused()99         protected void onFocused() {
100             super.onFocused();
101             mFocusedTrackId = mTrackId;
102             getMainActivity().selectAudioTrack(mTrackId);
103         }
104     }
105 
106     @Override
onDetach()107     public void onDetach() {
108         if (!TextUtils.equals(mSelectedTrackId, mFocusedTrackId)) {
109             getMainActivity().selectAudioTrack(mSelectedTrackId);
110         }
111         super.onDetach();
112     }
113 }
114