1 /*
2  * Copyright (C) 2017 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.testing;
18 
19 import android.media.tv.TvContentRatingSystemInfo;
20 import android.media.tv.TvInputInfo;
21 import android.media.tv.TvInputManager;
22 import android.os.Handler;
23 import com.android.tv.util.TvInputManagerHelper;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 
29 /** Fake implementation for testing. */
30 public class FakeTvInputManager implements TvInputManagerHelper.TvInputManagerInterface {
31 
32     private final Map<String, Integer> mInputStateMap = new HashMap<>();
33     private final Map<String, TvInputInfo> mInputMap = new HashMap<>();
34     private final Map<TvInputManager.TvInputCallback, Handler> mCallbacks = new HashMap<>();
35 
add(TvInputInfo inputInfo, int state)36     public void add(TvInputInfo inputInfo, int state) {
37         final String inputId = inputInfo.getId();
38         if (mInputStateMap.containsKey(inputId)) {
39             throw new IllegalArgumentException("inputId " + inputId);
40         }
41         mInputMap.put(inputId, inputInfo);
42         mInputStateMap.put(inputId, state);
43         for (final Map.Entry<TvInputManager.TvInputCallback, Handler> e : mCallbacks.entrySet()) {
44             e.getValue()
45                     .post(
46                             new Runnable() {
47                                 @Override
48                                 public void run() {
49                                     e.getKey().onInputAdded(inputId);
50                                 }
51                             });
52         }
53     }
54 
setInputState(final String inputId, final int state)55     public void setInputState(final String inputId, final int state) {
56         if (!mInputStateMap.containsKey(inputId)) {
57             throw new IllegalArgumentException("inputId " + inputId);
58         }
59         mInputStateMap.put(inputId, state);
60         for (final Map.Entry<TvInputManager.TvInputCallback, Handler> e : mCallbacks.entrySet()) {
61             e.getValue()
62                     .post(
63                             new Runnable() {
64                                 @Override
65                                 public void run() {
66                                     e.getKey().onInputStateChanged(inputId, state);
67                                 }
68                             });
69         }
70     }
71 
remove(final String inputId)72     public void remove(final String inputId) {
73         mInputMap.remove(inputId);
74         mInputStateMap.remove(inputId);
75         for (final Map.Entry<TvInputManager.TvInputCallback, Handler> e : mCallbacks.entrySet()) {
76             e.getValue()
77                     .post(
78                             new Runnable() {
79                                 @Override
80                                 public void run() {
81                                     e.getKey().onInputRemoved(inputId);
82                                 }
83                             });
84         }
85     }
86 
87     @Override
getTvInputInfo(String inputId)88     public TvInputInfo getTvInputInfo(String inputId) {
89         return mInputMap.get(inputId);
90     }
91 
92     @Override
getInputState(String inputId)93     public Integer getInputState(String inputId) {
94         return mInputStateMap.get(inputId);
95     }
96 
97     @Override
registerCallback(TvInputManager.TvInputCallback internalCallback, Handler handler)98     public void registerCallback(TvInputManager.TvInputCallback internalCallback, Handler handler) {
99         mCallbacks.put(internalCallback, handler);
100     }
101 
102     @Override
unregisterCallback(TvInputManager.TvInputCallback internalCallback)103     public void unregisterCallback(TvInputManager.TvInputCallback internalCallback) {
104         mCallbacks.remove(internalCallback);
105     }
106 
107     @Override
getTvInputList()108     public List<TvInputInfo> getTvInputList() {
109         return new ArrayList(mInputMap.values());
110     }
111 
112     @Override
getTvContentRatingSystemList()113     public List<TvContentRatingSystemInfo> getTvContentRatingSystemList() {
114         // TODO implement
115         return new ArrayList<>();
116     }
117 }
118