blob: f758a9655e1f69416e7268eb101a8a6ac48530a0 [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -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
17package com.android.incallui;
18
19import android.content.Context;
Eric Erfanianccca3152017-02-22 16:32:36 -080020import android.os.Bundle;
21import android.support.v4.app.Fragment;
22import android.support.v4.os.UserManagerCompat;
23import android.telecom.CallAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -080024import com.android.contacts.common.compat.CallCompat;
25import com.android.dialer.common.Assert;
26import com.android.dialer.common.LogUtil;
Eric Erfanian8369df02017-05-03 10:27:13 -070027import com.android.dialer.logging.DialerImpression;
Eric Erfanianccca3152017-02-22 16:32:36 -080028import com.android.dialer.logging.Logger;
Eric Erfanianccca3152017-02-22 16:32:36 -080029import com.android.incallui.InCallCameraManager.Listener;
30import com.android.incallui.InCallPresenter.CanAddCallListener;
31import com.android.incallui.InCallPresenter.InCallDetailsListener;
32import com.android.incallui.InCallPresenter.InCallState;
33import com.android.incallui.InCallPresenter.InCallStateListener;
34import com.android.incallui.InCallPresenter.IncomingCallListener;
Eric Erfanian8369df02017-05-03 10:27:13 -070035import com.android.incallui.audiomode.AudioModeProvider;
36import com.android.incallui.audiomode.AudioModeProvider.AudioModeListener;
Eric Erfanianccca3152017-02-22 16:32:36 -080037import com.android.incallui.call.CallList;
38import com.android.incallui.call.DialerCall;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070039import com.android.incallui.call.DialerCall.CameraDirection;
Eric Erfanianccca3152017-02-22 16:32:36 -080040import com.android.incallui.call.TelecomAdapter;
Eric Erfanianccca3152017-02-22 16:32:36 -080041import com.android.incallui.incall.protocol.InCallButtonIds;
42import com.android.incallui.incall.protocol.InCallButtonUi;
43import com.android.incallui.incall.protocol.InCallButtonUiDelegate;
Eric Erfanian90508232017-03-24 09:31:16 -070044import com.android.incallui.videotech.utils.VideoUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080045
46/** Logic for call buttons. */
47public class CallButtonPresenter
48 implements InCallStateListener,
49 AudioModeListener,
50 IncomingCallListener,
51 InCallDetailsListener,
52 CanAddCallListener,
53 Listener,
54 InCallButtonUiDelegate {
55
56 private static final String KEY_AUTOMATICALLY_MUTED = "incall_key_automatically_muted";
57 private static final String KEY_PREVIOUS_MUTE_STATE = "incall_key_previous_mute_state";
58
59 private final Context mContext;
60 private InCallButtonUi mInCallButtonUi;
61 private DialerCall mCall;
62 private boolean mAutomaticallyMuted = false;
63 private boolean mPreviousMuteState = false;
64 private boolean isInCallButtonUiReady;
65
66 public CallButtonPresenter(Context context) {
67 mContext = context.getApplicationContext();
68 }
69
70 @Override
71 public void onInCallButtonUiReady(InCallButtonUi ui) {
72 Assert.checkState(!isInCallButtonUiReady);
73 mInCallButtonUi = ui;
74 AudioModeProvider.getInstance().addListener(this);
75
76 // register for call state changes last
77 final InCallPresenter inCallPresenter = InCallPresenter.getInstance();
78 inCallPresenter.addListener(this);
79 inCallPresenter.addIncomingCallListener(this);
80 inCallPresenter.addDetailsListener(this);
81 inCallPresenter.addCanAddCallListener(this);
82 inCallPresenter.getInCallCameraManager().addCameraSelectionListener(this);
83
84 // Update the buttons state immediately for the current call
85 onStateChange(InCallState.NO_CALLS, inCallPresenter.getInCallState(), CallList.getInstance());
86 isInCallButtonUiReady = true;
87 }
88
89 @Override
90 public void onInCallButtonUiUnready() {
91 Assert.checkState(isInCallButtonUiReady);
92 mInCallButtonUi = null;
93 InCallPresenter.getInstance().removeListener(this);
94 AudioModeProvider.getInstance().removeListener(this);
95 InCallPresenter.getInstance().removeIncomingCallListener(this);
96 InCallPresenter.getInstance().removeDetailsListener(this);
97 InCallPresenter.getInstance().getInCallCameraManager().removeCameraSelectionListener(this);
98 InCallPresenter.getInstance().removeCanAddCallListener(this);
99 isInCallButtonUiReady = false;
100 }
101
102 @Override
103 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
104 if (newState == InCallState.OUTGOING) {
105 mCall = callList.getOutgoingCall();
106 } else if (newState == InCallState.INCALL) {
107 mCall = callList.getActiveOrBackgroundCall();
108
109 // When connected to voice mail, automatically shows the dialpad.
110 // (On previous releases we showed it when in-call shows up, before waiting for
111 // OUTGOING. We may want to do that once we start showing "Voice mail" label on
112 // the dialpad too.)
113 if (oldState == InCallState.OUTGOING && mCall != null) {
114 if (CallerInfoUtils.isVoiceMailNumber(mContext, mCall) && getActivity() != null) {
115 getActivity().showDialpadFragment(true /* show */, true /* animate */);
116 }
117 }
118 } else if (newState == InCallState.INCOMING) {
119 if (getActivity() != null) {
120 getActivity().showDialpadFragment(false /* show */, true /* animate */);
121 }
122 mCall = callList.getIncomingCall();
123 } else {
124 mCall = null;
125 }
126 updateUi(newState, mCall);
127 }
128
129 /**
130 * Updates the user interface in response to a change in the details of a call. Currently handles
131 * changes to the call buttons in response to a change in the details for a call. This is
132 * important to ensure changes to the active call are reflected in the available buttons.
133 *
134 * @param call The active call.
135 * @param details The call details.
136 */
137 @Override
138 public void onDetailsChanged(DialerCall call, android.telecom.Call.Details details) {
139 // Only update if the changes are for the currently active call
140 if (mInCallButtonUi != null && call != null && call.equals(mCall)) {
141 updateButtonsState(call);
142 }
143 }
144
145 @Override
146 public void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call) {
147 onStateChange(oldState, newState, CallList.getInstance());
148 }
149
150 @Override
151 public void onCanAddCallChanged(boolean canAddCall) {
152 if (mInCallButtonUi != null && mCall != null) {
153 updateButtonsState(mCall);
154 }
155 }
156
157 @Override
158 public void onAudioStateChanged(CallAudioState audioState) {
159 if (mInCallButtonUi != null) {
160 mInCallButtonUi.setAudioState(audioState);
161 }
162 }
163
164 @Override
165 public CallAudioState getCurrentAudioState() {
166 return AudioModeProvider.getInstance().getAudioState();
167 }
168
169 @Override
170 public void setAudioRoute(int route) {
171 LogUtil.i(
172 "CallButtonPresenter.setAudioRoute",
173 "sending new audio route: " + CallAudioState.audioRouteToString(route));
174 TelecomAdapter.getInstance().setAudioRoute(route);
175 }
176
177 /** Function assumes that bluetooth is not supported. */
178 @Override
179 public void toggleSpeakerphone() {
180 // This function should not be called if bluetooth is available.
181 CallAudioState audioState = getCurrentAudioState();
182 if (0 != (CallAudioState.ROUTE_BLUETOOTH & audioState.getSupportedRouteMask())) {
183 // It's clear the UI is wrong, so update the supported mode once again.
184 LogUtil.e(
185 "CallButtonPresenter", "toggling speakerphone not allowed when bluetooth supported.");
186 mInCallButtonUi.setAudioState(audioState);
187 return;
188 }
189
190 int newRoute;
191 if (audioState.getRoute() == CallAudioState.ROUTE_SPEAKER) {
192 newRoute = CallAudioState.ROUTE_WIRED_OR_EARPIECE;
193 Logger.get(mContext)
194 .logCallImpression(
195 DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_WIRED_OR_EARPIECE,
196 mCall.getUniqueCallId(),
197 mCall.getTimeAddedMs());
198 } else {
199 newRoute = CallAudioState.ROUTE_SPEAKER;
200 Logger.get(mContext)
201 .logCallImpression(
202 DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_SPEAKERPHONE,
203 mCall.getUniqueCallId(),
204 mCall.getTimeAddedMs());
205 }
206
207 setAudioRoute(newRoute);
208 }
209
210 @Override
Eric Erfanian9a090c82017-03-16 19:22:24 -0700211 public void muteClicked(boolean checked, boolean clickedByUser) {
212 LogUtil.i(
213 "CallButtonPresenter", "turning on mute: %s, clicked by user: %s", checked, clickedByUser);
214 if (clickedByUser) {
215 Logger.get(mContext)
216 .logCallImpression(
217 checked
218 ? DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_MUTE
219 : DialerImpression.Type.IN_CALL_SCREEN_TURN_OFF_MUTE,
220 mCall.getUniqueCallId(),
221 mCall.getTimeAddedMs());
222 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800223 TelecomAdapter.getInstance().mute(checked);
224 }
225
226 @Override
227 public void holdClicked(boolean checked) {
228 if (mCall == null) {
229 return;
230 }
231 if (checked) {
232 LogUtil.i("CallButtonPresenter", "putting the call on hold: " + mCall);
233 mCall.hold();
234 } else {
235 LogUtil.i("CallButtonPresenter", "removing the call from hold: " + mCall);
236 mCall.unhold();
237 }
238 }
239
240 @Override
241 public void swapClicked() {
242 if (mCall == null) {
243 return;
244 }
245
246 LogUtil.i("CallButtonPresenter", "swapping the call: " + mCall);
247 TelecomAdapter.getInstance().swap(mCall.getId());
248 }
249
250 @Override
251 public void mergeClicked() {
252 TelecomAdapter.getInstance().merge(mCall.getId());
253 }
254
255 @Override
256 public void addCallClicked() {
257 // Automatically mute the current call
258 mAutomaticallyMuted = true;
259 mPreviousMuteState = AudioModeProvider.getInstance().getAudioState().isMuted();
260 // Simulate a click on the mute button
Eric Erfanian9a090c82017-03-16 19:22:24 -0700261 muteClicked(true /* checked */, false /* clickedByUser */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800262 TelecomAdapter.getInstance().addCall();
263 }
264
265 @Override
266 public void showDialpadClicked(boolean checked) {
267 LogUtil.v("CallButtonPresenter", "show dialpad " + String.valueOf(checked));
268 getActivity().showDialpadFragment(checked /* show */, true /* animate */);
269 }
270
271 @Override
272 public void changeToVideoClicked() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700273 LogUtil.enterBlock("CallButtonPresenter.changeToVideoClicked");
Eric Erfanian8369df02017-05-03 10:27:13 -0700274 Logger.get(mContext)
275 .logCallImpression(
276 DialerImpression.Type.VIDEO_CALL_UPGRADE_REQUESTED,
277 mCall.getUniqueCallId(),
278 mCall.getTimeAddedMs());
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700279 mCall.getVideoTech().upgradeToVideo();
Eric Erfanianccca3152017-02-22 16:32:36 -0800280 }
281
282 @Override
283 public void onEndCallClicked() {
284 LogUtil.i("CallButtonPresenter.onEndCallClicked", "call: " + mCall);
285 if (mCall != null) {
286 mCall.disconnect();
287 }
288 }
289
290 @Override
291 public void showAudioRouteSelector() {
292 mInCallButtonUi.showAudioRouteSelector();
293 }
294
295 /**
296 * Switches the camera between the front-facing and back-facing camera.
297 *
298 * @param useFrontFacingCamera True if we should switch to using the front-facing camera, or false
299 * if we should switch to using the back-facing camera.
300 */
301 @Override
302 public void switchCameraClicked(boolean useFrontFacingCamera) {
Eric Erfanian91ce7d22017-06-05 13:35:02 -0700303 updateCamera(useFrontFacingCamera);
Eric Erfanianccca3152017-02-22 16:32:36 -0800304 }
305
306 @Override
307 public void toggleCameraClicked() {
308 LogUtil.i("CallButtonPresenter.toggleCameraClicked", "");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700309 Logger.get(mContext)
310 .logCallImpression(
311 DialerImpression.Type.IN_CALL_SCREEN_SWAP_CAMERA,
312 mCall.getUniqueCallId(),
313 mCall.getTimeAddedMs());
Eric Erfanianccca3152017-02-22 16:32:36 -0800314 switchCameraClicked(
315 !InCallPresenter.getInstance().getInCallCameraManager().isUsingFrontFacingCamera());
316 }
317
318 /**
319 * Stop or start client's video transmission.
320 *
321 * @param pause True if pausing the local user's video, or false if starting the local user's
322 * video.
323 */
324 @Override
325 public void pauseVideoClicked(boolean pause) {
326 LogUtil.i("CallButtonPresenter.pauseVideoClicked", "%s", pause ? "pause" : "unpause");
Eric Erfanianccca3152017-02-22 16:32:36 -0800327
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700328 Logger.get(mContext)
329 .logCallImpression(
330 pause
331 ? DialerImpression.Type.IN_CALL_SCREEN_TURN_OFF_VIDEO
332 : DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_VIDEO,
333 mCall.getUniqueCallId(),
334 mCall.getTimeAddedMs());
335
Eric Erfanianccca3152017-02-22 16:32:36 -0800336 if (pause) {
Eric Erfanian91ce7d22017-06-05 13:35:02 -0700337 mCall.getVideoTech().setCamera(null);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700338 mCall.getVideoTech().stopTransmission();
Eric Erfanianccca3152017-02-22 16:32:36 -0800339 } else {
Eric Erfanian91ce7d22017-06-05 13:35:02 -0700340 updateCamera(
341 InCallPresenter.getInstance().getInCallCameraManager().isUsingFrontFacingCamera());
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700342 mCall.getVideoTech().resumeTransmission();
Eric Erfanianccca3152017-02-22 16:32:36 -0800343 }
344
345 mInCallButtonUi.setVideoPaused(pause);
346 mInCallButtonUi.enableButton(InCallButtonIds.BUTTON_PAUSE_VIDEO, false);
347 }
348
Eric Erfanian91ce7d22017-06-05 13:35:02 -0700349 private void updateCamera(boolean useFrontFacingCamera) {
350 InCallCameraManager cameraManager = InCallPresenter.getInstance().getInCallCameraManager();
351 cameraManager.setUseFrontFacingCamera(useFrontFacingCamera);
352
353 String cameraId = cameraManager.getActiveCameraId();
354 if (cameraId != null) {
355 final int cameraDir =
356 cameraManager.isUsingFrontFacingCamera()
357 ? CameraDirection.CAMERA_DIRECTION_FRONT_FACING
358 : CameraDirection.CAMERA_DIRECTION_BACK_FACING;
359 mCall.setCameraDir(cameraDir);
360 mCall.getVideoTech().setCamera(cameraId);
361 }
362 }
363
Eric Erfanianccca3152017-02-22 16:32:36 -0800364 private void updateUi(InCallState state, DialerCall call) {
365 LogUtil.v("CallButtonPresenter", "updating call UI for call: ", call);
366
367 if (mInCallButtonUi == null) {
368 return;
369 }
370
371 if (call != null) {
372 mInCallButtonUi.updateInCallButtonUiColors();
373 }
374
375 final boolean isEnabled =
376 state.isConnectingOrConnected() && !state.isIncoming() && call != null;
377 mInCallButtonUi.setEnabled(isEnabled);
378
379 if (call == null) {
380 return;
381 }
382
383 updateButtonsState(call);
384 }
385
386 /**
387 * Updates the buttons applicable for the UI.
388 *
389 * @param call The active call.
390 */
391 private void updateButtonsState(DialerCall call) {
392 LogUtil.v("CallButtonPresenter.updateButtonsState", "");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700393 final boolean isVideo = call.isVideoCall();
Eric Erfanianccca3152017-02-22 16:32:36 -0800394
395 // Common functionality (audio, hold, etc).
396 // Show either HOLD or SWAP, but not both. If neither HOLD or SWAP is available:
397 // (1) If the device normally can hold, show HOLD in a disabled state.
398 // (2) If the device doesn't have the concept of hold/swap, remove the button.
399 final boolean showSwap = call.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
400 final boolean showHold =
401 !showSwap
402 && call.can(android.telecom.Call.Details.CAPABILITY_SUPPORT_HOLD)
403 && call.can(android.telecom.Call.Details.CAPABILITY_HOLD);
404 final boolean isCallOnHold = call.getState() == DialerCall.State.ONHOLD;
405
406 final boolean showAddCall =
407 TelecomAdapter.getInstance().canAddCall() && UserManagerCompat.isUserUnlocked(mContext);
408 final boolean showMerge = call.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700409 final boolean showUpgradeToVideo = !isVideo && (hasVideoCallCapabilities(call));
Eric Erfanianccca3152017-02-22 16:32:36 -0800410 final boolean showDowngradeToAudio = isVideo && isDowngradeToAudioSupported(call);
411 final boolean showMute = call.can(android.telecom.Call.Details.CAPABILITY_MUTE);
412
413 final boolean hasCameraPermission =
414 isVideo && VideoUtils.hasCameraPermissionAndAllowedByUser(mContext);
415 // Disabling local video doesn't seem to work when dialing. See b/30256571.
416 final boolean showPauseVideo =
417 isVideo
418 && call.getState() != DialerCall.State.DIALING
419 && call.getState() != DialerCall.State.CONNECTING;
420
421 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_AUDIO, true);
422 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_SWAP, showSwap);
423 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_HOLD, showHold);
424 mInCallButtonUi.setHold(isCallOnHold);
425 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_MUTE, showMute);
426 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_ADD_CALL, true);
427 mInCallButtonUi.enableButton(InCallButtonIds.BUTTON_ADD_CALL, showAddCall);
428 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_UPGRADE_TO_VIDEO, showUpgradeToVideo);
429 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_DOWNGRADE_TO_AUDIO, showDowngradeToAudio);
430 mInCallButtonUi.showButton(
431 InCallButtonIds.BUTTON_SWITCH_CAMERA, isVideo && hasCameraPermission);
432 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_PAUSE_VIDEO, showPauseVideo);
433 if (isVideo) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700434 mInCallButtonUi.setVideoPaused(!call.getVideoTech().isTransmitting() || !hasCameraPermission);
Eric Erfanianccca3152017-02-22 16:32:36 -0800435 }
436 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_DIALPAD, true);
437 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_MERGE, showMerge);
438
439 mInCallButtonUi.updateButtonStates();
440 }
441
442 private boolean hasVideoCallCapabilities(DialerCall call) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700443 return call.getVideoTech().isAvailable(mContext);
Eric Erfanianccca3152017-02-22 16:32:36 -0800444 }
445
446 /**
447 * Determines if downgrading from a video call to an audio-only call is supported. In order to
448 * support downgrade to audio, the SDK version must be >= N and the call should NOT have the
449 * {@link android.telecom.Call.Details#CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO}.
450 *
451 * @param call The call.
452 * @return {@code true} if downgrading to an audio-only call from a video call is supported.
453 */
454 private boolean isDowngradeToAudioSupported(DialerCall call) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700455 // TODO(b/33676907): If there is an RCS video share session, return true here
Eric Erfanianccca3152017-02-22 16:32:36 -0800456 return !call.can(CallCompat.Details.CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO);
457 }
458
459 @Override
460 public void refreshMuteState() {
461 // Restore the previous mute state
462 if (mAutomaticallyMuted
463 && AudioModeProvider.getInstance().getAudioState().isMuted() != mPreviousMuteState) {
464 if (mInCallButtonUi == null) {
465 return;
466 }
Eric Erfanian9a090c82017-03-16 19:22:24 -0700467 muteClicked(mPreviousMuteState, false /* clickedByUser */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800468 }
469 mAutomaticallyMuted = false;
470 }
471
472 @Override
473 public void onSaveInstanceState(Bundle outState) {
474 outState.putBoolean(KEY_AUTOMATICALLY_MUTED, mAutomaticallyMuted);
475 outState.putBoolean(KEY_PREVIOUS_MUTE_STATE, mPreviousMuteState);
476 }
477
478 @Override
479 public void onRestoreInstanceState(Bundle savedInstanceState) {
480 mAutomaticallyMuted =
481 savedInstanceState.getBoolean(KEY_AUTOMATICALLY_MUTED, mAutomaticallyMuted);
482 mPreviousMuteState = savedInstanceState.getBoolean(KEY_PREVIOUS_MUTE_STATE, mPreviousMuteState);
483 }
484
485 @Override
486 public void onCameraPermissionGranted() {
487 if (mCall != null) {
488 updateButtonsState(mCall);
489 }
490 }
491
492 @Override
493 public void onActiveCameraSelectionChanged(boolean isUsingFrontFacingCamera) {
494 if (mInCallButtonUi == null) {
495 return;
496 }
497 mInCallButtonUi.setCameraSwitched(!isUsingFrontFacingCamera);
498 }
499
500 @Override
501 public Context getContext() {
502 return mContext;
503 }
504
505 private InCallActivity getActivity() {
506 if (mInCallButtonUi != null) {
507 Fragment fragment = mInCallButtonUi.getInCallButtonUiFragment();
508 if (fragment != null) {
509 return (InCallActivity) fragment.getActivity();
510 }
511 }
512 return null;
513 }
514}