Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.incallui; |
| 18 | |
| 19 | import android.content.Context; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 20 | import android.os.Bundle; |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 21 | import android.os.Trace; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 22 | import android.support.v4.app.Fragment; |
| 23 | import android.support.v4.os.UserManagerCompat; |
| 24 | import android.telecom.CallAudioState; |
twyen | 00623aa | 2017-10-10 12:15:08 -0700 | [diff] [blame] | 25 | import android.telecom.PhoneAccountHandle; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 26 | import com.android.contacts.common.compat.CallCompat; |
| 27 | import com.android.dialer.common.Assert; |
| 28 | import com.android.dialer.common.LogUtil; |
twyen | 00623aa | 2017-10-10 12:15:08 -0700 | [diff] [blame] | 29 | import com.android.dialer.common.concurrent.DialerExecutorComponent; |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 30 | import com.android.dialer.logging.DialerImpression; |
twyen | fc1f9cf | 2017-11-27 13:22:48 -0800 | [diff] [blame] | 31 | import com.android.dialer.logging.DialerImpression.Type; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 32 | import com.android.dialer.logging.Logger; |
twyen | 00623aa | 2017-10-10 12:15:08 -0700 | [diff] [blame] | 33 | import com.android.dialer.telecom.TelecomUtil; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 34 | import com.android.incallui.InCallCameraManager.Listener; |
| 35 | import com.android.incallui.InCallPresenter.CanAddCallListener; |
| 36 | import com.android.incallui.InCallPresenter.InCallDetailsListener; |
| 37 | import com.android.incallui.InCallPresenter.InCallState; |
| 38 | import com.android.incallui.InCallPresenter.InCallStateListener; |
| 39 | import com.android.incallui.InCallPresenter.IncomingCallListener; |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 40 | import com.android.incallui.audiomode.AudioModeProvider; |
| 41 | import com.android.incallui.audiomode.AudioModeProvider.AudioModeListener; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 42 | import com.android.incallui.call.CallList; |
| 43 | import com.android.incallui.call.DialerCall; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 44 | import com.android.incallui.call.DialerCall.CameraDirection; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 45 | import com.android.incallui.call.TelecomAdapter; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 46 | import com.android.incallui.incall.protocol.InCallButtonIds; |
| 47 | import com.android.incallui.incall.protocol.InCallButtonUi; |
| 48 | import com.android.incallui.incall.protocol.InCallButtonUiDelegate; |
twyen | 00623aa | 2017-10-10 12:15:08 -0700 | [diff] [blame] | 49 | import com.android.incallui.multisim.SwapSimWorker; |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 50 | import com.android.incallui.videotech.utils.VideoUtils; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 51 | |
| 52 | /** Logic for call buttons. */ |
| 53 | public 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 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 65 | private final Context context; |
| 66 | private InCallButtonUi inCallButtonUi; |
| 67 | private DialerCall call; |
| 68 | private boolean automaticallyMuted = false; |
| 69 | private boolean previousMuteState = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 70 | private boolean isInCallButtonUiReady; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 71 | private PhoneAccountHandle otherAccount; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 72 | |
| 73 | public CallButtonPresenter(Context context) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 74 | this.context = context.getApplicationContext(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void onInCallButtonUiReady(InCallButtonUi ui) { |
| 79 | Assert.checkState(!isInCallButtonUiReady); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 80 | inCallButtonUi = ui; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 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); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 99 | inCallButtonUi = null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 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 Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 111 | Trace.beginSection("CallButtonPresenter.onStateChange"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 112 | if (newState == InCallState.OUTGOING) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 113 | call = callList.getOutgoingCall(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 114 | } else if (newState == InCallState.INCALL) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 115 | call = callList.getActiveOrBackgroundCall(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 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.) |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 121 | if (oldState == InCallState.OUTGOING && call != null) { |
| 122 | if (call.isVoiceMailNumber() && getActivity() != null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 123 | 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 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 130 | call = callList.getIncomingCall(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 131 | } else { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 132 | call = null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 133 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 134 | updateUi(newState, call); |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 135 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 136 | } |
| 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 |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 149 | if (inCallButtonUi != null && call != null && call.equals(this.call)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 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) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 161 | if (inCallButtonUi != null && call != null) { |
| 162 | updateButtonsState(call); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void onAudioStateChanged(CallAudioState audioState) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 168 | if (inCallButtonUi != null) { |
| 169 | inCallButtonUi.setAudioState(audioState); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 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."); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 195 | inCallButtonUi.setAudioState(audioState); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | |
| 199 | int newRoute; |
| 200 | if (audioState.getRoute() == CallAudioState.ROUTE_SPEAKER) { |
| 201 | newRoute = CallAudioState.ROUTE_WIRED_OR_EARPIECE; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 202 | Logger.get(context) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 203 | .logCallImpression( |
| 204 | DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_WIRED_OR_EARPIECE, |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 205 | call.getUniqueCallId(), |
| 206 | call.getTimeAddedMs()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 207 | } else { |
| 208 | newRoute = CallAudioState.ROUTE_SPEAKER; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 209 | Logger.get(context) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 210 | .logCallImpression( |
| 211 | DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_SPEAKERPHONE, |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 212 | call.getUniqueCallId(), |
| 213 | call.getTimeAddedMs()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | setAudioRoute(newRoute); |
| 217 | } |
| 218 | |
| 219 | @Override |
Eric Erfanian | 9a090c8 | 2017-03-16 19:22:24 -0700 | [diff] [blame] | 220 | 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) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 224 | Logger.get(context) |
Eric Erfanian | 9a090c8 | 2017-03-16 19:22:24 -0700 | [diff] [blame] | 225 | .logCallImpression( |
| 226 | checked |
| 227 | ? DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_MUTE |
| 228 | : DialerImpression.Type.IN_CALL_SCREEN_TURN_OFF_MUTE, |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 229 | call.getUniqueCallId(), |
| 230 | call.getTimeAddedMs()); |
Eric Erfanian | 9a090c8 | 2017-03-16 19:22:24 -0700 | [diff] [blame] | 231 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 232 | TelecomAdapter.getInstance().mute(checked); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public void holdClicked(boolean checked) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 237 | if (call == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 238 | return; |
| 239 | } |
| 240 | if (checked) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 241 | LogUtil.i("CallButtonPresenter", "putting the call on hold: " + call); |
| 242 | call.hold(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 243 | } else { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 244 | LogUtil.i("CallButtonPresenter", "removing the call from hold: " + call); |
| 245 | call.unhold(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public void swapClicked() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 251 | if (call == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 252 | return; |
| 253 | } |
| 254 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 255 | LogUtil.i("CallButtonPresenter", "swapping the call: " + call); |
| 256 | TelecomAdapter.getInstance().swap(call.getId()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public void mergeClicked() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 261 | Logger.get(context) |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 262 | .logCallImpression( |
| 263 | DialerImpression.Type.IN_CALL_MERGE_BUTTON_PRESSED, |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 264 | call.getUniqueCallId(), |
| 265 | call.getTimeAddedMs()); |
| 266 | TelecomAdapter.getInstance().merge(call.getId()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public void addCallClicked() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 271 | Logger.get(context) |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 272 | .logCallImpression( |
| 273 | DialerImpression.Type.IN_CALL_ADD_CALL_BUTTON_PRESSED, |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 274 | call.getUniqueCallId(), |
| 275 | call.getTimeAddedMs()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 276 | // Automatically mute the current call |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 277 | automaticallyMuted = true; |
| 278 | previousMuteState = AudioModeProvider.getInstance().getAudioState().isMuted(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 279 | // Simulate a click on the mute button |
Eric Erfanian | 9a090c8 | 2017-03-16 19:22:24 -0700 | [diff] [blame] | 280 | muteClicked(true /* checked */, false /* clickedByUser */); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 281 | TelecomAdapter.getInstance().addCall(); |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public void showDialpadClicked(boolean checked) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 286 | Logger.get(context) |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 287 | .logCallImpression( |
| 288 | DialerImpression.Type.IN_CALL_SHOW_DIALPAD_BUTTON_PRESSED, |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 289 | call.getUniqueCallId(), |
| 290 | call.getTimeAddedMs()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 291 | LogUtil.v("CallButtonPresenter", "show dialpad " + String.valueOf(checked)); |
| 292 | getActivity().showDialpadFragment(checked /* show */, true /* animate */); |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public void changeToVideoClicked() { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 297 | LogUtil.enterBlock("CallButtonPresenter.changeToVideoClicked"); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 298 | Logger.get(context) |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 299 | .logCallImpression( |
| 300 | DialerImpression.Type.VIDEO_CALL_UPGRADE_REQUESTED, |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 301 | call.getUniqueCallId(), |
| 302 | call.getTimeAddedMs()); |
| 303 | call.getVideoTech().upgradeToVideo(context); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | @Override |
wangqi | f6be617 | 2018-03-30 15:57:56 -0700 | [diff] [blame^] | 307 | public void changeToRttClicked() { |
| 308 | LogUtil.enterBlock("CallButtonPresenter.changeToRttClicked"); |
| 309 | call.sendRttUpgradeRequest(); |
| 310 | } |
| 311 | |
| 312 | @Override |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 313 | public void onEndCallClicked() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 314 | LogUtil.i("CallButtonPresenter.onEndCallClicked", "call: " + call); |
| 315 | if (call != null) { |
| 316 | call.disconnect(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public void showAudioRouteSelector() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 322 | inCallButtonUi.showAudioRouteSelector(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 323 | } |
| 324 | |
twyen | 00623aa | 2017-10-10 12:15:08 -0700 | [diff] [blame] | 325 | @Override |
| 326 | public void swapSimClicked() { |
| 327 | LogUtil.enterBlock("CallButtonPresenter.swapSimClicked"); |
twyen | fc1f9cf | 2017-11-27 13:22:48 -0800 | [diff] [blame] | 328 | Logger.get(getContext()).logImpression(Type.DUAL_SIM_CHANGE_SIM_PRESSED); |
twyen | 00623aa | 2017-10-10 12:15:08 -0700 | [diff] [blame] | 329 | SwapSimWorker worker = |
| 330 | new SwapSimWorker( |
| 331 | getContext(), |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 332 | call, |
twyen | 00623aa | 2017-10-10 12:15:08 -0700 | [diff] [blame] | 333 | InCallPresenter.getInstance().getCallList(), |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 334 | otherAccount, |
twyen | 00623aa | 2017-10-10 12:15:08 -0700 | [diff] [blame] | 335 | InCallPresenter.getInstance().acquireInCallUiLock("swapSim")); |
| 336 | DialerExecutorComponent.get(getContext()) |
| 337 | .dialerExecutorFactory() |
| 338 | .createNonUiTaskBuilder(worker) |
| 339 | .build() |
| 340 | .executeParallel(null); |
| 341 | } |
| 342 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 343 | /** |
| 344 | * Switches the camera between the front-facing and back-facing camera. |
| 345 | * |
| 346 | * @param useFrontFacingCamera True if we should switch to using the front-facing camera, or false |
| 347 | * if we should switch to using the back-facing camera. |
| 348 | */ |
| 349 | @Override |
| 350 | public void switchCameraClicked(boolean useFrontFacingCamera) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 351 | updateCamera(useFrontFacingCamera); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public void toggleCameraClicked() { |
| 356 | LogUtil.i("CallButtonPresenter.toggleCameraClicked", ""); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 357 | if (call == null) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 358 | return; |
| 359 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 360 | Logger.get(context) |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 361 | .logCallImpression( |
| 362 | DialerImpression.Type.IN_CALL_SCREEN_SWAP_CAMERA, |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 363 | call.getUniqueCallId(), |
| 364 | call.getTimeAddedMs()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 365 | switchCameraClicked( |
| 366 | !InCallPresenter.getInstance().getInCallCameraManager().isUsingFrontFacingCamera()); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Stop or start client's video transmission. |
| 371 | * |
| 372 | * @param pause True if pausing the local user's video, or false if starting the local user's |
| 373 | * video. |
| 374 | */ |
| 375 | @Override |
| 376 | public void pauseVideoClicked(boolean pause) { |
| 377 | LogUtil.i("CallButtonPresenter.pauseVideoClicked", "%s", pause ? "pause" : "unpause"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 378 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 379 | Logger.get(context) |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 380 | .logCallImpression( |
| 381 | pause |
| 382 | ? DialerImpression.Type.IN_CALL_SCREEN_TURN_OFF_VIDEO |
| 383 | : DialerImpression.Type.IN_CALL_SCREEN_TURN_ON_VIDEO, |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 384 | call.getUniqueCallId(), |
| 385 | call.getTimeAddedMs()); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 386 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 387 | if (pause) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 388 | call.getVideoTech().setCamera(null); |
| 389 | call.getVideoTech().stopTransmission(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 390 | } else { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 391 | updateCamera( |
| 392 | InCallPresenter.getInstance().getInCallCameraManager().isUsingFrontFacingCamera()); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 393 | call.getVideoTech().resumeTransmission(context); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 394 | } |
| 395 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 396 | inCallButtonUi.setVideoPaused(pause); |
| 397 | inCallButtonUi.enableButton(InCallButtonIds.BUTTON_PAUSE_VIDEO, false); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 400 | private void updateCamera(boolean useFrontFacingCamera) { |
| 401 | InCallCameraManager cameraManager = InCallPresenter.getInstance().getInCallCameraManager(); |
| 402 | cameraManager.setUseFrontFacingCamera(useFrontFacingCamera); |
| 403 | |
| 404 | String cameraId = cameraManager.getActiveCameraId(); |
| 405 | if (cameraId != null) { |
| 406 | final int cameraDir = |
| 407 | cameraManager.isUsingFrontFacingCamera() |
| 408 | ? CameraDirection.CAMERA_DIRECTION_FRONT_FACING |
| 409 | : CameraDirection.CAMERA_DIRECTION_BACK_FACING; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 410 | call.setCameraDir(cameraDir); |
| 411 | call.getVideoTech().setCamera(cameraId); |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 415 | private void updateUi(InCallState state, DialerCall call) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 416 | LogUtil.v("CallButtonPresenter", "updating call UI for call: %s", call); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 417 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 418 | if (inCallButtonUi == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | |
| 422 | if (call != null) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 423 | inCallButtonUi.updateInCallButtonUiColors( |
wangqi | 8d662ca | 2017-10-26 11:27:19 -0700 | [diff] [blame] | 424 | InCallPresenter.getInstance().getThemeColorManager().getSecondaryColor()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | final boolean isEnabled = |
| 428 | state.isConnectingOrConnected() && !state.isIncoming() && call != null; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 429 | inCallButtonUi.setEnabled(isEnabled); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 430 | |
| 431 | if (call == null) { |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | updateButtonsState(call); |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Updates the buttons applicable for the UI. |
| 440 | * |
| 441 | * @param call The active call. |
| 442 | */ |
twyen | 00623aa | 2017-10-10 12:15:08 -0700 | [diff] [blame] | 443 | @SuppressWarnings("MissingPermission") |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 444 | private void updateButtonsState(DialerCall call) { |
| 445 | LogUtil.v("CallButtonPresenter.updateButtonsState", ""); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 446 | final boolean isVideo = call.isVideoCall(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 447 | |
| 448 | // Common functionality (audio, hold, etc). |
| 449 | // Show either HOLD or SWAP, but not both. If neither HOLD or SWAP is available: |
| 450 | // (1) If the device normally can hold, show HOLD in a disabled state. |
| 451 | // (2) If the device doesn't have the concept of hold/swap, remove the button. |
| 452 | final boolean showSwap = call.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE); |
| 453 | final boolean showHold = |
| 454 | !showSwap |
| 455 | && call.can(android.telecom.Call.Details.CAPABILITY_SUPPORT_HOLD) |
| 456 | && call.can(android.telecom.Call.Details.CAPABILITY_HOLD); |
| 457 | final boolean isCallOnHold = call.getState() == DialerCall.State.ONHOLD; |
| 458 | |
| 459 | final boolean showAddCall = |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 460 | TelecomAdapter.getInstance().canAddCall() && UserManagerCompat.isUserUnlocked(context); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 461 | final boolean showMerge = call.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 462 | final boolean showUpgradeToVideo = !isVideo && (hasVideoCallCapabilities(call)); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 463 | final boolean showDowngradeToAudio = isVideo && isDowngradeToAudioSupported(call); |
| 464 | final boolean showMute = call.can(android.telecom.Call.Details.CAPABILITY_MUTE); |
| 465 | |
| 466 | final boolean hasCameraPermission = |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 467 | isVideo && VideoUtils.hasCameraPermissionAndShownPrivacyToast(context); |
Eric Erfanian | 938468d | 2017-10-24 14:05:52 -0700 | [diff] [blame] | 468 | // Disabling local video doesn't seem to work when dialing. See a bug. |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 469 | final boolean showPauseVideo = |
| 470 | isVideo |
| 471 | && call.getState() != DialerCall.State.DIALING |
| 472 | && call.getState() != DialerCall.State.CONNECTING; |
| 473 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 474 | otherAccount = TelecomUtil.getOtherAccount(getContext(), call.getAccountHandle()); |
twyen | 38af651 | 2017-11-22 17:12:49 -0800 | [diff] [blame] | 475 | boolean showSwapSim = |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 476 | otherAccount != null |
twyen | 2c6803d | 2017-11-28 11:41:49 -0800 | [diff] [blame] | 477 | && !call.isVoiceMailNumber() |
twyen | 38af651 | 2017-11-22 17:12:49 -0800 | [diff] [blame] | 478 | && DialerCall.State.isDialing(call.getState()) |
| 479 | // Most devices cannot make calls on 2 SIMs at the same time. |
| 480 | && InCallPresenter.getInstance().getCallList().getAllCalls().size() == 1; |
twyen | 00623aa | 2017-10-10 12:15:08 -0700 | [diff] [blame] | 481 | |
wangqi | f6be617 | 2018-03-30 15:57:56 -0700 | [diff] [blame^] | 482 | boolean showUpgradeToRtt = TelecomUtil.isRttEnabled(context) && call.canUpgradeToRttCall(); |
| 483 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 484 | inCallButtonUi.showButton(InCallButtonIds.BUTTON_AUDIO, true); |
| 485 | inCallButtonUi.showButton(InCallButtonIds.BUTTON_SWAP, showSwap); |
| 486 | inCallButtonUi.showButton(InCallButtonIds.BUTTON_HOLD, showHold); |
| 487 | inCallButtonUi.setHold(isCallOnHold); |
| 488 | inCallButtonUi.showButton(InCallButtonIds.BUTTON_MUTE, showMute); |
| 489 | inCallButtonUi.showButton(InCallButtonIds.BUTTON_SWAP_SIM, showSwapSim); |
| 490 | inCallButtonUi.showButton(InCallButtonIds.BUTTON_ADD_CALL, true); |
| 491 | inCallButtonUi.enableButton(InCallButtonIds.BUTTON_ADD_CALL, showAddCall); |
| 492 | inCallButtonUi.showButton(InCallButtonIds.BUTTON_UPGRADE_TO_VIDEO, showUpgradeToVideo); |
wangqi | f6be617 | 2018-03-30 15:57:56 -0700 | [diff] [blame^] | 493 | inCallButtonUi.showButton(InCallButtonIds.BUTTON_UPGRADE_TO_RTT, showUpgradeToRtt); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 494 | inCallButtonUi.showButton(InCallButtonIds.BUTTON_DOWNGRADE_TO_AUDIO, showDowngradeToAudio); |
| 495 | inCallButtonUi.showButton( |
roldenburg | f15085f | 2017-11-06 12:02:06 -0800 | [diff] [blame] | 496 | InCallButtonIds.BUTTON_SWITCH_CAMERA, |
| 497 | isVideo && hasCameraPermission && call.getVideoTech().isTransmitting()); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 498 | inCallButtonUi.showButton(InCallButtonIds.BUTTON_PAUSE_VIDEO, showPauseVideo); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 499 | if (isVideo) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 500 | inCallButtonUi.setVideoPaused(!call.getVideoTech().isTransmitting() || !hasCameraPermission); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 501 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 502 | inCallButtonUi.showButton(InCallButtonIds.BUTTON_DIALPAD, true); |
| 503 | inCallButtonUi.showButton(InCallButtonIds.BUTTON_MERGE, showMerge); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 504 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 505 | inCallButtonUi.updateButtonStates(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | private boolean hasVideoCallCapabilities(DialerCall call) { |
roldenburg | 6bd612f | 2018-01-18 12:57:19 -0800 | [diff] [blame] | 509 | return call.getVideoTech().isAvailable(context, call.getAccountHandle()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Determines if downgrading from a video call to an audio-only call is supported. In order to |
| 514 | * support downgrade to audio, the SDK version must be >= N and the call should NOT have the |
| 515 | * {@link android.telecom.Call.Details#CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO}. |
| 516 | * |
| 517 | * @param call The call. |
| 518 | * @return {@code true} if downgrading to an audio-only call from a video call is supported. |
| 519 | */ |
| 520 | private boolean isDowngradeToAudioSupported(DialerCall call) { |
Eric Erfanian | 938468d | 2017-10-24 14:05:52 -0700 | [diff] [blame] | 521 | // TODO(a bug): If there is an RCS video share session, return true here |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 522 | return !call.can(CallCompat.Details.CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO); |
| 523 | } |
| 524 | |
| 525 | @Override |
| 526 | public void refreshMuteState() { |
| 527 | // Restore the previous mute state |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 528 | if (automaticallyMuted |
| 529 | && AudioModeProvider.getInstance().getAudioState().isMuted() != previousMuteState) { |
| 530 | if (inCallButtonUi == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 531 | return; |
| 532 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 533 | muteClicked(previousMuteState, false /* clickedByUser */); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 534 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 535 | automaticallyMuted = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | @Override |
| 539 | public void onSaveInstanceState(Bundle outState) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 540 | outState.putBoolean(KEY_AUTOMATICALLY_MUTED, automaticallyMuted); |
| 541 | outState.putBoolean(KEY_PREVIOUS_MUTE_STATE, previousMuteState); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | @Override |
| 545 | public void onRestoreInstanceState(Bundle savedInstanceState) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 546 | automaticallyMuted = savedInstanceState.getBoolean(KEY_AUTOMATICALLY_MUTED, automaticallyMuted); |
| 547 | previousMuteState = savedInstanceState.getBoolean(KEY_PREVIOUS_MUTE_STATE, previousMuteState); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | @Override |
| 551 | public void onCameraPermissionGranted() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 552 | if (call != null) { |
| 553 | updateButtonsState(call); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | @Override |
| 558 | public void onActiveCameraSelectionChanged(boolean isUsingFrontFacingCamera) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 559 | if (inCallButtonUi == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 560 | return; |
| 561 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 562 | inCallButtonUi.setCameraSwitched(!isUsingFrontFacingCamera); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | @Override |
| 566 | public Context getContext() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 567 | return context; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | private InCallActivity getActivity() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 571 | if (inCallButtonUi != null) { |
| 572 | Fragment fragment = inCallButtonUi.getInCallButtonUiFragment(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 573 | if (fragment != null) { |
| 574 | return (InCallActivity) fragment.getActivity(); |
| 575 | } |
| 576 | } |
| 577 | return null; |
| 578 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 579 | } |