1 package com.android.cts.verifier.telecom;
2 
3 import android.os.Bundle;
4 import android.telecom.Connection;
5 import android.telecom.ConnectionRequest;
6 import android.telecom.ConnectionService;
7 import android.telecom.PhoneAccountHandle;
8 import android.telecom.TelecomManager;
9 
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.concurrent.CountDownLatch;
13 import java.util.concurrent.TimeUnit;
14 
15 public class CtsSelfManagedConnectionService extends ConnectionService {
16     static final int TIMEOUT_MILLIS = 10000;
17 
18     private CtsConnection.Listener mConnectionListener =
19             new CtsConnection.Listener() {
20                 @Override
21                 void onDestroyed(CtsConnection connection) {
22                     synchronized (mConnectionsLock) {
23                         mConnections.remove(connection);
24                     }
25                 }
26             };
27 
28     private static CtsSelfManagedConnectionService sConnectionService;
29     private static CountDownLatch sBindingLatch = new CountDownLatch(1);
30 
31     List<CtsConnection> mConnections = new ArrayList<>();
32     Object mConnectionsLock = new Object();
33     CountDownLatch mConnectionLatch = new CountDownLatch(1);
34 
getConnectionService()35     public static CtsSelfManagedConnectionService getConnectionService() {
36         return sConnectionService;
37     }
38 
waitForAndGetConnectionService()39     public static CtsSelfManagedConnectionService waitForAndGetConnectionService() {
40         if (sConnectionService == null) {
41             try {
42                 sBindingLatch.await(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
43             } catch (InterruptedException e) {
44             }
45         }
46         return sConnectionService;
47     }
48 
CtsSelfManagedConnectionService()49     public CtsSelfManagedConnectionService() throws Exception {
50         super();
51         sConnectionService = this;
52         if (sBindingLatch != null) {
53             sBindingLatch.countDown();
54         }
55         sBindingLatch = new CountDownLatch(1);
56     }
57 
getConnections()58     public List<CtsConnection> getConnections() {
59         synchronized (mConnectionsLock) {
60             return new ArrayList<CtsConnection>(mConnections);
61         }
62     }
63 
waitForAndGetConnection()64     public CtsConnection waitForAndGetConnection() {
65         try {
66             mConnectionLatch.await(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
67         } catch (InterruptedException e) {
68         }
69         mConnectionLatch = new CountDownLatch(1);
70         synchronized (mConnectionsLock) {
71             if (mConnections.size() > 0) {
72                 return mConnections.get(0);
73             } else {
74                 return null;
75             }
76         }
77     }
78 
79     @Override
onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)80     public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount,
81             ConnectionRequest request) {
82         return createConnection(request, true /* isIncoming */);
83     }
84 
85     @Override
onCreateOutgoingConnection(PhoneAccountHandle connectionManagerAccount, ConnectionRequest request)86     public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerAccount,
87             ConnectionRequest request) {
88         return createConnection(request, false /* isIncoming */);
89     }
90 
createConnection(ConnectionRequest request, boolean isIncoming)91     private Connection createConnection(ConnectionRequest request, boolean isIncoming) {
92         boolean useAudioClip =
93                 request.getExtras().getBoolean(CtsConnection.EXTRA_PLAY_CS_AUDIO, false);
94         CtsConnection connection = new CtsConnection(getApplicationContext(), isIncoming,
95                 mConnectionListener, useAudioClip);
96         connection.setConnectionCapabilities(Connection.CAPABILITY_SUPPORT_HOLD |
97                 Connection.CAPABILITY_HOLD);
98         connection.setAddress(request.getAddress(), TelecomManager.PRESENTATION_ALLOWED);
99         connection.setExtras(request.getExtras());
100 
101         Bundle moreExtras = new Bundle();
102         moreExtras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,
103                 request.getAccountHandle());
104         connection.putExtras(moreExtras);
105         connection.setVideoState(request.getVideoState());
106 
107         synchronized (mConnectionsLock) {
108             mConnections.add(connection);
109         }
110         if (mConnectionLatch != null) {
111             mConnectionLatch.countDown();
112         }
113         return connection;
114     }
115 }
116