Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [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 | |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 17 | package com.android.telecomm; |
| 18 | |
Sailesh Nepal | ce704b9 | 2014-03-17 18:31:43 -0700 | [diff] [blame] | 19 | import android.net.Uri; |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 20 | import android.os.Bundle; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 21 | import android.telecomm.CallAudioState; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 22 | import android.telecomm.CallInfo; |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 23 | import android.telecomm.CallServiceDescriptor; |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 24 | import android.telecomm.CallState; |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 25 | import android.telecomm.GatewayInfo; |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 26 | import android.telephony.DisconnectCause; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 27 | |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 28 | import com.google.common.base.Preconditions; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 29 | import com.google.common.collect.ImmutableCollection; |
| 30 | import com.google.common.collect.ImmutableList; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 31 | |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 32 | import java.util.HashSet; |
| 33 | import java.util.LinkedHashSet; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 34 | import java.util.Set; |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 35 | |
Ben Gilad | dd8c608 | 2013-12-30 14:44:08 -0800 | [diff] [blame] | 36 | /** |
| 37 | * Singleton. |
| 38 | * |
| 39 | * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow |
| 40 | * access from other packages specifically refraining from passing the CallsManager instance |
| 41 | * beyond the com.android.telecomm package boundary. |
| 42 | */ |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 43 | public final class CallsManager implements Call.Listener { |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 44 | |
Santos Cordon | 74d420b | 2014-05-07 14:38:47 -0700 | [diff] [blame] | 45 | // TODO(santoscordon): Consider renaming this CallsManagerPlugin. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 46 | interface CallsManagerListener { |
| 47 | void onCallAdded(Call call); |
| 48 | void onCallRemoved(Call call); |
| 49 | void onCallStateChanged(Call call, CallState oldState, CallState newState); |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 50 | void onCallHandoffHandleChanged(Call call, Uri oldHandle, Uri newHandle); |
| 51 | void onCallServiceChanged( |
| 52 | Call call, |
| 53 | CallServiceWrapper oldCallService, |
| 54 | CallServiceWrapper newCallService); |
| 55 | void onCallHandoffCallServiceDescriptorChanged( |
| 56 | Call call, |
| 57 | CallServiceDescriptor oldDescriptor, |
| 58 | CallServiceDescriptor newDescriptor); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 59 | void onIncomingCallAnswered(Call call); |
| 60 | void onIncomingCallRejected(Call call); |
| 61 | void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 62 | void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState); |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame^] | 63 | void onRequestingRingback(Call call, boolean ringback); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 66 | private static final CallsManager INSTANCE = new CallsManager(); |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 67 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 68 | /** |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 69 | * The main call repository. Keeps an instance of all live calls. New incoming and outgoing |
| 70 | * calls are added to the map and removed when the calls move to the disconnected state. |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 71 | */ |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 72 | private final Set<Call> mCalls = new LinkedHashSet<>(); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 73 | |
| 74 | /** |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 75 | * Set of new calls created to perform a handoff. The calls are added when handoff is initiated |
| 76 | * and removed when hadnoff is complete. |
| 77 | */ |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 78 | private final Set<Call> mPendingHandoffCalls = new LinkedHashSet<>(); |
| 79 | |
| 80 | |
| 81 | private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer(); |
| 82 | private final InCallController mInCallController = new InCallController(); |
| 83 | private final CallAudioManager mCallAudioManager; |
| 84 | private final Ringer mRinger; |
| 85 | private final Set<CallsManagerListener> mListeners = new HashSet<>(); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 88 | * The call the user is currently interacting with. This is the call that should have audio |
| 89 | * focus and be visible in the in-call UI. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 90 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 91 | private Call mForegroundCall; |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 92 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 93 | /** Singleton accessor. */ |
| 94 | static CallsManager getInstance() { |
| 95 | return INSTANCE; |
| 96 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 97 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 98 | /** |
Ben Gilad | 8bdaa46 | 2014-02-05 12:53:19 -0800 | [diff] [blame] | 99 | * Initializes the required Telecomm components. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 100 | */ |
| 101 | private CallsManager() { |
Santos Cordon | a0e5f1a | 2014-03-31 21:43:00 -0700 | [diff] [blame] | 102 | TelecommApp app = TelecommApp.getInstance(); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 103 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 104 | mCallAudioManager = new CallAudioManager(); |
Santos Cordon | c7b8eba | 2014-04-01 15:26:28 -0700 | [diff] [blame] | 105 | InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 106 | mRinger = new Ringer(mCallAudioManager, this, playerFactory, app); |
| 107 | |
Santos Cordon | a0e5f1a | 2014-03-31 21:43:00 -0700 | [diff] [blame] | 108 | mListeners.add(new CallLogManager(app)); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 109 | mListeners.add(new PhoneStateBroadcaster()); |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 110 | mListeners.add(mInCallController); |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 111 | mListeners.add(mRinger); |
Santos Cordon | c7b8eba | 2014-04-01 15:26:28 -0700 | [diff] [blame] | 112 | mListeners.add(new RingbackPlayer(this, playerFactory)); |
| 113 | mListeners.add(new InCallToneMonitor(playerFactory, this)); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 114 | mListeners.add(mCallAudioManager); |
Santos Cordon | a0e5f1a | 2014-03-31 21:43:00 -0700 | [diff] [blame] | 115 | mListeners.add(app.getMissedCallNotifier()); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 116 | mListeners.add(mDtmfLocalTonePlayer); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 119 | @Override |
| 120 | public void onSuccessfulOutgoingCall(Call call) { |
| 121 | Log.v(this, "onSuccessfulOutgoingCall, %s", call); |
| 122 | if (mCalls.contains(call)) { |
| 123 | // The call's CallService has been updated. |
| 124 | for (CallsManagerListener listener : mListeners) { |
| 125 | listener.onCallServiceChanged(call, null, call.getCallService()); |
| 126 | } |
| 127 | } else if (mPendingHandoffCalls.contains(call)) { |
| 128 | updateHandoffCallServiceDescriptor(call.getOriginalCall(), |
| 129 | call.getCallService().getDescriptor()); |
Santos Cordon | 6cb7ba9 | 2014-05-23 14:09:32 -0700 | [diff] [blame] | 130 | } else { |
| 131 | Log.wtf(this, "unexpected successful call notification: %s", call); |
| 132 | return; |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 133 | } |
Santos Cordon | 6cb7ba9 | 2014-05-23 14:09:32 -0700 | [diff] [blame] | 134 | |
| 135 | markCallAsDialing(call); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void onFailedOutgoingCall(Call call, boolean isAborted) { |
| 140 | Log.v(this, "onFailedOutgoingCall, call: %s, isAborted: %b", call, isAborted); |
| 141 | if (isAborted) { |
| 142 | setCallState(call, CallState.ABORTED); |
| 143 | removeCall(call); |
| 144 | } else { |
| 145 | // TODO: Replace disconnect cause with more specific disconnect causes. |
| 146 | markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void onSuccessfulIncomingCall(Call call, CallInfo callInfo) { |
| 152 | Log.d(this, "onSuccessfulIncomingCall"); |
| 153 | setCallState(call, callInfo.getState()); |
| 154 | addCall(call); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void onFailedIncomingCall(Call call) { |
| 159 | call.removeListener(this); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Ihab Awad | cb387ac | 2014-05-28 16:49:38 -0700 | [diff] [blame^] | 162 | @Override |
| 163 | public void onRequestingRingback(Call call, boolean ringback) { |
| 164 | for (CallsManagerListener listener : mListeners) { |
| 165 | listener.onRequestingRingback(call, ringback); |
| 166 | } |
| 167 | } |
| 168 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 169 | ImmutableCollection<Call> getCalls() { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 170 | return ImmutableList.copyOf(mCalls); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | Call getForegroundCall() { |
| 174 | return mForegroundCall; |
| 175 | } |
| 176 | |
Santos Cordon | ae19306 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 177 | Ringer getRinger() { |
| 178 | return mRinger; |
| 179 | } |
| 180 | |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 181 | InCallController getInCallController() { |
| 182 | return mInCallController; |
| 183 | } |
| 184 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 185 | boolean hasEmergencyCall() { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 186 | for (Call call : mCalls) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 187 | if (call.isEmergencyCall()) { |
| 188 | return true; |
| 189 | } |
| 190 | } |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | CallAudioState getAudioState() { |
| 195 | return mCallAudioManager.getAudioState(); |
| 196 | } |
| 197 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 198 | /** |
Santos Cordon | 493e8f2 | 2014-02-19 03:15:12 -0800 | [diff] [blame] | 199 | * Starts the incoming call sequence by having switchboard gather more information about the |
| 200 | * specified call; using the specified call service descriptor. Upon success, execution returns |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 201 | * to {@link #onSuccessfulIncomingCall} to start the in-call UI. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 202 | * |
Ben Gilad | c5b2269 | 2014-02-18 20:03:22 -0800 | [diff] [blame] | 203 | * @param descriptor The descriptor of the call service to use for this incoming call. |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 204 | * @param extras The optional extras Bundle passed with the intent used for the incoming call. |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 205 | */ |
Evan Charlton | a05805b | 2014-03-05 08:21:46 -0800 | [diff] [blame] | 206 | void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 207 | Log.d(this, "processIncomingCallIntent"); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 208 | // Create a call with no handle. Eventually, switchboard will update the call with |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 209 | // additional information from the call service, but for now we just need one to pass |
| 210 | // around. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 211 | Call call = new Call(true /* isIncoming */); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 212 | // TODO(santoscordon): Move this to be a part of addCall() |
| 213 | call.addListener(this); |
Santos Cordon | 80d9bdc | 2014-02-13 18:28:46 -0800 | [diff] [blame] | 214 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 215 | call.startIncoming(descriptor, extras); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 219 | * Attempts to issue/connect the specified call. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 220 | * |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 221 | * @param handle Handle to connect the call with. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 222 | * @param contactInfo Information about the entity being called. |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 223 | * @param gatewayInfo Optional gateway information that can be used to route the call to the |
Santos Cordon | 5b7b9b3 | 2014-03-26 14:00:22 -0700 | [diff] [blame] | 224 | * actual dialed handle via a gateway provider. May be null. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 225 | */ |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 226 | void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo) { |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 227 | final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle(); |
| 228 | |
| 229 | if (gatewayInfo == null) { |
Santos Cordon | b58f453 | 2014-05-29 11:59:06 -0700 | [diff] [blame] | 230 | Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle)); |
Yorke Lee | 3350163 | 2014-03-17 19:24:12 -0700 | [diff] [blame] | 231 | } else { |
| 232 | Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s", |
| 233 | Log.pii(uriHandle), Log.pii(handle)); |
| 234 | } |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 235 | |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 236 | Call call = new Call(uriHandle, gatewayInfo, false /* isIncoming */); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 237 | |
| 238 | // TODO(santoscordon): Move this to be a part of addCall() |
| 239 | call.addListener(this); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 240 | addCall(call); |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 241 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 242 | call.startOutgoing(); |
Yorke Lee | f98fb57 | 2014-03-05 10:56:55 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 246 | * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call |
| 247 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 248 | * the user opting to answer said call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 249 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 250 | void answerCall(Call call) { |
| 251 | if (!mCalls.contains(call)) { |
| 252 | Log.i(this, "Request to answer a non-existent call %s", call); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 253 | } else { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 254 | // If the foreground call is not the ringing call and it is currently isActive() or |
| 255 | // DIALING, put it on hold before answering the call. |
| 256 | if (mForegroundCall != null && mForegroundCall != call && |
| 257 | (mForegroundCall.isActive() || |
| 258 | mForegroundCall.getState() == CallState.DIALING)) { |
| 259 | Log.v(this, "Holding active/dialing call %s before answering incoming call %s.", |
| 260 | mForegroundCall, call); |
| 261 | mForegroundCall.hold(); |
| 262 | // TODO(santoscordon): Wait until we get confirmation of the active call being |
| 263 | // on-hold before answering the new call. |
| 264 | // TODO(santoscordon): Import logic from CallManager.acceptCall() |
| 265 | } |
| 266 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 267 | for (CallsManagerListener listener : mListeners) { |
| 268 | listener.onIncomingCallAnswered(call); |
| 269 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 270 | |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 271 | // We do not update the UI until we get confirmation of the answer() through |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 272 | // {@link #markCallAsActive}. |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 273 | call.answer(); |
| 274 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call |
| 279 | * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by |
| 280 | * the user opting to reject said call. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 281 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 282 | void rejectCall(Call call) { |
| 283 | if (!mCalls.contains(call)) { |
| 284 | Log.i(this, "Request to reject a non-existent call %s", call); |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 285 | } else { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 286 | for (CallsManagerListener listener : mListeners) { |
| 287 | listener.onIncomingCallRejected(call); |
| 288 | } |
Santos Cordon | 61d0f70 | 2014-02-19 02:52:23 -0800 | [diff] [blame] | 289 | call.reject(); |
| 290 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 294 | * Instructs Telecomm to play the specified DTMF tone within the specified call. |
| 295 | * |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 296 | * @param digit The DTMF digit to play. |
| 297 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 298 | void playDtmfTone(Call call, char digit) { |
| 299 | if (!mCalls.contains(call)) { |
| 300 | Log.i(this, "Request to play DTMF in a non-existent call %s", call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 301 | } else { |
| 302 | call.playDtmfTone(digit); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 303 | mDtmfLocalTonePlayer.playTone(call, digit); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Instructs Telecomm to stop the currently playing DTMF tone, if any. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 309 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 310 | void stopDtmfTone(Call call) { |
| 311 | if (!mCalls.contains(call)) { |
| 312 | Log.i(this, "Request to stop DTMF in a non-existent call %s", call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 313 | } else { |
| 314 | call.stopDtmfTone(); |
Santos Cordon | 92a2d81 | 2014-04-30 15:19:01 -0700 | [diff] [blame] | 315 | mDtmfLocalTonePlayer.stopTone(call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Instructs Telecomm to continue the current post-dial DTMF string, if any. |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 321 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 322 | void postDialContinue(Call call) { |
| 323 | if (!mCalls.contains(call)) { |
| 324 | Log.i(this, "Request to continue post-dial string in a non-existent call %s", call); |
Ihab Awad | 74549ec | 2014-03-10 15:33:25 -0700 | [diff] [blame] | 325 | } else { |
| 326 | // TODO(ihab): Implement this from this level on downwards |
| 327 | // call.postDialContinue(); |
| 328 | // Must play tones locally -- see DTMFTonePlayer.java in Telephony |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /** |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 333 | * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the |
| 334 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 335 | * the user hitting the end-call button. |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 336 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 337 | void disconnectCall(Call call) { |
Santos Cordon | 682fe6b | 2014-05-20 08:56:39 -0700 | [diff] [blame] | 338 | Log.v(this, "disconnectCall %s", call); |
| 339 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 340 | if (!mCalls.contains(call)) { |
| 341 | Log.w(this, "Unknown call (%s) asked to disconnect", call); |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 342 | } else { |
| 343 | call.disconnect(); |
| 344 | } |
Santos Cordon | e3d76ab | 2014-01-28 17:25:20 -0800 | [diff] [blame] | 345 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 346 | |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 347 | /** |
| 348 | * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the |
| 349 | * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by |
| 350 | * the user hitting the hold button during an active call. |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 351 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 352 | void holdCall(Call call) { |
| 353 | if (!mCalls.contains(call)) { |
| 354 | Log.w(this, "Unknown call (%s) asked to be put on hold", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 355 | } else { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 356 | Log.d(this, "Putting call on hold: (%s)", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 357 | call.hold(); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Instructs Telecomm to release the specified call from hold. Intended to be invoked by |
| 363 | * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered |
| 364 | * by the user hitting the hold button during a held call. |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 365 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 366 | void unholdCall(Call call) { |
| 367 | if (!mCalls.contains(call)) { |
| 368 | Log.w(this, "Unknown call (%s) asked to be removed from hold", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 369 | } else { |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 370 | Log.d(this, "unholding call: (%s)", call); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 371 | call.unhold(); |
| 372 | } |
| 373 | } |
| 374 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 375 | /** Called by the in-call UI to change the mute state. */ |
| 376 | void mute(boolean shouldMute) { |
| 377 | mCallAudioManager.mute(shouldMute); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Called by the in-call UI to change the audio route, for example to change from earpiece to |
| 382 | * speaker phone. |
| 383 | */ |
| 384 | void setAudioRoute(int route) { |
| 385 | mCallAudioManager.setAudioRoute(route); |
| 386 | } |
| 387 | |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 388 | void startHandoffForCall(Call originalCall) { |
| 389 | if (!mCalls.contains(originalCall)) { |
| 390 | Log.w(this, "Unknown call %s asked to be handed off", originalCall); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | for (Call handoffCall : mPendingHandoffCalls) { |
| 395 | if (handoffCall.getOriginalCall() == originalCall) { |
| 396 | Log.w(this, "Call %s is already being handed off, skipping", originalCall); |
| 397 | return; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // Create a new call to be placed in the background. If handoff is successful then the |
| 402 | // original call will live on but its state will be updated to the new call's state. In |
| 403 | // particular the original call's call service will be updated to the new call's call |
| 404 | // service. |
Santos Cordon | fd71f4a | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 405 | Call tempCall = |
| 406 | new Call(originalCall.getHandoffHandle(), originalCall.getGatewayInfo(), false); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 407 | tempCall.setOriginalCall(originalCall); |
| 408 | tempCall.setExtras(originalCall.getExtras()); |
| 409 | tempCall.setCallServiceSelector(originalCall.getCallServiceSelector()); |
| 410 | mPendingHandoffCalls.add(tempCall); |
Santos Cordon | 6cb7ba9 | 2014-05-23 14:09:32 -0700 | [diff] [blame] | 411 | tempCall.addListener(this); |
| 412 | |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 413 | Log.d(this, "Placing handoff call"); |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 414 | tempCall.startOutgoing(); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 417 | /** Called when the audio state changes. */ |
| 418 | void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) { |
| 419 | Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 420 | for (CallsManagerListener listener : mListeners) { |
| 421 | listener.onAudioStateChanged(oldAudioState, newAudioState); |
| 422 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 425 | void markCallAsRinging(Call call) { |
| 426 | setCallState(call, CallState.RINGING); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 429 | void markCallAsDialing(Call call) { |
| 430 | setCallState(call, CallState.DIALING); |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 433 | void markCallAsActive(Call call) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 434 | if (call.getConnectTimeMillis() == 0) { |
| 435 | call.setConnectTimeMillis(System.currentTimeMillis()); |
| 436 | } |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 437 | setCallState(call, CallState.ACTIVE); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 438 | |
| 439 | if (mPendingHandoffCalls.contains(call)) { |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 440 | completeHandoff(call, true); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 441 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 444 | void markCallAsOnHold(Call call) { |
| 445 | setCallState(call, CallState.ON_HOLD); |
Yorke Lee | cdf3ebd | 2014-03-12 18:31:41 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 448 | /** |
| 449 | * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last |
| 450 | * live call, then also disconnect from the in-call controller. |
| 451 | * |
Santos Cordon | 79ff2bc | 2014-03-27 15:31:27 -0700 | [diff] [blame] | 452 | * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}. |
| 453 | * @param disconnectMessage Optional call-service-provided message about the disconnect. |
Santos Cordon | 049b7b6 | 2014-01-30 05:34:26 -0800 | [diff] [blame] | 454 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 455 | void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) { |
| 456 | call.setDisconnectCause(disconnectCause, disconnectMessage); |
| 457 | setCallState(call, CallState.DISCONNECTED); |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 458 | |
| 459 | // Only remove the call if handoff is not pending. |
| 460 | if (call.getHandoffCallServiceDescriptor() == null) { |
| 461 | removeCall(call); |
| 462 | } |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Sailesh Nepal | 6ab6fb7 | 2014-04-01 20:03:19 -0700 | [diff] [blame] | 465 | void setHandoffInfo(Call call, Uri handle, Bundle extras) { |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 466 | if (!mCalls.contains(call)) { |
| 467 | Log.w(this, "Unknown call (%s) asked to set handoff info", call); |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | if (extras == null) { |
| 472 | call.setExtras(Bundle.EMPTY); |
| 473 | } else { |
| 474 | call.setExtras(extras); |
| 475 | } |
| 476 | |
| 477 | Uri oldHandle = call.getHandoffHandle(); |
| 478 | Log.v(this, "set handoff handle %s -> %s, for call: %s", oldHandle, handle, call); |
| 479 | if (!areUriEqual(oldHandle, handle)) { |
| 480 | call.setHandoffHandle(handle); |
| 481 | for (CallsManagerListener listener : mListeners) { |
| 482 | listener.onCallHandoffHandleChanged(call, oldHandle, handle); |
| 483 | } |
| 484 | } |
Sailesh Nepal | 6ab6fb7 | 2014-04-01 20:03:19 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 487 | /** |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 488 | * Cleans up any calls currently associated with the specified call service when the |
| 489 | * call-service binder disconnects unexpectedly. |
| 490 | * |
| 491 | * @param callService The call service that disconnected. |
| 492 | */ |
| 493 | void handleCallServiceDeath(CallServiceWrapper callService) { |
| 494 | Preconditions.checkNotNull(callService); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 495 | for (Call call : ImmutableList.copyOf(mCalls)) { |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 496 | if (call.getCallService() == callService) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 497 | markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null); |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
Santos Cordon | c7e85d4 | 2014-05-22 02:51:48 -0700 | [diff] [blame] | 502 | boolean hasActiveOrHoldingCall() { |
| 503 | for (Call call : mCalls) { |
| 504 | CallState state = call.getState(); |
| 505 | if (state == CallState.ACTIVE || state == CallState.ON_HOLD) { |
| 506 | return true; |
| 507 | } |
| 508 | } |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | boolean hasRingingCall() { |
| 513 | for (Call call : mCalls) { |
| 514 | if (call.getState() == CallState.RINGING) { |
| 515 | return true; |
| 516 | } |
| 517 | } |
| 518 | return false; |
| 519 | } |
| 520 | |
Santos Cordon | 4b2c119 | 2014-03-19 18:15:38 -0700 | [diff] [blame] | 521 | /** |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 522 | * Adds the specified call to the main list of live calls. |
| 523 | * |
| 524 | * @param call The call to add. |
| 525 | */ |
| 526 | private void addCall(Call call) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 527 | mCalls.add(call); |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 528 | |
| 529 | // TODO(santoscordon): Update mForegroundCall prior to invoking |
| 530 | // onCallAdded for calls which immediately take the foreground (like the first call). |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 531 | for (CallsManagerListener listener : mListeners) { |
| 532 | listener.onCallAdded(call); |
| 533 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 534 | updateForegroundCall(); |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 535 | } |
| 536 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 537 | private void removeCall(Call call) { |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 538 | // If a handoff is pending then the original call shouldn't be removed. |
| 539 | Preconditions.checkState(call.getHandoffCallServiceDescriptor() == null); |
Santos Cordon | c499c1c | 2014-04-14 17:13:14 -0700 | [diff] [blame] | 540 | Log.v(this, "removeCall(%s)", call); |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 541 | |
Santos Cordon | 766d04f | 2014-05-06 10:28:25 -0700 | [diff] [blame] | 542 | call.removeListener(this); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 543 | call.clearCallService(); |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 544 | call.clearCallServiceSelector(); |
| 545 | |
| 546 | boolean shouldNotify = false; |
| 547 | if (mCalls.contains(call)) { |
| 548 | mCalls.remove(call); |
| 549 | shouldNotify = true; |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 550 | } else if (mPendingHandoffCalls.contains(call)) { |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 551 | Log.v(this, "removeCall, marking handoff call as failed"); |
| 552 | completeHandoff(call, false); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 553 | } |
Ben Gilad | a0d9f75 | 2014-02-26 11:49:03 -0800 | [diff] [blame] | 554 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 555 | // Only broadcast changes for calls that are being tracked. |
| 556 | if (shouldNotify) { |
| 557 | for (CallsManagerListener listener : mListeners) { |
| 558 | listener.onCallRemoved(call); |
| 559 | } |
| 560 | updateForegroundCall(); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 561 | } |
| 562 | } |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 563 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 564 | /** |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 565 | * Sets the specified state on the specified call. |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 566 | * |
| 567 | * @param call The call. |
| 568 | * @param newState The new state of the call. |
| 569 | */ |
| 570 | private void setCallState(Call call, CallState newState) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 571 | Preconditions.checkNotNull(newState); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 572 | CallState oldState = call.getState(); |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 573 | Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 574 | if (newState != oldState) { |
| 575 | // Unfortunately, in the telephony world the radio is king. So if the call notifies |
| 576 | // us that the call is in a particular state, we allow it even if it doesn't make |
| 577 | // sense (e.g., ACTIVE -> RINGING). |
| 578 | // TODO(santoscordon): Consider putting a stop to the above and turning CallState |
| 579 | // into a well-defined state machine. |
| 580 | // TODO(santoscordon): Define expected state transitions here, and log when an |
| 581 | // unexpected transition occurs. |
| 582 | call.setState(newState); |
| 583 | |
| 584 | // Only broadcast state change for calls that are being tracked. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 585 | if (mCalls.contains(call)) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 586 | for (CallsManagerListener listener : mListeners) { |
| 587 | listener.onCallStateChanged(call, oldState, newState); |
| 588 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 589 | updateForegroundCall(); |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | /** |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 595 | * Checks which call should be visible to the user and have audio focus. |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 596 | */ |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 597 | private void updateForegroundCall() { |
| 598 | Call newForegroundCall = null; |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 599 | for (Call call : mCalls) { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 600 | // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless |
| 601 | // of its state will be foreground by default and instead the call service should be |
| 602 | // notified when its calls enter and exit foreground state. Foreground will mean that |
| 603 | // the call should play audio and listen to microphone if it wants. |
| 604 | |
| 605 | // Active calls have priority. |
| 606 | if (call.isActive()) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 607 | newForegroundCall = call; |
Evan Charlton | 5c670a9 | 2014-03-06 14:58:20 -0800 | [diff] [blame] | 608 | break; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 609 | } |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 610 | |
| 611 | if (call.isAlive() || call.getState() == CallState.RINGING) { |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 612 | newForegroundCall = call; |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 613 | // Don't break in case there's an active call that has priority. |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 614 | } |
| 615 | } |
Santos Cordon | 4e9fffe | 2014-03-04 18:13:41 -0800 | [diff] [blame] | 616 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 617 | if (newForegroundCall != mForegroundCall) { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 618 | Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 619 | Call oldForegroundCall = mForegroundCall; |
| 620 | mForegroundCall = newForegroundCall; |
| 621 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 622 | for (CallsManagerListener listener : mListeners) { |
| 623 | listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall); |
| 624 | } |
Santos Cordon | 681663d | 2014-01-30 04:32:15 -0800 | [diff] [blame] | 625 | } |
| 626 | } |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 627 | |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 628 | private void completeHandoff(Call handoffCall, boolean wasSuccessful) { |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 629 | Call originalCall = handoffCall.getOriginalCall(); |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 630 | Log.v(this, "complete handoff, %s -> %s, wasSuccessful: %b", handoffCall, originalCall, |
| 631 | wasSuccessful); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 632 | |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 633 | // Remove the transient handoff call object (don't disconnect because the call could still |
| 634 | // be live). |
| 635 | mPendingHandoffCalls.remove(handoffCall); |
Santos Cordon | 6cb7ba9 | 2014-05-23 14:09:32 -0700 | [diff] [blame] | 636 | handoffCall.removeListener(this); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 637 | |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 638 | if (wasSuccessful) { |
Sailesh Nepal | dcd6426 | 2014-05-23 17:03:28 -0700 | [diff] [blame] | 639 | if (TelephonyUtil.isCurrentlyPSTNCall(originalCall)) { |
| 640 | originalCall.disconnect(); |
| 641 | } |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 642 | |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 643 | // Synchronize. |
| 644 | originalCall.setCallService(handoffCall.getCallService(), handoffCall); |
| 645 | setCallState(originalCall, handoffCall.getState()); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 646 | |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 647 | // Force the foreground call changed notification to be sent. |
| 648 | for (CallsManagerListener listener : mListeners) { |
| 649 | listener.onForegroundCallChanged(mForegroundCall, mForegroundCall); |
| 650 | } |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 651 | |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 652 | updateHandoffCallServiceDescriptor(originalCall, null); |
| 653 | } else { |
| 654 | updateHandoffCallServiceDescriptor(originalCall, null); |
| 655 | if (originalCall.getState() == CallState.DISCONNECTED || |
| 656 | originalCall.getState() == CallState.ABORTED) { |
| 657 | removeCall(originalCall); |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 662 | private void updateHandoffCallServiceDescriptor( |
| 663 | Call originalCall, |
| 664 | CallServiceDescriptor newDescriptor) { |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 665 | CallServiceDescriptor oldDescriptor = originalCall.getHandoffCallServiceDescriptor(); |
Sailesh Nepal | 4857f47 | 2014-04-07 22:26:27 -0700 | [diff] [blame] | 666 | Log.v(this, "updateHandoffCallServiceDescriptor, call: %s, pending descriptor: %s -> %s", |
| 667 | originalCall, oldDescriptor, newDescriptor); |
Sailesh Nepal | 8c85dee | 2014-04-07 22:21:40 -0700 | [diff] [blame] | 668 | |
| 669 | if (!areDescriptorsEqual(oldDescriptor, newDescriptor)) { |
| 670 | originalCall.setHandoffCallServiceDescriptor(newDescriptor); |
| 671 | for (CallsManagerListener listener : mListeners) { |
| 672 | listener.onCallHandoffCallServiceDescriptorChanged(originalCall, oldDescriptor, |
| 673 | newDescriptor); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | private static boolean areDescriptorsEqual( |
| 679 | CallServiceDescriptor descriptor1, |
| 680 | CallServiceDescriptor descriptor2) { |
| 681 | if (descriptor1 == null) { |
| 682 | return descriptor2 == null; |
| 683 | } |
| 684 | return descriptor1.equals(descriptor2); |
| 685 | } |
| 686 | |
Sailesh Nepal | 84fa5f8 | 2014-04-02 11:01:11 -0700 | [diff] [blame] | 687 | private static boolean areUriEqual(Uri handle1, Uri handle2) { |
| 688 | if (handle1 == null) { |
| 689 | return handle2 == null; |
| 690 | } |
| 691 | return handle1.equals(handle2); |
| 692 | } |
Ben Gilad | 9f2bed3 | 2013-12-12 17:43:26 -0800 | [diff] [blame] | 693 | } |