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