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