1 /*
2  * Copyright (C) 2018 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;
18 
19 import android.content.Context;
20 import android.media.tv.TvTrackInfo;
21 import android.media.tv.TvView;
22 import android.util.AttributeSet;
23 
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import org.robolectric.annotation.Implementation;
28 import org.robolectric.annotation.Implements;
29 import org.robolectric.shadows.ShadowView;
30 
31 // TODO(b/78304522): move this class to robolectric
32 /** Shadow class of {@link TvView}. */
33 @Implements(TvView.class)
34 public class ShadowTvView extends ShadowView {
35     public Map<Integer, String> mSelectedTracks = new HashMap<>();
36     public Map<Integer, List<TvTrackInfo>> mTracks = new HashMap<>();
37     public MainActivity.MyOnTuneListener listener;
38     public TvView.TvInputCallback mCallback;
39     public boolean mAudioTrackCountChanged;
40 
41     @Implementation
__constructor__(Context context)42     public void __constructor__(Context context) {
43     }
44 
45     @Implementation
__constructor__(Context context, AttributeSet attrs)46     public void __constructor__(Context context, AttributeSet attrs) {
47     }
48 
49     @Override
__constructor__(Context context, AttributeSet attrs, int defStyleAttr)50     public void __constructor__(Context context, AttributeSet attrs, int defStyleAttr) {
51     }
52 
53     @Implementation
getTracks(int type)54     public List<TvTrackInfo> getTracks(int type) {
55         return mTracks.get(type);
56     }
57 
58     @Implementation
selectTrack(int type, String trackId)59     public void selectTrack(int type, String trackId) {
60         mSelectedTracks.put(type, trackId);
61         int infoIndex = findTrackIndex(type, trackId);
62         // for some drivers, audio track count is set to 0 until the corresponding track is
63         // selected. Here we replace the track with another one whose audio track count is non-zero
64         // to test this case.
65         if (mAudioTrackCountChanged) {
66             replaceTrack(type, infoIndex);
67         }
68         mCallback.onTrackSelected("fakeInputId", type, trackId);
69     }
70 
71     @Implementation
getSelectedTrack(int type)72     public String getSelectedTrack(int type) {
73         return mSelectedTracks.get(type);
74     }
75 
76     @Implementation
setCallback(TvView.TvInputCallback callback)77     public void setCallback(TvView.TvInputCallback callback) {
78         mCallback = callback;
79     }
80 
findTrackIndex(int type, String trackId)81     private int findTrackIndex(int type, String trackId) {
82         List<TvTrackInfo> tracks = mTracks.get(type);
83         if (tracks == null) {
84             return -1;
85         }
86         for (int i = 0; i < tracks.size(); i++) {
87             TvTrackInfo info = tracks.get(i);
88             if (info.getId().equals(trackId)) {
89                 return i;
90             }
91         }
92         return -1;
93     }
94 
replaceTrack(int type, int trackIndex)95     private void replaceTrack(int type, int trackIndex) {
96         if (trackIndex >= 0) {
97             TvTrackInfo info = mTracks.get(type).get(trackIndex);
98             info = new TvTrackInfo
99                     .Builder(info.getType(), info.getId())
100                     .setLanguage(info.getLanguage())
101                     .setAudioChannelCount(info.getAudioChannelCount() + 2)
102                     .build();
103             mTracks.get(type).set(trackIndex, info);
104         }
105     }
106 }
107