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