Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.telecomm; |
| 18 | |
Sailesh Nepal | 2ab88cc | 2014-07-18 14:49:18 -0700 | [diff] [blame] | 19 | import android.app.PendingIntent; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 20 | import android.net.Uri; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 21 | import android.os.RemoteException; |
| 22 | import android.telephony.DisconnectCause; |
| 23 | |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 24 | import com.android.internal.telecomm.IConnectionService; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 25 | |
| 26 | import java.util.HashSet; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | /** |
| 30 | * RemoteConnection object used by RemoteConnectionService. |
| 31 | */ |
| 32 | public final class RemoteConnection { |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame^] | 33 | public static abstract class Listener { |
| 34 | public void onStateChanged(RemoteConnection connection, int state) {} |
| 35 | public void onDisconnected(RemoteConnection connection, int cause, String message) {} |
| 36 | public void onRequestingRingback(RemoteConnection connection, boolean ringback) {} |
| 37 | public void onCallCapabilitiesChanged(RemoteConnection connection, int callCapabilities) {} |
| 38 | public void onPostDialWait(RemoteConnection connection, String remainingDigits) {} |
| 39 | public void onAudioModeIsVoipChanged(RemoteConnection connection, boolean isVoip) {} |
| 40 | public void onStatusHintsChanged(RemoteConnection connection, StatusHints statusHints) {} |
| 41 | public void onHandleChanged(RemoteConnection connection, Uri handle, int presentation) {} |
| 42 | public void onCallerDisplayNameChanged( |
| 43 | RemoteConnection connection, String callerDisplayName, int presentation) {} |
| 44 | public void onVideoStateChanged(RemoteConnection connection, int videoState) {} |
| 45 | public void onStartActivityFromInCall(RemoteConnection connection, PendingIntent intent) {} |
| 46 | public void onDestroyed(RemoteConnection connection) {} |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame^] | 49 | private IConnectionService mConnectionService; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 50 | private final String mConnectionId; |
| 51 | private final Set<Listener> mListeners = new HashSet<>(); |
| 52 | |
Sailesh Nepal | ade3f25 | 2014-07-01 17:25:37 -0700 | [diff] [blame] | 53 | private int mState = Connection.State.NEW; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 54 | private int mDisconnectCause = DisconnectCause.NOT_VALID; |
| 55 | private String mDisconnectMessage; |
| 56 | private boolean mRequestingRingback; |
| 57 | private boolean mConnected; |
Sailesh Nepal | 1a7061b | 2014-07-09 21:03:20 -0700 | [diff] [blame] | 58 | private int mCallCapabilities; |
Tyler Gunn | aa07df8 | 2014-07-17 07:50:22 -0700 | [diff] [blame] | 59 | private int mVideoState; |
Sailesh Nepal | 33aaae4 | 2014-07-07 22:49:44 -0700 | [diff] [blame] | 60 | private boolean mAudioModeIsVoip; |
Sailesh Nepal | e7ef59a | 2014-07-08 21:48:22 -0700 | [diff] [blame] | 61 | private StatusHints mStatusHints; |
Sailesh Nepal | 6120386 | 2014-07-11 14:50:13 -0700 | [diff] [blame] | 62 | private Uri mHandle; |
| 63 | private int mHandlePresentation; |
| 64 | private String mCallerDisplayName; |
| 65 | private int mCallerDisplayNamePresentation; |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame^] | 66 | private int mFailureCode; |
| 67 | private String mFailureMessage; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * @hide |
| 71 | */ |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame^] | 72 | RemoteConnection(IConnectionService connectionService, ConnectionRequest request, |
| 73 | boolean isIncoming) { |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 74 | mConnectionService = connectionService; |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame^] | 75 | mConnectionId = request.getCallId(); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 76 | |
| 77 | mConnected = true; |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame^] | 78 | mState = Connection.State.INITIALIZING; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Create a RemoteConnection which is used for failed connections. Note that using it for any |
| 83 | * "real" purpose will almost certainly fail. Callers should note the failure and act |
| 84 | * accordingly (moving on to another RemoteConnection, for example) |
| 85 | * |
| 86 | * @param failureCode |
| 87 | * @param failureMessage |
| 88 | */ |
| 89 | private RemoteConnection(int failureCode, String failureMessage) { |
| 90 | this(null, null, true); |
| 91 | mConnected = false; |
| 92 | mState = Connection.State.FAILED; |
| 93 | mFailureCode = failureCode; |
| 94 | mFailureMessage = failureMessage; |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | public void addListener(Listener listener) { |
| 98 | mListeners.add(listener); |
| 99 | } |
| 100 | |
| 101 | public void removeListener(Listener listener) { |
| 102 | mListeners.remove(listener); |
| 103 | } |
| 104 | |
Sailesh Nepal | ade3f25 | 2014-07-01 17:25:37 -0700 | [diff] [blame] | 105 | public int getState() { |
| 106 | return mState; |
| 107 | } |
| 108 | |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 109 | public int getDisconnectCause() { |
| 110 | return mDisconnectCause; |
| 111 | } |
| 112 | |
| 113 | public String getDisconnectMessage() { |
| 114 | return mDisconnectMessage; |
| 115 | } |
| 116 | |
Sailesh Nepal | 1a7061b | 2014-07-09 21:03:20 -0700 | [diff] [blame] | 117 | public int getCallCapabilities() { |
| 118 | return mCallCapabilities; |
| 119 | } |
| 120 | |
Sailesh Nepal | 33aaae4 | 2014-07-07 22:49:44 -0700 | [diff] [blame] | 121 | public boolean getAudioModeIsVoip() { |
| 122 | return mAudioModeIsVoip; |
| 123 | } |
| 124 | |
Sailesh Nepal | e7ef59a | 2014-07-08 21:48:22 -0700 | [diff] [blame] | 125 | public StatusHints getStatusHints() { |
| 126 | return mStatusHints; |
| 127 | } |
| 128 | |
Sailesh Nepal | 6120386 | 2014-07-11 14:50:13 -0700 | [diff] [blame] | 129 | public Uri getHandle() { |
| 130 | return mHandle; |
| 131 | } |
| 132 | |
| 133 | public int getHandlePresentation() { |
| 134 | return mHandlePresentation; |
| 135 | } |
| 136 | |
| 137 | public String getCallerDisplayName() { |
| 138 | return mCallerDisplayName; |
| 139 | } |
| 140 | |
| 141 | public int getCallerDisplayNamePresentation() { |
| 142 | return mCallerDisplayNamePresentation; |
| 143 | } |
| 144 | |
Tyler Gunn | aa07df8 | 2014-07-17 07:50:22 -0700 | [diff] [blame] | 145 | public int getVideoState() { |
| 146 | return mVideoState; |
| 147 | } |
| 148 | |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame^] | 149 | public int getFailureCode() { |
| 150 | return mFailureCode; |
| 151 | } |
| 152 | |
| 153 | public String getFailureMessage() { |
| 154 | return mFailureMessage; |
| 155 | } |
| 156 | |
Sailesh Nepal | 091768c | 2014-06-30 15:15:23 -0700 | [diff] [blame] | 157 | public void abort() { |
| 158 | try { |
| 159 | if (mConnected) { |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 160 | mConnectionService.abort(mConnectionId); |
Sailesh Nepal | 091768c | 2014-06-30 15:15:23 -0700 | [diff] [blame] | 161 | } |
| 162 | } catch (RemoteException ignored) { |
| 163 | } |
| 164 | } |
| 165 | |
Andrew Lee | 8da4c3c | 2014-07-16 10:11:42 -0700 | [diff] [blame] | 166 | public void answer(int videoState) { |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 167 | try { |
| 168 | if (mConnected) { |
Andrew Lee | 8da4c3c | 2014-07-16 10:11:42 -0700 | [diff] [blame] | 169 | mConnectionService.answer(mConnectionId, videoState); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 170 | } |
| 171 | } catch (RemoteException ignored) { |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | public void reject() { |
| 176 | try { |
| 177 | if (mConnected) { |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 178 | mConnectionService.reject(mConnectionId); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 179 | } |
| 180 | } catch (RemoteException ignored) { |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | public void hold() { |
| 185 | try { |
| 186 | if (mConnected) { |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 187 | mConnectionService.hold(mConnectionId); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 188 | } |
| 189 | } catch (RemoteException ignored) { |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | public void unhold() { |
| 194 | try { |
| 195 | if (mConnected) { |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 196 | mConnectionService.unhold(mConnectionId); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 197 | } |
| 198 | } catch (RemoteException ignored) { |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | public void disconnect() { |
| 203 | try { |
| 204 | if (mConnected) { |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 205 | mConnectionService.disconnect(mConnectionId); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 206 | } |
| 207 | } catch (RemoteException ignored) { |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | public void playDtmf(char digit) { |
| 212 | try { |
| 213 | if (mConnected) { |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 214 | mConnectionService.playDtmfTone(mConnectionId, digit); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 215 | } |
| 216 | } catch (RemoteException ignored) { |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | public void stopDtmf() { |
| 221 | try { |
| 222 | if (mConnected) { |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 223 | mConnectionService.stopDtmfTone(mConnectionId); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 224 | } |
| 225 | } catch (RemoteException ignored) { |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | public void postDialContinue(boolean proceed) { |
| 230 | try { |
| 231 | if (mConnected) { |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 232 | mConnectionService.onPostDialContinue(mConnectionId, proceed); |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 233 | } |
| 234 | } catch (RemoteException ignored) { |
| 235 | } |
| 236 | } |
| 237 | |
Sailesh Nepal | 6120386 | 2014-07-11 14:50:13 -0700 | [diff] [blame] | 238 | public void swapWithBackgroundCall() { |
| 239 | try { |
| 240 | if (mConnected) { |
| 241 | mConnectionService.swapWithBackgroundCall(mConnectionId); |
| 242 | } |
| 243 | } catch (RemoteException ignored) { |
| 244 | } |
| 245 | } |
| 246 | |
Sailesh Nepal | 091768c | 2014-06-30 15:15:23 -0700 | [diff] [blame] | 247 | public void setAudioState(CallAudioState state) { |
| 248 | try { |
| 249 | if (mConnected) { |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 250 | mConnectionService.onAudioStateChanged(mConnectionId, state); |
Sailesh Nepal | 091768c | 2014-06-30 15:15:23 -0700 | [diff] [blame] | 251 | } |
| 252 | } catch (RemoteException ignored) { |
| 253 | } |
| 254 | } |
| 255 | |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 256 | /** |
| 257 | * @hide |
| 258 | */ |
| 259 | void setState(int state) { |
| 260 | if (mState != state) { |
| 261 | mState = state; |
| 262 | for (Listener l: mListeners) { |
| 263 | l.onStateChanged(this, state); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * @hide |
| 270 | */ |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 271 | void setDisconnected(int cause, String message) { |
| 272 | if (mState != Connection.State.DISCONNECTED) { |
| 273 | mState = Connection.State.DISCONNECTED; |
| 274 | mDisconnectCause = cause; |
| 275 | mDisconnectMessage = message; |
| 276 | |
| 277 | for (Listener l : mListeners) { |
| 278 | l.onDisconnected(this, cause, message); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * @hide |
| 285 | */ |
| 286 | void setRequestingRingback(boolean ringback) { |
| 287 | if (mRequestingRingback != ringback) { |
| 288 | mRequestingRingback = ringback; |
| 289 | for (Listener l : mListeners) { |
| 290 | l.onRequestingRingback(this, ringback); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * @hide |
| 297 | */ |
Sailesh Nepal | 1a7061b | 2014-07-09 21:03:20 -0700 | [diff] [blame] | 298 | void setCallCapabilities(int callCapabilities) { |
| 299 | mCallCapabilities = callCapabilities; |
| 300 | for (Listener l : mListeners) { |
| 301 | l.onCallCapabilitiesChanged(this, callCapabilities); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * @hide |
| 307 | */ |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 308 | void setDestroyed() { |
| 309 | if (!mListeners.isEmpty()) { |
| 310 | // Make sure that the listeners are notified that the call is destroyed first. |
| 311 | if (mState != Connection.State.DISCONNECTED) { |
| 312 | setDisconnected(DisconnectCause.ERROR_UNSPECIFIED, "Connection destroyed."); |
| 313 | } |
| 314 | |
| 315 | Set<Listener> listeners = new HashSet<Listener>(mListeners); |
| 316 | mListeners.clear(); |
| 317 | for (Listener l : listeners) { |
| 318 | l.onDestroyed(this); |
| 319 | } |
| 320 | |
| 321 | mConnected = false; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * @hide |
| 327 | */ |
| 328 | void setPostDialWait(String remainingDigits) { |
| 329 | for (Listener l : mListeners) { |
| 330 | l.onPostDialWait(this, remainingDigits); |
| 331 | } |
| 332 | } |
Sailesh Nepal | 2a46b90 | 2014-07-04 17:21:07 -0700 | [diff] [blame] | 333 | |
Tyler Gunn | aa07df8 | 2014-07-17 07:50:22 -0700 | [diff] [blame] | 334 | /** |
| 335 | * @hide |
| 336 | */ |
| 337 | void setVideoState(int videoState) { |
| 338 | mVideoState = videoState; |
| 339 | for (Listener l : mListeners) { |
| 340 | l.onVideoStateChanged(this, videoState); |
| 341 | } |
| 342 | } |
| 343 | |
Sailesh Nepal | 33aaae4 | 2014-07-07 22:49:44 -0700 | [diff] [blame] | 344 | /** @hide */ |
| 345 | void setAudioModeIsVoip(boolean isVoip) { |
| 346 | mAudioModeIsVoip = isVoip; |
| 347 | for (Listener l : mListeners) { |
Sailesh Nepal | 6120386 | 2014-07-11 14:50:13 -0700 | [diff] [blame] | 348 | l.onAudioModeIsVoipChanged(this, isVoip); |
Sailesh Nepal | 33aaae4 | 2014-07-07 22:49:44 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
Sailesh Nepal | e7ef59a | 2014-07-08 21:48:22 -0700 | [diff] [blame] | 351 | |
| 352 | /** @hide */ |
| 353 | void setStatusHints(StatusHints statusHints) { |
| 354 | mStatusHints = statusHints; |
| 355 | for (Listener l : mListeners) { |
Sailesh Nepal | 6120386 | 2014-07-11 14:50:13 -0700 | [diff] [blame] | 356 | l.onStatusHintsChanged(this, statusHints); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /** @hide */ |
| 361 | void setHandle(Uri handle, int presentation) { |
| 362 | mHandle = handle; |
| 363 | mHandlePresentation = presentation; |
| 364 | for (Listener l : mListeners) { |
| 365 | l.onHandleChanged(this, handle, presentation); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** @hide */ |
| 370 | void setCallerDisplayName(String callerDisplayName, int presentation) { |
| 371 | mCallerDisplayName = callerDisplayName; |
| 372 | mCallerDisplayNamePresentation = presentation; |
| 373 | for (Listener l : mListeners) { |
| 374 | l.onCallerDisplayNameChanged(this, callerDisplayName, presentation); |
Sailesh Nepal | e7ef59a | 2014-07-08 21:48:22 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
Sailesh Nepal | 2ab88cc | 2014-07-18 14:49:18 -0700 | [diff] [blame] | 377 | |
| 378 | /** @hide */ |
| 379 | void startActivityFromInCall(PendingIntent intent) { |
| 380 | for (Listener l : mListeners) { |
| 381 | l.onStartActivityFromInCall(this, intent); |
| 382 | } |
| 383 | } |
Evan Charlton | bf11f98 | 2014-07-20 22:06:28 -0700 | [diff] [blame^] | 384 | |
| 385 | /** @hide */ |
| 386 | void setConnectionService(IConnectionService connectionService) { |
| 387 | mConnectionService = connectionService; |
| 388 | mConnectionService = null; |
| 389 | setState(Connection.State.NEW); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Create a RemoteConnection which is in the {@link Connection.State#FAILED} state. Attempting |
| 394 | * to use it for anything will almost certainly result in bad things happening. Do not do this. |
| 395 | * |
| 396 | * @return a failed {@link RemoteConnection} |
| 397 | * |
| 398 | * @hide |
| 399 | * |
| 400 | */ |
| 401 | public static RemoteConnection failure(int failureCode, String failureMessage) { |
| 402 | return new RemoteConnection(failureCode, failureMessage); |
| 403 | } |
Santos Cordon | 52d8a15 | 2014-06-17 19:08:45 -0700 | [diff] [blame] | 404 | } |