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