• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.tv.testinput;
17 
18 import android.content.ContentUris;
19 import android.content.Context;
20 import android.net.Uri;
21 import android.os.RemoteException;
22 import android.util.Log;
23 import android.util.LongSparseArray;
24 import com.android.tv.testing.data.ChannelInfo;
25 import com.android.tv.testing.data.ChannelUtils;
26 import com.android.tv.testing.testinput.ChannelState;
27 import com.android.tv.testing.testinput.ChannelStateData;
28 import com.android.tv.testing.testinput.ITestInputControl;
29 import java.util.Map;
30 
31 /**
32  * Maintains state for the {@link TestTvInputService}.
33  *
34  * <p>Maintains the current state for every channel. A default is sent if the state is not
35  * explicitly set. The state is versioned so TestTvInputService can tell if onNotifyXXX events need
36  * to be sent.
37  *
38  * <p>Test update the state using @{link ITestInputControl} via {@link TestInputControlService}.
39  */
40 class TestInputControl extends ITestInputControl.Stub {
41 
42     private static final String TAG = "TestInputControl";
43     private static final TestInputControl INSTANCE = new TestInputControl();
44 
45     private final LongSparseArray<ChannelInfo> mId2ChannelInfoMap = new LongSparseArray<>();
46     private final LongSparseArray<ChannelState> mOrigId2StateMap = new LongSparseArray<>();
47 
48     private java.lang.String mInputId;
49     private boolean initialized;
50 
TestInputControl()51     private TestInputControl() {}
52 
getInstance()53     public static TestInputControl getInstance() {
54         return INSTANCE;
55     }
56 
init(Context context, String inputId)57     public synchronized void init(Context context, String inputId) {
58         if (!initialized) {
59             // TODO run initialization in a separate thread.
60             mInputId = inputId;
61             updateChannelMap(context);
62             initialized = true;
63         }
64     }
65 
updateChannelMap(Context context)66     private void updateChannelMap(Context context) {
67         mId2ChannelInfoMap.clear();
68         Map<Long, ChannelInfo> channelIdToInfoMap =
69                 ChannelUtils.queryChannelInfoMapForTvInput(context, mInputId);
70         for (Long channelId : channelIdToInfoMap.keySet()) {
71             mId2ChannelInfoMap.put(channelId, channelIdToInfoMap.get(channelId));
72         }
73         Log.i(
74                 TAG,
75                 "Initialized channel map for "
76                         + mInputId
77                         + " with "
78                         + mId2ChannelInfoMap.size()
79                         + " channels");
80     }
81 
getChannelInfo(Uri channelUri)82     public ChannelInfo getChannelInfo(Uri channelUri) {
83         return mId2ChannelInfoMap.get(ContentUris.parseId(channelUri));
84     }
85 
getChannelState(int originalNetworkId)86     public ChannelState getChannelState(int originalNetworkId) {
87         return mOrigId2StateMap.get(originalNetworkId, ChannelState.DEFAULT);
88     }
89 
90     @Override
updateChannelState(int origId, ChannelStateData data)91     public synchronized void updateChannelState(int origId, ChannelStateData data)
92             throws RemoteException {
93         ChannelState state;
94         ChannelState orig = getChannelState(origId);
95         state = orig.next(data);
96         mOrigId2StateMap.put(origId, state);
97 
98         Log.i(TAG, "Setting channel " + origId + " state to " + state);
99     }
100 }
101