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.os.Bundle; 20 import android.telecom.Connection; 21 import android.telecom.ConnectionRequest; 22 import android.telecom.ConnectionService; 23 import android.telecom.PhoneAccountHandle; 24 import android.telecom.RemoteConference; 25 import android.telecom.RemoteConnection; 26 import android.telecom.TelecomManager; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 import java.util.concurrent.Semaphore; 31 import java.util.concurrent.TimeUnit; 32 33 /** 34 * Default implementation of a {@link CtsConnectionService}. This is used for the majority 35 * of Telecom CTS tests that simply require that a outgoing call is placed, or incoming call is 36 * received. 37 */ 38 public class MockConnectionService extends ConnectionService { 39 public static final String EXTRA_TEST = "com.android.telecom.extra.TEST"; 40 public static final String TEST_VALUE = "we've got it"; 41 public static final int CONNECTION_PRESENTATION = TelecomManager.PRESENTATION_ALLOWED; 42 43 public static final int EVENT_CONNECTION_SERVICE_FOCUS_GAINED = 0; 44 public static final int EVENT_CONNECTION_SERVICE_FOCUS_LOST = 1; 45 46 // Next event id is 2 47 private static final int TOTAL_EVENT = EVENT_CONNECTION_SERVICE_FOCUS_LOST + 1; 48 49 private static final int DEFAULT_EVENT_TIMEOUT_MS = 2000; 50 51 private final Semaphore[] mEventLock = initializeSemaphore(TOTAL_EVENT); 52 53 /** 54 * Used to control whether the {@link MockVideoProvider} will be created when connections are 55 * created. Used by {@link VideoCallTest#testVideoCallDelayProvider()} to test scenario where 56 * the {@link MockVideoProvider} is not created immediately when the Connection is created. 57 */ 58 private boolean mCreateVideoProvider = true; 59 60 public Semaphore lock = new Semaphore(0); 61 public List<MockConnection> outgoingConnections = new ArrayList<MockConnection>(); 62 public List<MockConnection> incomingConnections = new ArrayList<MockConnection>(); 63 public List<RemoteConnection> remoteConnections = new ArrayList<RemoteConnection>(); 64 public List<MockConference> conferences = new ArrayList<MockConference>(); 65 public List<RemoteConference> remoteConferences = new ArrayList<RemoteConference>(); 66 67 @Override 68 public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, 69 ConnectionRequest request) { 70 final MockConnection connection = new MockConnection(); 71 connection.setAddress(request.getAddress(), CONNECTION_PRESENTATION); 72 connection.setPhoneAccountHandle(connectionManagerPhoneAccount); 73 connection.setConnectionCapabilities(Connection.CAPABILITY_SUPPORT_HOLD | 74 Connection.CAPABILITY_HOLD); 75 if (mCreateVideoProvider) { 76 connection.createMockVideoProvider(); 77 } else { 78 mCreateVideoProvider = true; 79 } 80 connection.setVideoState(request.getVideoState()); 81 connection.setInitializing(); 82 if (request.isRequestingRtt()) { 83 connection.setRttTextStream(request.getRttTextStream()); 84 connection.setConnectionProperties(connection.getConnectionProperties() | 85 Connection.PROPERTY_IS_RTT); 86 } 87 // Emit an extra into the connection. We'll see if it makes it through. 88 Bundle testExtra = new Bundle(); 89 testExtra.putString(EXTRA_TEST, TEST_VALUE); 90 connection.putExtras(testExtra); 91 outgoingConnections.add(connection); 92 lock.release(); 93 return connection; 94 } 95 96 @Override 97 public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount, 98 ConnectionRequest request) { 99 final MockConnection connection = new MockConnection(); 100 connection.setAddress(request.getAddress(), CONNECTION_PRESENTATION); 101 connection.setConnectionCapabilities(connection.getConnectionCapabilities() 102 | Connection.CAPABILITY_CAN_SEND_RESPONSE_VIA_CONNECTION 103 | Connection.CAPABILITY_SUPPORT_HOLD 104 | Connection.CAPABILITY_HOLD); 105 connection.createMockVideoProvider(); 106 ((Connection) connection).setVideoState(request.getVideoState()); 107 if (request.isRequestingRtt()) { 108 connection.setRttTextStream(request.getRttTextStream()); 109 connection.setConnectionProperties(connection.getConnectionProperties() | 110 Connection.PROPERTY_IS_RTT); 111 } 112 connection.setRinging(); 113 // Emit an extra into the connection. We'll see if it makes it through. 114 Bundle testExtra = new Bundle(); 115 testExtra.putString(EXTRA_TEST, TEST_VALUE); 116 connection.putExtras(testExtra); 117 118 incomingConnections.add(connection); 119 lock.release(); 120 return connection; 121 } 122 123 @Override 124 public void onConference(Connection connection1, Connection connection2) { 125 // Make sure that these connections are already not conferenced. 126 if (connection1.getConference() == null && connection2.getConference() == null) { 127 MockConference conference = new MockConference( 128 (MockConnection)connection1, (MockConnection)connection2); 129 CtsConnectionService.addConferenceToTelecom(conference); 130 conferences.add(conference); 131 132 if (connection1.getState() == Connection.STATE_HOLDING){ 133 connection1.setActive(); 134 } 135 if(connection2.getState() == Connection.STATE_HOLDING){ 136 connection2.setActive(); 137 } 138 139 lock.release(); 140 } 141 } 142 143 @Override 144 public void onRemoteExistingConnectionAdded(RemoteConnection connection) { 145 // Keep track of the remote connections added to the service 146 remoteConnections.add(connection); 147 } 148 149 @Override 150 public void onRemoteConferenceAdded(RemoteConference conference) { 151 // Keep track of the remote connections added to the service 152 remoteConferences.add(conference); 153 } 154 155 @Override 156 public void onConnectionServiceFocusGained() { 157 mEventLock[EVENT_CONNECTION_SERVICE_FOCUS_GAINED].release(); 158 } 159 160 @Override 161 public void onConnectionServiceFocusLost() { 162 mEventLock[EVENT_CONNECTION_SERVICE_FOCUS_LOST].release(); 163 connectionServiceFocusReleased(); 164 } 165 166 public void setCreateVideoProvider(boolean createVideoProvider) { 167 mCreateVideoProvider = createVideoProvider; 168 } 169 170 /** Returns true if the given {@code event} is happened before the default timeout. */ 171 public boolean waitForEvent(int event) { 172 if (event < 0 || event >= mEventLock.length) { 173 return false; 174 } 175 176 try { 177 return mEventLock[event].tryAcquire(DEFAULT_EVENT_TIMEOUT_MS, TimeUnit.MILLISECONDS); 178 } catch (InterruptedException e) { 179 // No interaction for the given event within the given timeout. 180 return false; 181 } 182 } 183 184 private static final Semaphore[] initializeSemaphore(int total) { 185 Semaphore[] locks = new Semaphore[total]; 186 for (int i = 0; i < total; i++) { 187 locks[i] = new Semaphore(0); 188 } 189 return locks; 190 } 191 } 192