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