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.TestUtils.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 
166     @Override
onCallEvent(String event, Bundle extras)167     public void onCallEvent(String event, Bundle extras) {
168         super.onCallEvent(event, extras);
169         if (mInvokeCounterMap.get(ON_CALL_EVENT) != null) {
170             mInvokeCounterMap.get(ON_CALL_EVENT).invoke(event, extras);
171         }
172     }
173 
174     @Override
onPullExternalCall()175     public void onPullExternalCall() {
176         super.onPullExternalCall();
177         if (mInvokeCounterMap.get(ON_PULL_EXTERNAL_CALL) != null) {
178             mInvokeCounterMap.get(ON_PULL_EXTERNAL_CALL).invoke();
179         }
180     }
181 
182     @Override
onExtrasChanged(Bundle extras)183     public void onExtrasChanged(Bundle extras) {
184         super.onExtrasChanged(extras);
185         if (mInvokeCounterMap.get(ON_EXTRAS_CHANGED) != null) {
186             mInvokeCounterMap.get(ON_EXTRAS_CHANGED).invoke(extras);
187         }
188     }
189 
getCurrentState()190     public int getCurrentState()  {
191         return mState;
192     }
193 
getCurrentCallAudioState()194     public CallAudioState getCurrentCallAudioState() {
195         return mCallAudioState;
196     }
197 
getDtmfString()198     public String getDtmfString() {
199         return mDtmfString;
200     }
201 
getInvokeCounter(int counterIndex)202     public InvokeCounter getInvokeCounter(int counterIndex) {
203         if (mInvokeCounterMap.get(counterIndex) == null) {
204             mInvokeCounterMap.put(counterIndex,
205                     new InvokeCounter(getCounterLabel(counterIndex)));
206         }
207         return mInvokeCounterMap.get(counterIndex);
208     }
209 
210     /**
211      * Creates a mock video provider for this connection.
212      */
createMockVideoProvider()213     public void createMockVideoProvider() {
214         final MockVideoProvider mockVideoProvider = new MockVideoProvider(this);
215         mMockVideoProvider = mockVideoProvider;
216         setVideoProvider(mockVideoProvider);
217     }
218 
sendMockVideoQuality(int videoQuality)219     public void sendMockVideoQuality(int videoQuality) {
220         if (mMockVideoProvider == null) {
221             return;
222         }
223         mMockVideoProvider.sendMockVideoQuality(videoQuality);
224     }
225 
sendMockCallSessionEvent(int event)226     public void sendMockCallSessionEvent(int event) {
227         if (mMockVideoProvider == null) {
228             return;
229         }
230         mMockVideoProvider.sendMockCallSessionEvent(event);
231     }
232 
sendMockPeerWidth(int width)233     public void sendMockPeerWidth(int width) {
234         if (mMockVideoProvider == null) {
235             return;
236         }
237         mMockVideoProvider.sendMockPeerWidth(width);
238     }
239 
sendMockSessionModifyRequest(VideoProfile request)240     public void sendMockSessionModifyRequest(VideoProfile request) {
241         if (mMockVideoProvider == null) {
242             return;
243         }
244         mMockVideoProvider.sendMockSessionModifyRequest(request);
245     }
246 
getMockVideoProvider()247     public MockVideoProvider getMockVideoProvider() {
248         return mMockVideoProvider;
249     }
250 
setPhoneAccountHandle(PhoneAccountHandle handle)251     public void setPhoneAccountHandle(PhoneAccountHandle handle)  {
252         mPhoneAccountHandle = handle;
253     }
254 
getPhoneAccountHandle()255     public PhoneAccountHandle getPhoneAccountHandle()  {
256         return mPhoneAccountHandle;
257     }
258 
setRemoteConnection(RemoteConnection remoteConnection)259     public void setRemoteConnection(RemoteConnection remoteConnection)  {
260         mRemoteConnection = remoteConnection;
261     }
262 
getRemoteConnection()263     public RemoteConnection getRemoteConnection()  {
264         return mRemoteConnection;
265     }
266 
getCounterLabel(int counterIndex)267     private static String getCounterLabel(int counterIndex) {
268         switch (counterIndex) {
269             case ON_POST_DIAL_WAIT:
270                 return "onPostDialWait";
271             case ON_CALL_EVENT:
272                 return "onCallEvent";
273             case ON_PULL_EXTERNAL_CALL:
274                 return "onPullExternalCall";
275             case ON_EXTRAS_CHANGED:
276                 return "onExtrasChanged";
277             default:
278                 return "Callback";
279         }
280     }
281 }
282