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