blob: 95f07185280ab0597dc06e14aafb940c94c8cf98 [file] [log] [blame]
Ihab Awadc17294c2014-08-04 19:23:37 -07001/*
2 * Copyright (C) 2013 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
17package com.android.telecomm.testapps;
18
19import android.app.PendingIntent;
20import android.net.Uri;
Ihab Awad6fb37c82014-08-07 19:48:57 -070021import android.telecomm.AudioState;
Ihab Awad80008452014-08-23 20:35:44 -070022import android.telecomm.Conference;
Ihab Awadc17294c2014-08-04 19:23:37 -070023import android.telecomm.Connection;
24import android.telecomm.ConnectionRequest;
25import android.telecomm.ConnectionService;
26import android.telecomm.PhoneAccountHandle;
Ihab Awad80008452014-08-23 20:35:44 -070027import android.telecomm.RemoteConference;
Ihab Awadc17294c2014-08-04 19:23:37 -070028import android.telecomm.RemoteConnection;
29import android.telecomm.StatusHints;
30import android.util.Log;
31
Ihab Awad80008452014-08-23 20:35:44 -070032import java.util.ArrayList;
33import java.util.HashMap;
34import java.util.List;
35import java.util.Map;
Ihab Awadc17294c2014-08-04 19:23:37 -070036
37/**
38 * Service which acts as a fake ConnectionManager if so configured.
39 * TODO(santoscordon): Rename all classes in the directory to Dummy* (e.g., DummyConnectionService).
40 */
41public class TestConnectionManager extends ConnectionService {
Ihab Awad80008452014-08-23 20:35:44 -070042 public final class TestManagedConnection extends Connection {
43 private final RemoteConnection.Listener mRemoteListener = new RemoteConnection.Listener() {
Ihab Awadc17294c2014-08-04 19:23:37 -070044 @Override
45 public void onStateChanged(RemoteConnection connection, int state) {
46 setState(state);
47 }
48
49 @Override
50 public void onDisconnected(RemoteConnection connection, int cause, String message) {
51 setDisconnected(cause, message);
52 destroy();
53 }
54
55 @Override
56 public void onRequestingRingback(RemoteConnection connection, boolean ringback) {
57 setRequestingRingback(ringback);
58 }
59
60 @Override
61 public void onCallCapabilitiesChanged(RemoteConnection connection,
62 int callCapabilities) {
63 setCallCapabilities(callCapabilities);
64 }
65
66 @Override
67 public void onPostDialWait(RemoteConnection connection, String remainingDigits) {
68 setPostDialWait(remainingDigits);
69 }
70
71 @Override
72 public void onAudioModeIsVoipChanged(RemoteConnection connection, boolean isVoip) {
73 setAudioModeIsVoip(isVoip);
74 }
75
76 @Override
77 public void onStatusHintsChanged(RemoteConnection connection, StatusHints statusHints) {
78 setStatusHints(statusHints);
79 }
80
81 @Override
82 public void onVideoStateChanged(RemoteConnection connection, int videoState) {
83 setVideoState(videoState);
84 }
85
86 @Override
87 public void onHandleChanged(RemoteConnection connection, Uri handle, int presentation) {
88 setHandle(handle, presentation);
89 }
90
91 @Override
92 public void onCallerDisplayNameChanged(
93 RemoteConnection connection, String callerDisplayName, int presentation) {
94 setCallerDisplayName(callerDisplayName, presentation);
95 }
96
97 @Override
98 public void onStartActivityFromInCall(
99 RemoteConnection connection, PendingIntent intent) {
100 startActivityFromInCall(intent);
101 }
102
103 @Override
104 public void onDestroyed(RemoteConnection connection) {
105 destroy();
Ihab Awad80008452014-08-23 20:35:44 -0700106 mManagedConnectionByRemote.remove(mRemote);
107 }
108
109 @Override
110 public void onConferenceableConnectionsChanged(
111 RemoteConnection connect,
112 List<RemoteConnection> conferenceable) {
113 List<Connection> c = new ArrayList<>();
114 for (RemoteConnection remote : conferenceable) {
115 if (mManagedConnectionByRemote.containsKey(remote)) {
116 c.add(mManagedConnectionByRemote.get(remote));
117 }
118 }
119 setConferenceableConnections(c);
Ihab Awadc17294c2014-08-04 19:23:37 -0700120 }
121 };
122
Ihab Awad80008452014-08-23 20:35:44 -0700123 private final RemoteConnection mRemote;
Ihab Awadc17294c2014-08-04 19:23:37 -0700124 private final boolean mIsIncoming;
125
Ihab Awad80008452014-08-23 20:35:44 -0700126 TestManagedConnection(RemoteConnection remote, boolean isIncoming) {
127 mRemote = remote;
Ihab Awadc17294c2014-08-04 19:23:37 -0700128 mIsIncoming = isIncoming;
Ihab Awad80008452014-08-23 20:35:44 -0700129 mRemote.addListener(mRemoteListener);
130 setState(mRemote.getState());
Ihab Awadc17294c2014-08-04 19:23:37 -0700131 }
132
133 @Override
134 public void onAbort() {
Ihab Awad80008452014-08-23 20:35:44 -0700135 mRemote.abort();
Ihab Awadc17294c2014-08-04 19:23:37 -0700136 }
137
138 /** ${inheritDoc} */
139 @Override
140 public void onAnswer(int videoState) {
Ihab Awad80008452014-08-23 20:35:44 -0700141 mRemote.answer(videoState);
Ihab Awadc17294c2014-08-04 19:23:37 -0700142 }
143
144 /** ${inheritDoc} */
145 @Override
146 public void onDisconnect() {
Ihab Awad80008452014-08-23 20:35:44 -0700147 mRemote.disconnect();
Ihab Awadc17294c2014-08-04 19:23:37 -0700148 }
149
Ihab Awad39b6a982014-08-08 17:08:19 -0700150 @Override
151 public void onPlayDtmfTone(char c) {
Ihab Awad80008452014-08-23 20:35:44 -0700152 mRemote.playDtmfTone(c);
Ihab Awad39b6a982014-08-08 17:08:19 -0700153 }
154
Ihab Awadc17294c2014-08-04 19:23:37 -0700155 /** ${inheritDoc} */
156 @Override
157 public void onHold() {
Ihab Awad80008452014-08-23 20:35:44 -0700158 mRemote.hold();
Ihab Awadc17294c2014-08-04 19:23:37 -0700159 }
160
161 /** ${inheritDoc} */
162 @Override
163 public void onReject() {
Ihab Awad80008452014-08-23 20:35:44 -0700164 mRemote.reject();
Ihab Awadc17294c2014-08-04 19:23:37 -0700165 }
166
167 /** ${inheritDoc} */
168 @Override
169 public void onUnhold() {
Ihab Awad80008452014-08-23 20:35:44 -0700170 mRemote.unhold();
Ihab Awadc17294c2014-08-04 19:23:37 -0700171 }
172
173 @Override
Ihab Awad6fb37c82014-08-07 19:48:57 -0700174 public void onSetAudioState(AudioState state) {
Ihab Awad80008452014-08-23 20:35:44 -0700175 mRemote.setAudioState(state);
Ihab Awadc17294c2014-08-04 19:23:37 -0700176 }
177
178 private void setState(int state) {
179 log("setState: " + state);
180 switch (state) {
Ihab Awad6fb37c82014-08-07 19:48:57 -0700181 case STATE_ACTIVE:
Ihab Awadc17294c2014-08-04 19:23:37 -0700182 setActive();
183 break;
Ihab Awad6fb37c82014-08-07 19:48:57 -0700184 case STATE_HOLDING:
Ihab Awadc17294c2014-08-04 19:23:37 -0700185 setOnHold();
186 break;
Ihab Awad6fb37c82014-08-07 19:48:57 -0700187 case STATE_DIALING:
Ihab Awadc17294c2014-08-04 19:23:37 -0700188 setDialing();
189 break;
Ihab Awad6fb37c82014-08-07 19:48:57 -0700190 case STATE_RINGING:
Ihab Awadc17294c2014-08-04 19:23:37 -0700191 setRinging();
192 break;
193 }
194 }
195 }
196
Ihab Awad80008452014-08-23 20:35:44 -0700197 public final class TestManagedConference extends Conference {
198 private final RemoteConference.Listener mRemoteListener = new RemoteConference.Listener() {
199 @Override
200 public void onStateChanged(RemoteConference conference, int oldState, int newState) {
201 switch (newState) {
202 case Connection.STATE_DISCONNECTED:
203 // See onDisconnected below
204 break;
205 case Connection.STATE_HOLDING:
206 setOnHold();
207 break;
208 case Connection.STATE_ACTIVE:
209 setActive();
210 break;
211 default:
212 log("unrecognized state for Conference: " + newState);
213 break;
214 }
215 }
216
217 @Override
218 public void onDisconnected(RemoteConference conference, int cause, String message) {
219 setDisconnected(cause, message);
220 }
221
222 @Override
223 public void onConnectionAdded(
224 RemoteConference conference,
225 RemoteConnection connection) {
226 TestManagedConnection c = mManagedConnectionByRemote.get(connection);
227 if (c == null) {
228 log("onConnectionAdded cannot find remote connection: " + connection);
229 } else {
230 addConnection(c);
231 }
232 }
233
234 @Override
235 public void onConnectionRemoved(
236 RemoteConference conference,
237 RemoteConnection connection) {
238 TestManagedConnection c = mManagedConnectionByRemote.get(connection);
239 if (c == null) {
240 log("onConnectionRemoved cannot find remote connection: " + connection);
241 } else {
242 removeConnection(c);
243 }
244 }
245
246 @Override
247 public void onCapabilitiesChanged(RemoteConference conference, int capabilities) {
248 setCapabilities(capabilities);
249 }
250
251 @Override
252 public void onDestroyed(RemoteConference conference) {
253 destroy();
254 mRemote.removeListener(mRemoteListener);
255 mManagedConferenceByRemote.remove(mRemote);
256 }
257 };
258
259 private final RemoteConference mRemote;
260
261 public TestManagedConference(RemoteConference remote) {
262 super(null);
263 mRemote = remote;
264 remote.addListener(mRemoteListener);
265 setActive();
266 for (RemoteConnection r : remote.getConnections()) {
267 TestManagedConnection c = mManagedConnectionByRemote.get(r);
268 if (c != null) {
269 addConnection(c);
270 }
271 }
272 }
273 }
274
Ihab Awadc17294c2014-08-04 19:23:37 -0700275 private static void log(String msg) {
Sailesh Nepal50e41d02014-08-20 10:15:47 -0700276 Log.w("telecomtestcs", "[TestConnectionManager] " + msg);
Ihab Awadc17294c2014-08-04 19:23:37 -0700277 }
278
Ihab Awad80008452014-08-23 20:35:44 -0700279 private final Map<RemoteConference, TestManagedConference> mManagedConferenceByRemote
280 = new HashMap<>();
281 private final Map<RemoteConnection, TestManagedConnection> mManagedConnectionByRemote
282 = new HashMap<>();
283
Ihab Awadc17294c2014-08-04 19:23:37 -0700284 @Override
285 public Connection onCreateOutgoingConnection(
286 PhoneAccountHandle connectionManagerAccount,
287 final ConnectionRequest request) {
Ihab Awad80008452014-08-23 20:35:44 -0700288 return makeConnection(request, false);
Ihab Awadc17294c2014-08-04 19:23:37 -0700289 }
290
291 @Override
292 public Connection onCreateIncomingConnection(
293 PhoneAccountHandle connectionManagerAccount,
294 final ConnectionRequest request) {
Ihab Awad80008452014-08-23 20:35:44 -0700295 return makeConnection(request, true);
296 }
297
298 @Override
299 public void onConference(Connection a, Connection b) {
300 conferenceRemoteConnections(
301 ((TestManagedConnection) a).mRemote,
302 ((TestManagedConnection) b).mRemote);
303 }
304
305 @Override
306 public void onRemoteConferenceAdded(RemoteConference remoteConference) {
307 addConference(new TestManagedConference(remoteConference));
308 }
309
310 private Connection makeConnection(ConnectionRequest request, boolean incoming) {
311 RemoteConnection remote = incoming
312 ? createRemoteIncomingConnection(request.getAccountHandle(), request)
313 : createRemoteOutgoingConnection(request.getAccountHandle(), request);
314 TestManagedConnection local = new TestManagedConnection(remote, false);
315 mManagedConnectionByRemote.put(remote, local);
316 return local;
Ihab Awadc17294c2014-08-04 19:23:37 -0700317 }
318}