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 
66         outgoingConnections.add(connection);
67         lock.release();
68         return connection;
69     }
70 
71     @Override
onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request)72     public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount,
73             ConnectionRequest request) {
74         final MockConnection connection = new MockConnection();
75         connection.setAddress(request.getAddress(), CONNECTION_PRESENTATION);
76         connection.createMockVideoProvider();
77         ((Connection) connection).setVideoState(request.getVideoState());
78 
79         incomingConnections.add(connection);
80         lock.release();
81         return connection;
82     }
83 
84     @Override
onConference(Connection connection1, Connection connection2)85     public void onConference(Connection connection1, Connection connection2) {
86         // Make sure that these connections are already not conferenced.
87         if (connection1.getConference() == null && connection2.getConference() == null) {
88             MockConference conference = new MockConference(
89                     (MockConnection)connection1, (MockConnection)connection2);
90             CtsConnectionService.addConferenceToTelecom(conference);
91             conferences.add(conference);
92             lock.release();
93         }
94     }
95 
96     @Override
onRemoteExistingConnectionAdded(RemoteConnection connection)97     public void onRemoteExistingConnectionAdded(RemoteConnection connection) {
98         // Keep track of the remote connections added to the service
99         remoteConnections.add(connection);
100     }
101 
102     @Override
onRemoteConferenceAdded(RemoteConference conference)103     public void onRemoteConferenceAdded(RemoteConference conference) {
104         // Keep track of the remote connections added to the service
105         remoteConferences.add(conference);
106     }
107 
setCreateVideoProvider(boolean createVideoProvider)108     public void setCreateVideoProvider(boolean createVideoProvider) {
109         mCreateVideoProvider = createVideoProvider;
110     }
111 }
112