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