blob: 5e08c6969285d92304ba17b7652282961c4f44a6 [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;
Eric Erfanianccca3152017-02-22 16:32:36 -0800200 /** Whether or not we are currently bound and waiting for Telecom to send us a new call. */
linyuh183cb712017-12-27 17:02:37 -0800201 private boolean boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -0800202 /** Determines if the InCall UI is in fullscreen mode or not. */
linyuh183cb712017-12-27 17:02:37 -0800203 private boolean isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800204
linyuh183cb712017-12-27 17:02:37 -0800205 private boolean screenTimeoutEnabled = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700206
linyuh183cb712017-12-27 17:02:37 -0800207 private PhoneStateListener phoneStateListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800208 new PhoneStateListener() {
209 @Override
210 public void onCallStateChanged(int state, String incomingNumber) {
211 if (state == TelephonyManager.CALL_STATE_RINGING) {
linyuh183cb712017-12-27 17:02:37 -0800212 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800213 return;
214 }
215 // Check if the number is blocked, to silence the ringer.
linyuh183cb712017-12-27 17:02:37 -0800216 String countryIso = GeoUtil.getCurrentCountryIso(context);
217 filteredQueryHandler.isBlockedNumber(
218 onCheckBlockedListener, incomingNumber, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800219 }
220 }
221 };
twyena4745bd2017-12-12 18:40:11 -0800222
Eric Erfanianccca3152017-02-22 16:32:36 -0800223 /** Whether or not InCallService is bound to Telecom. */
linyuh183cb712017-12-27 17:02:37 -0800224 private boolean serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800225
226 /**
227 * When configuration changes Android kills the current activity and starts a new one. The flag is
228 * used to check if full clean up is necessary (activity is stopped and new activity won't be
229 * started), or if a new activity will be started right after the current one is destroyed, and
230 * therefore no need in release all resources.
231 */
linyuh183cb712017-12-27 17:02:37 -0800232 private boolean isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800233
linyuh183cb712017-12-27 17:02:37 -0800234 private boolean awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800235
linyuh183cb712017-12-27 17:02:37 -0800236 private ExternalCallList.ExternalCallListener externalCallListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800237 new ExternalCallList.ExternalCallListener() {
238
239 @Override
240 public void onExternalCallPulled(android.telecom.Call call) {
241 // Note: keep this code in sync with InCallPresenter#onCallAdded
242 LatencyReport latencyReport = new LatencyReport(call);
243 latencyReport.onCallBlockingDone();
244 // Note: External calls do not require spam checking.
linyuh183cb712017-12-27 17:02:37 -0800245 callList.onCallAdded(context, call, latencyReport);
246 call.registerCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800247 }
248
249 @Override
250 public void onExternalCallAdded(android.telecom.Call call) {
251 // No-op
252 }
253
254 @Override
255 public void onExternalCallRemoved(android.telecom.Call call) {
256 // No-op
257 }
258
259 @Override
260 public void onExternalCallUpdated(android.telecom.Call call) {
261 // No-op
262 }
263 };
264
linyuh183cb712017-12-27 17:02:37 -0800265 private ThemeColorManager themeColorManager;
266 private VideoSurfaceTexture localVideoSurfaceTexture;
267 private VideoSurfaceTexture remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -0800268
erfaniand05d8992018-03-20 19:42:26 -0700269 private SpeakEasyCallManager speakEasyCallManager;
270
yueg8e544862018-04-06 14:34:59 -0700271 private boolean audioRouteSetForBubbleMode;
yueg32d9ae72018-04-05 17:35:56 -0700272
Eric Erfanian10b34a52017-05-04 08:23:17 -0700273 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800274 @VisibleForTesting
275 InCallPresenter() {}
276
277 public static synchronized InCallPresenter getInstance() {
linyuh183cb712017-12-27 17:02:37 -0800278 if (inCallPresenter == null) {
wangqic8cf79e2017-10-17 09:21:00 -0700279 Trace.beginSection("InCallPresenter.Constructor");
linyuh183cb712017-12-27 17:02:37 -0800280 inCallPresenter = new InCallPresenter();
wangqic8cf79e2017-10-17 09:21:00 -0700281 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800282 }
linyuh183cb712017-12-27 17:02:37 -0800283 return inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -0800284 }
285
Eric Erfanian10b34a52017-05-04 08:23:17 -0700286 @VisibleForTesting
287 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
linyuh183cb712017-12-27 17:02:37 -0800288 InCallPresenter.inCallPresenter = inCallPresenter;
Eric Erfanian10b34a52017-05-04 08:23:17 -0700289 }
290
Eric Erfanianccca3152017-02-22 16:32:36 -0800291 /**
292 * Determines whether or not a call has no valid phone accounts that can be used to make the call
293 * with. Emergency calls do not require a phone account.
294 *
295 * @param call to check accounts for.
296 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
297 * call contains a phone account that could be used to initiate it with, or is an emergency
298 * call.
299 */
300 public static boolean isCallWithNoValidAccounts(DialerCall call) {
301 if (call != null && !call.isEmergencyCall()) {
302 Bundle extras = call.getIntentExtras();
303
304 if (extras == null) {
305 extras = EMPTY_EXTRAS;
306 }
307
308 final List<PhoneAccountHandle> phoneAccountHandles =
309 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
310
311 if ((call.getAccountHandle() == null
312 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700313 LogUtil.i(
314 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800315 return true;
316 }
317 }
318 return false;
319 }
320
321 public InCallState getInCallState() {
linyuh183cb712017-12-27 17:02:37 -0800322 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800323 }
324
325 public CallList getCallList() {
linyuh183cb712017-12-27 17:02:37 -0800326 return callList;
Eric Erfanianccca3152017-02-22 16:32:36 -0800327 }
328
329 public void setUp(
330 @NonNull Context context,
331 CallList callList,
332 ExternalCallList externalCallList,
333 StatusBarNotifier statusBarNotifier,
334 ExternalCallNotifier externalCallNotifier,
335 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700336 ProximitySensor proximitySensor,
erfaniand05d8992018-03-20 19:42:26 -0700337 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler,
yueg4613e8f2018-04-23 13:35:33 -0700338 @NonNull SpeakEasyCallManager speakEasyCallManager) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700339 Trace.beginSection("InCallPresenter.setUp");
linyuh183cb712017-12-27 17:02:37 -0800340 if (serviceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700341 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
linyuh183cb712017-12-27 17:02:37 -0800342 if (context != this.context || callList != this.callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800343 throw new IllegalStateException();
344 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700345 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800346 return;
347 }
348
349 Objects.requireNonNull(context);
linyuh183cb712017-12-27 17:02:37 -0800350 this.context = context;
Eric Erfanianccca3152017-02-22 16:32:36 -0800351
linyuh183cb712017-12-27 17:02:37 -0800352 this.contactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800353
linyuh183cb712017-12-27 17:02:37 -0800354 this.statusBarNotifier = statusBarNotifier;
355 this.externalCallNotifier = externalCallNotifier;
356 addListener(this.statusBarNotifier);
357 EnrichedCallComponent.get(this.context)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700358 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -0800359 .registerStateChangedListener(this.statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800360
linyuh183cb712017-12-27 17:02:37 -0800361 this.proximitySensor = proximitySensor;
362 addListener(this.proximitySensor);
Eric Erfanianccca3152017-02-22 16:32:36 -0800363
linyuh183cb712017-12-27 17:02:37 -0800364 if (themeColorManager == null) {
365 themeColorManager =
366 new ThemeColorManager(new InCallUIMaterialColorMapUtils(this.context.getResources()));
wangqi8d662ca2017-10-26 11:27:19 -0700367 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800368
linyuh183cb712017-12-27 17:02:37 -0800369 this.callList = callList;
370 this.externalCallList = externalCallList;
371 externalCallList.addExternalCallListener(this.externalCallNotifier);
372 externalCallList.addExternalCallListener(externalCallListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800373
374 // This only gets called by the service so this is okay.
linyuh183cb712017-12-27 17:02:37 -0800375 serviceConnected = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800376
377 // The final thing we do in this set up is add ourselves as a listener to CallList. This
378 // will kick off an update and the whole process can start.
linyuh183cb712017-12-27 17:02:37 -0800379 this.callList.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800380
381 // Create spam call list listener and add it to the list of listeners
linyuh183cb712017-12-27 17:02:37 -0800382 spamCallListListener =
zachh6a4cebd2017-10-24 17:10:06 -0700383 new SpamCallListListener(
384 context, DialerExecutorComponent.get(context).dialerExecutorFactory());
linyuh183cb712017-12-27 17:02:37 -0800385 this.callList.addListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800386
387 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800388
linyuh183cb712017-12-27 17:02:37 -0800389 filteredQueryHandler = filteredNumberQueryHandler;
erfaniand05d8992018-03-20 19:42:26 -0700390 this.speakEasyCallManager = speakEasyCallManager;
linyuh183cb712017-12-27 17:02:37 -0800391 this.context
Eric Erfanianccca3152017-02-22 16:32:36 -0800392 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800393 .listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800394
yueg77cb8e52017-10-27 16:42:51 -0700395 AudioModeProvider.getInstance().addListener(this);
396
roldenburg42099012018-04-09 14:47:15 -0700397 // Add listener to notify Telephony process when the incoming call screen is started or
398 // finished. This is for hiding USSD dialog because the incoming call screen should have
399 // higher precedence over this dialog.
400 MotorolaInCallUiNotifier motorolaInCallUiNotifier = new MotorolaInCallUiNotifier(context);
401 addInCallUiListener(motorolaInCallUiNotifier);
402 addListener(motorolaInCallUiNotifier);
yueg7f5acbe2018-01-10 13:50:29 -0800403
Eric Erfanian2ca43182017-08-31 06:57:16 -0700404 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
405 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800406 }
407
408 /**
yueg32d9ae72018-04-05 17:35:56 -0700409 * Return whether we should start call in bubble mode and not show InCallActivity. The call mode
410 * should be set in CallConfiguration in EXTRA_OUTGOING_CALL_EXTRAS when starting a call intent.
411 */
yueg4613e8f2018-04-23 13:35:33 -0700412 private boolean shouldStartInBubbleMode() {
yueg32d9ae72018-04-05 17:35:56 -0700413 if (!ReturnToCallController.isEnabled(context)) {
414 return false;
415 }
yueg4613e8f2018-04-23 13:35:33 -0700416
417 // We only start in Bubble mode for outgoing call
418 DialerCall dialerCall = callList.getPendingOutgoingCall();
419 if (dialerCall == null) {
420 dialerCall = callList.getOutgoingCall();
421 }
422 if (dialerCall == null) {
yueg32d9ae72018-04-05 17:35:56 -0700423 return false;
424 }
yueg4613e8f2018-04-23 13:35:33 -0700425
426 Bundle extras = dialerCall.getIntentExtras();
427 return shouldStartInBubbleModeWithExtras(extras);
428 }
429
430 private boolean shouldStartInBubbleModeWithExtras(Bundle outgoingExtras) {
431 if (!ReturnToCallController.isEnabled(context)) {
432 return false;
433 }
434
435 if (outgoingExtras == null) {
436 return false;
437 }
438 byte[] callConfigurationByteArray = outgoingExtras.getByteArray(CALL_CONFIGURATION_EXTRA);
yueg32d9ae72018-04-05 17:35:56 -0700439 if (callConfigurationByteArray == null) {
440 return false;
441 }
442 try {
443 CallConfiguration callConfiguration = CallConfiguration.parseFrom(callConfigurationByteArray);
444 LogUtil.i(
445 "InCallPresenter.shouldStartInBubbleMode",
446 "call mode: " + callConfiguration.getCallMode());
447 return callConfiguration.getCallMode() == Mode.BUBBLE;
448 } catch (InvalidProtocolBufferException e) {
449 return false;
450 }
451 }
452
453 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800454 * Called when the telephony service has disconnected from us. This will happen when there are no
455 * more active calls. However, we may still want to continue showing the UI for certain cases like
456 * showing "Call Ended". What we really want is to wait for the activity and the service to both
457 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
458 * secondary method that performs the aforementioned logic.
459 */
460 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700461 LogUtil.d("InCallPresenter.tearDown", "tearDown");
linyuh183cb712017-12-27 17:02:37 -0800462 callList.clearOnDisconnect();
Eric Erfanianccca3152017-02-22 16:32:36 -0800463
linyuh183cb712017-12-27 17:02:37 -0800464 serviceConnected = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800465
linyuh183cb712017-12-27 17:02:37 -0800466 context
Eric Erfanianccca3152017-02-22 16:32:36 -0800467 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800468 .listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800469
470 attemptCleanup();
471 VideoPauseController.getInstance().tearDown();
yueg77cb8e52017-10-27 16:42:51 -0700472 AudioModeProvider.getInstance().removeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800473 }
474
475 private void attemptFinishActivity() {
linyuh183cb712017-12-27 17:02:37 -0800476 screenTimeoutEnabled = true;
477 final boolean doFinish = (inCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700478 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800479 if (doFinish) {
linyuh183cb712017-12-27 17:02:37 -0800480 inCallActivity.setExcludeFromRecents(true);
481 inCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800482 }
483 }
484
485 /**
486 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
487 * for more insight on the tear-down process.
488 */
489 public void unsetActivity(InCallActivity inCallActivity) {
490 if (inCallActivity == null) {
491 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
492 }
linyuh183cb712017-12-27 17:02:37 -0800493 if (this.inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700494 LogUtil.i(
495 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800496 return;
497 }
linyuh183cb712017-12-27 17:02:37 -0800498 if (this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700499 LogUtil.w(
500 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800501 "Second instance of InCallActivity is trying to unregister when another"
502 + " instance is active. Ignoring.");
503 return;
504 }
505 updateActivity(null);
506 }
507
508 /**
509 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
510 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
511 */
512 private void updateActivity(InCallActivity inCallActivity) {
wangqi9982f0d2017-10-11 17:46:07 -0700513 Trace.beginSection("InCallPresenter.updateActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800514 boolean updateListeners = false;
515 boolean doAttemptCleanup = false;
516
517 if (inCallActivity != null) {
linyuh183cb712017-12-27 17:02:37 -0800518 if (this.inCallActivity == null) {
519 context = inCallActivity.getApplicationContext();
Eric Erfanianccca3152017-02-22 16:32:36 -0800520 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700521 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800522 } else {
523 // since setActivity is called onStart(), it can be called multiple times.
524 // This is fine and ignorable, but we do not want to update the world every time
525 // this happens (like going to/from background) so we do not set updateListeners.
526 }
527
linyuh183cb712017-12-27 17:02:37 -0800528 this.inCallActivity = inCallActivity;
529 this.inCallActivity.setExcludeFromRecents(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800530
531 // By the time the UI finally comes up, the call may already be disconnected.
532 // If that's the case, we may need to show an error dialog.
linyuh183cb712017-12-27 17:02:37 -0800533 if (callList != null && callList.getDisconnectedCall() != null) {
534 showDialogOrToastForDisconnectedCall(callList.getDisconnectedCall());
Eric Erfanianccca3152017-02-22 16:32:36 -0800535 }
536
537 // When the UI comes up, we need to first check the in-call state.
538 // If we are showing NO_CALLS, that means that a call probably connected and
539 // then immediately disconnected before the UI was able to come up.
540 // If we dont have any calls, start tearing down the UI instead.
541 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
542 // it has been set.
linyuh183cb712017-12-27 17:02:37 -0800543 if (inCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700544 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800545 attemptFinishActivity();
wangqi9982f0d2017-10-11 17:46:07 -0700546 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800547 return;
548 }
549 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700550 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800551 updateListeners = true;
linyuh183cb712017-12-27 17:02:37 -0800552 this.inCallActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800553
554 // We attempt cleanup for the destroy case but only after we recalculate the state
555 // to see if we need to come back up or stay shut down. This is why we do the
556 // cleanup after the call to onCallListChange() instead of directly here.
557 doAttemptCleanup = true;
558 }
559
560 // Messages can come from the telephony layer while the activity is coming up
561 // and while the activity is going down. So in both cases we need to recalculate what
562 // state we should be in after they complete.
563 // Examples: (1) A new incoming call could come in and then get disconnected before
564 // the activity is created.
565 // (2) All calls could disconnect and then get a new incoming call before the
566 // activity is destroyed.
567 //
Eric Erfanian938468d2017-10-24 14:05:52 -0700568 // a bug - We previously had a check for mServiceConnected here as well, but there are
Eric Erfanianccca3152017-02-22 16:32:36 -0800569 // cases where we need to recalculate the current state even if the service in not
570 // connected. In particular the case where startOrFinish() is called while the app is
571 // already finish()ing. In that case, we skip updating the state with the knowledge that
572 // we will check again once the activity has finished. That means we have to recalculate the
573 // state here even if the service is disconnected since we may not have finished a state
574 // transition while finish()ing.
575 if (updateListeners) {
linyuh183cb712017-12-27 17:02:37 -0800576 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800577 }
578
579 if (doAttemptCleanup) {
580 attemptCleanup();
581 }
wangqi9982f0d2017-10-11 17:46:07 -0700582 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800583 }
584
erfanian12243de2018-04-17 12:17:08 -0700585 public SpeakEasyCallManager getSpeakEasyCallManager() {
586 return this.speakEasyCallManager;
587 }
588
Eric Erfanianccca3152017-02-22 16:32:36 -0800589 public void setManageConferenceActivity(
590 @Nullable ManageConferenceActivity manageConferenceActivity) {
linyuh183cb712017-12-27 17:02:37 -0800591 this.manageConferenceActivity = manageConferenceActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -0800592 }
593
594 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700595 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800596 bringToForeground(showDialpad);
597 }
598
599 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700600 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800601 LatencyReport latencyReport = new LatencyReport(call);
602 if (shouldAttemptBlocking(call)) {
603 maybeBlockCall(call, latencyReport);
604 } else {
605 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800606 externalCallList.onCallAdded(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800607 } else {
608 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800609 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800610 }
611 }
612
613 // Since a call has been added we are no longer waiting for Telecom to send us a call.
614 setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -0800615 call.registerCallback(callCallback);
zachh78e54ac2017-12-05 16:38:35 -0800616 // TODO(maxwelb): Return the future in recordPhoneLookupInfo and propagate.
linyuh183cb712017-12-27 17:02:37 -0800617 PhoneLookupHistoryRecorder.recordPhoneLookupInfo(context.getApplicationContext(), call);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700618 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800619 }
620
621 private boolean shouldAttemptBlocking(android.telecom.Call call) {
622 if (call.getState() != android.telecom.Call.STATE_RINGING) {
623 return false;
624 }
linyuh183cb712017-12-27 17:02:37 -0800625 if (!UserManagerCompat.isUserUnlocked(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700626 LogUtil.i(
627 "InCallPresenter.shouldAttemptBlocking",
628 "not attempting to block incoming call because user is locked");
629 return false;
630 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800631 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700632 LogUtil.i(
633 "InCallPresenter.shouldAttemptBlocking",
634 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800635 return false;
636 }
linyuh183cb712017-12-27 17:02:37 -0800637 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700638 LogUtil.i(
639 "InCallPresenter.shouldAttemptBlocking",
640 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800641 return false;
642 }
643 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
644 return false;
645 }
linyuh183cb712017-12-27 17:02:37 -0800646 if (FilteredNumberCompat.useNewFiltering(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700647 LogUtil.i(
648 "InCallPresenter.shouldAttemptBlocking",
649 "not attempting to block incoming call because framework blocking is in use");
650 return false;
651 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800652 return true;
653 }
654
655 /**
656 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
657 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
658 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
659 * call anyways.
660 */
661 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
linyuh183cb712017-12-27 17:02:37 -0800662 final String countryIso = GeoUtil.getCurrentCountryIso(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800663 final String number = TelecomCallUtil.getNumber(call);
664 final long timeAdded = System.currentTimeMillis();
665
666 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
667 // main UI thread. It is needed so we can change its value within different scopes, since
668 // that cannot be done with a final boolean.
669 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
670
671 final Handler handler = new Handler();
672
673 // Proceed if the query is slow; the call may still be blocked after the query returns.
674 final Runnable runnable =
675 new Runnable() {
676 @Override
677 public void run() {
678 hasTimedOut.set(true);
679 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800680 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800681 }
682 };
683 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
684
685 OnCheckBlockedListener onCheckBlockedListener =
686 new OnCheckBlockedListener() {
687 @Override
688 public void onCheckComplete(final Integer id) {
689 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700690 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800691 return;
692 }
693 if (!hasTimedOut.get()) {
694 handler.removeCallbacks(runnable);
695 }
696 if (id == null) {
697 if (!hasTimedOut.get()) {
698 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800699 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800700 }
701 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700702 LogUtil.d(
703 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800704 if (!hasTimedOut.get()) {
705 handler.removeCallbacks(runnable);
706
707 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800708 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800709 }
710 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700711 LogUtil.i(
712 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800713 call.reject(false, null);
linyuh183cb712017-12-27 17:02:37 -0800714 Logger.get(context).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
Eric Erfanianccca3152017-02-22 16:32:36 -0800715
716 /*
717 * If mContext is null, then the InCallPresenter was torn down before the
718 * block check had a chance to complete. The context is no longer valid, so
719 * don't attempt to remove the call log entry.
720 */
linyuh183cb712017-12-27 17:02:37 -0800721 if (context == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800722 return;
723 }
724 // Register observer to update the call log.
725 // BlockedNumberContentObserver will unregister after successful log or timeout.
726 BlockedNumberContentObserver contentObserver =
linyuh183cb712017-12-27 17:02:37 -0800727 new BlockedNumberContentObserver(context, new Handler(), number, timeAdded);
Eric Erfanianccca3152017-02-22 16:32:36 -0800728 contentObserver.register();
729 }
730 }
731 };
732
linyuh183cb712017-12-27 17:02:37 -0800733 filteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800734 }
735
736 public void onCallRemoved(android.telecom.Call call) {
737 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800738 externalCallList.onCallRemoved(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800739 } else {
linyuh183cb712017-12-27 17:02:37 -0800740 callList.onCallRemoved(context, call);
741 call.unregisterCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800742 }
743 }
744
745 public void onCanAddCallChanged(boolean canAddCall) {
linyuh183cb712017-12-27 17:02:37 -0800746 for (CanAddCallListener listener : canAddCallListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800747 listener.onCanAddCallChanged(canAddCall);
748 }
749 }
750
751 @Override
752 public void onWiFiToLteHandover(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -0700753 if (call.hasShownWiFiToLteHandoverToast()) {
754 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800755 }
yueg32d9ae72018-04-05 17:35:56 -0700756
757 Toast.makeText(context, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG)
758 .show();
759 call.setHasShownWiFiToLteHandoverToast();
Eric Erfanianccca3152017-02-22 16:32:36 -0800760 }
761
762 @Override
763 public void onHandoverToWifiFailed(DialerCall call) {
linyuh183cb712017-12-27 17:02:37 -0800764 if (inCallActivity != null) {
765 inCallActivity.showDialogOrToastForWifiHandoverFailure(call);
yueg32d9ae72018-04-05 17:35:56 -0700766 } else {
767 Toast.makeText(context, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT)
768 .show();
Eric Erfanianccca3152017-02-22 16:32:36 -0800769 }
770 }
771
Eric Erfanianc857f902017-05-15 14:05:33 -0700772 @Override
773 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
774 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
linyuh183cb712017-12-27 17:02:37 -0800775 if (inCallActivity != null) {
776 inCallActivity.showDialogForInternationalCallOnWifi(call);
Eric Erfanianc857f902017-05-15 14:05:33 -0700777 }
778 }
779
Eric Erfanianccca3152017-02-22 16:32:36 -0800780 /**
781 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
782 * app based on the information it gets from CallList. Dispatches the in-call state to all
783 * listeners. Can trigger the creation or destruction of the UI based on the states that is
784 * calculates.
785 */
786 @Override
787 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700788 Trace.beginSection("InCallPresenter.onCallListChange");
linyuh183cb712017-12-27 17:02:37 -0800789 if (inCallActivity != null && inCallActivity.isInCallScreenAnimating()) {
790 awaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700791 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800792 return;
793 }
794 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700795 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800796 return;
797 }
798
linyuh183cb712017-12-27 17:02:37 -0800799 awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800800
801 InCallState newState = getPotentialStateFromCallList(callList);
linyuh183cb712017-12-27 17:02:37 -0800802 InCallState oldState = inCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700803 LogUtil.d(
804 "InCallPresenter.onCallListChange",
805 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700806
807 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
808 // incall activity for that call will still exist (even if it's not visible). In the case of
809 // an incoming call in that situation, just disconnect that "waiting for account" call and
810 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
Eric Erfanian938468d2017-10-24 14:05:52 -0700811 // a bug for more details.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700812 DialerCall waitingForAccountCall;
813 if (newState == InCallState.INCOMING
814 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
815 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700816 // The InCallActivity might be destroyed or not started yet at this point.
817 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800818 inCallActivity.dismissPendingDialogs();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700819 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700820 }
821
Eric Erfanianccca3152017-02-22 16:32:36 -0800822 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700823 LogUtil.d(
824 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800825
826 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700827 LogUtil.i(
828 "InCallPresenter.onCallListChange",
829 "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800830 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800831
yuegb9103042018-03-30 12:12:25 -0700832 // Foreground call changed
833 DialerCall primary = null;
834 if (newState == InCallState.INCOMING) {
835 primary = callList.getIncomingCall();
836 } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
837 primary = callList.getOutgoingCall();
838 if (primary == null) {
839 primary = callList.getPendingOutgoingCall();
840 }
841 } else if (newState == InCallState.INCALL) {
842 primary = getCallToDisplay(callList, null, false);
843 }
844 if (primary != null) {
845 onForegroundCallChanged(primary);
846 }
847
Eric Erfanianccca3152017-02-22 16:32:36 -0800848 // notify listeners of new state
linyuh183cb712017-12-27 17:02:37 -0800849 for (InCallStateListener listener : listeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700850 LogUtil.d(
851 "InCallPresenter.onCallListChange",
linyuh183cb712017-12-27 17:02:37 -0800852 "Notify " + listener + " of state " + inCallState.toString());
853 listener.onStateChange(oldState, inCallState, callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800854 }
855
856 if (isActivityStarted()) {
857 final boolean hasCall =
858 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
linyuh183cb712017-12-27 17:02:37 -0800859 inCallActivity.dismissKeyguard(hasCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800860 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700861 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800862 }
863
yuegb9103042018-03-30 12:12:25 -0700864 /**
865 * Get the highest priority call to display. Goes through the calls and chooses which to return
866 * based on priority of which type of call to display to the user. Callers can use the "ignore"
867 * feature to get the second best call by passing a previously found primary call as ignore.
868 *
869 * @param ignore A call to ignore if found.
870 */
871 static DialerCall getCallToDisplay(
872 CallList callList, DialerCall ignore, boolean skipDisconnected) {
873 // Active calls come second. An active call always gets precedent.
874 DialerCall retval = callList.getActiveCall();
875 if (retval != null && retval != ignore) {
876 return retval;
877 }
878
879 // Sometimes there is intemediate state that two calls are in active even one is about
880 // to be on hold.
881 retval = callList.getSecondActiveCall();
882 if (retval != null && retval != ignore) {
883 return retval;
884 }
885
886 // Disconnected calls get primary position if there are no active calls
887 // to let user know quickly what call has disconnected. Disconnected
888 // calls are very short lived.
889 if (!skipDisconnected) {
890 retval = callList.getDisconnectingCall();
891 if (retval != null && retval != ignore) {
892 return retval;
893 }
894 retval = callList.getDisconnectedCall();
895 if (retval != null && retval != ignore) {
896 return retval;
897 }
898 }
899
900 // Then we go to background call (calls on hold)
901 retval = callList.getBackgroundCall();
902 if (retval != null && retval != ignore) {
903 return retval;
904 }
905
906 // Lastly, we go to a second background call.
907 retval = callList.getSecondBackgroundCall();
908
909 return retval;
910 }
911
Eric Erfanianccca3152017-02-22 16:32:36 -0800912 /** Called when there is a new incoming call. */
913 @Override
914 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700915 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800916 InCallState newState = startOrFinishUi(InCallState.INCOMING);
linyuh183cb712017-12-27 17:02:37 -0800917 InCallState oldState = inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800918
Eric Erfanian2ca43182017-08-31 06:57:16 -0700919 LogUtil.i(
920 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800921 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800922
wangqicf61ca02017-08-31 15:32:55 -0700923 Trace.beginSection("listener.onIncomingCall");
linyuh183cb712017-12-27 17:02:37 -0800924 for (IncomingCallListener listener : incomingCallListeners) {
925 listener.onIncomingCall(oldState, inCallState, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800926 }
wangqicf61ca02017-08-31 15:32:55 -0700927 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800928
wangqicf61ca02017-08-31 15:32:55 -0700929 Trace.beginSection("onPrimaryCallStateChanged");
linyuh183cb712017-12-27 17:02:37 -0800930 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800931 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800932 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800933 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700934 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700935 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800936 }
937
938 @Override
939 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700940 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
linyuh183cb712017-12-27 17:02:37 -0800941 && inCallState == InCallPresenter.InCallState.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800942 LogUtil.i(
943 "InCallPresenter.onUpgradeToVideo",
944 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700945 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800946 }
947
linyuh183cb712017-12-27 17:02:37 -0800948 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800949 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800950 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800951 }
952 }
953
954 @Override
wangqibc28ea72018-04-02 16:23:00 -0700955 public void onUpgradeToRtt(DialerCall call, int rttRequestId) {
956 if (inCallActivity != null) {
957 inCallActivity.showDialogForRttRequest(call, rttRequestId);
958 }
959 }
960
961 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700962 public void onSessionModificationStateChange(DialerCall call) {
963 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800964 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
linyuh183cb712017-12-27 17:02:37 -0800965 if (proximitySensor == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800966 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
967 return;
968 }
linyuh183cb712017-12-27 17:02:37 -0800969 proximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700970 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
linyuh183cb712017-12-27 17:02:37 -0800971 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800972 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800973 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800974 }
975 }
976
977 /**
978 * Called when a call becomes disconnected. Called everytime an existing call changes from being
979 * connected (incoming/outgoing/active) to disconnected.
980 */
981 @Override
982 public void onDisconnect(DialerCall call) {
linyuh7b86f562017-11-16 11:24:09 -0800983 showDialogOrToastForDisconnectedCall(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800984
985 // We need to do the run the same code as onCallListChange.
linyuh183cb712017-12-27 17:02:37 -0800986 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800987
988 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800989 inCallActivity.dismissKeyguard(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800990 }
991
992 if (call.isEmergencyCall()) {
linyuh183cb712017-12-27 17:02:37 -0800993 FilteredNumbersUtil.recordLastEmergencyCallTime(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800994 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800995
linyuh183cb712017-12-27 17:02:37 -0800996 if (!callList.hasLiveCall()
Eric Erfanianfc37b022017-03-21 10:11:17 -0700997 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700998 && !isSecretCode(call.getNumber())
wangqi9982f0d2017-10-11 17:46:07 -0700999 && !call.isVoiceMailNumber()) {
linyuh183cb712017-12-27 17:02:37 -08001000 PostCall.onCallDisconnected(context, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -08001001 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001002 }
1003
Eric Erfanian10b34a52017-05-04 08:23:17 -07001004 private boolean isSecretCode(@Nullable String number) {
1005 return number != null
1006 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
1007 }
1008
Eric Erfanianccca3152017-02-22 16:32:36 -08001009 /** Given the call list, return the state in which the in-call screen should be. */
1010 public InCallState getPotentialStateFromCallList(CallList callList) {
1011
1012 InCallState newState = InCallState.NO_CALLS;
1013
1014 if (callList == null) {
1015 return newState;
1016 }
1017 if (callList.getIncomingCall() != null) {
1018 newState = InCallState.INCOMING;
1019 } else if (callList.getWaitingForAccountCall() != null) {
1020 newState = InCallState.WAITING_FOR_ACCOUNT;
1021 } else if (callList.getPendingOutgoingCall() != null) {
1022 newState = InCallState.PENDING_OUTGOING;
1023 } else if (callList.getOutgoingCall() != null) {
1024 newState = InCallState.OUTGOING;
1025 } else if (callList.getActiveCall() != null
1026 || callList.getBackgroundCall() != null
1027 || callList.getDisconnectedCall() != null
1028 || callList.getDisconnectingCall() != null) {
1029 newState = InCallState.INCALL;
1030 }
1031
1032 if (newState == InCallState.NO_CALLS) {
linyuh183cb712017-12-27 17:02:37 -08001033 if (boundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001034 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001035 }
1036 }
1037
1038 return newState;
1039 }
1040
1041 public boolean isBoundAndWaitingForOutgoingCall() {
linyuh183cb712017-12-27 17:02:37 -08001042 return boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -08001043 }
1044
1045 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001046 LogUtil.i(
1047 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
1048 "setBoundAndWaitingForOutgoingCall: " + isBound);
linyuh183cb712017-12-27 17:02:37 -08001049 boundAndWaitingForOutgoingCall = isBound;
1050 themeColorManager.setPendingPhoneAccountHandle(handle);
1051 if (isBound && inCallState == InCallState.NO_CALLS) {
1052 inCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001053 }
1054 }
1055
1056 public void onShrinkAnimationComplete() {
linyuh183cb712017-12-27 17:02:37 -08001057 if (awaitingCallListUpdate) {
1058 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001059 }
1060 }
1061
1062 public void addIncomingCallListener(IncomingCallListener listener) {
1063 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001064 incomingCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001065 }
1066
1067 public void removeIncomingCallListener(IncomingCallListener listener) {
1068 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001069 incomingCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001070 }
1071 }
1072
1073 public void addListener(InCallStateListener listener) {
1074 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001075 listeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001076 }
1077
1078 public void removeListener(InCallStateListener listener) {
1079 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001080 listeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001081 }
1082 }
1083
1084 public void addDetailsListener(InCallDetailsListener listener) {
1085 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001086 detailsListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001087 }
1088
1089 public void removeDetailsListener(InCallDetailsListener listener) {
1090 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001091 detailsListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001092 }
1093 }
1094
1095 public void addCanAddCallListener(CanAddCallListener listener) {
1096 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001097 canAddCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001098 }
1099
1100 public void removeCanAddCallListener(CanAddCallListener listener) {
1101 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001102 canAddCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001103 }
1104 }
1105
1106 public void addOrientationListener(InCallOrientationListener listener) {
1107 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001108 orientationListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001109 }
1110
1111 public void removeOrientationListener(InCallOrientationListener listener) {
1112 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001113 orientationListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001114 }
1115 }
1116
1117 public void addInCallEventListener(InCallEventListener listener) {
1118 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001119 inCallEventListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001120 }
1121
1122 public void removeInCallEventListener(InCallEventListener listener) {
1123 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001124 inCallEventListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001125 }
1126 }
1127
1128 public ProximitySensor getProximitySensor() {
linyuh183cb712017-12-27 17:02:37 -08001129 return proximitySensor;
Eric Erfanianccca3152017-02-22 16:32:36 -08001130 }
1131
1132 public PseudoScreenState getPseudoScreenState() {
linyuh183cb712017-12-27 17:02:37 -08001133 return pseudoScreenState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001134 }
1135
1136 /** Returns true if the incall app is the foreground application. */
1137 public boolean isShowingInCallUi() {
1138 if (!isActivityStarted()) {
1139 return false;
1140 }
linyuh183cb712017-12-27 17:02:37 -08001141 if (manageConferenceActivity != null && manageConferenceActivity.isVisible()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001142 return true;
1143 }
linyuh183cb712017-12-27 17:02:37 -08001144 return inCallActivity.isVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001145 }
1146
1147 /**
1148 * Returns true if the activity has been created and is running. Returns true as long as activity
1149 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
1150 * (not in foreground).
1151 */
1152 public boolean isActivityStarted() {
linyuh183cb712017-12-27 17:02:37 -08001153 return (inCallActivity != null
1154 && !inCallActivity.isDestroyed()
1155 && !inCallActivity.isFinishing());
Eric Erfanianccca3152017-02-22 16:32:36 -08001156 }
1157
1158 /**
1159 * Determines if the In-Call app is currently changing configuration.
1160 *
1161 * @return {@code true} if the In-Call app is changing configuration.
1162 */
1163 public boolean isChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001164 return isChangingConfigurations;
Eric Erfanianccca3152017-02-22 16:32:36 -08001165 }
1166
1167 /**
1168 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1169 * screen orientation).
1170 */
1171 /*package*/
1172 void updateIsChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001173 isChangingConfigurations = false;
1174 if (inCallActivity != null) {
1175 isChangingConfigurations = inCallActivity.isChangingConfigurations();
Eric Erfanianccca3152017-02-22 16:32:36 -08001176 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001177 LogUtil.v(
1178 "InCallPresenter.updateIsChangingConfigurations",
linyuh183cb712017-12-27 17:02:37 -08001179 "updateIsChangingConfigurations = " + isChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001180 }
1181
yueg10f6e822018-01-17 15:32:18 -08001182 /** Called when the activity goes in/out of the foreground. */
1183 public void onUiShowing(boolean showing) {
linyuh183cb712017-12-27 17:02:37 -08001184 if (proximitySensor != null) {
1185 proximitySensor.onInCallShowing(showing);
Eric Erfanianccca3152017-02-22 16:32:36 -08001186 }
1187
yueg092b21c2017-11-15 16:20:07 -08001188 if (!showing) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001189 updateIsChangingConfigurations();
1190 }
1191
linyuh183cb712017-12-27 17:02:37 -08001192 for (InCallUiListener listener : inCallUiListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001193 listener.onUiShowing(showing);
1194 }
1195
linyuh183cb712017-12-27 17:02:37 -08001196 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001197 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001198 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001199 }
1200 }
1201
Eric Erfanian2ca43182017-08-31 06:57:16 -07001202 public void refreshUi() {
linyuh183cb712017-12-27 17:02:37 -08001203 if (inCallActivity != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001204 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001205 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001206 }
1207 }
1208
Eric Erfanianccca3152017-02-22 16:32:36 -08001209 public void addInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001210 inCallUiListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001211 }
1212
1213 public boolean removeInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001214 return inCallUiListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001215 }
1216
1217 /*package*/
1218 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001219 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001220 notifyVideoPauseController(true);
Eric Erfanian2ca43182017-08-31 06:57:16 -07001221 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001222 }
1223
1224 /*package*/
1225 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001226 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001227 notifyVideoPauseController(false);
1228 }
1229
1230 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001231 LogUtil.d(
1232 "InCallPresenter.notifyVideoPauseController",
linyuh183cb712017-12-27 17:02:37 -08001233 "mIsChangingConfigurations=" + isChangingConfigurations);
1234 if (!isChangingConfigurations) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001235 VideoPauseController.getInstance().onUiShowing(showing);
1236 }
1237 }
1238
1239 /** Brings the app into the foreground if possible. */
1240 public void bringToForeground(boolean showDialpad) {
1241 // Before we bring the incall UI to the foreground, we check to see if:
1242 // 1. It is not currently in the foreground
1243 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1244 // be displayed)
1245 // If the activity hadn't actually been started previously, yet there are still calls
1246 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1247 // bring it up the UI regardless.
linyuh183cb712017-12-27 17:02:37 -08001248 if (!isShowingInCallUi() && inCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001249 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001250 }
1251 }
1252
1253 public void onPostDialCharWait(String callId, String chars) {
1254 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001255 inCallActivity.showDialogForPostCharWait(callId, chars);
Eric Erfanianccca3152017-02-22 16:32:36 -08001256 }
1257 }
1258
1259 /**
1260 * Handles the green CALL key while in-call.
1261 *
1262 * @return true if we consumed the event.
1263 */
1264 public boolean handleCallKey() {
1265 LogUtil.v("InCallPresenter.handleCallKey", null);
1266
1267 // The green CALL button means either "Answer", "Unhold", or
1268 // "Swap calls", or can be a no-op, depending on the current state
1269 // of the Phone.
1270
1271 /** INCOMING CALL */
linyuh183cb712017-12-27 17:02:37 -08001272 final CallList calls = callList;
Eric Erfanianccca3152017-02-22 16:32:36 -08001273 final DialerCall incomingCall = calls.getIncomingCall();
1274 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1275
1276 // (1) Attempt to answer a call
1277 if (incomingCall != null) {
1278 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1279 return true;
1280 }
1281
1282 /** STATE_ACTIVE CALL */
1283 final DialerCall activeCall = calls.getActiveCall();
1284 if (activeCall != null) {
1285 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1286 // consolidate this logic.
1287 final boolean canMerge =
1288 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1289 final boolean canSwap =
1290 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1291
Eric Erfanian2ca43182017-08-31 06:57:16 -07001292 LogUtil.v(
1293 "InCallPresenter.handleCallKey",
1294 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001295
1296 // (2) Attempt actions on conference calls
1297 if (canMerge) {
1298 TelecomAdapter.getInstance().merge(activeCall.getId());
1299 return true;
1300 } else if (canSwap) {
1301 TelecomAdapter.getInstance().swap(activeCall.getId());
1302 return true;
1303 }
1304 }
1305
1306 /** BACKGROUND CALL */
1307 final DialerCall heldCall = calls.getBackgroundCall();
1308 if (heldCall != null) {
1309 // We have a hold call so presumeable it will always support HOLD...but
1310 // there is no harm in double checking.
1311 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1312
Eric Erfanian2ca43182017-08-31 06:57:16 -07001313 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001314
1315 // (4) unhold call
1316 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1317 heldCall.unhold();
1318 return true;
1319 }
1320 }
1321
1322 // Always consume hard keys
1323 return true;
1324 }
1325
Eric Erfanianccca3152017-02-22 16:32:36 -08001326 /** Clears the previous fullscreen state. */
1327 public void clearFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001328 isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001329 }
1330
1331 /**
1332 * Changes the fullscreen mode of the in-call UI.
1333 *
1334 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1335 * otherwise.
1336 */
1337 public void setFullScreen(boolean isFullScreen) {
1338 setFullScreen(isFullScreen, false /* force */);
1339 }
1340
1341 /**
1342 * Changes the fullscreen mode of the in-call UI.
1343 *
1344 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1345 * otherwise.
1346 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1347 */
1348 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001349 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001350
1351 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1352 if (isDialpadVisible()) {
1353 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001354 LogUtil.v(
1355 "InCallPresenter.setFullScreen",
1356 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001357 }
1358
linyuh183cb712017-12-27 17:02:37 -08001359 if (this.isFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001360 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001361 return;
1362 }
linyuh183cb712017-12-27 17:02:37 -08001363 this.isFullScreen = isFullScreen;
1364 notifyFullscreenModeChange(this.isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001365 }
1366
1367 /**
1368 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1369 * otherwise.
1370 */
1371 public boolean isFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001372 return isFullScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001373 }
1374
1375 /**
1376 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1377 *
1378 * @param isFullscreenMode {@code True} if entering full screen mode.
1379 */
1380 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001381 for (InCallEventListener listener : inCallEventListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001382 listener.onFullscreenModeChanged(isFullscreenMode);
1383 }
1384 }
1385
linyuh7b86f562017-11-16 11:24:09 -08001386 /** Instruct the in-call activity to show an error dialog or toast for a disconnected call. */
1387 private void showDialogOrToastForDisconnectedCall(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -07001388 if (call.getState() != DialerCall.State.DISCONNECTED) {
linyuh7b86f562017-11-16 11:24:09 -08001389 return;
Eric Erfanianccca3152017-02-22 16:32:36 -08001390 }
linyuh7b86f562017-11-16 11:24:09 -08001391
1392 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1393 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1394 setDisconnectCauseForMissingAccounts(call);
1395 }
1396
yueg32d9ae72018-04-05 17:35:56 -07001397 if (isActivityStarted()) {
1398 inCallActivity.showDialogOrToastForDisconnectedCall(
1399 new DisconnectMessage(inCallActivity, call));
1400 } else {
1401 CharSequence message = new DisconnectMessage(context, call).toastMessage;
1402 if (message != null) {
1403 Toast.makeText(context, message, Toast.LENGTH_LONG).show();
1404 }
1405 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001406 }
1407
1408 /**
1409 * When the state of in-call changes, this is the first method to get called. It determines if the
1410 * UI needs to be started or finished depending on the new state and does it.
1411 */
1412 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001413 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001414 LogUtil.d(
linyuh183cb712017-12-27 17:02:37 -08001415 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + inCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001416
1417 // TODO: Consider a proper state machine implementation
1418
1419 // If the state isn't changing we have already done any starting/stopping of activities in
1420 // a previous pass...so lets cut out early
linyuh183cb712017-12-27 17:02:37 -08001421 if (newState == inCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001422 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001423 return newState;
1424 }
1425
1426 // A new Incoming call means that the user needs to be notified of the the call (since
1427 // it wasn't them who initiated it). We do this through full screen notifications and
1428 // happens indirectly through {@link StatusBarNotifier}.
1429 //
1430 // The process for incoming calls is as follows:
1431 //
1432 // 1) CallList - Announces existence of new INCOMING call
1433 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1434 // - should be set to INCOMING.
1435 // 3) InCallPresenter - This method is called to see if we need to start or finish
1436 // the app given the new state.
1437 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1438 // StatusBarNotifier explicitly to issue a FullScreen Notification
1439 // that will either start the InCallActivity or show the user a
1440 // top-level notification dialog if the user is in an immersive app.
1441 // That notification can also start the InCallActivity.
1442 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1443 // call InCallPresenter::setActivity() to let the presenter
1444 // know that start-up is complete.
1445 //
1446 // [ AND NOW YOU'RE IN THE CALL. voila! ]
Eric Erfanianccca3152017-02-22 16:32:36 -08001447
1448 // A dialog to show on top of the InCallUI to select a PhoneAccount
1449 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1450
1451 // A new outgoing call indicates that the user just now dialed a number and when that
1452 // happens we need to display the screen immediately or show an account picker dialog if
1453 // no default is set. However, if the main InCallUI is already visible, we do not want to
1454 // re-initiate the start-up animation, so we do not need to do anything here.
1455 //
1456 // It is also possible to go into an intermediate state where the call has been initiated
1457 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1458 // This pending outgoing state can also launch the call screen.
1459 //
1460 // This is different from the incoming call sequence because we do not need to shock the
1461 // user with a top-level notification. Just show the call UI normally.
1462 boolean callCardFragmentVisible =
linyuh183cb712017-12-27 17:02:37 -08001463 inCallActivity != null && inCallActivity.getCallCardFragmentVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001464 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1465 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1466
1467 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1468 // outgoing call process, so the UI should be brought up to show an error dialog.
1469 showCallUi |=
linyuh183cb712017-12-27 17:02:37 -08001470 (InCallState.PENDING_OUTGOING == inCallState
Eric Erfanianccca3152017-02-22 16:32:36 -08001471 && InCallState.INCALL == newState
1472 && !isShowingInCallUi());
1473
1474 // Another exception - InCallActivity is in charge of disconnecting a call with no
1475 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1476 // call so that:
1477 // 1) The call can be disconnected correctly
1478 // 2) The UI comes up and correctly displays the error dialog.
1479 // TODO: Remove these special case conditions by making InCallPresenter a true state
1480 // machine. Telecom should also be the component responsible for disconnecting a call
1481 // with no valid accounts.
1482 showCallUi |=
1483 InCallState.PENDING_OUTGOING == newState
1484 && mainUiNotVisible
linyuh183cb712017-12-27 17:02:37 -08001485 && isCallWithNoValidAccounts(callList.getPendingOutgoingCall());
Eric Erfanianccca3152017-02-22 16:32:36 -08001486
1487 // The only time that we have an instance of mInCallActivity and it isn't started is
1488 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1489 // the activity. When it is finally destroyed, we double check if we should bring it back
1490 // up so we aren't going to lose anything by avoiding a second startup here.
linyuh183cb712017-12-27 17:02:37 -08001491 boolean activityIsFinishing = inCallActivity != null && !isActivityStarted();
Eric Erfanianccca3152017-02-22 16:32:36 -08001492 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001493 LogUtil.i(
1494 "InCallPresenter.startOrFinishUi",
linyuh183cb712017-12-27 17:02:37 -08001495 "Undo the state change: " + newState + " -> " + inCallState);
wangqicf61ca02017-08-31 15:32:55 -07001496 Trace.endSection();
linyuh183cb712017-12-27 17:02:37 -08001497 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001498 }
1499
1500 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1501 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1502 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1503 // outgoing call after user ignore it by pressing home button.
1504 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1505 && !showCallUi
1506 && isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001507 inCallActivity.dismissPendingDialogs();
Eric Erfanianccca3152017-02-22 16:32:36 -08001508 }
1509
yueg4613e8f2018-04-23 13:35:33 -07001510 if ((showCallUi || showAccountPicker) && !shouldStartInBubbleMode()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001511 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001512 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001513 } else if (newState == InCallState.NO_CALLS) {
1514 // The new state is the no calls state. Tear everything down.
yueg8e544862018-04-06 14:34:59 -07001515 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001516 attemptFinishActivity();
1517 attemptCleanup();
1518 }
1519
wangqicf61ca02017-08-31 15:32:55 -07001520 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001521 return newState;
1522 }
1523
1524 /**
1525 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1526 * or PhoneAccounts to select from.
1527 */
1528 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1529
1530 Bundle extras = call.getIntentExtras();
1531 // Initialize the extras bundle to avoid NPE
1532 if (extras == null) {
1533 extras = new Bundle();
1534 }
1535
1536 final List<PhoneAccountHandle> phoneAccountHandles =
1537 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1538
1539 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1540 String scheme = call.getHandle().getScheme();
1541 final String errorMsg =
1542 PhoneAccount.SCHEME_TEL.equals(scheme)
linyuh183cb712017-12-27 17:02:37 -08001543 ? context.getString(R.string.callFailed_simError)
1544 : context.getString(R.string.incall_error_supp_service_unknown);
Eric Erfanianccca3152017-02-22 16:32:36 -08001545 DisconnectCause disconnectCause =
1546 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1547 call.setDisconnectCause(disconnectCause);
1548 }
1549 }
1550
Eric Erfanianccca3152017-02-22 16:32:36 -08001551 /**
1552 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1553 * Calling classes should use this as an indication whether to interact with the
1554 * InCallPresenter or not.
1555 */
1556 public boolean isReadyForTearDown() {
linyuh183cb712017-12-27 17:02:37 -08001557 return inCallActivity == null && !serviceConnected && inCallState == InCallState.NO_CALLS;
Eric Erfanianccca3152017-02-22 16:32:36 -08001558 }
1559
1560 /**
1561 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1562 */
1563 private void attemptCleanup() {
1564 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001565 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001566
1567 cleanupSurfaces();
1568
linyuh183cb712017-12-27 17:02:37 -08001569 isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001570
yueg8e544862018-04-06 14:34:59 -07001571 audioRouteSetForBubbleMode = false;
yueg32d9ae72018-04-05 17:35:56 -07001572
Eric Erfanianccca3152017-02-22 16:32:36 -08001573 // blow away stale contact info so that we get fresh data on
1574 // the next set of calls
linyuh183cb712017-12-27 17:02:37 -08001575 if (contactInfoCache != null) {
1576 contactInfoCache.clearCache();
Eric Erfanianccca3152017-02-22 16:32:36 -08001577 }
linyuh183cb712017-12-27 17:02:37 -08001578 contactInfoCache = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001579
linyuh183cb712017-12-27 17:02:37 -08001580 if (proximitySensor != null) {
1581 removeListener(proximitySensor);
1582 proximitySensor.tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -08001583 }
linyuh183cb712017-12-27 17:02:37 -08001584 proximitySensor = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001585
linyuh183cb712017-12-27 17:02:37 -08001586 if (statusBarNotifier != null) {
1587 removeListener(statusBarNotifier);
1588 EnrichedCallComponent.get(context)
Eric Erfaniand8046e52017-04-06 09:41:50 -07001589 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -08001590 .unregisterStateChangedListener(statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001591 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001592
linyuh183cb712017-12-27 17:02:37 -08001593 if (externalCallNotifier != null && externalCallList != null) {
1594 externalCallList.removeExternalCallListener(externalCallNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001595 }
linyuh183cb712017-12-27 17:02:37 -08001596 statusBarNotifier = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001597
linyuh183cb712017-12-27 17:02:37 -08001598 if (callList != null) {
1599 callList.removeListener(this);
1600 callList.removeListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001601 }
linyuh183cb712017-12-27 17:02:37 -08001602 callList = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001603
linyuh183cb712017-12-27 17:02:37 -08001604 context = null;
1605 inCallActivity = null;
1606 manageConferenceActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001607
linyuh183cb712017-12-27 17:02:37 -08001608 listeners.clear();
1609 incomingCallListeners.clear();
1610 detailsListeners.clear();
1611 canAddCallListeners.clear();
1612 orientationListeners.clear();
1613 inCallEventListeners.clear();
1614 inCallUiListeners.clear();
1615 if (!inCallUiLocks.isEmpty()) {
1616 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + inCallUiLocks);
1617 inCallUiLocks.clear();
twyen8efb4952017-10-06 16:35:54 -07001618 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001619 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001620 }
1621 }
1622
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001623 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001624 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
linyuh183cb712017-12-27 17:02:37 -08001625 context.startActivity(
1626 InCallActivity.getIntent(context, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001627 }
1628
1629 public void onServiceBind() {
linyuh183cb712017-12-27 17:02:37 -08001630 serviceBound = true;
Eric Erfanianccca3152017-02-22 16:32:36 -08001631 }
1632
1633 public void onServiceUnbind() {
1634 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -08001635 serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001636 }
1637
1638 public boolean isServiceBound() {
linyuh183cb712017-12-27 17:02:37 -08001639 return serviceBound;
Eric Erfanianccca3152017-02-22 16:32:36 -08001640 }
1641
1642 public void maybeStartRevealAnimation(Intent intent) {
linyuh183cb712017-12-27 17:02:37 -08001643 if (intent == null || inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001644 return;
1645 }
1646 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1647 if (extras == null) {
1648 // Incoming call, just show the in-call UI directly.
1649 return;
1650 }
1651
1652 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1653 // Account selection dialog will show up so don't show the animation.
1654 return;
1655 }
1656
1657 final PhoneAccountHandle accountHandle =
1658 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1659 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001660
yueg32d9ae72018-04-05 17:35:56 -07001661 setBoundAndWaitingForOutgoingCall(true, accountHandle);
1662
yueg4613e8f2018-04-23 13:35:33 -07001663 if (shouldStartInBubbleModeWithExtras(extras)) {
yueg32d9ae72018-04-05 17:35:56 -07001664 LogUtil.i("InCallPresenter.maybeStartRevealAnimation", "shouldStartInBubbleMode");
1665 // Show bubble instead of in call UI
1666 return;
1667 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001668
1669 final Intent activityIntent =
linyuh183cb712017-12-27 17:02:37 -08001670 InCallActivity.getIntent(context, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001671 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
linyuh183cb712017-12-27 17:02:37 -08001672 context.startActivity(activityIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001673 }
1674
1675 /**
1676 * Retrieves the current in-call camera manager instance, creating if necessary.
1677 *
1678 * @return The {@link InCallCameraManager}.
1679 */
1680 public InCallCameraManager getInCallCameraManager() {
1681 synchronized (this) {
linyuh183cb712017-12-27 17:02:37 -08001682 if (inCallCameraManager == null) {
1683 inCallCameraManager = new InCallCameraManager(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001684 }
1685
linyuh183cb712017-12-27 17:02:37 -08001686 return inCallCameraManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001687 }
1688 }
1689
1690 /**
1691 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1692 *
1693 * @param orientation The screen orientation of the device (one of: {@link
1694 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1695 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1696 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1697 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1698 */
1699 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001700 LogUtil.d(
1701 "InCallPresenter.onDeviceOrientationChange",
1702 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001703
linyuh183cb712017-12-27 17:02:37 -08001704 if (callList != null) {
1705 callList.notifyCallsOfDeviceRotation(orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001706 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001707 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001708 }
1709
1710 // Notify listeners of device orientation changed.
linyuh183cb712017-12-27 17:02:37 -08001711 for (InCallOrientationListener listener : orientationListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001712 listener.onDeviceOrientationChanged(orientation);
1713 }
1714 }
1715
1716 /**
1717 * Configures the in-call UI activity so it can change orientations or not. Enables the
1718 * orientation event listener if allowOrientationChange is true, disables it if false.
1719 *
1720 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1721 * landscape. {@code false} if the in-call UI should be locked in portrait.
1722 */
1723 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
linyuh183cb712017-12-27 17:02:37 -08001724 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001725 LogUtil.e(
1726 "InCallPresenter.setInCallAllowsOrientationChange",
1727 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001728 return;
1729 }
linyuh183cb712017-12-27 17:02:37 -08001730 inCallActivity.setAllowOrientationChange(allowOrientationChange);
Eric Erfanianccca3152017-02-22 16:32:36 -08001731 }
1732
1733 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001734 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
linyuh183cb712017-12-27 17:02:37 -08001735 screenTimeoutEnabled = enable;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001736 applyScreenTimeout();
1737 }
1738
1739 private void applyScreenTimeout() {
linyuh183cb712017-12-27 17:02:37 -08001740 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001741 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001742 return;
1743 }
1744
linyuh183cb712017-12-27 17:02:37 -08001745 final Window window = inCallActivity.getWindow();
1746 if (screenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001747 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1748 } else {
1749 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1750 }
1751 }
1752
1753 /**
1754 * Hides or shows the conference manager fragment.
1755 *
1756 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1757 * be hidden.
1758 */
1759 public void showConferenceCallManager(boolean show) {
linyuh183cb712017-12-27 17:02:37 -08001760 if (inCallActivity != null) {
1761 inCallActivity.showConferenceFragment(show);
Eric Erfanianccca3152017-02-22 16:32:36 -08001762 }
linyuh183cb712017-12-27 17:02:37 -08001763 if (!show && manageConferenceActivity != null) {
1764 manageConferenceActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -08001765 }
1766 }
1767
1768 /**
1769 * Determines if the dialpad is visible.
1770 *
1771 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1772 */
1773 public boolean isDialpadVisible() {
linyuh183cb712017-12-27 17:02:37 -08001774 if (inCallActivity == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001775 return false;
1776 }
linyuh183cb712017-12-27 17:02:37 -08001777 return inCallActivity.isDialpadVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001778 }
1779
1780 public ThemeColorManager getThemeColorManager() {
linyuh183cb712017-12-27 17:02:37 -08001781 return themeColorManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001782 }
1783
wangqi8d662ca2017-10-26 11:27:19 -07001784 @VisibleForTesting
1785 public void setThemeColorManager(ThemeColorManager themeColorManager) {
linyuh183cb712017-12-27 17:02:37 -08001786 this.themeColorManager = themeColorManager;
wangqi8d662ca2017-10-26 11:27:19 -07001787 }
1788
Eric Erfanianccca3152017-02-22 16:32:36 -08001789 /** Called when the foreground call changes. */
1790 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh183cb712017-12-27 17:02:37 -08001791 themeColorManager.onForegroundCallChanged(context, newForegroundCall);
1792 if (inCallActivity != null) {
1793 inCallActivity.onForegroundCallChanged(newForegroundCall);
Eric Erfanianccca3152017-02-22 16:32:36 -08001794 }
1795 }
1796
1797 public InCallActivity getActivity() {
linyuh183cb712017-12-27 17:02:37 -08001798 return inCallActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -08001799 }
1800
1801 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1802 public void setActivity(InCallActivity inCallActivity) {
1803 if (inCallActivity == null) {
1804 throw new IllegalArgumentException("registerActivity cannot be called with null");
1805 }
linyuh183cb712017-12-27 17:02:37 -08001806 if (this.inCallActivity != null && this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001807 LogUtil.w(
1808 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001809 }
1810 updateActivity(inCallActivity);
1811 }
1812
1813 ExternalCallNotifier getExternalCallNotifier() {
linyuh183cb712017-12-27 17:02:37 -08001814 return externalCallNotifier;
Eric Erfanianccca3152017-02-22 16:32:36 -08001815 }
1816
1817 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001818 if (localVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001819 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001820 if (context != null) {
1821 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001822 }
linyuh183cb712017-12-27 17:02:37 -08001823 localVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001824 }
linyuh183cb712017-12-27 17:02:37 -08001825 return localVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001826 }
1827
1828 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001829 if (remoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001830 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001831 if (context != null) {
1832 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001833 }
linyuh183cb712017-12-27 17:02:37 -08001834 remoteVideoSurfaceTexture = VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001835 }
linyuh183cb712017-12-27 17:02:37 -08001836 return remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001837 }
1838
1839 void cleanupSurfaces() {
linyuh183cb712017-12-27 17:02:37 -08001840 if (remoteVideoSurfaceTexture != null) {
1841 remoteVideoSurfaceTexture.setDoneWithSurface();
1842 remoteVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001843 }
linyuh183cb712017-12-27 17:02:37 -08001844 if (localVideoSurfaceTexture != null) {
1845 localVideoSurfaceTexture.setDoneWithSurface();
1846 localVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001847 }
1848 }
1849
yueg77cb8e52017-10-27 16:42:51 -07001850 @Override
1851 public void onAudioStateChanged(CallAudioState audioState) {
yueg8e544862018-04-06 14:34:59 -07001852 // Set sensible audio route for bubble mode when we get real audio state for the first time
1853 // During the first time this function is called, supportedRouteMask is set to
yueg4613e8f2018-04-23 13:35:33 -07001854 // SUPPORTED_AUDIO_ROUTE_ALL, but it's OK since shouldStartInBubbleMode() is false at that time
1855 // (callList not updated yet).
1856 if (!audioRouteSetForBubbleMode && shouldStartInBubbleMode()) {
yueg8e544862018-04-06 14:34:59 -07001857 setAudioRouteForBubbleMode(audioState);
1858 audioRouteSetForBubbleMode = true;
1859 }
1860
linyuh183cb712017-12-27 17:02:37 -08001861 if (statusBarNotifier != null) {
1862 statusBarNotifier.updateNotification();
yueg77cb8e52017-10-27 16:42:51 -07001863 }
1864 }
1865
yueg8e544862018-04-06 14:34:59 -07001866 /**
1867 * Set audio route to make audio sensible. According to availability, set audio route to Bluetooth
1868 * or wired headset or speaker.
1869 */
1870 private void setAudioRouteForBubbleMode(CallAudioState audioState) {
1871 if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
1872 == CallAudioState.ROUTE_BLUETOOTH) {
1873 // Use Bluetooth if available
1874 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
1875 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "bluetooth");
1876 } else if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_WIRED_HEADSET)
1877 == CallAudioState.ROUTE_WIRED_HEADSET) {
1878 // Use wired headset if available
1879 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_WIRED_HEADSET);
1880 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "wired headset");
1881 } else {
1882 // Use speaker
1883 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_SPEAKER);
1884 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "speaker");
1885 }
1886 }
1887
Eric Erfanianccca3152017-02-22 16:32:36 -08001888 /** All the main states of InCallActivity. */
1889 public enum InCallState {
1890 // InCall Screen is off and there are no calls
1891 NO_CALLS,
1892
1893 // Incoming-call screen is up
1894 INCOMING,
1895
1896 // In-call experience is showing
1897 INCALL,
1898
1899 // Waiting for user input before placing outgoing call
1900 WAITING_FOR_ACCOUNT,
1901
1902 // UI is starting up but no call has been initiated yet.
1903 // The UI is waiting for Telecom to respond.
1904 PENDING_OUTGOING,
1905
1906 // User is dialing out
1907 OUTGOING;
1908
1909 public boolean isIncoming() {
1910 return (this == INCOMING);
1911 }
1912
1913 public boolean isConnectingOrConnected() {
1914 return (this == INCOMING || this == OUTGOING || this == INCALL);
1915 }
1916 }
1917
1918 /** Interface implemented by classes that need to know about the InCall State. */
1919 public interface InCallStateListener {
1920
1921 // TODO: Enhance state to contain the call objects instead of passing CallList
1922 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1923 }
1924
1925 public interface IncomingCallListener {
1926
1927 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1928 }
1929
1930 public interface CanAddCallListener {
1931
1932 void onCanAddCallChanged(boolean canAddCall);
1933 }
1934
1935 public interface InCallDetailsListener {
1936
1937 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1938 }
1939
1940 public interface InCallOrientationListener {
1941
1942 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1943 }
1944
1945 /**
1946 * Interface implemented by classes that need to know about events which occur within the In-Call
1947 * UI. Used as a means of communicating between fragments that make up the UI.
1948 */
1949 public interface InCallEventListener {
1950
1951 void onFullscreenModeChanged(boolean isFullscreenMode);
1952 }
1953
1954 public interface InCallUiListener {
1955
1956 void onUiShowing(boolean showing);
1957 }
twyen8efb4952017-10-06 16:35:54 -07001958
1959 private class InCallUiLockImpl implements InCallUiLock {
1960 private final String tag;
1961
1962 private InCallUiLockImpl(String tag) {
1963 this.tag = tag;
1964 }
1965
1966 @MainThread
1967 @Override
1968 public void release() {
1969 Assert.isMainThread();
1970 releaseInCallUiLock(InCallUiLockImpl.this);
1971 }
1972
1973 @Override
1974 public String toString() {
1975 return "InCallUiLock[" + tag + "]";
1976 }
1977 }
1978
1979 @MainThread
1980 public InCallUiLock acquireInCallUiLock(String tag) {
1981 Assert.isMainThread();
1982 InCallUiLock lock = new InCallUiLockImpl(tag);
linyuh183cb712017-12-27 17:02:37 -08001983 inCallUiLocks.add(lock);
twyen8efb4952017-10-06 16:35:54 -07001984 return lock;
1985 }
1986
1987 @MainThread
1988 private void releaseInCallUiLock(InCallUiLock lock) {
1989 Assert.isMainThread();
1990 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
linyuh183cb712017-12-27 17:02:37 -08001991 inCallUiLocks.remove(lock);
1992 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07001993 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
linyuh183cb712017-12-27 17:02:37 -08001994 if (inCallState == InCallState.NO_CALLS) {
twyen8efb4952017-10-06 16:35:54 -07001995 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
1996 attemptFinishActivity();
1997 attemptCleanup();
1998 }
1999 }
2000 }
2001
2002 @MainThread
2003 public boolean isInCallUiLocked() {
2004 Assert.isMainThread();
linyuh183cb712017-12-27 17:02:37 -08002005 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002006 return false;
2007 }
linyuh183cb712017-12-27 17:02:37 -08002008 for (InCallUiLock lock : inCallUiLocks) {
twyen8efb4952017-10-06 16:35:54 -07002009 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
2010 }
2011 return true;
2012 }
2013
linyuh183cb712017-12-27 17:02:37 -08002014 private final Set<InCallUiLock> inCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08002015}