blob: d451162f6187cef856618aa82997fb22bbf209d1 [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;
Santos Cordon79ff2bc2014-03-27 15:31:27 -070026import android.telephony.DisconnectCause;
Santos Cordon8e8b8d22013-12-19 14:14:05 -080027
Santos Cordon681663d2014-01-30 04:32:15 -080028import com.google.common.base.Preconditions;
Sailesh Nepal810735e2014-03-18 18:15:46 -070029import com.google.common.collect.ImmutableCollection;
30import com.google.common.collect.ImmutableList;
Ben Gilad9f2bed32013-12-12 17:43:26 -080031
Santos Cordonf3671a62014-05-29 21:51:53 -070032import java.util.HashSet;
33import java.util.LinkedHashSet;
Santos Cordona56f2762014-03-24 15:55:53 -070034import java.util.Set;
Ben Gilad9f2bed32013-12-12 17:43:26 -080035
Ben Giladdd8c6082013-12-30 14:44:08 -080036/**
37 * Singleton.
38 *
39 * NOTE(gilad): by design most APIs are package private, use the relevant adapter/s to allow
40 * access from other packages specifically refraining from passing the CallsManager instance
41 * beyond the com.android.telecomm package boundary.
42 */
Santos Cordon64c7e962014-07-02 15:15:27 -070043public final class CallsManager extends Call.ListenerBase {
Ben Gilada0d9f752014-02-26 11:49:03 -080044
Santos Cordon74d420b2014-05-07 14:38:47 -070045 // TODO(santoscordon): Consider renaming this CallsManagerPlugin.
Sailesh Nepal810735e2014-03-18 18:15:46 -070046 interface CallsManagerListener {
47 void onCallAdded(Call call);
48 void onCallRemoved(Call call);
49 void onCallStateChanged(Call call, CallState oldState, CallState newState);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070050 void onConnectionServiceChanged(
Sailesh Nepal8c85dee2014-04-07 22:21:40 -070051 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070052 ConnectionServiceWrapper oldService,
53 ConnectionServiceWrapper newService);
Sailesh Nepal810735e2014-03-18 18:15:46 -070054 void onIncomingCallAnswered(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070055 void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage);
Sailesh Nepal810735e2014-03-18 18:15:46 -070056 void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070057 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState);
Ihab Awadcb387ac2014-05-28 16:49:38 -070058 void onRequestingRingback(Call call, boolean ringback);
Santos Cordona1610702014-06-04 20:22:56 -070059 void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable);
60 void onIsConferencedChanged(Call call);
Ihab Awadff7493a2014-06-10 13:47:44 -070061 void onCannedSmsResponsesLoaded(Call call);
Nancy Chena65d41f2014-06-24 12:06:03 -070062 void onCallVideoProviderChanged(Call call);
Tyler Gunne19cc002014-07-01 11:32:53 -070063 void onFeaturesChanged(Call call);
Sailesh Nepal7e669572014-07-08 21:29:12 -070064 void onAudioModeIsVoipChanged(Call call);
Sailesh Nepal810735e2014-03-18 18:15:46 -070065 }
66
Santos Cordon8e8b8d22013-12-19 14:14:05 -080067 private static final CallsManager INSTANCE = new CallsManager();
Ben Gilad9f2bed32013-12-12 17:43:26 -080068
Santos Cordon8e8b8d22013-12-19 14:14:05 -080069 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070070 * The main call repository. Keeps an instance of all live calls. New incoming and outgoing
71 * calls are added to the map and removed when the calls move to the disconnected state.
Santos Cordon681663d2014-01-30 04:32:15 -080072 */
Santos Cordonf3671a62014-05-29 21:51:53 -070073 private final Set<Call> mCalls = new LinkedHashSet<>();
Santos Cordon681663d2014-01-30 04:32:15 -080074
Santos Cordonf3671a62014-05-29 21:51:53 -070075 private final DtmfLocalTonePlayer mDtmfLocalTonePlayer = new DtmfLocalTonePlayer();
76 private final InCallController mInCallController = new InCallController();
77 private final CallAudioManager mCallAudioManager;
78 private final Ringer mRinger;
79 private final Set<CallsManagerListener> mListeners = new HashSet<>();
Santos Cordondeb8c892014-05-30 01:38:03 -070080 private final HeadsetMediaButton mHeadsetMediaButton;
Sailesh Nepal84fa5f82014-04-02 11:01:11 -070081
82 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -070083 * The call the user is currently interacting with. This is the call that should have audio
84 * focus and be visible in the in-call UI.
Santos Cordon4e9fffe2014-03-04 18:13:41 -080085 */
Sailesh Nepal810735e2014-03-18 18:15:46 -070086 private Call mForegroundCall;
Santos Cordon4e9fffe2014-03-04 18:13:41 -080087
Santos Cordon766d04f2014-05-06 10:28:25 -070088 /** Singleton accessor. */
89 static CallsManager getInstance() {
90 return INSTANCE;
91 }
Ben Gilad9f2bed32013-12-12 17:43:26 -080092
Santos Cordon8e8b8d22013-12-19 14:14:05 -080093 /**
Ben Gilad8bdaa462014-02-05 12:53:19 -080094 * Initializes the required Telecomm components.
Santos Cordon8e8b8d22013-12-19 14:14:05 -080095 */
96 private CallsManager() {
Santos Cordona0e5f1a2014-03-31 21:43:00 -070097 TelecommApp app = TelecommApp.getInstance();
Santos Cordona56f2762014-03-24 15:55:53 -070098
Santos Cordondeb8c892014-05-30 01:38:03 -070099 StatusBarNotifier statusBarNotifier = new StatusBarNotifier(app, this);
100 mCallAudioManager = new CallAudioManager(app, statusBarNotifier);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700101 InCallTonePlayer.Factory playerFactory = new InCallTonePlayer.Factory(mCallAudioManager);
Santos Cordonae193062014-05-21 21:21:49 -0700102 mRinger = new Ringer(mCallAudioManager, this, playerFactory, app);
Santos Cordondeb8c892014-05-30 01:38:03 -0700103 mHeadsetMediaButton = new HeadsetMediaButton(app, this);
Santos Cordonae193062014-05-21 21:21:49 -0700104
Santos Cordondeb8c892014-05-30 01:38:03 -0700105 mListeners.add(statusBarNotifier);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700106 mListeners.add(new CallLogManager(app));
Santos Cordona56f2762014-03-24 15:55:53 -0700107 mListeners.add(new PhoneStateBroadcaster());
Santos Cordonf3671a62014-05-29 21:51:53 -0700108 mListeners.add(mInCallController);
Santos Cordonae193062014-05-21 21:21:49 -0700109 mListeners.add(mRinger);
Santos Cordonc7b8eba2014-04-01 15:26:28 -0700110 mListeners.add(new RingbackPlayer(this, playerFactory));
111 mListeners.add(new InCallToneMonitor(playerFactory, this));
Santos Cordona56f2762014-03-24 15:55:53 -0700112 mListeners.add(mCallAudioManager);
Santos Cordona0e5f1a2014-03-31 21:43:00 -0700113 mListeners.add(app.getMissedCallNotifier());
Santos Cordon92a2d812014-04-30 15:19:01 -0700114 mListeners.add(mDtmfLocalTonePlayer);
Santos Cordon81289982014-06-03 16:03:08 -0700115 mListeners.add(mHeadsetMediaButton);
Ihab Awadff7493a2014-06-10 13:47:44 -0700116 mListeners.add(RespondViaSmsManager.getInstance());
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800117 }
118
Santos Cordon766d04f2014-05-06 10:28:25 -0700119 @Override
120 public void onSuccessfulOutgoingCall(Call call) {
121 Log.v(this, "onSuccessfulOutgoingCall, %s", call);
122 if (mCalls.contains(call)) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700123 // The call's ConnectionService has been updated.
Santos Cordon766d04f2014-05-06 10:28:25 -0700124 for (CallsManagerListener listener : mListeners) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700125 listener.onConnectionServiceChanged(call, null, call.getConnectionService());
Santos Cordon766d04f2014-05-06 10:28:25 -0700126 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700127 } else {
128 Log.wtf(this, "unexpected successful call notification: %s", call);
129 return;
Santos Cordon766d04f2014-05-06 10:28:25 -0700130 }
Santos Cordon6cb7ba92014-05-23 14:09:32 -0700131
132 markCallAsDialing(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700133 }
134
135 @Override
Sailesh Nepal5a73b032014-06-25 15:53:21 -0700136 public void onFailedOutgoingCall(Call call, int errorCode, String errorMsg) {
137 Log.v(this, "onFailedOutgoingCall, call: %s", call);
138 // TODO: Replace disconnect cause with more specific disconnect causes.
139 markCallAsDisconnected(call, errorCode, errorMsg);
140 }
141
142 @Override
143 public void onCancelledOutgoingCall(Call call) {
144 Log.v(this, "onCancelledOutgoingCall, call: %s", call);
145 setCallState(call, CallState.ABORTED);
146 removeCall(call);
Santos Cordon766d04f2014-05-06 10:28:25 -0700147 }
148
149 @Override
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700150 public void onSuccessfulIncomingCall(Call call) {
Santos Cordon766d04f2014-05-06 10:28:25 -0700151 Log.d(this, "onSuccessfulIncomingCall");
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700152 setCallState(call, CallState.RINGING);
Santos Cordon766d04f2014-05-06 10:28:25 -0700153 addCall(call);
154 }
155
156 @Override
157 public void onFailedIncomingCall(Call call) {
158 call.removeListener(this);
Ben Gilada0d9f752014-02-26 11:49:03 -0800159 }
160
Ihab Awadcb387ac2014-05-28 16:49:38 -0700161 @Override
Santos Cordona1610702014-06-04 20:22:56 -0700162 public void onIsConferenceCapableChanged(Call call, boolean isConferenceCapable) {
163 for (CallsManagerListener listener : mListeners) {
164 listener.onIsConferenceCapableChanged(call, isConferenceCapable);
165 }
166 }
167
168 @Override
Ihab Awadcb387ac2014-05-28 16:49:38 -0700169 public void onRequestingRingback(Call call, boolean ringback) {
170 for (CallsManagerListener listener : mListeners) {
171 listener.onRequestingRingback(call, ringback);
172 }
173 }
174
Evan Charlton352105c2014-06-03 14:10:54 -0700175 @Override
176 public void onPostDialWait(Call call, String remaining) {
177 mInCallController.onPostDialWait(call, remaining);
178 }
179
Santos Cordona1610702014-06-04 20:22:56 -0700180 @Override
181 public void onExpiredConferenceCall(Call call) {
182 call.removeListener(this);
183 }
184
185 @Override
186 public void onConfirmedConferenceCall(Call call) {
187 addCall(call);
188 Log.v(this, "confirming Conf call %s", call);
189 for (CallsManagerListener listener : mListeners) {
190 listener.onIsConferencedChanged(call);
191 }
192 }
193
194 @Override
195 public void onParentChanged(Call call) {
196 for (CallsManagerListener listener : mListeners) {
197 listener.onIsConferencedChanged(call);
198 }
199 }
200
201 @Override
202 public void onChildrenChanged(Call call) {
203 for (CallsManagerListener listener : mListeners) {
204 listener.onIsConferencedChanged(call);
205 }
206 }
207
Ihab Awadff7493a2014-06-10 13:47:44 -0700208 @Override
209 public void onCannedSmsResponsesLoaded(Call call) {
210 for (CallsManagerListener listener : mListeners) {
211 listener.onCannedSmsResponsesLoaded(call);
212 }
213 }
214
Nancy Chena65d41f2014-06-24 12:06:03 -0700215 @Override
216 public void onCallVideoProviderChanged(Call call) {
217 for (CallsManagerListener listener : mListeners) {
218 listener.onCallVideoProviderChanged(call);
219 }
220 }
221
Tyler Gunne19cc002014-07-01 11:32:53 -0700222 @Override
223 public void onFeaturesChanged(Call call) {
224 Log.v(this, "onFeaturesChanged: %d", call.getFeatures());
225 for (CallsManagerListener listener : mListeners) {
226 listener.onFeaturesChanged(call);
227 }
228 }
229
Sailesh Nepal7e669572014-07-08 21:29:12 -0700230 @Override
231 public void onAudioModeIsVoipChanged(Call call) {
232 for (CallsManagerListener listener : mListeners) {
233 listener.onAudioModeIsVoipChanged(call);
234 }
235 }
236
Sailesh Nepal810735e2014-03-18 18:15:46 -0700237 ImmutableCollection<Call> getCalls() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700238 return ImmutableList.copyOf(mCalls);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700239 }
240
241 Call getForegroundCall() {
242 return mForegroundCall;
243 }
244
Santos Cordonae193062014-05-21 21:21:49 -0700245 Ringer getRinger() {
246 return mRinger;
247 }
248
Santos Cordonf3671a62014-05-29 21:51:53 -0700249 InCallController getInCallController() {
250 return mInCallController;
251 }
252
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700253 boolean hasEmergencyCall() {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700254 for (Call call : mCalls) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700255 if (call.isEmergencyCall()) {
256 return true;
257 }
258 }
259 return false;
260 }
261
262 CallAudioState getAudioState() {
263 return mCallAudioManager.getAudioState();
264 }
265
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800266 /**
Santos Cordon493e8f22014-02-19 03:15:12 -0800267 * Starts the incoming call sequence by having switchboard gather more information about the
268 * specified call; using the specified call service descriptor. Upon success, execution returns
Santos Cordon766d04f2014-05-06 10:28:25 -0700269 * to {@link #onSuccessfulIncomingCall} to start the in-call UI.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800270 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700271 * @param descriptor The descriptor of the connection service to use for this incoming call.
Evan Charltona05805b2014-03-05 08:21:46 -0800272 * @param extras The optional extras Bundle passed with the intent used for the incoming call.
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800273 */
Evan Charltona05805b2014-03-05 08:21:46 -0800274 void processIncomingCallIntent(CallServiceDescriptor descriptor, Bundle extras) {
Sailesh Nepalf1c191d2014-03-07 18:17:39 -0800275 Log.d(this, "processIncomingCallIntent");
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800276 // Create a call with no handle. Eventually, switchboard will update the call with
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700277 // additional information from the connection service, but for now we just need one to pass
Sailesh Nepale59bb192014-04-01 18:33:59 -0700278 // around.
Santos Cordona1610702014-06-04 20:22:56 -0700279 Call call = new Call(true /* isIncoming */, false /* isConference */);
Santos Cordon766d04f2014-05-06 10:28:25 -0700280 // TODO(santoscordon): Move this to be a part of addCall()
281 call.addListener(this);
Santos Cordon80d9bdc2014-02-13 18:28:46 -0800282
Santos Cordon766d04f2014-05-06 10:28:25 -0700283 call.startIncoming(descriptor, extras);
Ben Gilada0d9f752014-02-26 11:49:03 -0800284 }
285
286 /**
Yorke Lee33501632014-03-17 19:24:12 -0700287 * Attempts to issue/connect the specified call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800288 *
Yorke Lee33501632014-03-17 19:24:12 -0700289 * @param handle Handle to connect the call with.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800290 * @param contactInfo Information about the entity being called.
Yorke Lee33501632014-03-17 19:24:12 -0700291 * @param gatewayInfo Optional gateway information that can be used to route the call to the
Santos Cordon5b7b9b32014-03-26 14:00:22 -0700292 * actual dialed handle via a gateway provider. May be null.
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700293 * @param speakerphoneOn Whether or not to turn the speakerphone on once the call connects.
Tyler Gunnc4abd912014-07-08 14:22:10 -0700294 * @param videoState The desired video state for the outgoing call.
Santos Cordon8e8b8d22013-12-19 14:14:05 -0800295 */
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700296 void placeOutgoingCall(Uri handle, ContactInfo contactInfo, GatewayInfo gatewayInfo,
Tyler Gunnc4abd912014-07-08 14:22:10 -0700297 PhoneAccount account, boolean speakerphoneOn, int videoState) {
298
Yorke Lee33501632014-03-17 19:24:12 -0700299 final Uri uriHandle = (gatewayInfo == null) ? handle : gatewayInfo.getGatewayHandle();
300
301 if (gatewayInfo == null) {
Santos Cordonb58f4532014-05-29 11:59:06 -0700302 Log.i(this, "Creating a new outgoing call with handle: %s", Log.piiHandle(uriHandle));
Yorke Lee33501632014-03-17 19:24:12 -0700303 } else {
304 Log.i(this, "Creating a new outgoing call with gateway handle: %s, original handle: %s",
305 Log.pii(uriHandle), Log.pii(handle));
306 }
Santos Cordon766d04f2014-05-06 10:28:25 -0700307
Santos Cordona1610702014-06-04 20:22:56 -0700308 Call call = new Call(
Ihab Awad98a55602014-06-30 21:27:28 -0700309 uriHandle, gatewayInfo, account,
Nancy Chen77d2d0e2014-06-24 12:06:03 -0700310 false /* isIncoming */, false /* isConference */);
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700311 call.setStartWithSpeakerphoneOn(speakerphoneOn);
Tyler Gunnc4abd912014-07-08 14:22:10 -0700312 call.setVideoState(videoState);
Santos Cordon766d04f2014-05-06 10:28:25 -0700313
314 // TODO(santoscordon): Move this to be a part of addCall()
315 call.addListener(this);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700316 addCall(call);
Santos Cordone3d76ab2014-01-28 17:25:20 -0800317
Santos Cordon766d04f2014-05-06 10:28:25 -0700318 call.startOutgoing();
Yorke Leef98fb572014-03-05 10:56:55 -0800319 }
320
321 /**
Santos Cordona1610702014-06-04 20:22:56 -0700322 * Attempts to start a conference call for the specified call.
323 *
324 * @param call The call to conference with.
325 */
326 void conference(Call call) {
327 Call conferenceCall = new Call(false, true);
328 conferenceCall.addListener(this);
329 call.conferenceInto(conferenceCall);
330 }
331
332 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800333 * Instructs Telecomm to answer the specified call. Intended to be invoked by the in-call
334 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
335 * the user opting to answer said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800336 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700337 void answerCall(Call call) {
338 if (!mCalls.contains(call)) {
339 Log.i(this, "Request to answer a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800340 } else {
Santos Cordon40f78c22014-04-07 02:11:42 -0700341 // If the foreground call is not the ringing call and it is currently isActive() or
342 // DIALING, put it on hold before answering the call.
343 if (mForegroundCall != null && mForegroundCall != call &&
344 (mForegroundCall.isActive() ||
345 mForegroundCall.getState() == CallState.DIALING)) {
346 Log.v(this, "Holding active/dialing call %s before answering incoming call %s.",
347 mForegroundCall, call);
348 mForegroundCall.hold();
349 // TODO(santoscordon): Wait until we get confirmation of the active call being
350 // on-hold before answering the new call.
351 // TODO(santoscordon): Import logic from CallManager.acceptCall()
352 }
353
Santos Cordona56f2762014-03-24 15:55:53 -0700354 for (CallsManagerListener listener : mListeners) {
355 listener.onIncomingCallAnswered(call);
356 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800357
Santos Cordon61d0f702014-02-19 02:52:23 -0800358 // We do not update the UI until we get confirmation of the answer() through
Sailesh Nepale59bb192014-04-01 18:33:59 -0700359 // {@link #markCallAsActive}.
Santos Cordon61d0f702014-02-19 02:52:23 -0800360 call.answer();
361 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800362 }
363
364 /**
365 * Instructs Telecomm to reject the specified call. Intended to be invoked by the in-call
366 * app through {@link InCallAdapter} after Telecomm notifies it of an incoming call followed by
367 * the user opting to reject said call.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800368 */
Ihab Awadff7493a2014-06-10 13:47:44 -0700369 void rejectCall(Call call, boolean rejectWithMessage, String textMessage) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700370 if (!mCalls.contains(call)) {
371 Log.i(this, "Request to reject a non-existent call %s", call);
Santos Cordon61d0f702014-02-19 02:52:23 -0800372 } else {
Santos Cordona56f2762014-03-24 15:55:53 -0700373 for (CallsManagerListener listener : mListeners) {
Ihab Awadff7493a2014-06-10 13:47:44 -0700374 listener.onIncomingCallRejected(call, rejectWithMessage, textMessage);
Santos Cordona56f2762014-03-24 15:55:53 -0700375 }
Ihab Awadff7493a2014-06-10 13:47:44 -0700376 call.reject(rejectWithMessage, textMessage);
Santos Cordon61d0f702014-02-19 02:52:23 -0800377 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800378 }
379
380 /**
Ihab Awad74549ec2014-03-10 15:33:25 -0700381 * Instructs Telecomm to play the specified DTMF tone within the specified call.
382 *
Ihab Awad74549ec2014-03-10 15:33:25 -0700383 * @param digit The DTMF digit to play.
384 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700385 void playDtmfTone(Call call, char digit) {
386 if (!mCalls.contains(call)) {
387 Log.i(this, "Request to play DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700388 } else {
389 call.playDtmfTone(digit);
Santos Cordon92a2d812014-04-30 15:19:01 -0700390 mDtmfLocalTonePlayer.playTone(call, digit);
Ihab Awad74549ec2014-03-10 15:33:25 -0700391 }
392 }
393
394 /**
395 * Instructs Telecomm to stop the currently playing DTMF tone, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700396 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700397 void stopDtmfTone(Call call) {
398 if (!mCalls.contains(call)) {
399 Log.i(this, "Request to stop DTMF in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700400 } else {
401 call.stopDtmfTone();
Santos Cordon92a2d812014-04-30 15:19:01 -0700402 mDtmfLocalTonePlayer.stopTone(call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700403 }
404 }
405
406 /**
Evan Charlton352105c2014-06-03 14:10:54 -0700407 * Instructs Telecomm to continue (or not) the current post-dial DTMF string, if any.
Ihab Awad74549ec2014-03-10 15:33:25 -0700408 */
Evan Charlton352105c2014-06-03 14:10:54 -0700409 void postDialContinue(Call call, boolean proceed) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700410 if (!mCalls.contains(call)) {
411 Log.i(this, "Request to continue post-dial string in a non-existent call %s", call);
Ihab Awad74549ec2014-03-10 15:33:25 -0700412 } else {
Evan Charlton352105c2014-06-03 14:10:54 -0700413 call.postDialContinue(proceed);
Ihab Awad74549ec2014-03-10 15:33:25 -0700414 }
415 }
416
417 /**
Santos Cordone3d76ab2014-01-28 17:25:20 -0800418 * Instructs Telecomm to disconnect the specified call. Intended to be invoked by the
419 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
420 * the user hitting the end-call button.
Santos Cordone3d76ab2014-01-28 17:25:20 -0800421 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700422 void disconnectCall(Call call) {
Santos Cordon682fe6b2014-05-20 08:56:39 -0700423 Log.v(this, "disconnectCall %s", call);
424
Sailesh Nepale59bb192014-04-01 18:33:59 -0700425 if (!mCalls.contains(call)) {
426 Log.w(this, "Unknown call (%s) asked to disconnect", call);
Santos Cordon049b7b62014-01-30 05:34:26 -0800427 } else {
428 call.disconnect();
429 }
Santos Cordone3d76ab2014-01-28 17:25:20 -0800430 }
Santos Cordon681663d2014-01-30 04:32:15 -0800431
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700432 /**
433 * Instructs Telecomm to put the specified call on hold. Intended to be invoked by the
434 * in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered by
435 * the user hitting the hold button during an active call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700436 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700437 void holdCall(Call call) {
438 if (!mCalls.contains(call)) {
439 Log.w(this, "Unknown call (%s) asked to be put on hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700440 } else {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700441 Log.d(this, "Putting call on hold: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700442 call.hold();
443 }
444 }
445
446 /**
447 * Instructs Telecomm to release the specified call from hold. Intended to be invoked by
448 * the in-call app through {@link InCallAdapter} for an ongoing call. This is usually triggered
449 * by the user hitting the hold button during a held call.
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700450 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700451 void unholdCall(Call call) {
452 if (!mCalls.contains(call)) {
453 Log.w(this, "Unknown call (%s) asked to be removed from hold", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700454 } else {
Santos Cordonc7e85d42014-05-22 02:51:48 -0700455 Log.d(this, "unholding call: (%s)", call);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700456 call.unhold();
457 }
458 }
459
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700460 /** Called by the in-call UI to change the mute state. */
461 void mute(boolean shouldMute) {
462 mCallAudioManager.mute(shouldMute);
463 }
464
465 /**
466 * Called by the in-call UI to change the audio route, for example to change from earpiece to
467 * speaker phone.
468 */
469 void setAudioRoute(int route) {
470 mCallAudioManager.setAudioRoute(route);
471 }
472
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700473 void phoneAccountClicked(Call call) {
474 if (!mCalls.contains(call)) {
475 Log.i(this, "phoneAccountClicked in a non-existent call %s", call);
476 } else {
477 call.phoneAccountClicked();
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700478 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700479 }
480
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700481 /** Called when the audio state changes. */
482 void onAudioStateChanged(CallAudioState oldAudioState, CallAudioState newAudioState) {
483 Log.v(this, "onAudioStateChanged, audioState: %s -> %s", oldAudioState, newAudioState);
Santos Cordona56f2762014-03-24 15:55:53 -0700484 for (CallsManagerListener listener : mListeners) {
485 listener.onAudioStateChanged(oldAudioState, newAudioState);
486 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700487 }
488
Sailesh Nepale59bb192014-04-01 18:33:59 -0700489 void markCallAsRinging(Call call) {
490 setCallState(call, CallState.RINGING);
Santos Cordon681663d2014-01-30 04:32:15 -0800491 }
492
Sailesh Nepale59bb192014-04-01 18:33:59 -0700493 void markCallAsDialing(Call call) {
494 setCallState(call, CallState.DIALING);
Santos Cordon681663d2014-01-30 04:32:15 -0800495 }
496
Sailesh Nepale59bb192014-04-01 18:33:59 -0700497 void markCallAsActive(Call call) {
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700498 if (call.getConnectTimeMillis() == 0) {
499 call.setConnectTimeMillis(System.currentTimeMillis());
500 }
Sailesh Nepale59bb192014-04-01 18:33:59 -0700501 setCallState(call, CallState.ACTIVE);
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700502
Sai Cheemalapatib7157e92014-06-11 17:51:55 -0700503 if (call.getStartWithSpeakerphoneOn()) {
504 setAudioRoute(CallAudioState.ROUTE_SPEAKER);
505 }
Santos Cordon681663d2014-01-30 04:32:15 -0800506 }
507
Sailesh Nepale59bb192014-04-01 18:33:59 -0700508 void markCallAsOnHold(Call call) {
509 setCallState(call, CallState.ON_HOLD);
Yorke Leecdf3ebd2014-03-12 18:31:41 -0700510 }
511
Santos Cordon049b7b62014-01-30 05:34:26 -0800512 /**
513 * Marks the specified call as DISCONNECTED and notifies the in-call app. If this was the last
514 * live call, then also disconnect from the in-call controller.
515 *
Santos Cordon79ff2bc2014-03-27 15:31:27 -0700516 * @param disconnectCause The disconnect reason, see {@link android.telephony.DisconnectCause}.
517 * @param disconnectMessage Optional call-service-provided message about the disconnect.
Santos Cordon049b7b62014-01-30 05:34:26 -0800518 */
Sailesh Nepale59bb192014-04-01 18:33:59 -0700519 void markCallAsDisconnected(Call call, int disconnectCause, String disconnectMessage) {
520 call.setDisconnectCause(disconnectCause, disconnectMessage);
521 setCallState(call, CallState.DISCONNECTED);
Sailesh Nepal77da19e2014-07-02 21:31:16 -0700522 removeCall(call);
Sailesh Nepal6ab6fb72014-04-01 20:03:19 -0700523 }
524
Ben Gilada0d9f752014-02-26 11:49:03 -0800525 /**
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700526 * Cleans up any calls currently associated with the specified connection service when the
Santos Cordon4b2c1192014-03-19 18:15:38 -0700527 * call-service binder disconnects unexpectedly.
528 *
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700529 * @param service The connection service that disconnected.
Santos Cordon4b2c1192014-03-19 18:15:38 -0700530 */
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700531 void handleConnectionServiceDeath(ConnectionServiceWrapper service) {
532 Preconditions.checkNotNull(service);
Sailesh Nepale59bb192014-04-01 18:33:59 -0700533 for (Call call : ImmutableList.copyOf(mCalls)) {
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700534 if (call.getConnectionService() == service) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700535 markCallAsDisconnected(call, DisconnectCause.ERROR_UNSPECIFIED, null);
Santos Cordon4b2c1192014-03-19 18:15:38 -0700536 }
537 }
538 }
539
Santos Cordondeb8c892014-05-30 01:38:03 -0700540 boolean hasAnyCalls() {
541 return !mCalls.isEmpty();
542 }
543
Santos Cordonc7e85d42014-05-22 02:51:48 -0700544 boolean hasActiveOrHoldingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700545 return getFirstCallWithState(CallState.ACTIVE, CallState.ON_HOLD) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700546 }
547
548 boolean hasRingingCall() {
Santos Cordon23baed32014-06-27 14:45:39 -0700549 return getFirstCallWithState(CallState.RINGING) != null;
Santos Cordonc7e85d42014-05-22 02:51:48 -0700550 }
551
Santos Cordondeb8c892014-05-30 01:38:03 -0700552 boolean onMediaButton(int type) {
553 if (hasAnyCalls()) {
554 if (HeadsetMediaButton.SHORT_PRESS == type) {
555 Call ringingCall = getFirstCallWithState(CallState.RINGING);
556 if (ringingCall == null) {
557 mCallAudioManager.toggleMute();
558 return true;
559 } else {
560 ringingCall.answer();
561 return true;
562 }
563 } else if (HeadsetMediaButton.LONG_PRESS == type) {
564 Log.d(this, "handleHeadsetHook: longpress -> hangup");
565 Call callToHangup = getFirstCallWithState(
566 CallState.RINGING, CallState.DIALING, CallState.ACTIVE, CallState.ON_HOLD);
567 if (callToHangup != null) {
568 callToHangup.disconnect();
569 return true;
570 }
571 }
572 }
573 return false;
574 }
575
576 /**
Santos Cordon10838c22014-06-11 17:36:04 -0700577 * Checks to see if the specified call is the only high-level call and if so, enable the
578 * "Add-call" button. We allow you to add a second call but not a third or beyond.
579 *
580 * @param call The call to test for add-call.
581 * @return Whether the add-call feature should be enabled for the call.
582 */
583 protected boolean isAddCallCapable(Call call) {
584 if (call.getParentCall() != null) {
585 // Never true for child calls.
586 return false;
587 }
588
589 // Loop through all the other calls and there exists a top level (has no parent) call
590 // that is not the specified call, return false.
591 for (Call otherCall : mCalls) {
592 if (call != otherCall && otherCall.getParentCall() == null) {
593 return false;
594 }
595 }
596 return true;
597 }
598
599 /**
Santos Cordondeb8c892014-05-30 01:38:03 -0700600 * Returns the first call that it finds with the given states. The states are treated as having
601 * priority order so that any call with the first state will be returned before any call with
602 * states listed later in the parameter list.
603 */
Santos Cordon23baed32014-06-27 14:45:39 -0700604 Call getFirstCallWithState(CallState... states) {
Santos Cordondeb8c892014-05-30 01:38:03 -0700605 for (CallState currentState : states) {
Santos Cordon23baed32014-06-27 14:45:39 -0700606 // check the foreground first
607 if (mForegroundCall != null && mForegroundCall.getState() == currentState) {
608 return mForegroundCall;
609 }
610
Santos Cordondeb8c892014-05-30 01:38:03 -0700611 for (Call call : mCalls) {
612 if (currentState == call.getState()) {
613 return call;
614 }
615 }
616 }
617 return null;
618 }
619
Santos Cordon4b2c1192014-03-19 18:15:38 -0700620 /**
Ben Gilada0d9f752014-02-26 11:49:03 -0800621 * Adds the specified call to the main list of live calls.
622 *
623 * @param call The call to add.
624 */
625 private void addCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700626 mCalls.add(call);
Santos Cordon40f78c22014-04-07 02:11:42 -0700627
628 // TODO(santoscordon): Update mForegroundCall prior to invoking
629 // onCallAdded for calls which immediately take the foreground (like the first call).
Santos Cordona56f2762014-03-24 15:55:53 -0700630 for (CallsManagerListener listener : mListeners) {
631 listener.onCallAdded(call);
632 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700633 updateForegroundCall();
Ben Gilada0d9f752014-02-26 11:49:03 -0800634 }
635
Sailesh Nepal810735e2014-03-18 18:15:46 -0700636 private void removeCall(Call call) {
Santos Cordonc499c1c2014-04-14 17:13:14 -0700637 Log.v(this, "removeCall(%s)", call);
Sailesh Nepal4857f472014-04-07 22:26:27 -0700638
Santos Cordon766d04f2014-05-06 10:28:25 -0700639 call.removeListener(this);
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700640 call.clearConnectionService();
Sailesh Nepale59bb192014-04-01 18:33:59 -0700641
642 boolean shouldNotify = false;
643 if (mCalls.contains(call)) {
644 mCalls.remove(call);
645 shouldNotify = true;
Santos Cordona56f2762014-03-24 15:55:53 -0700646 }
Ben Gilada0d9f752014-02-26 11:49:03 -0800647
Sailesh Nepale59bb192014-04-01 18:33:59 -0700648 // Only broadcast changes for calls that are being tracked.
649 if (shouldNotify) {
650 for (CallsManagerListener listener : mListeners) {
651 listener.onCallRemoved(call);
652 }
653 updateForegroundCall();
Sailesh Nepal810735e2014-03-18 18:15:46 -0700654 }
655 }
Evan Charlton5c670a92014-03-06 14:58:20 -0800656
Sailesh Nepal810735e2014-03-18 18:15:46 -0700657 /**
Santos Cordon1ae2b852014-03-19 03:03:10 -0700658 * Sets the specified state on the specified call.
Sailesh Nepal810735e2014-03-18 18:15:46 -0700659 *
660 * @param call The call.
661 * @param newState The new state of the call.
662 */
663 private void setCallState(Call call, CallState newState) {
Sailesh Nepale59bb192014-04-01 18:33:59 -0700664 Preconditions.checkNotNull(newState);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700665 CallState oldState = call.getState();
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700666 Log.i(this, "setCallState %s -> %s, call: %s", oldState, newState, call);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700667 if (newState != oldState) {
668 // Unfortunately, in the telephony world the radio is king. So if the call notifies
669 // us that the call is in a particular state, we allow it even if it doesn't make
670 // sense (e.g., ACTIVE -> RINGING).
671 // TODO(santoscordon): Consider putting a stop to the above and turning CallState
672 // into a well-defined state machine.
673 // TODO(santoscordon): Define expected state transitions here, and log when an
674 // unexpected transition occurs.
675 call.setState(newState);
676
677 // Only broadcast state change for calls that are being tracked.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700678 if (mCalls.contains(call)) {
Santos Cordona56f2762014-03-24 15:55:53 -0700679 for (CallsManagerListener listener : mListeners) {
680 listener.onCallStateChanged(call, oldState, newState);
681 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700682 updateForegroundCall();
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800683 }
684 }
685 }
686
687 /**
Sailesh Nepal810735e2014-03-18 18:15:46 -0700688 * Checks which call should be visible to the user and have audio focus.
Evan Charlton5c670a92014-03-06 14:58:20 -0800689 */
Sailesh Nepal810735e2014-03-18 18:15:46 -0700690 private void updateForegroundCall() {
691 Call newForegroundCall = null;
Sailesh Nepale59bb192014-04-01 18:33:59 -0700692 for (Call call : mCalls) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700693 // TODO(santoscordon): Foreground-ness needs to be explicitly set. No call, regardless
Sailesh Nepalc92c4362014-07-04 18:33:21 -0700694 // of its state will be foreground by default and instead the connection service should
695 // be notified when its calls enter and exit foreground state. Foreground will mean that
Santos Cordon40f78c22014-04-07 02:11:42 -0700696 // the call should play audio and listen to microphone if it wants.
697
698 // Active calls have priority.
699 if (call.isActive()) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700700 newForegroundCall = call;
Evan Charlton5c670a92014-03-06 14:58:20 -0800701 break;
Sailesh Nepal810735e2014-03-18 18:15:46 -0700702 }
Santos Cordon40f78c22014-04-07 02:11:42 -0700703
704 if (call.isAlive() || call.getState() == CallState.RINGING) {
Sailesh Nepal810735e2014-03-18 18:15:46 -0700705 newForegroundCall = call;
Santos Cordon40f78c22014-04-07 02:11:42 -0700706 // Don't break in case there's an active call that has priority.
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800707 }
708 }
Santos Cordon4e9fffe2014-03-04 18:13:41 -0800709
Sailesh Nepal810735e2014-03-18 18:15:46 -0700710 if (newForegroundCall != mForegroundCall) {
Santos Cordon40f78c22014-04-07 02:11:42 -0700711 Log.v(this, "Updating foreground call, %s -> %s.", mForegroundCall, newForegroundCall);
Sailesh Nepal810735e2014-03-18 18:15:46 -0700712 Call oldForegroundCall = mForegroundCall;
713 mForegroundCall = newForegroundCall;
714
Santos Cordona56f2762014-03-24 15:55:53 -0700715 for (CallsManagerListener listener : mListeners) {
716 listener.onForegroundCallChanged(oldForegroundCall, mForegroundCall);
717 }
Santos Cordon681663d2014-01-30 04:32:15 -0800718 }
719 }
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700720
Sailesh Nepal8c85dee2014-04-07 22:21:40 -0700721 private static boolean areDescriptorsEqual(
722 CallServiceDescriptor descriptor1,
723 CallServiceDescriptor descriptor2) {
724 if (descriptor1 == null) {
725 return descriptor2 == null;
726 }
727 return descriptor1.equals(descriptor2);
728 }
729
Sailesh Nepal84fa5f82014-04-02 11:01:11 -0700730 private static boolean areUriEqual(Uri handle1, Uri handle2) {
731 if (handle1 == null) {
732 return handle2 == null;
733 }
734 return handle1.equals(handle2);
735 }
Ben Gilad9f2bed32013-12-12 17:43:26 -0800736}