Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -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 | |
| 19 | import android.net.Uri; |
| 20 | import android.os.Bundle; |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 21 | |
| 22 | import java.util.HashSet; |
| 23 | import java.util.Set; |
| 24 | |
| 25 | /** |
| 26 | * Represents a connection to a remote endpoint that carries voice traffic. |
| 27 | */ |
| 28 | public abstract class Connection { |
| 29 | |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 30 | public interface Listener { |
| 31 | void onStateChanged(Connection c, int state); |
| 32 | void onAudioStateChanged(Connection c, CallAudioState state); |
| 33 | void onHandleChanged(Connection c, Uri newHandle); |
| 34 | void onSignalChanged(Connection c, Bundle details); |
| 35 | void onDisconnected(Connection c, int cause, String message); |
Ihab Awad | f835897 | 2014-05-28 16:46:42 -0700 | [diff] [blame] | 36 | void onRequestingRingback(Connection c, boolean ringback); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 37 | void onDestroyed(Connection c); |
| 38 | } |
| 39 | |
| 40 | public static class ListenerBase implements Listener { |
| 41 | /** {@inheritDoc} */ |
| 42 | @Override |
| 43 | public void onStateChanged(Connection c, int state) {} |
| 44 | |
| 45 | /** {@inheritDoc} */ |
| 46 | @Override |
| 47 | public void onAudioStateChanged(Connection c, CallAudioState state) {} |
| 48 | |
| 49 | /** {@inheritDoc} */ |
| 50 | @Override |
| 51 | public void onHandleChanged(Connection c, Uri newHandle) {} |
| 52 | |
| 53 | /** {@inheritDoc} */ |
| 54 | @Override |
| 55 | public void onSignalChanged(Connection c, Bundle details) {} |
| 56 | |
| 57 | /** {@inheritDoc} */ |
| 58 | @Override |
| 59 | public void onDisconnected(Connection c, int cause, String message) {} |
| 60 | |
| 61 | /** {@inheritDoc} */ |
| 62 | @Override |
| 63 | public void onDestroyed(Connection c) {} |
Ihab Awad | f835897 | 2014-05-28 16:46:42 -0700 | [diff] [blame] | 64 | |
| 65 | /** {@inheritDoc} */ |
| 66 | @Override |
| 67 | public void onRequestingRingback(Connection c, boolean ringback) {} |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | public final class State { |
| 71 | private State() {} |
| 72 | |
| 73 | public static final int NEW = 0; |
| 74 | public static final int RINGING = 1; |
| 75 | public static final int DIALING = 2; |
| 76 | public static final int ACTIVE = 3; |
| 77 | public static final int HOLDING = 4; |
| 78 | public static final int DISCONNECTED = 5; |
| 79 | } |
| 80 | |
| 81 | private final Set<Listener> mListeners = new HashSet<>(); |
| 82 | private int mState = State.NEW; |
| 83 | private CallAudioState mCallAudioState; |
| 84 | private Uri mHandle; |
Ihab Awad | f835897 | 2014-05-28 16:46:42 -0700 | [diff] [blame] | 85 | private boolean mRequestingRingback = false; |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Create a new Connection. |
| 89 | */ |
| 90 | protected Connection() {} |
| 91 | |
| 92 | /** |
| 93 | * @return The handle (e.g., phone number) to which this Connection |
| 94 | * is currently communicating. |
| 95 | */ |
| 96 | public final Uri getHandle() { |
| 97 | return mHandle; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @return The state of this Connection. |
| 102 | * |
| 103 | * @hide |
| 104 | */ |
| 105 | public final int getState() { |
| 106 | return mState; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return The audio state of the call, describing how its audio is currently |
| 111 | * being routed by the system. This is {@code null} if this Connection |
| 112 | * does not directly know about its audio state. |
| 113 | */ |
| 114 | public final CallAudioState getCallAudioState() { |
| 115 | return mCallAudioState; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Assign a listener to be notified of state changes. |
| 120 | * |
| 121 | * @param l A listener. |
| 122 | * @return This Connection. |
| 123 | * |
| 124 | * @hide |
| 125 | */ |
| 126 | public final Connection addConnectionListener(Listener l) { |
| 127 | mListeners.add(l); |
| 128 | return this; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Remove a previously assigned listener that was being notified of state changes. |
| 133 | * |
| 134 | * @param l A Listener. |
| 135 | * @return This Connection. |
| 136 | * |
| 137 | * @hide |
| 138 | */ |
| 139 | public final Connection removeConnectionListener(Listener l) { |
| 140 | mListeners.remove(l); |
| 141 | return this; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Play a DTMF tone in this Connection. |
| 146 | * |
| 147 | * @param c A DTMF character. |
| 148 | * |
| 149 | * @hide |
| 150 | */ |
| 151 | public final void playDtmfTone(char c) { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 152 | Log.d(this, "playDtmfTone %c", c); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 153 | onPlayDtmfTone(c); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Stop any DTMF tones which may be playing in this Connection. |
| 158 | * |
| 159 | * @hide |
| 160 | */ |
| 161 | public final void stopDtmfTone() { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 162 | Log.d(this, "stopDtmfTone"); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 163 | onStopDtmfTone(); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Disconnect this Connection. If and when the Connection can comply with |
| 168 | * this request, it will transition to the {@link State#DISCONNECTED} |
| 169 | * state and notify its listeners. |
| 170 | * |
| 171 | * @hide |
| 172 | */ |
| 173 | public final void disconnect() { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 174 | Log.d(this, "disconnect"); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 175 | onDisconnect(); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Abort this Connection. The Connection will immediately transition to |
| 180 | * the {@link State#DISCONNECTED} state, and send no notifications of this |
| 181 | * or any other future events. |
| 182 | * |
| 183 | * @hide |
| 184 | */ |
| 185 | public final void abort() { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 186 | Log.d(this, "abort"); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 187 | onAbort(); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Place this Connection on hold. If and when the Connection can comply with |
| 192 | * this request, it will transition to the {@link State#HOLDING} |
| 193 | * state and notify its listeners. |
| 194 | * |
| 195 | * @hide |
| 196 | */ |
| 197 | public final void hold() { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 198 | Log.d(this, "hold"); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 199 | onHold(); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Un-hold this Connection. If and when the Connection can comply with |
| 204 | * this request, it will transition to the {@link State#ACTIVE} |
| 205 | * state and notify its listeners. |
| 206 | * |
| 207 | * @hide |
| 208 | */ |
| 209 | public final void unhold() { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 210 | Log.d(this, "unhold"); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 211 | onUnhold(); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Accept a {@link State#RINGING} Connection. If and when the Connection |
| 216 | * can comply with this request, it will transition to the {@link State#ACTIVE} |
| 217 | * state and notify its listeners. |
| 218 | * |
| 219 | * @hide |
| 220 | */ |
| 221 | public final void answer() { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 222 | Log.d(this, "answer"); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 223 | if (mState == State.RINGING) { |
| 224 | onAnswer(); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Reject a {@link State#RINGING} Connection. If and when the Connection |
| 230 | * can comply with this request, it will transition to the {@link State#ACTIVE} |
| 231 | * state and notify its listeners. |
| 232 | * |
| 233 | * @hide |
| 234 | */ |
| 235 | public final void reject() { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 236 | Log.d(this, "reject"); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 237 | if (mState == State.RINGING) { |
| 238 | onReject(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Inform this Connection that the state of its audio output has been changed externally. |
| 244 | * |
| 245 | * @param state The new audio state. |
| 246 | */ |
| 247 | public void setAudioState(CallAudioState state) { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 248 | Log.d(this, "setAudioState %s", state); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 249 | onSetAudioState(state); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * @param state An integer value from {@link State}. |
| 254 | * @return A string representation of the value. |
| 255 | */ |
| 256 | public static String stateToString(int state) { |
| 257 | switch (state) { |
| 258 | case State.NEW: |
| 259 | return "NEW"; |
| 260 | case State.RINGING: |
| 261 | return "RINGING"; |
| 262 | case State.DIALING: |
| 263 | return "DIALING"; |
| 264 | case State.ACTIVE: |
| 265 | return "ACTIVE"; |
| 266 | case State.HOLDING: |
| 267 | return "HOLDING"; |
| 268 | case State.DISCONNECTED: |
| 269 | return "DISCONNECTED"; |
| 270 | default: |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 271 | Log.wtf(Connection.class, "Unknown state %d", state); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 272 | return "UNKNOWN"; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /** |
Ihab Awad | f835897 | 2014-05-28 16:46:42 -0700 | [diff] [blame] | 277 | * @return Whether this connection is requesting that the system play a ringback tone |
| 278 | * on its behalf. |
| 279 | */ |
| 280 | public boolean isRequestingRingback() { |
| 281 | return mRequestingRingback; |
| 282 | } |
| 283 | |
| 284 | /** |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 285 | * Sets the value of the {@link #getHandle()} property and notifies listeners. |
| 286 | * |
| 287 | * @param handle The new handle. |
| 288 | */ |
| 289 | protected void setHandle(Uri handle) { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 290 | Log.d(this, "setHandle %s", handle); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 291 | // TODO: Enforce super called |
| 292 | mHandle = handle; |
| 293 | for (Listener l : mListeners) { |
| 294 | l.onHandleChanged(this, handle); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Sets state to active (e.g., an ongoing call where two or more parties can actively |
| 300 | * communicate). |
| 301 | */ |
| 302 | protected void setActive() { |
Ihab Awad | f835897 | 2014-05-28 16:46:42 -0700 | [diff] [blame] | 303 | setRequestingRingback(false); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 304 | setState(State.ACTIVE); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Sets state to ringing (e.g., an inbound ringing call). |
| 309 | */ |
| 310 | protected void setRinging() { |
| 311 | setState(State.RINGING); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Sets state to dialing (e.g., dialing an outbound call). |
| 316 | */ |
| 317 | protected void setDialing() { |
| 318 | setState(State.DIALING); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Sets state to be on hold. |
| 323 | */ |
| 324 | protected void setOnHold() { |
| 325 | setState(State.HOLDING); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Sets state to disconnected. This will first notify listeners with an |
| 330 | * {@link Listener#onStateChanged(Connection, int)} event, then will fire an |
| 331 | * {@link Listener#onDisconnected(Connection, int, String)} event with additional |
| 332 | * details. |
| 333 | * |
| 334 | * @param cause The reason for the disconnection, any of |
| 335 | * {@link android.telephony.DisconnectCause}. |
| 336 | * @param message Optional call-service-provided message about the disconnect. |
| 337 | */ |
| 338 | protected void setDisconnected(int cause, String message) { |
| 339 | setState(State.DISCONNECTED); |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 340 | Log.d(this, "Disconnected with cause %d message %s", cause, message); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 341 | for (Listener l : mListeners) { |
| 342 | l.onDisconnected(this, cause, message); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /** |
Ihab Awad | f835897 | 2014-05-28 16:46:42 -0700 | [diff] [blame] | 347 | * Requests that the framework play a ringback tone. This is to be invoked by implementations |
| 348 | * that do not play a ringback tone themselves in the call's audio stream. |
| 349 | * |
| 350 | * @param ringback Whether the ringback tone is to be played. |
| 351 | */ |
| 352 | protected void setRequestingRingback(boolean ringback) { |
| 353 | if (mRequestingRingback != ringback) { |
| 354 | mRequestingRingback = ringback; |
| 355 | for (Listener l : mListeners) { |
| 356 | l.onRequestingRingback(this, ringback); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /** |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 362 | * Notifies this Connection and listeners that the {@link #getCallAudioState()} property |
| 363 | * has a new value. |
| 364 | * |
| 365 | * @param state The new call audio state. |
| 366 | */ |
| 367 | protected void onSetAudioState(CallAudioState state) { |
| 368 | // TODO: Enforce super called |
Ihab Awad | f835897 | 2014-05-28 16:46:42 -0700 | [diff] [blame] | 369 | mCallAudioState = state; |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 370 | for (Listener l : mListeners) { |
| 371 | l.onAudioStateChanged(this, state); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Notifies this Connection and listeners of a change in the current signal levels |
| 377 | * for the underlying data transport. |
| 378 | * |
| 379 | * @param details A {@link android.os.Bundle} containing details of the current level. |
| 380 | */ |
| 381 | protected void onSetSignal(Bundle details) { |
| 382 | // TODO: Enforce super called |
| 383 | for (Listener l : mListeners) { |
| 384 | l.onSignalChanged(this, details); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /** |
Ihab Awad | f835897 | 2014-05-28 16:46:42 -0700 | [diff] [blame] | 389 | * Notifies this Connection of an internal state change. This method is called before the |
| 390 | * state is actually changed. Overriding implementations must call |
| 391 | * {@code super.onSetState(state)}. |
| 392 | * |
| 393 | * @param state The new state, a {@link Connection.State} member. |
| 394 | */ |
| 395 | protected void onSetState(int state) { |
| 396 | // TODO: Enforce super called |
| 397 | this.mState = state; |
| 398 | for (Listener l : mListeners) { |
| 399 | l.onStateChanged(this, state); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /** |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 404 | * Notifies this Connection of a request to play a DTMF tone. |
| 405 | * |
| 406 | * @param c A DTMF character. |
| 407 | */ |
| 408 | protected void onPlayDtmfTone(char c) {} |
| 409 | |
| 410 | /** |
| 411 | * Notifies this Connection of a request to stop any currently playing DTMF tones. |
| 412 | */ |
| 413 | protected void onStopDtmfTone() {} |
| 414 | |
| 415 | /** |
| 416 | * Notifies this Connection of a request to disconnect. |
| 417 | */ |
| 418 | protected void onDisconnect() {} |
| 419 | |
| 420 | /** |
| 421 | * Notifies this Connection of a request to abort. |
| 422 | */ |
| 423 | protected void onAbort() {} |
| 424 | |
| 425 | /** |
| 426 | * Notifies this Connection of a request to hold. |
| 427 | */ |
| 428 | protected void onHold() {} |
| 429 | |
| 430 | /** |
| 431 | * Notifies this Connection of a request to exit a hold state. |
| 432 | */ |
| 433 | protected void onUnhold() {} |
| 434 | |
| 435 | /** |
| 436 | * Notifies this Connection, which is in {@link State#RINGING}, of |
| 437 | * a request to accept. |
| 438 | */ |
| 439 | protected void onAnswer() {} |
| 440 | |
| 441 | /** |
| 442 | * Notifies this Connection, which is in {@link State#RINGING}, of |
| 443 | * a request to reject. |
| 444 | */ |
| 445 | protected void onReject() {} |
| 446 | |
Evan Charlton | 6dea4ac | 2014-06-03 14:07:13 -0700 | [diff] [blame^] | 447 | /** |
| 448 | * Notifies this Connection whether the user wishes to proceed with the post-dial DTMF codes. |
| 449 | */ |
| 450 | protected void onPostDialContinue(boolean proceed) {} |
| 451 | |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 452 | private void setState(int state) { |
Ihab Awad | 60ac30b | 2014-05-20 22:32:12 -0700 | [diff] [blame] | 453 | Log.d(this, "setState: %s", stateToString(state)); |
Ihab Awad | f835897 | 2014-05-28 16:46:42 -0700 | [diff] [blame] | 454 | onSetState(state); |
Ihab Awad | 542e0ea | 2014-05-16 10:22:16 -0700 | [diff] [blame] | 455 | } |
| 456 | } |