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