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