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