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     @Implementation
getTracks(int type)50     public List<TvTrackInfo> getTracks(int type) {
51         return mTracks.get(type);
52     }
53 
54     @Implementation
selectTrack(int type, String trackId)55     public void selectTrack(int type, String trackId) {
56         mSelectedTracks.put(type, trackId);
57         int infoIndex = findTrackIndex(type, trackId);
58         // for some drivers, audio track count is set to 0 until the corresponding track is
59         // selected. Here we replace the track with another one whose audio track count is non-zero
60         // to test this case.
61         if (mAudioTrackCountChanged) {
62             replaceTrack(type, infoIndex);
63         }
64         mCallback.onTrackSelected("fakeInputId", type, trackId);
65     }
66 
67     @Implementation
getSelectedTrack(int type)68     public String getSelectedTrack(int type) {
69         return mSelectedTracks.get(type);
70     }
71 
72     @Implementation
setCallback(TvView.TvInputCallback callback)73     public void setCallback(TvView.TvInputCallback callback) {
74         mCallback = callback;
75     }
76 
findTrackIndex(int type, String trackId)77     private int findTrackIndex(int type, String trackId) {
78         List<TvTrackInfo> tracks = mTracks.get(type);
79         if (tracks == null) {
80             return -1;
81         }
82         for (int i = 0; i < tracks.size(); i++) {
83             TvTrackInfo info = tracks.get(i);
84             if (info.getId().equals(trackId)) {
85                 return i;
86             }
87         }
88         return -1;
89     }
90 
replaceTrack(int type, int trackIndex)91     private void replaceTrack(int type, int trackIndex) {
92         if (trackIndex >= 0) {
93             TvTrackInfo info = mTracks.get(type).get(trackIndex);
94             info = new TvTrackInfo
95                     .Builder(info.getType(), info.getId())
96                     .setLanguage(info.getLanguage())
97                     .setAudioChannelCount(info.getAudioChannelCount() + 2)
98                     .build();
99             mTracks.get(type).set(trackIndex, info);
100         }
101     }
102 }
103