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; |
| 20 | import android.content.Intent; |
| 21 | import android.graphics.Point; |
| 22 | import android.os.Bundle; |
| 23 | import android.os.Handler; |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 24 | import android.os.Trace; |
twyen | 8efb495 | 2017-10-06 16:35:54 -0700 | [diff] [blame] | 25 | import android.support.annotation.MainThread; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 26 | import android.support.annotation.NonNull; |
| 27 | import android.support.annotation.Nullable; |
| 28 | import android.support.annotation.VisibleForTesting; |
Eric Erfanian | 83b2021 | 2017-05-31 08:53:10 -0700 | [diff] [blame] | 29 | import android.support.v4.os.UserManagerCompat; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 30 | import android.telecom.Call.Details; |
yueg | 77cb8e5 | 2017-10-27 16:42:51 -0700 | [diff] [blame] | 31 | import android.telecom.CallAudioState; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 32 | import android.telecom.DisconnectCause; |
| 33 | import android.telecom.PhoneAccount; |
| 34 | import android.telecom.PhoneAccountHandle; |
| 35 | import android.telecom.TelecomManager; |
| 36 | import android.telecom.VideoProfile; |
| 37 | import android.telephony.PhoneStateListener; |
| 38 | import android.telephony.TelephonyManager; |
twyen | 8efb495 | 2017-10-06 16:35:54 -0700 | [diff] [blame] | 39 | import android.util.ArraySet; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 40 | import android.view.Window; |
| 41 | import android.view.WindowManager; |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 42 | import android.widget.Toast; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 43 | import com.android.contacts.common.compat.CallCompat; |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 44 | import com.android.dialer.CallConfiguration; |
| 45 | import com.android.dialer.Mode; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 46 | import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler; |
| 47 | import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler.OnCheckBlockedListener; |
Eric Erfanian | 83b2021 | 2017-05-31 08:53:10 -0700 | [diff] [blame] | 48 | import com.android.dialer.blocking.FilteredNumberCompat; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 49 | import com.android.dialer.blocking.FilteredNumbersUtil; |
twyen | 8efb495 | 2017-10-06 16:35:54 -0700 | [diff] [blame] | 50 | import com.android.dialer.common.Assert; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 51 | import com.android.dialer.common.LogUtil; |
zachh | 6a4cebd | 2017-10-24 17:10:06 -0700 | [diff] [blame] | 52 | import com.android.dialer.common.concurrent.DialerExecutorComponent; |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 53 | import com.android.dialer.enrichedcall.EnrichedCallComponent; |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame] | 54 | import com.android.dialer.location.GeoUtil; |
yueg | b493c3f | 2018-04-27 14:00:12 -0700 | [diff] [blame^] | 55 | import com.android.dialer.logging.DialerImpression; |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 56 | import com.android.dialer.logging.InteractionEvent; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 57 | import com.android.dialer.logging.Logger; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 58 | import com.android.dialer.postcall.PostCall; |
twyen | a4745bd | 2017-12-12 18:40:11 -0800 | [diff] [blame] | 59 | import com.android.dialer.telecom.TelecomCallUtil; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 60 | import com.android.dialer.telecom.TelecomUtil; |
| 61 | import com.android.dialer.util.TouchPointManager; |
| 62 | import com.android.incallui.InCallOrientationEventListener.ScreenOrientation; |
| 63 | import com.android.incallui.answerproximitysensor.PseudoScreenState; |
yueg | 77cb8e5 | 2017-10-27 16:42:51 -0700 | [diff] [blame] | 64 | import com.android.incallui.audiomode.AudioModeProvider; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 65 | import com.android.incallui.call.CallList; |
| 66 | import com.android.incallui.call.DialerCall; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 67 | import com.android.incallui.call.ExternalCallList; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 68 | import com.android.incallui.call.TelecomAdapter; |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 69 | import com.android.incallui.disconnectdialog.DisconnectMessage; |
twyen | 8efb495 | 2017-10-06 16:35:54 -0700 | [diff] [blame] | 70 | import com.android.incallui.incalluilock.InCallUiLock; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 71 | import com.android.incallui.latencyreport.LatencyReport; |
| 72 | import com.android.incallui.legacyblocking.BlockedNumberContentObserver; |
| 73 | import com.android.incallui.spam.SpamCallListListener; |
erfanian | d05d899 | 2018-03-20 19:42:26 -0700 | [diff] [blame] | 74 | import com.android.incallui.speakeasy.SpeakEasyCallManager; |
yueg | b47528e | 2018-04-24 12:12:57 -0700 | [diff] [blame] | 75 | import com.android.incallui.telecomeventui.InternationalCallOnWifiDialogActivity; |
| 76 | import com.android.incallui.telecomeventui.InternationalCallOnWifiDialogFragment; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 77 | import com.android.incallui.videosurface.bindings.VideoSurfaceBindings; |
| 78 | import com.android.incallui.videosurface.protocol.VideoSurfaceTexture; |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 79 | import com.android.incallui.videotech.utils.VideoUtils; |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 80 | import com.google.protobuf.InvalidProtocolBufferException; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 81 | import java.util.Collections; |
| 82 | import java.util.List; |
| 83 | import java.util.Objects; |
| 84 | import java.util.Set; |
| 85 | import java.util.concurrent.ConcurrentHashMap; |
| 86 | import java.util.concurrent.CopyOnWriteArrayList; |
| 87 | import java.util.concurrent.atomic.AtomicBoolean; |
| 88 | |
| 89 | /** |
| 90 | * Takes updates from the CallList and notifies the InCallActivity (UI) of the changes. Responsible |
| 91 | * for starting the activity for a new call and finishing the activity when all calls are |
| 92 | * disconnected. Creates and manages the in-call state and provides a listener pattern for the |
| 93 | * presenters that want to listen in on the in-call state changes. TODO: This class has become more |
| 94 | * of a state machine at this point. Consider renaming. |
| 95 | */ |
yueg | 77cb8e5 | 2017-10-27 16:42:51 -0700 | [diff] [blame] | 96 | public class InCallPresenter implements CallList.Listener, AudioModeProvider.AudioModeListener { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 97 | private static final String PIXEL2017_SYSTEM_FEATURE = |
| 98 | "com.google.android.feature.PIXEL_2017_EXPERIENCE"; |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 99 | private static final String CALL_CONFIGURATION_EXTRA = "call_configuration"; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 100 | |
| 101 | private static final long BLOCK_QUERY_TIMEOUT_MS = 1000; |
| 102 | |
| 103 | private static final Bundle EMPTY_EXTRAS = new Bundle(); |
| 104 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 105 | private static InCallPresenter inCallPresenter; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is load factor before |
| 109 | * resizing, 1 means we only expect a single thread to access the map so make only a single shard |
| 110 | */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 111 | private final Set<InCallStateListener> listeners = |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 112 | Collections.newSetFromMap(new ConcurrentHashMap<InCallStateListener, Boolean>(8, 0.9f, 1)); |
| 113 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 114 | private final List<IncomingCallListener> incomingCallListeners = new CopyOnWriteArrayList<>(); |
| 115 | private final Set<InCallDetailsListener> detailsListeners = |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 116 | Collections.newSetFromMap(new ConcurrentHashMap<InCallDetailsListener, Boolean>(8, 0.9f, 1)); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 117 | private final Set<CanAddCallListener> canAddCallListeners = |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 118 | Collections.newSetFromMap(new ConcurrentHashMap<CanAddCallListener, Boolean>(8, 0.9f, 1)); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 119 | private final Set<InCallUiListener> inCallUiListeners = |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 120 | Collections.newSetFromMap(new ConcurrentHashMap<InCallUiListener, Boolean>(8, 0.9f, 1)); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 121 | private final Set<InCallOrientationListener> orientationListeners = |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 122 | Collections.newSetFromMap( |
| 123 | new ConcurrentHashMap<InCallOrientationListener, Boolean>(8, 0.9f, 1)); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 124 | private final Set<InCallEventListener> inCallEventListeners = |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 125 | Collections.newSetFromMap(new ConcurrentHashMap<InCallEventListener, Boolean>(8, 0.9f, 1)); |
| 126 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 127 | private StatusBarNotifier statusBarNotifier; |
| 128 | private ExternalCallNotifier externalCallNotifier; |
| 129 | private ContactInfoCache contactInfoCache; |
| 130 | private Context context; |
| 131 | private final OnCheckBlockedListener onCheckBlockedListener = |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 132 | new OnCheckBlockedListener() { |
| 133 | @Override |
| 134 | public void onCheckComplete(final Integer id) { |
| 135 | if (id != null && id != FilteredNumberAsyncQueryHandler.INVALID_ID) { |
| 136 | // Silence the ringer now to prevent ringing and vibration before the call is |
| 137 | // terminated when Telecom attempts to add it. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 138 | TelecomUtil.silenceRinger(context); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | }; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 142 | private CallList callList; |
| 143 | private ExternalCallList externalCallList; |
| 144 | private InCallActivity inCallActivity; |
| 145 | private ManageConferenceActivity manageConferenceActivity; |
| 146 | private final android.telecom.Call.Callback callCallback = |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 147 | new android.telecom.Call.Callback() { |
| 148 | @Override |
| 149 | public void onPostDialWait( |
| 150 | android.telecom.Call telecomCall, String remainingPostDialSequence) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 151 | final DialerCall call = callList.getDialerCallFromTelecomCall(telecomCall); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 152 | if (call == null) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 153 | LogUtil.w( |
| 154 | "InCallPresenter.onPostDialWait", |
| 155 | "DialerCall not found in call list: " + telecomCall); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | onPostDialCharWait(call.getId(), remainingPostDialSequence); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void onDetailsChanged( |
| 163 | android.telecom.Call telecomCall, android.telecom.Call.Details details) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 164 | final DialerCall call = callList.getDialerCallFromTelecomCall(telecomCall); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 165 | if (call == null) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 166 | LogUtil.w( |
| 167 | "InCallPresenter.onDetailsChanged", |
| 168 | "DialerCall not found in call list: " + telecomCall); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | |
| 172 | if (details.hasProperty(Details.PROPERTY_IS_EXTERNAL_CALL) |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 173 | && !externalCallList.isCallTracked(telecomCall)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 174 | |
| 175 | // A regular call became an external call so swap call lists. |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 176 | LogUtil.i("InCallPresenter.onDetailsChanged", "Call became external: " + telecomCall); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 177 | callList.onInternalCallMadeExternal(context, telecomCall); |
| 178 | externalCallList.onCallAdded(telecomCall); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 179 | return; |
| 180 | } |
| 181 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 182 | for (InCallDetailsListener listener : detailsListeners) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 183 | listener.onDetailsChanged(call, details); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void onConferenceableCallsChanged( |
| 189 | android.telecom.Call telecomCall, List<android.telecom.Call> conferenceableCalls) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 190 | LogUtil.i( |
| 191 | "InCallPresenter.onConferenceableCallsChanged", |
| 192 | "onConferenceableCallsChanged: " + telecomCall); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 193 | onDetailsChanged(telecomCall, telecomCall.getDetails()); |
| 194 | } |
| 195 | }; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 196 | private InCallState inCallState = InCallState.NO_CALLS; |
| 197 | private ProximitySensor proximitySensor; |
| 198 | private final PseudoScreenState pseudoScreenState = new PseudoScreenState(); |
| 199 | private boolean serviceConnected; |
| 200 | private InCallCameraManager inCallCameraManager; |
| 201 | private FilteredNumberAsyncQueryHandler filteredQueryHandler; |
| 202 | private CallList.Listener spamCallListListener; |
twyen | 27c3718 | 2018-04-24 11:48:12 -0700 | [diff] [blame] | 203 | private CallList.Listener activeCallsListener; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 204 | /** Whether or not we are currently bound and waiting for Telecom to send us a new call. */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 205 | private boolean boundAndWaitingForOutgoingCall; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 206 | /** Determines if the InCall UI is in fullscreen mode or not. */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 207 | private boolean isFullScreen = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 208 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 209 | private boolean screenTimeoutEnabled = true; |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 210 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 211 | private PhoneStateListener phoneStateListener = |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 212 | new PhoneStateListener() { |
| 213 | @Override |
| 214 | public void onCallStateChanged(int state, String incomingNumber) { |
| 215 | if (state == TelephonyManager.CALL_STATE_RINGING) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 216 | if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | // Check if the number is blocked, to silence the ringer. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 220 | String countryIso = GeoUtil.getCurrentCountryIso(context); |
| 221 | filteredQueryHandler.isBlockedNumber( |
| 222 | onCheckBlockedListener, incomingNumber, countryIso); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | }; |
twyen | a4745bd | 2017-12-12 18:40:11 -0800 | [diff] [blame] | 226 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 227 | /** Whether or not InCallService is bound to Telecom. */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 228 | private boolean serviceBound = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 229 | |
| 230 | /** |
| 231 | * When configuration changes Android kills the current activity and starts a new one. The flag is |
| 232 | * used to check if full clean up is necessary (activity is stopped and new activity won't be |
| 233 | * started), or if a new activity will be started right after the current one is destroyed, and |
| 234 | * therefore no need in release all resources. |
| 235 | */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 236 | private boolean isChangingConfigurations = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 237 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 238 | private boolean awaitingCallListUpdate = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 239 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 240 | private ExternalCallList.ExternalCallListener externalCallListener = |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 241 | new ExternalCallList.ExternalCallListener() { |
| 242 | |
| 243 | @Override |
| 244 | public void onExternalCallPulled(android.telecom.Call call) { |
| 245 | // Note: keep this code in sync with InCallPresenter#onCallAdded |
| 246 | LatencyReport latencyReport = new LatencyReport(call); |
| 247 | latencyReport.onCallBlockingDone(); |
| 248 | // Note: External calls do not require spam checking. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 249 | callList.onCallAdded(context, call, latencyReport); |
| 250 | call.registerCallback(callCallback); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public void onExternalCallAdded(android.telecom.Call call) { |
| 255 | // No-op |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void onExternalCallRemoved(android.telecom.Call call) { |
| 260 | // No-op |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public void onExternalCallUpdated(android.telecom.Call call) { |
| 265 | // No-op |
| 266 | } |
| 267 | }; |
| 268 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 269 | private ThemeColorManager themeColorManager; |
| 270 | private VideoSurfaceTexture localVideoSurfaceTexture; |
| 271 | private VideoSurfaceTexture remoteVideoSurfaceTexture; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 272 | |
erfanian | d05d899 | 2018-03-20 19:42:26 -0700 | [diff] [blame] | 273 | private SpeakEasyCallManager speakEasyCallManager; |
| 274 | |
yueg | 8e54486 | 2018-04-06 14:34:59 -0700 | [diff] [blame] | 275 | private boolean audioRouteSetForBubbleMode; |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 276 | |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame] | 277 | /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */ |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 278 | @VisibleForTesting |
| 279 | InCallPresenter() {} |
| 280 | |
| 281 | public static synchronized InCallPresenter getInstance() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 282 | if (inCallPresenter == null) { |
wangqi | c8cf79e | 2017-10-17 09:21:00 -0700 | [diff] [blame] | 283 | Trace.beginSection("InCallPresenter.Constructor"); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 284 | inCallPresenter = new InCallPresenter(); |
wangqi | c8cf79e | 2017-10-17 09:21:00 -0700 | [diff] [blame] | 285 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 286 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 287 | return inCallPresenter; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame] | 290 | @VisibleForTesting |
| 291 | public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 292 | InCallPresenter.inCallPresenter = inCallPresenter; |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 295 | /** |
| 296 | * Determines whether or not a call has no valid phone accounts that can be used to make the call |
| 297 | * with. Emergency calls do not require a phone account. |
| 298 | * |
| 299 | * @param call to check accounts for. |
| 300 | * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the |
| 301 | * call contains a phone account that could be used to initiate it with, or is an emergency |
| 302 | * call. |
| 303 | */ |
| 304 | public static boolean isCallWithNoValidAccounts(DialerCall call) { |
| 305 | if (call != null && !call.isEmergencyCall()) { |
| 306 | Bundle extras = call.getIntentExtras(); |
| 307 | |
| 308 | if (extras == null) { |
| 309 | extras = EMPTY_EXTRAS; |
| 310 | } |
| 311 | |
| 312 | final List<PhoneAccountHandle> phoneAccountHandles = |
| 313 | extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS); |
| 314 | |
| 315 | if ((call.getAccountHandle() == null |
| 316 | && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 317 | LogUtil.i( |
| 318 | "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 319 | return true; |
| 320 | } |
| 321 | } |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | public InCallState getInCallState() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 326 | return inCallState; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | public CallList getCallList() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 330 | return callList; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | public void setUp( |
| 334 | @NonNull Context context, |
| 335 | CallList callList, |
| 336 | ExternalCallList externalCallList, |
| 337 | StatusBarNotifier statusBarNotifier, |
| 338 | ExternalCallNotifier externalCallNotifier, |
| 339 | ContactInfoCache contactInfoCache, |
Eric Erfanian | 83b2021 | 2017-05-31 08:53:10 -0700 | [diff] [blame] | 340 | ProximitySensor proximitySensor, |
erfanian | d05d899 | 2018-03-20 19:42:26 -0700 | [diff] [blame] | 341 | FilteredNumberAsyncQueryHandler filteredNumberQueryHandler, |
yueg | 4613e8f | 2018-04-23 13:35:33 -0700 | [diff] [blame] | 342 | @NonNull SpeakEasyCallManager speakEasyCallManager) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 343 | Trace.beginSection("InCallPresenter.setUp"); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 344 | if (serviceConnected) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 345 | LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one."); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 346 | if (context != this.context || callList != this.callList) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 347 | throw new IllegalStateException(); |
| 348 | } |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 349 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 350 | return; |
| 351 | } |
| 352 | |
| 353 | Objects.requireNonNull(context); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 354 | this.context = context; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 355 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 356 | this.contactInfoCache = contactInfoCache; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 357 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 358 | this.statusBarNotifier = statusBarNotifier; |
| 359 | this.externalCallNotifier = externalCallNotifier; |
| 360 | addListener(this.statusBarNotifier); |
| 361 | EnrichedCallComponent.get(this.context) |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 362 | .getEnrichedCallManager() |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 363 | .registerStateChangedListener(this.statusBarNotifier); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 364 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 365 | this.proximitySensor = proximitySensor; |
| 366 | addListener(this.proximitySensor); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 367 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 368 | if (themeColorManager == null) { |
| 369 | themeColorManager = |
| 370 | new ThemeColorManager(new InCallUIMaterialColorMapUtils(this.context.getResources())); |
wangqi | 8d662ca | 2017-10-26 11:27:19 -0700 | [diff] [blame] | 371 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 372 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 373 | this.callList = callList; |
| 374 | this.externalCallList = externalCallList; |
| 375 | externalCallList.addExternalCallListener(this.externalCallNotifier); |
| 376 | externalCallList.addExternalCallListener(externalCallListener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 377 | |
| 378 | // This only gets called by the service so this is okay. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 379 | serviceConnected = true; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 380 | |
| 381 | // The final thing we do in this set up is add ourselves as a listener to CallList. This |
| 382 | // will kick off an update and the whole process can start. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 383 | this.callList.addListener(this); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 384 | |
| 385 | // Create spam call list listener and add it to the list of listeners |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 386 | spamCallListListener = |
zachh | 6a4cebd | 2017-10-24 17:10:06 -0700 | [diff] [blame] | 387 | new SpamCallListListener( |
| 388 | context, DialerExecutorComponent.get(context).dialerExecutorFactory()); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 389 | this.callList.addListener(spamCallListListener); |
twyen | 27c3718 | 2018-04-24 11:48:12 -0700 | [diff] [blame] | 390 | activeCallsListener = new ActiveCallsCallListListener(context); |
| 391 | this.callList.addListener(activeCallsListener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 392 | |
| 393 | VideoPauseController.getInstance().setUp(this); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 394 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 395 | filteredQueryHandler = filteredNumberQueryHandler; |
erfanian | d05d899 | 2018-03-20 19:42:26 -0700 | [diff] [blame] | 396 | this.speakEasyCallManager = speakEasyCallManager; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 397 | this.context |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 398 | .getSystemService(TelephonyManager.class) |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 399 | .listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 400 | |
yueg | 77cb8e5 | 2017-10-27 16:42:51 -0700 | [diff] [blame] | 401 | AudioModeProvider.getInstance().addListener(this); |
| 402 | |
roldenburg | 4209901 | 2018-04-09 14:47:15 -0700 | [diff] [blame] | 403 | // Add listener to notify Telephony process when the incoming call screen is started or |
| 404 | // finished. This is for hiding USSD dialog because the incoming call screen should have |
| 405 | // higher precedence over this dialog. |
| 406 | MotorolaInCallUiNotifier motorolaInCallUiNotifier = new MotorolaInCallUiNotifier(context); |
| 407 | addInCallUiListener(motorolaInCallUiNotifier); |
| 408 | addListener(motorolaInCallUiNotifier); |
yueg | 7f5acbe | 2018-01-10 13:50:29 -0800 | [diff] [blame] | 409 | |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 410 | LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp"); |
| 411 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /** |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 415 | * Return whether we should start call in bubble mode and not show InCallActivity. The call mode |
| 416 | * should be set in CallConfiguration in EXTRA_OUTGOING_CALL_EXTRAS when starting a call intent. |
| 417 | */ |
yueg | 4613e8f | 2018-04-23 13:35:33 -0700 | [diff] [blame] | 418 | private boolean shouldStartInBubbleMode() { |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 419 | if (!ReturnToCallController.isEnabled(context)) { |
| 420 | return false; |
| 421 | } |
yueg | 4613e8f | 2018-04-23 13:35:33 -0700 | [diff] [blame] | 422 | |
| 423 | // We only start in Bubble mode for outgoing call |
| 424 | DialerCall dialerCall = callList.getPendingOutgoingCall(); |
| 425 | if (dialerCall == null) { |
| 426 | dialerCall = callList.getOutgoingCall(); |
| 427 | } |
| 428 | if (dialerCall == null) { |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 429 | return false; |
| 430 | } |
yueg | 4613e8f | 2018-04-23 13:35:33 -0700 | [diff] [blame] | 431 | |
| 432 | Bundle extras = dialerCall.getIntentExtras(); |
yueg | b493c3f | 2018-04-27 14:00:12 -0700 | [diff] [blame^] | 433 | boolean result = shouldStartInBubbleModeWithExtras(extras); |
| 434 | if (result) { |
| 435 | Logger.get(context) |
| 436 | .logCallImpression( |
| 437 | DialerImpression.Type.START_CALL_IN_BUBBLE_MODE, |
| 438 | dialerCall.getUniqueCallId(), |
| 439 | dialerCall.getTimeAddedMs()); |
| 440 | } |
| 441 | return result; |
yueg | 4613e8f | 2018-04-23 13:35:33 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | private boolean shouldStartInBubbleModeWithExtras(Bundle outgoingExtras) { |
| 445 | if (!ReturnToCallController.isEnabled(context)) { |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | if (outgoingExtras == null) { |
| 450 | return false; |
| 451 | } |
| 452 | byte[] callConfigurationByteArray = outgoingExtras.getByteArray(CALL_CONFIGURATION_EXTRA); |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 453 | if (callConfigurationByteArray == null) { |
| 454 | return false; |
| 455 | } |
| 456 | try { |
| 457 | CallConfiguration callConfiguration = CallConfiguration.parseFrom(callConfigurationByteArray); |
| 458 | LogUtil.i( |
| 459 | "InCallPresenter.shouldStartInBubbleMode", |
| 460 | "call mode: " + callConfiguration.getCallMode()); |
| 461 | return callConfiguration.getCallMode() == Mode.BUBBLE; |
| 462 | } catch (InvalidProtocolBufferException e) { |
| 463 | return false; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | /** |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 468 | * Called when the telephony service has disconnected from us. This will happen when there are no |
| 469 | * more active calls. However, we may still want to continue showing the UI for certain cases like |
| 470 | * showing "Call Ended". What we really want is to wait for the activity and the service to both |
| 471 | * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a |
| 472 | * secondary method that performs the aforementioned logic. |
| 473 | */ |
| 474 | public void tearDown() { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 475 | LogUtil.d("InCallPresenter.tearDown", "tearDown"); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 476 | callList.clearOnDisconnect(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 477 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 478 | serviceConnected = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 479 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 480 | context |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 481 | .getSystemService(TelephonyManager.class) |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 482 | .listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 483 | |
| 484 | attemptCleanup(); |
| 485 | VideoPauseController.getInstance().tearDown(); |
yueg | 77cb8e5 | 2017-10-27 16:42:51 -0700 | [diff] [blame] | 486 | AudioModeProvider.getInstance().removeListener(this); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | private void attemptFinishActivity() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 490 | screenTimeoutEnabled = true; |
| 491 | final boolean doFinish = (inCallActivity != null && isActivityStarted()); |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 492 | LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 493 | if (doFinish) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 494 | inCallActivity.setExcludeFromRecents(true); |
| 495 | inCallActivity.finish(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()} |
| 501 | * for more insight on the tear-down process. |
| 502 | */ |
| 503 | public void unsetActivity(InCallActivity inCallActivity) { |
| 504 | if (inCallActivity == null) { |
| 505 | throw new IllegalArgumentException("unregisterActivity cannot be called with null"); |
| 506 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 507 | if (this.inCallActivity == null) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 508 | LogUtil.i( |
| 509 | "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset."); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 510 | return; |
| 511 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 512 | if (this.inCallActivity != inCallActivity) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 513 | LogUtil.w( |
| 514 | "InCallPresenter.unsetActivity", |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 515 | "Second instance of InCallActivity is trying to unregister when another" |
| 516 | + " instance is active. Ignoring."); |
| 517 | return; |
| 518 | } |
| 519 | updateActivity(null); |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null} |
| 524 | * activity is provided, it means that the activity was finished and we should attempt to cleanup. |
| 525 | */ |
| 526 | private void updateActivity(InCallActivity inCallActivity) { |
wangqi | 9982f0d | 2017-10-11 17:46:07 -0700 | [diff] [blame] | 527 | Trace.beginSection("InCallPresenter.updateActivity"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 528 | boolean updateListeners = false; |
| 529 | boolean doAttemptCleanup = false; |
| 530 | |
| 531 | if (inCallActivity != null) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 532 | if (this.inCallActivity == null) { |
| 533 | context = inCallActivity.getApplicationContext(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 534 | updateListeners = true; |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 535 | LogUtil.i("InCallPresenter.updateActivity", "UI Initialized"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 536 | } else { |
| 537 | // since setActivity is called onStart(), it can be called multiple times. |
| 538 | // This is fine and ignorable, but we do not want to update the world every time |
| 539 | // this happens (like going to/from background) so we do not set updateListeners. |
| 540 | } |
| 541 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 542 | this.inCallActivity = inCallActivity; |
| 543 | this.inCallActivity.setExcludeFromRecents(false); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 544 | |
| 545 | // By the time the UI finally comes up, the call may already be disconnected. |
| 546 | // If that's the case, we may need to show an error dialog. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 547 | if (callList != null && callList.getDisconnectedCall() != null) { |
| 548 | showDialogOrToastForDisconnectedCall(callList.getDisconnectedCall()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | // When the UI comes up, we need to first check the in-call state. |
| 552 | // If we are showing NO_CALLS, that means that a call probably connected and |
| 553 | // then immediately disconnected before the UI was able to come up. |
| 554 | // If we dont have any calls, start tearing down the UI instead. |
| 555 | // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after |
| 556 | // it has been set. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 557 | if (inCallState == InCallState.NO_CALLS) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 558 | LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 559 | attemptFinishActivity(); |
wangqi | 9982f0d | 2017-10-11 17:46:07 -0700 | [diff] [blame] | 560 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 561 | return; |
| 562 | } |
| 563 | } else { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 564 | LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 565 | updateListeners = true; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 566 | this.inCallActivity = null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 567 | |
| 568 | // We attempt cleanup for the destroy case but only after we recalculate the state |
| 569 | // to see if we need to come back up or stay shut down. This is why we do the |
| 570 | // cleanup after the call to onCallListChange() instead of directly here. |
| 571 | doAttemptCleanup = true; |
| 572 | } |
| 573 | |
| 574 | // Messages can come from the telephony layer while the activity is coming up |
| 575 | // and while the activity is going down. So in both cases we need to recalculate what |
| 576 | // state we should be in after they complete. |
| 577 | // Examples: (1) A new incoming call could come in and then get disconnected before |
| 578 | // the activity is created. |
| 579 | // (2) All calls could disconnect and then get a new incoming call before the |
| 580 | // activity is destroyed. |
| 581 | // |
Eric Erfanian | 938468d | 2017-10-24 14:05:52 -0700 | [diff] [blame] | 582 | // a bug - We previously had a check for mServiceConnected here as well, but there are |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 583 | // cases where we need to recalculate the current state even if the service in not |
| 584 | // connected. In particular the case where startOrFinish() is called while the app is |
| 585 | // already finish()ing. In that case, we skip updating the state with the knowledge that |
| 586 | // we will check again once the activity has finished. That means we have to recalculate the |
| 587 | // state here even if the service is disconnected since we may not have finished a state |
| 588 | // transition while finish()ing. |
| 589 | if (updateListeners) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 590 | onCallListChange(callList); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | if (doAttemptCleanup) { |
| 594 | attemptCleanup(); |
| 595 | } |
wangqi | 9982f0d | 2017-10-11 17:46:07 -0700 | [diff] [blame] | 596 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 597 | } |
| 598 | |
erfanian | 12243de | 2018-04-17 12:17:08 -0700 | [diff] [blame] | 599 | public SpeakEasyCallManager getSpeakEasyCallManager() { |
| 600 | return this.speakEasyCallManager; |
| 601 | } |
| 602 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 603 | public void setManageConferenceActivity( |
| 604 | @Nullable ManageConferenceActivity manageConferenceActivity) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 605 | this.manageConferenceActivity = manageConferenceActivity; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | public void onBringToForeground(boolean showDialpad) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 609 | LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground."); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 610 | bringToForeground(showDialpad); |
| 611 | } |
| 612 | |
| 613 | public void onCallAdded(final android.telecom.Call call) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 614 | Trace.beginSection("InCallPresenter.onCallAdded"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 615 | LatencyReport latencyReport = new LatencyReport(call); |
| 616 | if (shouldAttemptBlocking(call)) { |
| 617 | maybeBlockCall(call, latencyReport); |
| 618 | } else { |
| 619 | if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 620 | externalCallList.onCallAdded(call); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 621 | } else { |
| 622 | latencyReport.onCallBlockingDone(); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 623 | callList.onCallAdded(context, call, latencyReport); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | // Since a call has been added we are no longer waiting for Telecom to send us a call. |
| 628 | setBoundAndWaitingForOutgoingCall(false, null); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 629 | call.registerCallback(callCallback); |
zachh | 78e54ac | 2017-12-05 16:38:35 -0800 | [diff] [blame] | 630 | // TODO(maxwelb): Return the future in recordPhoneLookupInfo and propagate. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 631 | PhoneLookupHistoryRecorder.recordPhoneLookupInfo(context.getApplicationContext(), call); |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 632 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | private boolean shouldAttemptBlocking(android.telecom.Call call) { |
| 636 | if (call.getState() != android.telecom.Call.STATE_RINGING) { |
| 637 | return false; |
| 638 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 639 | if (!UserManagerCompat.isUserUnlocked(context)) { |
Eric Erfanian | 83b2021 | 2017-05-31 08:53:10 -0700 | [diff] [blame] | 640 | LogUtil.i( |
| 641 | "InCallPresenter.shouldAttemptBlocking", |
| 642 | "not attempting to block incoming call because user is locked"); |
| 643 | return false; |
| 644 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 645 | if (TelecomCallUtil.isEmergencyCall(call)) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 646 | LogUtil.i( |
| 647 | "InCallPresenter.shouldAttemptBlocking", |
| 648 | "Not attempting to block incoming emergency call"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 649 | return false; |
| 650 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 651 | if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 652 | LogUtil.i( |
| 653 | "InCallPresenter.shouldAttemptBlocking", |
| 654 | "Not attempting to block incoming call due to recent emergency call"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 655 | return false; |
| 656 | } |
| 657 | if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) { |
| 658 | return false; |
| 659 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 660 | if (FilteredNumberCompat.useNewFiltering(context)) { |
Eric Erfanian | 83b2021 | 2017-05-31 08:53:10 -0700 | [diff] [blame] | 661 | LogUtil.i( |
| 662 | "InCallPresenter.shouldAttemptBlocking", |
| 663 | "not attempting to block incoming call because framework blocking is in use"); |
| 664 | return false; |
| 665 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 666 | return true; |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to |
| 671 | * the CallList so it can proceed as normal. There is a timeout, so if the function for checking |
| 672 | * whether a function is blocked does not return in a reasonable time, we proceed with adding the |
| 673 | * call anyways. |
| 674 | */ |
| 675 | private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 676 | final String countryIso = GeoUtil.getCurrentCountryIso(context); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 677 | final String number = TelecomCallUtil.getNumber(call); |
| 678 | final long timeAdded = System.currentTimeMillis(); |
| 679 | |
| 680 | // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the |
| 681 | // main UI thread. It is needed so we can change its value within different scopes, since |
| 682 | // that cannot be done with a final boolean. |
| 683 | final AtomicBoolean hasTimedOut = new AtomicBoolean(false); |
| 684 | |
| 685 | final Handler handler = new Handler(); |
| 686 | |
| 687 | // Proceed if the query is slow; the call may still be blocked after the query returns. |
| 688 | final Runnable runnable = |
| 689 | new Runnable() { |
| 690 | @Override |
| 691 | public void run() { |
| 692 | hasTimedOut.set(true); |
| 693 | latencyReport.onCallBlockingDone(); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 694 | callList.onCallAdded(context, call, latencyReport); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 695 | } |
| 696 | }; |
| 697 | handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS); |
| 698 | |
| 699 | OnCheckBlockedListener onCheckBlockedListener = |
| 700 | new OnCheckBlockedListener() { |
| 701 | @Override |
| 702 | public void onCheckComplete(final Integer id) { |
| 703 | if (isReadyForTearDown()) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 704 | LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 705 | return; |
| 706 | } |
| 707 | if (!hasTimedOut.get()) { |
| 708 | handler.removeCallbacks(runnable); |
| 709 | } |
| 710 | if (id == null) { |
| 711 | if (!hasTimedOut.get()) { |
| 712 | latencyReport.onCallBlockingDone(); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 713 | callList.onCallAdded(context, call, latencyReport); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 714 | } |
| 715 | } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 716 | LogUtil.d( |
| 717 | "InCallPresenter.onCheckComplete", "invalid number, skipping block checking"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 718 | if (!hasTimedOut.get()) { |
| 719 | handler.removeCallbacks(runnable); |
| 720 | |
| 721 | latencyReport.onCallBlockingDone(); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 722 | callList.onCallAdded(context, call, latencyReport); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 723 | } |
| 724 | } else { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 725 | LogUtil.i( |
| 726 | "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 727 | call.reject(false, null); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 728 | Logger.get(context).logInteraction(InteractionEvent.Type.CALL_BLOCKED); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 729 | |
| 730 | /* |
| 731 | * If mContext is null, then the InCallPresenter was torn down before the |
| 732 | * block check had a chance to complete. The context is no longer valid, so |
| 733 | * don't attempt to remove the call log entry. |
| 734 | */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 735 | if (context == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 736 | return; |
| 737 | } |
| 738 | // Register observer to update the call log. |
| 739 | // BlockedNumberContentObserver will unregister after successful log or timeout. |
| 740 | BlockedNumberContentObserver contentObserver = |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 741 | new BlockedNumberContentObserver(context, new Handler(), number, timeAdded); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 742 | contentObserver.register(); |
| 743 | } |
| 744 | } |
| 745 | }; |
| 746 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 747 | filteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | public void onCallRemoved(android.telecom.Call call) { |
| 751 | if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 752 | externalCallList.onCallRemoved(call); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 753 | } else { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 754 | callList.onCallRemoved(context, call); |
| 755 | call.unregisterCallback(callCallback); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
| 759 | public void onCanAddCallChanged(boolean canAddCall) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 760 | for (CanAddCallListener listener : canAddCallListeners) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 761 | listener.onCanAddCallChanged(canAddCall); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | @Override |
| 766 | public void onWiFiToLteHandover(DialerCall call) { |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 767 | if (call.hasShownWiFiToLteHandoverToast()) { |
| 768 | return; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 769 | } |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 770 | |
| 771 | Toast.makeText(context, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG) |
| 772 | .show(); |
| 773 | call.setHasShownWiFiToLteHandoverToast(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | @Override |
| 777 | public void onHandoverToWifiFailed(DialerCall call) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 778 | if (inCallActivity != null) { |
| 779 | inCallActivity.showDialogOrToastForWifiHandoverFailure(call); |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 780 | } else { |
| 781 | Toast.makeText(context, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT) |
| 782 | .show(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
Eric Erfanian | c857f90 | 2017-05-15 14:05:33 -0700 | [diff] [blame] | 786 | @Override |
| 787 | public void onInternationalCallOnWifi(@NonNull DialerCall call) { |
| 788 | LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi"); |
yueg | b47528e | 2018-04-24 12:12:57 -0700 | [diff] [blame] | 789 | |
| 790 | if (!InternationalCallOnWifiDialogFragment.shouldShow(context)) { |
| 791 | LogUtil.i( |
| 792 | "InCallPresenter.onInternationalCallOnWifi", |
| 793 | "InternationalCallOnWifiDialogFragment.shouldShow returned false"); |
| 794 | return; |
| 795 | } |
| 796 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 797 | if (inCallActivity != null) { |
| 798 | inCallActivity.showDialogForInternationalCallOnWifi(call); |
yueg | b47528e | 2018-04-24 12:12:57 -0700 | [diff] [blame] | 799 | } else { |
| 800 | Intent intent = new Intent(context, InternationalCallOnWifiDialogActivity.class); |
| 801 | // Prevent showing MainActivity with InternationalCallOnWifiDialogActivity on above |
| 802 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); |
| 803 | intent.putExtra(InternationalCallOnWifiDialogActivity.EXTRA_CALL_ID, call.getId()); |
| 804 | context.startActivity(intent); |
Eric Erfanian | c857f90 | 2017-05-15 14:05:33 -0700 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 808 | /** |
| 809 | * Called when there is a change to the call list. Sets the In-Call state for the entire in-call |
| 810 | * app based on the information it gets from CallList. Dispatches the in-call state to all |
| 811 | * listeners. Can trigger the creation or destruction of the UI based on the states that is |
| 812 | * calculates. |
| 813 | */ |
| 814 | @Override |
| 815 | public void onCallListChange(CallList callList) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 816 | Trace.beginSection("InCallPresenter.onCallListChange"); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 817 | if (inCallActivity != null && inCallActivity.isInCallScreenAnimating()) { |
| 818 | awaitingCallListUpdate = true; |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 819 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 820 | return; |
| 821 | } |
| 822 | if (callList == null) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 823 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 824 | return; |
| 825 | } |
| 826 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 827 | awaitingCallListUpdate = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 828 | |
| 829 | InCallState newState = getPotentialStateFromCallList(callList); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 830 | InCallState oldState = inCallState; |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 831 | LogUtil.d( |
| 832 | "InCallPresenter.onCallListChange", |
| 833 | "onCallListChange oldState= " + oldState + " newState=" + newState); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 834 | |
| 835 | // If the user placed a call and was asked to choose the account, but then pressed "Home", the |
| 836 | // incall activity for that call will still exist (even if it's not visible). In the case of |
| 837 | // an incoming call in that situation, just disconnect that "waiting for account" call and |
| 838 | // dismiss the dialog. The same activity will be reused to handle the new incoming call. See |
Eric Erfanian | 938468d | 2017-10-24 14:05:52 -0700 | [diff] [blame] | 839 | // a bug for more details. |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 840 | DialerCall waitingForAccountCall; |
| 841 | if (newState == InCallState.INCOMING |
| 842 | && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) { |
| 843 | waitingForAccountCall.disconnect(); |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 844 | // The InCallActivity might be destroyed or not started yet at this point. |
| 845 | if (isActivityStarted()) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 846 | inCallActivity.dismissPendingDialogs(); |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 847 | } |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 850 | newState = startOrFinishUi(newState); |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 851 | LogUtil.d( |
| 852 | "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 853 | |
| 854 | // Set the new state before announcing it to the world |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 855 | LogUtil.i( |
| 856 | "InCallPresenter.onCallListChange", |
| 857 | "Phone switching state: " + oldState + " -> " + newState); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 858 | inCallState = newState; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 859 | |
yueg | b910304 | 2018-03-30 12:12:25 -0700 | [diff] [blame] | 860 | // Foreground call changed |
| 861 | DialerCall primary = null; |
| 862 | if (newState == InCallState.INCOMING) { |
| 863 | primary = callList.getIncomingCall(); |
| 864 | } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) { |
| 865 | primary = callList.getOutgoingCall(); |
| 866 | if (primary == null) { |
| 867 | primary = callList.getPendingOutgoingCall(); |
| 868 | } |
| 869 | } else if (newState == InCallState.INCALL) { |
| 870 | primary = getCallToDisplay(callList, null, false); |
| 871 | } |
| 872 | if (primary != null) { |
| 873 | onForegroundCallChanged(primary); |
| 874 | } |
| 875 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 876 | // notify listeners of new state |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 877 | for (InCallStateListener listener : listeners) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 878 | LogUtil.d( |
| 879 | "InCallPresenter.onCallListChange", |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 880 | "Notify " + listener + " of state " + inCallState.toString()); |
| 881 | listener.onStateChange(oldState, inCallState, callList); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | if (isActivityStarted()) { |
| 885 | final boolean hasCall = |
| 886 | callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 887 | inCallActivity.dismissKeyguard(hasCall); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 888 | } |
twyen | 27c3718 | 2018-04-24 11:48:12 -0700 | [diff] [blame] | 889 | |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 890 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 891 | } |
| 892 | |
yueg | b910304 | 2018-03-30 12:12:25 -0700 | [diff] [blame] | 893 | /** |
| 894 | * Get the highest priority call to display. Goes through the calls and chooses which to return |
| 895 | * based on priority of which type of call to display to the user. Callers can use the "ignore" |
| 896 | * feature to get the second best call by passing a previously found primary call as ignore. |
| 897 | * |
| 898 | * @param ignore A call to ignore if found. |
| 899 | */ |
| 900 | static DialerCall getCallToDisplay( |
| 901 | CallList callList, DialerCall ignore, boolean skipDisconnected) { |
| 902 | // Active calls come second. An active call always gets precedent. |
| 903 | DialerCall retval = callList.getActiveCall(); |
| 904 | if (retval != null && retval != ignore) { |
| 905 | return retval; |
| 906 | } |
| 907 | |
| 908 | // Sometimes there is intemediate state that two calls are in active even one is about |
| 909 | // to be on hold. |
| 910 | retval = callList.getSecondActiveCall(); |
| 911 | if (retval != null && retval != ignore) { |
| 912 | return retval; |
| 913 | } |
| 914 | |
| 915 | // Disconnected calls get primary position if there are no active calls |
| 916 | // to let user know quickly what call has disconnected. Disconnected |
| 917 | // calls are very short lived. |
| 918 | if (!skipDisconnected) { |
| 919 | retval = callList.getDisconnectingCall(); |
| 920 | if (retval != null && retval != ignore) { |
| 921 | return retval; |
| 922 | } |
| 923 | retval = callList.getDisconnectedCall(); |
| 924 | if (retval != null && retval != ignore) { |
| 925 | return retval; |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | // Then we go to background call (calls on hold) |
| 930 | retval = callList.getBackgroundCall(); |
| 931 | if (retval != null && retval != ignore) { |
| 932 | return retval; |
| 933 | } |
| 934 | |
| 935 | // Lastly, we go to a second background call. |
| 936 | retval = callList.getSecondBackgroundCall(); |
| 937 | |
| 938 | return retval; |
| 939 | } |
| 940 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 941 | /** Called when there is a new incoming call. */ |
| 942 | @Override |
| 943 | public void onIncomingCall(DialerCall call) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 944 | Trace.beginSection("InCallPresenter.onIncomingCall"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 945 | InCallState newState = startOrFinishUi(InCallState.INCOMING); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 946 | InCallState oldState = inCallState; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 947 | |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 948 | LogUtil.i( |
| 949 | "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 950 | inCallState = newState; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 951 | |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 952 | Trace.beginSection("listener.onIncomingCall"); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 953 | for (IncomingCallListener listener : incomingCallListeners) { |
| 954 | listener.onIncomingCall(oldState, inCallState, call); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 955 | } |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 956 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 957 | |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 958 | Trace.beginSection("onPrimaryCallStateChanged"); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 959 | if (inCallActivity != null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 960 | // Re-evaluate which fragment is being shown. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 961 | inCallActivity.onPrimaryCallStateChanged(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 962 | } |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 963 | Trace.endSection(); |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 964 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | @Override |
| 968 | public void onUpgradeToVideo(DialerCall call) { |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 969 | if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState()) |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 970 | && inCallState == InCallPresenter.InCallState.INCOMING) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 971 | LogUtil.i( |
| 972 | "InCallPresenter.onUpgradeToVideo", |
| 973 | "rejecting upgrade request due to existing incoming call"); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 974 | call.getVideoTech().declineVideoRequest(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 975 | } |
| 976 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 977 | if (inCallActivity != null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 978 | // Re-evaluate which fragment is being shown. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 979 | inCallActivity.onPrimaryCallStateChanged(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | |
| 983 | @Override |
wangqi | bc28ea7 | 2018-04-02 16:23:00 -0700 | [diff] [blame] | 984 | public void onUpgradeToRtt(DialerCall call, int rttRequestId) { |
| 985 | if (inCallActivity != null) { |
| 986 | inCallActivity.showDialogForRttRequest(call, rttRequestId); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | @Override |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 991 | public void onSessionModificationStateChange(DialerCall call) { |
| 992 | int newState = call.getVideoTech().getSessionModificationState(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 993 | LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 994 | if (proximitySensor == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 995 | LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null"); |
| 996 | return; |
| 997 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 998 | proximitySensor.setIsAttemptingVideoCall( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 999 | call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest()); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1000 | if (inCallActivity != null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1001 | // Re-evaluate which fragment is being shown. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1002 | inCallActivity.onPrimaryCallStateChanged(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * Called when a call becomes disconnected. Called everytime an existing call changes from being |
| 1008 | * connected (incoming/outgoing/active) to disconnected. |
| 1009 | */ |
| 1010 | @Override |
| 1011 | public void onDisconnect(DialerCall call) { |
linyuh | 7b86f56 | 2017-11-16 11:24:09 -0800 | [diff] [blame] | 1012 | showDialogOrToastForDisconnectedCall(call); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1013 | |
| 1014 | // We need to do the run the same code as onCallListChange. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1015 | onCallListChange(callList); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1016 | |
| 1017 | if (isActivityStarted()) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1018 | inCallActivity.dismissKeyguard(false); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | if (call.isEmergencyCall()) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1022 | FilteredNumbersUtil.recordLastEmergencyCallTime(context); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1023 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1024 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1025 | if (!callList.hasLiveCall() |
Eric Erfanian | fc37b02 | 2017-03-21 10:11:17 -0700 | [diff] [blame] | 1026 | && !call.getLogState().isIncoming |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame] | 1027 | && !isSecretCode(call.getNumber()) |
wangqi | 9982f0d | 2017-10-11 17:46:07 -0700 | [diff] [blame] | 1028 | && !call.isVoiceMailNumber()) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1029 | PostCall.onCallDisconnected(context, call.getNumber(), call.getConnectTimeMillis()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1030 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame] | 1033 | private boolean isSecretCode(@Nullable String number) { |
| 1034 | return number != null |
| 1035 | && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*")); |
| 1036 | } |
| 1037 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1038 | /** Given the call list, return the state in which the in-call screen should be. */ |
| 1039 | public InCallState getPotentialStateFromCallList(CallList callList) { |
| 1040 | |
| 1041 | InCallState newState = InCallState.NO_CALLS; |
| 1042 | |
| 1043 | if (callList == null) { |
| 1044 | return newState; |
| 1045 | } |
| 1046 | if (callList.getIncomingCall() != null) { |
| 1047 | newState = InCallState.INCOMING; |
| 1048 | } else if (callList.getWaitingForAccountCall() != null) { |
| 1049 | newState = InCallState.WAITING_FOR_ACCOUNT; |
| 1050 | } else if (callList.getPendingOutgoingCall() != null) { |
| 1051 | newState = InCallState.PENDING_OUTGOING; |
| 1052 | } else if (callList.getOutgoingCall() != null) { |
| 1053 | newState = InCallState.OUTGOING; |
| 1054 | } else if (callList.getActiveCall() != null |
| 1055 | || callList.getBackgroundCall() != null |
| 1056 | || callList.getDisconnectedCall() != null |
| 1057 | || callList.getDisconnectingCall() != null) { |
| 1058 | newState = InCallState.INCALL; |
| 1059 | } |
| 1060 | |
| 1061 | if (newState == InCallState.NO_CALLS) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1062 | if (boundAndWaitingForOutgoingCall) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1063 | return InCallState.PENDING_OUTGOING; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | return newState; |
| 1068 | } |
| 1069 | |
| 1070 | public boolean isBoundAndWaitingForOutgoingCall() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1071 | return boundAndWaitingForOutgoingCall; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1075 | LogUtil.i( |
| 1076 | "InCallPresenter.setBoundAndWaitingForOutgoingCall", |
| 1077 | "setBoundAndWaitingForOutgoingCall: " + isBound); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1078 | boundAndWaitingForOutgoingCall = isBound; |
| 1079 | themeColorManager.setPendingPhoneAccountHandle(handle); |
| 1080 | if (isBound && inCallState == InCallState.NO_CALLS) { |
| 1081 | inCallState = InCallState.PENDING_OUTGOING; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | public void onShrinkAnimationComplete() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1086 | if (awaitingCallListUpdate) { |
| 1087 | onCallListChange(callList); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | public void addIncomingCallListener(IncomingCallListener listener) { |
| 1092 | Objects.requireNonNull(listener); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1093 | incomingCallListeners.add(listener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | public void removeIncomingCallListener(IncomingCallListener listener) { |
| 1097 | if (listener != null) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1098 | incomingCallListeners.remove(listener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | public void addListener(InCallStateListener listener) { |
| 1103 | Objects.requireNonNull(listener); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1104 | listeners.add(listener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | public void removeListener(InCallStateListener listener) { |
| 1108 | if (listener != null) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1109 | listeners.remove(listener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | public void addDetailsListener(InCallDetailsListener listener) { |
| 1114 | Objects.requireNonNull(listener); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1115 | detailsListeners.add(listener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | public void removeDetailsListener(InCallDetailsListener listener) { |
| 1119 | if (listener != null) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1120 | detailsListeners.remove(listener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | public void addCanAddCallListener(CanAddCallListener listener) { |
| 1125 | Objects.requireNonNull(listener); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1126 | canAddCallListeners.add(listener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | public void removeCanAddCallListener(CanAddCallListener listener) { |
| 1130 | if (listener != null) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1131 | canAddCallListeners.remove(listener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | public void addOrientationListener(InCallOrientationListener listener) { |
| 1136 | Objects.requireNonNull(listener); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1137 | orientationListeners.add(listener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | public void removeOrientationListener(InCallOrientationListener listener) { |
| 1141 | if (listener != null) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1142 | orientationListeners.remove(listener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | public void addInCallEventListener(InCallEventListener listener) { |
| 1147 | Objects.requireNonNull(listener); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1148 | inCallEventListeners.add(listener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | public void removeInCallEventListener(InCallEventListener listener) { |
| 1152 | if (listener != null) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1153 | inCallEventListeners.remove(listener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | public ProximitySensor getProximitySensor() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1158 | return proximitySensor; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | public PseudoScreenState getPseudoScreenState() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1162 | return pseudoScreenState; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | /** Returns true if the incall app is the foreground application. */ |
| 1166 | public boolean isShowingInCallUi() { |
| 1167 | if (!isActivityStarted()) { |
| 1168 | return false; |
| 1169 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1170 | if (manageConferenceActivity != null && manageConferenceActivity.isVisible()) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1171 | return true; |
| 1172 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1173 | return inCallActivity.isVisible(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | /** |
| 1177 | * Returns true if the activity has been created and is running. Returns true as long as activity |
| 1178 | * is not destroyed or finishing. This ensures that we return true even if the activity is paused |
| 1179 | * (not in foreground). |
| 1180 | */ |
| 1181 | public boolean isActivityStarted() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1182 | return (inCallActivity != null |
| 1183 | && !inCallActivity.isDestroyed() |
| 1184 | && !inCallActivity.isFinishing()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | /** |
| 1188 | * Determines if the In-Call app is currently changing configuration. |
| 1189 | * |
| 1190 | * @return {@code true} if the In-Call app is changing configuration. |
| 1191 | */ |
| 1192 | public boolean isChangingConfigurations() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1193 | return isChangingConfigurations; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | /** |
| 1197 | * Tracks whether the In-Call app is currently in the process of changing configuration (i.e. |
| 1198 | * screen orientation). |
| 1199 | */ |
| 1200 | /*package*/ |
| 1201 | void updateIsChangingConfigurations() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1202 | isChangingConfigurations = false; |
| 1203 | if (inCallActivity != null) { |
| 1204 | isChangingConfigurations = inCallActivity.isChangingConfigurations(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1205 | } |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1206 | LogUtil.v( |
| 1207 | "InCallPresenter.updateIsChangingConfigurations", |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1208 | "updateIsChangingConfigurations = " + isChangingConfigurations); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1209 | } |
| 1210 | |
yueg | 10f6e82 | 2018-01-17 15:32:18 -0800 | [diff] [blame] | 1211 | /** Called when the activity goes in/out of the foreground. */ |
| 1212 | public void onUiShowing(boolean showing) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1213 | if (proximitySensor != null) { |
| 1214 | proximitySensor.onInCallShowing(showing); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1215 | } |
| 1216 | |
yueg | 092b21c | 2017-11-15 16:20:07 -0800 | [diff] [blame] | 1217 | if (!showing) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1218 | updateIsChangingConfigurations(); |
| 1219 | } |
| 1220 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1221 | for (InCallUiListener listener : inCallUiListeners) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1222 | listener.onUiShowing(showing); |
| 1223 | } |
| 1224 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1225 | if (inCallActivity != null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1226 | // Re-evaluate which fragment is being shown. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1227 | inCallActivity.onPrimaryCallStateChanged(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1231 | public void refreshUi() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1232 | if (inCallActivity != null) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1233 | // Re-evaluate which fragment is being shown. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1234 | inCallActivity.onPrimaryCallStateChanged(); |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1238 | public void addInCallUiListener(InCallUiListener listener) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1239 | inCallUiListeners.add(listener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | public boolean removeInCallUiListener(InCallUiListener listener) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1243 | return inCallUiListeners.remove(listener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | /*package*/ |
| 1247 | void onActivityStarted() { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1248 | LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1249 | notifyVideoPauseController(true); |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1250 | applyScreenTimeout(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | /*package*/ |
| 1254 | void onActivityStopped() { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1255 | LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1256 | notifyVideoPauseController(false); |
| 1257 | } |
| 1258 | |
| 1259 | private void notifyVideoPauseController(boolean showing) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1260 | LogUtil.d( |
| 1261 | "InCallPresenter.notifyVideoPauseController", |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1262 | "mIsChangingConfigurations=" + isChangingConfigurations); |
| 1263 | if (!isChangingConfigurations) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1264 | VideoPauseController.getInstance().onUiShowing(showing); |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | /** Brings the app into the foreground if possible. */ |
| 1269 | public void bringToForeground(boolean showDialpad) { |
| 1270 | // Before we bring the incall UI to the foreground, we check to see if: |
| 1271 | // 1. It is not currently in the foreground |
| 1272 | // 2. We are in a state where we want to show the incall ui (i.e. there are calls to |
| 1273 | // be displayed) |
| 1274 | // If the activity hadn't actually been started previously, yet there are still calls |
| 1275 | // present (e.g. a call was accepted by a bluetooth or wired headset), we want to |
| 1276 | // bring it up the UI regardless. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1277 | if (!isShowingInCallUi() && inCallState != InCallState.NO_CALLS) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1278 | showInCall(showDialpad, false /* newOutgoingCall */); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | public void onPostDialCharWait(String callId, String chars) { |
yueg | a489f51 | 2018-04-25 12:01:09 -0700 | [diff] [blame] | 1283 | // If not visible, inCallActivity is stopped. Starting from P, calling recreate() will destroy |
| 1284 | // the old activity instance and create a new instance immediately. Previously, the old activity |
| 1285 | // went through its lifecycle from create to destroy before creating a new instance. |
| 1286 | // So this case doesn't work now: make a call with char WAIT, leave in call UI, call gets |
| 1287 | // connected, and go back to in call UI to see the dialog. |
| 1288 | // So we should show dialog in an empty activity if inCallActivity is not visible. And it also |
| 1289 | // helps with background calling. |
| 1290 | if (isActivityStarted() && inCallActivity.isVisible()) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1291 | inCallActivity.showDialogForPostCharWait(callId, chars); |
yueg | a489f51 | 2018-04-25 12:01:09 -0700 | [diff] [blame] | 1292 | } else { |
| 1293 | Intent intent = new Intent(context, PostCharDialogActivity.class); |
| 1294 | // Prevent showing MainActivity with PostCharDialogActivity on above |
| 1295 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); |
| 1296 | intent.putExtra(PostCharDialogActivity.EXTRA_CALL_ID, callId); |
| 1297 | intent.putExtra(PostCharDialogActivity.EXTRA_POST_DIAL_STRING, chars); |
| 1298 | context.startActivity(intent); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | /** |
| 1303 | * Handles the green CALL key while in-call. |
| 1304 | * |
| 1305 | * @return true if we consumed the event. |
| 1306 | */ |
| 1307 | public boolean handleCallKey() { |
| 1308 | LogUtil.v("InCallPresenter.handleCallKey", null); |
| 1309 | |
| 1310 | // The green CALL button means either "Answer", "Unhold", or |
| 1311 | // "Swap calls", or can be a no-op, depending on the current state |
| 1312 | // of the Phone. |
| 1313 | |
| 1314 | /** INCOMING CALL */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1315 | final CallList calls = callList; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1316 | final DialerCall incomingCall = calls.getIncomingCall(); |
| 1317 | LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall); |
| 1318 | |
| 1319 | // (1) Attempt to answer a call |
| 1320 | if (incomingCall != null) { |
| 1321 | incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY); |
| 1322 | return true; |
| 1323 | } |
| 1324 | |
| 1325 | /** STATE_ACTIVE CALL */ |
| 1326 | final DialerCall activeCall = calls.getActiveCall(); |
| 1327 | if (activeCall != null) { |
| 1328 | // TODO: This logic is repeated from CallButtonPresenter.java. We should |
| 1329 | // consolidate this logic. |
| 1330 | final boolean canMerge = |
| 1331 | activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE); |
| 1332 | final boolean canSwap = |
| 1333 | activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE); |
| 1334 | |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1335 | LogUtil.v( |
| 1336 | "InCallPresenter.handleCallKey", |
| 1337 | "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1338 | |
| 1339 | // (2) Attempt actions on conference calls |
| 1340 | if (canMerge) { |
| 1341 | TelecomAdapter.getInstance().merge(activeCall.getId()); |
| 1342 | return true; |
| 1343 | } else if (canSwap) { |
| 1344 | TelecomAdapter.getInstance().swap(activeCall.getId()); |
| 1345 | return true; |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | /** BACKGROUND CALL */ |
| 1350 | final DialerCall heldCall = calls.getBackgroundCall(); |
| 1351 | if (heldCall != null) { |
| 1352 | // We have a hold call so presumeable it will always support HOLD...but |
| 1353 | // there is no harm in double checking. |
| 1354 | final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD); |
| 1355 | |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1356 | LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1357 | |
| 1358 | // (4) unhold call |
| 1359 | if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) { |
| 1360 | heldCall.unhold(); |
| 1361 | return true; |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | // Always consume hard keys |
| 1366 | return true; |
| 1367 | } |
| 1368 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1369 | /** Clears the previous fullscreen state. */ |
| 1370 | public void clearFullscreen() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1371 | isFullScreen = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | /** |
| 1375 | * Changes the fullscreen mode of the in-call UI. |
| 1376 | * |
| 1377 | * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false} |
| 1378 | * otherwise. |
| 1379 | */ |
| 1380 | public void setFullScreen(boolean isFullScreen) { |
| 1381 | setFullScreen(isFullScreen, false /* force */); |
| 1382 | } |
| 1383 | |
| 1384 | /** |
| 1385 | * Changes the fullscreen mode of the in-call UI. |
| 1386 | * |
| 1387 | * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false} |
| 1388 | * otherwise. |
| 1389 | * @param force {@code true} if fullscreen mode should be set regardless of its current state. |
| 1390 | */ |
| 1391 | public void setFullScreen(boolean isFullScreen, boolean force) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1392 | LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1393 | |
| 1394 | // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown. |
| 1395 | if (isDialpadVisible()) { |
| 1396 | isFullScreen = false; |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1397 | LogUtil.v( |
| 1398 | "InCallPresenter.setFullScreen", |
| 1399 | "setFullScreen overridden as dialpad is shown = " + isFullScreen); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1400 | } |
| 1401 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1402 | if (this.isFullScreen == isFullScreen && !force) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1403 | LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state."); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1404 | return; |
| 1405 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1406 | this.isFullScreen = isFullScreen; |
| 1407 | notifyFullscreenModeChange(this.isFullScreen); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | /** |
| 1411 | * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false} |
| 1412 | * otherwise. |
| 1413 | */ |
| 1414 | public boolean isFullscreen() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1415 | return isFullScreen; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | /** |
| 1419 | * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status. |
| 1420 | * |
| 1421 | * @param isFullscreenMode {@code True} if entering full screen mode. |
| 1422 | */ |
| 1423 | public void notifyFullscreenModeChange(boolean isFullscreenMode) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1424 | for (InCallEventListener listener : inCallEventListeners) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1425 | listener.onFullscreenModeChanged(isFullscreenMode); |
| 1426 | } |
| 1427 | } |
| 1428 | |
linyuh | 7b86f56 | 2017-11-16 11:24:09 -0800 | [diff] [blame] | 1429 | /** Instruct the in-call activity to show an error dialog or toast for a disconnected call. */ |
| 1430 | private void showDialogOrToastForDisconnectedCall(DialerCall call) { |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 1431 | if (call.getState() != DialerCall.State.DISCONNECTED) { |
linyuh | 7b86f56 | 2017-11-16 11:24:09 -0800 | [diff] [blame] | 1432 | return; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1433 | } |
linyuh | 7b86f56 | 2017-11-16 11:24:09 -0800 | [diff] [blame] | 1434 | |
| 1435 | // For newly disconnected calls, we may want to show a dialog on specific error conditions |
| 1436 | if (call.getAccountHandle() == null && !call.isConferenceCall()) { |
| 1437 | setDisconnectCauseForMissingAccounts(call); |
| 1438 | } |
| 1439 | |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 1440 | if (isActivityStarted()) { |
| 1441 | inCallActivity.showDialogOrToastForDisconnectedCall( |
| 1442 | new DisconnectMessage(inCallActivity, call)); |
| 1443 | } else { |
| 1444 | CharSequence message = new DisconnectMessage(context, call).toastMessage; |
| 1445 | if (message != null) { |
| 1446 | Toast.makeText(context, message, Toast.LENGTH_LONG).show(); |
| 1447 | } |
| 1448 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | /** |
| 1452 | * When the state of in-call changes, this is the first method to get called. It determines if the |
| 1453 | * UI needs to be started or finished depending on the new state and does it. |
| 1454 | */ |
| 1455 | private InCallState startOrFinishUi(InCallState newState) { |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 1456 | Trace.beginSection("InCallPresenter.startOrFinishUi"); |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1457 | LogUtil.d( |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1458 | "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + inCallState + " -> " + newState); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1459 | |
| 1460 | // TODO: Consider a proper state machine implementation |
| 1461 | |
| 1462 | // If the state isn't changing we have already done any starting/stopping of activities in |
| 1463 | // a previous pass...so lets cut out early |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1464 | if (newState == inCallState) { |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 1465 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1466 | return newState; |
| 1467 | } |
| 1468 | |
| 1469 | // A new Incoming call means that the user needs to be notified of the the call (since |
| 1470 | // it wasn't them who initiated it). We do this through full screen notifications and |
| 1471 | // happens indirectly through {@link StatusBarNotifier}. |
| 1472 | // |
| 1473 | // The process for incoming calls is as follows: |
| 1474 | // |
| 1475 | // 1) CallList - Announces existence of new INCOMING call |
| 1476 | // 2) InCallPresenter - Gets announcement and calculates that the new InCallState |
| 1477 | // - should be set to INCOMING. |
| 1478 | // 3) InCallPresenter - This method is called to see if we need to start or finish |
| 1479 | // the app given the new state. |
| 1480 | // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls |
| 1481 | // StatusBarNotifier explicitly to issue a FullScreen Notification |
| 1482 | // that will either start the InCallActivity or show the user a |
| 1483 | // top-level notification dialog if the user is in an immersive app. |
| 1484 | // That notification can also start the InCallActivity. |
| 1485 | // 5) InCallActivity - Main activity starts up and at the end of its onCreate will |
| 1486 | // call InCallPresenter::setActivity() to let the presenter |
| 1487 | // know that start-up is complete. |
| 1488 | // |
| 1489 | // [ AND NOW YOU'RE IN THE CALL. voila! ] |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1490 | |
| 1491 | // A dialog to show on top of the InCallUI to select a PhoneAccount |
| 1492 | final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState); |
| 1493 | |
| 1494 | // A new outgoing call indicates that the user just now dialed a number and when that |
| 1495 | // happens we need to display the screen immediately or show an account picker dialog if |
| 1496 | // no default is set. However, if the main InCallUI is already visible, we do not want to |
| 1497 | // re-initiate the start-up animation, so we do not need to do anything here. |
| 1498 | // |
| 1499 | // It is also possible to go into an intermediate state where the call has been initiated |
| 1500 | // but Telecom has not yet returned with the details of the call (handle, gateway, etc.). |
| 1501 | // This pending outgoing state can also launch the call screen. |
| 1502 | // |
| 1503 | // This is different from the incoming call sequence because we do not need to shock the |
| 1504 | // user with a top-level notification. Just show the call UI normally. |
| 1505 | boolean callCardFragmentVisible = |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1506 | inCallActivity != null && inCallActivity.getCallCardFragmentVisible(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1507 | final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible; |
| 1508 | boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible; |
| 1509 | |
| 1510 | // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the |
| 1511 | // outgoing call process, so the UI should be brought up to show an error dialog. |
| 1512 | showCallUi |= |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1513 | (InCallState.PENDING_OUTGOING == inCallState |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1514 | && InCallState.INCALL == newState |
| 1515 | && !isShowingInCallUi()); |
| 1516 | |
| 1517 | // Another exception - InCallActivity is in charge of disconnecting a call with no |
| 1518 | // valid accounts set. Bring the UI up if this is true for the current pending outgoing |
| 1519 | // call so that: |
| 1520 | // 1) The call can be disconnected correctly |
| 1521 | // 2) The UI comes up and correctly displays the error dialog. |
| 1522 | // TODO: Remove these special case conditions by making InCallPresenter a true state |
| 1523 | // machine. Telecom should also be the component responsible for disconnecting a call |
| 1524 | // with no valid accounts. |
| 1525 | showCallUi |= |
| 1526 | InCallState.PENDING_OUTGOING == newState |
| 1527 | && mainUiNotVisible |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1528 | && isCallWithNoValidAccounts(callList.getPendingOutgoingCall()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1529 | |
| 1530 | // The only time that we have an instance of mInCallActivity and it isn't started is |
| 1531 | // when it is being destroyed. In that case, lets avoid bringing up another instance of |
| 1532 | // the activity. When it is finally destroyed, we double check if we should bring it back |
| 1533 | // up so we aren't going to lose anything by avoiding a second startup here. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1534 | boolean activityIsFinishing = inCallActivity != null && !isActivityStarted(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1535 | if (activityIsFinishing) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1536 | LogUtil.i( |
| 1537 | "InCallPresenter.startOrFinishUi", |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1538 | "Undo the state change: " + newState + " -> " + inCallState); |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 1539 | Trace.endSection(); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1540 | return inCallState; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | // We're about the bring up the in-call UI for outgoing and incoming call. If we still have |
| 1544 | // dialogs up, we need to clear them out before showing in-call screen. This is necessary |
| 1545 | // to fix the bug that dialog will show up when data reaches limit even after makeing new |
| 1546 | // outgoing call after user ignore it by pressing home button. |
| 1547 | if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING) |
| 1548 | && !showCallUi |
| 1549 | && isActivityStarted()) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1550 | inCallActivity.dismissPendingDialogs(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1551 | } |
| 1552 | |
yueg | 4613e8f | 2018-04-23 13:35:33 -0700 | [diff] [blame] | 1553 | if ((showCallUi || showAccountPicker) && !shouldStartInBubbleMode()) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1554 | LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI"); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1555 | showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1556 | } else if (newState == InCallState.NO_CALLS) { |
| 1557 | // The new state is the no calls state. Tear everything down. |
yueg | 8e54486 | 2018-04-06 14:34:59 -0700 | [diff] [blame] | 1558 | inCallState = newState; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1559 | attemptFinishActivity(); |
| 1560 | attemptCleanup(); |
| 1561 | } |
| 1562 | |
wangqi | cf61ca0 | 2017-08-31 15:32:55 -0700 | [diff] [blame] | 1563 | Trace.endSection(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1564 | return newState; |
| 1565 | } |
| 1566 | |
| 1567 | /** |
| 1568 | * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount |
| 1569 | * or PhoneAccounts to select from. |
| 1570 | */ |
| 1571 | private void setDisconnectCauseForMissingAccounts(DialerCall call) { |
| 1572 | |
| 1573 | Bundle extras = call.getIntentExtras(); |
| 1574 | // Initialize the extras bundle to avoid NPE |
| 1575 | if (extras == null) { |
| 1576 | extras = new Bundle(); |
| 1577 | } |
| 1578 | |
| 1579 | final List<PhoneAccountHandle> phoneAccountHandles = |
| 1580 | extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS); |
| 1581 | |
| 1582 | if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) { |
| 1583 | String scheme = call.getHandle().getScheme(); |
| 1584 | final String errorMsg = |
| 1585 | PhoneAccount.SCHEME_TEL.equals(scheme) |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1586 | ? context.getString(R.string.callFailed_simError) |
| 1587 | : context.getString(R.string.incall_error_supp_service_unknown); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1588 | DisconnectCause disconnectCause = |
| 1589 | new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg); |
| 1590 | call.setDisconnectCause(disconnectCause); |
| 1591 | } |
| 1592 | } |
| 1593 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1594 | /** |
| 1595 | * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise. |
| 1596 | * Calling classes should use this as an indication whether to interact with the |
| 1597 | * InCallPresenter or not. |
| 1598 | */ |
| 1599 | public boolean isReadyForTearDown() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1600 | return inCallActivity == null && !serviceConnected && inCallState == InCallState.NO_CALLS; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | /** |
| 1604 | * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down. |
| 1605 | */ |
| 1606 | private void attemptCleanup() { |
| 1607 | if (isReadyForTearDown()) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1608 | LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1609 | |
| 1610 | cleanupSurfaces(); |
| 1611 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1612 | isChangingConfigurations = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1613 | |
yueg | 8e54486 | 2018-04-06 14:34:59 -0700 | [diff] [blame] | 1614 | audioRouteSetForBubbleMode = false; |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 1615 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1616 | // blow away stale contact info so that we get fresh data on |
| 1617 | // the next set of calls |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1618 | if (contactInfoCache != null) { |
| 1619 | contactInfoCache.clearCache(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1620 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1621 | contactInfoCache = null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1622 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1623 | if (proximitySensor != null) { |
| 1624 | removeListener(proximitySensor); |
| 1625 | proximitySensor.tearDown(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1626 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1627 | proximitySensor = null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1628 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1629 | if (statusBarNotifier != null) { |
| 1630 | removeListener(statusBarNotifier); |
| 1631 | EnrichedCallComponent.get(context) |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 1632 | .getEnrichedCallManager() |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1633 | .unregisterStateChangedListener(statusBarNotifier); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1634 | } |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 1635 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1636 | if (externalCallNotifier != null && externalCallList != null) { |
| 1637 | externalCallList.removeExternalCallListener(externalCallNotifier); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1638 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1639 | statusBarNotifier = null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1640 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1641 | if (callList != null) { |
| 1642 | callList.removeListener(this); |
| 1643 | callList.removeListener(spamCallListListener); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1644 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1645 | callList = null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1646 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1647 | context = null; |
| 1648 | inCallActivity = null; |
| 1649 | manageConferenceActivity = null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1650 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1651 | listeners.clear(); |
| 1652 | incomingCallListeners.clear(); |
| 1653 | detailsListeners.clear(); |
| 1654 | canAddCallListeners.clear(); |
| 1655 | orientationListeners.clear(); |
| 1656 | inCallEventListeners.clear(); |
| 1657 | inCallUiListeners.clear(); |
| 1658 | if (!inCallUiLocks.isEmpty()) { |
| 1659 | LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + inCallUiLocks); |
| 1660 | inCallUiLocks.clear(); |
twyen | 8efb495 | 2017-10-06 16:35:54 -0700 | [diff] [blame] | 1661 | } |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1662 | LogUtil.d("InCallPresenter.attemptCleanup", "finished"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1666 | public void showInCall(boolean showDialpad, boolean newOutgoingCall) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1667 | LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity"); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1668 | context.startActivity( |
| 1669 | InCallActivity.getIntent(context, showDialpad, newOutgoingCall, false /* forFullScreen */)); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | public void onServiceBind() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1673 | serviceBound = true; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | public void onServiceUnbind() { |
| 1677 | InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1678 | serviceBound = false; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | public boolean isServiceBound() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1682 | return serviceBound; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | public void maybeStartRevealAnimation(Intent intent) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1686 | if (intent == null || inCallActivity != null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1687 | return; |
| 1688 | } |
| 1689 | final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS); |
| 1690 | if (extras == null) { |
| 1691 | // Incoming call, just show the in-call UI directly. |
| 1692 | return; |
| 1693 | } |
| 1694 | |
| 1695 | if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) { |
| 1696 | // Account selection dialog will show up so don't show the animation. |
| 1697 | return; |
| 1698 | } |
| 1699 | |
| 1700 | final PhoneAccountHandle accountHandle = |
| 1701 | intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE); |
| 1702 | final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1703 | |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 1704 | setBoundAndWaitingForOutgoingCall(true, accountHandle); |
| 1705 | |
yueg | 4613e8f | 2018-04-23 13:35:33 -0700 | [diff] [blame] | 1706 | if (shouldStartInBubbleModeWithExtras(extras)) { |
yueg | 32d9ae7 | 2018-04-05 17:35:56 -0700 | [diff] [blame] | 1707 | LogUtil.i("InCallPresenter.maybeStartRevealAnimation", "shouldStartInBubbleMode"); |
| 1708 | // Show bubble instead of in call UI |
| 1709 | return; |
| 1710 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1711 | |
| 1712 | final Intent activityIntent = |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1713 | InCallActivity.getIntent(context, false, true, false /* forFullScreen */); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1714 | activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1715 | context.startActivity(activityIntent); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | /** |
| 1719 | * Retrieves the current in-call camera manager instance, creating if necessary. |
| 1720 | * |
| 1721 | * @return The {@link InCallCameraManager}. |
| 1722 | */ |
| 1723 | public InCallCameraManager getInCallCameraManager() { |
| 1724 | synchronized (this) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1725 | if (inCallCameraManager == null) { |
| 1726 | inCallCameraManager = new InCallCameraManager(context); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1727 | } |
| 1728 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1729 | return inCallCameraManager; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1730 | } |
| 1731 | } |
| 1732 | |
| 1733 | /** |
| 1734 | * Notifies listeners of changes in orientation and notify calls of rotation angle change. |
| 1735 | * |
| 1736 | * @param orientation The screen orientation of the device (one of: {@link |
| 1737 | * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link |
| 1738 | * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link |
| 1739 | * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link |
| 1740 | * InCallOrientationEventListener#SCREEN_ORIENTATION_270}). |
| 1741 | */ |
| 1742 | public void onDeviceOrientationChange(@ScreenOrientation int orientation) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1743 | LogUtil.d( |
| 1744 | "InCallPresenter.onDeviceOrientationChange", |
| 1745 | "onDeviceOrientationChange: orientation= " + orientation); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1746 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1747 | if (callList != null) { |
| 1748 | callList.notifyCallsOfDeviceRotation(orientation); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1749 | } else { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1750 | LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null."); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | // Notify listeners of device orientation changed. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1754 | for (InCallOrientationListener listener : orientationListeners) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1755 | listener.onDeviceOrientationChanged(orientation); |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | /** |
| 1760 | * Configures the in-call UI activity so it can change orientations or not. Enables the |
| 1761 | * orientation event listener if allowOrientationChange is true, disables it if false. |
| 1762 | * |
| 1763 | * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and |
| 1764 | * landscape. {@code false} if the in-call UI should be locked in portrait. |
| 1765 | */ |
| 1766 | public void setInCallAllowsOrientationChange(boolean allowOrientationChange) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1767 | if (inCallActivity == null) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1768 | LogUtil.e( |
| 1769 | "InCallPresenter.setInCallAllowsOrientationChange", |
| 1770 | "InCallActivity is null. Can't set requested orientation."); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1771 | return; |
| 1772 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1773 | inCallActivity.setAllowOrientationChange(allowOrientationChange); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | public void enableScreenTimeout(boolean enable) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1777 | LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1778 | screenTimeoutEnabled = enable; |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1779 | applyScreenTimeout(); |
| 1780 | } |
| 1781 | |
| 1782 | private void applyScreenTimeout() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1783 | if (inCallActivity == null) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1784 | LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null."); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1785 | return; |
| 1786 | } |
| 1787 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1788 | final Window window = inCallActivity.getWindow(); |
| 1789 | if (screenTimeoutEnabled) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1790 | window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 1791 | } else { |
| 1792 | window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | /** |
| 1797 | * Hides or shows the conference manager fragment. |
| 1798 | * |
| 1799 | * @param show {@code true} if the conference manager should be shown, {@code false} if it should |
| 1800 | * be hidden. |
| 1801 | */ |
| 1802 | public void showConferenceCallManager(boolean show) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1803 | if (inCallActivity != null) { |
| 1804 | inCallActivity.showConferenceFragment(show); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1805 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1806 | if (!show && manageConferenceActivity != null) { |
| 1807 | manageConferenceActivity.finish(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | /** |
| 1812 | * Determines if the dialpad is visible. |
| 1813 | * |
| 1814 | * @return {@code true} if the dialpad is visible, {@code false} otherwise. |
| 1815 | */ |
| 1816 | public boolean isDialpadVisible() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1817 | if (inCallActivity == null) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1818 | return false; |
| 1819 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1820 | return inCallActivity.isDialpadVisible(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | public ThemeColorManager getThemeColorManager() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1824 | return themeColorManager; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1825 | } |
| 1826 | |
wangqi | 8d662ca | 2017-10-26 11:27:19 -0700 | [diff] [blame] | 1827 | @VisibleForTesting |
| 1828 | public void setThemeColorManager(ThemeColorManager themeColorManager) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1829 | this.themeColorManager = themeColorManager; |
wangqi | 8d662ca | 2017-10-26 11:27:19 -0700 | [diff] [blame] | 1830 | } |
| 1831 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1832 | /** Called when the foreground call changes. */ |
| 1833 | public void onForegroundCallChanged(DialerCall newForegroundCall) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1834 | themeColorManager.onForegroundCallChanged(context, newForegroundCall); |
| 1835 | if (inCallActivity != null) { |
| 1836 | inCallActivity.onForegroundCallChanged(newForegroundCall); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | public InCallActivity getActivity() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1841 | return inCallActivity; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | /** Called when the UI begins, and starts the callstate callbacks if necessary. */ |
| 1845 | public void setActivity(InCallActivity inCallActivity) { |
| 1846 | if (inCallActivity == null) { |
| 1847 | throw new IllegalArgumentException("registerActivity cannot be called with null"); |
| 1848 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1849 | if (this.inCallActivity != null && this.inCallActivity != inCallActivity) { |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1850 | LogUtil.w( |
| 1851 | "InCallPresenter.setActivity", "Setting a second activity before destroying the first."); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1852 | } |
| 1853 | updateActivity(inCallActivity); |
| 1854 | } |
| 1855 | |
| 1856 | ExternalCallNotifier getExternalCallNotifier() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1857 | return externalCallNotifier; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | VideoSurfaceTexture getLocalVideoSurfaceTexture() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1861 | if (localVideoSurfaceTexture == null) { |
roldenburg | 12b50c6 | 2017-09-01 14:41:19 -0700 | [diff] [blame] | 1862 | boolean isPixel2017 = false; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1863 | if (context != null) { |
| 1864 | isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE); |
roldenburg | 12b50c6 | 2017-09-01 14:41:19 -0700 | [diff] [blame] | 1865 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1866 | localVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1867 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1868 | return localVideoSurfaceTexture; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | VideoSurfaceTexture getRemoteVideoSurfaceTexture() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1872 | if (remoteVideoSurfaceTexture == null) { |
roldenburg | 12b50c6 | 2017-09-01 14:41:19 -0700 | [diff] [blame] | 1873 | boolean isPixel2017 = false; |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1874 | if (context != null) { |
| 1875 | isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE); |
roldenburg | 12b50c6 | 2017-09-01 14:41:19 -0700 | [diff] [blame] | 1876 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1877 | remoteVideoSurfaceTexture = VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1878 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1879 | return remoteVideoSurfaceTexture; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | void cleanupSurfaces() { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1883 | if (remoteVideoSurfaceTexture != null) { |
| 1884 | remoteVideoSurfaceTexture.setDoneWithSurface(); |
| 1885 | remoteVideoSurfaceTexture = null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1886 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1887 | if (localVideoSurfaceTexture != null) { |
| 1888 | localVideoSurfaceTexture.setDoneWithSurface(); |
| 1889 | localVideoSurfaceTexture = null; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1890 | } |
| 1891 | } |
| 1892 | |
yueg | 77cb8e5 | 2017-10-27 16:42:51 -0700 | [diff] [blame] | 1893 | @Override |
| 1894 | public void onAudioStateChanged(CallAudioState audioState) { |
yueg | 8e54486 | 2018-04-06 14:34:59 -0700 | [diff] [blame] | 1895 | // Set sensible audio route for bubble mode when we get real audio state for the first time |
| 1896 | // During the first time this function is called, supportedRouteMask is set to |
yueg | 4613e8f | 2018-04-23 13:35:33 -0700 | [diff] [blame] | 1897 | // SUPPORTED_AUDIO_ROUTE_ALL, but it's OK since shouldStartInBubbleMode() is false at that time |
| 1898 | // (callList not updated yet). |
| 1899 | if (!audioRouteSetForBubbleMode && shouldStartInBubbleMode()) { |
yueg | 8e54486 | 2018-04-06 14:34:59 -0700 | [diff] [blame] | 1900 | setAudioRouteForBubbleMode(audioState); |
| 1901 | audioRouteSetForBubbleMode = true; |
| 1902 | } |
| 1903 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 1904 | if (statusBarNotifier != null) { |
| 1905 | statusBarNotifier.updateNotification(); |
yueg | 77cb8e5 | 2017-10-27 16:42:51 -0700 | [diff] [blame] | 1906 | } |
| 1907 | } |
| 1908 | |
yueg | 8e54486 | 2018-04-06 14:34:59 -0700 | [diff] [blame] | 1909 | /** |
| 1910 | * Set audio route to make audio sensible. According to availability, set audio route to Bluetooth |
| 1911 | * or wired headset or speaker. |
| 1912 | */ |
| 1913 | private void setAudioRouteForBubbleMode(CallAudioState audioState) { |
| 1914 | if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH) |
| 1915 | == CallAudioState.ROUTE_BLUETOOTH) { |
| 1916 | // Use Bluetooth if available |
| 1917 | TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_BLUETOOTH); |
| 1918 | LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "bluetooth"); |
| 1919 | } else if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_WIRED_HEADSET) |
| 1920 | == CallAudioState.ROUTE_WIRED_HEADSET) { |
| 1921 | // Use wired headset if available |
| 1922 | TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_WIRED_HEADSET); |
| 1923 | LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "wired headset"); |
| 1924 | } else { |
| 1925 | // Use speaker |
| 1926 | TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_SPEAKER); |
| 1927 | LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "speaker"); |
| 1928 | } |
| 1929 | } |
| 1930 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1931 | /** All the main states of InCallActivity. */ |
| 1932 | public enum InCallState { |
| 1933 | // InCall Screen is off and there are no calls |
| 1934 | NO_CALLS, |
| 1935 | |
| 1936 | // Incoming-call screen is up |
| 1937 | INCOMING, |
| 1938 | |
| 1939 | // In-call experience is showing |
| 1940 | INCALL, |
| 1941 | |
| 1942 | // Waiting for user input before placing outgoing call |
| 1943 | WAITING_FOR_ACCOUNT, |
| 1944 | |
| 1945 | // UI is starting up but no call has been initiated yet. |
| 1946 | // The UI is waiting for Telecom to respond. |
| 1947 | PENDING_OUTGOING, |
| 1948 | |
| 1949 | // User is dialing out |
| 1950 | OUTGOING; |
| 1951 | |
| 1952 | public boolean isIncoming() { |
| 1953 | return (this == INCOMING); |
| 1954 | } |
| 1955 | |
| 1956 | public boolean isConnectingOrConnected() { |
| 1957 | return (this == INCOMING || this == OUTGOING || this == INCALL); |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | /** Interface implemented by classes that need to know about the InCall State. */ |
| 1962 | public interface InCallStateListener { |
| 1963 | |
| 1964 | // TODO: Enhance state to contain the call objects instead of passing CallList |
| 1965 | void onStateChange(InCallState oldState, InCallState newState, CallList callList); |
| 1966 | } |
| 1967 | |
| 1968 | public interface IncomingCallListener { |
| 1969 | |
| 1970 | void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call); |
| 1971 | } |
| 1972 | |
| 1973 | public interface CanAddCallListener { |
| 1974 | |
| 1975 | void onCanAddCallChanged(boolean canAddCall); |
| 1976 | } |
| 1977 | |
| 1978 | public interface InCallDetailsListener { |
| 1979 | |
| 1980 | void onDetailsChanged(DialerCall call, android.telecom.Call.Details details); |
| 1981 | } |
| 1982 | |
| 1983 | public interface InCallOrientationListener { |
| 1984 | |
| 1985 | void onDeviceOrientationChanged(@ScreenOrientation int orientation); |
| 1986 | } |
| 1987 | |
| 1988 | /** |
| 1989 | * Interface implemented by classes that need to know about events which occur within the In-Call |
| 1990 | * UI. Used as a means of communicating between fragments that make up the UI. |
| 1991 | */ |
| 1992 | public interface InCallEventListener { |
| 1993 | |
| 1994 | void onFullscreenModeChanged(boolean isFullscreenMode); |
| 1995 | } |
| 1996 | |
| 1997 | public interface InCallUiListener { |
| 1998 | |
| 1999 | void onUiShowing(boolean showing); |
| 2000 | } |
twyen | 8efb495 | 2017-10-06 16:35:54 -0700 | [diff] [blame] | 2001 | |
| 2002 | private class InCallUiLockImpl implements InCallUiLock { |
| 2003 | private final String tag; |
| 2004 | |
| 2005 | private InCallUiLockImpl(String tag) { |
| 2006 | this.tag = tag; |
| 2007 | } |
| 2008 | |
| 2009 | @MainThread |
| 2010 | @Override |
| 2011 | public void release() { |
| 2012 | Assert.isMainThread(); |
| 2013 | releaseInCallUiLock(InCallUiLockImpl.this); |
| 2014 | } |
| 2015 | |
| 2016 | @Override |
| 2017 | public String toString() { |
| 2018 | return "InCallUiLock[" + tag + "]"; |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | @MainThread |
| 2023 | public InCallUiLock acquireInCallUiLock(String tag) { |
| 2024 | Assert.isMainThread(); |
| 2025 | InCallUiLock lock = new InCallUiLockImpl(tag); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 2026 | inCallUiLocks.add(lock); |
twyen | 8efb495 | 2017-10-06 16:35:54 -0700 | [diff] [blame] | 2027 | return lock; |
| 2028 | } |
| 2029 | |
| 2030 | @MainThread |
| 2031 | private void releaseInCallUiLock(InCallUiLock lock) { |
| 2032 | Assert.isMainThread(); |
| 2033 | LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 2034 | inCallUiLocks.remove(lock); |
| 2035 | if (inCallUiLocks.isEmpty()) { |
twyen | 8efb495 | 2017-10-06 16:35:54 -0700 | [diff] [blame] | 2036 | LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released"); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 2037 | if (inCallState == InCallState.NO_CALLS) { |
twyen | 8efb495 | 2017-10-06 16:35:54 -0700 | [diff] [blame] | 2038 | LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI"); |
| 2039 | attemptFinishActivity(); |
| 2040 | attemptCleanup(); |
| 2041 | } |
| 2042 | } |
| 2043 | } |
| 2044 | |
| 2045 | @MainThread |
| 2046 | public boolean isInCallUiLocked() { |
| 2047 | Assert.isMainThread(); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 2048 | if (inCallUiLocks.isEmpty()) { |
twyen | 8efb495 | 2017-10-06 16:35:54 -0700 | [diff] [blame] | 2049 | return false; |
| 2050 | } |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 2051 | for (InCallUiLock lock : inCallUiLocks) { |
twyen | 8efb495 | 2017-10-06 16:35:54 -0700 | [diff] [blame] | 2052 | LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock); |
| 2053 | } |
| 2054 | return true; |
| 2055 | } |
| 2056 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 2057 | private final Set<InCallUiLock> inCallUiLocks = new ArraySet<>(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 2058 | } |