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