blob: 150499f73ec0f6eebc029b1f8308175c8fc37854 [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
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
17package com.android.incallui;
18
19import android.content.Context;
20import android.content.Intent;
21import android.graphics.Point;
22import android.os.Bundle;
23import android.os.Handler;
Eric Erfanian2ca43182017-08-31 06:57:16 -070024import android.os.Trace;
twyen8efb4952017-10-06 16:35:54 -070025import android.support.annotation.MainThread;
Eric Erfanianccca3152017-02-22 16:32:36 -080026import android.support.annotation.NonNull;
27import android.support.annotation.Nullable;
28import android.support.annotation.VisibleForTesting;
Eric Erfanian83b20212017-05-31 08:53:10 -070029import android.support.v4.os.UserManagerCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080030import android.telecom.Call.Details;
yueg77cb8e52017-10-27 16:42:51 -070031import android.telecom.CallAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -080032import android.telecom.DisconnectCause;
33import android.telecom.PhoneAccount;
34import android.telecom.PhoneAccountHandle;
35import android.telecom.TelecomManager;
36import android.telecom.VideoProfile;
37import android.telephony.PhoneStateListener;
38import android.telephony.TelephonyManager;
twyen8efb4952017-10-06 16:35:54 -070039import android.util.ArraySet;
Eric Erfanianccca3152017-02-22 16:32:36 -080040import android.view.Window;
41import android.view.WindowManager;
yueg32d9ae72018-04-05 17:35:56 -070042import android.widget.Toast;
Eric Erfanianccca3152017-02-22 16:32:36 -080043import com.android.contacts.common.compat.CallCompat;
yueg32d9ae72018-04-05 17:35:56 -070044import com.android.dialer.CallConfiguration;
45import com.android.dialer.Mode;
Eric Erfanianccca3152017-02-22 16:32:36 -080046import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler;
47import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler.OnCheckBlockedListener;
Eric Erfanian83b20212017-05-31 08:53:10 -070048import com.android.dialer.blocking.FilteredNumberCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080049import com.android.dialer.blocking.FilteredNumbersUtil;
twyen8efb4952017-10-06 16:35:54 -070050import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080051import com.android.dialer.common.LogUtil;
zachh6a4cebd2017-10-24 17:10:06 -070052import com.android.dialer.common.concurrent.DialerExecutorComponent;
Eric Erfaniand8046e52017-04-06 09:41:50 -070053import com.android.dialer.enrichedcall.EnrichedCallComponent;
Eric Erfanian10b34a52017-05-04 08:23:17 -070054import com.android.dialer.location.GeoUtil;
yuegb493c3f2018-04-27 14:00:12 -070055import com.android.dialer.logging.DialerImpression;
Eric Erfanian8369df02017-05-03 10:27:13 -070056import com.android.dialer.logging.InteractionEvent;
Eric Erfanianccca3152017-02-22 16:32:36 -080057import com.android.dialer.logging.Logger;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070058import com.android.dialer.postcall.PostCall;
twyena4745bd2017-12-12 18:40:11 -080059import com.android.dialer.telecom.TelecomCallUtil;
Eric Erfanianccca3152017-02-22 16:32:36 -080060import com.android.dialer.telecom.TelecomUtil;
61import com.android.dialer.util.TouchPointManager;
62import com.android.incallui.InCallOrientationEventListener.ScreenOrientation;
63import com.android.incallui.answerproximitysensor.PseudoScreenState;
yueg77cb8e52017-10-27 16:42:51 -070064import com.android.incallui.audiomode.AudioModeProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080065import com.android.incallui.call.CallList;
66import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080067import com.android.incallui.call.ExternalCallList;
Eric Erfanianccca3152017-02-22 16:32:36 -080068import com.android.incallui.call.TelecomAdapter;
wangqibb94ca62018-04-27 14:34:04 -070069import com.android.incallui.call.state.DialerCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -070070import com.android.incallui.disconnectdialog.DisconnectMessage;
twyen8efb4952017-10-06 16:35:54 -070071import com.android.incallui.incalluilock.InCallUiLock;
Eric Erfanianccca3152017-02-22 16:32:36 -080072import com.android.incallui.latencyreport.LatencyReport;
73import com.android.incallui.legacyblocking.BlockedNumberContentObserver;
74import com.android.incallui.spam.SpamCallListListener;
erfaniand05d8992018-03-20 19:42:26 -070075import com.android.incallui.speakeasy.SpeakEasyCallManager;
yuegb47528e2018-04-24 12:12:57 -070076import com.android.incallui.telecomeventui.InternationalCallOnWifiDialogActivity;
77import com.android.incallui.telecomeventui.InternationalCallOnWifiDialogFragment;
Eric Erfanianccca3152017-02-22 16:32:36 -080078import com.android.incallui.videosurface.bindings.VideoSurfaceBindings;
79import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
Eric Erfanian90508232017-03-24 09:31:16 -070080import com.android.incallui.videotech.utils.VideoUtils;
yueg32d9ae72018-04-05 17:35:56 -070081import com.google.protobuf.InvalidProtocolBufferException;
Eric Erfanianccca3152017-02-22 16:32:36 -080082import java.util.Collections;
83import java.util.List;
84import java.util.Objects;
85import java.util.Set;
86import java.util.concurrent.ConcurrentHashMap;
87import java.util.concurrent.CopyOnWriteArrayList;
88import java.util.concurrent.atomic.AtomicBoolean;
89
90/**
91 * Takes updates from the CallList and notifies the InCallActivity (UI) of the changes. Responsible
92 * for starting the activity for a new call and finishing the activity when all calls are
93 * disconnected. Creates and manages the in-call state and provides a listener pattern for the
94 * presenters that want to listen in on the in-call state changes. TODO: This class has become more
95 * of a state machine at this point. Consider renaming.
96 */
yueg77cb8e52017-10-27 16:42:51 -070097public class InCallPresenter implements CallList.Listener, AudioModeProvider.AudioModeListener {
Eric Erfanian2ca43182017-08-31 06:57:16 -070098 private static final String PIXEL2017_SYSTEM_FEATURE =
99 "com.google.android.feature.PIXEL_2017_EXPERIENCE";
yueg32d9ae72018-04-05 17:35:56 -0700100 private static final String CALL_CONFIGURATION_EXTRA = "call_configuration";
Eric Erfanianccca3152017-02-22 16:32:36 -0800101
102 private static final long BLOCK_QUERY_TIMEOUT_MS = 1000;
103
104 private static final Bundle EMPTY_EXTRAS = new Bundle();
105
linyuh183cb712017-12-27 17:02:37 -0800106 private static InCallPresenter inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -0800107
108 /**
109 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is load factor before
110 * resizing, 1 means we only expect a single thread to access the map so make only a single shard
111 */
linyuh183cb712017-12-27 17:02:37 -0800112 private final Set<InCallStateListener> listeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800113 Collections.newSetFromMap(new ConcurrentHashMap<InCallStateListener, Boolean>(8, 0.9f, 1));
114
linyuh183cb712017-12-27 17:02:37 -0800115 private final List<IncomingCallListener> incomingCallListeners = new CopyOnWriteArrayList<>();
116 private final Set<InCallDetailsListener> detailsListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800117 Collections.newSetFromMap(new ConcurrentHashMap<InCallDetailsListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800118 private final Set<CanAddCallListener> canAddCallListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800119 Collections.newSetFromMap(new ConcurrentHashMap<CanAddCallListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800120 private final Set<InCallUiListener> inCallUiListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800121 Collections.newSetFromMap(new ConcurrentHashMap<InCallUiListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800122 private final Set<InCallOrientationListener> orientationListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800123 Collections.newSetFromMap(
124 new ConcurrentHashMap<InCallOrientationListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800125 private final Set<InCallEventListener> inCallEventListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800126 Collections.newSetFromMap(new ConcurrentHashMap<InCallEventListener, Boolean>(8, 0.9f, 1));
127
linyuh183cb712017-12-27 17:02:37 -0800128 private StatusBarNotifier statusBarNotifier;
129 private ExternalCallNotifier externalCallNotifier;
130 private ContactInfoCache contactInfoCache;
131 private Context context;
132 private final OnCheckBlockedListener onCheckBlockedListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800133 new OnCheckBlockedListener() {
134 @Override
135 public void onCheckComplete(final Integer id) {
136 if (id != null && id != FilteredNumberAsyncQueryHandler.INVALID_ID) {
137 // Silence the ringer now to prevent ringing and vibration before the call is
138 // terminated when Telecom attempts to add it.
linyuh183cb712017-12-27 17:02:37 -0800139 TelecomUtil.silenceRinger(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800140 }
141 }
142 };
linyuh183cb712017-12-27 17:02:37 -0800143 private CallList callList;
144 private ExternalCallList externalCallList;
145 private InCallActivity inCallActivity;
146 private ManageConferenceActivity manageConferenceActivity;
147 private final android.telecom.Call.Callback callCallback =
Eric Erfanianccca3152017-02-22 16:32:36 -0800148 new android.telecom.Call.Callback() {
149 @Override
150 public void onPostDialWait(
151 android.telecom.Call telecomCall, String remainingPostDialSequence) {
linyuh183cb712017-12-27 17:02:37 -0800152 final DialerCall call = callList.getDialerCallFromTelecomCall(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800153 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700154 LogUtil.w(
155 "InCallPresenter.onPostDialWait",
156 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800157 return;
158 }
159 onPostDialCharWait(call.getId(), remainingPostDialSequence);
160 }
161
162 @Override
163 public void onDetailsChanged(
164 android.telecom.Call telecomCall, android.telecom.Call.Details details) {
linyuh183cb712017-12-27 17:02:37 -0800165 final DialerCall call = callList.getDialerCallFromTelecomCall(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800166 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700167 LogUtil.w(
168 "InCallPresenter.onDetailsChanged",
169 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800170 return;
171 }
172
173 if (details.hasProperty(Details.PROPERTY_IS_EXTERNAL_CALL)
linyuh183cb712017-12-27 17:02:37 -0800174 && !externalCallList.isCallTracked(telecomCall)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800175
176 // A regular call became an external call so swap call lists.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700177 LogUtil.i("InCallPresenter.onDetailsChanged", "Call became external: " + telecomCall);
linyuh183cb712017-12-27 17:02:37 -0800178 callList.onInternalCallMadeExternal(context, telecomCall);
179 externalCallList.onCallAdded(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800180 return;
181 }
182
linyuh183cb712017-12-27 17:02:37 -0800183 for (InCallDetailsListener listener : detailsListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800184 listener.onDetailsChanged(call, details);
185 }
186 }
187
188 @Override
189 public void onConferenceableCallsChanged(
190 android.telecom.Call telecomCall, List<android.telecom.Call> conferenceableCalls) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700191 LogUtil.i(
192 "InCallPresenter.onConferenceableCallsChanged",
193 "onConferenceableCallsChanged: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800194 onDetailsChanged(telecomCall, telecomCall.getDetails());
195 }
196 };
linyuh183cb712017-12-27 17:02:37 -0800197 private InCallState inCallState = InCallState.NO_CALLS;
198 private ProximitySensor proximitySensor;
199 private final PseudoScreenState pseudoScreenState = new PseudoScreenState();
200 private boolean serviceConnected;
201 private InCallCameraManager inCallCameraManager;
202 private FilteredNumberAsyncQueryHandler filteredQueryHandler;
203 private CallList.Listener spamCallListListener;
twyen27c37182018-04-24 11:48:12 -0700204 private CallList.Listener activeCallsListener;
Eric Erfanianccca3152017-02-22 16:32:36 -0800205 /** Whether or not we are currently bound and waiting for Telecom to send us a new call. */
linyuh183cb712017-12-27 17:02:37 -0800206 private boolean boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -0800207 /** Determines if the InCall UI is in fullscreen mode or not. */
linyuh183cb712017-12-27 17:02:37 -0800208 private boolean isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800209
linyuh183cb712017-12-27 17:02:37 -0800210 private boolean screenTimeoutEnabled = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700211
linyuh183cb712017-12-27 17:02:37 -0800212 private PhoneStateListener phoneStateListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800213 new PhoneStateListener() {
214 @Override
215 public void onCallStateChanged(int state, String incomingNumber) {
216 if (state == TelephonyManager.CALL_STATE_RINGING) {
linyuh183cb712017-12-27 17:02:37 -0800217 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800218 return;
219 }
220 // Check if the number is blocked, to silence the ringer.
linyuh183cb712017-12-27 17:02:37 -0800221 String countryIso = GeoUtil.getCurrentCountryIso(context);
222 filteredQueryHandler.isBlockedNumber(
223 onCheckBlockedListener, incomingNumber, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800224 }
225 }
226 };
twyena4745bd2017-12-12 18:40:11 -0800227
Eric Erfanianccca3152017-02-22 16:32:36 -0800228 /** Whether or not InCallService is bound to Telecom. */
linyuh183cb712017-12-27 17:02:37 -0800229 private boolean serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800230
231 /**
232 * When configuration changes Android kills the current activity and starts a new one. The flag is
233 * used to check if full clean up is necessary (activity is stopped and new activity won't be
234 * started), or if a new activity will be started right after the current one is destroyed, and
235 * therefore no need in release all resources.
236 */
linyuh183cb712017-12-27 17:02:37 -0800237 private boolean isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800238
linyuh183cb712017-12-27 17:02:37 -0800239 private boolean awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800240
linyuh183cb712017-12-27 17:02:37 -0800241 private ExternalCallList.ExternalCallListener externalCallListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800242 new ExternalCallList.ExternalCallListener() {
243
244 @Override
245 public void onExternalCallPulled(android.telecom.Call call) {
246 // Note: keep this code in sync with InCallPresenter#onCallAdded
247 LatencyReport latencyReport = new LatencyReport(call);
248 latencyReport.onCallBlockingDone();
249 // Note: External calls do not require spam checking.
linyuh183cb712017-12-27 17:02:37 -0800250 callList.onCallAdded(context, call, latencyReport);
251 call.registerCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800252 }
253
254 @Override
255 public void onExternalCallAdded(android.telecom.Call call) {
256 // No-op
257 }
258
259 @Override
260 public void onExternalCallRemoved(android.telecom.Call call) {
261 // No-op
262 }
263
264 @Override
265 public void onExternalCallUpdated(android.telecom.Call call) {
266 // No-op
267 }
268 };
269
linyuh183cb712017-12-27 17:02:37 -0800270 private ThemeColorManager themeColorManager;
271 private VideoSurfaceTexture localVideoSurfaceTexture;
272 private VideoSurfaceTexture remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -0800273
erfaniand05d8992018-03-20 19:42:26 -0700274 private SpeakEasyCallManager speakEasyCallManager;
275
Eric Erfanian10b34a52017-05-04 08:23:17 -0700276 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800277 @VisibleForTesting
278 InCallPresenter() {}
279
280 public static synchronized InCallPresenter getInstance() {
linyuh183cb712017-12-27 17:02:37 -0800281 if (inCallPresenter == null) {
wangqic8cf79e2017-10-17 09:21:00 -0700282 Trace.beginSection("InCallPresenter.Constructor");
linyuh183cb712017-12-27 17:02:37 -0800283 inCallPresenter = new InCallPresenter();
wangqic8cf79e2017-10-17 09:21:00 -0700284 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800285 }
linyuh183cb712017-12-27 17:02:37 -0800286 return inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -0800287 }
288
Eric Erfanian10b34a52017-05-04 08:23:17 -0700289 @VisibleForTesting
290 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
linyuh183cb712017-12-27 17:02:37 -0800291 InCallPresenter.inCallPresenter = inCallPresenter;
Eric Erfanian10b34a52017-05-04 08:23:17 -0700292 }
293
Eric Erfanianccca3152017-02-22 16:32:36 -0800294 /**
295 * Determines whether or not a call has no valid phone accounts that can be used to make the call
296 * with. Emergency calls do not require a phone account.
297 *
298 * @param call to check accounts for.
299 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
300 * call contains a phone account that could be used to initiate it with, or is an emergency
301 * call.
302 */
303 public static boolean isCallWithNoValidAccounts(DialerCall call) {
304 if (call != null && !call.isEmergencyCall()) {
305 Bundle extras = call.getIntentExtras();
306
307 if (extras == null) {
308 extras = EMPTY_EXTRAS;
309 }
310
311 final List<PhoneAccountHandle> phoneAccountHandles =
312 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
313
314 if ((call.getAccountHandle() == null
315 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700316 LogUtil.i(
317 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800318 return true;
319 }
320 }
321 return false;
322 }
323
324 public InCallState getInCallState() {
linyuh183cb712017-12-27 17:02:37 -0800325 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800326 }
327
328 public CallList getCallList() {
linyuh183cb712017-12-27 17:02:37 -0800329 return callList;
Eric Erfanianccca3152017-02-22 16:32:36 -0800330 }
331
332 public void setUp(
333 @NonNull Context context,
334 CallList callList,
335 ExternalCallList externalCallList,
336 StatusBarNotifier statusBarNotifier,
337 ExternalCallNotifier externalCallNotifier,
338 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700339 ProximitySensor proximitySensor,
erfaniand05d8992018-03-20 19:42:26 -0700340 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler,
yueg4613e8f2018-04-23 13:35:33 -0700341 @NonNull SpeakEasyCallManager speakEasyCallManager) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700342 Trace.beginSection("InCallPresenter.setUp");
linyuh183cb712017-12-27 17:02:37 -0800343 if (serviceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700344 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
linyuh183cb712017-12-27 17:02:37 -0800345 if (context != this.context || callList != this.callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800346 throw new IllegalStateException();
347 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700348 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800349 return;
350 }
351
352 Objects.requireNonNull(context);
linyuh183cb712017-12-27 17:02:37 -0800353 this.context = context;
Eric Erfanianccca3152017-02-22 16:32:36 -0800354
linyuh183cb712017-12-27 17:02:37 -0800355 this.contactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800356
linyuh183cb712017-12-27 17:02:37 -0800357 this.statusBarNotifier = statusBarNotifier;
358 this.externalCallNotifier = externalCallNotifier;
359 addListener(this.statusBarNotifier);
360 EnrichedCallComponent.get(this.context)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700361 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -0800362 .registerStateChangedListener(this.statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800363
linyuh183cb712017-12-27 17:02:37 -0800364 this.proximitySensor = proximitySensor;
365 addListener(this.proximitySensor);
Eric Erfanianccca3152017-02-22 16:32:36 -0800366
linyuh183cb712017-12-27 17:02:37 -0800367 if (themeColorManager == null) {
calderwoodraa93df432018-05-23 12:59:03 -0700368 themeColorManager = new ThemeColorManager(new InCallUIMaterialColorMapUtils(this.context));
wangqi8d662ca2017-10-26 11:27:19 -0700369 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800370
linyuh183cb712017-12-27 17:02:37 -0800371 this.callList = callList;
372 this.externalCallList = externalCallList;
373 externalCallList.addExternalCallListener(this.externalCallNotifier);
374 externalCallList.addExternalCallListener(externalCallListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800375
376 // This only gets called by the service so this is okay.
linyuh183cb712017-12-27 17:02:37 -0800377 serviceConnected = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800378
379 // The final thing we do in this set up is add ourselves as a listener to CallList. This
380 // will kick off an update and the whole process can start.
linyuh183cb712017-12-27 17:02:37 -0800381 this.callList.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800382
383 // Create spam call list listener and add it to the list of listeners
linyuh183cb712017-12-27 17:02:37 -0800384 spamCallListListener =
zachh6a4cebd2017-10-24 17:10:06 -0700385 new SpamCallListListener(
386 context, DialerExecutorComponent.get(context).dialerExecutorFactory());
linyuh183cb712017-12-27 17:02:37 -0800387 this.callList.addListener(spamCallListListener);
twyen27c37182018-04-24 11:48:12 -0700388 activeCallsListener = new ActiveCallsCallListListener(context);
389 this.callList.addListener(activeCallsListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800390
391 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800392
linyuh183cb712017-12-27 17:02:37 -0800393 filteredQueryHandler = filteredNumberQueryHandler;
erfaniand05d8992018-03-20 19:42:26 -0700394 this.speakEasyCallManager = speakEasyCallManager;
linyuh183cb712017-12-27 17:02:37 -0800395 this.context
Eric Erfanianccca3152017-02-22 16:32:36 -0800396 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800397 .listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800398
yueg77cb8e52017-10-27 16:42:51 -0700399 AudioModeProvider.getInstance().addListener(this);
400
roldenburg42099012018-04-09 14:47:15 -0700401 // 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 motorolaInCallUiNotifier = new MotorolaInCallUiNotifier(context);
405 addInCallUiListener(motorolaInCallUiNotifier);
406 addListener(motorolaInCallUiNotifier);
yueg7f5acbe2018-01-10 13:50:29 -0800407
Eric Erfanian2ca43182017-08-31 06:57:16 -0700408 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
409 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800410 }
411
412 /**
yueg32d9ae72018-04-05 17:35:56 -0700413 * Return whether we should start call in bubble mode and not show InCallActivity. The call mode
414 * should be set in CallConfiguration in EXTRA_OUTGOING_CALL_EXTRAS when starting a call intent.
415 */
yueg71bf7e42018-04-30 12:00:48 -0700416 public boolean shouldStartInBubbleMode() {
yueg32d9ae72018-04-05 17:35:56 -0700417 if (!ReturnToCallController.isEnabled(context)) {
418 return false;
419 }
yueg4613e8f2018-04-23 13:35:33 -0700420
421 // We only start in Bubble mode for outgoing call
422 DialerCall dialerCall = callList.getPendingOutgoingCall();
423 if (dialerCall == null) {
424 dialerCall = callList.getOutgoingCall();
425 }
yueg743b5012018-05-30 13:26:14 -0700426 // Outgoing call can be disconnected and reason will be shown in toast
427 if (dialerCall == null) {
428 dialerCall = callList.getDisconnectedCall();
429 }
yueg4613e8f2018-04-23 13:35:33 -0700430 if (dialerCall == null) {
yueg32d9ae72018-04-05 17:35:56 -0700431 return false;
432 }
yueg2e366472018-04-30 18:08:44 -0700433 if (dialerCall.isEmergencyCall()) {
434 return false;
435 }
yueg4613e8f2018-04-23 13:35:33 -0700436
437 Bundle extras = dialerCall.getIntentExtras();
yuegb493c3f2018-04-27 14:00:12 -0700438 boolean result = shouldStartInBubbleModeWithExtras(extras);
439 if (result) {
440 Logger.get(context)
441 .logCallImpression(
442 DialerImpression.Type.START_CALL_IN_BUBBLE_MODE,
443 dialerCall.getUniqueCallId(),
444 dialerCall.getTimeAddedMs());
445 }
446 return result;
yueg4613e8f2018-04-23 13:35:33 -0700447 }
448
449 private boolean shouldStartInBubbleModeWithExtras(Bundle outgoingExtras) {
450 if (!ReturnToCallController.isEnabled(context)) {
451 return false;
452 }
453
454 if (outgoingExtras == null) {
455 return false;
456 }
457 byte[] callConfigurationByteArray = outgoingExtras.getByteArray(CALL_CONFIGURATION_EXTRA);
yueg32d9ae72018-04-05 17:35:56 -0700458 if (callConfigurationByteArray == null) {
459 return false;
460 }
461 try {
462 CallConfiguration callConfiguration = CallConfiguration.parseFrom(callConfigurationByteArray);
463 LogUtil.i(
464 "InCallPresenter.shouldStartInBubbleMode",
465 "call mode: " + callConfiguration.getCallMode());
466 return callConfiguration.getCallMode() == Mode.BUBBLE;
467 } catch (InvalidProtocolBufferException e) {
468 return false;
469 }
470 }
471
472 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800473 * Called when the telephony service has disconnected from us. This will happen when there are no
474 * more active calls. However, we may still want to continue showing the UI for certain cases like
475 * showing "Call Ended". What we really want is to wait for the activity and the service to both
476 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
477 * secondary method that performs the aforementioned logic.
478 */
479 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700480 LogUtil.d("InCallPresenter.tearDown", "tearDown");
linyuh183cb712017-12-27 17:02:37 -0800481 callList.clearOnDisconnect();
Eric Erfanianccca3152017-02-22 16:32:36 -0800482
linyuh183cb712017-12-27 17:02:37 -0800483 serviceConnected = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800484
linyuh183cb712017-12-27 17:02:37 -0800485 context
Eric Erfanianccca3152017-02-22 16:32:36 -0800486 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800487 .listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800488
489 attemptCleanup();
490 VideoPauseController.getInstance().tearDown();
yueg77cb8e52017-10-27 16:42:51 -0700491 AudioModeProvider.getInstance().removeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800492 }
493
494 private void attemptFinishActivity() {
linyuh183cb712017-12-27 17:02:37 -0800495 screenTimeoutEnabled = true;
496 final boolean doFinish = (inCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700497 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800498 if (doFinish) {
linyuh183cb712017-12-27 17:02:37 -0800499 inCallActivity.setExcludeFromRecents(true);
500 inCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800501 }
502 }
503
504 /**
505 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
506 * for more insight on the tear-down process.
507 */
508 public void unsetActivity(InCallActivity inCallActivity) {
509 if (inCallActivity == null) {
510 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
511 }
linyuh183cb712017-12-27 17:02:37 -0800512 if (this.inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700513 LogUtil.i(
514 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800515 return;
516 }
linyuh183cb712017-12-27 17:02:37 -0800517 if (this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700518 LogUtil.w(
519 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800520 "Second instance of InCallActivity is trying to unregister when another"
521 + " instance is active. Ignoring.");
522 return;
523 }
524 updateActivity(null);
525 }
526
527 /**
528 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
529 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
530 */
531 private void updateActivity(InCallActivity inCallActivity) {
wangqi9982f0d2017-10-11 17:46:07 -0700532 Trace.beginSection("InCallPresenter.updateActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800533 boolean updateListeners = false;
534 boolean doAttemptCleanup = false;
535
536 if (inCallActivity != null) {
linyuh183cb712017-12-27 17:02:37 -0800537 if (this.inCallActivity == null) {
538 context = inCallActivity.getApplicationContext();
Eric Erfanianccca3152017-02-22 16:32:36 -0800539 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700540 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800541 } else {
542 // since setActivity is called onStart(), it can be called multiple times.
543 // This is fine and ignorable, but we do not want to update the world every time
544 // this happens (like going to/from background) so we do not set updateListeners.
545 }
546
linyuh183cb712017-12-27 17:02:37 -0800547 this.inCallActivity = inCallActivity;
548 this.inCallActivity.setExcludeFromRecents(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800549
550 // By the time the UI finally comes up, the call may already be disconnected.
551 // If that's the case, we may need to show an error dialog.
linyuh183cb712017-12-27 17:02:37 -0800552 if (callList != null && callList.getDisconnectedCall() != null) {
553 showDialogOrToastForDisconnectedCall(callList.getDisconnectedCall());
Eric Erfanianccca3152017-02-22 16:32:36 -0800554 }
555
556 // When the UI comes up, we need to first check the in-call state.
557 // If we are showing NO_CALLS, that means that a call probably connected and
558 // then immediately disconnected before the UI was able to come up.
559 // If we dont have any calls, start tearing down the UI instead.
560 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
561 // it has been set.
linyuh183cb712017-12-27 17:02:37 -0800562 if (inCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700563 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800564 attemptFinishActivity();
wangqi9982f0d2017-10-11 17:46:07 -0700565 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800566 return;
567 }
568 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700569 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800570 updateListeners = true;
linyuh183cb712017-12-27 17:02:37 -0800571 this.inCallActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800572
573 // We attempt cleanup for the destroy case but only after we recalculate the state
574 // to see if we need to come back up or stay shut down. This is why we do the
575 // cleanup after the call to onCallListChange() instead of directly here.
576 doAttemptCleanup = true;
577 }
578
579 // Messages can come from the telephony layer while the activity is coming up
580 // and while the activity is going down. So in both cases we need to recalculate what
581 // state we should be in after they complete.
582 // Examples: (1) A new incoming call could come in and then get disconnected before
583 // the activity is created.
584 // (2) All calls could disconnect and then get a new incoming call before the
585 // activity is destroyed.
586 //
Eric Erfanian938468d2017-10-24 14:05:52 -0700587 // a bug - We previously had a check for mServiceConnected here as well, but there are
Eric Erfanianccca3152017-02-22 16:32:36 -0800588 // cases where we need to recalculate the current state even if the service in not
589 // connected. In particular the case where startOrFinish() is called while the app is
590 // already finish()ing. In that case, we skip updating the state with the knowledge that
591 // we will check again once the activity has finished. That means we have to recalculate the
592 // state here even if the service is disconnected since we may not have finished a state
593 // transition while finish()ing.
594 if (updateListeners) {
linyuh183cb712017-12-27 17:02:37 -0800595 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800596 }
597
598 if (doAttemptCleanup) {
599 attemptCleanup();
600 }
wangqi9982f0d2017-10-11 17:46:07 -0700601 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800602 }
603
erfanian12243de2018-04-17 12:17:08 -0700604 public SpeakEasyCallManager getSpeakEasyCallManager() {
605 return this.speakEasyCallManager;
606 }
607
Eric Erfanianccca3152017-02-22 16:32:36 -0800608 public void setManageConferenceActivity(
609 @Nullable ManageConferenceActivity manageConferenceActivity) {
linyuh183cb712017-12-27 17:02:37 -0800610 this.manageConferenceActivity = manageConferenceActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -0800611 }
612
613 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700614 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800615 bringToForeground(showDialpad);
616 }
617
618 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700619 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800620 LatencyReport latencyReport = new LatencyReport(call);
621 if (shouldAttemptBlocking(call)) {
622 maybeBlockCall(call, latencyReport);
623 } else {
624 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800625 externalCallList.onCallAdded(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800626 } else {
627 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800628 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800629 }
630 }
631
632 // Since a call has been added we are no longer waiting for Telecom to send us a call.
633 setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -0800634 call.registerCallback(callCallback);
zachh78e54ac2017-12-05 16:38:35 -0800635 // TODO(maxwelb): Return the future in recordPhoneLookupInfo and propagate.
linyuh183cb712017-12-27 17:02:37 -0800636 PhoneLookupHistoryRecorder.recordPhoneLookupInfo(context.getApplicationContext(), call);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700637 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800638 }
639
640 private boolean shouldAttemptBlocking(android.telecom.Call call) {
641 if (call.getState() != android.telecom.Call.STATE_RINGING) {
642 return false;
643 }
linyuh183cb712017-12-27 17:02:37 -0800644 if (!UserManagerCompat.isUserUnlocked(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700645 LogUtil.i(
646 "InCallPresenter.shouldAttemptBlocking",
647 "not attempting to block incoming call because user is locked");
648 return false;
649 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800650 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700651 LogUtil.i(
652 "InCallPresenter.shouldAttemptBlocking",
653 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800654 return false;
655 }
linyuh183cb712017-12-27 17:02:37 -0800656 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700657 LogUtil.i(
658 "InCallPresenter.shouldAttemptBlocking",
659 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800660 return false;
661 }
662 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
663 return false;
664 }
linyuh183cb712017-12-27 17:02:37 -0800665 if (FilteredNumberCompat.useNewFiltering(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700666 LogUtil.i(
667 "InCallPresenter.shouldAttemptBlocking",
668 "not attempting to block incoming call because framework blocking is in use");
669 return false;
670 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800671 return true;
672 }
673
674 /**
675 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
676 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
677 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
678 * call anyways.
679 */
680 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
linyuh183cb712017-12-27 17:02:37 -0800681 final String countryIso = GeoUtil.getCurrentCountryIso(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800682 final String number = TelecomCallUtil.getNumber(call);
683 final long timeAdded = System.currentTimeMillis();
684
685 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
686 // main UI thread. It is needed so we can change its value within different scopes, since
687 // that cannot be done with a final boolean.
688 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
689
690 final Handler handler = new Handler();
691
692 // Proceed if the query is slow; the call may still be blocked after the query returns.
693 final Runnable runnable =
694 new Runnable() {
695 @Override
696 public void run() {
697 hasTimedOut.set(true);
698 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800699 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800700 }
701 };
702 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
703
704 OnCheckBlockedListener onCheckBlockedListener =
705 new OnCheckBlockedListener() {
706 @Override
707 public void onCheckComplete(final Integer id) {
708 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700709 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800710 return;
711 }
712 if (!hasTimedOut.get()) {
713 handler.removeCallbacks(runnable);
714 }
715 if (id == null) {
716 if (!hasTimedOut.get()) {
717 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800718 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800719 }
720 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700721 LogUtil.d(
722 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800723 if (!hasTimedOut.get()) {
724 handler.removeCallbacks(runnable);
725
726 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800727 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800728 }
729 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700730 LogUtil.i(
731 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800732 call.reject(false, null);
linyuh183cb712017-12-27 17:02:37 -0800733 Logger.get(context).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
Eric Erfanianccca3152017-02-22 16:32:36 -0800734
735 /*
736 * If mContext is null, then the InCallPresenter was torn down before the
737 * block check had a chance to complete. The context is no longer valid, so
738 * don't attempt to remove the call log entry.
739 */
linyuh183cb712017-12-27 17:02:37 -0800740 if (context == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800741 return;
742 }
743 // Register observer to update the call log.
744 // BlockedNumberContentObserver will unregister after successful log or timeout.
745 BlockedNumberContentObserver contentObserver =
linyuh183cb712017-12-27 17:02:37 -0800746 new BlockedNumberContentObserver(context, new Handler(), number, timeAdded);
Eric Erfanianccca3152017-02-22 16:32:36 -0800747 contentObserver.register();
748 }
749 }
750 };
751
linyuh183cb712017-12-27 17:02:37 -0800752 filteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800753 }
754
755 public void onCallRemoved(android.telecom.Call call) {
756 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800757 externalCallList.onCallRemoved(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800758 } else {
linyuh183cb712017-12-27 17:02:37 -0800759 callList.onCallRemoved(context, call);
760 call.unregisterCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800761 }
762 }
763
764 public void onCanAddCallChanged(boolean canAddCall) {
linyuh183cb712017-12-27 17:02:37 -0800765 for (CanAddCallListener listener : canAddCallListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800766 listener.onCanAddCallChanged(canAddCall);
767 }
768 }
769
770 @Override
771 public void onWiFiToLteHandover(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -0700772 if (call.hasShownWiFiToLteHandoverToast()) {
773 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800774 }
yueg32d9ae72018-04-05 17:35:56 -0700775
776 Toast.makeText(context, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG)
777 .show();
778 call.setHasShownWiFiToLteHandoverToast();
Eric Erfanianccca3152017-02-22 16:32:36 -0800779 }
780
781 @Override
782 public void onHandoverToWifiFailed(DialerCall call) {
linyuh183cb712017-12-27 17:02:37 -0800783 if (inCallActivity != null) {
784 inCallActivity.showDialogOrToastForWifiHandoverFailure(call);
yueg32d9ae72018-04-05 17:35:56 -0700785 } else {
786 Toast.makeText(context, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT)
787 .show();
Eric Erfanianccca3152017-02-22 16:32:36 -0800788 }
789 }
790
Eric Erfanianc857f902017-05-15 14:05:33 -0700791 @Override
792 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
793 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
yuegb47528e2018-04-24 12:12:57 -0700794
795 if (!InternationalCallOnWifiDialogFragment.shouldShow(context)) {
796 LogUtil.i(
797 "InCallPresenter.onInternationalCallOnWifi",
798 "InternationalCallOnWifiDialogFragment.shouldShow returned false");
799 return;
800 }
801
linyuh183cb712017-12-27 17:02:37 -0800802 if (inCallActivity != null) {
803 inCallActivity.showDialogForInternationalCallOnWifi(call);
yuegb47528e2018-04-24 12:12:57 -0700804 } else {
805 Intent intent = new Intent(context, InternationalCallOnWifiDialogActivity.class);
806 // Prevent showing MainActivity with InternationalCallOnWifiDialogActivity on above
807 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
808 intent.putExtra(InternationalCallOnWifiDialogActivity.EXTRA_CALL_ID, call.getId());
809 context.startActivity(intent);
Eric Erfanianc857f902017-05-15 14:05:33 -0700810 }
811 }
812
Eric Erfanianccca3152017-02-22 16:32:36 -0800813 /**
814 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
815 * app based on the information it gets from CallList. Dispatches the in-call state to all
816 * listeners. Can trigger the creation or destruction of the UI based on the states that is
817 * calculates.
818 */
819 @Override
820 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700821 Trace.beginSection("InCallPresenter.onCallListChange");
linyuh183cb712017-12-27 17:02:37 -0800822 if (inCallActivity != null && inCallActivity.isInCallScreenAnimating()) {
823 awaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700824 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800825 return;
826 }
827 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700828 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800829 return;
830 }
831
linyuh183cb712017-12-27 17:02:37 -0800832 awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800833
834 InCallState newState = getPotentialStateFromCallList(callList);
linyuh183cb712017-12-27 17:02:37 -0800835 InCallState oldState = inCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700836 LogUtil.d(
837 "InCallPresenter.onCallListChange",
838 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700839
840 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
841 // incall activity for that call will still exist (even if it's not visible). In the case of
842 // an incoming call in that situation, just disconnect that "waiting for account" call and
843 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
Eric Erfanian938468d2017-10-24 14:05:52 -0700844 // a bug for more details.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700845 DialerCall waitingForAccountCall;
846 if (newState == InCallState.INCOMING
847 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
848 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700849 // The InCallActivity might be destroyed or not started yet at this point.
850 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800851 inCallActivity.dismissPendingDialogs();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700852 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700853 }
854
Eric Erfanianccca3152017-02-22 16:32:36 -0800855 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700856 LogUtil.d(
857 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800858
859 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700860 LogUtil.i(
861 "InCallPresenter.onCallListChange",
862 "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800863 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800864
yuegb9103042018-03-30 12:12:25 -0700865 // Foreground call changed
866 DialerCall primary = null;
867 if (newState == InCallState.INCOMING) {
868 primary = callList.getIncomingCall();
869 } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
870 primary = callList.getOutgoingCall();
871 if (primary == null) {
872 primary = callList.getPendingOutgoingCall();
873 }
874 } else if (newState == InCallState.INCALL) {
875 primary = getCallToDisplay(callList, null, false);
876 }
877 if (primary != null) {
878 onForegroundCallChanged(primary);
879 }
880
Eric Erfanianccca3152017-02-22 16:32:36 -0800881 // notify listeners of new state
linyuh183cb712017-12-27 17:02:37 -0800882 for (InCallStateListener listener : listeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700883 LogUtil.d(
884 "InCallPresenter.onCallListChange",
linyuh183cb712017-12-27 17:02:37 -0800885 "Notify " + listener + " of state " + inCallState.toString());
886 listener.onStateChange(oldState, inCallState, callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800887 }
888
889 if (isActivityStarted()) {
890 final boolean hasCall =
891 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
linyuh183cb712017-12-27 17:02:37 -0800892 inCallActivity.dismissKeyguard(hasCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800893 }
twyen27c37182018-04-24 11:48:12 -0700894
Eric Erfanian2ca43182017-08-31 06:57:16 -0700895 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800896 }
897
yuegb9103042018-03-30 12:12:25 -0700898 /**
899 * Get the highest priority call to display. Goes through the calls and chooses which to return
900 * based on priority of which type of call to display to the user. Callers can use the "ignore"
901 * feature to get the second best call by passing a previously found primary call as ignore.
902 *
903 * @param ignore A call to ignore if found.
904 */
905 static DialerCall getCallToDisplay(
906 CallList callList, DialerCall ignore, boolean skipDisconnected) {
907 // Active calls come second. An active call always gets precedent.
908 DialerCall retval = callList.getActiveCall();
909 if (retval != null && retval != ignore) {
910 return retval;
911 }
912
913 // Sometimes there is intemediate state that two calls are in active even one is about
914 // to be on hold.
915 retval = callList.getSecondActiveCall();
916 if (retval != null && retval != ignore) {
917 return retval;
918 }
919
920 // Disconnected calls get primary position if there are no active calls
921 // to let user know quickly what call has disconnected. Disconnected
922 // calls are very short lived.
923 if (!skipDisconnected) {
924 retval = callList.getDisconnectingCall();
925 if (retval != null && retval != ignore) {
926 return retval;
927 }
928 retval = callList.getDisconnectedCall();
929 if (retval != null && retval != ignore) {
930 return retval;
931 }
932 }
933
934 // Then we go to background call (calls on hold)
935 retval = callList.getBackgroundCall();
936 if (retval != null && retval != ignore) {
937 return retval;
938 }
939
940 // Lastly, we go to a second background call.
941 retval = callList.getSecondBackgroundCall();
942
943 return retval;
944 }
945
Eric Erfanianccca3152017-02-22 16:32:36 -0800946 /** Called when there is a new incoming call. */
947 @Override
948 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700949 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800950 InCallState newState = startOrFinishUi(InCallState.INCOMING);
linyuh183cb712017-12-27 17:02:37 -0800951 InCallState oldState = inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800952
Eric Erfanian2ca43182017-08-31 06:57:16 -0700953 LogUtil.i(
954 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800955 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800956
wangqicf61ca02017-08-31 15:32:55 -0700957 Trace.beginSection("listener.onIncomingCall");
linyuh183cb712017-12-27 17:02:37 -0800958 for (IncomingCallListener listener : incomingCallListeners) {
959 listener.onIncomingCall(oldState, inCallState, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800960 }
wangqicf61ca02017-08-31 15:32:55 -0700961 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800962
wangqicf61ca02017-08-31 15:32:55 -0700963 Trace.beginSection("onPrimaryCallStateChanged");
linyuh183cb712017-12-27 17:02:37 -0800964 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800965 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800966 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800967 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700968 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700969 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800970 }
971
972 @Override
973 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700974 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
linyuh183cb712017-12-27 17:02:37 -0800975 && inCallState == InCallPresenter.InCallState.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800976 LogUtil.i(
977 "InCallPresenter.onUpgradeToVideo",
978 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700979 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800980 }
981
linyuh183cb712017-12-27 17:02:37 -0800982 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800983 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800984 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800985 }
986 }
987
988 @Override
wangqibc28ea72018-04-02 16:23:00 -0700989 public void onUpgradeToRtt(DialerCall call, int rttRequestId) {
990 if (inCallActivity != null) {
991 inCallActivity.showDialogForRttRequest(call, rttRequestId);
992 }
993 }
994
995 @Override
erfanian3887ab02018-05-11 11:02:53 -0700996 public void onSpeakEasyStateChange() {
997 if (inCallActivity != null) {
998 inCallActivity.onPrimaryCallStateChanged();
999 }
1000 }
1001
1002 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001003 public void onSessionModificationStateChange(DialerCall call) {
1004 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -08001005 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
linyuh183cb712017-12-27 17:02:37 -08001006 if (proximitySensor == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001007 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
1008 return;
1009 }
linyuh183cb712017-12-27 17:02:37 -08001010 proximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001011 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
linyuh183cb712017-12-27 17:02:37 -08001012 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001013 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001014 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001015 }
1016 }
1017
1018 /**
1019 * Called when a call becomes disconnected. Called everytime an existing call changes from being
1020 * connected (incoming/outgoing/active) to disconnected.
1021 */
1022 @Override
1023 public void onDisconnect(DialerCall call) {
linyuh7b86f562017-11-16 11:24:09 -08001024 showDialogOrToastForDisconnectedCall(call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001025
1026 // We need to do the run the same code as onCallListChange.
linyuh183cb712017-12-27 17:02:37 -08001027 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001028
1029 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001030 inCallActivity.dismissKeyguard(false);
Eric Erfanianccca3152017-02-22 16:32:36 -08001031 }
1032
1033 if (call.isEmergencyCall()) {
linyuh183cb712017-12-27 17:02:37 -08001034 FilteredNumbersUtil.recordLastEmergencyCallTime(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001035 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001036
linyuh183cb712017-12-27 17:02:37 -08001037 if (!callList.hasLiveCall()
Eric Erfanianfc37b022017-03-21 10:11:17 -07001038 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -07001039 && !isSecretCode(call.getNumber())
wangqi9982f0d2017-10-11 17:46:07 -07001040 && !call.isVoiceMailNumber()) {
linyuh183cb712017-12-27 17:02:37 -08001041 PostCall.onCallDisconnected(context, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -08001042 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001043 }
1044
Eric Erfanian10b34a52017-05-04 08:23:17 -07001045 private boolean isSecretCode(@Nullable String number) {
1046 return number != null
1047 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
1048 }
1049
Eric Erfanianccca3152017-02-22 16:32:36 -08001050 /** Given the call list, return the state in which the in-call screen should be. */
1051 public InCallState getPotentialStateFromCallList(CallList callList) {
1052
1053 InCallState newState = InCallState.NO_CALLS;
1054
1055 if (callList == null) {
1056 return newState;
1057 }
1058 if (callList.getIncomingCall() != null) {
1059 newState = InCallState.INCOMING;
1060 } else if (callList.getWaitingForAccountCall() != null) {
1061 newState = InCallState.WAITING_FOR_ACCOUNT;
1062 } else if (callList.getPendingOutgoingCall() != null) {
1063 newState = InCallState.PENDING_OUTGOING;
1064 } else if (callList.getOutgoingCall() != null) {
1065 newState = InCallState.OUTGOING;
1066 } else if (callList.getActiveCall() != null
1067 || callList.getBackgroundCall() != null
1068 || callList.getDisconnectedCall() != null
1069 || callList.getDisconnectingCall() != null) {
1070 newState = InCallState.INCALL;
1071 }
1072
1073 if (newState == InCallState.NO_CALLS) {
linyuh183cb712017-12-27 17:02:37 -08001074 if (boundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001075 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001076 }
1077 }
1078
1079 return newState;
1080 }
1081
1082 public boolean isBoundAndWaitingForOutgoingCall() {
linyuh183cb712017-12-27 17:02:37 -08001083 return boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -08001084 }
1085
1086 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001087 LogUtil.i(
1088 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
1089 "setBoundAndWaitingForOutgoingCall: " + isBound);
linyuh183cb712017-12-27 17:02:37 -08001090 boundAndWaitingForOutgoingCall = isBound;
1091 themeColorManager.setPendingPhoneAccountHandle(handle);
1092 if (isBound && inCallState == InCallState.NO_CALLS) {
1093 inCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001094 }
1095 }
1096
1097 public void onShrinkAnimationComplete() {
linyuh183cb712017-12-27 17:02:37 -08001098 if (awaitingCallListUpdate) {
1099 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001100 }
1101 }
1102
1103 public void addIncomingCallListener(IncomingCallListener listener) {
1104 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001105 incomingCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001106 }
1107
1108 public void removeIncomingCallListener(IncomingCallListener listener) {
1109 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001110 incomingCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001111 }
1112 }
1113
1114 public void addListener(InCallStateListener listener) {
1115 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001116 listeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001117 }
1118
1119 public void removeListener(InCallStateListener listener) {
1120 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001121 listeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001122 }
1123 }
1124
1125 public void addDetailsListener(InCallDetailsListener listener) {
1126 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001127 detailsListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001128 }
1129
1130 public void removeDetailsListener(InCallDetailsListener listener) {
1131 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001132 detailsListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001133 }
1134 }
1135
1136 public void addCanAddCallListener(CanAddCallListener listener) {
1137 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001138 canAddCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001139 }
1140
1141 public void removeCanAddCallListener(CanAddCallListener listener) {
1142 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001143 canAddCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001144 }
1145 }
1146
1147 public void addOrientationListener(InCallOrientationListener listener) {
1148 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001149 orientationListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001150 }
1151
1152 public void removeOrientationListener(InCallOrientationListener listener) {
1153 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001154 orientationListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001155 }
1156 }
1157
1158 public void addInCallEventListener(InCallEventListener listener) {
1159 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001160 inCallEventListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001161 }
1162
1163 public void removeInCallEventListener(InCallEventListener listener) {
1164 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001165 inCallEventListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001166 }
1167 }
1168
1169 public ProximitySensor getProximitySensor() {
linyuh183cb712017-12-27 17:02:37 -08001170 return proximitySensor;
Eric Erfanianccca3152017-02-22 16:32:36 -08001171 }
1172
1173 public PseudoScreenState getPseudoScreenState() {
linyuh183cb712017-12-27 17:02:37 -08001174 return pseudoScreenState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001175 }
1176
1177 /** Returns true if the incall app is the foreground application. */
1178 public boolean isShowingInCallUi() {
1179 if (!isActivityStarted()) {
1180 return false;
1181 }
linyuh183cb712017-12-27 17:02:37 -08001182 if (manageConferenceActivity != null && manageConferenceActivity.isVisible()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001183 return true;
1184 }
linyuh183cb712017-12-27 17:02:37 -08001185 return inCallActivity.isVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001186 }
1187
1188 /**
1189 * Returns true if the activity has been created and is running. Returns true as long as activity
1190 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
1191 * (not in foreground).
1192 */
1193 public boolean isActivityStarted() {
linyuh183cb712017-12-27 17:02:37 -08001194 return (inCallActivity != null
1195 && !inCallActivity.isDestroyed()
1196 && !inCallActivity.isFinishing());
Eric Erfanianccca3152017-02-22 16:32:36 -08001197 }
1198
1199 /**
1200 * Determines if the In-Call app is currently changing configuration.
1201 *
1202 * @return {@code true} if the In-Call app is changing configuration.
1203 */
1204 public boolean isChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001205 return isChangingConfigurations;
Eric Erfanianccca3152017-02-22 16:32:36 -08001206 }
1207
1208 /**
1209 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1210 * screen orientation).
1211 */
1212 /*package*/
1213 void updateIsChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001214 isChangingConfigurations = false;
1215 if (inCallActivity != null) {
1216 isChangingConfigurations = inCallActivity.isChangingConfigurations();
Eric Erfanianccca3152017-02-22 16:32:36 -08001217 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001218 LogUtil.v(
1219 "InCallPresenter.updateIsChangingConfigurations",
linyuh183cb712017-12-27 17:02:37 -08001220 "updateIsChangingConfigurations = " + isChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001221 }
1222
yueg10f6e822018-01-17 15:32:18 -08001223 /** Called when the activity goes in/out of the foreground. */
1224 public void onUiShowing(boolean showing) {
linyuh183cb712017-12-27 17:02:37 -08001225 if (proximitySensor != null) {
1226 proximitySensor.onInCallShowing(showing);
Eric Erfanianccca3152017-02-22 16:32:36 -08001227 }
1228
yueg092b21c2017-11-15 16:20:07 -08001229 if (!showing) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001230 updateIsChangingConfigurations();
1231 }
1232
linyuh183cb712017-12-27 17:02:37 -08001233 for (InCallUiListener listener : inCallUiListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001234 listener.onUiShowing(showing);
1235 }
1236
linyuh183cb712017-12-27 17:02:37 -08001237 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001238 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001239 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001240 }
1241 }
1242
Eric Erfanian2ca43182017-08-31 06:57:16 -07001243 public void refreshUi() {
linyuh183cb712017-12-27 17:02:37 -08001244 if (inCallActivity != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001245 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001246 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001247 }
1248 }
1249
Eric Erfanianccca3152017-02-22 16:32:36 -08001250 public void addInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001251 inCallUiListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001252 }
1253
1254 public boolean removeInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001255 return inCallUiListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001256 }
1257
1258 /*package*/
1259 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001260 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001261 notifyVideoPauseController(true);
Eric Erfanian2ca43182017-08-31 06:57:16 -07001262 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001263 }
1264
1265 /*package*/
1266 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001267 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001268 notifyVideoPauseController(false);
1269 }
1270
1271 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001272 LogUtil.d(
1273 "InCallPresenter.notifyVideoPauseController",
linyuh183cb712017-12-27 17:02:37 -08001274 "mIsChangingConfigurations=" + isChangingConfigurations);
1275 if (!isChangingConfigurations) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001276 VideoPauseController.getInstance().onUiShowing(showing);
1277 }
1278 }
1279
1280 /** Brings the app into the foreground if possible. */
1281 public void bringToForeground(boolean showDialpad) {
1282 // Before we bring the incall UI to the foreground, we check to see if:
1283 // 1. It is not currently in the foreground
1284 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1285 // be displayed)
1286 // If the activity hadn't actually been started previously, yet there are still calls
1287 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1288 // bring it up the UI regardless.
linyuh183cb712017-12-27 17:02:37 -08001289 if (!isShowingInCallUi() && inCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001290 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001291 }
1292 }
1293
1294 public void onPostDialCharWait(String callId, String chars) {
yuega489f512018-04-25 12:01:09 -07001295 // If not visible, inCallActivity is stopped. Starting from P, calling recreate() will destroy
1296 // the old activity instance and create a new instance immediately. Previously, the old activity
1297 // went through its lifecycle from create to destroy before creating a new instance.
1298 // So this case doesn't work now: make a call with char WAIT, leave in call UI, call gets
1299 // connected, and go back to in call UI to see the dialog.
1300 // So we should show dialog in an empty activity if inCallActivity is not visible. And it also
1301 // helps with background calling.
1302 if (isActivityStarted() && inCallActivity.isVisible()) {
linyuh183cb712017-12-27 17:02:37 -08001303 inCallActivity.showDialogForPostCharWait(callId, chars);
yuega489f512018-04-25 12:01:09 -07001304 } else {
1305 Intent intent = new Intent(context, PostCharDialogActivity.class);
1306 // Prevent showing MainActivity with PostCharDialogActivity on above
1307 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1308 intent.putExtra(PostCharDialogActivity.EXTRA_CALL_ID, callId);
1309 intent.putExtra(PostCharDialogActivity.EXTRA_POST_DIAL_STRING, chars);
1310 context.startActivity(intent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001311 }
1312 }
1313
1314 /**
1315 * Handles the green CALL key while in-call.
1316 *
1317 * @return true if we consumed the event.
1318 */
1319 public boolean handleCallKey() {
1320 LogUtil.v("InCallPresenter.handleCallKey", null);
1321
1322 // The green CALL button means either "Answer", "Unhold", or
1323 // "Swap calls", or can be a no-op, depending on the current state
1324 // of the Phone.
1325
1326 /** INCOMING CALL */
linyuh183cb712017-12-27 17:02:37 -08001327 final CallList calls = callList;
Eric Erfanianccca3152017-02-22 16:32:36 -08001328 final DialerCall incomingCall = calls.getIncomingCall();
1329 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1330
1331 // (1) Attempt to answer a call
1332 if (incomingCall != null) {
1333 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1334 return true;
1335 }
1336
1337 /** STATE_ACTIVE CALL */
1338 final DialerCall activeCall = calls.getActiveCall();
1339 if (activeCall != null) {
1340 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1341 // consolidate this logic.
1342 final boolean canMerge =
1343 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1344 final boolean canSwap =
1345 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1346
Eric Erfanian2ca43182017-08-31 06:57:16 -07001347 LogUtil.v(
1348 "InCallPresenter.handleCallKey",
1349 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001350
1351 // (2) Attempt actions on conference calls
1352 if (canMerge) {
1353 TelecomAdapter.getInstance().merge(activeCall.getId());
1354 return true;
1355 } else if (canSwap) {
1356 TelecomAdapter.getInstance().swap(activeCall.getId());
1357 return true;
1358 }
1359 }
1360
1361 /** BACKGROUND CALL */
1362 final DialerCall heldCall = calls.getBackgroundCall();
1363 if (heldCall != null) {
1364 // We have a hold call so presumeable it will always support HOLD...but
1365 // there is no harm in double checking.
1366 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1367
Eric Erfanian2ca43182017-08-31 06:57:16 -07001368 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001369
1370 // (4) unhold call
wangqibb94ca62018-04-27 14:34:04 -07001371 if (heldCall.getState() == DialerCallState.ONHOLD && canHold) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001372 heldCall.unhold();
1373 return true;
1374 }
1375 }
1376
1377 // Always consume hard keys
1378 return true;
1379 }
1380
Eric Erfanianccca3152017-02-22 16:32:36 -08001381 /** Clears the previous fullscreen state. */
1382 public void clearFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001383 isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001384 }
1385
1386 /**
1387 * Changes the fullscreen mode of the in-call UI.
1388 *
1389 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1390 * otherwise.
1391 */
1392 public void setFullScreen(boolean isFullScreen) {
1393 setFullScreen(isFullScreen, false /* force */);
1394 }
1395
1396 /**
1397 * Changes the fullscreen mode of the in-call UI.
1398 *
1399 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1400 * otherwise.
1401 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1402 */
1403 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001404 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001405
1406 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1407 if (isDialpadVisible()) {
1408 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001409 LogUtil.v(
1410 "InCallPresenter.setFullScreen",
1411 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001412 }
1413
linyuh183cb712017-12-27 17:02:37 -08001414 if (this.isFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001415 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001416 return;
1417 }
linyuh183cb712017-12-27 17:02:37 -08001418 this.isFullScreen = isFullScreen;
1419 notifyFullscreenModeChange(this.isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001420 }
1421
1422 /**
1423 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1424 * otherwise.
1425 */
1426 public boolean isFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001427 return isFullScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001428 }
1429
1430 /**
1431 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1432 *
1433 * @param isFullscreenMode {@code True} if entering full screen mode.
1434 */
1435 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001436 for (InCallEventListener listener : inCallEventListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001437 listener.onFullscreenModeChanged(isFullscreenMode);
1438 }
1439 }
1440
linyuh7b86f562017-11-16 11:24:09 -08001441 /** Instruct the in-call activity to show an error dialog or toast for a disconnected call. */
1442 private void showDialogOrToastForDisconnectedCall(DialerCall call) {
wangqibb94ca62018-04-27 14:34:04 -07001443 if (call.getState() != DialerCallState.DISCONNECTED) {
linyuh7b86f562017-11-16 11:24:09 -08001444 return;
Eric Erfanianccca3152017-02-22 16:32:36 -08001445 }
linyuh7b86f562017-11-16 11:24:09 -08001446
1447 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1448 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1449 setDisconnectCauseForMissingAccounts(call);
1450 }
1451
yueg32d9ae72018-04-05 17:35:56 -07001452 if (isActivityStarted()) {
1453 inCallActivity.showDialogOrToastForDisconnectedCall(
1454 new DisconnectMessage(inCallActivity, call));
1455 } else {
1456 CharSequence message = new DisconnectMessage(context, call).toastMessage;
1457 if (message != null) {
1458 Toast.makeText(context, message, Toast.LENGTH_LONG).show();
1459 }
1460 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001461 }
1462
1463 /**
1464 * When the state of in-call changes, this is the first method to get called. It determines if the
1465 * UI needs to be started or finished depending on the new state and does it.
1466 */
1467 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001468 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001469 LogUtil.d(
linyuh183cb712017-12-27 17:02:37 -08001470 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + inCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001471
1472 // TODO: Consider a proper state machine implementation
1473
1474 // If the state isn't changing we have already done any starting/stopping of activities in
1475 // a previous pass...so lets cut out early
linyuh183cb712017-12-27 17:02:37 -08001476 if (newState == inCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001477 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001478 return newState;
1479 }
1480
1481 // A new Incoming call means that the user needs to be notified of the the call (since
1482 // it wasn't them who initiated it). We do this through full screen notifications and
1483 // happens indirectly through {@link StatusBarNotifier}.
1484 //
1485 // The process for incoming calls is as follows:
1486 //
1487 // 1) CallList - Announces existence of new INCOMING call
1488 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1489 // - should be set to INCOMING.
1490 // 3) InCallPresenter - This method is called to see if we need to start or finish
1491 // the app given the new state.
1492 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1493 // StatusBarNotifier explicitly to issue a FullScreen Notification
1494 // that will either start the InCallActivity or show the user a
1495 // top-level notification dialog if the user is in an immersive app.
1496 // That notification can also start the InCallActivity.
1497 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1498 // call InCallPresenter::setActivity() to let the presenter
1499 // know that start-up is complete.
1500 //
1501 // [ AND NOW YOU'RE IN THE CALL. voila! ]
Eric Erfanianccca3152017-02-22 16:32:36 -08001502
1503 // A dialog to show on top of the InCallUI to select a PhoneAccount
1504 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1505
1506 // A new outgoing call indicates that the user just now dialed a number and when that
1507 // happens we need to display the screen immediately or show an account picker dialog if
1508 // no default is set. However, if the main InCallUI is already visible, we do not want to
1509 // re-initiate the start-up animation, so we do not need to do anything here.
1510 //
1511 // It is also possible to go into an intermediate state where the call has been initiated
1512 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1513 // This pending outgoing state can also launch the call screen.
1514 //
1515 // This is different from the incoming call sequence because we do not need to shock the
1516 // user with a top-level notification. Just show the call UI normally.
1517 boolean callCardFragmentVisible =
linyuh183cb712017-12-27 17:02:37 -08001518 inCallActivity != null && inCallActivity.getCallCardFragmentVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001519 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1520 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1521
1522 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1523 // outgoing call process, so the UI should be brought up to show an error dialog.
1524 showCallUi |=
linyuh183cb712017-12-27 17:02:37 -08001525 (InCallState.PENDING_OUTGOING == inCallState
Eric Erfanianccca3152017-02-22 16:32:36 -08001526 && InCallState.INCALL == newState
1527 && !isShowingInCallUi());
1528
1529 // Another exception - InCallActivity is in charge of disconnecting a call with no
1530 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1531 // call so that:
1532 // 1) The call can be disconnected correctly
1533 // 2) The UI comes up and correctly displays the error dialog.
1534 // TODO: Remove these special case conditions by making InCallPresenter a true state
1535 // machine. Telecom should also be the component responsible for disconnecting a call
1536 // with no valid accounts.
1537 showCallUi |=
1538 InCallState.PENDING_OUTGOING == newState
1539 && mainUiNotVisible
linyuh183cb712017-12-27 17:02:37 -08001540 && isCallWithNoValidAccounts(callList.getPendingOutgoingCall());
Eric Erfanianccca3152017-02-22 16:32:36 -08001541
1542 // The only time that we have an instance of mInCallActivity and it isn't started is
1543 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1544 // the activity. When it is finally destroyed, we double check if we should bring it back
1545 // up so we aren't going to lose anything by avoiding a second startup here.
linyuh183cb712017-12-27 17:02:37 -08001546 boolean activityIsFinishing = inCallActivity != null && !isActivityStarted();
Eric Erfanianccca3152017-02-22 16:32:36 -08001547 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001548 LogUtil.i(
1549 "InCallPresenter.startOrFinishUi",
linyuh183cb712017-12-27 17:02:37 -08001550 "Undo the state change: " + newState + " -> " + inCallState);
wangqicf61ca02017-08-31 15:32:55 -07001551 Trace.endSection();
linyuh183cb712017-12-27 17:02:37 -08001552 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001553 }
1554
1555 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1556 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1557 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1558 // outgoing call after user ignore it by pressing home button.
1559 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1560 && !showCallUi
1561 && isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001562 inCallActivity.dismissPendingDialogs();
Eric Erfanianccca3152017-02-22 16:32:36 -08001563 }
1564
yueg4613e8f2018-04-23 13:35:33 -07001565 if ((showCallUi || showAccountPicker) && !shouldStartInBubbleMode()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001566 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001567 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001568 } else if (newState == InCallState.NO_CALLS) {
1569 // The new state is the no calls state. Tear everything down.
yueg8e544862018-04-06 14:34:59 -07001570 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001571 attemptFinishActivity();
1572 attemptCleanup();
1573 }
1574
wangqicf61ca02017-08-31 15:32:55 -07001575 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001576 return newState;
1577 }
1578
1579 /**
1580 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1581 * or PhoneAccounts to select from.
1582 */
1583 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1584
1585 Bundle extras = call.getIntentExtras();
1586 // Initialize the extras bundle to avoid NPE
1587 if (extras == null) {
1588 extras = new Bundle();
1589 }
1590
1591 final List<PhoneAccountHandle> phoneAccountHandles =
1592 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1593
1594 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1595 String scheme = call.getHandle().getScheme();
1596 final String errorMsg =
1597 PhoneAccount.SCHEME_TEL.equals(scheme)
linyuh183cb712017-12-27 17:02:37 -08001598 ? context.getString(R.string.callFailed_simError)
1599 : context.getString(R.string.incall_error_supp_service_unknown);
Eric Erfanianccca3152017-02-22 16:32:36 -08001600 DisconnectCause disconnectCause =
1601 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1602 call.setDisconnectCause(disconnectCause);
1603 }
1604 }
1605
Eric Erfanianccca3152017-02-22 16:32:36 -08001606 /**
1607 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1608 * Calling classes should use this as an indication whether to interact with the
1609 * InCallPresenter or not.
1610 */
1611 public boolean isReadyForTearDown() {
linyuh183cb712017-12-27 17:02:37 -08001612 return inCallActivity == null && !serviceConnected && inCallState == InCallState.NO_CALLS;
Eric Erfanianccca3152017-02-22 16:32:36 -08001613 }
1614
1615 /**
1616 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1617 */
1618 private void attemptCleanup() {
1619 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001620 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001621
1622 cleanupSurfaces();
1623
linyuh183cb712017-12-27 17:02:37 -08001624 isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001625
1626 // blow away stale contact info so that we get fresh data on
1627 // the next set of calls
linyuh183cb712017-12-27 17:02:37 -08001628 if (contactInfoCache != null) {
1629 contactInfoCache.clearCache();
Eric Erfanianccca3152017-02-22 16:32:36 -08001630 }
linyuh183cb712017-12-27 17:02:37 -08001631 contactInfoCache = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001632
linyuh183cb712017-12-27 17:02:37 -08001633 if (proximitySensor != null) {
1634 removeListener(proximitySensor);
1635 proximitySensor.tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -08001636 }
linyuh183cb712017-12-27 17:02:37 -08001637 proximitySensor = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001638
linyuh183cb712017-12-27 17:02:37 -08001639 if (statusBarNotifier != null) {
1640 removeListener(statusBarNotifier);
1641 EnrichedCallComponent.get(context)
Eric Erfaniand8046e52017-04-06 09:41:50 -07001642 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -08001643 .unregisterStateChangedListener(statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001644 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001645
linyuh183cb712017-12-27 17:02:37 -08001646 if (externalCallNotifier != null && externalCallList != null) {
1647 externalCallList.removeExternalCallListener(externalCallNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001648 }
linyuh183cb712017-12-27 17:02:37 -08001649 statusBarNotifier = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001650
linyuh183cb712017-12-27 17:02:37 -08001651 if (callList != null) {
1652 callList.removeListener(this);
1653 callList.removeListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001654 }
linyuh183cb712017-12-27 17:02:37 -08001655 callList = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001656
linyuh183cb712017-12-27 17:02:37 -08001657 context = null;
1658 inCallActivity = null;
1659 manageConferenceActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001660
linyuh183cb712017-12-27 17:02:37 -08001661 listeners.clear();
1662 incomingCallListeners.clear();
1663 detailsListeners.clear();
1664 canAddCallListeners.clear();
1665 orientationListeners.clear();
1666 inCallEventListeners.clear();
1667 inCallUiListeners.clear();
1668 if (!inCallUiLocks.isEmpty()) {
1669 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + inCallUiLocks);
1670 inCallUiLocks.clear();
twyen8efb4952017-10-06 16:35:54 -07001671 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001672 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001673 }
1674 }
1675
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001676 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001677 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
linyuh183cb712017-12-27 17:02:37 -08001678 context.startActivity(
1679 InCallActivity.getIntent(context, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001680 }
1681
1682 public void onServiceBind() {
linyuh183cb712017-12-27 17:02:37 -08001683 serviceBound = true;
Eric Erfanianccca3152017-02-22 16:32:36 -08001684 }
1685
1686 public void onServiceUnbind() {
1687 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -08001688 serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001689 }
1690
1691 public boolean isServiceBound() {
linyuh183cb712017-12-27 17:02:37 -08001692 return serviceBound;
Eric Erfanianccca3152017-02-22 16:32:36 -08001693 }
1694
1695 public void maybeStartRevealAnimation(Intent intent) {
linyuh183cb712017-12-27 17:02:37 -08001696 if (intent == null || inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001697 return;
1698 }
1699 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1700 if (extras == null) {
1701 // Incoming call, just show the in-call UI directly.
1702 return;
1703 }
1704
1705 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1706 // Account selection dialog will show up so don't show the animation.
1707 return;
1708 }
1709
1710 final PhoneAccountHandle accountHandle =
1711 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1712 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001713
yueg32d9ae72018-04-05 17:35:56 -07001714 setBoundAndWaitingForOutgoingCall(true, accountHandle);
1715
yueg4613e8f2018-04-23 13:35:33 -07001716 if (shouldStartInBubbleModeWithExtras(extras)) {
yueg32d9ae72018-04-05 17:35:56 -07001717 LogUtil.i("InCallPresenter.maybeStartRevealAnimation", "shouldStartInBubbleMode");
1718 // Show bubble instead of in call UI
1719 return;
1720 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001721
1722 final Intent activityIntent =
linyuh183cb712017-12-27 17:02:37 -08001723 InCallActivity.getIntent(context, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001724 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
linyuh183cb712017-12-27 17:02:37 -08001725 context.startActivity(activityIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001726 }
1727
1728 /**
1729 * Retrieves the current in-call camera manager instance, creating if necessary.
1730 *
1731 * @return The {@link InCallCameraManager}.
1732 */
1733 public InCallCameraManager getInCallCameraManager() {
1734 synchronized (this) {
linyuh183cb712017-12-27 17:02:37 -08001735 if (inCallCameraManager == null) {
1736 inCallCameraManager = new InCallCameraManager(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001737 }
1738
linyuh183cb712017-12-27 17:02:37 -08001739 return inCallCameraManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001740 }
1741 }
1742
1743 /**
1744 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1745 *
1746 * @param orientation The screen orientation of the device (one of: {@link
1747 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1748 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1749 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1750 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1751 */
1752 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001753 LogUtil.d(
1754 "InCallPresenter.onDeviceOrientationChange",
1755 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001756
linyuh183cb712017-12-27 17:02:37 -08001757 if (callList != null) {
1758 callList.notifyCallsOfDeviceRotation(orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001759 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001760 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001761 }
1762
1763 // Notify listeners of device orientation changed.
linyuh183cb712017-12-27 17:02:37 -08001764 for (InCallOrientationListener listener : orientationListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001765 listener.onDeviceOrientationChanged(orientation);
1766 }
1767 }
1768
1769 /**
1770 * Configures the in-call UI activity so it can change orientations or not. Enables the
1771 * orientation event listener if allowOrientationChange is true, disables it if false.
1772 *
1773 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1774 * landscape. {@code false} if the in-call UI should be locked in portrait.
1775 */
1776 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
linyuh183cb712017-12-27 17:02:37 -08001777 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001778 LogUtil.e(
1779 "InCallPresenter.setInCallAllowsOrientationChange",
1780 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001781 return;
1782 }
linyuh183cb712017-12-27 17:02:37 -08001783 inCallActivity.setAllowOrientationChange(allowOrientationChange);
Eric Erfanianccca3152017-02-22 16:32:36 -08001784 }
1785
1786 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001787 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
linyuh183cb712017-12-27 17:02:37 -08001788 screenTimeoutEnabled = enable;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001789 applyScreenTimeout();
1790 }
1791
1792 private void applyScreenTimeout() {
linyuh183cb712017-12-27 17:02:37 -08001793 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001794 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001795 return;
1796 }
1797
linyuh183cb712017-12-27 17:02:37 -08001798 final Window window = inCallActivity.getWindow();
1799 if (screenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001800 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1801 } else {
1802 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1803 }
1804 }
1805
1806 /**
1807 * Hides or shows the conference manager fragment.
1808 *
1809 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1810 * be hidden.
1811 */
1812 public void showConferenceCallManager(boolean show) {
linyuh183cb712017-12-27 17:02:37 -08001813 if (inCallActivity != null) {
1814 inCallActivity.showConferenceFragment(show);
Eric Erfanianccca3152017-02-22 16:32:36 -08001815 }
linyuh183cb712017-12-27 17:02:37 -08001816 if (!show && manageConferenceActivity != null) {
1817 manageConferenceActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -08001818 }
1819 }
1820
1821 /**
1822 * Determines if the dialpad is visible.
1823 *
1824 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1825 */
1826 public boolean isDialpadVisible() {
linyuh183cb712017-12-27 17:02:37 -08001827 if (inCallActivity == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001828 return false;
1829 }
linyuh183cb712017-12-27 17:02:37 -08001830 return inCallActivity.isDialpadVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001831 }
1832
1833 public ThemeColorManager getThemeColorManager() {
linyuh183cb712017-12-27 17:02:37 -08001834 return themeColorManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001835 }
1836
wangqi8d662ca2017-10-26 11:27:19 -07001837 @VisibleForTesting
1838 public void setThemeColorManager(ThemeColorManager themeColorManager) {
linyuh183cb712017-12-27 17:02:37 -08001839 this.themeColorManager = themeColorManager;
wangqi8d662ca2017-10-26 11:27:19 -07001840 }
1841
Eric Erfanianccca3152017-02-22 16:32:36 -08001842 /** Called when the foreground call changes. */
1843 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh183cb712017-12-27 17:02:37 -08001844 themeColorManager.onForegroundCallChanged(context, newForegroundCall);
1845 if (inCallActivity != null) {
1846 inCallActivity.onForegroundCallChanged(newForegroundCall);
Eric Erfanianccca3152017-02-22 16:32:36 -08001847 }
1848 }
1849
1850 public InCallActivity getActivity() {
linyuh183cb712017-12-27 17:02:37 -08001851 return inCallActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -08001852 }
1853
1854 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1855 public void setActivity(InCallActivity inCallActivity) {
1856 if (inCallActivity == null) {
1857 throw new IllegalArgumentException("registerActivity cannot be called with null");
1858 }
linyuh183cb712017-12-27 17:02:37 -08001859 if (this.inCallActivity != null && this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001860 LogUtil.w(
1861 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001862 }
1863 updateActivity(inCallActivity);
1864 }
1865
1866 ExternalCallNotifier getExternalCallNotifier() {
linyuh183cb712017-12-27 17:02:37 -08001867 return externalCallNotifier;
Eric Erfanianccca3152017-02-22 16:32:36 -08001868 }
1869
1870 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001871 if (localVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001872 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001873 if (context != null) {
1874 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001875 }
linyuh183cb712017-12-27 17:02:37 -08001876 localVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001877 }
linyuh183cb712017-12-27 17:02:37 -08001878 return localVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001879 }
1880
1881 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001882 if (remoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001883 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001884 if (context != null) {
1885 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001886 }
linyuh183cb712017-12-27 17:02:37 -08001887 remoteVideoSurfaceTexture = VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001888 }
linyuh183cb712017-12-27 17:02:37 -08001889 return remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001890 }
1891
1892 void cleanupSurfaces() {
linyuh183cb712017-12-27 17:02:37 -08001893 if (remoteVideoSurfaceTexture != null) {
1894 remoteVideoSurfaceTexture.setDoneWithSurface();
1895 remoteVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001896 }
linyuh183cb712017-12-27 17:02:37 -08001897 if (localVideoSurfaceTexture != null) {
1898 localVideoSurfaceTexture.setDoneWithSurface();
1899 localVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001900 }
1901 }
1902
yueg77cb8e52017-10-27 16:42:51 -07001903 @Override
1904 public void onAudioStateChanged(CallAudioState audioState) {
linyuh183cb712017-12-27 17:02:37 -08001905 if (statusBarNotifier != null) {
1906 statusBarNotifier.updateNotification();
yueg77cb8e52017-10-27 16:42:51 -07001907 }
1908 }
1909
Eric Erfanianccca3152017-02-22 16:32:36 -08001910 /** All the main states of InCallActivity. */
1911 public enum InCallState {
1912 // InCall Screen is off and there are no calls
1913 NO_CALLS,
1914
1915 // Incoming-call screen is up
1916 INCOMING,
1917
1918 // In-call experience is showing
1919 INCALL,
1920
1921 // Waiting for user input before placing outgoing call
1922 WAITING_FOR_ACCOUNT,
1923
1924 // UI is starting up but no call has been initiated yet.
1925 // The UI is waiting for Telecom to respond.
1926 PENDING_OUTGOING,
1927
1928 // User is dialing out
1929 OUTGOING;
1930
1931 public boolean isIncoming() {
1932 return (this == INCOMING);
1933 }
1934
1935 public boolean isConnectingOrConnected() {
1936 return (this == INCOMING || this == OUTGOING || this == INCALL);
1937 }
1938 }
1939
1940 /** Interface implemented by classes that need to know about the InCall State. */
1941 public interface InCallStateListener {
1942
1943 // TODO: Enhance state to contain the call objects instead of passing CallList
1944 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1945 }
1946
1947 public interface IncomingCallListener {
1948
1949 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1950 }
1951
1952 public interface CanAddCallListener {
1953
1954 void onCanAddCallChanged(boolean canAddCall);
1955 }
1956
1957 public interface InCallDetailsListener {
1958
1959 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1960 }
1961
1962 public interface InCallOrientationListener {
1963
1964 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1965 }
1966
1967 /**
1968 * Interface implemented by classes that need to know about events which occur within the In-Call
1969 * UI. Used as a means of communicating between fragments that make up the UI.
1970 */
1971 public interface InCallEventListener {
1972
1973 void onFullscreenModeChanged(boolean isFullscreenMode);
1974 }
1975
1976 public interface InCallUiListener {
1977
1978 void onUiShowing(boolean showing);
1979 }
twyen8efb4952017-10-06 16:35:54 -07001980
1981 private class InCallUiLockImpl implements InCallUiLock {
1982 private final String tag;
1983
1984 private InCallUiLockImpl(String tag) {
1985 this.tag = tag;
1986 }
1987
1988 @MainThread
1989 @Override
1990 public void release() {
1991 Assert.isMainThread();
1992 releaseInCallUiLock(InCallUiLockImpl.this);
1993 }
1994
1995 @Override
1996 public String toString() {
1997 return "InCallUiLock[" + tag + "]";
1998 }
1999 }
2000
2001 @MainThread
2002 public InCallUiLock acquireInCallUiLock(String tag) {
2003 Assert.isMainThread();
2004 InCallUiLock lock = new InCallUiLockImpl(tag);
linyuh183cb712017-12-27 17:02:37 -08002005 inCallUiLocks.add(lock);
twyen8efb4952017-10-06 16:35:54 -07002006 return lock;
2007 }
2008
2009 @MainThread
2010 private void releaseInCallUiLock(InCallUiLock lock) {
2011 Assert.isMainThread();
2012 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
linyuh183cb712017-12-27 17:02:37 -08002013 inCallUiLocks.remove(lock);
2014 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002015 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
linyuh183cb712017-12-27 17:02:37 -08002016 if (inCallState == InCallState.NO_CALLS) {
twyen8efb4952017-10-06 16:35:54 -07002017 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
2018 attemptFinishActivity();
2019 attemptCleanup();
2020 }
2021 }
2022 }
2023
2024 @MainThread
2025 public boolean isInCallUiLocked() {
2026 Assert.isMainThread();
linyuh183cb712017-12-27 17:02:37 -08002027 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002028 return false;
2029 }
linyuh183cb712017-12-27 17:02:37 -08002030 for (InCallUiLock lock : inCallUiLocks) {
twyen8efb4952017-10-06 16:35:54 -07002031 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
2032 }
2033 return true;
2034 }
2035
linyuh183cb712017-12-27 17:02:37 -08002036 private final Set<InCallUiLock> inCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08002037}