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