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.testing.testinput; 17 18 import android.content.ComponentName; 19 import android.content.ServiceConnection; 20 import android.os.IBinder; 21 import android.os.RemoteException; 22 import android.os.SystemClock; 23 import android.util.Log; 24 import com.android.tv.testing.data.ChannelInfo; 25 26 /** 27 * Connection for controlling the Test TV Input Service. 28 * 29 * <p>Wrapped methods for calling {@link ITestInputControl} that waits for a binding and rethrows 30 * {@link RemoteException} as {@link RuntimeException } are also included. 31 */ 32 public class TestInputControlConnection implements ServiceConnection { 33 private static final String TAG = "TestInputControlConn"; 34 private static final int BOUND_CHECK_INTERVAL_MS = 10; 35 36 private ITestInputControl mControl; 37 38 @Override onServiceConnected(ComponentName name, IBinder service)39 public void onServiceConnected(ComponentName name, IBinder service) { 40 mControl = ITestInputControl.Stub.asInterface(service); 41 } 42 43 @Override onServiceDisconnected(ComponentName name)44 public void onServiceDisconnected(ComponentName name) { 45 Log.w(TAG, "TestInputControl service disconnected unexpectedly."); 46 mControl = null; 47 } 48 49 /** Is the service currently connected. */ isBound()50 public boolean isBound() { 51 return mControl != null; 52 } 53 54 /** 55 * Update the state of the channel. 56 * 57 * @param channel the channel to update. 58 * @param data the new state for the channel. 59 */ updateChannelState(ChannelInfo channel, ChannelStateData data)60 public void updateChannelState(ChannelInfo channel, ChannelStateData data) { 61 waitUntilBound(); 62 try { 63 mControl.updateChannelState(channel.originalNetworkId, data); 64 } catch (RemoteException e) { 65 throw new RuntimeException(e); 66 } 67 } 68 69 /** Sleep until {@link #isBound()} is true; */ waitUntilBound()70 public void waitUntilBound() { 71 while (!isBound()) { 72 SystemClock.sleep(BOUND_CHECK_INTERVAL_MS); 73 } 74 } 75 } 76