blob: 2a9600a2b528b1e4515401d13febdd05eddf99da [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;
Sekine Yasuakida2c0ae2017-07-07 17:01:56 +090045import com.android.incallui.call.DialerCallListener;
Eric Erfanianccca3152017-02-22 16:32:36 -080046import com.android.incallui.call.TelecomAdapter;
wangqibb94ca62018-04-27 14:34:04 -070047import com.android.incallui.call.state.DialerCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -080048import com.android.incallui.incall.protocol.InCallButtonIds;
49import com.android.incallui.incall.protocol.InCallButtonUi;
50import com.android.incallui.incall.protocol.InCallButtonUiDelegate;
twyen00623aa2017-10-10 12:15:08 -070051import com.android.incallui.multisim.SwapSimWorker;
Eric Erfanian90508232017-03-24 09:31:16 -070052import com.android.incallui.videotech.utils.VideoUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080053
54/** Logic for call buttons. */
55public class CallButtonPresenter
56 implements InCallStateListener,
57 AudioModeListener,
58 IncomingCallListener,
59 InCallDetailsListener,
60 CanAddCallListener,
61 Listener,
Sekine Yasuakida2c0ae2017-07-07 17:01:56 +090062 InCallButtonUiDelegate,
63 DialerCallListener {
Eric Erfanianccca3152017-02-22 16:32:36 -080064
linyuh183cb712017-12-27 17:02:37 -080065 private final Context context;
66 private InCallButtonUi inCallButtonUi;
67 private DialerCall call;
Eric Erfanianccca3152017-02-22 16:32:36 -080068 private boolean isInCallButtonUiReady;
linyuh183cb712017-12-27 17:02:37 -080069 private PhoneAccountHandle otherAccount;
Eric Erfanianccca3152017-02-22 16:32:36 -080070
71 public CallButtonPresenter(Context context) {
linyuh183cb712017-12-27 17:02:37 -080072 this.context = context.getApplicationContext();
Eric Erfanianccca3152017-02-22 16:32:36 -080073 }
74
75 @Override
76 public void onInCallButtonUiReady(InCallButtonUi ui) {
77 Assert.checkState(!isInCallButtonUiReady);
linyuh183cb712017-12-27 17:02:37 -080078 inCallButtonUi = ui;
Eric Erfanianccca3152017-02-22 16:32:36 -080079 AudioModeProvider.getInstance().addListener(this);
80
81 // register for call state changes last
82 final InCallPresenter inCallPresenter = InCallPresenter.getInstance();
83 inCallPresenter.addListener(this);
84 inCallPresenter.addIncomingCallListener(this);
85 inCallPresenter.addDetailsListener(this);
86 inCallPresenter.addCanAddCallListener(this);
87 inCallPresenter.getInCallCameraManager().addCameraSelectionListener(this);
88
89 // Update the buttons state immediately for the current call
90 onStateChange(InCallState.NO_CALLS, inCallPresenter.getInCallState(), CallList.getInstance());
91 isInCallButtonUiReady = true;
92 }
93
94 @Override
95 public void onInCallButtonUiUnready() {
96 Assert.checkState(isInCallButtonUiReady);
linyuh183cb712017-12-27 17:02:37 -080097 inCallButtonUi = null;
Eric Erfanianccca3152017-02-22 16:32:36 -080098 InCallPresenter.getInstance().removeListener(this);
99 AudioModeProvider.getInstance().removeListener(this);
100 InCallPresenter.getInstance().removeIncomingCallListener(this);
101 InCallPresenter.getInstance().removeDetailsListener(this);
102 InCallPresenter.getInstance().getInCallCameraManager().removeCameraSelectionListener(this);
103 InCallPresenter.getInstance().removeCanAddCallListener(this);
104 isInCallButtonUiReady = false;
Sekine Yasuakida2c0ae2017-07-07 17:01:56 +0900105
106 if (call != null) {
107 call.removeListener(this);
108 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800109 }
110
111 @Override
112 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700113 Trace.beginSection("CallButtonPresenter.onStateChange");
Sekine Yasuakida2c0ae2017-07-07 17:01:56 +0900114 if (call != null) {
115 call.removeListener(this);
116 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800117 if (newState == InCallState.OUTGOING) {
linyuh183cb712017-12-27 17:02:37 -0800118 call = callList.getOutgoingCall();
Eric Erfanianccca3152017-02-22 16:32:36 -0800119 } else if (newState == InCallState.INCALL) {
linyuh183cb712017-12-27 17:02:37 -0800120 call = callList.getActiveOrBackgroundCall();
Eric Erfanianccca3152017-02-22 16:32:36 -0800121
122 // When connected to voice mail, automatically shows the dialpad.
123 // (On previous releases we showed it when in-call shows up, before waiting for
124 // OUTGOING. We may want to do that once we start showing "Voice mail" label on
125 // the dialpad too.)
linyuh183cb712017-12-27 17:02:37 -0800126 if (oldState == InCallState.OUTGOING && call != null) {
127 if (call.isVoiceMailNumber() && getActivity() != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800128 getActivity().showDialpadFragment(true /* show */, true /* animate */);
129 }
130 }
131 } else if (newState == InCallState.INCOMING) {
132 if (getActivity() != null) {
133 getActivity().showDialpadFragment(false /* show */, true /* animate */);
134 }
linyuh183cb712017-12-27 17:02:37 -0800135 call = callList.getIncomingCall();
Eric Erfanianccca3152017-02-22 16:32:36 -0800136 } else {
linyuh183cb712017-12-27 17:02:37 -0800137 call = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800138 }
Sekine Yasuakida2c0ae2017-07-07 17:01:56 +0900139
140 if (call != null) {
141 call.addListener(this);
142 }
linyuh183cb712017-12-27 17:02:37 -0800143 updateUi(newState, call);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700144 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800145 }
146
147 /**
148 * Updates the user interface in response to a change in the details of a call. Currently handles
149 * changes to the call buttons in response to a change in the details for a call. This is
150 * important to ensure changes to the active call are reflected in the available buttons.
151 *
152 * @param call The active call.
153 * @param details The call details.
154 */
155 @Override
156 public void onDetailsChanged(DialerCall call, android.telecom.Call.Details details) {
157 // Only update if the changes are for the currently active call
linyuh183cb712017-12-27 17:02:37 -0800158 if (inCallButtonUi != null && call != null && call.equals(this.call)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800159 updateButtonsState(call);
160 }
161 }
162
163 @Override
164 public void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call) {
165 onStateChange(oldState, newState, CallList.getInstance());
166 }
167
168 @Override
169 public void onCanAddCallChanged(boolean canAddCall) {
linyuh183cb712017-12-27 17:02:37 -0800170 if (inCallButtonUi != null && call != null) {
171 updateButtonsState(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800172 }
173 }
174
175 @Override
176 public void onAudioStateChanged(CallAudioState audioState) {
linyuh183cb712017-12-27 17:02:37 -0800177 if (inCallButtonUi != null) {
178 inCallButtonUi.setAudioState(audioState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800179 }
180 }
181
182 @Override
183 public CallAudioState getCurrentAudioState() {
184 return AudioModeProvider.getInstance().getAudioState();
185 }
186
187 @Override
188 public void setAudioRoute(int route) {
189 LogUtil.i(
190 "CallButtonPresenter.setAudioRoute",
191 "sending new audio route: " + CallAudioState.audioRouteToString(route));
192 TelecomAdapter.getInstance().setAudioRoute(route);
193 }
194
195 /** Function assumes that bluetooth is not supported. */
196 @Override
197 public void toggleSpeakerphone() {
198 // This function should not be called if bluetooth is available.
199 CallAudioState audioState = getCurrentAudioState();
200 if (0 != (CallAudioState.ROUTE_BLUETOOTH & audioState.getSupportedRouteMask())) {
201 // It's clear the UI is wrong, so update the supported mode once again.
202 LogUtil.e(
203 "CallButtonPresenter", "toggling speakerphone not allowed when bluetooth supported.");
linyuh183cb712017-12-27 17:02:37 -0800204 inCallButtonUi.setAudioState(audioState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800205 return;
206 }
207
208 int newRoute;
209 if (audioState.getRoute() == CallAudioState.ROUTE_SPEAKER) {
210 newRoute = CallAudioState.ROUTE_WIRED_OR_EARPIECE;
linyuh183cb712017-12-27 17:02:37 -0800211 Logger.get(context)
Eric Erfanianccca3152017-02-22 16:32:36 -0800212 .logCallImpression(
213 DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_WIRED_OR_EARPIECE,
linyuh183cb712017-12-27 17:02:37 -0800214 call.getUniqueCallId(),
215 call.getTimeAddedMs());
Eric Erfanianccca3152017-02-22 16:32:36 -0800216 } else {
217 newRoute = CallAudioState.ROUTE_SPEAKER;
linyuh183cb712017-12-27 17:02:37 -0800218 Logger.get(context)
Eric Erfanianccca3152017-02-22 16:32:36 -0800219 .logCallImpression(
220 DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_SPEAKERPHONE,
linyuh183cb712017-12-27 17:02:37 -0800221 call.getUniqueCallId(),
222 call.getTimeAddedMs());
Eric Erfanianccca3152017-02-22 16:32:36 -0800223 }
224
225 setAudioRoute(newRoute);
226 }
227
228 @Override
Eric Erfanian9a090c82017-03-16 19:22:24 -0700229 public void muteClicked(boolean checked, boolean clickedByUser) {
230 LogUtil.i(
231 "CallButtonPresenter", "turning on mute: %s, clicked by user: %s", checked, clickedByUser);
232 if (clickedByUser) {
linyuh183cb712017-12-27 17:02:37 -0800233 Logger.get(context)
Eric Erfanian9a090c82017-03-16 19:22:24 -0700234 .logCallImpression(
235 checked
236 ? DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_MUTE
237 : DialerImpression.Type.IN_CALL_SCREEN_TURN_OFF_MUTE,
linyuh183cb712017-12-27 17:02:37 -0800238 call.getUniqueCallId(),
239 call.getTimeAddedMs());
Eric Erfanian9a090c82017-03-16 19:22:24 -0700240 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800241 TelecomAdapter.getInstance().mute(checked);
242 }
243
244 @Override
245 public void holdClicked(boolean checked) {
linyuh183cb712017-12-27 17:02:37 -0800246 if (call == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800247 return;
248 }
249 if (checked) {
linyuh183cb712017-12-27 17:02:37 -0800250 LogUtil.i("CallButtonPresenter", "putting the call on hold: " + call);
251 call.hold();
Eric Erfanianccca3152017-02-22 16:32:36 -0800252 } else {
linyuh183cb712017-12-27 17:02:37 -0800253 LogUtil.i("CallButtonPresenter", "removing the call from hold: " + call);
254 call.unhold();
Eric Erfanianccca3152017-02-22 16:32:36 -0800255 }
256 }
257
258 @Override
259 public void swapClicked() {
linyuh183cb712017-12-27 17:02:37 -0800260 if (call == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800261 return;
262 }
263
linyuh183cb712017-12-27 17:02:37 -0800264 LogUtil.i("CallButtonPresenter", "swapping the call: " + call);
265 TelecomAdapter.getInstance().swap(call.getId());
Eric Erfanianccca3152017-02-22 16:32:36 -0800266 }
267
268 @Override
269 public void mergeClicked() {
linyuh183cb712017-12-27 17:02:37 -0800270 Logger.get(context)
Eric Erfanian2ca43182017-08-31 06:57:16 -0700271 .logCallImpression(
272 DialerImpression.Type.IN_CALL_MERGE_BUTTON_PRESSED,
linyuh183cb712017-12-27 17:02:37 -0800273 call.getUniqueCallId(),
274 call.getTimeAddedMs());
275 TelecomAdapter.getInstance().merge(call.getId());
Eric Erfanianccca3152017-02-22 16:32:36 -0800276 }
277
278 @Override
279 public void addCallClicked() {
linyuh183cb712017-12-27 17:02:37 -0800280 Logger.get(context)
Eric Erfanian2ca43182017-08-31 06:57:16 -0700281 .logCallImpression(
282 DialerImpression.Type.IN_CALL_ADD_CALL_BUTTON_PRESSED,
linyuh183cb712017-12-27 17:02:37 -0800283 call.getUniqueCallId(),
284 call.getTimeAddedMs());
Sekine Yasuakia7020842018-05-31 17:10:12 +0900285 InCallPresenter.getInstance().addCallClicked();
Eric Erfanianccca3152017-02-22 16:32:36 -0800286 }
287
288 @Override
289 public void showDialpadClicked(boolean checked) {
linyuh183cb712017-12-27 17:02:37 -0800290 Logger.get(context)
Eric Erfanian2ca43182017-08-31 06:57:16 -0700291 .logCallImpression(
292 DialerImpression.Type.IN_CALL_SHOW_DIALPAD_BUTTON_PRESSED,
linyuh183cb712017-12-27 17:02:37 -0800293 call.getUniqueCallId(),
294 call.getTimeAddedMs());
Eric Erfanianccca3152017-02-22 16:32:36 -0800295 LogUtil.v("CallButtonPresenter", "show dialpad " + String.valueOf(checked));
296 getActivity().showDialpadFragment(checked /* show */, true /* animate */);
297 }
298
299 @Override
300 public void changeToVideoClicked() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700301 LogUtil.enterBlock("CallButtonPresenter.changeToVideoClicked");
linyuh183cb712017-12-27 17:02:37 -0800302 Logger.get(context)
Eric Erfanian8369df02017-05-03 10:27:13 -0700303 .logCallImpression(
304 DialerImpression.Type.VIDEO_CALL_UPGRADE_REQUESTED,
linyuh183cb712017-12-27 17:02:37 -0800305 call.getUniqueCallId(),
306 call.getTimeAddedMs());
307 call.getVideoTech().upgradeToVideo(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800308 }
309
310 @Override
wangqif6be6172018-03-30 15:57:56 -0700311 public void changeToRttClicked() {
312 LogUtil.enterBlock("CallButtonPresenter.changeToRttClicked");
313 call.sendRttUpgradeRequest();
314 }
315
316 @Override
Eric Erfanianccca3152017-02-22 16:32:36 -0800317 public void onEndCallClicked() {
linyuh183cb712017-12-27 17:02:37 -0800318 LogUtil.i("CallButtonPresenter.onEndCallClicked", "call: " + call);
319 if (call != null) {
320 call.disconnect();
Eric Erfanianccca3152017-02-22 16:32:36 -0800321 }
322 }
323
324 @Override
325 public void showAudioRouteSelector() {
linyuh183cb712017-12-27 17:02:37 -0800326 inCallButtonUi.showAudioRouteSelector();
Eric Erfanianccca3152017-02-22 16:32:36 -0800327 }
328
twyen00623aa2017-10-10 12:15:08 -0700329 @Override
330 public void swapSimClicked() {
331 LogUtil.enterBlock("CallButtonPresenter.swapSimClicked");
twyenfc1f9cf2017-11-27 13:22:48 -0800332 Logger.get(getContext()).logImpression(Type.DUAL_SIM_CHANGE_SIM_PRESSED);
twyen00623aa2017-10-10 12:15:08 -0700333 SwapSimWorker worker =
334 new SwapSimWorker(
335 getContext(),
linyuh183cb712017-12-27 17:02:37 -0800336 call,
twyen00623aa2017-10-10 12:15:08 -0700337 InCallPresenter.getInstance().getCallList(),
linyuh183cb712017-12-27 17:02:37 -0800338 otherAccount,
twyen00623aa2017-10-10 12:15:08 -0700339 InCallPresenter.getInstance().acquireInCallUiLock("swapSim"));
340 DialerExecutorComponent.get(getContext())
341 .dialerExecutorFactory()
342 .createNonUiTaskBuilder(worker)
343 .build()
344 .executeParallel(null);
345 }
346
Eric Erfanianccca3152017-02-22 16:32:36 -0800347 /**
348 * Switches the camera between the front-facing and back-facing camera.
349 *
350 * @param useFrontFacingCamera True if we should switch to using the front-facing camera, or false
351 * if we should switch to using the back-facing camera.
352 */
353 @Override
354 public void switchCameraClicked(boolean useFrontFacingCamera) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700355 updateCamera(useFrontFacingCamera);
Eric Erfanianccca3152017-02-22 16:32:36 -0800356 }
357
358 @Override
359 public void toggleCameraClicked() {
360 LogUtil.i("CallButtonPresenter.toggleCameraClicked", "");
linyuh183cb712017-12-27 17:02:37 -0800361 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700362 return;
363 }
linyuh183cb712017-12-27 17:02:37 -0800364 Logger.get(context)
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700365 .logCallImpression(
366 DialerImpression.Type.IN_CALL_SCREEN_SWAP_CAMERA,
linyuh183cb712017-12-27 17:02:37 -0800367 call.getUniqueCallId(),
368 call.getTimeAddedMs());
Eric Erfanianccca3152017-02-22 16:32:36 -0800369 switchCameraClicked(
370 !InCallPresenter.getInstance().getInCallCameraManager().isUsingFrontFacingCamera());
371 }
372
373 /**
374 * Stop or start client's video transmission.
375 *
376 * @param pause True if pausing the local user's video, or false if starting the local user's
377 * video.
378 */
379 @Override
380 public void pauseVideoClicked(boolean pause) {
381 LogUtil.i("CallButtonPresenter.pauseVideoClicked", "%s", pause ? "pause" : "unpause");
Eric Erfanianccca3152017-02-22 16:32:36 -0800382
linyuh183cb712017-12-27 17:02:37 -0800383 Logger.get(context)
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700384 .logCallImpression(
385 pause
386 ? DialerImpression.Type.IN_CALL_SCREEN_TURN_OFF_VIDEO
387 : DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_VIDEO,
linyuh183cb712017-12-27 17:02:37 -0800388 call.getUniqueCallId(),
389 call.getTimeAddedMs());
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700390
Eric Erfanianccca3152017-02-22 16:32:36 -0800391 if (pause) {
linyuh183cb712017-12-27 17:02:37 -0800392 call.getVideoTech().stopTransmission();
Eric Erfanianccca3152017-02-22 16:32:36 -0800393 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700394 updateCamera(
395 InCallPresenter.getInstance().getInCallCameraManager().isUsingFrontFacingCamera());
linyuh183cb712017-12-27 17:02:37 -0800396 call.getVideoTech().resumeTransmission(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800397 }
398
linyuh183cb712017-12-27 17:02:37 -0800399 inCallButtonUi.setVideoPaused(pause);
400 inCallButtonUi.enableButton(InCallButtonIds.BUTTON_PAUSE_VIDEO, false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800401 }
402
Eric Erfanian2ca43182017-08-31 06:57:16 -0700403 private void updateCamera(boolean useFrontFacingCamera) {
404 InCallCameraManager cameraManager = InCallPresenter.getInstance().getInCallCameraManager();
405 cameraManager.setUseFrontFacingCamera(useFrontFacingCamera);
406
407 String cameraId = cameraManager.getActiveCameraId();
408 if (cameraId != null) {
409 final int cameraDir =
410 cameraManager.isUsingFrontFacingCamera()
411 ? CameraDirection.CAMERA_DIRECTION_FRONT_FACING
412 : CameraDirection.CAMERA_DIRECTION_BACK_FACING;
linyuh183cb712017-12-27 17:02:37 -0800413 call.setCameraDir(cameraDir);
414 call.getVideoTech().setCamera(cameraId);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700415 }
416 }
417
Eric Erfanianccca3152017-02-22 16:32:36 -0800418 private void updateUi(InCallState state, DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700419 LogUtil.v("CallButtonPresenter", "updating call UI for call: %s", call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800420
linyuh183cb712017-12-27 17:02:37 -0800421 if (inCallButtonUi == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800422 return;
423 }
424
Eric Erfanian92854e02018-07-10 18:37:33 +0000425 if (call != null) {
426 inCallButtonUi.updateInCallButtonUiColors(
427 InCallPresenter.getInstance().getThemeColorManager().getSecondaryColor());
428 }
429
Eric Erfanianccca3152017-02-22 16:32:36 -0800430 final boolean isEnabled =
431 state.isConnectingOrConnected() && !state.isIncoming() && call != null;
linyuh183cb712017-12-27 17:02:37 -0800432 inCallButtonUi.setEnabled(isEnabled);
Eric Erfanianccca3152017-02-22 16:32:36 -0800433
434 if (call == null) {
435 return;
436 }
437
438 updateButtonsState(call);
439 }
440
441 /**
442 * Updates the buttons applicable for the UI.
443 *
444 * @param call The active call.
445 */
linyuh60c1e572018-06-28 20:13:52 -0700446 @SuppressWarnings(value = {"MissingPermission"})
Eric Erfanianccca3152017-02-22 16:32:36 -0800447 private void updateButtonsState(DialerCall call) {
448 LogUtil.v("CallButtonPresenter.updateButtonsState", "");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700449 final boolean isVideo = call.isVideoCall();
Eric Erfanianccca3152017-02-22 16:32:36 -0800450
451 // Common functionality (audio, hold, etc).
452 // Show either HOLD or SWAP, but not both. If neither HOLD or SWAP is available:
453 // (1) If the device normally can hold, show HOLD in a disabled state.
454 // (2) If the device doesn't have the concept of hold/swap, remove the button.
455 final boolean showSwap = call.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
456 final boolean showHold =
457 !showSwap
458 && call.can(android.telecom.Call.Details.CAPABILITY_SUPPORT_HOLD)
459 && call.can(android.telecom.Call.Details.CAPABILITY_HOLD);
wangqibb94ca62018-04-27 14:34:04 -0700460 final boolean isCallOnHold = call.getState() == DialerCallState.ONHOLD;
Eric Erfanianccca3152017-02-22 16:32:36 -0800461
462 final boolean showAddCall =
linyuh183cb712017-12-27 17:02:37 -0800463 TelecomAdapter.getInstance().canAddCall() && UserManagerCompat.isUserUnlocked(context);
erfanian9dfd6a42018-04-17 15:01:17 -0700464 // There can only be two calls so don't show the ability to merge when one of them
465 // is a speak easy call.
466 final boolean showMerge =
467 InCallPresenter.getInstance()
468 .getCallList()
469 .getAllCalls()
470 .stream()
471 .noneMatch(c -> c != null && c.isSpeakEasyCall())
472 && call.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700473 final boolean showUpgradeToVideo = !isVideo && (hasVideoCallCapabilities(call));
Eric Erfanianccca3152017-02-22 16:32:36 -0800474 final boolean showDowngradeToAudio = isVideo && isDowngradeToAudioSupported(call);
475 final boolean showMute = call.can(android.telecom.Call.Details.CAPABILITY_MUTE);
476
477 final boolean hasCameraPermission =
linyuh183cb712017-12-27 17:02:37 -0800478 isVideo && VideoUtils.hasCameraPermissionAndShownPrivacyToast(context);
Eric Erfanian938468d2017-10-24 14:05:52 -0700479 // Disabling local video doesn't seem to work when dialing. See a bug.
Eric Erfanianccca3152017-02-22 16:32:36 -0800480 final boolean showPauseVideo =
481 isVideo
wangqibb94ca62018-04-27 14:34:04 -0700482 && call.getState() != DialerCallState.DIALING
483 && call.getState() != DialerCallState.CONNECTING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800484
linyuh183cb712017-12-27 17:02:37 -0800485 otherAccount = TelecomUtil.getOtherAccount(getContext(), call.getAccountHandle());
twyen38af6512017-11-22 17:12:49 -0800486 boolean showSwapSim =
twyenea3b1622018-04-24 10:45:08 -0700487 !call.isEmergencyCall()
488 && otherAccount != null
twyen2c6803d2017-11-28 11:41:49 -0800489 && !call.isVoiceMailNumber()
wangqibb94ca62018-04-27 14:34:04 -0700490 && DialerCallState.isDialing(call.getState())
twyen38af6512017-11-22 17:12:49 -0800491 // Most devices cannot make calls on 2 SIMs at the same time.
492 && InCallPresenter.getInstance().getCallList().getAllCalls().size() == 1;
twyen00623aa2017-10-10 12:15:08 -0700493
wangqi01b78272018-04-03 17:02:44 -0700494 boolean showUpgradeToRtt = call.canUpgradeToRttCall();
wangqibb94ca62018-04-27 14:34:04 -0700495 boolean enableUpgradeToRtt = showUpgradeToRtt && call.getState() == DialerCallState.ACTIVE;
wangqif6be6172018-03-30 15:57:56 -0700496
linyuh183cb712017-12-27 17:02:37 -0800497 inCallButtonUi.showButton(InCallButtonIds.BUTTON_AUDIO, true);
498 inCallButtonUi.showButton(InCallButtonIds.BUTTON_SWAP, showSwap);
499 inCallButtonUi.showButton(InCallButtonIds.BUTTON_HOLD, showHold);
500 inCallButtonUi.setHold(isCallOnHold);
501 inCallButtonUi.showButton(InCallButtonIds.BUTTON_MUTE, showMute);
502 inCallButtonUi.showButton(InCallButtonIds.BUTTON_SWAP_SIM, showSwapSim);
503 inCallButtonUi.showButton(InCallButtonIds.BUTTON_ADD_CALL, true);
504 inCallButtonUi.enableButton(InCallButtonIds.BUTTON_ADD_CALL, showAddCall);
505 inCallButtonUi.showButton(InCallButtonIds.BUTTON_UPGRADE_TO_VIDEO, showUpgradeToVideo);
wangqif6be6172018-03-30 15:57:56 -0700506 inCallButtonUi.showButton(InCallButtonIds.BUTTON_UPGRADE_TO_RTT, showUpgradeToRtt);
wangqi15514ef2018-04-04 10:50:13 -0700507 inCallButtonUi.enableButton(InCallButtonIds.BUTTON_UPGRADE_TO_RTT, enableUpgradeToRtt);
linyuh183cb712017-12-27 17:02:37 -0800508 inCallButtonUi.showButton(InCallButtonIds.BUTTON_DOWNGRADE_TO_AUDIO, showDowngradeToAudio);
509 inCallButtonUi.showButton(
roldenburgf15085f2017-11-06 12:02:06 -0800510 InCallButtonIds.BUTTON_SWITCH_CAMERA,
511 isVideo && hasCameraPermission && call.getVideoTech().isTransmitting());
linyuh183cb712017-12-27 17:02:37 -0800512 inCallButtonUi.showButton(InCallButtonIds.BUTTON_PAUSE_VIDEO, showPauseVideo);
Eric Erfanianccca3152017-02-22 16:32:36 -0800513 if (isVideo) {
linyuh183cb712017-12-27 17:02:37 -0800514 inCallButtonUi.setVideoPaused(!call.getVideoTech().isTransmitting() || !hasCameraPermission);
Eric Erfanianccca3152017-02-22 16:32:36 -0800515 }
linyuh183cb712017-12-27 17:02:37 -0800516 inCallButtonUi.showButton(InCallButtonIds.BUTTON_DIALPAD, true);
517 inCallButtonUi.showButton(InCallButtonIds.BUTTON_MERGE, showMerge);
Eric Erfanianccca3152017-02-22 16:32:36 -0800518
linyuh183cb712017-12-27 17:02:37 -0800519 inCallButtonUi.updateButtonStates();
Eric Erfanianccca3152017-02-22 16:32:36 -0800520 }
521
522 private boolean hasVideoCallCapabilities(DialerCall call) {
roldenburg6bd612f2018-01-18 12:57:19 -0800523 return call.getVideoTech().isAvailable(context, call.getAccountHandle());
Eric Erfanianccca3152017-02-22 16:32:36 -0800524 }
525
526 /**
527 * Determines if downgrading from a video call to an audio-only call is supported. In order to
528 * support downgrade to audio, the SDK version must be >= N and the call should NOT have the
529 * {@link android.telecom.Call.Details#CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO}.
530 *
531 * @param call The call.
532 * @return {@code true} if downgrading to an audio-only call from a video call is supported.
533 */
534 private boolean isDowngradeToAudioSupported(DialerCall call) {
Eric Erfanian938468d2017-10-24 14:05:52 -0700535 // TODO(a bug): If there is an RCS video share session, return true here
Eric Erfanianccca3152017-02-22 16:32:36 -0800536 return !call.can(CallCompat.Details.CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO);
537 }
538
539 @Override
Sekine Yasuakia7020842018-05-31 17:10:12 +0900540 public void onSaveInstanceState(Bundle outState) {}
Eric Erfanianccca3152017-02-22 16:32:36 -0800541
542 @Override
Sekine Yasuakia7020842018-05-31 17:10:12 +0900543 public void onRestoreInstanceState(Bundle savedInstanceState) {}
Eric Erfanianccca3152017-02-22 16:32:36 -0800544
545 @Override
546 public void onCameraPermissionGranted() {
linyuh183cb712017-12-27 17:02:37 -0800547 if (call != null) {
548 updateButtonsState(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800549 }
550 }
551
552 @Override
553 public void onActiveCameraSelectionChanged(boolean isUsingFrontFacingCamera) {
linyuh183cb712017-12-27 17:02:37 -0800554 if (inCallButtonUi == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800555 return;
556 }
linyuh183cb712017-12-27 17:02:37 -0800557 inCallButtonUi.setCameraSwitched(!isUsingFrontFacingCamera);
Eric Erfanianccca3152017-02-22 16:32:36 -0800558 }
559
560 @Override
Sekine Yasuakida2c0ae2017-07-07 17:01:56 +0900561 public void onDialerCallSessionModificationStateChange() {
562 if (inCallButtonUi != null && call != null) {
563 inCallButtonUi.enableButton(InCallButtonIds.BUTTON_PAUSE_VIDEO, true);
564 updateButtonsState(call);
565 }
566 }
567
568 @Override
569 public void onDialerCallDisconnect() {}
570
571 @Override
572 public void onDialerCallUpdate() {}
573
574 @Override
575 public void onDialerCallChildNumberChange() {}
576
577 @Override
578 public void onDialerCallLastForwardedNumberChange() {}
579
580 @Override
581 public void onDialerCallUpgradeToVideo() {}
582
583 @Override
584 public void onWiFiToLteHandover() {}
585
586 @Override
587 public void onHandoverToWifiFailure() {}
588
589 @Override
590 public void onInternationalCallOnWifi() {}
591
592 @Override
593 public void onEnrichedCallSessionUpdate() {}
594
595 @Override
Eric Erfanianccca3152017-02-22 16:32:36 -0800596 public Context getContext() {
linyuh183cb712017-12-27 17:02:37 -0800597 return context;
Eric Erfanianccca3152017-02-22 16:32:36 -0800598 }
599
600 private InCallActivity getActivity() {
linyuh183cb712017-12-27 17:02:37 -0800601 if (inCallButtonUi != null) {
602 Fragment fragment = inCallButtonUi.getInCallButtonUiFragment();
Eric Erfanianccca3152017-02-22 16:32:36 -0800603 if (fragment != null) {
604 return (InCallActivity) fragment.getActivity();
605 }
606 }
607 return null;
608 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800609}