blob: 7ea2fadf33fd24ab2f021cc67ff0d46530aeb91d [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
yueg7f5acbe2018-01-10 13:50:29 -0800269 private MotorolaInCallUiNotifier motorolaInCallUiNotifier;
270
erfaniand05d8992018-03-20 19:42:26 -0700271 private SpeakEasyCallManager speakEasyCallManager;
272
yueg32d9ae72018-04-05 17:35:56 -0700273 private boolean shouldStartInBubbleMode;
yueg8e544862018-04-06 14:34:59 -0700274 private boolean audioRouteSetForBubbleMode;
yueg32d9ae72018-04-05 17:35:56 -0700275
Eric Erfanian10b34a52017-05-04 08:23:17 -0700276 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800277 @VisibleForTesting
278 InCallPresenter() {}
279
280 public static synchronized InCallPresenter getInstance() {
linyuh183cb712017-12-27 17:02:37 -0800281 if (inCallPresenter == null) {
wangqic8cf79e2017-10-17 09:21:00 -0700282 Trace.beginSection("InCallPresenter.Constructor");
linyuh183cb712017-12-27 17:02:37 -0800283 inCallPresenter = new InCallPresenter();
wangqic8cf79e2017-10-17 09:21:00 -0700284 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800285 }
linyuh183cb712017-12-27 17:02:37 -0800286 return inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -0800287 }
288
Eric Erfanian10b34a52017-05-04 08:23:17 -0700289 @VisibleForTesting
290 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
linyuh183cb712017-12-27 17:02:37 -0800291 InCallPresenter.inCallPresenter = inCallPresenter;
Eric Erfanian10b34a52017-05-04 08:23:17 -0700292 }
293
Eric Erfanianccca3152017-02-22 16:32:36 -0800294 /**
295 * Determines whether or not a call has no valid phone accounts that can be used to make the call
296 * with. Emergency calls do not require a phone account.
297 *
298 * @param call to check accounts for.
299 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
300 * call contains a phone account that could be used to initiate it with, or is an emergency
301 * call.
302 */
303 public static boolean isCallWithNoValidAccounts(DialerCall call) {
304 if (call != null && !call.isEmergencyCall()) {
305 Bundle extras = call.getIntentExtras();
306
307 if (extras == null) {
308 extras = EMPTY_EXTRAS;
309 }
310
311 final List<PhoneAccountHandle> phoneAccountHandles =
312 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
313
314 if ((call.getAccountHandle() == null
315 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700316 LogUtil.i(
317 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800318 return true;
319 }
320 }
321 return false;
322 }
323
324 public InCallState getInCallState() {
linyuh183cb712017-12-27 17:02:37 -0800325 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800326 }
327
328 public CallList getCallList() {
linyuh183cb712017-12-27 17:02:37 -0800329 return callList;
Eric Erfanianccca3152017-02-22 16:32:36 -0800330 }
331
332 public void setUp(
333 @NonNull Context context,
334 CallList callList,
335 ExternalCallList externalCallList,
336 StatusBarNotifier statusBarNotifier,
337 ExternalCallNotifier externalCallNotifier,
338 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700339 ProximitySensor proximitySensor,
erfaniand05d8992018-03-20 19:42:26 -0700340 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler,
yueg32d9ae72018-04-05 17:35:56 -0700341 @NonNull SpeakEasyCallManager speakEasyCallManager,
342 Intent intent) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700343 Trace.beginSection("InCallPresenter.setUp");
linyuh183cb712017-12-27 17:02:37 -0800344 if (serviceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700345 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
linyuh183cb712017-12-27 17:02:37 -0800346 if (context != this.context || callList != this.callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800347 throw new IllegalStateException();
348 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700349 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800350 return;
351 }
352
353 Objects.requireNonNull(context);
linyuh183cb712017-12-27 17:02:37 -0800354 this.context = context;
Eric Erfanianccca3152017-02-22 16:32:36 -0800355
linyuh183cb712017-12-27 17:02:37 -0800356 this.contactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800357
linyuh183cb712017-12-27 17:02:37 -0800358 this.statusBarNotifier = statusBarNotifier;
359 this.externalCallNotifier = externalCallNotifier;
360 addListener(this.statusBarNotifier);
361 EnrichedCallComponent.get(this.context)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700362 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -0800363 .registerStateChangedListener(this.statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800364
linyuh183cb712017-12-27 17:02:37 -0800365 this.proximitySensor = proximitySensor;
366 addListener(this.proximitySensor);
Eric Erfanianccca3152017-02-22 16:32:36 -0800367
linyuh183cb712017-12-27 17:02:37 -0800368 if (themeColorManager == null) {
369 themeColorManager =
370 new ThemeColorManager(new InCallUIMaterialColorMapUtils(this.context.getResources()));
wangqi8d662ca2017-10-26 11:27:19 -0700371 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800372
linyuh183cb712017-12-27 17:02:37 -0800373 this.callList = callList;
374 this.externalCallList = externalCallList;
375 externalCallList.addExternalCallListener(this.externalCallNotifier);
376 externalCallList.addExternalCallListener(externalCallListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800377
378 // This only gets called by the service so this is okay.
linyuh183cb712017-12-27 17:02:37 -0800379 serviceConnected = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800380
381 // The final thing we do in this set up is add ourselves as a listener to CallList. This
382 // will kick off an update and the whole process can start.
linyuh183cb712017-12-27 17:02:37 -0800383 this.callList.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800384
385 // Create spam call list listener and add it to the list of listeners
linyuh183cb712017-12-27 17:02:37 -0800386 spamCallListListener =
zachh6a4cebd2017-10-24 17:10:06 -0700387 new SpamCallListListener(
388 context, DialerExecutorComponent.get(context).dialerExecutorFactory());
linyuh183cb712017-12-27 17:02:37 -0800389 this.callList.addListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800390
391 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800392
linyuh183cb712017-12-27 17:02:37 -0800393 filteredQueryHandler = filteredNumberQueryHandler;
erfaniand05d8992018-03-20 19:42:26 -0700394 this.speakEasyCallManager = speakEasyCallManager;
linyuh183cb712017-12-27 17:02:37 -0800395 this.context
Eric Erfanianccca3152017-02-22 16:32:36 -0800396 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800397 .listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800398
yueg77cb8e52017-10-27 16:42:51 -0700399 AudioModeProvider.getInstance().addListener(this);
400
yueg7f5acbe2018-01-10 13:50:29 -0800401 if (motorolaInCallUiNotifier == null) {
402 // Add listener to notify Telephony process when the incoming call screen is started or
403 // finished. This is for hiding USSD dialog because the incoming call screen should have
404 // higher precedence over this dialog.
405 motorolaInCallUiNotifier = new MotorolaInCallUiNotifier(context);
406 addInCallUiListener(motorolaInCallUiNotifier);
407 addListener(motorolaInCallUiNotifier);
408 }
409
yueg32d9ae72018-04-05 17:35:56 -0700410 this.shouldStartInBubbleMode = shouldStartInBubbleMode(intent);
411
Eric Erfanian2ca43182017-08-31 06:57:16 -0700412 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
413 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800414 }
415
416 /**
yueg32d9ae72018-04-05 17:35:56 -0700417 * Return whether we should start call in bubble mode and not show InCallActivity. The call mode
418 * should be set in CallConfiguration in EXTRA_OUTGOING_CALL_EXTRAS when starting a call intent.
419 */
420 private boolean shouldStartInBubbleMode(Intent intent) {
421 if (!ReturnToCallController.isEnabled(context)) {
422 return false;
423 }
424 if (!intent.hasExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS)) {
425 return false;
426 }
427 Bundle extras = intent.getParcelableExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
428 byte[] callConfigurationByteArray = extras.getByteArray(CALL_CONFIGURATION_EXTRA);
429 if (callConfigurationByteArray == null) {
430 return false;
431 }
432 try {
433 CallConfiguration callConfiguration = CallConfiguration.parseFrom(callConfigurationByteArray);
434 LogUtil.i(
435 "InCallPresenter.shouldStartInBubbleMode",
436 "call mode: " + callConfiguration.getCallMode());
437 return callConfiguration.getCallMode() == Mode.BUBBLE;
438 } catch (InvalidProtocolBufferException e) {
439 return false;
440 }
441 }
442
443 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800444 * Called when the telephony service has disconnected from us. This will happen when there are no
445 * more active calls. However, we may still want to continue showing the UI for certain cases like
446 * showing "Call Ended". What we really want is to wait for the activity and the service to both
447 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
448 * secondary method that performs the aforementioned logic.
449 */
450 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700451 LogUtil.d("InCallPresenter.tearDown", "tearDown");
linyuh183cb712017-12-27 17:02:37 -0800452 callList.clearOnDisconnect();
Eric Erfanianccca3152017-02-22 16:32:36 -0800453
linyuh183cb712017-12-27 17:02:37 -0800454 serviceConnected = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800455
linyuh183cb712017-12-27 17:02:37 -0800456 context
Eric Erfanianccca3152017-02-22 16:32:36 -0800457 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800458 .listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800459
460 attemptCleanup();
461 VideoPauseController.getInstance().tearDown();
yueg77cb8e52017-10-27 16:42:51 -0700462 AudioModeProvider.getInstance().removeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800463 }
464
465 private void attemptFinishActivity() {
linyuh183cb712017-12-27 17:02:37 -0800466 screenTimeoutEnabled = true;
467 final boolean doFinish = (inCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700468 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800469 if (doFinish) {
linyuh183cb712017-12-27 17:02:37 -0800470 inCallActivity.setExcludeFromRecents(true);
471 inCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800472 }
473 }
474
475 /**
476 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
477 * for more insight on the tear-down process.
478 */
479 public void unsetActivity(InCallActivity inCallActivity) {
480 if (inCallActivity == null) {
481 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
482 }
linyuh183cb712017-12-27 17:02:37 -0800483 if (this.inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700484 LogUtil.i(
485 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800486 return;
487 }
linyuh183cb712017-12-27 17:02:37 -0800488 if (this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700489 LogUtil.w(
490 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800491 "Second instance of InCallActivity is trying to unregister when another"
492 + " instance is active. Ignoring.");
493 return;
494 }
495 updateActivity(null);
496 }
497
498 /**
499 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
500 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
501 */
502 private void updateActivity(InCallActivity inCallActivity) {
wangqi9982f0d2017-10-11 17:46:07 -0700503 Trace.beginSection("InCallPresenter.updateActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800504 boolean updateListeners = false;
505 boolean doAttemptCleanup = false;
506
507 if (inCallActivity != null) {
linyuh183cb712017-12-27 17:02:37 -0800508 if (this.inCallActivity == null) {
509 context = inCallActivity.getApplicationContext();
Eric Erfanianccca3152017-02-22 16:32:36 -0800510 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700511 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800512 } else {
513 // since setActivity is called onStart(), it can be called multiple times.
514 // This is fine and ignorable, but we do not want to update the world every time
515 // this happens (like going to/from background) so we do not set updateListeners.
516 }
517
linyuh183cb712017-12-27 17:02:37 -0800518 this.inCallActivity = inCallActivity;
519 this.inCallActivity.setExcludeFromRecents(false);
erfaniand05d8992018-03-20 19:42:26 -0700520 this.inCallActivity.setSpeakEasyCallManager(this.speakEasyCallManager);
Eric Erfanianccca3152017-02-22 16:32:36 -0800521
522 // By the time the UI finally comes up, the call may already be disconnected.
523 // If that's the case, we may need to show an error dialog.
linyuh183cb712017-12-27 17:02:37 -0800524 if (callList != null && callList.getDisconnectedCall() != null) {
525 showDialogOrToastForDisconnectedCall(callList.getDisconnectedCall());
Eric Erfanianccca3152017-02-22 16:32:36 -0800526 }
527
528 // When the UI comes up, we need to first check the in-call state.
529 // If we are showing NO_CALLS, that means that a call probably connected and
530 // then immediately disconnected before the UI was able to come up.
531 // If we dont have any calls, start tearing down the UI instead.
532 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
533 // it has been set.
linyuh183cb712017-12-27 17:02:37 -0800534 if (inCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700535 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800536 attemptFinishActivity();
wangqi9982f0d2017-10-11 17:46:07 -0700537 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800538 return;
539 }
540 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700541 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800542 updateListeners = true;
linyuh183cb712017-12-27 17:02:37 -0800543 this.inCallActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800544
545 // We attempt cleanup for the destroy case but only after we recalculate the state
546 // to see if we need to come back up or stay shut down. This is why we do the
547 // cleanup after the call to onCallListChange() instead of directly here.
548 doAttemptCleanup = true;
549 }
550
551 // Messages can come from the telephony layer while the activity is coming up
552 // and while the activity is going down. So in both cases we need to recalculate what
553 // state we should be in after they complete.
554 // Examples: (1) A new incoming call could come in and then get disconnected before
555 // the activity is created.
556 // (2) All calls could disconnect and then get a new incoming call before the
557 // activity is destroyed.
558 //
Eric Erfanian938468d2017-10-24 14:05:52 -0700559 // a bug - We previously had a check for mServiceConnected here as well, but there are
Eric Erfanianccca3152017-02-22 16:32:36 -0800560 // cases where we need to recalculate the current state even if the service in not
561 // connected. In particular the case where startOrFinish() is called while the app is
562 // already finish()ing. In that case, we skip updating the state with the knowledge that
563 // we will check again once the activity has finished. That means we have to recalculate the
564 // state here even if the service is disconnected since we may not have finished a state
565 // transition while finish()ing.
566 if (updateListeners) {
linyuh183cb712017-12-27 17:02:37 -0800567 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800568 }
569
570 if (doAttemptCleanup) {
571 attemptCleanup();
572 }
wangqi9982f0d2017-10-11 17:46:07 -0700573 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800574 }
575
576 public void setManageConferenceActivity(
577 @Nullable ManageConferenceActivity manageConferenceActivity) {
linyuh183cb712017-12-27 17:02:37 -0800578 this.manageConferenceActivity = manageConferenceActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -0800579 }
580
581 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700582 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800583 bringToForeground(showDialpad);
584 }
585
586 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700587 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800588 LatencyReport latencyReport = new LatencyReport(call);
589 if (shouldAttemptBlocking(call)) {
590 maybeBlockCall(call, latencyReport);
591 } else {
592 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800593 externalCallList.onCallAdded(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800594 } else {
595 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800596 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800597 }
598 }
599
600 // Since a call has been added we are no longer waiting for Telecom to send us a call.
601 setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -0800602 call.registerCallback(callCallback);
zachh78e54ac2017-12-05 16:38:35 -0800603 // TODO(maxwelb): Return the future in recordPhoneLookupInfo and propagate.
linyuh183cb712017-12-27 17:02:37 -0800604 PhoneLookupHistoryRecorder.recordPhoneLookupInfo(context.getApplicationContext(), call);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700605 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800606 }
607
608 private boolean shouldAttemptBlocking(android.telecom.Call call) {
609 if (call.getState() != android.telecom.Call.STATE_RINGING) {
610 return false;
611 }
linyuh183cb712017-12-27 17:02:37 -0800612 if (!UserManagerCompat.isUserUnlocked(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700613 LogUtil.i(
614 "InCallPresenter.shouldAttemptBlocking",
615 "not attempting to block incoming call because user is locked");
616 return false;
617 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800618 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700619 LogUtil.i(
620 "InCallPresenter.shouldAttemptBlocking",
621 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800622 return false;
623 }
linyuh183cb712017-12-27 17:02:37 -0800624 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700625 LogUtil.i(
626 "InCallPresenter.shouldAttemptBlocking",
627 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800628 return false;
629 }
630 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
631 return false;
632 }
linyuh183cb712017-12-27 17:02:37 -0800633 if (FilteredNumberCompat.useNewFiltering(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700634 LogUtil.i(
635 "InCallPresenter.shouldAttemptBlocking",
636 "not attempting to block incoming call because framework blocking is in use");
637 return false;
638 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800639 return true;
640 }
641
642 /**
643 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
644 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
645 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
646 * call anyways.
647 */
648 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
linyuh183cb712017-12-27 17:02:37 -0800649 final String countryIso = GeoUtil.getCurrentCountryIso(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800650 final String number = TelecomCallUtil.getNumber(call);
651 final long timeAdded = System.currentTimeMillis();
652
653 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
654 // main UI thread. It is needed so we can change its value within different scopes, since
655 // that cannot be done with a final boolean.
656 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
657
658 final Handler handler = new Handler();
659
660 // Proceed if the query is slow; the call may still be blocked after the query returns.
661 final Runnable runnable =
662 new Runnable() {
663 @Override
664 public void run() {
665 hasTimedOut.set(true);
666 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800667 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800668 }
669 };
670 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
671
672 OnCheckBlockedListener onCheckBlockedListener =
673 new OnCheckBlockedListener() {
674 @Override
675 public void onCheckComplete(final Integer id) {
676 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700677 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800678 return;
679 }
680 if (!hasTimedOut.get()) {
681 handler.removeCallbacks(runnable);
682 }
683 if (id == null) {
684 if (!hasTimedOut.get()) {
685 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800686 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800687 }
688 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700689 LogUtil.d(
690 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800691 if (!hasTimedOut.get()) {
692 handler.removeCallbacks(runnable);
693
694 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800695 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800696 }
697 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700698 LogUtil.i(
699 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800700 call.reject(false, null);
linyuh183cb712017-12-27 17:02:37 -0800701 Logger.get(context).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
Eric Erfanianccca3152017-02-22 16:32:36 -0800702
703 /*
704 * If mContext is null, then the InCallPresenter was torn down before the
705 * block check had a chance to complete. The context is no longer valid, so
706 * don't attempt to remove the call log entry.
707 */
linyuh183cb712017-12-27 17:02:37 -0800708 if (context == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800709 return;
710 }
711 // Register observer to update the call log.
712 // BlockedNumberContentObserver will unregister after successful log or timeout.
713 BlockedNumberContentObserver contentObserver =
linyuh183cb712017-12-27 17:02:37 -0800714 new BlockedNumberContentObserver(context, new Handler(), number, timeAdded);
Eric Erfanianccca3152017-02-22 16:32:36 -0800715 contentObserver.register();
716 }
717 }
718 };
719
linyuh183cb712017-12-27 17:02:37 -0800720 filteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800721 }
722
723 public void onCallRemoved(android.telecom.Call call) {
724 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800725 externalCallList.onCallRemoved(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800726 } else {
linyuh183cb712017-12-27 17:02:37 -0800727 callList.onCallRemoved(context, call);
728 call.unregisterCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800729 }
730 }
731
732 public void onCanAddCallChanged(boolean canAddCall) {
linyuh183cb712017-12-27 17:02:37 -0800733 for (CanAddCallListener listener : canAddCallListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800734 listener.onCanAddCallChanged(canAddCall);
735 }
736 }
737
738 @Override
739 public void onWiFiToLteHandover(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -0700740 if (call.hasShownWiFiToLteHandoverToast()) {
741 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800742 }
yueg32d9ae72018-04-05 17:35:56 -0700743
744 Toast.makeText(context, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG)
745 .show();
746 call.setHasShownWiFiToLteHandoverToast();
Eric Erfanianccca3152017-02-22 16:32:36 -0800747 }
748
749 @Override
750 public void onHandoverToWifiFailed(DialerCall call) {
linyuh183cb712017-12-27 17:02:37 -0800751 if (inCallActivity != null) {
752 inCallActivity.showDialogOrToastForWifiHandoverFailure(call);
yueg32d9ae72018-04-05 17:35:56 -0700753 } else {
754 Toast.makeText(context, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT)
755 .show();
Eric Erfanianccca3152017-02-22 16:32:36 -0800756 }
757 }
758
Eric Erfanianc857f902017-05-15 14:05:33 -0700759 @Override
760 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
761 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
linyuh183cb712017-12-27 17:02:37 -0800762 if (inCallActivity != null) {
763 inCallActivity.showDialogForInternationalCallOnWifi(call);
Eric Erfanianc857f902017-05-15 14:05:33 -0700764 }
765 }
766
Eric Erfanianccca3152017-02-22 16:32:36 -0800767 /**
768 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
769 * app based on the information it gets from CallList. Dispatches the in-call state to all
770 * listeners. Can trigger the creation or destruction of the UI based on the states that is
771 * calculates.
772 */
773 @Override
774 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700775 Trace.beginSection("InCallPresenter.onCallListChange");
linyuh183cb712017-12-27 17:02:37 -0800776 if (inCallActivity != null && inCallActivity.isInCallScreenAnimating()) {
777 awaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700778 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800779 return;
780 }
781 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700782 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800783 return;
784 }
785
linyuh183cb712017-12-27 17:02:37 -0800786 awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800787
788 InCallState newState = getPotentialStateFromCallList(callList);
linyuh183cb712017-12-27 17:02:37 -0800789 InCallState oldState = inCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700790 LogUtil.d(
791 "InCallPresenter.onCallListChange",
792 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700793
794 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
795 // incall activity for that call will still exist (even if it's not visible). In the case of
796 // an incoming call in that situation, just disconnect that "waiting for account" call and
797 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
Eric Erfanian938468d2017-10-24 14:05:52 -0700798 // a bug for more details.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700799 DialerCall waitingForAccountCall;
800 if (newState == InCallState.INCOMING
801 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
802 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700803 // The InCallActivity might be destroyed or not started yet at this point.
804 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800805 inCallActivity.dismissPendingDialogs();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700806 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700807 }
808
Eric Erfanianccca3152017-02-22 16:32:36 -0800809 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700810 LogUtil.d(
811 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800812
813 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700814 LogUtil.i(
815 "InCallPresenter.onCallListChange",
816 "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800817 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800818
yuegb9103042018-03-30 12:12:25 -0700819 // Foreground call changed
820 DialerCall primary = null;
821 if (newState == InCallState.INCOMING) {
822 primary = callList.getIncomingCall();
823 } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
824 primary = callList.getOutgoingCall();
825 if (primary == null) {
826 primary = callList.getPendingOutgoingCall();
827 }
828 } else if (newState == InCallState.INCALL) {
829 primary = getCallToDisplay(callList, null, false);
830 }
831 if (primary != null) {
832 onForegroundCallChanged(primary);
833 }
834
Eric Erfanianccca3152017-02-22 16:32:36 -0800835 // notify listeners of new state
linyuh183cb712017-12-27 17:02:37 -0800836 for (InCallStateListener listener : listeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700837 LogUtil.d(
838 "InCallPresenter.onCallListChange",
linyuh183cb712017-12-27 17:02:37 -0800839 "Notify " + listener + " of state " + inCallState.toString());
840 listener.onStateChange(oldState, inCallState, callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800841 }
842
843 if (isActivityStarted()) {
844 final boolean hasCall =
845 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
linyuh183cb712017-12-27 17:02:37 -0800846 inCallActivity.dismissKeyguard(hasCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800847 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700848 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800849 }
850
yuegb9103042018-03-30 12:12:25 -0700851 /**
852 * Get the highest priority call to display. Goes through the calls and chooses which to return
853 * based on priority of which type of call to display to the user. Callers can use the "ignore"
854 * feature to get the second best call by passing a previously found primary call as ignore.
855 *
856 * @param ignore A call to ignore if found.
857 */
858 static DialerCall getCallToDisplay(
859 CallList callList, DialerCall ignore, boolean skipDisconnected) {
860 // Active calls come second. An active call always gets precedent.
861 DialerCall retval = callList.getActiveCall();
862 if (retval != null && retval != ignore) {
863 return retval;
864 }
865
866 // Sometimes there is intemediate state that two calls are in active even one is about
867 // to be on hold.
868 retval = callList.getSecondActiveCall();
869 if (retval != null && retval != ignore) {
870 return retval;
871 }
872
873 // Disconnected calls get primary position if there are no active calls
874 // to let user know quickly what call has disconnected. Disconnected
875 // calls are very short lived.
876 if (!skipDisconnected) {
877 retval = callList.getDisconnectingCall();
878 if (retval != null && retval != ignore) {
879 return retval;
880 }
881 retval = callList.getDisconnectedCall();
882 if (retval != null && retval != ignore) {
883 return retval;
884 }
885 }
886
887 // Then we go to background call (calls on hold)
888 retval = callList.getBackgroundCall();
889 if (retval != null && retval != ignore) {
890 return retval;
891 }
892
893 // Lastly, we go to a second background call.
894 retval = callList.getSecondBackgroundCall();
895
896 return retval;
897 }
898
Eric Erfanianccca3152017-02-22 16:32:36 -0800899 /** Called when there is a new incoming call. */
900 @Override
901 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700902 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800903 InCallState newState = startOrFinishUi(InCallState.INCOMING);
linyuh183cb712017-12-27 17:02:37 -0800904 InCallState oldState = inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800905
Eric Erfanian2ca43182017-08-31 06:57:16 -0700906 LogUtil.i(
907 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800908 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800909
wangqicf61ca02017-08-31 15:32:55 -0700910 Trace.beginSection("listener.onIncomingCall");
linyuh183cb712017-12-27 17:02:37 -0800911 for (IncomingCallListener listener : incomingCallListeners) {
912 listener.onIncomingCall(oldState, inCallState, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800913 }
wangqicf61ca02017-08-31 15:32:55 -0700914 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800915
wangqicf61ca02017-08-31 15:32:55 -0700916 Trace.beginSection("onPrimaryCallStateChanged");
linyuh183cb712017-12-27 17:02:37 -0800917 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800918 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800919 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800920 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700921 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700922 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800923 }
924
925 @Override
926 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700927 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
linyuh183cb712017-12-27 17:02:37 -0800928 && inCallState == InCallPresenter.InCallState.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800929 LogUtil.i(
930 "InCallPresenter.onUpgradeToVideo",
931 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700932 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800933 }
934
linyuh183cb712017-12-27 17:02:37 -0800935 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800936 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800937 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800938 }
939 }
940
941 @Override
wangqibc28ea72018-04-02 16:23:00 -0700942 public void onUpgradeToRtt(DialerCall call, int rttRequestId) {
943 if (inCallActivity != null) {
944 inCallActivity.showDialogForRttRequest(call, rttRequestId);
945 }
946 }
947
948 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700949 public void onSessionModificationStateChange(DialerCall call) {
950 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800951 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
linyuh183cb712017-12-27 17:02:37 -0800952 if (proximitySensor == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800953 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
954 return;
955 }
linyuh183cb712017-12-27 17:02:37 -0800956 proximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700957 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
linyuh183cb712017-12-27 17:02:37 -0800958 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800959 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800960 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800961 }
962 }
963
964 /**
965 * Called when a call becomes disconnected. Called everytime an existing call changes from being
966 * connected (incoming/outgoing/active) to disconnected.
967 */
968 @Override
969 public void onDisconnect(DialerCall call) {
linyuh7b86f562017-11-16 11:24:09 -0800970 showDialogOrToastForDisconnectedCall(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800971
972 // We need to do the run the same code as onCallListChange.
linyuh183cb712017-12-27 17:02:37 -0800973 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800974
975 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800976 inCallActivity.dismissKeyguard(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800977 }
978
979 if (call.isEmergencyCall()) {
linyuh183cb712017-12-27 17:02:37 -0800980 FilteredNumbersUtil.recordLastEmergencyCallTime(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800981 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800982
linyuh183cb712017-12-27 17:02:37 -0800983 if (!callList.hasLiveCall()
Eric Erfanianfc37b022017-03-21 10:11:17 -0700984 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700985 && !isSecretCode(call.getNumber())
wangqi9982f0d2017-10-11 17:46:07 -0700986 && !call.isVoiceMailNumber()) {
linyuh183cb712017-12-27 17:02:37 -0800987 PostCall.onCallDisconnected(context, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -0800988 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800989 }
990
Eric Erfanian10b34a52017-05-04 08:23:17 -0700991 private boolean isSecretCode(@Nullable String number) {
992 return number != null
993 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
994 }
995
Eric Erfanianccca3152017-02-22 16:32:36 -0800996 /** Given the call list, return the state in which the in-call screen should be. */
997 public InCallState getPotentialStateFromCallList(CallList callList) {
998
999 InCallState newState = InCallState.NO_CALLS;
1000
1001 if (callList == null) {
1002 return newState;
1003 }
1004 if (callList.getIncomingCall() != null) {
1005 newState = InCallState.INCOMING;
1006 } else if (callList.getWaitingForAccountCall() != null) {
1007 newState = InCallState.WAITING_FOR_ACCOUNT;
1008 } else if (callList.getPendingOutgoingCall() != null) {
1009 newState = InCallState.PENDING_OUTGOING;
1010 } else if (callList.getOutgoingCall() != null) {
1011 newState = InCallState.OUTGOING;
1012 } else if (callList.getActiveCall() != null
1013 || callList.getBackgroundCall() != null
1014 || callList.getDisconnectedCall() != null
1015 || callList.getDisconnectingCall() != null) {
1016 newState = InCallState.INCALL;
1017 }
1018
1019 if (newState == InCallState.NO_CALLS) {
linyuh183cb712017-12-27 17:02:37 -08001020 if (boundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001021 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001022 }
1023 }
1024
1025 return newState;
1026 }
1027
1028 public boolean isBoundAndWaitingForOutgoingCall() {
linyuh183cb712017-12-27 17:02:37 -08001029 return boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -08001030 }
1031
1032 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001033 LogUtil.i(
1034 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
1035 "setBoundAndWaitingForOutgoingCall: " + isBound);
linyuh183cb712017-12-27 17:02:37 -08001036 boundAndWaitingForOutgoingCall = isBound;
1037 themeColorManager.setPendingPhoneAccountHandle(handle);
1038 if (isBound && inCallState == InCallState.NO_CALLS) {
1039 inCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001040 }
1041 }
1042
1043 public void onShrinkAnimationComplete() {
linyuh183cb712017-12-27 17:02:37 -08001044 if (awaitingCallListUpdate) {
1045 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001046 }
1047 }
1048
1049 public void addIncomingCallListener(IncomingCallListener listener) {
1050 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001051 incomingCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001052 }
1053
1054 public void removeIncomingCallListener(IncomingCallListener listener) {
1055 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001056 incomingCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001057 }
1058 }
1059
1060 public void addListener(InCallStateListener listener) {
1061 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001062 listeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001063 }
1064
1065 public void removeListener(InCallStateListener listener) {
1066 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001067 listeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001068 }
1069 }
1070
1071 public void addDetailsListener(InCallDetailsListener listener) {
1072 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001073 detailsListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001074 }
1075
1076 public void removeDetailsListener(InCallDetailsListener listener) {
1077 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001078 detailsListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001079 }
1080 }
1081
1082 public void addCanAddCallListener(CanAddCallListener listener) {
1083 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001084 canAddCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001085 }
1086
1087 public void removeCanAddCallListener(CanAddCallListener listener) {
1088 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001089 canAddCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001090 }
1091 }
1092
1093 public void addOrientationListener(InCallOrientationListener listener) {
1094 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001095 orientationListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001096 }
1097
1098 public void removeOrientationListener(InCallOrientationListener listener) {
1099 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001100 orientationListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001101 }
1102 }
1103
1104 public void addInCallEventListener(InCallEventListener listener) {
1105 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001106 inCallEventListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001107 }
1108
1109 public void removeInCallEventListener(InCallEventListener listener) {
1110 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001111 inCallEventListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001112 }
1113 }
1114
1115 public ProximitySensor getProximitySensor() {
linyuh183cb712017-12-27 17:02:37 -08001116 return proximitySensor;
Eric Erfanianccca3152017-02-22 16:32:36 -08001117 }
1118
1119 public PseudoScreenState getPseudoScreenState() {
linyuh183cb712017-12-27 17:02:37 -08001120 return pseudoScreenState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001121 }
1122
1123 /** Returns true if the incall app is the foreground application. */
1124 public boolean isShowingInCallUi() {
1125 if (!isActivityStarted()) {
1126 return false;
1127 }
linyuh183cb712017-12-27 17:02:37 -08001128 if (manageConferenceActivity != null && manageConferenceActivity.isVisible()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001129 return true;
1130 }
linyuh183cb712017-12-27 17:02:37 -08001131 return inCallActivity.isVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001132 }
1133
1134 /**
1135 * Returns true if the activity has been created and is running. Returns true as long as activity
1136 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
1137 * (not in foreground).
1138 */
1139 public boolean isActivityStarted() {
linyuh183cb712017-12-27 17:02:37 -08001140 return (inCallActivity != null
1141 && !inCallActivity.isDestroyed()
1142 && !inCallActivity.isFinishing());
Eric Erfanianccca3152017-02-22 16:32:36 -08001143 }
1144
1145 /**
1146 * Determines if the In-Call app is currently changing configuration.
1147 *
1148 * @return {@code true} if the In-Call app is changing configuration.
1149 */
1150 public boolean isChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001151 return isChangingConfigurations;
Eric Erfanianccca3152017-02-22 16:32:36 -08001152 }
1153
1154 /**
1155 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1156 * screen orientation).
1157 */
1158 /*package*/
1159 void updateIsChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001160 isChangingConfigurations = false;
1161 if (inCallActivity != null) {
1162 isChangingConfigurations = inCallActivity.isChangingConfigurations();
Eric Erfanianccca3152017-02-22 16:32:36 -08001163 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001164 LogUtil.v(
1165 "InCallPresenter.updateIsChangingConfigurations",
linyuh183cb712017-12-27 17:02:37 -08001166 "updateIsChangingConfigurations = " + isChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001167 }
1168
yueg10f6e822018-01-17 15:32:18 -08001169 /** Called when the activity goes in/out of the foreground. */
1170 public void onUiShowing(boolean showing) {
linyuh183cb712017-12-27 17:02:37 -08001171 if (proximitySensor != null) {
1172 proximitySensor.onInCallShowing(showing);
Eric Erfanianccca3152017-02-22 16:32:36 -08001173 }
1174
yueg092b21c2017-11-15 16:20:07 -08001175 if (!showing) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001176 updateIsChangingConfigurations();
1177 }
1178
linyuh183cb712017-12-27 17:02:37 -08001179 for (InCallUiListener listener : inCallUiListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001180 listener.onUiShowing(showing);
1181 }
1182
linyuh183cb712017-12-27 17:02:37 -08001183 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001184 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001185 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001186 }
1187 }
1188
Eric Erfanian2ca43182017-08-31 06:57:16 -07001189 public void refreshUi() {
linyuh183cb712017-12-27 17:02:37 -08001190 if (inCallActivity != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001191 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001192 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001193 }
1194 }
1195
Eric Erfanianccca3152017-02-22 16:32:36 -08001196 public void addInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001197 inCallUiListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001198 }
1199
1200 public boolean removeInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001201 return inCallUiListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001202 }
1203
1204 /*package*/
1205 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001206 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001207 notifyVideoPauseController(true);
Eric Erfanian2ca43182017-08-31 06:57:16 -07001208 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001209 }
1210
1211 /*package*/
1212 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001213 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001214 notifyVideoPauseController(false);
1215 }
1216
1217 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001218 LogUtil.d(
1219 "InCallPresenter.notifyVideoPauseController",
linyuh183cb712017-12-27 17:02:37 -08001220 "mIsChangingConfigurations=" + isChangingConfigurations);
1221 if (!isChangingConfigurations) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001222 VideoPauseController.getInstance().onUiShowing(showing);
1223 }
1224 }
1225
1226 /** Brings the app into the foreground if possible. */
1227 public void bringToForeground(boolean showDialpad) {
1228 // Before we bring the incall UI to the foreground, we check to see if:
1229 // 1. It is not currently in the foreground
1230 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1231 // be displayed)
1232 // If the activity hadn't actually been started previously, yet there are still calls
1233 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1234 // bring it up the UI regardless.
linyuh183cb712017-12-27 17:02:37 -08001235 if (!isShowingInCallUi() && inCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001236 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001237 }
1238 }
1239
1240 public void onPostDialCharWait(String callId, String chars) {
1241 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001242 inCallActivity.showDialogForPostCharWait(callId, chars);
Eric Erfanianccca3152017-02-22 16:32:36 -08001243 }
1244 }
1245
1246 /**
1247 * Handles the green CALL key while in-call.
1248 *
1249 * @return true if we consumed the event.
1250 */
1251 public boolean handleCallKey() {
1252 LogUtil.v("InCallPresenter.handleCallKey", null);
1253
1254 // The green CALL button means either "Answer", "Unhold", or
1255 // "Swap calls", or can be a no-op, depending on the current state
1256 // of the Phone.
1257
1258 /** INCOMING CALL */
linyuh183cb712017-12-27 17:02:37 -08001259 final CallList calls = callList;
Eric Erfanianccca3152017-02-22 16:32:36 -08001260 final DialerCall incomingCall = calls.getIncomingCall();
1261 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1262
1263 // (1) Attempt to answer a call
1264 if (incomingCall != null) {
1265 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1266 return true;
1267 }
1268
1269 /** STATE_ACTIVE CALL */
1270 final DialerCall activeCall = calls.getActiveCall();
1271 if (activeCall != null) {
1272 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1273 // consolidate this logic.
1274 final boolean canMerge =
1275 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1276 final boolean canSwap =
1277 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1278
Eric Erfanian2ca43182017-08-31 06:57:16 -07001279 LogUtil.v(
1280 "InCallPresenter.handleCallKey",
1281 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001282
1283 // (2) Attempt actions on conference calls
1284 if (canMerge) {
1285 TelecomAdapter.getInstance().merge(activeCall.getId());
1286 return true;
1287 } else if (canSwap) {
1288 TelecomAdapter.getInstance().swap(activeCall.getId());
1289 return true;
1290 }
1291 }
1292
1293 /** BACKGROUND CALL */
1294 final DialerCall heldCall = calls.getBackgroundCall();
1295 if (heldCall != null) {
1296 // We have a hold call so presumeable it will always support HOLD...but
1297 // there is no harm in double checking.
1298 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1299
Eric Erfanian2ca43182017-08-31 06:57:16 -07001300 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001301
1302 // (4) unhold call
1303 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1304 heldCall.unhold();
1305 return true;
1306 }
1307 }
1308
1309 // Always consume hard keys
1310 return true;
1311 }
1312
Eric Erfanianccca3152017-02-22 16:32:36 -08001313 /** Clears the previous fullscreen state. */
1314 public void clearFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001315 isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001316 }
1317
1318 /**
1319 * Changes the fullscreen mode of the in-call UI.
1320 *
1321 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1322 * otherwise.
1323 */
1324 public void setFullScreen(boolean isFullScreen) {
1325 setFullScreen(isFullScreen, false /* force */);
1326 }
1327
1328 /**
1329 * Changes the fullscreen mode of the in-call UI.
1330 *
1331 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1332 * otherwise.
1333 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1334 */
1335 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001336 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001337
1338 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1339 if (isDialpadVisible()) {
1340 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001341 LogUtil.v(
1342 "InCallPresenter.setFullScreen",
1343 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001344 }
1345
linyuh183cb712017-12-27 17:02:37 -08001346 if (this.isFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001347 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001348 return;
1349 }
linyuh183cb712017-12-27 17:02:37 -08001350 this.isFullScreen = isFullScreen;
1351 notifyFullscreenModeChange(this.isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001352 }
1353
1354 /**
1355 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1356 * otherwise.
1357 */
1358 public boolean isFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001359 return isFullScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001360 }
1361
1362 /**
1363 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1364 *
1365 * @param isFullscreenMode {@code True} if entering full screen mode.
1366 */
1367 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001368 for (InCallEventListener listener : inCallEventListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001369 listener.onFullscreenModeChanged(isFullscreenMode);
1370 }
1371 }
1372
linyuh7b86f562017-11-16 11:24:09 -08001373 /** Instruct the in-call activity to show an error dialog or toast for a disconnected call. */
1374 private void showDialogOrToastForDisconnectedCall(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -07001375 if (call.getState() != DialerCall.State.DISCONNECTED) {
linyuh7b86f562017-11-16 11:24:09 -08001376 return;
Eric Erfanianccca3152017-02-22 16:32:36 -08001377 }
linyuh7b86f562017-11-16 11:24:09 -08001378
1379 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1380 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1381 setDisconnectCauseForMissingAccounts(call);
1382 }
1383
yueg32d9ae72018-04-05 17:35:56 -07001384 if (isActivityStarted()) {
1385 inCallActivity.showDialogOrToastForDisconnectedCall(
1386 new DisconnectMessage(inCallActivity, call));
1387 } else {
1388 CharSequence message = new DisconnectMessage(context, call).toastMessage;
1389 if (message != null) {
1390 Toast.makeText(context, message, Toast.LENGTH_LONG).show();
1391 }
1392 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001393 }
1394
1395 /**
1396 * When the state of in-call changes, this is the first method to get called. It determines if the
1397 * UI needs to be started or finished depending on the new state and does it.
1398 */
1399 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001400 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001401 LogUtil.d(
linyuh183cb712017-12-27 17:02:37 -08001402 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + inCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001403
1404 // TODO: Consider a proper state machine implementation
1405
1406 // If the state isn't changing we have already done any starting/stopping of activities in
1407 // a previous pass...so lets cut out early
linyuh183cb712017-12-27 17:02:37 -08001408 if (newState == inCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001409 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001410 return newState;
1411 }
1412
1413 // A new Incoming call means that the user needs to be notified of the the call (since
1414 // it wasn't them who initiated it). We do this through full screen notifications and
1415 // happens indirectly through {@link StatusBarNotifier}.
1416 //
1417 // The process for incoming calls is as follows:
1418 //
1419 // 1) CallList - Announces existence of new INCOMING call
1420 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1421 // - should be set to INCOMING.
1422 // 3) InCallPresenter - This method is called to see if we need to start or finish
1423 // the app given the new state.
1424 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1425 // StatusBarNotifier explicitly to issue a FullScreen Notification
1426 // that will either start the InCallActivity or show the user a
1427 // top-level notification dialog if the user is in an immersive app.
1428 // That notification can also start the InCallActivity.
1429 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1430 // call InCallPresenter::setActivity() to let the presenter
1431 // know that start-up is complete.
1432 //
1433 // [ AND NOW YOU'RE IN THE CALL. voila! ]
Eric Erfanianccca3152017-02-22 16:32:36 -08001434
1435 // A dialog to show on top of the InCallUI to select a PhoneAccount
1436 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1437
1438 // A new outgoing call indicates that the user just now dialed a number and when that
1439 // happens we need to display the screen immediately or show an account picker dialog if
1440 // no default is set. However, if the main InCallUI is already visible, we do not want to
1441 // re-initiate the start-up animation, so we do not need to do anything here.
1442 //
1443 // It is also possible to go into an intermediate state where the call has been initiated
1444 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1445 // This pending outgoing state can also launch the call screen.
1446 //
1447 // This is different from the incoming call sequence because we do not need to shock the
1448 // user with a top-level notification. Just show the call UI normally.
1449 boolean callCardFragmentVisible =
linyuh183cb712017-12-27 17:02:37 -08001450 inCallActivity != null && inCallActivity.getCallCardFragmentVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001451 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1452 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1453
1454 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1455 // outgoing call process, so the UI should be brought up to show an error dialog.
1456 showCallUi |=
linyuh183cb712017-12-27 17:02:37 -08001457 (InCallState.PENDING_OUTGOING == inCallState
Eric Erfanianccca3152017-02-22 16:32:36 -08001458 && InCallState.INCALL == newState
1459 && !isShowingInCallUi());
1460
1461 // Another exception - InCallActivity is in charge of disconnecting a call with no
1462 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1463 // call so that:
1464 // 1) The call can be disconnected correctly
1465 // 2) The UI comes up and correctly displays the error dialog.
1466 // TODO: Remove these special case conditions by making InCallPresenter a true state
1467 // machine. Telecom should also be the component responsible for disconnecting a call
1468 // with no valid accounts.
1469 showCallUi |=
1470 InCallState.PENDING_OUTGOING == newState
1471 && mainUiNotVisible
linyuh183cb712017-12-27 17:02:37 -08001472 && isCallWithNoValidAccounts(callList.getPendingOutgoingCall());
Eric Erfanianccca3152017-02-22 16:32:36 -08001473
1474 // The only time that we have an instance of mInCallActivity and it isn't started is
1475 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1476 // the activity. When it is finally destroyed, we double check if we should bring it back
1477 // up so we aren't going to lose anything by avoiding a second startup here.
linyuh183cb712017-12-27 17:02:37 -08001478 boolean activityIsFinishing = inCallActivity != null && !isActivityStarted();
Eric Erfanianccca3152017-02-22 16:32:36 -08001479 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001480 LogUtil.i(
1481 "InCallPresenter.startOrFinishUi",
linyuh183cb712017-12-27 17:02:37 -08001482 "Undo the state change: " + newState + " -> " + inCallState);
wangqicf61ca02017-08-31 15:32:55 -07001483 Trace.endSection();
linyuh183cb712017-12-27 17:02:37 -08001484 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001485 }
1486
1487 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1488 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1489 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1490 // outgoing call after user ignore it by pressing home button.
1491 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1492 && !showCallUi
1493 && isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001494 inCallActivity.dismissPendingDialogs();
Eric Erfanianccca3152017-02-22 16:32:36 -08001495 }
1496
yueg32d9ae72018-04-05 17:35:56 -07001497 if ((showCallUi || showAccountPicker) && !shouldStartInBubbleMode) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001498 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001499 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001500 } else if (newState == InCallState.NO_CALLS) {
1501 // The new state is the no calls state. Tear everything down.
yueg8e544862018-04-06 14:34:59 -07001502 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001503 attemptFinishActivity();
1504 attemptCleanup();
1505 }
1506
wangqicf61ca02017-08-31 15:32:55 -07001507 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001508 return newState;
1509 }
1510
1511 /**
1512 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1513 * or PhoneAccounts to select from.
1514 */
1515 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1516
1517 Bundle extras = call.getIntentExtras();
1518 // Initialize the extras bundle to avoid NPE
1519 if (extras == null) {
1520 extras = new Bundle();
1521 }
1522
1523 final List<PhoneAccountHandle> phoneAccountHandles =
1524 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1525
1526 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1527 String scheme = call.getHandle().getScheme();
1528 final String errorMsg =
1529 PhoneAccount.SCHEME_TEL.equals(scheme)
linyuh183cb712017-12-27 17:02:37 -08001530 ? context.getString(R.string.callFailed_simError)
1531 : context.getString(R.string.incall_error_supp_service_unknown);
Eric Erfanianccca3152017-02-22 16:32:36 -08001532 DisconnectCause disconnectCause =
1533 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1534 call.setDisconnectCause(disconnectCause);
1535 }
1536 }
1537
Eric Erfanianccca3152017-02-22 16:32:36 -08001538 /**
1539 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1540 * Calling classes should use this as an indication whether to interact with the
1541 * InCallPresenter or not.
1542 */
1543 public boolean isReadyForTearDown() {
linyuh183cb712017-12-27 17:02:37 -08001544 return inCallActivity == null && !serviceConnected && inCallState == InCallState.NO_CALLS;
Eric Erfanianccca3152017-02-22 16:32:36 -08001545 }
1546
1547 /**
1548 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1549 */
1550 private void attemptCleanup() {
1551 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001552 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001553
1554 cleanupSurfaces();
1555
linyuh183cb712017-12-27 17:02:37 -08001556 isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001557
yueg32d9ae72018-04-05 17:35:56 -07001558 shouldStartInBubbleMode = false;
yueg8e544862018-04-06 14:34:59 -07001559 audioRouteSetForBubbleMode = false;
yueg32d9ae72018-04-05 17:35:56 -07001560
Eric Erfanianccca3152017-02-22 16:32:36 -08001561 // blow away stale contact info so that we get fresh data on
1562 // the next set of calls
linyuh183cb712017-12-27 17:02:37 -08001563 if (contactInfoCache != null) {
1564 contactInfoCache.clearCache();
Eric Erfanianccca3152017-02-22 16:32:36 -08001565 }
linyuh183cb712017-12-27 17:02:37 -08001566 contactInfoCache = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001567
linyuh183cb712017-12-27 17:02:37 -08001568 if (proximitySensor != null) {
1569 removeListener(proximitySensor);
1570 proximitySensor.tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -08001571 }
linyuh183cb712017-12-27 17:02:37 -08001572 proximitySensor = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001573
linyuh183cb712017-12-27 17:02:37 -08001574 if (statusBarNotifier != null) {
1575 removeListener(statusBarNotifier);
1576 EnrichedCallComponent.get(context)
Eric Erfaniand8046e52017-04-06 09:41:50 -07001577 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -08001578 .unregisterStateChangedListener(statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001579 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001580
linyuh183cb712017-12-27 17:02:37 -08001581 if (externalCallNotifier != null && externalCallList != null) {
1582 externalCallList.removeExternalCallListener(externalCallNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001583 }
linyuh183cb712017-12-27 17:02:37 -08001584 statusBarNotifier = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001585
linyuh183cb712017-12-27 17:02:37 -08001586 if (callList != null) {
1587 callList.removeListener(this);
1588 callList.removeListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001589 }
linyuh183cb712017-12-27 17:02:37 -08001590 callList = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001591
linyuh183cb712017-12-27 17:02:37 -08001592 context = null;
1593 inCallActivity = null;
1594 manageConferenceActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001595
linyuh183cb712017-12-27 17:02:37 -08001596 listeners.clear();
1597 incomingCallListeners.clear();
1598 detailsListeners.clear();
1599 canAddCallListeners.clear();
1600 orientationListeners.clear();
1601 inCallEventListeners.clear();
1602 inCallUiListeners.clear();
1603 if (!inCallUiLocks.isEmpty()) {
1604 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + inCallUiLocks);
1605 inCallUiLocks.clear();
twyen8efb4952017-10-06 16:35:54 -07001606 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001607 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001608 }
1609 }
1610
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001611 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001612 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
linyuh183cb712017-12-27 17:02:37 -08001613 context.startActivity(
1614 InCallActivity.getIntent(context, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001615 }
1616
1617 public void onServiceBind() {
linyuh183cb712017-12-27 17:02:37 -08001618 serviceBound = true;
Eric Erfanianccca3152017-02-22 16:32:36 -08001619 }
1620
1621 public void onServiceUnbind() {
1622 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -08001623 serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001624 }
1625
1626 public boolean isServiceBound() {
linyuh183cb712017-12-27 17:02:37 -08001627 return serviceBound;
Eric Erfanianccca3152017-02-22 16:32:36 -08001628 }
1629
1630 public void maybeStartRevealAnimation(Intent intent) {
linyuh183cb712017-12-27 17:02:37 -08001631 if (intent == null || inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001632 return;
1633 }
1634 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1635 if (extras == null) {
1636 // Incoming call, just show the in-call UI directly.
1637 return;
1638 }
1639
1640 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1641 // Account selection dialog will show up so don't show the animation.
1642 return;
1643 }
1644
1645 final PhoneAccountHandle accountHandle =
1646 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1647 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001648
yueg32d9ae72018-04-05 17:35:56 -07001649 setBoundAndWaitingForOutgoingCall(true, accountHandle);
1650
1651 if (shouldStartInBubbleMode) {
1652 LogUtil.i("InCallPresenter.maybeStartRevealAnimation", "shouldStartInBubbleMode");
1653 // Show bubble instead of in call UI
1654 return;
1655 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001656
1657 final Intent activityIntent =
linyuh183cb712017-12-27 17:02:37 -08001658 InCallActivity.getIntent(context, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001659 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
linyuh183cb712017-12-27 17:02:37 -08001660 context.startActivity(activityIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001661 }
1662
1663 /**
1664 * Retrieves the current in-call camera manager instance, creating if necessary.
1665 *
1666 * @return The {@link InCallCameraManager}.
1667 */
1668 public InCallCameraManager getInCallCameraManager() {
1669 synchronized (this) {
linyuh183cb712017-12-27 17:02:37 -08001670 if (inCallCameraManager == null) {
1671 inCallCameraManager = new InCallCameraManager(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001672 }
1673
linyuh183cb712017-12-27 17:02:37 -08001674 return inCallCameraManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001675 }
1676 }
1677
1678 /**
1679 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1680 *
1681 * @param orientation The screen orientation of the device (one of: {@link
1682 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1683 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1684 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1685 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1686 */
1687 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001688 LogUtil.d(
1689 "InCallPresenter.onDeviceOrientationChange",
1690 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001691
linyuh183cb712017-12-27 17:02:37 -08001692 if (callList != null) {
1693 callList.notifyCallsOfDeviceRotation(orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001694 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001695 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001696 }
1697
1698 // Notify listeners of device orientation changed.
linyuh183cb712017-12-27 17:02:37 -08001699 for (InCallOrientationListener listener : orientationListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001700 listener.onDeviceOrientationChanged(orientation);
1701 }
1702 }
1703
1704 /**
1705 * Configures the in-call UI activity so it can change orientations or not. Enables the
1706 * orientation event listener if allowOrientationChange is true, disables it if false.
1707 *
1708 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1709 * landscape. {@code false} if the in-call UI should be locked in portrait.
1710 */
1711 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
linyuh183cb712017-12-27 17:02:37 -08001712 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001713 LogUtil.e(
1714 "InCallPresenter.setInCallAllowsOrientationChange",
1715 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001716 return;
1717 }
linyuh183cb712017-12-27 17:02:37 -08001718 inCallActivity.setAllowOrientationChange(allowOrientationChange);
Eric Erfanianccca3152017-02-22 16:32:36 -08001719 }
1720
1721 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001722 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
linyuh183cb712017-12-27 17:02:37 -08001723 screenTimeoutEnabled = enable;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001724 applyScreenTimeout();
1725 }
1726
1727 private void applyScreenTimeout() {
linyuh183cb712017-12-27 17:02:37 -08001728 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001729 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001730 return;
1731 }
1732
linyuh183cb712017-12-27 17:02:37 -08001733 final Window window = inCallActivity.getWindow();
1734 if (screenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001735 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1736 } else {
1737 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1738 }
1739 }
1740
1741 /**
1742 * Hides or shows the conference manager fragment.
1743 *
1744 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1745 * be hidden.
1746 */
1747 public void showConferenceCallManager(boolean show) {
linyuh183cb712017-12-27 17:02:37 -08001748 if (inCallActivity != null) {
1749 inCallActivity.showConferenceFragment(show);
Eric Erfanianccca3152017-02-22 16:32:36 -08001750 }
linyuh183cb712017-12-27 17:02:37 -08001751 if (!show && manageConferenceActivity != null) {
1752 manageConferenceActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -08001753 }
1754 }
1755
1756 /**
1757 * Determines if the dialpad is visible.
1758 *
1759 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1760 */
1761 public boolean isDialpadVisible() {
linyuh183cb712017-12-27 17:02:37 -08001762 if (inCallActivity == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001763 return false;
1764 }
linyuh183cb712017-12-27 17:02:37 -08001765 return inCallActivity.isDialpadVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001766 }
1767
1768 public ThemeColorManager getThemeColorManager() {
linyuh183cb712017-12-27 17:02:37 -08001769 return themeColorManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001770 }
1771
wangqi8d662ca2017-10-26 11:27:19 -07001772 @VisibleForTesting
1773 public void setThemeColorManager(ThemeColorManager themeColorManager) {
linyuh183cb712017-12-27 17:02:37 -08001774 this.themeColorManager = themeColorManager;
wangqi8d662ca2017-10-26 11:27:19 -07001775 }
1776
Eric Erfanianccca3152017-02-22 16:32:36 -08001777 /** Called when the foreground call changes. */
1778 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh183cb712017-12-27 17:02:37 -08001779 themeColorManager.onForegroundCallChanged(context, newForegroundCall);
1780 if (inCallActivity != null) {
1781 inCallActivity.onForegroundCallChanged(newForegroundCall);
Eric Erfanianccca3152017-02-22 16:32:36 -08001782 }
1783 }
1784
1785 public InCallActivity getActivity() {
linyuh183cb712017-12-27 17:02:37 -08001786 return inCallActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -08001787 }
1788
1789 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1790 public void setActivity(InCallActivity inCallActivity) {
1791 if (inCallActivity == null) {
1792 throw new IllegalArgumentException("registerActivity cannot be called with null");
1793 }
linyuh183cb712017-12-27 17:02:37 -08001794 if (this.inCallActivity != null && this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001795 LogUtil.w(
1796 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001797 }
1798 updateActivity(inCallActivity);
1799 }
1800
1801 ExternalCallNotifier getExternalCallNotifier() {
linyuh183cb712017-12-27 17:02:37 -08001802 return externalCallNotifier;
Eric Erfanianccca3152017-02-22 16:32:36 -08001803 }
1804
1805 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001806 if (localVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001807 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001808 if (context != null) {
1809 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001810 }
linyuh183cb712017-12-27 17:02:37 -08001811 localVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001812 }
linyuh183cb712017-12-27 17:02:37 -08001813 return localVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001814 }
1815
1816 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001817 if (remoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001818 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001819 if (context != null) {
1820 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001821 }
linyuh183cb712017-12-27 17:02:37 -08001822 remoteVideoSurfaceTexture = VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001823 }
linyuh183cb712017-12-27 17:02:37 -08001824 return remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001825 }
1826
1827 void cleanupSurfaces() {
linyuh183cb712017-12-27 17:02:37 -08001828 if (remoteVideoSurfaceTexture != null) {
1829 remoteVideoSurfaceTexture.setDoneWithSurface();
1830 remoteVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001831 }
linyuh183cb712017-12-27 17:02:37 -08001832 if (localVideoSurfaceTexture != null) {
1833 localVideoSurfaceTexture.setDoneWithSurface();
1834 localVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001835 }
1836 }
1837
yueg77cb8e52017-10-27 16:42:51 -07001838 @Override
1839 public void onAudioStateChanged(CallAudioState audioState) {
yueg8e544862018-04-06 14:34:59 -07001840 // Set sensible audio route for bubble mode when we get real audio state for the first time
1841 // During the first time this function is called, supportedRouteMask is set to
1842 // SUPPORTED_AUDIO_ROUTE_ALL, but it's OK since shouldStartInBubbleMode is not set at that time.
1843 if (!audioRouteSetForBubbleMode && shouldStartInBubbleMode) {
1844 setAudioRouteForBubbleMode(audioState);
1845 audioRouteSetForBubbleMode = true;
1846 }
1847
linyuh183cb712017-12-27 17:02:37 -08001848 if (statusBarNotifier != null) {
1849 statusBarNotifier.updateNotification();
yueg77cb8e52017-10-27 16:42:51 -07001850 }
1851 }
1852
yueg8e544862018-04-06 14:34:59 -07001853 /**
1854 * Set audio route to make audio sensible. According to availability, set audio route to Bluetooth
1855 * or wired headset or speaker.
1856 */
1857 private void setAudioRouteForBubbleMode(CallAudioState audioState) {
1858 if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
1859 == CallAudioState.ROUTE_BLUETOOTH) {
1860 // Use Bluetooth if available
1861 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
1862 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "bluetooth");
1863 } else if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_WIRED_HEADSET)
1864 == CallAudioState.ROUTE_WIRED_HEADSET) {
1865 // Use wired headset if available
1866 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_WIRED_HEADSET);
1867 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "wired headset");
1868 } else {
1869 // Use speaker
1870 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_SPEAKER);
1871 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "speaker");
1872 }
1873 }
1874
Eric Erfanianccca3152017-02-22 16:32:36 -08001875 /** All the main states of InCallActivity. */
1876 public enum InCallState {
1877 // InCall Screen is off and there are no calls
1878 NO_CALLS,
1879
1880 // Incoming-call screen is up
1881 INCOMING,
1882
1883 // In-call experience is showing
1884 INCALL,
1885
1886 // Waiting for user input before placing outgoing call
1887 WAITING_FOR_ACCOUNT,
1888
1889 // UI is starting up but no call has been initiated yet.
1890 // The UI is waiting for Telecom to respond.
1891 PENDING_OUTGOING,
1892
1893 // User is dialing out
1894 OUTGOING;
1895
1896 public boolean isIncoming() {
1897 return (this == INCOMING);
1898 }
1899
1900 public boolean isConnectingOrConnected() {
1901 return (this == INCOMING || this == OUTGOING || this == INCALL);
1902 }
1903 }
1904
1905 /** Interface implemented by classes that need to know about the InCall State. */
1906 public interface InCallStateListener {
1907
1908 // TODO: Enhance state to contain the call objects instead of passing CallList
1909 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1910 }
1911
1912 public interface IncomingCallListener {
1913
1914 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1915 }
1916
1917 public interface CanAddCallListener {
1918
1919 void onCanAddCallChanged(boolean canAddCall);
1920 }
1921
1922 public interface InCallDetailsListener {
1923
1924 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1925 }
1926
1927 public interface InCallOrientationListener {
1928
1929 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1930 }
1931
1932 /**
1933 * Interface implemented by classes that need to know about events which occur within the In-Call
1934 * UI. Used as a means of communicating between fragments that make up the UI.
1935 */
1936 public interface InCallEventListener {
1937
1938 void onFullscreenModeChanged(boolean isFullscreenMode);
1939 }
1940
1941 public interface InCallUiListener {
1942
1943 void onUiShowing(boolean showing);
1944 }
twyen8efb4952017-10-06 16:35:54 -07001945
1946 private class InCallUiLockImpl implements InCallUiLock {
1947 private final String tag;
1948
1949 private InCallUiLockImpl(String tag) {
1950 this.tag = tag;
1951 }
1952
1953 @MainThread
1954 @Override
1955 public void release() {
1956 Assert.isMainThread();
1957 releaseInCallUiLock(InCallUiLockImpl.this);
1958 }
1959
1960 @Override
1961 public String toString() {
1962 return "InCallUiLock[" + tag + "]";
1963 }
1964 }
1965
1966 @MainThread
1967 public InCallUiLock acquireInCallUiLock(String tag) {
1968 Assert.isMainThread();
1969 InCallUiLock lock = new InCallUiLockImpl(tag);
linyuh183cb712017-12-27 17:02:37 -08001970 inCallUiLocks.add(lock);
twyen8efb4952017-10-06 16:35:54 -07001971 return lock;
1972 }
1973
1974 @MainThread
1975 private void releaseInCallUiLock(InCallUiLock lock) {
1976 Assert.isMainThread();
1977 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
linyuh183cb712017-12-27 17:02:37 -08001978 inCallUiLocks.remove(lock);
1979 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07001980 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
linyuh183cb712017-12-27 17:02:37 -08001981 if (inCallState == InCallState.NO_CALLS) {
twyen8efb4952017-10-06 16:35:54 -07001982 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
1983 attemptFinishActivity();
1984 attemptCleanup();
1985 }
1986 }
1987 }
1988
1989 @MainThread
1990 public boolean isInCallUiLocked() {
1991 Assert.isMainThread();
linyuh183cb712017-12-27 17:02:37 -08001992 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07001993 return false;
1994 }
linyuh183cb712017-12-27 17:02:37 -08001995 for (InCallUiLock lock : inCallUiLocks) {
twyen8efb4952017-10-06 16:35:54 -07001996 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
1997 }
1998 return true;
1999 }
2000
linyuh183cb712017-12-27 17:02:37 -08002001 private final Set<InCallUiLock> inCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08002002}