blob: 6b316bef8dbb054b319ee1004ea92dc4fc5ffe2b [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;
Eric Erfanian2ca43182017-08-31 06:57:16 -070021import android.os.Trace;
Eric Erfanianccca3152017-02-22 16:32:36 -080022import android.support.v4.app.Fragment;
23import android.support.v4.os.UserManagerCompat;
24import android.telecom.CallAudioState;
twyen00623aa2017-10-10 12:15:08 -070025import android.telecom.PhoneAccountHandle;
Eric Erfanianccca3152017-02-22 16:32:36 -080026import com.android.contacts.common.compat.CallCompat;
27import com.android.dialer.common.Assert;
28import com.android.dialer.common.LogUtil;
twyen00623aa2017-10-10 12:15:08 -070029import com.android.dialer.common.concurrent.DialerExecutorComponent;
Eric Erfanian8369df02017-05-03 10:27:13 -070030import com.android.dialer.logging.DialerImpression;
twyenfc1f9cf2017-11-27 13:22:48 -080031import com.android.dialer.logging.DialerImpression.Type;
Eric Erfanianccca3152017-02-22 16:32:36 -080032import com.android.dialer.logging.Logger;
twyen00623aa2017-10-10 12:15:08 -070033import com.android.dialer.telecom.TelecomUtil;
Eric Erfanianccca3152017-02-22 16:32:36 -080034import com.android.incallui.InCallCameraManager.Listener;
35import com.android.incallui.InCallPresenter.CanAddCallListener;
36import com.android.incallui.InCallPresenter.InCallDetailsListener;
37import com.android.incallui.InCallPresenter.InCallState;
38import com.android.incallui.InCallPresenter.InCallStateListener;
39import com.android.incallui.InCallPresenter.IncomingCallListener;
Eric Erfanian8369df02017-05-03 10:27:13 -070040import com.android.incallui.audiomode.AudioModeProvider;
41import com.android.incallui.audiomode.AudioModeProvider.AudioModeListener;
Eric Erfanianccca3152017-02-22 16:32:36 -080042import com.android.incallui.call.CallList;
43import com.android.incallui.call.DialerCall;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070044import com.android.incallui.call.DialerCall.CameraDirection;
Eric Erfanianccca3152017-02-22 16:32:36 -080045import com.android.incallui.call.TelecomAdapter;
Eric Erfanianccca3152017-02-22 16:32:36 -080046import com.android.incallui.incall.protocol.InCallButtonIds;
47import com.android.incallui.incall.protocol.InCallButtonUi;
48import com.android.incallui.incall.protocol.InCallButtonUiDelegate;
twyen00623aa2017-10-10 12:15:08 -070049import com.android.incallui.multisim.SwapSimWorker;
Eric Erfanian90508232017-03-24 09:31:16 -070050import com.android.incallui.videotech.utils.VideoUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080051
52/** Logic for call buttons. */
53public class CallButtonPresenter
54 implements InCallStateListener,
55 AudioModeListener,
56 IncomingCallListener,
57 InCallDetailsListener,
58 CanAddCallListener,
59 Listener,
60 InCallButtonUiDelegate {
61
62 private static final String KEY_AUTOMATICALLY_MUTED = "incall_key_automatically_muted";
63 private static final String KEY_PREVIOUS_MUTE_STATE = "incall_key_previous_mute_state";
64
65 private final Context mContext;
66 private InCallButtonUi mInCallButtonUi;
67 private DialerCall mCall;
68 private boolean mAutomaticallyMuted = false;
69 private boolean mPreviousMuteState = false;
70 private boolean isInCallButtonUiReady;
twyen00623aa2017-10-10 12:15:08 -070071 private PhoneAccountHandle mOtherAccount;
Eric Erfanianccca3152017-02-22 16:32:36 -080072
73 public CallButtonPresenter(Context context) {
74 mContext = context.getApplicationContext();
75 }
76
77 @Override
78 public void onInCallButtonUiReady(InCallButtonUi ui) {
79 Assert.checkState(!isInCallButtonUiReady);
80 mInCallButtonUi = ui;
81 AudioModeProvider.getInstance().addListener(this);
82
83 // register for call state changes last
84 final InCallPresenter inCallPresenter = InCallPresenter.getInstance();
85 inCallPresenter.addListener(this);
86 inCallPresenter.addIncomingCallListener(this);
87 inCallPresenter.addDetailsListener(this);
88 inCallPresenter.addCanAddCallListener(this);
89 inCallPresenter.getInCallCameraManager().addCameraSelectionListener(this);
90
91 // Update the buttons state immediately for the current call
92 onStateChange(InCallState.NO_CALLS, inCallPresenter.getInCallState(), CallList.getInstance());
93 isInCallButtonUiReady = true;
94 }
95
96 @Override
97 public void onInCallButtonUiUnready() {
98 Assert.checkState(isInCallButtonUiReady);
99 mInCallButtonUi = null;
100 InCallPresenter.getInstance().removeListener(this);
101 AudioModeProvider.getInstance().removeListener(this);
102 InCallPresenter.getInstance().removeIncomingCallListener(this);
103 InCallPresenter.getInstance().removeDetailsListener(this);
104 InCallPresenter.getInstance().getInCallCameraManager().removeCameraSelectionListener(this);
105 InCallPresenter.getInstance().removeCanAddCallListener(this);
106 isInCallButtonUiReady = false;
107 }
108
109 @Override
110 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700111 Trace.beginSection("CallButtonPresenter.onStateChange");
Eric Erfanianccca3152017-02-22 16:32:36 -0800112 if (newState == InCallState.OUTGOING) {
113 mCall = callList.getOutgoingCall();
114 } else if (newState == InCallState.INCALL) {
115 mCall = callList.getActiveOrBackgroundCall();
116
117 // When connected to voice mail, automatically shows the dialpad.
118 // (On previous releases we showed it when in-call shows up, before waiting for
119 // OUTGOING. We may want to do that once we start showing "Voice mail" label on
120 // the dialpad too.)
121 if (oldState == InCallState.OUTGOING && mCall != null) {
wangqi9982f0d2017-10-11 17:46:07 -0700122 if (mCall.isVoiceMailNumber() && getActivity() != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800123 getActivity().showDialpadFragment(true /* show */, true /* animate */);
124 }
125 }
126 } else if (newState == InCallState.INCOMING) {
127 if (getActivity() != null) {
128 getActivity().showDialpadFragment(false /* show */, true /* animate */);
129 }
130 mCall = callList.getIncomingCall();
131 } else {
132 mCall = null;
133 }
134 updateUi(newState, mCall);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700135 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800136 }
137
138 /**
139 * Updates the user interface in response to a change in the details of a call. Currently handles
140 * changes to the call buttons in response to a change in the details for a call. This is
141 * important to ensure changes to the active call are reflected in the available buttons.
142 *
143 * @param call The active call.
144 * @param details The call details.
145 */
146 @Override
147 public void onDetailsChanged(DialerCall call, android.telecom.Call.Details details) {
148 // Only update if the changes are for the currently active call
149 if (mInCallButtonUi != null && call != null && call.equals(mCall)) {
150 updateButtonsState(call);
151 }
152 }
153
154 @Override
155 public void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call) {
156 onStateChange(oldState, newState, CallList.getInstance());
157 }
158
159 @Override
160 public void onCanAddCallChanged(boolean canAddCall) {
161 if (mInCallButtonUi != null && mCall != null) {
162 updateButtonsState(mCall);
163 }
164 }
165
166 @Override
167 public void onAudioStateChanged(CallAudioState audioState) {
168 if (mInCallButtonUi != null) {
169 mInCallButtonUi.setAudioState(audioState);
170 }
171 }
172
173 @Override
174 public CallAudioState getCurrentAudioState() {
175 return AudioModeProvider.getInstance().getAudioState();
176 }
177
178 @Override
179 public void setAudioRoute(int route) {
180 LogUtil.i(
181 "CallButtonPresenter.setAudioRoute",
182 "sending new audio route: " + CallAudioState.audioRouteToString(route));
183 TelecomAdapter.getInstance().setAudioRoute(route);
184 }
185
186 /** Function assumes that bluetooth is not supported. */
187 @Override
188 public void toggleSpeakerphone() {
189 // This function should not be called if bluetooth is available.
190 CallAudioState audioState = getCurrentAudioState();
191 if (0 != (CallAudioState.ROUTE_BLUETOOTH & audioState.getSupportedRouteMask())) {
192 // It's clear the UI is wrong, so update the supported mode once again.
193 LogUtil.e(
194 "CallButtonPresenter", "toggling speakerphone not allowed when bluetooth supported.");
195 mInCallButtonUi.setAudioState(audioState);
196 return;
197 }
198
199 int newRoute;
200 if (audioState.getRoute() == CallAudioState.ROUTE_SPEAKER) {
201 newRoute = CallAudioState.ROUTE_WIRED_OR_EARPIECE;
202 Logger.get(mContext)
203 .logCallImpression(
204 DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_WIRED_OR_EARPIECE,
205 mCall.getUniqueCallId(),
206 mCall.getTimeAddedMs());
207 } else {
208 newRoute = CallAudioState.ROUTE_SPEAKER;
209 Logger.get(mContext)
210 .logCallImpression(
211 DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_SPEAKERPHONE,
212 mCall.getUniqueCallId(),
213 mCall.getTimeAddedMs());
214 }
215
216 setAudioRoute(newRoute);
217 }
218
219 @Override
Eric Erfanian9a090c82017-03-16 19:22:24 -0700220 public void muteClicked(boolean checked, boolean clickedByUser) {
221 LogUtil.i(
222 "CallButtonPresenter", "turning on mute: %s, clicked by user: %s", checked, clickedByUser);
223 if (clickedByUser) {
224 Logger.get(mContext)
225 .logCallImpression(
226 checked
227 ? DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_MUTE
228 : DialerImpression.Type.IN_CALL_SCREEN_TURN_OFF_MUTE,
229 mCall.getUniqueCallId(),
230 mCall.getTimeAddedMs());
231 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800232 TelecomAdapter.getInstance().mute(checked);
233 }
234
235 @Override
236 public void holdClicked(boolean checked) {
237 if (mCall == null) {
238 return;
239 }
240 if (checked) {
241 LogUtil.i("CallButtonPresenter", "putting the call on hold: " + mCall);
242 mCall.hold();
243 } else {
244 LogUtil.i("CallButtonPresenter", "removing the call from hold: " + mCall);
245 mCall.unhold();
246 }
247 }
248
249 @Override
250 public void swapClicked() {
251 if (mCall == null) {
252 return;
253 }
254
255 LogUtil.i("CallButtonPresenter", "swapping the call: " + mCall);
256 TelecomAdapter.getInstance().swap(mCall.getId());
257 }
258
259 @Override
260 public void mergeClicked() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700261 Logger.get(mContext)
262 .logCallImpression(
263 DialerImpression.Type.IN_CALL_MERGE_BUTTON_PRESSED,
264 mCall.getUniqueCallId(),
265 mCall.getTimeAddedMs());
Eric Erfanianccca3152017-02-22 16:32:36 -0800266 TelecomAdapter.getInstance().merge(mCall.getId());
267 }
268
269 @Override
270 public void addCallClicked() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700271 Logger.get(mContext)
272 .logCallImpression(
273 DialerImpression.Type.IN_CALL_ADD_CALL_BUTTON_PRESSED,
274 mCall.getUniqueCallId(),
275 mCall.getTimeAddedMs());
Eric Erfanianccca3152017-02-22 16:32:36 -0800276 // Automatically mute the current call
277 mAutomaticallyMuted = true;
278 mPreviousMuteState = AudioModeProvider.getInstance().getAudioState().isMuted();
279 // Simulate a click on the mute button
Eric Erfanian9a090c82017-03-16 19:22:24 -0700280 muteClicked(true /* checked */, false /* clickedByUser */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800281 TelecomAdapter.getInstance().addCall();
282 }
283
284 @Override
285 public void showDialpadClicked(boolean checked) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700286 Logger.get(mContext)
287 .logCallImpression(
288 DialerImpression.Type.IN_CALL_SHOW_DIALPAD_BUTTON_PRESSED,
289 mCall.getUniqueCallId(),
290 mCall.getTimeAddedMs());
Eric Erfanianccca3152017-02-22 16:32:36 -0800291 LogUtil.v("CallButtonPresenter", "show dialpad " + String.valueOf(checked));
292 getActivity().showDialpadFragment(checked /* show */, true /* animate */);
293 }
294
295 @Override
296 public void changeToVideoClicked() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700297 LogUtil.enterBlock("CallButtonPresenter.changeToVideoClicked");
Eric Erfanian8369df02017-05-03 10:27:13 -0700298 Logger.get(mContext)
299 .logCallImpression(
300 DialerImpression.Type.VIDEO_CALL_UPGRADE_REQUESTED,
301 mCall.getUniqueCallId(),
302 mCall.getTimeAddedMs());
twyen59209802017-09-13 10:37:01 -0700303 mCall.getVideoTech().upgradeToVideo(mContext);
Eric Erfanianccca3152017-02-22 16:32:36 -0800304 }
305
306 @Override
307 public void onEndCallClicked() {
308 LogUtil.i("CallButtonPresenter.onEndCallClicked", "call: " + mCall);
309 if (mCall != null) {
310 mCall.disconnect();
311 }
312 }
313
314 @Override
315 public void showAudioRouteSelector() {
316 mInCallButtonUi.showAudioRouteSelector();
317 }
318
twyen00623aa2017-10-10 12:15:08 -0700319 @Override
320 public void swapSimClicked() {
321 LogUtil.enterBlock("CallButtonPresenter.swapSimClicked");
twyenfc1f9cf2017-11-27 13:22:48 -0800322 Logger.get(getContext()).logImpression(Type.DUAL_SIM_CHANGE_SIM_PRESSED);
twyen00623aa2017-10-10 12:15:08 -0700323 SwapSimWorker worker =
324 new SwapSimWorker(
325 getContext(),
326 mCall,
327 InCallPresenter.getInstance().getCallList(),
328 mOtherAccount,
329 InCallPresenter.getInstance().acquireInCallUiLock("swapSim"));
330 DialerExecutorComponent.get(getContext())
331 .dialerExecutorFactory()
332 .createNonUiTaskBuilder(worker)
333 .build()
334 .executeParallel(null);
335 }
336
Eric Erfanianccca3152017-02-22 16:32:36 -0800337 /**
338 * Switches the camera between the front-facing and back-facing camera.
339 *
340 * @param useFrontFacingCamera True if we should switch to using the front-facing camera, or false
341 * if we should switch to using the back-facing camera.
342 */
343 @Override
344 public void switchCameraClicked(boolean useFrontFacingCamera) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700345 updateCamera(useFrontFacingCamera);
Eric Erfanianccca3152017-02-22 16:32:36 -0800346 }
347
348 @Override
349 public void toggleCameraClicked() {
350 LogUtil.i("CallButtonPresenter.toggleCameraClicked", "");
Eric Erfanian2ca43182017-08-31 06:57:16 -0700351 if (mCall == null) {
352 return;
353 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700354 Logger.get(mContext)
355 .logCallImpression(
356 DialerImpression.Type.IN_CALL_SCREEN_SWAP_CAMERA,
357 mCall.getUniqueCallId(),
358 mCall.getTimeAddedMs());
Eric Erfanianccca3152017-02-22 16:32:36 -0800359 switchCameraClicked(
360 !InCallPresenter.getInstance().getInCallCameraManager().isUsingFrontFacingCamera());
361 }
362
363 /**
364 * Stop or start client's video transmission.
365 *
366 * @param pause True if pausing the local user's video, or false if starting the local user's
367 * video.
368 */
369 @Override
370 public void pauseVideoClicked(boolean pause) {
371 LogUtil.i("CallButtonPresenter.pauseVideoClicked", "%s", pause ? "pause" : "unpause");
Eric Erfanianccca3152017-02-22 16:32:36 -0800372
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700373 Logger.get(mContext)
374 .logCallImpression(
375 pause
376 ? DialerImpression.Type.IN_CALL_SCREEN_TURN_OFF_VIDEO
377 : DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_VIDEO,
378 mCall.getUniqueCallId(),
379 mCall.getTimeAddedMs());
380
Eric Erfanianccca3152017-02-22 16:32:36 -0800381 if (pause) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700382 mCall.getVideoTech().setCamera(null);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700383 mCall.getVideoTech().stopTransmission();
Eric Erfanianccca3152017-02-22 16:32:36 -0800384 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700385 updateCamera(
386 InCallPresenter.getInstance().getInCallCameraManager().isUsingFrontFacingCamera());
twyen59209802017-09-13 10:37:01 -0700387 mCall.getVideoTech().resumeTransmission(mContext);
Eric Erfanianccca3152017-02-22 16:32:36 -0800388 }
389
390 mInCallButtonUi.setVideoPaused(pause);
391 mInCallButtonUi.enableButton(InCallButtonIds.BUTTON_PAUSE_VIDEO, false);
392 }
393
Eric Erfanian2ca43182017-08-31 06:57:16 -0700394 private void updateCamera(boolean useFrontFacingCamera) {
395 InCallCameraManager cameraManager = InCallPresenter.getInstance().getInCallCameraManager();
396 cameraManager.setUseFrontFacingCamera(useFrontFacingCamera);
397
398 String cameraId = cameraManager.getActiveCameraId();
399 if (cameraId != null) {
400 final int cameraDir =
401 cameraManager.isUsingFrontFacingCamera()
402 ? CameraDirection.CAMERA_DIRECTION_FRONT_FACING
403 : CameraDirection.CAMERA_DIRECTION_BACK_FACING;
404 mCall.setCameraDir(cameraDir);
405 mCall.getVideoTech().setCamera(cameraId);
406 }
407 }
408
Eric Erfanianccca3152017-02-22 16:32:36 -0800409 private void updateUi(InCallState state, DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700410 LogUtil.v("CallButtonPresenter", "updating call UI for call: %s", call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800411
412 if (mInCallButtonUi == null) {
413 return;
414 }
415
416 if (call != null) {
wangqi8d662ca2017-10-26 11:27:19 -0700417 mInCallButtonUi.updateInCallButtonUiColors(
418 InCallPresenter.getInstance().getThemeColorManager().getSecondaryColor());
Eric Erfanianccca3152017-02-22 16:32:36 -0800419 }
420
421 final boolean isEnabled =
422 state.isConnectingOrConnected() && !state.isIncoming() && call != null;
423 mInCallButtonUi.setEnabled(isEnabled);
424
425 if (call == null) {
426 return;
427 }
428
429 updateButtonsState(call);
430 }
431
432 /**
433 * Updates the buttons applicable for the UI.
434 *
435 * @param call The active call.
436 */
twyen00623aa2017-10-10 12:15:08 -0700437 @SuppressWarnings("MissingPermission")
Eric Erfanianccca3152017-02-22 16:32:36 -0800438 private void updateButtonsState(DialerCall call) {
439 LogUtil.v("CallButtonPresenter.updateButtonsState", "");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700440 final boolean isVideo = call.isVideoCall();
Eric Erfanianccca3152017-02-22 16:32:36 -0800441
442 // Common functionality (audio, hold, etc).
443 // Show either HOLD or SWAP, but not both. If neither HOLD or SWAP is available:
444 // (1) If the device normally can hold, show HOLD in a disabled state.
445 // (2) If the device doesn't have the concept of hold/swap, remove the button.
446 final boolean showSwap = call.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
447 final boolean showHold =
448 !showSwap
449 && call.can(android.telecom.Call.Details.CAPABILITY_SUPPORT_HOLD)
450 && call.can(android.telecom.Call.Details.CAPABILITY_HOLD);
451 final boolean isCallOnHold = call.getState() == DialerCall.State.ONHOLD;
452
453 final boolean showAddCall =
454 TelecomAdapter.getInstance().canAddCall() && UserManagerCompat.isUserUnlocked(mContext);
455 final boolean showMerge = call.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700456 final boolean showUpgradeToVideo = !isVideo && (hasVideoCallCapabilities(call));
Eric Erfanianccca3152017-02-22 16:32:36 -0800457 final boolean showDowngradeToAudio = isVideo && isDowngradeToAudioSupported(call);
458 final boolean showMute = call.can(android.telecom.Call.Details.CAPABILITY_MUTE);
459
460 final boolean hasCameraPermission =
Eric Erfanian2ca43182017-08-31 06:57:16 -0700461 isVideo && VideoUtils.hasCameraPermissionAndShownPrivacyToast(mContext);
Eric Erfanian938468d2017-10-24 14:05:52 -0700462 // Disabling local video doesn't seem to work when dialing. See a bug.
Eric Erfanianccca3152017-02-22 16:32:36 -0800463 final boolean showPauseVideo =
464 isVideo
465 && call.getState() != DialerCall.State.DIALING
466 && call.getState() != DialerCall.State.CONNECTING;
467
twyen00623aa2017-10-10 12:15:08 -0700468 mOtherAccount = TelecomUtil.getOtherAccount(getContext(), call.getAccountHandle());
twyen38af6512017-11-22 17:12:49 -0800469 boolean showSwapSim =
470 mOtherAccount != null
471 && DialerCall.State.isDialing(call.getState())
472 // Most devices cannot make calls on 2 SIMs at the same time.
473 && InCallPresenter.getInstance().getCallList().getAllCalls().size() == 1;
twyen00623aa2017-10-10 12:15:08 -0700474
Eric Erfanianccca3152017-02-22 16:32:36 -0800475 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_AUDIO, true);
476 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_SWAP, showSwap);
477 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_HOLD, showHold);
478 mInCallButtonUi.setHold(isCallOnHold);
479 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_MUTE, showMute);
twyen00623aa2017-10-10 12:15:08 -0700480 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_SWAP_SIM, showSwapSim);
Eric Erfanianccca3152017-02-22 16:32:36 -0800481 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_ADD_CALL, true);
482 mInCallButtonUi.enableButton(InCallButtonIds.BUTTON_ADD_CALL, showAddCall);
483 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_UPGRADE_TO_VIDEO, showUpgradeToVideo);
484 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_DOWNGRADE_TO_AUDIO, showDowngradeToAudio);
485 mInCallButtonUi.showButton(
roldenburgf15085f2017-11-06 12:02:06 -0800486 InCallButtonIds.BUTTON_SWITCH_CAMERA,
487 isVideo && hasCameraPermission && call.getVideoTech().isTransmitting());
Eric Erfanianccca3152017-02-22 16:32:36 -0800488 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_PAUSE_VIDEO, showPauseVideo);
489 if (isVideo) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700490 mInCallButtonUi.setVideoPaused(!call.getVideoTech().isTransmitting() || !hasCameraPermission);
Eric Erfanianccca3152017-02-22 16:32:36 -0800491 }
492 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_DIALPAD, true);
493 mInCallButtonUi.showButton(InCallButtonIds.BUTTON_MERGE, showMerge);
494
495 mInCallButtonUi.updateButtonStates();
496 }
497
498 private boolean hasVideoCallCapabilities(DialerCall call) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700499 return call.getVideoTech().isAvailable(mContext);
Eric Erfanianccca3152017-02-22 16:32:36 -0800500 }
501
502 /**
503 * Determines if downgrading from a video call to an audio-only call is supported. In order to
504 * support downgrade to audio, the SDK version must be >= N and the call should NOT have the
505 * {@link android.telecom.Call.Details#CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO}.
506 *
507 * @param call The call.
508 * @return {@code true} if downgrading to an audio-only call from a video call is supported.
509 */
510 private boolean isDowngradeToAudioSupported(DialerCall call) {
Eric Erfanian938468d2017-10-24 14:05:52 -0700511 // TODO(a bug): If there is an RCS video share session, return true here
Eric Erfanianccca3152017-02-22 16:32:36 -0800512 return !call.can(CallCompat.Details.CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO);
513 }
514
515 @Override
516 public void refreshMuteState() {
517 // Restore the previous mute state
518 if (mAutomaticallyMuted
519 && AudioModeProvider.getInstance().getAudioState().isMuted() != mPreviousMuteState) {
520 if (mInCallButtonUi == null) {
521 return;
522 }
Eric Erfanian9a090c82017-03-16 19:22:24 -0700523 muteClicked(mPreviousMuteState, false /* clickedByUser */);
Eric Erfanianccca3152017-02-22 16:32:36 -0800524 }
525 mAutomaticallyMuted = false;
526 }
527
528 @Override
529 public void onSaveInstanceState(Bundle outState) {
530 outState.putBoolean(KEY_AUTOMATICALLY_MUTED, mAutomaticallyMuted);
531 outState.putBoolean(KEY_PREVIOUS_MUTE_STATE, mPreviousMuteState);
532 }
533
534 @Override
535 public void onRestoreInstanceState(Bundle savedInstanceState) {
536 mAutomaticallyMuted =
537 savedInstanceState.getBoolean(KEY_AUTOMATICALLY_MUTED, mAutomaticallyMuted);
538 mPreviousMuteState = savedInstanceState.getBoolean(KEY_PREVIOUS_MUTE_STATE, mPreviousMuteState);
539 }
540
541 @Override
542 public void onCameraPermissionGranted() {
543 if (mCall != null) {
544 updateButtonsState(mCall);
545 }
546 }
547
548 @Override
549 public void onActiveCameraSelectionChanged(boolean isUsingFrontFacingCamera) {
550 if (mInCallButtonUi == null) {
551 return;
552 }
553 mInCallButtonUi.setCameraSwitched(!isUsingFrontFacingCamera);
554 }
555
556 @Override
557 public Context getContext() {
558 return mContext;
559 }
560
561 private InCallActivity getActivity() {
562 if (mInCallButtonUi != null) {
563 Fragment fragment = mInCallButtonUi.getInCallButtonUiFragment();
564 if (fragment != null) {
565 return (InCallActivity) fragment.getActivity();
566 }
567 }
568 return null;
569 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800570}