blob: 1dc150d56cd9c1409040fd3bd3dc76027cb0e8d7 [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
yueg32d9ae72018-04-05 17:35:56 -0700271 private boolean shouldStartInBubbleMode;
yueg8e544862018-04-06 14:34:59 -0700272 private boolean audioRouteSetForBubbleMode;
yueg32d9ae72018-04-05 17:35:56 -0700273
Eric Erfanian10b34a52017-05-04 08:23:17 -0700274 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800275 @VisibleForTesting
276 InCallPresenter() {}
277
278 public static synchronized InCallPresenter getInstance() {
linyuh183cb712017-12-27 17:02:37 -0800279 if (inCallPresenter == null) {
wangqic8cf79e2017-10-17 09:21:00 -0700280 Trace.beginSection("InCallPresenter.Constructor");
linyuh183cb712017-12-27 17:02:37 -0800281 inCallPresenter = new InCallPresenter();
wangqic8cf79e2017-10-17 09:21:00 -0700282 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800283 }
linyuh183cb712017-12-27 17:02:37 -0800284 return inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -0800285 }
286
Eric Erfanian10b34a52017-05-04 08:23:17 -0700287 @VisibleForTesting
288 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
linyuh183cb712017-12-27 17:02:37 -0800289 InCallPresenter.inCallPresenter = inCallPresenter;
Eric Erfanian10b34a52017-05-04 08:23:17 -0700290 }
291
Eric Erfanianccca3152017-02-22 16:32:36 -0800292 /**
293 * Determines whether or not a call has no valid phone accounts that can be used to make the call
294 * with. Emergency calls do not require a phone account.
295 *
296 * @param call to check accounts for.
297 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
298 * call contains a phone account that could be used to initiate it with, or is an emergency
299 * call.
300 */
301 public static boolean isCallWithNoValidAccounts(DialerCall call) {
302 if (call != null && !call.isEmergencyCall()) {
303 Bundle extras = call.getIntentExtras();
304
305 if (extras == null) {
306 extras = EMPTY_EXTRAS;
307 }
308
309 final List<PhoneAccountHandle> phoneAccountHandles =
310 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
311
312 if ((call.getAccountHandle() == null
313 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700314 LogUtil.i(
315 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800316 return true;
317 }
318 }
319 return false;
320 }
321
322 public InCallState getInCallState() {
linyuh183cb712017-12-27 17:02:37 -0800323 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800324 }
325
326 public CallList getCallList() {
linyuh183cb712017-12-27 17:02:37 -0800327 return callList;
Eric Erfanianccca3152017-02-22 16:32:36 -0800328 }
329
330 public void setUp(
331 @NonNull Context context,
332 CallList callList,
333 ExternalCallList externalCallList,
334 StatusBarNotifier statusBarNotifier,
335 ExternalCallNotifier externalCallNotifier,
336 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700337 ProximitySensor proximitySensor,
erfaniand05d8992018-03-20 19:42:26 -0700338 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler,
yueg32d9ae72018-04-05 17:35:56 -0700339 @NonNull SpeakEasyCallManager speakEasyCallManager,
340 Intent intent) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700341 Trace.beginSection("InCallPresenter.setUp");
linyuh183cb712017-12-27 17:02:37 -0800342 if (serviceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700343 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
linyuh183cb712017-12-27 17:02:37 -0800344 if (context != this.context || callList != this.callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800345 throw new IllegalStateException();
346 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700347 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800348 return;
349 }
350
351 Objects.requireNonNull(context);
linyuh183cb712017-12-27 17:02:37 -0800352 this.context = context;
Eric Erfanianccca3152017-02-22 16:32:36 -0800353
linyuh183cb712017-12-27 17:02:37 -0800354 this.contactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800355
linyuh183cb712017-12-27 17:02:37 -0800356 this.statusBarNotifier = statusBarNotifier;
357 this.externalCallNotifier = externalCallNotifier;
358 addListener(this.statusBarNotifier);
359 EnrichedCallComponent.get(this.context)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700360 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -0800361 .registerStateChangedListener(this.statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800362
linyuh183cb712017-12-27 17:02:37 -0800363 this.proximitySensor = proximitySensor;
364 addListener(this.proximitySensor);
Eric Erfanianccca3152017-02-22 16:32:36 -0800365
linyuh183cb712017-12-27 17:02:37 -0800366 if (themeColorManager == null) {
367 themeColorManager =
368 new ThemeColorManager(new InCallUIMaterialColorMapUtils(this.context.getResources()));
wangqi8d662ca2017-10-26 11:27:19 -0700369 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800370
linyuh183cb712017-12-27 17:02:37 -0800371 this.callList = callList;
372 this.externalCallList = externalCallList;
373 externalCallList.addExternalCallListener(this.externalCallNotifier);
374 externalCallList.addExternalCallListener(externalCallListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800375
376 // This only gets called by the service so this is okay.
linyuh183cb712017-12-27 17:02:37 -0800377 serviceConnected = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800378
379 // The final thing we do in this set up is add ourselves as a listener to CallList. This
380 // will kick off an update and the whole process can start.
linyuh183cb712017-12-27 17:02:37 -0800381 this.callList.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800382
383 // Create spam call list listener and add it to the list of listeners
linyuh183cb712017-12-27 17:02:37 -0800384 spamCallListListener =
zachh6a4cebd2017-10-24 17:10:06 -0700385 new SpamCallListListener(
386 context, DialerExecutorComponent.get(context).dialerExecutorFactory());
linyuh183cb712017-12-27 17:02:37 -0800387 this.callList.addListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800388
389 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800390
linyuh183cb712017-12-27 17:02:37 -0800391 filteredQueryHandler = filteredNumberQueryHandler;
erfaniand05d8992018-03-20 19:42:26 -0700392 this.speakEasyCallManager = speakEasyCallManager;
linyuh183cb712017-12-27 17:02:37 -0800393 this.context
Eric Erfanianccca3152017-02-22 16:32:36 -0800394 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800395 .listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800396
yueg77cb8e52017-10-27 16:42:51 -0700397 AudioModeProvider.getInstance().addListener(this);
398
roldenburg42099012018-04-09 14:47:15 -0700399 // Add listener to notify Telephony process when the incoming call screen is started or
400 // finished. This is for hiding USSD dialog because the incoming call screen should have
401 // higher precedence over this dialog.
402 MotorolaInCallUiNotifier motorolaInCallUiNotifier = new MotorolaInCallUiNotifier(context);
403 addInCallUiListener(motorolaInCallUiNotifier);
404 addListener(motorolaInCallUiNotifier);
yueg7f5acbe2018-01-10 13:50:29 -0800405
yueg32d9ae72018-04-05 17:35:56 -0700406 this.shouldStartInBubbleMode = shouldStartInBubbleMode(intent);
407
Eric Erfanian2ca43182017-08-31 06:57:16 -0700408 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
409 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800410 }
411
412 /**
yueg32d9ae72018-04-05 17:35:56 -0700413 * Return whether we should start call in bubble mode and not show InCallActivity. The call mode
414 * should be set in CallConfiguration in EXTRA_OUTGOING_CALL_EXTRAS when starting a call intent.
415 */
416 private boolean shouldStartInBubbleMode(Intent intent) {
417 if (!ReturnToCallController.isEnabled(context)) {
418 return false;
419 }
420 if (!intent.hasExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS)) {
421 return false;
422 }
423 Bundle extras = intent.getParcelableExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
424 byte[] callConfigurationByteArray = extras.getByteArray(CALL_CONFIGURATION_EXTRA);
425 if (callConfigurationByteArray == null) {
426 return false;
427 }
428 try {
429 CallConfiguration callConfiguration = CallConfiguration.parseFrom(callConfigurationByteArray);
430 LogUtil.i(
431 "InCallPresenter.shouldStartInBubbleMode",
432 "call mode: " + callConfiguration.getCallMode());
433 return callConfiguration.getCallMode() == Mode.BUBBLE;
434 } catch (InvalidProtocolBufferException e) {
435 return false;
436 }
437 }
438
439 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800440 * Called when the telephony service has disconnected from us. This will happen when there are no
441 * more active calls. However, we may still want to continue showing the UI for certain cases like
442 * showing "Call Ended". What we really want is to wait for the activity and the service to both
443 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
444 * secondary method that performs the aforementioned logic.
445 */
446 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700447 LogUtil.d("InCallPresenter.tearDown", "tearDown");
linyuh183cb712017-12-27 17:02:37 -0800448 callList.clearOnDisconnect();
Eric Erfanianccca3152017-02-22 16:32:36 -0800449
linyuh183cb712017-12-27 17:02:37 -0800450 serviceConnected = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800451
linyuh183cb712017-12-27 17:02:37 -0800452 context
Eric Erfanianccca3152017-02-22 16:32:36 -0800453 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800454 .listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800455
456 attemptCleanup();
457 VideoPauseController.getInstance().tearDown();
yueg77cb8e52017-10-27 16:42:51 -0700458 AudioModeProvider.getInstance().removeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800459 }
460
461 private void attemptFinishActivity() {
linyuh183cb712017-12-27 17:02:37 -0800462 screenTimeoutEnabled = true;
463 final boolean doFinish = (inCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700464 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800465 if (doFinish) {
linyuh183cb712017-12-27 17:02:37 -0800466 inCallActivity.setExcludeFromRecents(true);
467 inCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800468 }
469 }
470
471 /**
472 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
473 * for more insight on the tear-down process.
474 */
475 public void unsetActivity(InCallActivity inCallActivity) {
476 if (inCallActivity == null) {
477 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
478 }
linyuh183cb712017-12-27 17:02:37 -0800479 if (this.inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700480 LogUtil.i(
481 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800482 return;
483 }
linyuh183cb712017-12-27 17:02:37 -0800484 if (this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700485 LogUtil.w(
486 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800487 "Second instance of InCallActivity is trying to unregister when another"
488 + " instance is active. Ignoring.");
489 return;
490 }
491 updateActivity(null);
492 }
493
494 /**
495 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
496 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
497 */
498 private void updateActivity(InCallActivity inCallActivity) {
wangqi9982f0d2017-10-11 17:46:07 -0700499 Trace.beginSection("InCallPresenter.updateActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800500 boolean updateListeners = false;
501 boolean doAttemptCleanup = false;
502
503 if (inCallActivity != null) {
linyuh183cb712017-12-27 17:02:37 -0800504 if (this.inCallActivity == null) {
505 context = inCallActivity.getApplicationContext();
Eric Erfanianccca3152017-02-22 16:32:36 -0800506 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700507 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800508 } else {
509 // since setActivity is called onStart(), it can be called multiple times.
510 // This is fine and ignorable, but we do not want to update the world every time
511 // this happens (like going to/from background) so we do not set updateListeners.
512 }
513
linyuh183cb712017-12-27 17:02:37 -0800514 this.inCallActivity = inCallActivity;
515 this.inCallActivity.setExcludeFromRecents(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800516
517 // By the time the UI finally comes up, the call may already be disconnected.
518 // If that's the case, we may need to show an error dialog.
linyuh183cb712017-12-27 17:02:37 -0800519 if (callList != null && callList.getDisconnectedCall() != null) {
520 showDialogOrToastForDisconnectedCall(callList.getDisconnectedCall());
Eric Erfanianccca3152017-02-22 16:32:36 -0800521 }
522
523 // When the UI comes up, we need to first check the in-call state.
524 // If we are showing NO_CALLS, that means that a call probably connected and
525 // then immediately disconnected before the UI was able to come up.
526 // If we dont have any calls, start tearing down the UI instead.
527 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
528 // it has been set.
linyuh183cb712017-12-27 17:02:37 -0800529 if (inCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700530 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800531 attemptFinishActivity();
wangqi9982f0d2017-10-11 17:46:07 -0700532 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800533 return;
534 }
535 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700536 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800537 updateListeners = true;
linyuh183cb712017-12-27 17:02:37 -0800538 this.inCallActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800539
540 // We attempt cleanup for the destroy case but only after we recalculate the state
541 // to see if we need to come back up or stay shut down. This is why we do the
542 // cleanup after the call to onCallListChange() instead of directly here.
543 doAttemptCleanup = true;
544 }
545
546 // Messages can come from the telephony layer while the activity is coming up
547 // and while the activity is going down. So in both cases we need to recalculate what
548 // state we should be in after they complete.
549 // Examples: (1) A new incoming call could come in and then get disconnected before
550 // the activity is created.
551 // (2) All calls could disconnect and then get a new incoming call before the
552 // activity is destroyed.
553 //
Eric Erfanian938468d2017-10-24 14:05:52 -0700554 // a bug - We previously had a check for mServiceConnected here as well, but there are
Eric Erfanianccca3152017-02-22 16:32:36 -0800555 // cases where we need to recalculate the current state even if the service in not
556 // connected. In particular the case where startOrFinish() is called while the app is
557 // already finish()ing. In that case, we skip updating the state with the knowledge that
558 // we will check again once the activity has finished. That means we have to recalculate the
559 // state here even if the service is disconnected since we may not have finished a state
560 // transition while finish()ing.
561 if (updateListeners) {
linyuh183cb712017-12-27 17:02:37 -0800562 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800563 }
564
565 if (doAttemptCleanup) {
566 attemptCleanup();
567 }
wangqi9982f0d2017-10-11 17:46:07 -0700568 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800569 }
570
erfanian12243de2018-04-17 12:17:08 -0700571 public SpeakEasyCallManager getSpeakEasyCallManager() {
572 return this.speakEasyCallManager;
573 }
574
Eric Erfanianccca3152017-02-22 16:32:36 -0800575 public void setManageConferenceActivity(
576 @Nullable ManageConferenceActivity manageConferenceActivity) {
linyuh183cb712017-12-27 17:02:37 -0800577 this.manageConferenceActivity = manageConferenceActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -0800578 }
579
580 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700581 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800582 bringToForeground(showDialpad);
583 }
584
585 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700586 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800587 LatencyReport latencyReport = new LatencyReport(call);
588 if (shouldAttemptBlocking(call)) {
589 maybeBlockCall(call, latencyReport);
590 } else {
591 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800592 externalCallList.onCallAdded(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800593 } else {
594 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800595 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800596 }
597 }
598
599 // Since a call has been added we are no longer waiting for Telecom to send us a call.
600 setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -0800601 call.registerCallback(callCallback);
zachh78e54ac2017-12-05 16:38:35 -0800602 // TODO(maxwelb): Return the future in recordPhoneLookupInfo and propagate.
linyuh183cb712017-12-27 17:02:37 -0800603 PhoneLookupHistoryRecorder.recordPhoneLookupInfo(context.getApplicationContext(), call);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700604 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800605 }
606
607 private boolean shouldAttemptBlocking(android.telecom.Call call) {
608 if (call.getState() != android.telecom.Call.STATE_RINGING) {
609 return false;
610 }
linyuh183cb712017-12-27 17:02:37 -0800611 if (!UserManagerCompat.isUserUnlocked(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700612 LogUtil.i(
613 "InCallPresenter.shouldAttemptBlocking",
614 "not attempting to block incoming call because user is locked");
615 return false;
616 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800617 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700618 LogUtil.i(
619 "InCallPresenter.shouldAttemptBlocking",
620 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800621 return false;
622 }
linyuh183cb712017-12-27 17:02:37 -0800623 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700624 LogUtil.i(
625 "InCallPresenter.shouldAttemptBlocking",
626 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800627 return false;
628 }
629 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
630 return false;
631 }
linyuh183cb712017-12-27 17:02:37 -0800632 if (FilteredNumberCompat.useNewFiltering(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700633 LogUtil.i(
634 "InCallPresenter.shouldAttemptBlocking",
635 "not attempting to block incoming call because framework blocking is in use");
636 return false;
637 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800638 return true;
639 }
640
641 /**
642 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
643 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
644 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
645 * call anyways.
646 */
647 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
linyuh183cb712017-12-27 17:02:37 -0800648 final String countryIso = GeoUtil.getCurrentCountryIso(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800649 final String number = TelecomCallUtil.getNumber(call);
650 final long timeAdded = System.currentTimeMillis();
651
652 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
653 // main UI thread. It is needed so we can change its value within different scopes, since
654 // that cannot be done with a final boolean.
655 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
656
657 final Handler handler = new Handler();
658
659 // Proceed if the query is slow; the call may still be blocked after the query returns.
660 final Runnable runnable =
661 new Runnable() {
662 @Override
663 public void run() {
664 hasTimedOut.set(true);
665 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800666 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800667 }
668 };
669 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
670
671 OnCheckBlockedListener onCheckBlockedListener =
672 new OnCheckBlockedListener() {
673 @Override
674 public void onCheckComplete(final Integer id) {
675 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700676 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800677 return;
678 }
679 if (!hasTimedOut.get()) {
680 handler.removeCallbacks(runnable);
681 }
682 if (id == null) {
683 if (!hasTimedOut.get()) {
684 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800685 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800686 }
687 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700688 LogUtil.d(
689 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800690 if (!hasTimedOut.get()) {
691 handler.removeCallbacks(runnable);
692
693 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800694 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800695 }
696 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700697 LogUtil.i(
698 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800699 call.reject(false, null);
linyuh183cb712017-12-27 17:02:37 -0800700 Logger.get(context).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
Eric Erfanianccca3152017-02-22 16:32:36 -0800701
702 /*
703 * If mContext is null, then the InCallPresenter was torn down before the
704 * block check had a chance to complete. The context is no longer valid, so
705 * don't attempt to remove the call log entry.
706 */
linyuh183cb712017-12-27 17:02:37 -0800707 if (context == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800708 return;
709 }
710 // Register observer to update the call log.
711 // BlockedNumberContentObserver will unregister after successful log or timeout.
712 BlockedNumberContentObserver contentObserver =
linyuh183cb712017-12-27 17:02:37 -0800713 new BlockedNumberContentObserver(context, new Handler(), number, timeAdded);
Eric Erfanianccca3152017-02-22 16:32:36 -0800714 contentObserver.register();
715 }
716 }
717 };
718
linyuh183cb712017-12-27 17:02:37 -0800719 filteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800720 }
721
722 public void onCallRemoved(android.telecom.Call call) {
723 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800724 externalCallList.onCallRemoved(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800725 } else {
linyuh183cb712017-12-27 17:02:37 -0800726 callList.onCallRemoved(context, call);
727 call.unregisterCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800728 }
729 }
730
731 public void onCanAddCallChanged(boolean canAddCall) {
linyuh183cb712017-12-27 17:02:37 -0800732 for (CanAddCallListener listener : canAddCallListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800733 listener.onCanAddCallChanged(canAddCall);
734 }
735 }
736
737 @Override
738 public void onWiFiToLteHandover(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -0700739 if (call.hasShownWiFiToLteHandoverToast()) {
740 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800741 }
yueg32d9ae72018-04-05 17:35:56 -0700742
743 Toast.makeText(context, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG)
744 .show();
745 call.setHasShownWiFiToLteHandoverToast();
Eric Erfanianccca3152017-02-22 16:32:36 -0800746 }
747
748 @Override
749 public void onHandoverToWifiFailed(DialerCall call) {
linyuh183cb712017-12-27 17:02:37 -0800750 if (inCallActivity != null) {
751 inCallActivity.showDialogOrToastForWifiHandoverFailure(call);
yueg32d9ae72018-04-05 17:35:56 -0700752 } else {
753 Toast.makeText(context, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT)
754 .show();
Eric Erfanianccca3152017-02-22 16:32:36 -0800755 }
756 }
757
Eric Erfanianc857f902017-05-15 14:05:33 -0700758 @Override
759 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
760 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
linyuh183cb712017-12-27 17:02:37 -0800761 if (inCallActivity != null) {
762 inCallActivity.showDialogForInternationalCallOnWifi(call);
Eric Erfanianc857f902017-05-15 14:05:33 -0700763 }
764 }
765
Eric Erfanianccca3152017-02-22 16:32:36 -0800766 /**
767 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
768 * app based on the information it gets from CallList. Dispatches the in-call state to all
769 * listeners. Can trigger the creation or destruction of the UI based on the states that is
770 * calculates.
771 */
772 @Override
773 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700774 Trace.beginSection("InCallPresenter.onCallListChange");
linyuh183cb712017-12-27 17:02:37 -0800775 if (inCallActivity != null && inCallActivity.isInCallScreenAnimating()) {
776 awaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700777 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800778 return;
779 }
780 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700781 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800782 return;
783 }
784
linyuh183cb712017-12-27 17:02:37 -0800785 awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800786
787 InCallState newState = getPotentialStateFromCallList(callList);
linyuh183cb712017-12-27 17:02:37 -0800788 InCallState oldState = inCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700789 LogUtil.d(
790 "InCallPresenter.onCallListChange",
791 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700792
793 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
794 // incall activity for that call will still exist (even if it's not visible). In the case of
795 // an incoming call in that situation, just disconnect that "waiting for account" call and
796 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
Eric Erfanian938468d2017-10-24 14:05:52 -0700797 // a bug for more details.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700798 DialerCall waitingForAccountCall;
799 if (newState == InCallState.INCOMING
800 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
801 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700802 // The InCallActivity might be destroyed or not started yet at this point.
803 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800804 inCallActivity.dismissPendingDialogs();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700805 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700806 }
807
Eric Erfanianccca3152017-02-22 16:32:36 -0800808 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700809 LogUtil.d(
810 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800811
812 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700813 LogUtil.i(
814 "InCallPresenter.onCallListChange",
815 "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800816 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800817
yuegb9103042018-03-30 12:12:25 -0700818 // Foreground call changed
819 DialerCall primary = null;
820 if (newState == InCallState.INCOMING) {
821 primary = callList.getIncomingCall();
822 } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
823 primary = callList.getOutgoingCall();
824 if (primary == null) {
825 primary = callList.getPendingOutgoingCall();
826 }
827 } else if (newState == InCallState.INCALL) {
828 primary = getCallToDisplay(callList, null, false);
829 }
830 if (primary != null) {
831 onForegroundCallChanged(primary);
832 }
833
Eric Erfanianccca3152017-02-22 16:32:36 -0800834 // notify listeners of new state
linyuh183cb712017-12-27 17:02:37 -0800835 for (InCallStateListener listener : listeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700836 LogUtil.d(
837 "InCallPresenter.onCallListChange",
linyuh183cb712017-12-27 17:02:37 -0800838 "Notify " + listener + " of state " + inCallState.toString());
839 listener.onStateChange(oldState, inCallState, callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800840 }
841
842 if (isActivityStarted()) {
843 final boolean hasCall =
844 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
linyuh183cb712017-12-27 17:02:37 -0800845 inCallActivity.dismissKeyguard(hasCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800846 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700847 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800848 }
849
yuegb9103042018-03-30 12:12:25 -0700850 /**
851 * Get the highest priority call to display. Goes through the calls and chooses which to return
852 * based on priority of which type of call to display to the user. Callers can use the "ignore"
853 * feature to get the second best call by passing a previously found primary call as ignore.
854 *
855 * @param ignore A call to ignore if found.
856 */
857 static DialerCall getCallToDisplay(
858 CallList callList, DialerCall ignore, boolean skipDisconnected) {
859 // Active calls come second. An active call always gets precedent.
860 DialerCall retval = callList.getActiveCall();
861 if (retval != null && retval != ignore) {
862 return retval;
863 }
864
865 // Sometimes there is intemediate state that two calls are in active even one is about
866 // to be on hold.
867 retval = callList.getSecondActiveCall();
868 if (retval != null && retval != ignore) {
869 return retval;
870 }
871
872 // Disconnected calls get primary position if there are no active calls
873 // to let user know quickly what call has disconnected. Disconnected
874 // calls are very short lived.
875 if (!skipDisconnected) {
876 retval = callList.getDisconnectingCall();
877 if (retval != null && retval != ignore) {
878 return retval;
879 }
880 retval = callList.getDisconnectedCall();
881 if (retval != null && retval != ignore) {
882 return retval;
883 }
884 }
885
886 // Then we go to background call (calls on hold)
887 retval = callList.getBackgroundCall();
888 if (retval != null && retval != ignore) {
889 return retval;
890 }
891
892 // Lastly, we go to a second background call.
893 retval = callList.getSecondBackgroundCall();
894
895 return retval;
896 }
897
Eric Erfanianccca3152017-02-22 16:32:36 -0800898 /** Called when there is a new incoming call. */
899 @Override
900 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700901 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800902 InCallState newState = startOrFinishUi(InCallState.INCOMING);
linyuh183cb712017-12-27 17:02:37 -0800903 InCallState oldState = inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800904
Eric Erfanian2ca43182017-08-31 06:57:16 -0700905 LogUtil.i(
906 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800907 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800908
wangqicf61ca02017-08-31 15:32:55 -0700909 Trace.beginSection("listener.onIncomingCall");
linyuh183cb712017-12-27 17:02:37 -0800910 for (IncomingCallListener listener : incomingCallListeners) {
911 listener.onIncomingCall(oldState, inCallState, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800912 }
wangqicf61ca02017-08-31 15:32:55 -0700913 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800914
wangqicf61ca02017-08-31 15:32:55 -0700915 Trace.beginSection("onPrimaryCallStateChanged");
linyuh183cb712017-12-27 17:02:37 -0800916 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800917 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800918 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800919 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700920 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700921 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800922 }
923
924 @Override
925 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700926 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
linyuh183cb712017-12-27 17:02:37 -0800927 && inCallState == InCallPresenter.InCallState.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800928 LogUtil.i(
929 "InCallPresenter.onUpgradeToVideo",
930 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700931 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800932 }
933
linyuh183cb712017-12-27 17:02:37 -0800934 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800935 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800936 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800937 }
938 }
939
940 @Override
wangqibc28ea72018-04-02 16:23:00 -0700941 public void onUpgradeToRtt(DialerCall call, int rttRequestId) {
942 if (inCallActivity != null) {
943 inCallActivity.showDialogForRttRequest(call, rttRequestId);
944 }
945 }
946
947 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700948 public void onSessionModificationStateChange(DialerCall call) {
949 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800950 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
linyuh183cb712017-12-27 17:02:37 -0800951 if (proximitySensor == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800952 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
953 return;
954 }
linyuh183cb712017-12-27 17:02:37 -0800955 proximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700956 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
linyuh183cb712017-12-27 17:02:37 -0800957 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800958 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800959 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800960 }
961 }
962
963 /**
964 * Called when a call becomes disconnected. Called everytime an existing call changes from being
965 * connected (incoming/outgoing/active) to disconnected.
966 */
967 @Override
968 public void onDisconnect(DialerCall call) {
linyuh7b86f562017-11-16 11:24:09 -0800969 showDialogOrToastForDisconnectedCall(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800970
971 // We need to do the run the same code as onCallListChange.
linyuh183cb712017-12-27 17:02:37 -0800972 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800973
974 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800975 inCallActivity.dismissKeyguard(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800976 }
977
978 if (call.isEmergencyCall()) {
linyuh183cb712017-12-27 17:02:37 -0800979 FilteredNumbersUtil.recordLastEmergencyCallTime(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800980 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800981
linyuh183cb712017-12-27 17:02:37 -0800982 if (!callList.hasLiveCall()
Eric Erfanianfc37b022017-03-21 10:11:17 -0700983 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700984 && !isSecretCode(call.getNumber())
wangqi9982f0d2017-10-11 17:46:07 -0700985 && !call.isVoiceMailNumber()) {
linyuh183cb712017-12-27 17:02:37 -0800986 PostCall.onCallDisconnected(context, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -0800987 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800988 }
989
Eric Erfanian10b34a52017-05-04 08:23:17 -0700990 private boolean isSecretCode(@Nullable String number) {
991 return number != null
992 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
993 }
994
Eric Erfanianccca3152017-02-22 16:32:36 -0800995 /** Given the call list, return the state in which the in-call screen should be. */
996 public InCallState getPotentialStateFromCallList(CallList callList) {
997
998 InCallState newState = InCallState.NO_CALLS;
999
1000 if (callList == null) {
1001 return newState;
1002 }
1003 if (callList.getIncomingCall() != null) {
1004 newState = InCallState.INCOMING;
1005 } else if (callList.getWaitingForAccountCall() != null) {
1006 newState = InCallState.WAITING_FOR_ACCOUNT;
1007 } else if (callList.getPendingOutgoingCall() != null) {
1008 newState = InCallState.PENDING_OUTGOING;
1009 } else if (callList.getOutgoingCall() != null) {
1010 newState = InCallState.OUTGOING;
1011 } else if (callList.getActiveCall() != null
1012 || callList.getBackgroundCall() != null
1013 || callList.getDisconnectedCall() != null
1014 || callList.getDisconnectingCall() != null) {
1015 newState = InCallState.INCALL;
1016 }
1017
1018 if (newState == InCallState.NO_CALLS) {
linyuh183cb712017-12-27 17:02:37 -08001019 if (boundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001020 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001021 }
1022 }
1023
1024 return newState;
1025 }
1026
1027 public boolean isBoundAndWaitingForOutgoingCall() {
linyuh183cb712017-12-27 17:02:37 -08001028 return boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -08001029 }
1030
1031 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001032 LogUtil.i(
1033 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
1034 "setBoundAndWaitingForOutgoingCall: " + isBound);
linyuh183cb712017-12-27 17:02:37 -08001035 boundAndWaitingForOutgoingCall = isBound;
1036 themeColorManager.setPendingPhoneAccountHandle(handle);
1037 if (isBound && inCallState == InCallState.NO_CALLS) {
1038 inCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001039 }
1040 }
1041
1042 public void onShrinkAnimationComplete() {
linyuh183cb712017-12-27 17:02:37 -08001043 if (awaitingCallListUpdate) {
1044 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001045 }
1046 }
1047
1048 public void addIncomingCallListener(IncomingCallListener listener) {
1049 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001050 incomingCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001051 }
1052
1053 public void removeIncomingCallListener(IncomingCallListener listener) {
1054 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001055 incomingCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001056 }
1057 }
1058
1059 public void addListener(InCallStateListener listener) {
1060 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001061 listeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001062 }
1063
1064 public void removeListener(InCallStateListener listener) {
1065 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001066 listeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001067 }
1068 }
1069
1070 public void addDetailsListener(InCallDetailsListener listener) {
1071 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001072 detailsListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001073 }
1074
1075 public void removeDetailsListener(InCallDetailsListener listener) {
1076 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001077 detailsListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001078 }
1079 }
1080
1081 public void addCanAddCallListener(CanAddCallListener listener) {
1082 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001083 canAddCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001084 }
1085
1086 public void removeCanAddCallListener(CanAddCallListener listener) {
1087 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001088 canAddCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001089 }
1090 }
1091
1092 public void addOrientationListener(InCallOrientationListener listener) {
1093 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001094 orientationListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001095 }
1096
1097 public void removeOrientationListener(InCallOrientationListener listener) {
1098 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001099 orientationListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001100 }
1101 }
1102
1103 public void addInCallEventListener(InCallEventListener listener) {
1104 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001105 inCallEventListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001106 }
1107
1108 public void removeInCallEventListener(InCallEventListener listener) {
1109 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001110 inCallEventListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001111 }
1112 }
1113
1114 public ProximitySensor getProximitySensor() {
linyuh183cb712017-12-27 17:02:37 -08001115 return proximitySensor;
Eric Erfanianccca3152017-02-22 16:32:36 -08001116 }
1117
1118 public PseudoScreenState getPseudoScreenState() {
linyuh183cb712017-12-27 17:02:37 -08001119 return pseudoScreenState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001120 }
1121
1122 /** Returns true if the incall app is the foreground application. */
1123 public boolean isShowingInCallUi() {
1124 if (!isActivityStarted()) {
1125 return false;
1126 }
linyuh183cb712017-12-27 17:02:37 -08001127 if (manageConferenceActivity != null && manageConferenceActivity.isVisible()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001128 return true;
1129 }
linyuh183cb712017-12-27 17:02:37 -08001130 return inCallActivity.isVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001131 }
1132
1133 /**
1134 * Returns true if the activity has been created and is running. Returns true as long as activity
1135 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
1136 * (not in foreground).
1137 */
1138 public boolean isActivityStarted() {
linyuh183cb712017-12-27 17:02:37 -08001139 return (inCallActivity != null
1140 && !inCallActivity.isDestroyed()
1141 && !inCallActivity.isFinishing());
Eric Erfanianccca3152017-02-22 16:32:36 -08001142 }
1143
1144 /**
1145 * Determines if the In-Call app is currently changing configuration.
1146 *
1147 * @return {@code true} if the In-Call app is changing configuration.
1148 */
1149 public boolean isChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001150 return isChangingConfigurations;
Eric Erfanianccca3152017-02-22 16:32:36 -08001151 }
1152
1153 /**
1154 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1155 * screen orientation).
1156 */
1157 /*package*/
1158 void updateIsChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001159 isChangingConfigurations = false;
1160 if (inCallActivity != null) {
1161 isChangingConfigurations = inCallActivity.isChangingConfigurations();
Eric Erfanianccca3152017-02-22 16:32:36 -08001162 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001163 LogUtil.v(
1164 "InCallPresenter.updateIsChangingConfigurations",
linyuh183cb712017-12-27 17:02:37 -08001165 "updateIsChangingConfigurations = " + isChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001166 }
1167
yueg10f6e822018-01-17 15:32:18 -08001168 /** Called when the activity goes in/out of the foreground. */
1169 public void onUiShowing(boolean showing) {
linyuh183cb712017-12-27 17:02:37 -08001170 if (proximitySensor != null) {
1171 proximitySensor.onInCallShowing(showing);
Eric Erfanianccca3152017-02-22 16:32:36 -08001172 }
1173
yueg092b21c2017-11-15 16:20:07 -08001174 if (!showing) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001175 updateIsChangingConfigurations();
1176 }
1177
linyuh183cb712017-12-27 17:02:37 -08001178 for (InCallUiListener listener : inCallUiListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001179 listener.onUiShowing(showing);
1180 }
1181
linyuh183cb712017-12-27 17:02:37 -08001182 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001183 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001184 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001185 }
1186 }
1187
Eric Erfanian2ca43182017-08-31 06:57:16 -07001188 public void refreshUi() {
linyuh183cb712017-12-27 17:02:37 -08001189 if (inCallActivity != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001190 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001191 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001192 }
1193 }
1194
Eric Erfanianccca3152017-02-22 16:32:36 -08001195 public void addInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001196 inCallUiListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001197 }
1198
1199 public boolean removeInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001200 return inCallUiListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001201 }
1202
1203 /*package*/
1204 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001205 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001206 notifyVideoPauseController(true);
Eric Erfanian2ca43182017-08-31 06:57:16 -07001207 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001208 }
1209
1210 /*package*/
1211 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001212 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001213 notifyVideoPauseController(false);
1214 }
1215
1216 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001217 LogUtil.d(
1218 "InCallPresenter.notifyVideoPauseController",
linyuh183cb712017-12-27 17:02:37 -08001219 "mIsChangingConfigurations=" + isChangingConfigurations);
1220 if (!isChangingConfigurations) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001221 VideoPauseController.getInstance().onUiShowing(showing);
1222 }
1223 }
1224
1225 /** Brings the app into the foreground if possible. */
1226 public void bringToForeground(boolean showDialpad) {
1227 // Before we bring the incall UI to the foreground, we check to see if:
1228 // 1. It is not currently in the foreground
1229 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1230 // be displayed)
1231 // If the activity hadn't actually been started previously, yet there are still calls
1232 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1233 // bring it up the UI regardless.
linyuh183cb712017-12-27 17:02:37 -08001234 if (!isShowingInCallUi() && inCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001235 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001236 }
1237 }
1238
1239 public void onPostDialCharWait(String callId, String chars) {
1240 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001241 inCallActivity.showDialogForPostCharWait(callId, chars);
Eric Erfanianccca3152017-02-22 16:32:36 -08001242 }
1243 }
1244
1245 /**
1246 * Handles the green CALL key while in-call.
1247 *
1248 * @return true if we consumed the event.
1249 */
1250 public boolean handleCallKey() {
1251 LogUtil.v("InCallPresenter.handleCallKey", null);
1252
1253 // The green CALL button means either "Answer", "Unhold", or
1254 // "Swap calls", or can be a no-op, depending on the current state
1255 // of the Phone.
1256
1257 /** INCOMING CALL */
linyuh183cb712017-12-27 17:02:37 -08001258 final CallList calls = callList;
Eric Erfanianccca3152017-02-22 16:32:36 -08001259 final DialerCall incomingCall = calls.getIncomingCall();
1260 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1261
1262 // (1) Attempt to answer a call
1263 if (incomingCall != null) {
1264 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1265 return true;
1266 }
1267
1268 /** STATE_ACTIVE CALL */
1269 final DialerCall activeCall = calls.getActiveCall();
1270 if (activeCall != null) {
1271 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1272 // consolidate this logic.
1273 final boolean canMerge =
1274 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1275 final boolean canSwap =
1276 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1277
Eric Erfanian2ca43182017-08-31 06:57:16 -07001278 LogUtil.v(
1279 "InCallPresenter.handleCallKey",
1280 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001281
1282 // (2) Attempt actions on conference calls
1283 if (canMerge) {
1284 TelecomAdapter.getInstance().merge(activeCall.getId());
1285 return true;
1286 } else if (canSwap) {
1287 TelecomAdapter.getInstance().swap(activeCall.getId());
1288 return true;
1289 }
1290 }
1291
1292 /** BACKGROUND CALL */
1293 final DialerCall heldCall = calls.getBackgroundCall();
1294 if (heldCall != null) {
1295 // We have a hold call so presumeable it will always support HOLD...but
1296 // there is no harm in double checking.
1297 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1298
Eric Erfanian2ca43182017-08-31 06:57:16 -07001299 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001300
1301 // (4) unhold call
1302 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1303 heldCall.unhold();
1304 return true;
1305 }
1306 }
1307
1308 // Always consume hard keys
1309 return true;
1310 }
1311
Eric Erfanianccca3152017-02-22 16:32:36 -08001312 /** Clears the previous fullscreen state. */
1313 public void clearFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001314 isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001315 }
1316
1317 /**
1318 * Changes the fullscreen mode of the in-call UI.
1319 *
1320 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1321 * otherwise.
1322 */
1323 public void setFullScreen(boolean isFullScreen) {
1324 setFullScreen(isFullScreen, false /* force */);
1325 }
1326
1327 /**
1328 * Changes the fullscreen mode of the in-call UI.
1329 *
1330 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1331 * otherwise.
1332 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1333 */
1334 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001335 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001336
1337 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1338 if (isDialpadVisible()) {
1339 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001340 LogUtil.v(
1341 "InCallPresenter.setFullScreen",
1342 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001343 }
1344
linyuh183cb712017-12-27 17:02:37 -08001345 if (this.isFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001346 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001347 return;
1348 }
linyuh183cb712017-12-27 17:02:37 -08001349 this.isFullScreen = isFullScreen;
1350 notifyFullscreenModeChange(this.isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001351 }
1352
1353 /**
1354 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1355 * otherwise.
1356 */
1357 public boolean isFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001358 return isFullScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001359 }
1360
1361 /**
1362 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1363 *
1364 * @param isFullscreenMode {@code True} if entering full screen mode.
1365 */
1366 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001367 for (InCallEventListener listener : inCallEventListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001368 listener.onFullscreenModeChanged(isFullscreenMode);
1369 }
1370 }
1371
linyuh7b86f562017-11-16 11:24:09 -08001372 /** Instruct the in-call activity to show an error dialog or toast for a disconnected call. */
1373 private void showDialogOrToastForDisconnectedCall(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -07001374 if (call.getState() != DialerCall.State.DISCONNECTED) {
linyuh7b86f562017-11-16 11:24:09 -08001375 return;
Eric Erfanianccca3152017-02-22 16:32:36 -08001376 }
linyuh7b86f562017-11-16 11:24:09 -08001377
1378 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1379 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1380 setDisconnectCauseForMissingAccounts(call);
1381 }
1382
yueg32d9ae72018-04-05 17:35:56 -07001383 if (isActivityStarted()) {
1384 inCallActivity.showDialogOrToastForDisconnectedCall(
1385 new DisconnectMessage(inCallActivity, call));
1386 } else {
1387 CharSequence message = new DisconnectMessage(context, call).toastMessage;
1388 if (message != null) {
1389 Toast.makeText(context, message, Toast.LENGTH_LONG).show();
1390 }
1391 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001392 }
1393
1394 /**
1395 * When the state of in-call changes, this is the first method to get called. It determines if the
1396 * UI needs to be started or finished depending on the new state and does it.
1397 */
1398 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001399 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001400 LogUtil.d(
linyuh183cb712017-12-27 17:02:37 -08001401 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + inCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001402
1403 // TODO: Consider a proper state machine implementation
1404
1405 // If the state isn't changing we have already done any starting/stopping of activities in
1406 // a previous pass...so lets cut out early
linyuh183cb712017-12-27 17:02:37 -08001407 if (newState == inCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001408 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001409 return newState;
1410 }
1411
1412 // A new Incoming call means that the user needs to be notified of the the call (since
1413 // it wasn't them who initiated it). We do this through full screen notifications and
1414 // happens indirectly through {@link StatusBarNotifier}.
1415 //
1416 // The process for incoming calls is as follows:
1417 //
1418 // 1) CallList - Announces existence of new INCOMING call
1419 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1420 // - should be set to INCOMING.
1421 // 3) InCallPresenter - This method is called to see if we need to start or finish
1422 // the app given the new state.
1423 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1424 // StatusBarNotifier explicitly to issue a FullScreen Notification
1425 // that will either start the InCallActivity or show the user a
1426 // top-level notification dialog if the user is in an immersive app.
1427 // That notification can also start the InCallActivity.
1428 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1429 // call InCallPresenter::setActivity() to let the presenter
1430 // know that start-up is complete.
1431 //
1432 // [ AND NOW YOU'RE IN THE CALL. voila! ]
Eric Erfanianccca3152017-02-22 16:32:36 -08001433
1434 // A dialog to show on top of the InCallUI to select a PhoneAccount
1435 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1436
1437 // A new outgoing call indicates that the user just now dialed a number and when that
1438 // happens we need to display the screen immediately or show an account picker dialog if
1439 // no default is set. However, if the main InCallUI is already visible, we do not want to
1440 // re-initiate the start-up animation, so we do not need to do anything here.
1441 //
1442 // It is also possible to go into an intermediate state where the call has been initiated
1443 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1444 // This pending outgoing state can also launch the call screen.
1445 //
1446 // This is different from the incoming call sequence because we do not need to shock the
1447 // user with a top-level notification. Just show the call UI normally.
1448 boolean callCardFragmentVisible =
linyuh183cb712017-12-27 17:02:37 -08001449 inCallActivity != null && inCallActivity.getCallCardFragmentVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001450 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1451 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1452
1453 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1454 // outgoing call process, so the UI should be brought up to show an error dialog.
1455 showCallUi |=
linyuh183cb712017-12-27 17:02:37 -08001456 (InCallState.PENDING_OUTGOING == inCallState
Eric Erfanianccca3152017-02-22 16:32:36 -08001457 && InCallState.INCALL == newState
1458 && !isShowingInCallUi());
1459
1460 // Another exception - InCallActivity is in charge of disconnecting a call with no
1461 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1462 // call so that:
1463 // 1) The call can be disconnected correctly
1464 // 2) The UI comes up and correctly displays the error dialog.
1465 // TODO: Remove these special case conditions by making InCallPresenter a true state
1466 // machine. Telecom should also be the component responsible for disconnecting a call
1467 // with no valid accounts.
1468 showCallUi |=
1469 InCallState.PENDING_OUTGOING == newState
1470 && mainUiNotVisible
linyuh183cb712017-12-27 17:02:37 -08001471 && isCallWithNoValidAccounts(callList.getPendingOutgoingCall());
Eric Erfanianccca3152017-02-22 16:32:36 -08001472
1473 // The only time that we have an instance of mInCallActivity and it isn't started is
1474 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1475 // the activity. When it is finally destroyed, we double check if we should bring it back
1476 // up so we aren't going to lose anything by avoiding a second startup here.
linyuh183cb712017-12-27 17:02:37 -08001477 boolean activityIsFinishing = inCallActivity != null && !isActivityStarted();
Eric Erfanianccca3152017-02-22 16:32:36 -08001478 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001479 LogUtil.i(
1480 "InCallPresenter.startOrFinishUi",
linyuh183cb712017-12-27 17:02:37 -08001481 "Undo the state change: " + newState + " -> " + inCallState);
wangqicf61ca02017-08-31 15:32:55 -07001482 Trace.endSection();
linyuh183cb712017-12-27 17:02:37 -08001483 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001484 }
1485
1486 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1487 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1488 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1489 // outgoing call after user ignore it by pressing home button.
1490 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1491 && !showCallUi
1492 && isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001493 inCallActivity.dismissPendingDialogs();
Eric Erfanianccca3152017-02-22 16:32:36 -08001494 }
1495
yueg32d9ae72018-04-05 17:35:56 -07001496 if ((showCallUi || showAccountPicker) && !shouldStartInBubbleMode) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001497 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001498 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001499 } else if (newState == InCallState.NO_CALLS) {
1500 // The new state is the no calls state. Tear everything down.
yueg8e544862018-04-06 14:34:59 -07001501 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001502 attemptFinishActivity();
1503 attemptCleanup();
1504 }
1505
wangqicf61ca02017-08-31 15:32:55 -07001506 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001507 return newState;
1508 }
1509
1510 /**
1511 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1512 * or PhoneAccounts to select from.
1513 */
1514 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1515
1516 Bundle extras = call.getIntentExtras();
1517 // Initialize the extras bundle to avoid NPE
1518 if (extras == null) {
1519 extras = new Bundle();
1520 }
1521
1522 final List<PhoneAccountHandle> phoneAccountHandles =
1523 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1524
1525 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1526 String scheme = call.getHandle().getScheme();
1527 final String errorMsg =
1528 PhoneAccount.SCHEME_TEL.equals(scheme)
linyuh183cb712017-12-27 17:02:37 -08001529 ? context.getString(R.string.callFailed_simError)
1530 : context.getString(R.string.incall_error_supp_service_unknown);
Eric Erfanianccca3152017-02-22 16:32:36 -08001531 DisconnectCause disconnectCause =
1532 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1533 call.setDisconnectCause(disconnectCause);
1534 }
1535 }
1536
Eric Erfanianccca3152017-02-22 16:32:36 -08001537 /**
1538 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1539 * Calling classes should use this as an indication whether to interact with the
1540 * InCallPresenter or not.
1541 */
1542 public boolean isReadyForTearDown() {
linyuh183cb712017-12-27 17:02:37 -08001543 return inCallActivity == null && !serviceConnected && inCallState == InCallState.NO_CALLS;
Eric Erfanianccca3152017-02-22 16:32:36 -08001544 }
1545
1546 /**
1547 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1548 */
1549 private void attemptCleanup() {
1550 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001551 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001552
1553 cleanupSurfaces();
1554
linyuh183cb712017-12-27 17:02:37 -08001555 isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001556
yueg32d9ae72018-04-05 17:35:56 -07001557 shouldStartInBubbleMode = false;
yueg8e544862018-04-06 14:34:59 -07001558 audioRouteSetForBubbleMode = false;
yueg32d9ae72018-04-05 17:35:56 -07001559
Eric Erfanianccca3152017-02-22 16:32:36 -08001560 // blow away stale contact info so that we get fresh data on
1561 // the next set of calls
linyuh183cb712017-12-27 17:02:37 -08001562 if (contactInfoCache != null) {
1563 contactInfoCache.clearCache();
Eric Erfanianccca3152017-02-22 16:32:36 -08001564 }
linyuh183cb712017-12-27 17:02:37 -08001565 contactInfoCache = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001566
linyuh183cb712017-12-27 17:02:37 -08001567 if (proximitySensor != null) {
1568 removeListener(proximitySensor);
1569 proximitySensor.tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -08001570 }
linyuh183cb712017-12-27 17:02:37 -08001571 proximitySensor = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001572
linyuh183cb712017-12-27 17:02:37 -08001573 if (statusBarNotifier != null) {
1574 removeListener(statusBarNotifier);
1575 EnrichedCallComponent.get(context)
Eric Erfaniand8046e52017-04-06 09:41:50 -07001576 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -08001577 .unregisterStateChangedListener(statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001578 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001579
linyuh183cb712017-12-27 17:02:37 -08001580 if (externalCallNotifier != null && externalCallList != null) {
1581 externalCallList.removeExternalCallListener(externalCallNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001582 }
linyuh183cb712017-12-27 17:02:37 -08001583 statusBarNotifier = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001584
linyuh183cb712017-12-27 17:02:37 -08001585 if (callList != null) {
1586 callList.removeListener(this);
1587 callList.removeListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001588 }
linyuh183cb712017-12-27 17:02:37 -08001589 callList = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001590
linyuh183cb712017-12-27 17:02:37 -08001591 context = null;
1592 inCallActivity = null;
1593 manageConferenceActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001594
linyuh183cb712017-12-27 17:02:37 -08001595 listeners.clear();
1596 incomingCallListeners.clear();
1597 detailsListeners.clear();
1598 canAddCallListeners.clear();
1599 orientationListeners.clear();
1600 inCallEventListeners.clear();
1601 inCallUiListeners.clear();
1602 if (!inCallUiLocks.isEmpty()) {
1603 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + inCallUiLocks);
1604 inCallUiLocks.clear();
twyen8efb4952017-10-06 16:35:54 -07001605 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001606 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001607 }
1608 }
1609
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001610 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001611 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
linyuh183cb712017-12-27 17:02:37 -08001612 context.startActivity(
1613 InCallActivity.getIntent(context, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001614 }
1615
1616 public void onServiceBind() {
linyuh183cb712017-12-27 17:02:37 -08001617 serviceBound = true;
Eric Erfanianccca3152017-02-22 16:32:36 -08001618 }
1619
1620 public void onServiceUnbind() {
1621 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -08001622 serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001623 }
1624
1625 public boolean isServiceBound() {
linyuh183cb712017-12-27 17:02:37 -08001626 return serviceBound;
Eric Erfanianccca3152017-02-22 16:32:36 -08001627 }
1628
1629 public void maybeStartRevealAnimation(Intent intent) {
linyuh183cb712017-12-27 17:02:37 -08001630 if (intent == null || inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001631 return;
1632 }
1633 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1634 if (extras == null) {
1635 // Incoming call, just show the in-call UI directly.
1636 return;
1637 }
1638
1639 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1640 // Account selection dialog will show up so don't show the animation.
1641 return;
1642 }
1643
1644 final PhoneAccountHandle accountHandle =
1645 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1646 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001647
yueg32d9ae72018-04-05 17:35:56 -07001648 setBoundAndWaitingForOutgoingCall(true, accountHandle);
1649
1650 if (shouldStartInBubbleMode) {
1651 LogUtil.i("InCallPresenter.maybeStartRevealAnimation", "shouldStartInBubbleMode");
1652 // Show bubble instead of in call UI
1653 return;
1654 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001655
1656 final Intent activityIntent =
linyuh183cb712017-12-27 17:02:37 -08001657 InCallActivity.getIntent(context, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001658 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
linyuh183cb712017-12-27 17:02:37 -08001659 context.startActivity(activityIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001660 }
1661
1662 /**
1663 * Retrieves the current in-call camera manager instance, creating if necessary.
1664 *
1665 * @return The {@link InCallCameraManager}.
1666 */
1667 public InCallCameraManager getInCallCameraManager() {
1668 synchronized (this) {
linyuh183cb712017-12-27 17:02:37 -08001669 if (inCallCameraManager == null) {
1670 inCallCameraManager = new InCallCameraManager(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001671 }
1672
linyuh183cb712017-12-27 17:02:37 -08001673 return inCallCameraManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001674 }
1675 }
1676
1677 /**
1678 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1679 *
1680 * @param orientation The screen orientation of the device (one of: {@link
1681 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1682 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1683 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1684 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1685 */
1686 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001687 LogUtil.d(
1688 "InCallPresenter.onDeviceOrientationChange",
1689 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001690
linyuh183cb712017-12-27 17:02:37 -08001691 if (callList != null) {
1692 callList.notifyCallsOfDeviceRotation(orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001693 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001694 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001695 }
1696
1697 // Notify listeners of device orientation changed.
linyuh183cb712017-12-27 17:02:37 -08001698 for (InCallOrientationListener listener : orientationListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001699 listener.onDeviceOrientationChanged(orientation);
1700 }
1701 }
1702
1703 /**
1704 * Configures the in-call UI activity so it can change orientations or not. Enables the
1705 * orientation event listener if allowOrientationChange is true, disables it if false.
1706 *
1707 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1708 * landscape. {@code false} if the in-call UI should be locked in portrait.
1709 */
1710 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
linyuh183cb712017-12-27 17:02:37 -08001711 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001712 LogUtil.e(
1713 "InCallPresenter.setInCallAllowsOrientationChange",
1714 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001715 return;
1716 }
linyuh183cb712017-12-27 17:02:37 -08001717 inCallActivity.setAllowOrientationChange(allowOrientationChange);
Eric Erfanianccca3152017-02-22 16:32:36 -08001718 }
1719
1720 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001721 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
linyuh183cb712017-12-27 17:02:37 -08001722 screenTimeoutEnabled = enable;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001723 applyScreenTimeout();
1724 }
1725
1726 private void applyScreenTimeout() {
linyuh183cb712017-12-27 17:02:37 -08001727 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001728 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001729 return;
1730 }
1731
linyuh183cb712017-12-27 17:02:37 -08001732 final Window window = inCallActivity.getWindow();
1733 if (screenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001734 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1735 } else {
1736 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1737 }
1738 }
1739
1740 /**
1741 * Hides or shows the conference manager fragment.
1742 *
1743 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1744 * be hidden.
1745 */
1746 public void showConferenceCallManager(boolean show) {
linyuh183cb712017-12-27 17:02:37 -08001747 if (inCallActivity != null) {
1748 inCallActivity.showConferenceFragment(show);
Eric Erfanianccca3152017-02-22 16:32:36 -08001749 }
linyuh183cb712017-12-27 17:02:37 -08001750 if (!show && manageConferenceActivity != null) {
1751 manageConferenceActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -08001752 }
1753 }
1754
1755 /**
1756 * Determines if the dialpad is visible.
1757 *
1758 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1759 */
1760 public boolean isDialpadVisible() {
linyuh183cb712017-12-27 17:02:37 -08001761 if (inCallActivity == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001762 return false;
1763 }
linyuh183cb712017-12-27 17:02:37 -08001764 return inCallActivity.isDialpadVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001765 }
1766
1767 public ThemeColorManager getThemeColorManager() {
linyuh183cb712017-12-27 17:02:37 -08001768 return themeColorManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001769 }
1770
wangqi8d662ca2017-10-26 11:27:19 -07001771 @VisibleForTesting
1772 public void setThemeColorManager(ThemeColorManager themeColorManager) {
linyuh183cb712017-12-27 17:02:37 -08001773 this.themeColorManager = themeColorManager;
wangqi8d662ca2017-10-26 11:27:19 -07001774 }
1775
Eric Erfanianccca3152017-02-22 16:32:36 -08001776 /** Called when the foreground call changes. */
1777 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh183cb712017-12-27 17:02:37 -08001778 themeColorManager.onForegroundCallChanged(context, newForegroundCall);
1779 if (inCallActivity != null) {
1780 inCallActivity.onForegroundCallChanged(newForegroundCall);
Eric Erfanianccca3152017-02-22 16:32:36 -08001781 }
1782 }
1783
1784 public InCallActivity getActivity() {
linyuh183cb712017-12-27 17:02:37 -08001785 return inCallActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -08001786 }
1787
1788 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1789 public void setActivity(InCallActivity inCallActivity) {
1790 if (inCallActivity == null) {
1791 throw new IllegalArgumentException("registerActivity cannot be called with null");
1792 }
linyuh183cb712017-12-27 17:02:37 -08001793 if (this.inCallActivity != null && this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001794 LogUtil.w(
1795 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001796 }
1797 updateActivity(inCallActivity);
1798 }
1799
1800 ExternalCallNotifier getExternalCallNotifier() {
linyuh183cb712017-12-27 17:02:37 -08001801 return externalCallNotifier;
Eric Erfanianccca3152017-02-22 16:32:36 -08001802 }
1803
1804 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001805 if (localVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001806 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001807 if (context != null) {
1808 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001809 }
linyuh183cb712017-12-27 17:02:37 -08001810 localVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001811 }
linyuh183cb712017-12-27 17:02:37 -08001812 return localVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001813 }
1814
1815 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001816 if (remoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001817 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001818 if (context != null) {
1819 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001820 }
linyuh183cb712017-12-27 17:02:37 -08001821 remoteVideoSurfaceTexture = VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001822 }
linyuh183cb712017-12-27 17:02:37 -08001823 return remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001824 }
1825
1826 void cleanupSurfaces() {
linyuh183cb712017-12-27 17:02:37 -08001827 if (remoteVideoSurfaceTexture != null) {
1828 remoteVideoSurfaceTexture.setDoneWithSurface();
1829 remoteVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001830 }
linyuh183cb712017-12-27 17:02:37 -08001831 if (localVideoSurfaceTexture != null) {
1832 localVideoSurfaceTexture.setDoneWithSurface();
1833 localVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001834 }
1835 }
1836
yueg77cb8e52017-10-27 16:42:51 -07001837 @Override
1838 public void onAudioStateChanged(CallAudioState audioState) {
yueg8e544862018-04-06 14:34:59 -07001839 // Set sensible audio route for bubble mode when we get real audio state for the first time
1840 // During the first time this function is called, supportedRouteMask is set to
1841 // SUPPORTED_AUDIO_ROUTE_ALL, but it's OK since shouldStartInBubbleMode is not set at that time.
1842 if (!audioRouteSetForBubbleMode && shouldStartInBubbleMode) {
1843 setAudioRouteForBubbleMode(audioState);
1844 audioRouteSetForBubbleMode = true;
1845 }
1846
linyuh183cb712017-12-27 17:02:37 -08001847 if (statusBarNotifier != null) {
1848 statusBarNotifier.updateNotification();
yueg77cb8e52017-10-27 16:42:51 -07001849 }
1850 }
1851
yueg8e544862018-04-06 14:34:59 -07001852 /**
1853 * Set audio route to make audio sensible. According to availability, set audio route to Bluetooth
1854 * or wired headset or speaker.
1855 */
1856 private void setAudioRouteForBubbleMode(CallAudioState audioState) {
1857 if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
1858 == CallAudioState.ROUTE_BLUETOOTH) {
1859 // Use Bluetooth if available
1860 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
1861 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "bluetooth");
1862 } else if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_WIRED_HEADSET)
1863 == CallAudioState.ROUTE_WIRED_HEADSET) {
1864 // Use wired headset if available
1865 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_WIRED_HEADSET);
1866 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "wired headset");
1867 } else {
1868 // Use speaker
1869 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_SPEAKER);
1870 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "speaker");
1871 }
1872 }
1873
Eric Erfanianccca3152017-02-22 16:32:36 -08001874 /** All the main states of InCallActivity. */
1875 public enum InCallState {
1876 // InCall Screen is off and there are no calls
1877 NO_CALLS,
1878
1879 // Incoming-call screen is up
1880 INCOMING,
1881
1882 // In-call experience is showing
1883 INCALL,
1884
1885 // Waiting for user input before placing outgoing call
1886 WAITING_FOR_ACCOUNT,
1887
1888 // UI is starting up but no call has been initiated yet.
1889 // The UI is waiting for Telecom to respond.
1890 PENDING_OUTGOING,
1891
1892 // User is dialing out
1893 OUTGOING;
1894
1895 public boolean isIncoming() {
1896 return (this == INCOMING);
1897 }
1898
1899 public boolean isConnectingOrConnected() {
1900 return (this == INCOMING || this == OUTGOING || this == INCALL);
1901 }
1902 }
1903
1904 /** Interface implemented by classes that need to know about the InCall State. */
1905 public interface InCallStateListener {
1906
1907 // TODO: Enhance state to contain the call objects instead of passing CallList
1908 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1909 }
1910
1911 public interface IncomingCallListener {
1912
1913 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1914 }
1915
1916 public interface CanAddCallListener {
1917
1918 void onCanAddCallChanged(boolean canAddCall);
1919 }
1920
1921 public interface InCallDetailsListener {
1922
1923 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1924 }
1925
1926 public interface InCallOrientationListener {
1927
1928 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1929 }
1930
1931 /**
1932 * Interface implemented by classes that need to know about events which occur within the In-Call
1933 * UI. Used as a means of communicating between fragments that make up the UI.
1934 */
1935 public interface InCallEventListener {
1936
1937 void onFullscreenModeChanged(boolean isFullscreenMode);
1938 }
1939
1940 public interface InCallUiListener {
1941
1942 void onUiShowing(boolean showing);
1943 }
twyen8efb4952017-10-06 16:35:54 -07001944
1945 private class InCallUiLockImpl implements InCallUiLock {
1946 private final String tag;
1947
1948 private InCallUiLockImpl(String tag) {
1949 this.tag = tag;
1950 }
1951
1952 @MainThread
1953 @Override
1954 public void release() {
1955 Assert.isMainThread();
1956 releaseInCallUiLock(InCallUiLockImpl.this);
1957 }
1958
1959 @Override
1960 public String toString() {
1961 return "InCallUiLock[" + tag + "]";
1962 }
1963 }
1964
1965 @MainThread
1966 public InCallUiLock acquireInCallUiLock(String tag) {
1967 Assert.isMainThread();
1968 InCallUiLock lock = new InCallUiLockImpl(tag);
linyuh183cb712017-12-27 17:02:37 -08001969 inCallUiLocks.add(lock);
twyen8efb4952017-10-06 16:35:54 -07001970 return lock;
1971 }
1972
1973 @MainThread
1974 private void releaseInCallUiLock(InCallUiLock lock) {
1975 Assert.isMainThread();
1976 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
linyuh183cb712017-12-27 17:02:37 -08001977 inCallUiLocks.remove(lock);
1978 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07001979 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
linyuh183cb712017-12-27 17:02:37 -08001980 if (inCallState == InCallState.NO_CALLS) {
twyen8efb4952017-10-06 16:35:54 -07001981 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
1982 attemptFinishActivity();
1983 attemptCleanup();
1984 }
1985 }
1986 }
1987
1988 @MainThread
1989 public boolean isInCallUiLocked() {
1990 Assert.isMainThread();
linyuh183cb712017-12-27 17:02:37 -08001991 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07001992 return false;
1993 }
linyuh183cb712017-12-27 17:02:37 -08001994 for (InCallUiLock lock : inCallUiLocks) {
twyen8efb4952017-10-06 16:35:54 -07001995 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
1996 }
1997 return true;
1998 }
1999
linyuh183cb712017-12-27 17:02:37 -08002000 private final Set<InCallUiLock> inCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08002001}