blob: 0893d380dc6b4f91fe8b3c562df91d6fdae5e33a [file] [log] [blame]
Santos Cordon8e8b8d22013-12-19 14:14:05 -08001/*
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 Gilad9f2bed32013-12-12 17:43:26 -080017package com.android.telecomm;
18
Sailesh Nepalce704b92014-03-17 18:31:43 -070019import android.net.Uri;
Evan Charltona05805b2014-03-05 08:21:46 -080020import android.os.Bundle;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070021import android.telecomm.CallAudioState;
Ben Giladc5b22692014-02-18 20:03:22 -080022import android.telecomm.CallServiceDescriptor;
Santos Cordon681663d2014-01-30 04:32:15 -080023import android.telecomm.CallState;
Yorke Lee33501632014-03-17 19:24:12 -070024import android.telecomm.GatewayInfo;
Ihab Awad98a55602014-06-30 21:27:28 -070025import android.telecomm.PhoneAccount;
Sailesh Nepal35faf8c2014-07-08 22:02:34 -070026import android.telecomm.StatusHints;
Santos Cordon79ff2bc2014-03-27 15:31:27 -070027import android.telephony.DisconnectCause;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080028
Santos Cordon681663d2014-01-30 04:32:15 -080029import com.google.common.base.Preconditions;
Sailesh Nepal810735e2014-03-18 18:15:46 -070030import com.google.common.collect.ImmutableCollection;
31import com.google.common.collect.ImmutableList;
Ben Gilad9f2bed32013-12-12 17:43:26 -080032
Santos Cordonf3671a62014-05-29 21:51:53 -070033import java.util.HashSet;
34import java.util.LinkedHashSet;
Santos Cordona56f2762014-03-24 15:55:53 -070035import java.util.Set;
Santos Cordon626697a2014-07-10 22:36:37 -070036import java.util.concurrent.CopyOnWriteArraySet;
Ben Gilad9f2bed32013-12-12 17:43:26 -080037
Ben Giladdd8c6082013-12-30 14:44:08 -080038/**
39 * Singleton.
40 *
41 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
42 * access from other packages specifically refraining from passing the CallsManager instance
43 * beyond the com.android.telecomm package boundary.
44 */
Santos Cordon64c7e962014-07-02 15:15:27 -070045public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080046
Santos Cordon74d420b2014-05-07 14:38:47 -070047 // TODO(santoscordon): Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070048 interface CallsManagerListener {
49 void onCallAdded(Call call);
50 void onCallRemoved(Call call);
51 void onCallStateChanged(Call call, CallState oldState, CallState newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070052 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070053 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070054 ConnectionServiceWrapper oldService,
55 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070056 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070057 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070058 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070059 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Ihab Awadcb387ac2014-05-28 16:49:38 -070060 void onRequestingRingback(Call call, boolean ringback);
Sailesh Nepale20bc972014-07-09 21:22:36 -070061 void onCallCapabilitiesChanged(Call call);
Santos Cordona1610702014-06-04 20:22:56 -070062 void onIsConferencedChanged(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070063 void onCannedSmsResponsesLoaded(Call call);
Nancy Chena65d41f2014-06-24 12:06:03 -070064 void onCallVideoProviderChanged(Call call);
Tyler Gunne19cc002014-07-01 11:32:53 -070065 void onFeaturesChanged(Call call);
Sailesh Nepal7e669572014-07-08 21:29:12 -070066 void onAudioModeIsVoipChanged(Call call);
Sailesh Nepal35faf8c2014-07-08 22:02:34 -070067 void onStatusHintsChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070068 }
69
Santos Cordon8e8b8d22013-12-19 14:14:05 -080070 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080071
Santos Cordon8e8b8d22013-12-19 14:14:05 -080072 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070073 * 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 Cordon681663d2014-01-30 04:32:15 -080075 */
Santos Cordon626697a2014-07-10 22:36:37 -070076 private final Set<Call> mCalls = new CopyOnWriteArraySet<Call>();
Santos Cordon681663d2014-01-30 04:32:15 -080077
Santos Cordonf3671a62014-05-29 21:51:53 -070078 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 Cordondeb8c892014-05-30 01:38:03 -070083 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070084
85 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070086 * 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 Cordon4e9fffe2014-03-04 18:13:41 -080088 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070089 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080090
Santos Cordon766d04f2014-05-06 10:28:25 -070091 /** Singleton accessor. */
92 static CallsManager getInstance() {
93 return INSTANCE;
94 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080095
Santos Cordon8e8b8d22013-12-19 14:14:05 -080096 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080097 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080098 */
99 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700100 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -0700101
Santos Cordondeb8c892014-05-30 01:38:03 -0700102 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this);
103 mCallAudioManager = new CallAudioManager(app, statusBarNotifier);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700104 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonae193062014-05-21 21:21:49 -0700105 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
Santos Cordondeb8c892014-05-30 01:38:03 -0700106 mHeadsetMediaButton = new HeadsetMediaButton(app, this);
Santos Cordonae193062014-05-21 21:21:49 -0700107
Santos Cordondeb8c892014-05-30 01:38:03 -0700108 mListeners.add(statusBarNotifier);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700109 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700110 mListeners.add(new PhoneStateBroadcaster());
Santos Cordonf3671a62014-05-29 21:51:53 -0700111 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700112 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700113 mListeners.add(new RingbackPlayer(this, playerFactory));
114 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700115 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700116 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700117 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700118 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700119 mListeners.add(RespondViaSmsManager.getInstance());
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800120 }
121
Santos Cordon766d04f2014-05-06 10:28:25 -0700122 @Override
123 public void onSuccessfulOutgoingCall(Call call) {
124 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
125 if (mCalls.contains(call)) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700126 // The call's ConnectionService has been updated.
Santos Cordon766d04f2014-05-06 10:28:25 -0700127 for (CallsManagerListener listener : mListeners) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700128 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700129 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700130 } else {
131 Log.wtf(this, "unexpected successful call notification: %s", call);
132 return;
Santos Cordon766d04f2014-05-06 10:28:25 -0700133 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700134
135 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700136 }
137
138 @Override
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700139 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 Cordon766d04f2014-05-06 10:28:25 -0700150 }
151
152 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700153 public void onSuccessfulIncomingCall(Call call) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700154 Log.d(this, "onSuccessfulIncomingCall");
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700155 setCallState(call, CallState.RINGING);
Santos Cordon766d04f2014-05-06 10:28:25 -0700156 addCall(call);
157 }
158
159 @Override
160 public void onFailedIncomingCall(Call call) {
161 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800162 }
163
Ihab Awadcb387ac2014-05-28 16:49:38 -0700164 @Override
Sailesh Nepale20bc972014-07-09 21:22:36 -0700165 public void onCallCapabilitiesChanged(Call call) {
Santos Cordona1610702014-06-04 20:22:56 -0700166 for (CallsManagerListener listener : mListeners) {
Sailesh Nepale20bc972014-07-09 21:22:36 -0700167 listener.onCallCapabilitiesChanged(call);
Santos Cordona1610702014-06-04 20:22:56 -0700168 }
169 }
170
171 @Override
Ihab Awadcb387ac2014-05-28 16:49:38 -0700172 public void onRequestingRingback(Call call, boolean ringback) {
173 for (CallsManagerListener listener : mListeners) {
174 listener.onRequestingRingback(call, ringback);
175 }
176 }
177
Evan Charlton352105c2014-06-03 14:10:54 -0700178 @Override
179 public void onPostDialWait(Call call, String remaining) {
180 mInCallController.onPostDialWait(call, remaining);
181 }
182
Santos Cordona1610702014-06-04 20:22:56 -0700183 @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 Awadff7493a2014-06-10 13:47:44 -0700211 @Override
212 public void onCannedSmsResponsesLoaded(Call call) {
213 for (CallsManagerListener listener : mListeners) {
214 listener.onCannedSmsResponsesLoaded(call);
215 }
216 }
217
Nancy Chena65d41f2014-06-24 12:06:03 -0700218 @Override
219 public void onCallVideoProviderChanged(Call call) {
220 for (CallsManagerListener listener : mListeners) {
221 listener.onCallVideoProviderChanged(call);
222 }
223 }
224
Tyler Gunne19cc002014-07-01 11:32:53 -0700225 @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 Nepal7e669572014-07-08 21:29:12 -0700233 @Override
234 public void onAudioModeIsVoipChanged(Call call) {
235 for (CallsManagerListener listener : mListeners) {
236 listener.onAudioModeIsVoipChanged(call);
237 }
238 }
239
Sailesh Nepal35faf8c2014-07-08 22:02:34 -0700240 @Override
241 public void onStatusHintsChanged(Call call) {
242 for (CallsManagerListener listener : mListeners) {
243 listener.onStatusHintsChanged(call);
244 }
245 }
246
Sailesh Nepal810735e2014-03-18 18:15:46 -0700247 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700248 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700249 }
250
251 Call getForegroundCall() {
252 return mForegroundCall;
253 }
254
Santos Cordonae193062014-05-21 21:21:49 -0700255 Ringer getRinger() {
256 return mRinger;
257 }
258
Santos Cordonf3671a62014-05-29 21:51:53 -0700259 InCallController getInCallController() {
260 return mInCallController;
261 }
262
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700263 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700264 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700265 if (call.isEmergencyCall()) {
266 return true;
267 }
268 }
269 return false;
270 }
271
272 CallAudioState getAudioState() {
273 return mCallAudioManager.getAudioState();
274 }
275
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800276 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800277 * Starts the incoming call sequence by having switchboard gather more information about the
278 * specified call; using the specified call service descriptor. Upon success, execution returns
Santos Cordon766d04f2014-05-06 10:28:25 -0700279 * to {@link #onSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800280 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700281 * @param descriptor The descriptor of the connection service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800282 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800283 */
Evan Charltona05805b2014-03-05 08:21:46 -0800284 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800285 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800286 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700287 // additional information from the connection service, but for now we just need one to pass
Sailesh Nepale59bb192014-04-01 18:33:59 -0700288 // around.
Santos Cordona1610702014-06-04 20:22:56 -0700289 Call call = new Call(true /* isIncoming */, false /* isConference */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700290 // TODO(santoscordon): Move this to be a part of addCall()
291 call.addListener(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800292
Santos Cordon766d04f2014-05-06 10:28:25 -0700293 call.startIncoming(descriptor, extras);
Ben Gilada0d9f752014-02-26 11:49:03 -0800294 }
295
296 /**
Yorke Lee33501632014-03-17 19:24:12 -0700297 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800298 *
Yorke Lee33501632014-03-17 19:24:12 -0700299 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800300 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700301 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700302 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700303 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700304 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800305 */
Yorke Lee6f3f7af2014-07-11 10:59:46 -0700306 void placeOutgoingCall(Uri handle, GatewayInfo gatewayInfo, PhoneAccount account,
307 boolean speakerphoneOn, int videoState) {
Tyler Gunnc4abd912014-07-08 14:22:10 -0700308
Yorke Lee33501632014-03-17 19:24:12 -0700309 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
310
311 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700312 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700313 } else {
314 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
315 Log.pii(uriHandle), Log.pii(handle));
316 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700317
Santos Cordona1610702014-06-04 20:22:56 -0700318 Call call = new Call(
Ihab Awad98a55602014-06-30 21:27:28 -0700319 uriHandle, gatewayInfo, account,
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700320 false /* isIncoming */, false /* isConference */);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700321 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700322 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700323
324 // TODO(santoscordon): Move this to be a part of addCall()
325 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700326 addCall(call);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800327
Santos Cordon766d04f2014-05-06 10:28:25 -0700328 call.startOutgoing();
Yorke Leef98fb572014-03-05 10:56:55 -0800329 }
330
331 /**
Santos Cordona1610702014-06-04 20:22:56 -0700332 * Attempts to start a conference call for the specified call.
333 *
334 * @param call The call to conference with.
335 */
336 void conference(Call call) {
337 Call conferenceCall = new Call(false, true);
338 conferenceCall.addListener(this);
339 call.conferenceInto(conferenceCall);
340 }
341
342 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800343 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
344 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
345 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800346 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700347 void answerCall(Call call) {
348 if (!mCalls.contains(call)) {
349 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800350 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700351 // If the foreground call is not the ringing call and it is currently isActive() or
352 // DIALING, put it on hold before answering the call.
353 if (mForegroundCall != null && mForegroundCall != call &&
354 (mForegroundCall.isActive() ||
355 mForegroundCall.getState() == CallState.DIALING)) {
356 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
357 mForegroundCall, call);
358 mForegroundCall.hold();
359 // TODO(santoscordon): Wait until we get confirmation of the active call being
360 // on-hold before answering the new call.
361 // TODO(santoscordon): Import logic from CallManager.acceptCall()
362 }
363
Santos Cordona56f2762014-03-24 15:55:53 -0700364 for (CallsManagerListener listener : mListeners) {
365 listener.onIncomingCallAnswered(call);
366 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800367
Santos Cordon61d0f702014-02-19 02:52:23 -0800368 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700369 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800370 call.answer();
371 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800372 }
373
374 /**
375 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
376 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
377 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800378 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700379 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700380 if (!mCalls.contains(call)) {
381 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800382 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700383 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700384 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700385 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700386 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800387 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800388 }
389
390 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700391 * Instructs Telecomm to play the specified DTMF tone within the specified call.
392 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700393 * @param digit The DTMF digit to play.
394 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700395 void playDtmfTone(Call call, char digit) {
396 if (!mCalls.contains(call)) {
397 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700398 } else {
399 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700400 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700401 }
402 }
403
404 /**
405 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700406 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700407 void stopDtmfTone(Call call) {
408 if (!mCalls.contains(call)) {
409 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700410 } else {
411 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700412 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700413 }
414 }
415
416 /**
Evan Charlton352105c2014-06-03 14:10:54 -0700417 * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700418 */
Evan Charlton352105c2014-06-03 14:10:54 -0700419 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700420 if (!mCalls.contains(call)) {
421 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700422 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700423 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700424 }
425 }
426
427 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800428 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
429 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
430 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800431 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700432 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700433 Log.v(this, "disconnectCall %s", call);
434
Sailesh Nepale59bb192014-04-01 18:33:59 -0700435 if (!mCalls.contains(call)) {
436 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800437 } else {
438 call.disconnect();
439 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800440 }
Santos Cordon681663d2014-01-30 04:32:15 -0800441
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700442 /**
443 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
444 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
445 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700446 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700447 void holdCall(Call call) {
448 if (!mCalls.contains(call)) {
449 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700450 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700451 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700452 call.hold();
453 }
454 }
455
456 /**
457 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
458 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
459 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700460 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700461 void unholdCall(Call call) {
462 if (!mCalls.contains(call)) {
463 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700464 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700465 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700466 call.unhold();
467 }
468 }
469
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700470 /** Called by the in-call UI to change the mute state. */
471 void mute(boolean shouldMute) {
472 mCallAudioManager.mute(shouldMute);
473 }
474
475 /**
476 * Called by the in-call UI to change the audio route, for example to change from earpiece to
477 * speaker phone.
478 */
479 void setAudioRoute(int route) {
480 mCallAudioManager.setAudioRoute(route);
481 }
482
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700483 void phoneAccountClicked(Call call) {
484 if (!mCalls.contains(call)) {
485 Log.i(this, "phoneAccountClicked in a non-existent call %s", call);
486 } else {
487 call.phoneAccountClicked();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700488 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700489 }
490
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700491 /** Called when the audio state changes. */
492 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
493 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700494 for (CallsManagerListener listener : mListeners) {
495 listener.onAudioStateChanged(oldAudioState, newAudioState);
496 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700497 }
498
Sailesh Nepale59bb192014-04-01 18:33:59 -0700499 void markCallAsRinging(Call call) {
500 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800501 }
502
Sailesh Nepale59bb192014-04-01 18:33:59 -0700503 void markCallAsDialing(Call call) {
504 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800505 }
506
Sailesh Nepale59bb192014-04-01 18:33:59 -0700507 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700508 if (call.getConnectTimeMillis() == 0) {
509 call.setConnectTimeMillis(System.currentTimeMillis());
510 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700511 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700512
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700513 if (call.getStartWithSpeakerphoneOn()) {
514 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
515 }
Santos Cordon681663d2014-01-30 04:32:15 -0800516 }
517
Sailesh Nepale59bb192014-04-01 18:33:59 -0700518 void markCallAsOnHold(Call call) {
519 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700520 }
521
Santos Cordon049b7b62014-01-30 05:34:26 -0800522 /**
523 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
524 * live call, then also disconnect from the in-call controller.
525 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700526 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
527 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800528 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700529 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
530 call.setDisconnectCause(disconnectCause, disconnectMessage);
531 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700532 removeCall(call);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700533 }
534
Ben Gilada0d9f752014-02-26 11:49:03 -0800535 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700536 * Cleans up any calls currently associated with the specified connection service when the
Santos Cordon4b2c1192014-03-19 18:15:38 -0700537 * call-service binder disconnects unexpectedly.
538 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700539 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700540 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700541 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
542 Preconditions.checkNotNull(service);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700543 for (Call call : ImmutableList.copyOf(mCalls)) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700544 if (call.getConnectionService() == service) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700545 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700546 }
547 }
548 }
549
Santos Cordondeb8c892014-05-30 01:38:03 -0700550 boolean hasAnyCalls() {
551 return !mCalls.isEmpty();
552 }
553
Santos Cordonc7e85d42014-05-22 02:51:48 -0700554 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700555 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700556 }
557
558 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700559 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700560 }
561
Santos Cordondeb8c892014-05-30 01:38:03 -0700562 boolean onMediaButton(int type) {
563 if (hasAnyCalls()) {
564 if (HeadsetMediaButton.SHORT_PRESS == type) {
565 Call ringingCall = getFirstCallWithState(CallState.RINGING);
566 if (ringingCall == null) {
567 mCallAudioManager.toggleMute();
568 return true;
569 } else {
570 ringingCall.answer();
571 return true;
572 }
573 } else if (HeadsetMediaButton.LONG_PRESS == type) {
574 Log.d(this, "handleHeadsetHook: longpress -> hangup");
575 Call callToHangup = getFirstCallWithState(
576 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
577 if (callToHangup != null) {
578 callToHangup.disconnect();
579 return true;
580 }
581 }
582 }
583 return false;
584 }
585
586 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700587 * Checks to see if the specified call is the only high-level call and if so, enable the
588 * "Add-call" button. We allow you to add a second call but not a third or beyond.
589 *
590 * @param call The call to test for add-call.
591 * @return Whether the add-call feature should be enabled for the call.
592 */
593 protected boolean isAddCallCapable(Call call) {
594 if (call.getParentCall() != null) {
595 // Never true for child calls.
596 return false;
597 }
598
599 // Loop through all the other calls and there exists a top level (has no parent) call
600 // that is not the specified call, return false.
601 for (Call otherCall : mCalls) {
602 if (call != otherCall && otherCall.getParentCall() == null) {
603 return false;
604 }
605 }
606 return true;
607 }
608
609 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700610 * Returns the first call that it finds with the given states. The states are treated as having
611 * priority order so that any call with the first state will be returned before any call with
612 * states listed later in the parameter list.
613 */
Santos Cordon23baed32014-06-27 14:45:39 -0700614 Call getFirstCallWithState(CallState... states) {
Santos Cordondeb8c892014-05-30 01:38:03 -0700615 for (CallState currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700616 // check the foreground first
617 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
618 return mForegroundCall;
619 }
620
Santos Cordondeb8c892014-05-30 01:38:03 -0700621 for (Call call : mCalls) {
622 if (currentState == call.getState()) {
623 return call;
624 }
625 }
626 }
627 return null;
628 }
629
Santos Cordon4b2c1192014-03-19 18:15:38 -0700630 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800631 * Adds the specified call to the main list of live calls.
632 *
633 * @param call The call to add.
634 */
635 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700636 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700637
638 // TODO(santoscordon): Update mForegroundCall prior to invoking
639 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700640 for (CallsManagerListener listener : mListeners) {
641 listener.onCallAdded(call);
642 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700643 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800644 }
645
Sailesh Nepal810735e2014-03-18 18:15:46 -0700646 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700647 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700648
Santos Cordon766d04f2014-05-06 10:28:25 -0700649 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700650 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700651
652 boolean shouldNotify = false;
653 if (mCalls.contains(call)) {
654 mCalls.remove(call);
655 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700656 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800657
Sailesh Nepale59bb192014-04-01 18:33:59 -0700658 // Only broadcast changes for calls that are being tracked.
659 if (shouldNotify) {
660 for (CallsManagerListener listener : mListeners) {
661 listener.onCallRemoved(call);
662 }
663 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700664 }
665 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800666
Sailesh Nepal810735e2014-03-18 18:15:46 -0700667 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700668 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700669 *
670 * @param call The call.
671 * @param newState The new state of the call.
672 */
673 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700674 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700675 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700676 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700677 if (newState != oldState) {
678 // Unfortunately, in the telephony world the radio is king. So if the call notifies
679 // us that the call is in a particular state, we allow it even if it doesn't make
680 // sense (e.g., ACTIVE -> RINGING).
681 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
682 // into a well-defined state machine.
683 // TODO(santoscordon): Define expected state transitions here, and log when an
684 // unexpected transition occurs.
685 call.setState(newState);
686
687 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700688 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700689 for (CallsManagerListener listener : mListeners) {
690 listener.onCallStateChanged(call, oldState, newState);
691 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700692 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800693 }
694 }
695 }
696
697 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700698 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800699 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700700 private void updateForegroundCall() {
701 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700702 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700703 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700704 // of its state will be foreground by default and instead the connection service should
705 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -0700706 // the call should play audio and listen to microphone if it wants.
707
708 // Active calls have priority.
709 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700710 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800711 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700712 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700713
714 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700715 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700716 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800717 }
718 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800719
Sailesh Nepal810735e2014-03-18 18:15:46 -0700720 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700721 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700722 Call oldForegroundCall = mForegroundCall;
723 mForegroundCall = newForegroundCall;
724
Santos Cordona56f2762014-03-24 15:55:53 -0700725 for (CallsManagerListener listener : mListeners) {
726 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
727 }
Santos Cordon681663d2014-01-30 04:32:15 -0800728 }
729 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700730
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700731 private static boolean areDescriptorsEqual(
732 CallServiceDescriptor descriptor1,
733 CallServiceDescriptor descriptor2) {
734 if (descriptor1 == null) {
735 return descriptor2 == null;
736 }
737 return descriptor1.equals(descriptor2);
738 }
739
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700740 private static boolean areUriEqual(Uri handle1, Uri handle2) {
741 if (handle1 == null) {
742 return handle2 == null;
743 }
744 return handle1.equals(handle2);
745 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800746}