blob: 28e264a49598dcf59982a2ace6a53c1b38870b57 [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;
Sailesh Nepal810735e2014-03-18 18:15:46 -070022import android.telecomm.CallInfo;
Ben Giladc5b22692014-02-18 20:03:22 -080023import android.telecomm.CallServiceDescriptor;
Santos Cordon681663d2014-01-30 04:32:15 -080024import android.telecomm.CallState;
Yorke Lee33501632014-03-17 19:24:12 -070025import android.telecomm.GatewayInfo;
Ihab Awad98a55602014-06-30 21:27:28 -070026import android.telecomm.PhoneAccount;
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 Cordon23baed32014-06-27 14:45:39 -070033import java.util.ArrayList;
34import java.util.Arrays;
35import java.util.Collections;
Santos Cordonf3671a62014-05-29 21:51:53 -070036import java.util.HashSet;
37import java.util.LinkedHashSet;
Santos Cordona56f2762014-03-24 15:55:53 -070038import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080039
Ben Giladdd8c6082013-12-30 14:44:08 -080040/**
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 Cordon64c7e962014-07-02 15:15:27 -070047public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080048
Santos Cordon74d420b2014-05-07 14:38:47 -070049 // TODO(santoscordon): Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070050 interface CallsManagerListener {
51 void onCallAdded(Call call);
52 void onCallRemoved(Call call);
53 void onCallStateChanged(Call call, CallState oldState, CallState newState);
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070054 void onCallServiceChanged(
55 Call call,
56 CallServiceWrapper oldCallService,
57 CallServiceWrapper newCallService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070058 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070059 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070060 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070061 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Ihab Awadcb387ac2014-05-28 16:49:38 -070062 void onRequestingRingback(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070063 void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable);
64 void onIsConferencedChanged(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070065 void onCannedSmsResponsesLoaded(Call call);
Nancy Chena65d41f2014-06-24 12:06:03 -070066 void onCallVideoProviderChanged(Call call);
Tyler Gunne19cc002014-07-01 11:32:53 -070067 void onFeaturesChanged(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 Cordonf3671a62014-05-29 21:51:53 -070076 private final Set<Call> mCalls = new LinkedHashSet<>();
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)) {
126 // The call's CallService has been updated.
127 for (CallsManagerListener listener : mListeners) {
128 listener.onCallServiceChanged(call, null, call.getCallService());
129 }
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
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 Gilada0d9f752014-02-26 11:49:03 -0800162 }
163
Ihab Awadcb387ac2014-05-28 16:49:38 -0700164 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700165 public void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable) {
166 for (CallsManagerListener listener : mListeners) {
167 listener.onIsConferenceCapableChanged(call, isConferenceCapable);
168 }
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 Nepal810735e2014-03-18 18:15:46 -0700233 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700234 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700235 }
236
237 Call getForegroundCall() {
238 return mForegroundCall;
239 }
240
Santos Cordonae193062014-05-21 21:21:49 -0700241 Ringer getRinger() {
242 return mRinger;
243 }
244
Santos Cordonf3671a62014-05-29 21:51:53 -0700245 InCallController getInCallController() {
246 return mInCallController;
247 }
248
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700249 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700250 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700251 if (call.isEmergencyCall()) {
252 return true;
253 }
254 }
255 return false;
256 }
257
258 CallAudioState getAudioState() {
259 return mCallAudioManager.getAudioState();
260 }
261
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800262 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800263 * 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 Cordon766d04f2014-05-06 10:28:25 -0700265 * to {@link #onSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800266 *
Ben Giladc5b22692014-02-18 20:03:22 -0800267 * @param descriptor The descriptor of the call service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800268 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800269 */
Evan Charltona05805b2014-03-05 08:21:46 -0800270 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800271 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800272 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepale59bb192014-04-01 18:33:59 -0700273 // additional information from the call service, but for now we just need one to pass
274 // around.
Santos Cordona1610702014-06-04 20:22:56 -0700275 Call call = new Call(true /* isIncoming */, false /* isConference */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700276 // TODO(santoscordon): Move this to be a part of addCall()
277 call.addListener(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800278
Santos Cordon766d04f2014-05-06 10:28:25 -0700279 call.startIncoming(descriptor, extras);
Ben Gilada0d9f752014-02-26 11:49:03 -0800280 }
281
282 /**
Yorke Lee33501632014-03-17 19:24:12 -0700283 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800284 *
Yorke Lee33501632014-03-17 19:24:12 -0700285 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800286 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700287 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700288 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700289 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800290 */
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700291 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo,
Ihab Awad98a55602014-06-30 21:27:28 -0700292 PhoneAccount account, boolean speakerphoneOn) {
Yorke Lee33501632014-03-17 19:24:12 -0700293 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
294
295 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700296 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700297 } 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 Cordon766d04f2014-05-06 10:28:25 -0700301
Santos Cordona1610702014-06-04 20:22:56 -0700302 Call call = new Call(
Ihab Awad98a55602014-06-30 21:27:28 -0700303 uriHandle, gatewayInfo, account,
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700304 false /* isIncoming */, false /* isConference */);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700305 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Santos Cordon766d04f2014-05-06 10:28:25 -0700306
307 // TODO(santoscordon): Move this to be a part of addCall()
308 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700309 addCall(call);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800310
Santos Cordon766d04f2014-05-06 10:28:25 -0700311 call.startOutgoing();
Yorke Leef98fb572014-03-05 10:56:55 -0800312 }
313
314 /**
Santos Cordona1610702014-06-04 20:22:56 -0700315 * 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 Cordone3d76ab2014-01-28 17:25:20 -0800326 * 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 Cordone3d76ab2014-01-28 17:25:20 -0800329 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700330 void answerCall(Call call) {
331 if (!mCalls.contains(call)) {
332 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800333 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700334 // 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 Cordona56f2762014-03-24 15:55:53 -0700347 for (CallsManagerListener listener : mListeners) {
348 listener.onIncomingCallAnswered(call);
349 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800350
Santos Cordon61d0f702014-02-19 02:52:23 -0800351 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700352 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800353 call.answer();
354 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800355 }
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 Cordone3d76ab2014-01-28 17:25:20 -0800361 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700362 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700363 if (!mCalls.contains(call)) {
364 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800365 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700366 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700367 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700368 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700369 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800370 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800371 }
372
373 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700374 * Instructs Telecomm to play the specified DTMF tone within the specified call.
375 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700376 * @param digit The DTMF digit to play.
377 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700378 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 Awad74549ec2014-03-10 15:33:25 -0700381 } else {
382 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700383 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700384 }
385 }
386
387 /**
388 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700389 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700390 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 Awad74549ec2014-03-10 15:33:25 -0700393 } else {
394 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700395 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700396 }
397 }
398
399 /**
Evan Charlton352105c2014-06-03 14:10:54 -0700400 * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700401 */
Evan Charlton352105c2014-06-03 14:10:54 -0700402 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700403 if (!mCalls.contains(call)) {
404 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700405 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700406 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700407 }
408 }
409
410 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800411 * 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 Cordone3d76ab2014-01-28 17:25:20 -0800414 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700415 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700416 Log.v(this, "disconnectCall %s", call);
417
Sailesh Nepale59bb192014-04-01 18:33:59 -0700418 if (!mCalls.contains(call)) {
419 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800420 } else {
421 call.disconnect();
422 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800423 }
Santos Cordon681663d2014-01-30 04:32:15 -0800424
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700425 /**
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 Leecdf3ebd2014-03-12 18:31:41 -0700429 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700430 void holdCall(Call call) {
431 if (!mCalls.contains(call)) {
432 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700433 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700434 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700435 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 Leecdf3ebd2014-03-12 18:31:41 -0700443 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700444 void unholdCall(Call call) {
445 if (!mCalls.contains(call)) {
446 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700447 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700448 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700449 call.unhold();
450 }
451 }
452
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700453 /** 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 Nepal77da19e2014-07-02 21:31:16 -0700466 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 Nepal84fa5f82014-04-02 11:01:11 -0700471 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700472 }
473
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700474 /** Called when the audio state changes. */
475 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
476 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700477 for (CallsManagerListener listener : mListeners) {
478 listener.onAudioStateChanged(oldAudioState, newAudioState);
479 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700480 }
481
Sailesh Nepale59bb192014-04-01 18:33:59 -0700482 void markCallAsRinging(Call call) {
483 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800484 }
485
Sailesh Nepale59bb192014-04-01 18:33:59 -0700486 void markCallAsDialing(Call call) {
487 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800488 }
489
Sailesh Nepale59bb192014-04-01 18:33:59 -0700490 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700491 if (call.getConnectTimeMillis() == 0) {
492 call.setConnectTimeMillis(System.currentTimeMillis());
493 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700494 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700495
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700496 if (call.getStartWithSpeakerphoneOn()) {
497 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
498 }
Santos Cordon681663d2014-01-30 04:32:15 -0800499 }
500
Sailesh Nepale59bb192014-04-01 18:33:59 -0700501 void markCallAsOnHold(Call call) {
502 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700503 }
504
Santos Cordon049b7b62014-01-30 05:34:26 -0800505 /**
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 Cordon79ff2bc2014-03-27 15:31:27 -0700509 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
510 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800511 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700512 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
513 call.setDisconnectCause(disconnectCause, disconnectMessage);
514 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700515 removeCall(call);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700516 }
517
Ben Gilada0d9f752014-02-26 11:49:03 -0800518 /**
Santos Cordon4b2c1192014-03-19 18:15:38 -0700519 * 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 Nepale59bb192014-04-01 18:33:59 -0700526 for (Call call : ImmutableList.copyOf(mCalls)) {
Santos Cordon4b2c1192014-03-19 18:15:38 -0700527 if (call.getCallService() == callService) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700528 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700529 }
530 }
531 }
532
Santos Cordondeb8c892014-05-30 01:38:03 -0700533 boolean hasAnyCalls() {
534 return !mCalls.isEmpty();
535 }
536
Santos Cordonc7e85d42014-05-22 02:51:48 -0700537 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700538 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700539 }
540
541 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700542 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700543 }
544
Santos Cordondeb8c892014-05-30 01:38:03 -0700545 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 Cordon10838c22014-06-11 17:36:04 -0700570 * 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 Cordondeb8c892014-05-30 01:38:03 -0700593 * 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 Cordon23baed32014-06-27 14:45:39 -0700597 Call getFirstCallWithState(CallState... states) {
Santos Cordondeb8c892014-05-30 01:38:03 -0700598 for (CallState currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700599 // check the foreground first
600 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
601 return mForegroundCall;
602 }
603
Santos Cordondeb8c892014-05-30 01:38:03 -0700604 for (Call call : mCalls) {
605 if (currentState == call.getState()) {
606 return call;
607 }
608 }
609 }
610 return null;
611 }
612
Santos Cordon4b2c1192014-03-19 18:15:38 -0700613 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800614 * 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 Nepale59bb192014-04-01 18:33:59 -0700619 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700620
621 // TODO(santoscordon): Update mForegroundCall prior to invoking
622 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700623 for (CallsManagerListener listener : mListeners) {
624 listener.onCallAdded(call);
625 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700626 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800627 }
628
Sailesh Nepal810735e2014-03-18 18:15:46 -0700629 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700630 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700631
Santos Cordon766d04f2014-05-06 10:28:25 -0700632 call.removeListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700633 call.clearCallService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700634
635 boolean shouldNotify = false;
636 if (mCalls.contains(call)) {
637 mCalls.remove(call);
638 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700639 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800640
Sailesh Nepale59bb192014-04-01 18:33:59 -0700641 // 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 Nepal810735e2014-03-18 18:15:46 -0700647 }
648 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800649
Sailesh Nepal810735e2014-03-18 18:15:46 -0700650 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700651 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700652 *
653 * @param call The call.
654 * @param newState The new state of the call.
655 */
656 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700657 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700658 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700659 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700660 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 Nepale59bb192014-04-01 18:33:59 -0700671 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700672 for (CallsManagerListener listener : mListeners) {
673 listener.onCallStateChanged(call, oldState, newState);
674 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700675 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800676 }
677 }
678 }
679
680 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700681 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800682 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700683 private void updateForegroundCall() {
684 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700685 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700686 // 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 Nepal810735e2014-03-18 18:15:46 -0700693 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800694 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700695 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700696
697 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700698 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700699 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800700 }
701 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800702
Sailesh Nepal810735e2014-03-18 18:15:46 -0700703 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700704 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700705 Call oldForegroundCall = mForegroundCall;
706 mForegroundCall = newForegroundCall;
707
Santos Cordona56f2762014-03-24 15:55:53 -0700708 for (CallsManagerListener listener : mListeners) {
709 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
710 }
Santos Cordon681663d2014-01-30 04:32:15 -0800711 }
712 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700713
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700714 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 Nepal84fa5f82014-04-02 11:01:11 -0700723 private static boolean areUriEqual(Uri handle1, Uri handle2) {
724 if (handle1 == null) {
725 return handle2 == null;
726 }
727 return handle1.equals(handle2);
728 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800729}