Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.telecomm.testapps; |
| 18 | |
| 19 | import android.app.PendingIntent; |
| 20 | import android.net.Uri; |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 21 | import android.telecomm.AudioState; |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 22 | import android.telecomm.Conference; |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 23 | import android.telecomm.Connection; |
| 24 | import android.telecomm.ConnectionRequest; |
| 25 | import android.telecomm.ConnectionService; |
| 26 | import android.telecomm.PhoneAccountHandle; |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 27 | import android.telecomm.RemoteConference; |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 28 | import android.telecomm.RemoteConnection; |
| 29 | import android.telecomm.StatusHints; |
| 30 | import android.util.Log; |
| 31 | |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 32 | import java.util.ArrayList; |
| 33 | import java.util.HashMap; |
| 34 | import java.util.List; |
| 35 | import java.util.Map; |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 36 | |
| 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 | */ |
| 41 | public class TestConnectionManager extends ConnectionService { |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 42 | public final class TestManagedConnection extends Connection { |
| 43 | private final RemoteConnection.Listener mRemoteListener = new RemoteConnection.Listener() { |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 44 | @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 Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 106 | 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 Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 120 | } |
| 121 | }; |
| 122 | |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 123 | private final RemoteConnection mRemote; |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 124 | private final boolean mIsIncoming; |
| 125 | |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 126 | TestManagedConnection(RemoteConnection remote, boolean isIncoming) { |
| 127 | mRemote = remote; |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 128 | mIsIncoming = isIncoming; |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 129 | mRemote.addListener(mRemoteListener); |
| 130 | setState(mRemote.getState()); |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void onAbort() { |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 135 | mRemote.abort(); |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** ${inheritDoc} */ |
| 139 | @Override |
| 140 | public void onAnswer(int videoState) { |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 141 | mRemote.answer(videoState); |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** ${inheritDoc} */ |
| 145 | @Override |
| 146 | public void onDisconnect() { |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 147 | mRemote.disconnect(); |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Ihab Awad | 39b6a98 | 2014-08-08 17:08:19 -0700 | [diff] [blame] | 150 | @Override |
| 151 | public void onPlayDtmfTone(char c) { |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 152 | mRemote.playDtmfTone(c); |
Ihab Awad | 39b6a98 | 2014-08-08 17:08:19 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 155 | /** ${inheritDoc} */ |
| 156 | @Override |
| 157 | public void onHold() { |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 158 | mRemote.hold(); |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** ${inheritDoc} */ |
| 162 | @Override |
| 163 | public void onReject() { |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 164 | mRemote.reject(); |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** ${inheritDoc} */ |
| 168 | @Override |
| 169 | public void onUnhold() { |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 170 | mRemote.unhold(); |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 174 | public void onSetAudioState(AudioState state) { |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 175 | mRemote.setAudioState(state); |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | private void setState(int state) { |
| 179 | log("setState: " + state); |
| 180 | switch (state) { |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 181 | case STATE_ACTIVE: |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 182 | setActive(); |
| 183 | break; |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 184 | case STATE_HOLDING: |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 185 | setOnHold(); |
| 186 | break; |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 187 | case STATE_DIALING: |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 188 | setDialing(); |
| 189 | break; |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 190 | case STATE_RINGING: |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 191 | setRinging(); |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 197 | 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 Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 275 | private static void log(String msg) { |
Sailesh Nepal | 50e41d0 | 2014-08-20 10:15:47 -0700 | [diff] [blame] | 276 | Log.w("telecomtestcs", "[TestConnectionManager] " + msg); |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 279 | private final Map<RemoteConference, TestManagedConference> mManagedConferenceByRemote |
| 280 | = new HashMap<>(); |
| 281 | private final Map<RemoteConnection, TestManagedConnection> mManagedConnectionByRemote |
| 282 | = new HashMap<>(); |
| 283 | |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 284 | @Override |
| 285 | public Connection onCreateOutgoingConnection( |
| 286 | PhoneAccountHandle connectionManagerAccount, |
| 287 | final ConnectionRequest request) { |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 288 | return makeConnection(request, false); |
Ihab Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public Connection onCreateIncomingConnection( |
| 293 | PhoneAccountHandle connectionManagerAccount, |
| 294 | final ConnectionRequest request) { |
Ihab Awad | 8000845 | 2014-08-23 20:35:44 -0700 | [diff] [blame] | 295 | 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 Awad | c17294c | 2014-08-04 19:23:37 -0700 | [diff] [blame] | 317 | } |
| 318 | } |