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