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