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 static android.telecom.CallAudioState.*; 20 21 import android.net.Uri; 22 import android.os.Bundle; 23 import android.telecom.CallAudioState; 24 import android.telecom.Connection; 25 import android.telecom.DisconnectCause; 26 import android.telecom.PhoneAccountHandle; 27 import android.telecom.RemoteConnection; 28 import android.telecom.VideoProfile; 29 import android.telecom.cts.TestUtils.InvokeCounter; 30 import android.util.SparseArray; 31 32 /** 33 * {@link Connection} subclass that immediately performs any state changes that are a result of 34 * callbacks sent from Telecom. 35 */ 36 public class MockConnection extends Connection { 37 public static final int ON_POST_DIAL_WAIT = 1; 38 public static final int ON_CALL_EVENT = 2; 39 public static final int ON_PULL_EXTERNAL_CALL = 3; 40 public static final int ON_EXTRAS_CHANGED = 4; 41 public static final int ON_START_RTT = 5; 42 public static final int ON_RTT_REQUEST_RESPONSE = 6; 43 public static final int ON_STOP_RTT = 7; 44 public static final int ON_DEFLECT = 8; 45 public static final int ON_SILENCE = 9; 46 47 private CallAudioState mCallAudioState = 48 new CallAudioState(false, CallAudioState.ROUTE_EARPIECE, ROUTE_EARPIECE | ROUTE_SPEAKER); 49 private int mState = STATE_NEW; 50 public int videoState = VideoProfile.STATE_AUDIO_ONLY; 51 private String mDtmfString = ""; 52 private MockVideoProvider mMockVideoProvider; 53 private PhoneAccountHandle mPhoneAccountHandle; 54 private RemoteConnection mRemoteConnection = null; 55 private RttTextStream mRttTextStream; 56 57 private SparseArray<InvokeCounter> mInvokeCounterMap = new SparseArray<>(10); 58 59 @Override onAnswer()60 public void onAnswer() { 61 super.onAnswer(); 62 } 63 64 @Override onAnswer(int videoState)65 public void onAnswer(int videoState) { 66 super.onAnswer(videoState); 67 this.videoState = videoState; 68 setActive(); 69 if (mRemoteConnection != null) { 70 mRemoteConnection.answer(); 71 } 72 } 73 74 @Override onReject()75 public void onReject() { 76 super.onReject(); 77 setDisconnected(new DisconnectCause(DisconnectCause.REJECTED)); 78 if (mRemoteConnection != null) { 79 mRemoteConnection.reject(); 80 } 81 destroy(); 82 } 83 84 @Override onReject(String reason)85 public void onReject(String reason) { 86 super.onReject(); 87 setDisconnected(new DisconnectCause(DisconnectCause.REJECTED, reason)); 88 if (mRemoteConnection != null) { 89 mRemoteConnection.reject(); 90 } 91 destroy(); 92 } 93 94 @Override onHold()95 public void onHold() { 96 super.onHold(); 97 setOnHold(); 98 if (mRemoteConnection != null) { 99 mRemoteConnection.hold(); 100 } 101 } 102 103 @Override onUnhold()104 public void onUnhold() { 105 super.onUnhold(); 106 setActive(); 107 if (mRemoteConnection != null) { 108 mRemoteConnection.unhold(); 109 } 110 } 111 112 @Override onDisconnect()113 public void onDisconnect() { 114 super.onDisconnect(); 115 setDisconnected(new DisconnectCause(DisconnectCause.LOCAL)); 116 if (mRemoteConnection != null) { 117 mRemoteConnection.disconnect(); 118 } 119 destroy(); 120 } 121 122 @Override onAbort()123 public void onAbort() { 124 super.onAbort(); 125 setDisconnected(new DisconnectCause(DisconnectCause.UNKNOWN)); 126 if (mRemoteConnection != null) { 127 mRemoteConnection.abort(); 128 } 129 destroy(); 130 } 131 132 @Override onPlayDtmfTone(char c)133 public void onPlayDtmfTone(char c) { 134 super.onPlayDtmfTone(c); 135 mDtmfString += c; 136 if (mRemoteConnection != null) { 137 mRemoteConnection.playDtmfTone(c); 138 } 139 } 140 141 @Override onStopDtmfTone()142 public void onStopDtmfTone() { 143 super.onStopDtmfTone(); 144 mDtmfString += "."; 145 if (mRemoteConnection != null) { 146 mRemoteConnection.stopDtmfTone(); 147 } 148 } 149 150 @Override onCallAudioStateChanged(CallAudioState state)151 public void onCallAudioStateChanged(CallAudioState state) { 152 super.onCallAudioStateChanged(state); 153 mCallAudioState = state; 154 if (mRemoteConnection != null) { 155 mRemoteConnection.setCallAudioState(state); 156 } 157 } 158 159 @Override onStateChanged(int state)160 public void onStateChanged(int state) { 161 super.onStateChanged(state); 162 mState = state; 163 } 164 165 @Override onPostDialContinue(boolean proceed)166 public void onPostDialContinue(boolean proceed) { 167 super.onPostDialContinue(proceed); 168 if (mInvokeCounterMap.get(ON_POST_DIAL_WAIT) != null) { 169 mInvokeCounterMap.get(ON_POST_DIAL_WAIT).invoke(proceed); 170 } 171 } 172 173 @Override onCallEvent(String event, Bundle extras)174 public void onCallEvent(String event, Bundle extras) { 175 super.onCallEvent(event, extras); 176 if (mInvokeCounterMap.get(ON_CALL_EVENT) != null) { 177 mInvokeCounterMap.get(ON_CALL_EVENT).invoke(event, extras); 178 } 179 } 180 181 @Override onPullExternalCall()182 public void onPullExternalCall() { 183 super.onPullExternalCall(); 184 if (mInvokeCounterMap.get(ON_PULL_EXTERNAL_CALL) != null) { 185 mInvokeCounterMap.get(ON_PULL_EXTERNAL_CALL).invoke(); 186 } 187 } 188 189 @Override onExtrasChanged(Bundle extras)190 public void onExtrasChanged(Bundle extras) { 191 super.onExtrasChanged(extras); 192 if (mInvokeCounterMap.get(ON_EXTRAS_CHANGED) != null) { 193 mInvokeCounterMap.get(ON_EXTRAS_CHANGED).invoke(extras); 194 } 195 } 196 197 @Override onStartRtt(RttTextStream rttTextStream)198 public void onStartRtt(RttTextStream rttTextStream) { 199 super.onStartRtt(rttTextStream); 200 if (mInvokeCounterMap.get(ON_START_RTT) != null) { 201 mInvokeCounterMap.get(ON_START_RTT).invoke(rttTextStream); 202 } 203 } 204 205 @Override handleRttUpgradeResponse(RttTextStream rttTextStream)206 public void handleRttUpgradeResponse(RttTextStream rttTextStream) { 207 super.handleRttUpgradeResponse(rttTextStream); 208 if (rttTextStream != null) { 209 setRttTextStream(rttTextStream); 210 setConnectionProperties(getConnectionProperties() | PROPERTY_IS_RTT); 211 } 212 213 if (mInvokeCounterMap.get(ON_RTT_REQUEST_RESPONSE) != null) { 214 mInvokeCounterMap.get(ON_RTT_REQUEST_RESPONSE).invoke(rttTextStream); 215 } 216 } 217 218 @Override onStopRtt()219 public void onStopRtt() { 220 super.onStopRtt(); 221 222 if (mInvokeCounterMap.get(ON_STOP_RTT) != null) { 223 mInvokeCounterMap.get(ON_STOP_RTT).invoke(); 224 } 225 } 226 227 @Override onDeflect(Uri address)228 public void onDeflect(Uri address) { 229 if (mInvokeCounterMap.get(ON_DEFLECT) != null) { 230 mInvokeCounterMap.get(ON_DEFLECT).invoke(address); 231 } 232 } 233 234 @Override onSilence()235 public void onSilence() { 236 super.onSilence(); 237 238 if (mInvokeCounterMap.get(ON_SILENCE) != null) { 239 mInvokeCounterMap.get(ON_SILENCE).invoke(); 240 } 241 } 242 getCurrentState()243 public int getCurrentState() { 244 return mState; 245 } 246 getCurrentCallAudioState()247 public CallAudioState getCurrentCallAudioState() { 248 return mCallAudioState; 249 } 250 getDtmfString()251 public String getDtmfString() { 252 return mDtmfString; 253 } 254 getInvokeCounter(int counterIndex)255 public InvokeCounter getInvokeCounter(int counterIndex) { 256 if (mInvokeCounterMap.get(counterIndex) == null) { 257 mInvokeCounterMap.put(counterIndex, 258 new InvokeCounter(getCounterLabel(counterIndex))); 259 } 260 return mInvokeCounterMap.get(counterIndex); 261 } 262 263 /** 264 * Creates a mock video provider for this connection. 265 */ createMockVideoProvider()266 public void createMockVideoProvider() { 267 final MockVideoProvider mockVideoProvider = new MockVideoProvider(this); 268 mMockVideoProvider = mockVideoProvider; 269 setVideoProvider(mockVideoProvider); 270 } 271 sendMockVideoQuality(int videoQuality)272 public void sendMockVideoQuality(int videoQuality) { 273 if (mMockVideoProvider == null) { 274 return; 275 } 276 mMockVideoProvider.sendMockVideoQuality(videoQuality); 277 } 278 sendMockCallSessionEvent(int event)279 public void sendMockCallSessionEvent(int event) { 280 if (mMockVideoProvider == null) { 281 return; 282 } 283 mMockVideoProvider.sendMockCallSessionEvent(event); 284 } 285 sendMockPeerWidth(int width)286 public void sendMockPeerWidth(int width) { 287 if (mMockVideoProvider == null) { 288 return; 289 } 290 mMockVideoProvider.sendMockPeerWidth(width); 291 } 292 sendMockSessionModifyRequest(VideoProfile request)293 public void sendMockSessionModifyRequest(VideoProfile request) { 294 if (mMockVideoProvider == null) { 295 return; 296 } 297 mMockVideoProvider.sendMockSessionModifyRequest(request); 298 } 299 getMockVideoProvider()300 public MockVideoProvider getMockVideoProvider() { 301 return mMockVideoProvider; 302 } 303 setPhoneAccountHandle(PhoneAccountHandle handle)304 public void setPhoneAccountHandle(PhoneAccountHandle handle) { 305 mPhoneAccountHandle = handle; 306 } 307 getPhoneAccountHandle()308 public PhoneAccountHandle getPhoneAccountHandle() { 309 return mPhoneAccountHandle; 310 } 311 setRemoteConnection(RemoteConnection remoteConnection)312 public void setRemoteConnection(RemoteConnection remoteConnection) { 313 mRemoteConnection = remoteConnection; 314 } 315 getRemoteConnection()316 public RemoteConnection getRemoteConnection() { 317 return mRemoteConnection; 318 } 319 setRttTextStream(RttTextStream rttTextStream)320 public void setRttTextStream(RttTextStream rttTextStream) { 321 mRttTextStream = rttTextStream; 322 } 323 getRttTextStream()324 public RttTextStream getRttTextStream() { 325 return mRttTextStream; 326 } 327 getCounterLabel(int counterIndex)328 private static String getCounterLabel(int counterIndex) { 329 switch (counterIndex) { 330 case ON_POST_DIAL_WAIT: 331 return "onPostDialWait"; 332 case ON_CALL_EVENT: 333 return "onCallEvent"; 334 case ON_PULL_EXTERNAL_CALL: 335 return "onPullExternalCall"; 336 case ON_EXTRAS_CHANGED: 337 return "onExtrasChanged"; 338 case ON_START_RTT: 339 return "onStartRtt"; 340 case ON_RTT_REQUEST_RESPONSE: 341 return "onRttRequestResponse"; 342 case ON_STOP_RTT: 343 return "onStopRtt"; 344 case ON_DEFLECT: 345 return "onDeflect"; 346 case ON_SILENCE: 347 return "onSilence"; 348 default: 349 return "Callback"; 350 } 351 } 352 } 353