1 package android.telecom.cts;
2 
3 import android.content.ComponentName;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.ServiceConnection;
7 import android.os.IBinder;
8 import android.telecom.cts.api29incallservice.CtsApi29InCallServiceControl;
9 import android.telecom.cts.api29incallservice.ICtsApi29InCallServiceControl;
10 import android.util.Log;
11 import android.util.Pair;
12 
13 import junit.framework.TestCase;
14 
15 import java.util.concurrent.LinkedBlockingQueue;
16 import java.util.concurrent.TimeUnit;
17 
18 public final class Api29InCallUtils {
19     private static final String LOG_TAG = Api29InCallUtils.class.getSimpleName();
20 
setupControl( Context context)21     public static Pair<ServiceConnection, ICtsApi29InCallServiceControl> setupControl(
22             Context context) throws Exception {
23 
24         Intent bindIntent = new Intent(CtsApi29InCallServiceControl.CONTROL_INTERFACE_ACTION);
25         ComponentName controlComponentName =
26                 ComponentName.createRelative(
27                         CtsApi29InCallServiceControl.class.getPackage().getName(),
28                         CtsApi29InCallServiceControl.class.getName());
29 
30         bindIntent.setComponent(controlComponentName);
31         LinkedBlockingQueue<ICtsApi29InCallServiceControl> result = new LinkedBlockingQueue<>(1);
32 
33         ServiceConnection serviceConnection = new ServiceConnection() {
34             @Override
35             public void onServiceConnected(ComponentName name, IBinder service) {
36                 Log.i(LOG_TAG, "Service Connected: " + name);
37                 result.offer(ICtsApi29InCallServiceControl.Stub.asInterface(service));
38             }
39 
40             @Override
41             public void onServiceDisconnected(ComponentName name) {
42             }
43         };
44 
45         boolean success = context.bindService(bindIntent,
46                 serviceConnection, Context.BIND_AUTO_CREATE);
47 
48         if (!success) {
49             TestCase.fail("Failed to get control interface -- bind error");
50         }
51         return Pair.create(serviceConnection,
52                 result.poll(TestUtils.WAIT_FOR_STATE_CHANGE_TIMEOUT_MS, TimeUnit.MILLISECONDS));
53     }
54 
tearDownControl(Context context, ServiceConnection serviceConnection)55     public static void tearDownControl(Context context, ServiceConnection serviceConnection) {
56         context.unbindService(serviceConnection);
57     }
58 
Api29InCallUtils()59     private Api29InCallUtils() {}
60 }
61