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