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