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