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.os.Bundle; 22 import android.telecom.CallAudioState; 23 import android.telecom.Connection; 24 import android.telecom.DisconnectCause; 25 import android.telecom.PhoneAccountHandle; 26 import android.telecom.RemoteConnection; 27 import android.telecom.VideoProfile; 28 import android.telecom.cts.BaseTelecomTestWithMockServices.InvokeCounter; 29 import android.util.SparseArray; 30 31 /** 32 * {@link Connection} subclass that immediately performs any state changes that are a result of 33 * callbacks sent from Telecom. 34 */ 35 public class MockConnection extends Connection { 36 public static final int ON_POST_DIAL_WAIT = 1; 37 public static final int ON_CALL_EVENT = 2; 38 public static final int ON_PULL_EXTERNAL_CALL = 3; 39 public static final int ON_EXTRAS_CHANGED = 4; 40 41 private CallAudioState mCallAudioState = 42 new CallAudioState(false, CallAudioState.ROUTE_EARPIECE, ROUTE_EARPIECE | ROUTE_SPEAKER); 43 private int mState = STATE_NEW; 44 public int videoState = VideoProfile.STATE_AUDIO_ONLY; 45 private String mDtmfString = ""; 46 private MockVideoProvider mMockVideoProvider; 47 private PhoneAccountHandle mPhoneAccountHandle; 48 private RemoteConnection mRemoteConnection = null; 49 50 private SparseArray<InvokeCounter> mInvokeCounterMap = new SparseArray<>(10); 51 52 @Override onAnswer()53 public void onAnswer() { 54 super.onAnswer(); 55 } 56 57 @Override onAnswer(int videoState)58 public void onAnswer(int videoState) { 59 super.onAnswer(videoState); 60 this.videoState = videoState; 61 setActive(); 62 if (mRemoteConnection != null) { 63 mRemoteConnection.answer(); 64 } 65 } 66 67 @Override onReject()68 public void onReject() { 69 super.onReject(); 70 setDisconnected(new DisconnectCause(DisconnectCause.REJECTED)); 71 if (mRemoteConnection != null) { 72 mRemoteConnection.reject(); 73 } 74 destroy(); 75 } 76 77 @Override onReject(String reason)78 public void onReject(String reason) { 79 super.onReject(); 80 setDisconnected(new DisconnectCause(DisconnectCause.REJECTED, reason)); 81 if (mRemoteConnection != null) { 82 mRemoteConnection.reject(); 83 } 84 destroy(); 85 } 86 87 @Override onHold()88 public void onHold() { 89 super.onHold(); 90 setOnHold(); 91 if (mRemoteConnection != null) { 92 mRemoteConnection.hold(); 93 } 94 } 95 96 @Override onUnhold()97 public void onUnhold() { 98 super.onUnhold(); 99 setActive(); 100 if (mRemoteConnection != null) { 101 mRemoteConnection.unhold(); 102 } 103 } 104 105 @Override onDisconnect()106 public void onDisconnect() { 107 super.onDisconnect(); 108 setDisconnected(new DisconnectCause(DisconnectCause.LOCAL)); 109 if (mRemoteConnection != null) { 110 mRemoteConnection.disconnect(); 111 } 112 destroy(); 113 } 114 115 @Override onAbort()116 public void onAbort() { 117 super.onAbort(); 118 setDisconnected(new DisconnectCause(DisconnectCause.UNKNOWN)); 119 if (mRemoteConnection != null) { 120 mRemoteConnection.abort(); 121 } 122 destroy(); 123 } 124 125 @Override onPlayDtmfTone(char c)126 public void onPlayDtmfTone(char c) { 127 super.onPlayDtmfTone(c); 128 mDtmfString += c; 129 if (mRemoteConnection != null) { 130 mRemoteConnection.playDtmfTone(c); 131 } 132 } 133 134 @Override onStopDtmfTone()135 public void onStopDtmfTone() { 136 super.onStopDtmfTone(); 137 mDtmfString += "."; 138 if (mRemoteConnection != null) { 139 mRemoteConnection.stopDtmfTone(); 140 } 141 } 142 143 @Override onCallAudioStateChanged(CallAudioState state)144 public void onCallAudioStateChanged(CallAudioState state) { 145 super.onCallAudioStateChanged(state); 146 mCallAudioState = state; 147 if (mRemoteConnection != null) { 148 mRemoteConnection.setCallAudioState(state); 149 } 150 } 151 152 @Override onStateChanged(int state)153 public void onStateChanged(int state) { 154 super.onStateChanged(state); 155 mState = state; 156 } 157 158 @Override onPostDialContinue(boolean proceed)159 public void onPostDialContinue(boolean proceed) { 160 super.onPostDialContinue(proceed); 161 if (mInvokeCounterMap.get(ON_POST_DIAL_WAIT) != null) { 162 mInvokeCounterMap.get(ON_POST_DIAL_WAIT).invoke(proceed); 163 } 164 } 165 getCurrentState()166 public int getCurrentState() { 167 return mState; 168 } 169 getCurrentCallAudioState()170 public CallAudioState getCurrentCallAudioState() { 171 return mCallAudioState; 172 } 173 getDtmfString()174 public String getDtmfString() { 175 return mDtmfString; 176 } 177 getInvokeCounter(int counterIndex)178 public InvokeCounter getInvokeCounter(int counterIndex) { 179 if (mInvokeCounterMap.get(counterIndex) == null) { 180 mInvokeCounterMap.put(counterIndex, 181 new InvokeCounter(getCounterLabel(counterIndex))); 182 } 183 return mInvokeCounterMap.get(counterIndex); 184 } 185 186 /** 187 * Creates a mock video provider for this connection. 188 */ createMockVideoProvider()189 public void createMockVideoProvider() { 190 final MockVideoProvider mockVideoProvider = new MockVideoProvider(this); 191 mMockVideoProvider = mockVideoProvider; 192 setVideoProvider(mockVideoProvider); 193 } 194 sendMockVideoQuality(int videoQuality)195 public void sendMockVideoQuality(int videoQuality) { 196 if (mMockVideoProvider == null) { 197 return; 198 } 199 mMockVideoProvider.sendMockVideoQuality(videoQuality); 200 } 201 sendMockCallSessionEvent(int event)202 public void sendMockCallSessionEvent(int event) { 203 if (mMockVideoProvider == null) { 204 return; 205 } 206 mMockVideoProvider.sendMockCallSessionEvent(event); 207 } 208 sendMockPeerWidth(int width)209 public void sendMockPeerWidth(int width) { 210 if (mMockVideoProvider == null) { 211 return; 212 } 213 mMockVideoProvider.sendMockPeerWidth(width); 214 } 215 sendMockSessionModifyRequest(VideoProfile request)216 public void sendMockSessionModifyRequest(VideoProfile request) { 217 if (mMockVideoProvider == null) { 218 return; 219 } 220 mMockVideoProvider.sendMockSessionModifyRequest(request); 221 } 222 getMockVideoProvider()223 public MockVideoProvider getMockVideoProvider() { 224 return mMockVideoProvider; 225 } 226 setPhoneAccountHandle(PhoneAccountHandle handle)227 public void setPhoneAccountHandle(PhoneAccountHandle handle) { 228 mPhoneAccountHandle = handle; 229 } 230 getPhoneAccountHandle()231 public PhoneAccountHandle getPhoneAccountHandle() { 232 return mPhoneAccountHandle; 233 } 234 setRemoteConnection(RemoteConnection remoteConnection)235 public void setRemoteConnection(RemoteConnection remoteConnection) { 236 mRemoteConnection = remoteConnection; 237 } 238 getRemoteConnection()239 public RemoteConnection getRemoteConnection() { 240 return mRemoteConnection; 241 } 242 getCounterLabel(int counterIndex)243 private static String getCounterLabel(int counterIndex) { 244 switch (counterIndex) { 245 case ON_POST_DIAL_WAIT: 246 return "onPostDialWait"; 247 case ON_CALL_EVENT: 248 return "onCallEvent"; 249 case ON_PULL_EXTERNAL_CALL: 250 return "onPullExternalCall"; 251 case ON_EXTRAS_CHANGED: 252 return "onExtrasChanged"; 253 default: 254 return "Callback"; 255 } 256 } 257 } 258