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