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.CompletableFuture;
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         CompletableFuture<ICtsApi29InCallServiceControl> resultFuture = new CompletableFuture<>();
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                 resultFuture.complete(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                 resultFuture.get(TestUtils.WAIT_FOR_STATE_CHANGE_TIMEOUT_MS,
53                         TimeUnit.MILLISECONDS));
54     }
55 
tearDownControl(Context context, ServiceConnection serviceConnection)56     public static void tearDownControl(Context context, ServiceConnection serviceConnection) {
57         context.unbindService(serviceConnection);
58     }
59 
Api29InCallUtils()60     private Api29InCallUtils() {}
61 }
62