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