blob: a67dab513c7e9cfb29175723e684ef53148fdbf2 [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;
Eric Erfanian8369df02017-05-03 10:27:13 -070055import com.android.dialer.logging.InteractionEvent;
Eric Erfanianccca3152017-02-22 16:32:36 -080056import com.android.dialer.logging.Logger;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070057import com.android.dialer.postcall.PostCall;
twyena4745bd2017-12-12 18:40:11 -080058import com.android.dialer.telecom.TelecomCallUtil;
Eric Erfanianccca3152017-02-22 16:32:36 -080059import com.android.dialer.telecom.TelecomUtil;
60import com.android.dialer.util.TouchPointManager;
61import com.android.incallui.InCallOrientationEventListener.ScreenOrientation;
62import com.android.incallui.answerproximitysensor.PseudoScreenState;
yueg77cb8e52017-10-27 16:42:51 -070063import com.android.incallui.audiomode.AudioModeProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080064import com.android.incallui.call.CallList;
65import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080066import com.android.incallui.call.ExternalCallList;
Eric Erfanianccca3152017-02-22 16:32:36 -080067import com.android.incallui.call.TelecomAdapter;
Eric Erfanian2ca43182017-08-31 06:57:16 -070068import com.android.incallui.disconnectdialog.DisconnectMessage;
twyen8efb4952017-10-06 16:35:54 -070069import com.android.incallui.incalluilock.InCallUiLock;
Eric Erfanianccca3152017-02-22 16:32:36 -080070import com.android.incallui.latencyreport.LatencyReport;
71import com.android.incallui.legacyblocking.BlockedNumberContentObserver;
72import com.android.incallui.spam.SpamCallListListener;
erfaniand05d8992018-03-20 19:42:26 -070073import com.android.incallui.speakeasy.SpeakEasyCallManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080074import com.android.incallui.videosurface.bindings.VideoSurfaceBindings;
75import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
Eric Erfanian90508232017-03-24 09:31:16 -070076import com.android.incallui.videotech.utils.VideoUtils;
yueg32d9ae72018-04-05 17:35:56 -070077import com.google.protobuf.InvalidProtocolBufferException;
Eric Erfanianccca3152017-02-22 16:32:36 -080078import java.util.Collections;
79import java.util.List;
80import java.util.Objects;
81import java.util.Set;
82import java.util.concurrent.ConcurrentHashMap;
83import java.util.concurrent.CopyOnWriteArrayList;
84import java.util.concurrent.atomic.AtomicBoolean;
85
86/**
87 * Takes updates from the CallList and notifies the InCallActivity (UI) of the changes. Responsible
88 * for starting the activity for a new call and finishing the activity when all calls are
89 * disconnected. Creates and manages the in-call state and provides a listener pattern for the
90 * presenters that want to listen in on the in-call state changes. TODO: This class has become more
91 * of a state machine at this point. Consider renaming.
92 */
yueg77cb8e52017-10-27 16:42:51 -070093public class InCallPresenter implements CallList.Listener, AudioModeProvider.AudioModeListener {
Eric Erfanian2ca43182017-08-31 06:57:16 -070094 private static final String PIXEL2017_SYSTEM_FEATURE =
95 "com.google.android.feature.PIXEL_2017_EXPERIENCE";
yueg32d9ae72018-04-05 17:35:56 -070096 private static final String CALL_CONFIGURATION_EXTRA = "call_configuration";
Eric Erfanianccca3152017-02-22 16:32:36 -080097
98 private static final long BLOCK_QUERY_TIMEOUT_MS = 1000;
99
100 private static final Bundle EMPTY_EXTRAS = new Bundle();
101
linyuh183cb712017-12-27 17:02:37 -0800102 private static InCallPresenter inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -0800103
104 /**
105 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is load factor before
106 * resizing, 1 means we only expect a single thread to access the map so make only a single shard
107 */
linyuh183cb712017-12-27 17:02:37 -0800108 private final Set<InCallStateListener> listeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800109 Collections.newSetFromMap(new ConcurrentHashMap<InCallStateListener, Boolean>(8, 0.9f, 1));
110
linyuh183cb712017-12-27 17:02:37 -0800111 private final List<IncomingCallListener> incomingCallListeners = new CopyOnWriteArrayList<>();
112 private final Set<InCallDetailsListener> detailsListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800113 Collections.newSetFromMap(new ConcurrentHashMap<InCallDetailsListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800114 private final Set<CanAddCallListener> canAddCallListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800115 Collections.newSetFromMap(new ConcurrentHashMap<CanAddCallListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800116 private final Set<InCallUiListener> inCallUiListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800117 Collections.newSetFromMap(new ConcurrentHashMap<InCallUiListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800118 private final Set<InCallOrientationListener> orientationListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800119 Collections.newSetFromMap(
120 new ConcurrentHashMap<InCallOrientationListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800121 private final Set<InCallEventListener> inCallEventListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800122 Collections.newSetFromMap(new ConcurrentHashMap<InCallEventListener, Boolean>(8, 0.9f, 1));
123
linyuh183cb712017-12-27 17:02:37 -0800124 private StatusBarNotifier statusBarNotifier;
125 private ExternalCallNotifier externalCallNotifier;
126 private ContactInfoCache contactInfoCache;
127 private Context context;
128 private final OnCheckBlockedListener onCheckBlockedListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800129 new OnCheckBlockedListener() {
130 @Override
131 public void onCheckComplete(final Integer id) {
132 if (id != null && id != FilteredNumberAsyncQueryHandler.INVALID_ID) {
133 // Silence the ringer now to prevent ringing and vibration before the call is
134 // terminated when Telecom attempts to add it.
linyuh183cb712017-12-27 17:02:37 -0800135 TelecomUtil.silenceRinger(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800136 }
137 }
138 };
linyuh183cb712017-12-27 17:02:37 -0800139 private CallList callList;
140 private ExternalCallList externalCallList;
141 private InCallActivity inCallActivity;
142 private ManageConferenceActivity manageConferenceActivity;
143 private final android.telecom.Call.Callback callCallback =
Eric Erfanianccca3152017-02-22 16:32:36 -0800144 new android.telecom.Call.Callback() {
145 @Override
146 public void onPostDialWait(
147 android.telecom.Call telecomCall, String remainingPostDialSequence) {
linyuh183cb712017-12-27 17:02:37 -0800148 final DialerCall call = callList.getDialerCallFromTelecomCall(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800149 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700150 LogUtil.w(
151 "InCallPresenter.onPostDialWait",
152 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800153 return;
154 }
155 onPostDialCharWait(call.getId(), remainingPostDialSequence);
156 }
157
158 @Override
159 public void onDetailsChanged(
160 android.telecom.Call telecomCall, android.telecom.Call.Details details) {
linyuh183cb712017-12-27 17:02:37 -0800161 final DialerCall call = callList.getDialerCallFromTelecomCall(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800162 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700163 LogUtil.w(
164 "InCallPresenter.onDetailsChanged",
165 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800166 return;
167 }
168
169 if (details.hasProperty(Details.PROPERTY_IS_EXTERNAL_CALL)
linyuh183cb712017-12-27 17:02:37 -0800170 && !externalCallList.isCallTracked(telecomCall)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800171
172 // A regular call became an external call so swap call lists.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700173 LogUtil.i("InCallPresenter.onDetailsChanged", "Call became external: " + telecomCall);
linyuh183cb712017-12-27 17:02:37 -0800174 callList.onInternalCallMadeExternal(context, telecomCall);
175 externalCallList.onCallAdded(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800176 return;
177 }
178
linyuh183cb712017-12-27 17:02:37 -0800179 for (InCallDetailsListener listener : detailsListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800180 listener.onDetailsChanged(call, details);
181 }
182 }
183
184 @Override
185 public void onConferenceableCallsChanged(
186 android.telecom.Call telecomCall, List<android.telecom.Call> conferenceableCalls) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700187 LogUtil.i(
188 "InCallPresenter.onConferenceableCallsChanged",
189 "onConferenceableCallsChanged: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800190 onDetailsChanged(telecomCall, telecomCall.getDetails());
191 }
192 };
linyuh183cb712017-12-27 17:02:37 -0800193 private InCallState inCallState = InCallState.NO_CALLS;
194 private ProximitySensor proximitySensor;
195 private final PseudoScreenState pseudoScreenState = new PseudoScreenState();
196 private boolean serviceConnected;
197 private InCallCameraManager inCallCameraManager;
198 private FilteredNumberAsyncQueryHandler filteredQueryHandler;
199 private CallList.Listener spamCallListListener;
twyen27c37182018-04-24 11:48:12 -0700200 private CallList.Listener activeCallsListener;
Eric Erfanianccca3152017-02-22 16:32:36 -0800201 /** Whether or not we are currently bound and waiting for Telecom to send us a new call. */
linyuh183cb712017-12-27 17:02:37 -0800202 private boolean boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -0800203 /** Determines if the InCall UI is in fullscreen mode or not. */
linyuh183cb712017-12-27 17:02:37 -0800204 private boolean isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800205
linyuh183cb712017-12-27 17:02:37 -0800206 private boolean screenTimeoutEnabled = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700207
linyuh183cb712017-12-27 17:02:37 -0800208 private PhoneStateListener phoneStateListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800209 new PhoneStateListener() {
210 @Override
211 public void onCallStateChanged(int state, String incomingNumber) {
212 if (state == TelephonyManager.CALL_STATE_RINGING) {
linyuh183cb712017-12-27 17:02:37 -0800213 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800214 return;
215 }
216 // Check if the number is blocked, to silence the ringer.
linyuh183cb712017-12-27 17:02:37 -0800217 String countryIso = GeoUtil.getCurrentCountryIso(context);
218 filteredQueryHandler.isBlockedNumber(
219 onCheckBlockedListener, incomingNumber, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800220 }
221 }
222 };
twyena4745bd2017-12-12 18:40:11 -0800223
Eric Erfanianccca3152017-02-22 16:32:36 -0800224 /** Whether or not InCallService is bound to Telecom. */
linyuh183cb712017-12-27 17:02:37 -0800225 private boolean serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800226
227 /**
228 * When configuration changes Android kills the current activity and starts a new one. The flag is
229 * used to check if full clean up is necessary (activity is stopped and new activity won't be
230 * started), or if a new activity will be started right after the current one is destroyed, and
231 * therefore no need in release all resources.
232 */
linyuh183cb712017-12-27 17:02:37 -0800233 private boolean isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800234
linyuh183cb712017-12-27 17:02:37 -0800235 private boolean awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800236
linyuh183cb712017-12-27 17:02:37 -0800237 private ExternalCallList.ExternalCallListener externalCallListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800238 new ExternalCallList.ExternalCallListener() {
239
240 @Override
241 public void onExternalCallPulled(android.telecom.Call call) {
242 // Note: keep this code in sync with InCallPresenter#onCallAdded
243 LatencyReport latencyReport = new LatencyReport(call);
244 latencyReport.onCallBlockingDone();
245 // Note: External calls do not require spam checking.
linyuh183cb712017-12-27 17:02:37 -0800246 callList.onCallAdded(context, call, latencyReport);
247 call.registerCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800248 }
249
250 @Override
251 public void onExternalCallAdded(android.telecom.Call call) {
252 // No-op
253 }
254
255 @Override
256 public void onExternalCallRemoved(android.telecom.Call call) {
257 // No-op
258 }
259
260 @Override
261 public void onExternalCallUpdated(android.telecom.Call call) {
262 // No-op
263 }
264 };
265
linyuh183cb712017-12-27 17:02:37 -0800266 private ThemeColorManager themeColorManager;
267 private VideoSurfaceTexture localVideoSurfaceTexture;
268 private VideoSurfaceTexture remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -0800269
erfaniand05d8992018-03-20 19:42:26 -0700270 private SpeakEasyCallManager speakEasyCallManager;
271
yueg8e544862018-04-06 14:34:59 -0700272 private boolean audioRouteSetForBubbleMode;
yueg32d9ae72018-04-05 17:35:56 -0700273
Eric Erfanian10b34a52017-05-04 08:23:17 -0700274 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800275 @VisibleForTesting
276 InCallPresenter() {}
277
278 public static synchronized InCallPresenter getInstance() {
linyuh183cb712017-12-27 17:02:37 -0800279 if (inCallPresenter == null) {
wangqic8cf79e2017-10-17 09:21:00 -0700280 Trace.beginSection("InCallPresenter.Constructor");
linyuh183cb712017-12-27 17:02:37 -0800281 inCallPresenter = new InCallPresenter();
wangqic8cf79e2017-10-17 09:21:00 -0700282 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800283 }
linyuh183cb712017-12-27 17:02:37 -0800284 return inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -0800285 }
286
Eric Erfanian10b34a52017-05-04 08:23:17 -0700287 @VisibleForTesting
288 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
linyuh183cb712017-12-27 17:02:37 -0800289 InCallPresenter.inCallPresenter = inCallPresenter;
Eric Erfanian10b34a52017-05-04 08:23:17 -0700290 }
291
Eric Erfanianccca3152017-02-22 16:32:36 -0800292 /**
293 * Determines whether or not a call has no valid phone accounts that can be used to make the call
294 * with. Emergency calls do not require a phone account.
295 *
296 * @param call to check accounts for.
297 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
298 * call contains a phone account that could be used to initiate it with, or is an emergency
299 * call.
300 */
301 public static boolean isCallWithNoValidAccounts(DialerCall call) {
302 if (call != null && !call.isEmergencyCall()) {
303 Bundle extras = call.getIntentExtras();
304
305 if (extras == null) {
306 extras = EMPTY_EXTRAS;
307 }
308
309 final List<PhoneAccountHandle> phoneAccountHandles =
310 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
311
312 if ((call.getAccountHandle() == null
313 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700314 LogUtil.i(
315 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800316 return true;
317 }
318 }
319 return false;
320 }
321
322 public InCallState getInCallState() {
linyuh183cb712017-12-27 17:02:37 -0800323 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800324 }
325
326 public CallList getCallList() {
linyuh183cb712017-12-27 17:02:37 -0800327 return callList;
Eric Erfanianccca3152017-02-22 16:32:36 -0800328 }
329
330 public void setUp(
331 @NonNull Context context,
332 CallList callList,
333 ExternalCallList externalCallList,
334 StatusBarNotifier statusBarNotifier,
335 ExternalCallNotifier externalCallNotifier,
336 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700337 ProximitySensor proximitySensor,
erfaniand05d8992018-03-20 19:42:26 -0700338 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler,
yueg4613e8f2018-04-23 13:35:33 -0700339 @NonNull SpeakEasyCallManager speakEasyCallManager) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700340 Trace.beginSection("InCallPresenter.setUp");
linyuh183cb712017-12-27 17:02:37 -0800341 if (serviceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700342 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
linyuh183cb712017-12-27 17:02:37 -0800343 if (context != this.context || callList != this.callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800344 throw new IllegalStateException();
345 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700346 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800347 return;
348 }
349
350 Objects.requireNonNull(context);
linyuh183cb712017-12-27 17:02:37 -0800351 this.context = context;
Eric Erfanianccca3152017-02-22 16:32:36 -0800352
linyuh183cb712017-12-27 17:02:37 -0800353 this.contactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800354
linyuh183cb712017-12-27 17:02:37 -0800355 this.statusBarNotifier = statusBarNotifier;
356 this.externalCallNotifier = externalCallNotifier;
357 addListener(this.statusBarNotifier);
358 EnrichedCallComponent.get(this.context)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700359 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -0800360 .registerStateChangedListener(this.statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800361
linyuh183cb712017-12-27 17:02:37 -0800362 this.proximitySensor = proximitySensor;
363 addListener(this.proximitySensor);
Eric Erfanianccca3152017-02-22 16:32:36 -0800364
linyuh183cb712017-12-27 17:02:37 -0800365 if (themeColorManager == null) {
366 themeColorManager =
367 new ThemeColorManager(new InCallUIMaterialColorMapUtils(this.context.getResources()));
wangqi8d662ca2017-10-26 11:27:19 -0700368 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800369
linyuh183cb712017-12-27 17:02:37 -0800370 this.callList = callList;
371 this.externalCallList = externalCallList;
372 externalCallList.addExternalCallListener(this.externalCallNotifier);
373 externalCallList.addExternalCallListener(externalCallListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800374
375 // This only gets called by the service so this is okay.
linyuh183cb712017-12-27 17:02:37 -0800376 serviceConnected = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800377
378 // The final thing we do in this set up is add ourselves as a listener to CallList. This
379 // will kick off an update and the whole process can start.
linyuh183cb712017-12-27 17:02:37 -0800380 this.callList.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800381
382 // Create spam call list listener and add it to the list of listeners
linyuh183cb712017-12-27 17:02:37 -0800383 spamCallListListener =
zachh6a4cebd2017-10-24 17:10:06 -0700384 new SpamCallListListener(
385 context, DialerExecutorComponent.get(context).dialerExecutorFactory());
linyuh183cb712017-12-27 17:02:37 -0800386 this.callList.addListener(spamCallListListener);
twyen27c37182018-04-24 11:48:12 -0700387 activeCallsListener = new ActiveCallsCallListListener(context);
388 this.callList.addListener(activeCallsListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800389
390 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800391
linyuh183cb712017-12-27 17:02:37 -0800392 filteredQueryHandler = filteredNumberQueryHandler;
erfaniand05d8992018-03-20 19:42:26 -0700393 this.speakEasyCallManager = speakEasyCallManager;
linyuh183cb712017-12-27 17:02:37 -0800394 this.context
Eric Erfanianccca3152017-02-22 16:32:36 -0800395 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800396 .listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800397
yueg77cb8e52017-10-27 16:42:51 -0700398 AudioModeProvider.getInstance().addListener(this);
399
roldenburg42099012018-04-09 14:47:15 -0700400 // Add listener to notify Telephony process when the incoming call screen is started or
401 // finished. This is for hiding USSD dialog because the incoming call screen should have
402 // higher precedence over this dialog.
403 MotorolaInCallUiNotifier motorolaInCallUiNotifier = new MotorolaInCallUiNotifier(context);
404 addInCallUiListener(motorolaInCallUiNotifier);
405 addListener(motorolaInCallUiNotifier);
yueg7f5acbe2018-01-10 13:50:29 -0800406
Eric Erfanian2ca43182017-08-31 06:57:16 -0700407 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
408 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800409 }
410
411 /**
yueg32d9ae72018-04-05 17:35:56 -0700412 * Return whether we should start call in bubble mode and not show InCallActivity. The call mode
413 * should be set in CallConfiguration in EXTRA_OUTGOING_CALL_EXTRAS when starting a call intent.
414 */
yueg4613e8f2018-04-23 13:35:33 -0700415 private boolean shouldStartInBubbleMode() {
yueg32d9ae72018-04-05 17:35:56 -0700416 if (!ReturnToCallController.isEnabled(context)) {
417 return false;
418 }
yueg4613e8f2018-04-23 13:35:33 -0700419
420 // We only start in Bubble mode for outgoing call
421 DialerCall dialerCall = callList.getPendingOutgoingCall();
422 if (dialerCall == null) {
423 dialerCall = callList.getOutgoingCall();
424 }
425 if (dialerCall == null) {
yueg32d9ae72018-04-05 17:35:56 -0700426 return false;
427 }
yueg4613e8f2018-04-23 13:35:33 -0700428
429 Bundle extras = dialerCall.getIntentExtras();
430 return shouldStartInBubbleModeWithExtras(extras);
431 }
432
433 private boolean shouldStartInBubbleModeWithExtras(Bundle outgoingExtras) {
434 if (!ReturnToCallController.isEnabled(context)) {
435 return false;
436 }
437
438 if (outgoingExtras == null) {
439 return false;
440 }
441 byte[] callConfigurationByteArray = outgoingExtras.getByteArray(CALL_CONFIGURATION_EXTRA);
yueg32d9ae72018-04-05 17:35:56 -0700442 if (callConfigurationByteArray == null) {
443 return false;
444 }
445 try {
446 CallConfiguration callConfiguration = CallConfiguration.parseFrom(callConfigurationByteArray);
447 LogUtil.i(
448 "InCallPresenter.shouldStartInBubbleMode",
449 "call mode: " + callConfiguration.getCallMode());
450 return callConfiguration.getCallMode() == Mode.BUBBLE;
451 } catch (InvalidProtocolBufferException e) {
452 return false;
453 }
454 }
455
456 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800457 * Called when the telephony service has disconnected from us. This will happen when there are no
458 * more active calls. However, we may still want to continue showing the UI for certain cases like
459 * showing "Call Ended". What we really want is to wait for the activity and the service to both
460 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
461 * secondary method that performs the aforementioned logic.
462 */
463 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700464 LogUtil.d("InCallPresenter.tearDown", "tearDown");
linyuh183cb712017-12-27 17:02:37 -0800465 callList.clearOnDisconnect();
Eric Erfanianccca3152017-02-22 16:32:36 -0800466
linyuh183cb712017-12-27 17:02:37 -0800467 serviceConnected = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800468
linyuh183cb712017-12-27 17:02:37 -0800469 context
Eric Erfanianccca3152017-02-22 16:32:36 -0800470 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800471 .listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800472
473 attemptCleanup();
474 VideoPauseController.getInstance().tearDown();
yueg77cb8e52017-10-27 16:42:51 -0700475 AudioModeProvider.getInstance().removeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800476 }
477
478 private void attemptFinishActivity() {
linyuh183cb712017-12-27 17:02:37 -0800479 screenTimeoutEnabled = true;
480 final boolean doFinish = (inCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700481 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800482 if (doFinish) {
linyuh183cb712017-12-27 17:02:37 -0800483 inCallActivity.setExcludeFromRecents(true);
484 inCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800485 }
486 }
487
488 /**
489 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
490 * for more insight on the tear-down process.
491 */
492 public void unsetActivity(InCallActivity inCallActivity) {
493 if (inCallActivity == null) {
494 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
495 }
linyuh183cb712017-12-27 17:02:37 -0800496 if (this.inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700497 LogUtil.i(
498 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800499 return;
500 }
linyuh183cb712017-12-27 17:02:37 -0800501 if (this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700502 LogUtil.w(
503 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800504 "Second instance of InCallActivity is trying to unregister when another"
505 + " instance is active. Ignoring.");
506 return;
507 }
508 updateActivity(null);
509 }
510
511 /**
512 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
513 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
514 */
515 private void updateActivity(InCallActivity inCallActivity) {
wangqi9982f0d2017-10-11 17:46:07 -0700516 Trace.beginSection("InCallPresenter.updateActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800517 boolean updateListeners = false;
518 boolean doAttemptCleanup = false;
519
520 if (inCallActivity != null) {
linyuh183cb712017-12-27 17:02:37 -0800521 if (this.inCallActivity == null) {
522 context = inCallActivity.getApplicationContext();
Eric Erfanianccca3152017-02-22 16:32:36 -0800523 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700524 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800525 } else {
526 // since setActivity is called onStart(), it can be called multiple times.
527 // This is fine and ignorable, but we do not want to update the world every time
528 // this happens (like going to/from background) so we do not set updateListeners.
529 }
530
linyuh183cb712017-12-27 17:02:37 -0800531 this.inCallActivity = inCallActivity;
532 this.inCallActivity.setExcludeFromRecents(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800533
534 // By the time the UI finally comes up, the call may already be disconnected.
535 // If that's the case, we may need to show an error dialog.
linyuh183cb712017-12-27 17:02:37 -0800536 if (callList != null && callList.getDisconnectedCall() != null) {
537 showDialogOrToastForDisconnectedCall(callList.getDisconnectedCall());
Eric Erfanianccca3152017-02-22 16:32:36 -0800538 }
539
540 // When the UI comes up, we need to first check the in-call state.
541 // If we are showing NO_CALLS, that means that a call probably connected and
542 // then immediately disconnected before the UI was able to come up.
543 // If we dont have any calls, start tearing down the UI instead.
544 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
545 // it has been set.
linyuh183cb712017-12-27 17:02:37 -0800546 if (inCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700547 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800548 attemptFinishActivity();
wangqi9982f0d2017-10-11 17:46:07 -0700549 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800550 return;
551 }
552 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700553 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800554 updateListeners = true;
linyuh183cb712017-12-27 17:02:37 -0800555 this.inCallActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800556
557 // We attempt cleanup for the destroy case but only after we recalculate the state
558 // to see if we need to come back up or stay shut down. This is why we do the
559 // cleanup after the call to onCallListChange() instead of directly here.
560 doAttemptCleanup = true;
561 }
562
563 // Messages can come from the telephony layer while the activity is coming up
564 // and while the activity is going down. So in both cases we need to recalculate what
565 // state we should be in after they complete.
566 // Examples: (1) A new incoming call could come in and then get disconnected before
567 // the activity is created.
568 // (2) All calls could disconnect and then get a new incoming call before the
569 // activity is destroyed.
570 //
Eric Erfanian938468d2017-10-24 14:05:52 -0700571 // a bug - We previously had a check for mServiceConnected here as well, but there are
Eric Erfanianccca3152017-02-22 16:32:36 -0800572 // cases where we need to recalculate the current state even if the service in not
573 // connected. In particular the case where startOrFinish() is called while the app is
574 // already finish()ing. In that case, we skip updating the state with the knowledge that
575 // we will check again once the activity has finished. That means we have to recalculate the
576 // state here even if the service is disconnected since we may not have finished a state
577 // transition while finish()ing.
578 if (updateListeners) {
linyuh183cb712017-12-27 17:02:37 -0800579 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800580 }
581
582 if (doAttemptCleanup) {
583 attemptCleanup();
584 }
wangqi9982f0d2017-10-11 17:46:07 -0700585 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800586 }
587
erfanian12243de2018-04-17 12:17:08 -0700588 public SpeakEasyCallManager getSpeakEasyCallManager() {
589 return this.speakEasyCallManager;
590 }
591
Eric Erfanianccca3152017-02-22 16:32:36 -0800592 public void setManageConferenceActivity(
593 @Nullable ManageConferenceActivity manageConferenceActivity) {
linyuh183cb712017-12-27 17:02:37 -0800594 this.manageConferenceActivity = manageConferenceActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -0800595 }
596
597 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700598 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800599 bringToForeground(showDialpad);
600 }
601
602 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700603 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800604 LatencyReport latencyReport = new LatencyReport(call);
605 if (shouldAttemptBlocking(call)) {
606 maybeBlockCall(call, latencyReport);
607 } else {
608 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800609 externalCallList.onCallAdded(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800610 } else {
611 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800612 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800613 }
614 }
615
616 // Since a call has been added we are no longer waiting for Telecom to send us a call.
617 setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -0800618 call.registerCallback(callCallback);
zachh78e54ac2017-12-05 16:38:35 -0800619 // TODO(maxwelb): Return the future in recordPhoneLookupInfo and propagate.
linyuh183cb712017-12-27 17:02:37 -0800620 PhoneLookupHistoryRecorder.recordPhoneLookupInfo(context.getApplicationContext(), call);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700621 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800622 }
623
624 private boolean shouldAttemptBlocking(android.telecom.Call call) {
625 if (call.getState() != android.telecom.Call.STATE_RINGING) {
626 return false;
627 }
linyuh183cb712017-12-27 17:02:37 -0800628 if (!UserManagerCompat.isUserUnlocked(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700629 LogUtil.i(
630 "InCallPresenter.shouldAttemptBlocking",
631 "not attempting to block incoming call because user is locked");
632 return false;
633 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800634 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700635 LogUtil.i(
636 "InCallPresenter.shouldAttemptBlocking",
637 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800638 return false;
639 }
linyuh183cb712017-12-27 17:02:37 -0800640 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700641 LogUtil.i(
642 "InCallPresenter.shouldAttemptBlocking",
643 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800644 return false;
645 }
646 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
647 return false;
648 }
linyuh183cb712017-12-27 17:02:37 -0800649 if (FilteredNumberCompat.useNewFiltering(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700650 LogUtil.i(
651 "InCallPresenter.shouldAttemptBlocking",
652 "not attempting to block incoming call because framework blocking is in use");
653 return false;
654 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800655 return true;
656 }
657
658 /**
659 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
660 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
661 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
662 * call anyways.
663 */
664 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
linyuh183cb712017-12-27 17:02:37 -0800665 final String countryIso = GeoUtil.getCurrentCountryIso(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800666 final String number = TelecomCallUtil.getNumber(call);
667 final long timeAdded = System.currentTimeMillis();
668
669 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
670 // main UI thread. It is needed so we can change its value within different scopes, since
671 // that cannot be done with a final boolean.
672 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
673
674 final Handler handler = new Handler();
675
676 // Proceed if the query is slow; the call may still be blocked after the query returns.
677 final Runnable runnable =
678 new Runnable() {
679 @Override
680 public void run() {
681 hasTimedOut.set(true);
682 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800683 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800684 }
685 };
686 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
687
688 OnCheckBlockedListener onCheckBlockedListener =
689 new OnCheckBlockedListener() {
690 @Override
691 public void onCheckComplete(final Integer id) {
692 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700693 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800694 return;
695 }
696 if (!hasTimedOut.get()) {
697 handler.removeCallbacks(runnable);
698 }
699 if (id == null) {
700 if (!hasTimedOut.get()) {
701 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800702 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800703 }
704 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700705 LogUtil.d(
706 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800707 if (!hasTimedOut.get()) {
708 handler.removeCallbacks(runnable);
709
710 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800711 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800712 }
713 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700714 LogUtil.i(
715 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800716 call.reject(false, null);
linyuh183cb712017-12-27 17:02:37 -0800717 Logger.get(context).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
Eric Erfanianccca3152017-02-22 16:32:36 -0800718
719 /*
720 * If mContext is null, then the InCallPresenter was torn down before the
721 * block check had a chance to complete. The context is no longer valid, so
722 * don't attempt to remove the call log entry.
723 */
linyuh183cb712017-12-27 17:02:37 -0800724 if (context == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800725 return;
726 }
727 // Register observer to update the call log.
728 // BlockedNumberContentObserver will unregister after successful log or timeout.
729 BlockedNumberContentObserver contentObserver =
linyuh183cb712017-12-27 17:02:37 -0800730 new BlockedNumberContentObserver(context, new Handler(), number, timeAdded);
Eric Erfanianccca3152017-02-22 16:32:36 -0800731 contentObserver.register();
732 }
733 }
734 };
735
linyuh183cb712017-12-27 17:02:37 -0800736 filteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800737 }
738
739 public void onCallRemoved(android.telecom.Call call) {
740 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800741 externalCallList.onCallRemoved(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800742 } else {
linyuh183cb712017-12-27 17:02:37 -0800743 callList.onCallRemoved(context, call);
744 call.unregisterCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800745 }
746 }
747
748 public void onCanAddCallChanged(boolean canAddCall) {
linyuh183cb712017-12-27 17:02:37 -0800749 for (CanAddCallListener listener : canAddCallListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800750 listener.onCanAddCallChanged(canAddCall);
751 }
752 }
753
754 @Override
755 public void onWiFiToLteHandover(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -0700756 if (call.hasShownWiFiToLteHandoverToast()) {
757 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800758 }
yueg32d9ae72018-04-05 17:35:56 -0700759
760 Toast.makeText(context, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG)
761 .show();
762 call.setHasShownWiFiToLteHandoverToast();
Eric Erfanianccca3152017-02-22 16:32:36 -0800763 }
764
765 @Override
766 public void onHandoverToWifiFailed(DialerCall call) {
linyuh183cb712017-12-27 17:02:37 -0800767 if (inCallActivity != null) {
768 inCallActivity.showDialogOrToastForWifiHandoverFailure(call);
yueg32d9ae72018-04-05 17:35:56 -0700769 } else {
770 Toast.makeText(context, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT)
771 .show();
Eric Erfanianccca3152017-02-22 16:32:36 -0800772 }
773 }
774
Eric Erfanianc857f902017-05-15 14:05:33 -0700775 @Override
776 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
777 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
linyuh183cb712017-12-27 17:02:37 -0800778 if (inCallActivity != null) {
779 inCallActivity.showDialogForInternationalCallOnWifi(call);
Eric Erfanianc857f902017-05-15 14:05:33 -0700780 }
781 }
782
Eric Erfanianccca3152017-02-22 16:32:36 -0800783 /**
784 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
785 * app based on the information it gets from CallList. Dispatches the in-call state to all
786 * listeners. Can trigger the creation or destruction of the UI based on the states that is
787 * calculates.
788 */
789 @Override
790 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700791 Trace.beginSection("InCallPresenter.onCallListChange");
linyuh183cb712017-12-27 17:02:37 -0800792 if (inCallActivity != null && inCallActivity.isInCallScreenAnimating()) {
793 awaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700794 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800795 return;
796 }
797 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700798 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800799 return;
800 }
801
linyuh183cb712017-12-27 17:02:37 -0800802 awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800803
804 InCallState newState = getPotentialStateFromCallList(callList);
linyuh183cb712017-12-27 17:02:37 -0800805 InCallState oldState = inCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700806 LogUtil.d(
807 "InCallPresenter.onCallListChange",
808 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700809
810 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
811 // incall activity for that call will still exist (even if it's not visible). In the case of
812 // an incoming call in that situation, just disconnect that "waiting for account" call and
813 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
Eric Erfanian938468d2017-10-24 14:05:52 -0700814 // a bug for more details.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700815 DialerCall waitingForAccountCall;
816 if (newState == InCallState.INCOMING
817 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
818 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700819 // The InCallActivity might be destroyed or not started yet at this point.
820 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800821 inCallActivity.dismissPendingDialogs();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700822 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700823 }
824
Eric Erfanianccca3152017-02-22 16:32:36 -0800825 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700826 LogUtil.d(
827 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800828
829 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700830 LogUtil.i(
831 "InCallPresenter.onCallListChange",
832 "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800833 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800834
yuegb9103042018-03-30 12:12:25 -0700835 // Foreground call changed
836 DialerCall primary = null;
837 if (newState == InCallState.INCOMING) {
838 primary = callList.getIncomingCall();
839 } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
840 primary = callList.getOutgoingCall();
841 if (primary == null) {
842 primary = callList.getPendingOutgoingCall();
843 }
844 } else if (newState == InCallState.INCALL) {
845 primary = getCallToDisplay(callList, null, false);
846 }
847 if (primary != null) {
848 onForegroundCallChanged(primary);
849 }
850
Eric Erfanianccca3152017-02-22 16:32:36 -0800851 // notify listeners of new state
linyuh183cb712017-12-27 17:02:37 -0800852 for (InCallStateListener listener : listeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700853 LogUtil.d(
854 "InCallPresenter.onCallListChange",
linyuh183cb712017-12-27 17:02:37 -0800855 "Notify " + listener + " of state " + inCallState.toString());
856 listener.onStateChange(oldState, inCallState, callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800857 }
858
859 if (isActivityStarted()) {
860 final boolean hasCall =
861 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
linyuh183cb712017-12-27 17:02:37 -0800862 inCallActivity.dismissKeyguard(hasCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800863 }
twyen27c37182018-04-24 11:48:12 -0700864
Eric Erfanian2ca43182017-08-31 06:57:16 -0700865 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800866 }
867
yuegb9103042018-03-30 12:12:25 -0700868 /**
869 * Get the highest priority call to display. Goes through the calls and chooses which to return
870 * based on priority of which type of call to display to the user. Callers can use the "ignore"
871 * feature to get the second best call by passing a previously found primary call as ignore.
872 *
873 * @param ignore A call to ignore if found.
874 */
875 static DialerCall getCallToDisplay(
876 CallList callList, DialerCall ignore, boolean skipDisconnected) {
877 // Active calls come second. An active call always gets precedent.
878 DialerCall retval = callList.getActiveCall();
879 if (retval != null && retval != ignore) {
880 return retval;
881 }
882
883 // Sometimes there is intemediate state that two calls are in active even one is about
884 // to be on hold.
885 retval = callList.getSecondActiveCall();
886 if (retval != null && retval != ignore) {
887 return retval;
888 }
889
890 // Disconnected calls get primary position if there are no active calls
891 // to let user know quickly what call has disconnected. Disconnected
892 // calls are very short lived.
893 if (!skipDisconnected) {
894 retval = callList.getDisconnectingCall();
895 if (retval != null && retval != ignore) {
896 return retval;
897 }
898 retval = callList.getDisconnectedCall();
899 if (retval != null && retval != ignore) {
900 return retval;
901 }
902 }
903
904 // Then we go to background call (calls on hold)
905 retval = callList.getBackgroundCall();
906 if (retval != null && retval != ignore) {
907 return retval;
908 }
909
910 // Lastly, we go to a second background call.
911 retval = callList.getSecondBackgroundCall();
912
913 return retval;
914 }
915
Eric Erfanianccca3152017-02-22 16:32:36 -0800916 /** Called when there is a new incoming call. */
917 @Override
918 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700919 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800920 InCallState newState = startOrFinishUi(InCallState.INCOMING);
linyuh183cb712017-12-27 17:02:37 -0800921 InCallState oldState = inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800922
Eric Erfanian2ca43182017-08-31 06:57:16 -0700923 LogUtil.i(
924 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800925 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800926
wangqicf61ca02017-08-31 15:32:55 -0700927 Trace.beginSection("listener.onIncomingCall");
linyuh183cb712017-12-27 17:02:37 -0800928 for (IncomingCallListener listener : incomingCallListeners) {
929 listener.onIncomingCall(oldState, inCallState, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800930 }
wangqicf61ca02017-08-31 15:32:55 -0700931 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800932
wangqicf61ca02017-08-31 15:32:55 -0700933 Trace.beginSection("onPrimaryCallStateChanged");
linyuh183cb712017-12-27 17:02:37 -0800934 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800935 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800936 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800937 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700938 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700939 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800940 }
941
942 @Override
943 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700944 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
linyuh183cb712017-12-27 17:02:37 -0800945 && inCallState == InCallPresenter.InCallState.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800946 LogUtil.i(
947 "InCallPresenter.onUpgradeToVideo",
948 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700949 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800950 }
951
linyuh183cb712017-12-27 17:02:37 -0800952 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800953 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800954 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800955 }
956 }
957
958 @Override
wangqibc28ea72018-04-02 16:23:00 -0700959 public void onUpgradeToRtt(DialerCall call, int rttRequestId) {
960 if (inCallActivity != null) {
961 inCallActivity.showDialogForRttRequest(call, rttRequestId);
962 }
963 }
964
965 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700966 public void onSessionModificationStateChange(DialerCall call) {
967 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800968 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
linyuh183cb712017-12-27 17:02:37 -0800969 if (proximitySensor == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800970 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
971 return;
972 }
linyuh183cb712017-12-27 17:02:37 -0800973 proximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700974 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
linyuh183cb712017-12-27 17:02:37 -0800975 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800976 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800977 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800978 }
979 }
980
981 /**
982 * Called when a call becomes disconnected. Called everytime an existing call changes from being
983 * connected (incoming/outgoing/active) to disconnected.
984 */
985 @Override
986 public void onDisconnect(DialerCall call) {
linyuh7b86f562017-11-16 11:24:09 -0800987 showDialogOrToastForDisconnectedCall(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800988
989 // We need to do the run the same code as onCallListChange.
linyuh183cb712017-12-27 17:02:37 -0800990 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800991
992 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800993 inCallActivity.dismissKeyguard(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800994 }
995
996 if (call.isEmergencyCall()) {
linyuh183cb712017-12-27 17:02:37 -0800997 FilteredNumbersUtil.recordLastEmergencyCallTime(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800998 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800999
linyuh183cb712017-12-27 17:02:37 -08001000 if (!callList.hasLiveCall()
Eric Erfanianfc37b022017-03-21 10:11:17 -07001001 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -07001002 && !isSecretCode(call.getNumber())
wangqi9982f0d2017-10-11 17:46:07 -07001003 && !call.isVoiceMailNumber()) {
linyuh183cb712017-12-27 17:02:37 -08001004 PostCall.onCallDisconnected(context, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -08001005 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001006 }
1007
Eric Erfanian10b34a52017-05-04 08:23:17 -07001008 private boolean isSecretCode(@Nullable String number) {
1009 return number != null
1010 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
1011 }
1012
Eric Erfanianccca3152017-02-22 16:32:36 -08001013 /** Given the call list, return the state in which the in-call screen should be. */
1014 public InCallState getPotentialStateFromCallList(CallList callList) {
1015
1016 InCallState newState = InCallState.NO_CALLS;
1017
1018 if (callList == null) {
1019 return newState;
1020 }
1021 if (callList.getIncomingCall() != null) {
1022 newState = InCallState.INCOMING;
1023 } else if (callList.getWaitingForAccountCall() != null) {
1024 newState = InCallState.WAITING_FOR_ACCOUNT;
1025 } else if (callList.getPendingOutgoingCall() != null) {
1026 newState = InCallState.PENDING_OUTGOING;
1027 } else if (callList.getOutgoingCall() != null) {
1028 newState = InCallState.OUTGOING;
1029 } else if (callList.getActiveCall() != null
1030 || callList.getBackgroundCall() != null
1031 || callList.getDisconnectedCall() != null
1032 || callList.getDisconnectingCall() != null) {
1033 newState = InCallState.INCALL;
1034 }
1035
1036 if (newState == InCallState.NO_CALLS) {
linyuh183cb712017-12-27 17:02:37 -08001037 if (boundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001038 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001039 }
1040 }
1041
1042 return newState;
1043 }
1044
1045 public boolean isBoundAndWaitingForOutgoingCall() {
linyuh183cb712017-12-27 17:02:37 -08001046 return boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -08001047 }
1048
1049 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001050 LogUtil.i(
1051 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
1052 "setBoundAndWaitingForOutgoingCall: " + isBound);
linyuh183cb712017-12-27 17:02:37 -08001053 boundAndWaitingForOutgoingCall = isBound;
1054 themeColorManager.setPendingPhoneAccountHandle(handle);
1055 if (isBound && inCallState == InCallState.NO_CALLS) {
1056 inCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001057 }
1058 }
1059
1060 public void onShrinkAnimationComplete() {
linyuh183cb712017-12-27 17:02:37 -08001061 if (awaitingCallListUpdate) {
1062 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001063 }
1064 }
1065
1066 public void addIncomingCallListener(IncomingCallListener listener) {
1067 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001068 incomingCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001069 }
1070
1071 public void removeIncomingCallListener(IncomingCallListener listener) {
1072 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001073 incomingCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001074 }
1075 }
1076
1077 public void addListener(InCallStateListener listener) {
1078 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001079 listeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001080 }
1081
1082 public void removeListener(InCallStateListener listener) {
1083 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001084 listeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001085 }
1086 }
1087
1088 public void addDetailsListener(InCallDetailsListener listener) {
1089 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001090 detailsListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001091 }
1092
1093 public void removeDetailsListener(InCallDetailsListener listener) {
1094 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001095 detailsListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001096 }
1097 }
1098
1099 public void addCanAddCallListener(CanAddCallListener listener) {
1100 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001101 canAddCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001102 }
1103
1104 public void removeCanAddCallListener(CanAddCallListener listener) {
1105 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001106 canAddCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001107 }
1108 }
1109
1110 public void addOrientationListener(InCallOrientationListener listener) {
1111 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001112 orientationListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001113 }
1114
1115 public void removeOrientationListener(InCallOrientationListener listener) {
1116 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001117 orientationListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001118 }
1119 }
1120
1121 public void addInCallEventListener(InCallEventListener listener) {
1122 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001123 inCallEventListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001124 }
1125
1126 public void removeInCallEventListener(InCallEventListener listener) {
1127 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001128 inCallEventListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001129 }
1130 }
1131
1132 public ProximitySensor getProximitySensor() {
linyuh183cb712017-12-27 17:02:37 -08001133 return proximitySensor;
Eric Erfanianccca3152017-02-22 16:32:36 -08001134 }
1135
1136 public PseudoScreenState getPseudoScreenState() {
linyuh183cb712017-12-27 17:02:37 -08001137 return pseudoScreenState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001138 }
1139
1140 /** Returns true if the incall app is the foreground application. */
1141 public boolean isShowingInCallUi() {
1142 if (!isActivityStarted()) {
1143 return false;
1144 }
linyuh183cb712017-12-27 17:02:37 -08001145 if (manageConferenceActivity != null && manageConferenceActivity.isVisible()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001146 return true;
1147 }
linyuh183cb712017-12-27 17:02:37 -08001148 return inCallActivity.isVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001149 }
1150
1151 /**
1152 * Returns true if the activity has been created and is running. Returns true as long as activity
1153 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
1154 * (not in foreground).
1155 */
1156 public boolean isActivityStarted() {
linyuh183cb712017-12-27 17:02:37 -08001157 return (inCallActivity != null
1158 && !inCallActivity.isDestroyed()
1159 && !inCallActivity.isFinishing());
Eric Erfanianccca3152017-02-22 16:32:36 -08001160 }
1161
1162 /**
1163 * Determines if the In-Call app is currently changing configuration.
1164 *
1165 * @return {@code true} if the In-Call app is changing configuration.
1166 */
1167 public boolean isChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001168 return isChangingConfigurations;
Eric Erfanianccca3152017-02-22 16:32:36 -08001169 }
1170
1171 /**
1172 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1173 * screen orientation).
1174 */
1175 /*package*/
1176 void updateIsChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001177 isChangingConfigurations = false;
1178 if (inCallActivity != null) {
1179 isChangingConfigurations = inCallActivity.isChangingConfigurations();
Eric Erfanianccca3152017-02-22 16:32:36 -08001180 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001181 LogUtil.v(
1182 "InCallPresenter.updateIsChangingConfigurations",
linyuh183cb712017-12-27 17:02:37 -08001183 "updateIsChangingConfigurations = " + isChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001184 }
1185
yueg10f6e822018-01-17 15:32:18 -08001186 /** Called when the activity goes in/out of the foreground. */
1187 public void onUiShowing(boolean showing) {
linyuh183cb712017-12-27 17:02:37 -08001188 if (proximitySensor != null) {
1189 proximitySensor.onInCallShowing(showing);
Eric Erfanianccca3152017-02-22 16:32:36 -08001190 }
1191
yueg092b21c2017-11-15 16:20:07 -08001192 if (!showing) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001193 updateIsChangingConfigurations();
1194 }
1195
linyuh183cb712017-12-27 17:02:37 -08001196 for (InCallUiListener listener : inCallUiListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001197 listener.onUiShowing(showing);
1198 }
1199
linyuh183cb712017-12-27 17:02:37 -08001200 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001201 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001202 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001203 }
1204 }
1205
Eric Erfanian2ca43182017-08-31 06:57:16 -07001206 public void refreshUi() {
linyuh183cb712017-12-27 17:02:37 -08001207 if (inCallActivity != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001208 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001209 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001210 }
1211 }
1212
Eric Erfanianccca3152017-02-22 16:32:36 -08001213 public void addInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001214 inCallUiListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001215 }
1216
1217 public boolean removeInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001218 return inCallUiListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001219 }
1220
1221 /*package*/
1222 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001223 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001224 notifyVideoPauseController(true);
Eric Erfanian2ca43182017-08-31 06:57:16 -07001225 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001226 }
1227
1228 /*package*/
1229 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001230 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001231 notifyVideoPauseController(false);
1232 }
1233
1234 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001235 LogUtil.d(
1236 "InCallPresenter.notifyVideoPauseController",
linyuh183cb712017-12-27 17:02:37 -08001237 "mIsChangingConfigurations=" + isChangingConfigurations);
1238 if (!isChangingConfigurations) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001239 VideoPauseController.getInstance().onUiShowing(showing);
1240 }
1241 }
1242
1243 /** Brings the app into the foreground if possible. */
1244 public void bringToForeground(boolean showDialpad) {
1245 // Before we bring the incall UI to the foreground, we check to see if:
1246 // 1. It is not currently in the foreground
1247 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1248 // be displayed)
1249 // If the activity hadn't actually been started previously, yet there are still calls
1250 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1251 // bring it up the UI regardless.
linyuh183cb712017-12-27 17:02:37 -08001252 if (!isShowingInCallUi() && inCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001253 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001254 }
1255 }
1256
1257 public void onPostDialCharWait(String callId, String chars) {
1258 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001259 inCallActivity.showDialogForPostCharWait(callId, chars);
Eric Erfanianccca3152017-02-22 16:32:36 -08001260 }
1261 }
1262
1263 /**
1264 * Handles the green CALL key while in-call.
1265 *
1266 * @return true if we consumed the event.
1267 */
1268 public boolean handleCallKey() {
1269 LogUtil.v("InCallPresenter.handleCallKey", null);
1270
1271 // The green CALL button means either "Answer", "Unhold", or
1272 // "Swap calls", or can be a no-op, depending on the current state
1273 // of the Phone.
1274
1275 /** INCOMING CALL */
linyuh183cb712017-12-27 17:02:37 -08001276 final CallList calls = callList;
Eric Erfanianccca3152017-02-22 16:32:36 -08001277 final DialerCall incomingCall = calls.getIncomingCall();
1278 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1279
1280 // (1) Attempt to answer a call
1281 if (incomingCall != null) {
1282 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1283 return true;
1284 }
1285
1286 /** STATE_ACTIVE CALL */
1287 final DialerCall activeCall = calls.getActiveCall();
1288 if (activeCall != null) {
1289 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1290 // consolidate this logic.
1291 final boolean canMerge =
1292 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1293 final boolean canSwap =
1294 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1295
Eric Erfanian2ca43182017-08-31 06:57:16 -07001296 LogUtil.v(
1297 "InCallPresenter.handleCallKey",
1298 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001299
1300 // (2) Attempt actions on conference calls
1301 if (canMerge) {
1302 TelecomAdapter.getInstance().merge(activeCall.getId());
1303 return true;
1304 } else if (canSwap) {
1305 TelecomAdapter.getInstance().swap(activeCall.getId());
1306 return true;
1307 }
1308 }
1309
1310 /** BACKGROUND CALL */
1311 final DialerCall heldCall = calls.getBackgroundCall();
1312 if (heldCall != null) {
1313 // We have a hold call so presumeable it will always support HOLD...but
1314 // there is no harm in double checking.
1315 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1316
Eric Erfanian2ca43182017-08-31 06:57:16 -07001317 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001318
1319 // (4) unhold call
1320 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1321 heldCall.unhold();
1322 return true;
1323 }
1324 }
1325
1326 // Always consume hard keys
1327 return true;
1328 }
1329
Eric Erfanianccca3152017-02-22 16:32:36 -08001330 /** Clears the previous fullscreen state. */
1331 public void clearFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001332 isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001333 }
1334
1335 /**
1336 * Changes the fullscreen mode of the in-call UI.
1337 *
1338 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1339 * otherwise.
1340 */
1341 public void setFullScreen(boolean isFullScreen) {
1342 setFullScreen(isFullScreen, false /* force */);
1343 }
1344
1345 /**
1346 * Changes the fullscreen mode of the in-call UI.
1347 *
1348 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1349 * otherwise.
1350 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1351 */
1352 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001353 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001354
1355 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1356 if (isDialpadVisible()) {
1357 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001358 LogUtil.v(
1359 "InCallPresenter.setFullScreen",
1360 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001361 }
1362
linyuh183cb712017-12-27 17:02:37 -08001363 if (this.isFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001364 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001365 return;
1366 }
linyuh183cb712017-12-27 17:02:37 -08001367 this.isFullScreen = isFullScreen;
1368 notifyFullscreenModeChange(this.isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001369 }
1370
1371 /**
1372 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1373 * otherwise.
1374 */
1375 public boolean isFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001376 return isFullScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001377 }
1378
1379 /**
1380 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1381 *
1382 * @param isFullscreenMode {@code True} if entering full screen mode.
1383 */
1384 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001385 for (InCallEventListener listener : inCallEventListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001386 listener.onFullscreenModeChanged(isFullscreenMode);
1387 }
1388 }
1389
linyuh7b86f562017-11-16 11:24:09 -08001390 /** Instruct the in-call activity to show an error dialog or toast for a disconnected call. */
1391 private void showDialogOrToastForDisconnectedCall(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -07001392 if (call.getState() != DialerCall.State.DISCONNECTED) {
linyuh7b86f562017-11-16 11:24:09 -08001393 return;
Eric Erfanianccca3152017-02-22 16:32:36 -08001394 }
linyuh7b86f562017-11-16 11:24:09 -08001395
1396 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1397 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1398 setDisconnectCauseForMissingAccounts(call);
1399 }
1400
yueg32d9ae72018-04-05 17:35:56 -07001401 if (isActivityStarted()) {
1402 inCallActivity.showDialogOrToastForDisconnectedCall(
1403 new DisconnectMessage(inCallActivity, call));
1404 } else {
1405 CharSequence message = new DisconnectMessage(context, call).toastMessage;
1406 if (message != null) {
1407 Toast.makeText(context, message, Toast.LENGTH_LONG).show();
1408 }
1409 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001410 }
1411
1412 /**
1413 * When the state of in-call changes, this is the first method to get called. It determines if the
1414 * UI needs to be started or finished depending on the new state and does it.
1415 */
1416 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001417 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001418 LogUtil.d(
linyuh183cb712017-12-27 17:02:37 -08001419 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + inCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001420
1421 // TODO: Consider a proper state machine implementation
1422
1423 // If the state isn't changing we have already done any starting/stopping of activities in
1424 // a previous pass...so lets cut out early
linyuh183cb712017-12-27 17:02:37 -08001425 if (newState == inCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001426 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001427 return newState;
1428 }
1429
1430 // A new Incoming call means that the user needs to be notified of the the call (since
1431 // it wasn't them who initiated it). We do this through full screen notifications and
1432 // happens indirectly through {@link StatusBarNotifier}.
1433 //
1434 // The process for incoming calls is as follows:
1435 //
1436 // 1) CallList - Announces existence of new INCOMING call
1437 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1438 // - should be set to INCOMING.
1439 // 3) InCallPresenter - This method is called to see if we need to start or finish
1440 // the app given the new state.
1441 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1442 // StatusBarNotifier explicitly to issue a FullScreen Notification
1443 // that will either start the InCallActivity or show the user a
1444 // top-level notification dialog if the user is in an immersive app.
1445 // That notification can also start the InCallActivity.
1446 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1447 // call InCallPresenter::setActivity() to let the presenter
1448 // know that start-up is complete.
1449 //
1450 // [ AND NOW YOU'RE IN THE CALL. voila! ]
Eric Erfanianccca3152017-02-22 16:32:36 -08001451
1452 // A dialog to show on top of the InCallUI to select a PhoneAccount
1453 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1454
1455 // A new outgoing call indicates that the user just now dialed a number and when that
1456 // happens we need to display the screen immediately or show an account picker dialog if
1457 // no default is set. However, if the main InCallUI is already visible, we do not want to
1458 // re-initiate the start-up animation, so we do not need to do anything here.
1459 //
1460 // It is also possible to go into an intermediate state where the call has been initiated
1461 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1462 // This pending outgoing state can also launch the call screen.
1463 //
1464 // This is different from the incoming call sequence because we do not need to shock the
1465 // user with a top-level notification. Just show the call UI normally.
1466 boolean callCardFragmentVisible =
linyuh183cb712017-12-27 17:02:37 -08001467 inCallActivity != null && inCallActivity.getCallCardFragmentVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001468 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1469 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1470
1471 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1472 // outgoing call process, so the UI should be brought up to show an error dialog.
1473 showCallUi |=
linyuh183cb712017-12-27 17:02:37 -08001474 (InCallState.PENDING_OUTGOING == inCallState
Eric Erfanianccca3152017-02-22 16:32:36 -08001475 && InCallState.INCALL == newState
1476 && !isShowingInCallUi());
1477
1478 // Another exception - InCallActivity is in charge of disconnecting a call with no
1479 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1480 // call so that:
1481 // 1) The call can be disconnected correctly
1482 // 2) The UI comes up and correctly displays the error dialog.
1483 // TODO: Remove these special case conditions by making InCallPresenter a true state
1484 // machine. Telecom should also be the component responsible for disconnecting a call
1485 // with no valid accounts.
1486 showCallUi |=
1487 InCallState.PENDING_OUTGOING == newState
1488 && mainUiNotVisible
linyuh183cb712017-12-27 17:02:37 -08001489 && isCallWithNoValidAccounts(callList.getPendingOutgoingCall());
Eric Erfanianccca3152017-02-22 16:32:36 -08001490
1491 // The only time that we have an instance of mInCallActivity and it isn't started is
1492 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1493 // the activity. When it is finally destroyed, we double check if we should bring it back
1494 // up so we aren't going to lose anything by avoiding a second startup here.
linyuh183cb712017-12-27 17:02:37 -08001495 boolean activityIsFinishing = inCallActivity != null && !isActivityStarted();
Eric Erfanianccca3152017-02-22 16:32:36 -08001496 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001497 LogUtil.i(
1498 "InCallPresenter.startOrFinishUi",
linyuh183cb712017-12-27 17:02:37 -08001499 "Undo the state change: " + newState + " -> " + inCallState);
wangqicf61ca02017-08-31 15:32:55 -07001500 Trace.endSection();
linyuh183cb712017-12-27 17:02:37 -08001501 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001502 }
1503
1504 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1505 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1506 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1507 // outgoing call after user ignore it by pressing home button.
1508 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1509 && !showCallUi
1510 && isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001511 inCallActivity.dismissPendingDialogs();
Eric Erfanianccca3152017-02-22 16:32:36 -08001512 }
1513
yueg4613e8f2018-04-23 13:35:33 -07001514 if ((showCallUi || showAccountPicker) && !shouldStartInBubbleMode()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001515 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001516 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001517 } else if (newState == InCallState.NO_CALLS) {
1518 // The new state is the no calls state. Tear everything down.
yueg8e544862018-04-06 14:34:59 -07001519 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001520 attemptFinishActivity();
1521 attemptCleanup();
1522 }
1523
wangqicf61ca02017-08-31 15:32:55 -07001524 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001525 return newState;
1526 }
1527
1528 /**
1529 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1530 * or PhoneAccounts to select from.
1531 */
1532 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1533
1534 Bundle extras = call.getIntentExtras();
1535 // Initialize the extras bundle to avoid NPE
1536 if (extras == null) {
1537 extras = new Bundle();
1538 }
1539
1540 final List<PhoneAccountHandle> phoneAccountHandles =
1541 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1542
1543 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1544 String scheme = call.getHandle().getScheme();
1545 final String errorMsg =
1546 PhoneAccount.SCHEME_TEL.equals(scheme)
linyuh183cb712017-12-27 17:02:37 -08001547 ? context.getString(R.string.callFailed_simError)
1548 : context.getString(R.string.incall_error_supp_service_unknown);
Eric Erfanianccca3152017-02-22 16:32:36 -08001549 DisconnectCause disconnectCause =
1550 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1551 call.setDisconnectCause(disconnectCause);
1552 }
1553 }
1554
Eric Erfanianccca3152017-02-22 16:32:36 -08001555 /**
1556 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1557 * Calling classes should use this as an indication whether to interact with the
1558 * InCallPresenter or not.
1559 */
1560 public boolean isReadyForTearDown() {
linyuh183cb712017-12-27 17:02:37 -08001561 return inCallActivity == null && !serviceConnected && inCallState == InCallState.NO_CALLS;
Eric Erfanianccca3152017-02-22 16:32:36 -08001562 }
1563
1564 /**
1565 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1566 */
1567 private void attemptCleanup() {
1568 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001569 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001570
1571 cleanupSurfaces();
1572
linyuh183cb712017-12-27 17:02:37 -08001573 isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001574
yueg8e544862018-04-06 14:34:59 -07001575 audioRouteSetForBubbleMode = false;
yueg32d9ae72018-04-05 17:35:56 -07001576
Eric Erfanianccca3152017-02-22 16:32:36 -08001577 // blow away stale contact info so that we get fresh data on
1578 // the next set of calls
linyuh183cb712017-12-27 17:02:37 -08001579 if (contactInfoCache != null) {
1580 contactInfoCache.clearCache();
Eric Erfanianccca3152017-02-22 16:32:36 -08001581 }
linyuh183cb712017-12-27 17:02:37 -08001582 contactInfoCache = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001583
linyuh183cb712017-12-27 17:02:37 -08001584 if (proximitySensor != null) {
1585 removeListener(proximitySensor);
1586 proximitySensor.tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -08001587 }
linyuh183cb712017-12-27 17:02:37 -08001588 proximitySensor = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001589
linyuh183cb712017-12-27 17:02:37 -08001590 if (statusBarNotifier != null) {
1591 removeListener(statusBarNotifier);
1592 EnrichedCallComponent.get(context)
Eric Erfaniand8046e52017-04-06 09:41:50 -07001593 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -08001594 .unregisterStateChangedListener(statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001595 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001596
linyuh183cb712017-12-27 17:02:37 -08001597 if (externalCallNotifier != null && externalCallList != null) {
1598 externalCallList.removeExternalCallListener(externalCallNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001599 }
linyuh183cb712017-12-27 17:02:37 -08001600 statusBarNotifier = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001601
linyuh183cb712017-12-27 17:02:37 -08001602 if (callList != null) {
1603 callList.removeListener(this);
1604 callList.removeListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001605 }
linyuh183cb712017-12-27 17:02:37 -08001606 callList = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001607
linyuh183cb712017-12-27 17:02:37 -08001608 context = null;
1609 inCallActivity = null;
1610 manageConferenceActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001611
linyuh183cb712017-12-27 17:02:37 -08001612 listeners.clear();
1613 incomingCallListeners.clear();
1614 detailsListeners.clear();
1615 canAddCallListeners.clear();
1616 orientationListeners.clear();
1617 inCallEventListeners.clear();
1618 inCallUiListeners.clear();
1619 if (!inCallUiLocks.isEmpty()) {
1620 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + inCallUiLocks);
1621 inCallUiLocks.clear();
twyen8efb4952017-10-06 16:35:54 -07001622 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001623 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001624 }
1625 }
1626
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001627 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001628 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
linyuh183cb712017-12-27 17:02:37 -08001629 context.startActivity(
1630 InCallActivity.getIntent(context, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001631 }
1632
1633 public void onServiceBind() {
linyuh183cb712017-12-27 17:02:37 -08001634 serviceBound = true;
Eric Erfanianccca3152017-02-22 16:32:36 -08001635 }
1636
1637 public void onServiceUnbind() {
1638 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -08001639 serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001640 }
1641
1642 public boolean isServiceBound() {
linyuh183cb712017-12-27 17:02:37 -08001643 return serviceBound;
Eric Erfanianccca3152017-02-22 16:32:36 -08001644 }
1645
1646 public void maybeStartRevealAnimation(Intent intent) {
linyuh183cb712017-12-27 17:02:37 -08001647 if (intent == null || inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001648 return;
1649 }
1650 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1651 if (extras == null) {
1652 // Incoming call, just show the in-call UI directly.
1653 return;
1654 }
1655
1656 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1657 // Account selection dialog will show up so don't show the animation.
1658 return;
1659 }
1660
1661 final PhoneAccountHandle accountHandle =
1662 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1663 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001664
yueg32d9ae72018-04-05 17:35:56 -07001665 setBoundAndWaitingForOutgoingCall(true, accountHandle);
1666
yueg4613e8f2018-04-23 13:35:33 -07001667 if (shouldStartInBubbleModeWithExtras(extras)) {
yueg32d9ae72018-04-05 17:35:56 -07001668 LogUtil.i("InCallPresenter.maybeStartRevealAnimation", "shouldStartInBubbleMode");
1669 // Show bubble instead of in call UI
1670 return;
1671 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001672
1673 final Intent activityIntent =
linyuh183cb712017-12-27 17:02:37 -08001674 InCallActivity.getIntent(context, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001675 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
linyuh183cb712017-12-27 17:02:37 -08001676 context.startActivity(activityIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001677 }
1678
1679 /**
1680 * Retrieves the current in-call camera manager instance, creating if necessary.
1681 *
1682 * @return The {@link InCallCameraManager}.
1683 */
1684 public InCallCameraManager getInCallCameraManager() {
1685 synchronized (this) {
linyuh183cb712017-12-27 17:02:37 -08001686 if (inCallCameraManager == null) {
1687 inCallCameraManager = new InCallCameraManager(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001688 }
1689
linyuh183cb712017-12-27 17:02:37 -08001690 return inCallCameraManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001691 }
1692 }
1693
1694 /**
1695 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1696 *
1697 * @param orientation The screen orientation of the device (one of: {@link
1698 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1699 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1700 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1701 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1702 */
1703 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001704 LogUtil.d(
1705 "InCallPresenter.onDeviceOrientationChange",
1706 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001707
linyuh183cb712017-12-27 17:02:37 -08001708 if (callList != null) {
1709 callList.notifyCallsOfDeviceRotation(orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001710 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001711 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001712 }
1713
1714 // Notify listeners of device orientation changed.
linyuh183cb712017-12-27 17:02:37 -08001715 for (InCallOrientationListener listener : orientationListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001716 listener.onDeviceOrientationChanged(orientation);
1717 }
1718 }
1719
1720 /**
1721 * Configures the in-call UI activity so it can change orientations or not. Enables the
1722 * orientation event listener if allowOrientationChange is true, disables it if false.
1723 *
1724 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1725 * landscape. {@code false} if the in-call UI should be locked in portrait.
1726 */
1727 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
linyuh183cb712017-12-27 17:02:37 -08001728 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001729 LogUtil.e(
1730 "InCallPresenter.setInCallAllowsOrientationChange",
1731 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001732 return;
1733 }
linyuh183cb712017-12-27 17:02:37 -08001734 inCallActivity.setAllowOrientationChange(allowOrientationChange);
Eric Erfanianccca3152017-02-22 16:32:36 -08001735 }
1736
1737 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001738 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
linyuh183cb712017-12-27 17:02:37 -08001739 screenTimeoutEnabled = enable;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001740 applyScreenTimeout();
1741 }
1742
1743 private void applyScreenTimeout() {
linyuh183cb712017-12-27 17:02:37 -08001744 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001745 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001746 return;
1747 }
1748
linyuh183cb712017-12-27 17:02:37 -08001749 final Window window = inCallActivity.getWindow();
1750 if (screenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001751 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1752 } else {
1753 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1754 }
1755 }
1756
1757 /**
1758 * Hides or shows the conference manager fragment.
1759 *
1760 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1761 * be hidden.
1762 */
1763 public void showConferenceCallManager(boolean show) {
linyuh183cb712017-12-27 17:02:37 -08001764 if (inCallActivity != null) {
1765 inCallActivity.showConferenceFragment(show);
Eric Erfanianccca3152017-02-22 16:32:36 -08001766 }
linyuh183cb712017-12-27 17:02:37 -08001767 if (!show && manageConferenceActivity != null) {
1768 manageConferenceActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -08001769 }
1770 }
1771
1772 /**
1773 * Determines if the dialpad is visible.
1774 *
1775 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1776 */
1777 public boolean isDialpadVisible() {
linyuh183cb712017-12-27 17:02:37 -08001778 if (inCallActivity == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001779 return false;
1780 }
linyuh183cb712017-12-27 17:02:37 -08001781 return inCallActivity.isDialpadVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001782 }
1783
1784 public ThemeColorManager getThemeColorManager() {
linyuh183cb712017-12-27 17:02:37 -08001785 return themeColorManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001786 }
1787
wangqi8d662ca2017-10-26 11:27:19 -07001788 @VisibleForTesting
1789 public void setThemeColorManager(ThemeColorManager themeColorManager) {
linyuh183cb712017-12-27 17:02:37 -08001790 this.themeColorManager = themeColorManager;
wangqi8d662ca2017-10-26 11:27:19 -07001791 }
1792
Eric Erfanianccca3152017-02-22 16:32:36 -08001793 /** Called when the foreground call changes. */
1794 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh183cb712017-12-27 17:02:37 -08001795 themeColorManager.onForegroundCallChanged(context, newForegroundCall);
1796 if (inCallActivity != null) {
1797 inCallActivity.onForegroundCallChanged(newForegroundCall);
Eric Erfanianccca3152017-02-22 16:32:36 -08001798 }
1799 }
1800
1801 public InCallActivity getActivity() {
linyuh183cb712017-12-27 17:02:37 -08001802 return inCallActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -08001803 }
1804
1805 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1806 public void setActivity(InCallActivity inCallActivity) {
1807 if (inCallActivity == null) {
1808 throw new IllegalArgumentException("registerActivity cannot be called with null");
1809 }
linyuh183cb712017-12-27 17:02:37 -08001810 if (this.inCallActivity != null && this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001811 LogUtil.w(
1812 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001813 }
1814 updateActivity(inCallActivity);
1815 }
1816
1817 ExternalCallNotifier getExternalCallNotifier() {
linyuh183cb712017-12-27 17:02:37 -08001818 return externalCallNotifier;
Eric Erfanianccca3152017-02-22 16:32:36 -08001819 }
1820
1821 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001822 if (localVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001823 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001824 if (context != null) {
1825 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001826 }
linyuh183cb712017-12-27 17:02:37 -08001827 localVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001828 }
linyuh183cb712017-12-27 17:02:37 -08001829 return localVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001830 }
1831
1832 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001833 if (remoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001834 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001835 if (context != null) {
1836 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001837 }
linyuh183cb712017-12-27 17:02:37 -08001838 remoteVideoSurfaceTexture = VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001839 }
linyuh183cb712017-12-27 17:02:37 -08001840 return remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001841 }
1842
1843 void cleanupSurfaces() {
linyuh183cb712017-12-27 17:02:37 -08001844 if (remoteVideoSurfaceTexture != null) {
1845 remoteVideoSurfaceTexture.setDoneWithSurface();
1846 remoteVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001847 }
linyuh183cb712017-12-27 17:02:37 -08001848 if (localVideoSurfaceTexture != null) {
1849 localVideoSurfaceTexture.setDoneWithSurface();
1850 localVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001851 }
1852 }
1853
yueg77cb8e52017-10-27 16:42:51 -07001854 @Override
1855 public void onAudioStateChanged(CallAudioState audioState) {
yueg8e544862018-04-06 14:34:59 -07001856 // Set sensible audio route for bubble mode when we get real audio state for the first time
1857 // During the first time this function is called, supportedRouteMask is set to
yueg4613e8f2018-04-23 13:35:33 -07001858 // SUPPORTED_AUDIO_ROUTE_ALL, but it's OK since shouldStartInBubbleMode() is false at that time
1859 // (callList not updated yet).
1860 if (!audioRouteSetForBubbleMode && shouldStartInBubbleMode()) {
yueg8e544862018-04-06 14:34:59 -07001861 setAudioRouteForBubbleMode(audioState);
1862 audioRouteSetForBubbleMode = true;
1863 }
1864
linyuh183cb712017-12-27 17:02:37 -08001865 if (statusBarNotifier != null) {
1866 statusBarNotifier.updateNotification();
yueg77cb8e52017-10-27 16:42:51 -07001867 }
1868 }
1869
yueg8e544862018-04-06 14:34:59 -07001870 /**
1871 * Set audio route to make audio sensible. According to availability, set audio route to Bluetooth
1872 * or wired headset or speaker.
1873 */
1874 private void setAudioRouteForBubbleMode(CallAudioState audioState) {
1875 if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
1876 == CallAudioState.ROUTE_BLUETOOTH) {
1877 // Use Bluetooth if available
1878 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
1879 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "bluetooth");
1880 } else if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_WIRED_HEADSET)
1881 == CallAudioState.ROUTE_WIRED_HEADSET) {
1882 // Use wired headset if available
1883 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_WIRED_HEADSET);
1884 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "wired headset");
1885 } else {
1886 // Use speaker
1887 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_SPEAKER);
1888 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "speaker");
1889 }
1890 }
1891
Eric Erfanianccca3152017-02-22 16:32:36 -08001892 /** All the main states of InCallActivity. */
1893 public enum InCallState {
1894 // InCall Screen is off and there are no calls
1895 NO_CALLS,
1896
1897 // Incoming-call screen is up
1898 INCOMING,
1899
1900 // In-call experience is showing
1901 INCALL,
1902
1903 // Waiting for user input before placing outgoing call
1904 WAITING_FOR_ACCOUNT,
1905
1906 // UI is starting up but no call has been initiated yet.
1907 // The UI is waiting for Telecom to respond.
1908 PENDING_OUTGOING,
1909
1910 // User is dialing out
1911 OUTGOING;
1912
1913 public boolean isIncoming() {
1914 return (this == INCOMING);
1915 }
1916
1917 public boolean isConnectingOrConnected() {
1918 return (this == INCOMING || this == OUTGOING || this == INCALL);
1919 }
1920 }
1921
1922 /** Interface implemented by classes that need to know about the InCall State. */
1923 public interface InCallStateListener {
1924
1925 // TODO: Enhance state to contain the call objects instead of passing CallList
1926 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1927 }
1928
1929 public interface IncomingCallListener {
1930
1931 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1932 }
1933
1934 public interface CanAddCallListener {
1935
1936 void onCanAddCallChanged(boolean canAddCall);
1937 }
1938
1939 public interface InCallDetailsListener {
1940
1941 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1942 }
1943
1944 public interface InCallOrientationListener {
1945
1946 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1947 }
1948
1949 /**
1950 * Interface implemented by classes that need to know about events which occur within the In-Call
1951 * UI. Used as a means of communicating between fragments that make up the UI.
1952 */
1953 public interface InCallEventListener {
1954
1955 void onFullscreenModeChanged(boolean isFullscreenMode);
1956 }
1957
1958 public interface InCallUiListener {
1959
1960 void onUiShowing(boolean showing);
1961 }
twyen8efb4952017-10-06 16:35:54 -07001962
1963 private class InCallUiLockImpl implements InCallUiLock {
1964 private final String tag;
1965
1966 private InCallUiLockImpl(String tag) {
1967 this.tag = tag;
1968 }
1969
1970 @MainThread
1971 @Override
1972 public void release() {
1973 Assert.isMainThread();
1974 releaseInCallUiLock(InCallUiLockImpl.this);
1975 }
1976
1977 @Override
1978 public String toString() {
1979 return "InCallUiLock[" + tag + "]";
1980 }
1981 }
1982
1983 @MainThread
1984 public InCallUiLock acquireInCallUiLock(String tag) {
1985 Assert.isMainThread();
1986 InCallUiLock lock = new InCallUiLockImpl(tag);
linyuh183cb712017-12-27 17:02:37 -08001987 inCallUiLocks.add(lock);
twyen8efb4952017-10-06 16:35:54 -07001988 return lock;
1989 }
1990
1991 @MainThread
1992 private void releaseInCallUiLock(InCallUiLock lock) {
1993 Assert.isMainThread();
1994 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
linyuh183cb712017-12-27 17:02:37 -08001995 inCallUiLocks.remove(lock);
1996 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07001997 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
linyuh183cb712017-12-27 17:02:37 -08001998 if (inCallState == InCallState.NO_CALLS) {
twyen8efb4952017-10-06 16:35:54 -07001999 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
2000 attemptFinishActivity();
2001 attemptCleanup();
2002 }
2003 }
2004 }
2005
2006 @MainThread
2007 public boolean isInCallUiLocked() {
2008 Assert.isMainThread();
linyuh183cb712017-12-27 17:02:37 -08002009 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002010 return false;
2011 }
linyuh183cb712017-12-27 17:02:37 -08002012 for (InCallUiLock lock : inCallUiLocks) {
twyen8efb4952017-10-06 16:35:54 -07002013 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
2014 }
2015 return true;
2016 }
2017
linyuh183cb712017-12-27 17:02:37 -08002018 private final Set<InCallUiLock> inCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08002019}