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