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