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 
17 package android.telecom.cts;
18 
19 import android.telecom.Connection;
20 import android.telecom.ConnectionRequest;
21 import android.telecom.ConnectionService;
22 import android.telecom.PhoneAccountHandle;
23 import android.telecom.RemoteConference;
24 import android.telecom.RemoteConnection;
25 import android.telecom.TelecomManager;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.concurrent.Semaphore;
30 
31 /**
32  * Default implementation of a {@link CtsConnectionService}. This is used for the majority
33  * of Telecom CTS tests that simply require that a outgoing call is placed, or incoming call is
34  * received.
35  */
36 public class MockConnectionService extends ConnectionService {
37     public static final int CONNECTION_PRESENTATION =  TelecomManager.PRESENTATION_ALLOWED;
38 
39     /**
40      * Used to control whether the {@link MockVideoProvider} will be created when connections are
41      * created.  Used by {@link VideoCallTest#testVideoCallDelayProvider()} to test scenario where
42      * the {@link MockVideoProvider} is not created immediately when the Connection is created.
43      */
44     private boolean mCreateVideoProvider = true;
45 
46     public Semaphore lock = new Semaphore(0);
47     public List<MockConnection> outgoingConnections = new ArrayList<MockConnection>();
48     public List<MockConnection> incomingConnections = new ArrayList<MockConnection>();
49     public List<RemoteConnection> remoteConnections = new ArrayList<RemoteConnection>();
50     public List<MockConference> conferences = new ArrayList<MockConference>();
51     public List<RemoteConference> remoteConferences = new ArrayList<RemoteConference>();
52 
53     @Override
onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)54     public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount,
55             ConnectionRequest request) {
56         final MockConnection connection = new MockConnection();
57         connection.setAddress(request.getAddress(), CONNECTION_PRESENTATION);
58         connection.setPhoneAccountHandle(connectionManagerPhoneAccount);
59         if (mCreateVideoProvider) {
60             connection.createMockVideoProvider();
61         } else {
62             mCreateVideoProvider = true;
63         }
64         connection.setVideoState(request.getVideoState());
65         connection.setInitializing();
66 
67         outgoingConnections.add(connection);
68         lock.release();
69         return connection;
70     }
71 
72     @Override
onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)73     public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount,
74             ConnectionRequest request) {
75         final MockConnection connection = new MockConnection();
76         connection.setAddress(request.getAddress(), CONNECTION_PRESENTATION);
77         connection.setConnectionCapabilities(
78                 connection.getConnectionCapabilities() |
79                         Connection.CAPABILITY_CAN_SEND_RESPONSE_VIA_CONNECTION);
80         connection.createMockVideoProvider();
81         ((Connection) connection).setVideoState(request.getVideoState());
82         connection.setRinging();
83 
84         incomingConnections.add(connection);
85         lock.release();
86         return connection;
87     }
88 
89     @Override
onConference(Connection connection1, Connection connection2)90     public void onConference(Connection connection1, Connection connection2) {
91         // Make sure that these connections are already not conferenced.
92         if (connection1.getConference() == null && connection2.getConference() == null) {
93             MockConference conference = new MockConference(
94                     (MockConnection)connection1, (MockConnection)connection2);
95             CtsConnectionService.addConferenceToTelecom(conference);
96             conferences.add(conference);
97 
98             if (connection1.getState() == Connection.STATE_HOLDING){
99                 connection1.setActive();
100             }
101             if(connection2.getState() == Connection.STATE_HOLDING){
102                 connection2.setActive();
103             }
104 
105             lock.release();
106         }
107     }
108 
109     @Override
onRemoteExistingConnectionAdded(RemoteConnection connection)110     public void onRemoteExistingConnectionAdded(RemoteConnection connection) {
111         // Keep track of the remote connections added to the service
112         remoteConnections.add(connection);
113     }
114 
115     @Override
onRemoteConferenceAdded(RemoteConference conference)116     public void onRemoteConferenceAdded(RemoteConference conference) {
117         // Keep track of the remote connections added to the service
118         remoteConferences.add(conference);
119     }
120 
setCreateVideoProvider(boolean createVideoProvider)121     public void setCreateVideoProvider(boolean createVideoProvider) {
122         mCreateVideoProvider = createVideoProvider;
123     }
124 }
125