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.Conference;
21 import android.telecom.Connection;
22 import android.telecom.DisconnectCause;
23 import android.telecom.PhoneAccountHandle;
24 import android.telecom.RemoteConference;
25 import android.telecom.TelecomManager;
26 
27 /**
28  * {@link Conference} subclass that immediately performs any state changes that are a result of
29  * callbacks sent from Telecom.
30  */
31 public class MockConference extends Conference {
32 
33     private RemoteConference mRemoteConference = null;
34     private String mDtmfString = "";
35     public BaseTelecomTestWithMockServices.InvokeCounter mOnExtrasChanged =
36             new BaseTelecomTestWithMockServices.InvokeCounter("onExtrasChanged");
37 
MockConference(PhoneAccountHandle phoneAccount)38     public MockConference(PhoneAccountHandle phoneAccount) {
39         super(phoneAccount);
40     }
41 
MockConference(MockConnection a, MockConnection b)42     public MockConference(MockConnection a, MockConnection b) {
43         super(a.getPhoneAccountHandle());
44         addConnection(a);
45         addConnection(b);
46         // a is the primary connection, so inherit the properties from it.
47         setConnectionCapabilities(a.getConnectionCapabilities());
48         setStatusHints(a.getStatusHints());
49         setExtras(a.getExtras());
50     }
51 
52     @Override
onDisconnect()53     public void onDisconnect() {
54         super.onDisconnect();
55         for (Connection c : getConnections()) {
56             c.setDisconnected(new DisconnectCause(DisconnectCause.REMOTE));
57             c.destroy();
58         }
59         destroy();
60         if (mRemoteConference != null) {
61             mRemoteConference.disconnect();
62         }
63     }
64 
65     @Override
onSeparate(Connection connection)66     public void onSeparate(Connection connection) {
67         super.onSeparate(connection);
68         if (getConnections().contains(connection)) {
69             removeConnection(connection);
70         }
71         if (mRemoteConference != null) {
72             mRemoteConference.separate(((MockConnection)connection).getRemoteConnection());
73         }
74     }
75 
76     @Override
onMerge()77     public void onMerge() {
78         super.onMerge();
79         // Let's just change the connection display name for testing that onMerge was invoked.
80         for (Connection c : getConnections()) {
81             c.setCallerDisplayName(
82                     TestUtils.MERGE_CALLER_NAME, TelecomManager.PRESENTATION_ALLOWED);
83         }
84         if (mRemoteConference != null) {
85             mRemoteConference.merge();
86         }
87     }
88 
89     @Override
onSwap()90     public void onSwap() {
91         super.onSwap();
92         // Let's just change the connection display name for testing that onSwap was invoked.
93         for (Connection c : getConnections()) {
94             c.setCallerDisplayName(
95                     TestUtils.SWAP_CALLER_NAME, TelecomManager.PRESENTATION_ALLOWED);
96         }
97         if (mRemoteConference != null) {
98             mRemoteConference.swap();
99         }
100     }
101 
102     @Override
onHold()103     public void onHold() {
104         super.onHold();
105         for (Connection c : getConnections()) {
106             c.setOnHold();
107         }
108         setOnHold();
109         if (mRemoteConference != null) {
110             mRemoteConference.hold();
111         }
112     }
113 
114     @Override
onUnhold()115     public void onUnhold() {
116         super.onUnhold();
117         for (Connection c : getConnections()) {
118             c.setActive();
119         }
120         setActive();
121         if (mRemoteConference != null) {
122             mRemoteConference.unhold();
123         }
124     }
125 
126     @Override
onPlayDtmfTone(char c)127     public void onPlayDtmfTone(char c) {
128         super.onPlayDtmfTone(c);
129         mDtmfString += c;
130         if (mRemoteConference != null) {
131             mRemoteConference.playDtmfTone(c);
132         }
133     }
134 
135     @Override
onStopDtmfTone()136     public void onStopDtmfTone() {
137         super.onStopDtmfTone();
138         mDtmfString += ".";
139         if (mRemoteConference != null) {
140             mRemoteConference.stopDtmfTone();
141         }
142     }
143 
setRemoteConference(RemoteConference remoteConference)144     public void setRemoteConference(RemoteConference remoteConference) {
145         mRemoteConference = remoteConference;
146     }
147 
getRemoteConference()148     public RemoteConference getRemoteConference() {
149         return mRemoteConference;
150     }
151 
getDtmfString()152     public String getDtmfString() {
153         return mDtmfString;
154     }
155 }
156