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