blob: 526cc64d3d7cf3e8e45678293f188f2d4c7e1423 [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;
yuegb47528e2018-04-24 12:12:57 -070074import com.android.incallui.telecomeventui.InternationalCallOnWifiDialogActivity;
75import com.android.incallui.telecomeventui.InternationalCallOnWifiDialogFragment;
Eric Erfanianccca3152017-02-22 16:32:36 -080076import com.android.incallui.videosurface.bindings.VideoSurfaceBindings;
77import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
Eric Erfanian90508232017-03-24 09:31:16 -070078import com.android.incallui.videotech.utils.VideoUtils;
yueg32d9ae72018-04-05 17:35:56 -070079import com.google.protobuf.InvalidProtocolBufferException;
Eric Erfanianccca3152017-02-22 16:32:36 -080080import java.util.Collections;
81import java.util.List;
82import java.util.Objects;
83import java.util.Set;
84import java.util.concurrent.ConcurrentHashMap;
85import java.util.concurrent.CopyOnWriteArrayList;
86import java.util.concurrent.atomic.AtomicBoolean;
87
88/**
89 * Takes updates from the CallList and notifies the InCallActivity (UI) of the changes. Responsible
90 * for starting the activity for a new call and finishing the activity when all calls are
91 * disconnected. Creates and manages the in-call state and provides a listener pattern for the
92 * presenters that want to listen in on the in-call state changes. TODO: This class has become more
93 * of a state machine at this point. Consider renaming.
94 */
yueg77cb8e52017-10-27 16:42:51 -070095public class InCallPresenter implements CallList.Listener, AudioModeProvider.AudioModeListener {
Eric Erfanian2ca43182017-08-31 06:57:16 -070096 private static final String PIXEL2017_SYSTEM_FEATURE =
97 "com.google.android.feature.PIXEL_2017_EXPERIENCE";
yueg32d9ae72018-04-05 17:35:56 -070098 private static final String CALL_CONFIGURATION_EXTRA = "call_configuration";
Eric Erfanianccca3152017-02-22 16:32:36 -080099
100 private static final long BLOCK_QUERY_TIMEOUT_MS = 1000;
101
102 private static final Bundle EMPTY_EXTRAS = new Bundle();
103
linyuh183cb712017-12-27 17:02:37 -0800104 private static InCallPresenter inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -0800105
106 /**
107 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is load factor before
108 * resizing, 1 means we only expect a single thread to access the map so make only a single shard
109 */
linyuh183cb712017-12-27 17:02:37 -0800110 private final Set<InCallStateListener> listeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800111 Collections.newSetFromMap(new ConcurrentHashMap<InCallStateListener, Boolean>(8, 0.9f, 1));
112
linyuh183cb712017-12-27 17:02:37 -0800113 private final List<IncomingCallListener> incomingCallListeners = new CopyOnWriteArrayList<>();
114 private final Set<InCallDetailsListener> detailsListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800115 Collections.newSetFromMap(new ConcurrentHashMap<InCallDetailsListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800116 private final Set<CanAddCallListener> canAddCallListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800117 Collections.newSetFromMap(new ConcurrentHashMap<CanAddCallListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800118 private final Set<InCallUiListener> inCallUiListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800119 Collections.newSetFromMap(new ConcurrentHashMap<InCallUiListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800120 private final Set<InCallOrientationListener> orientationListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800121 Collections.newSetFromMap(
122 new ConcurrentHashMap<InCallOrientationListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800123 private final Set<InCallEventListener> inCallEventListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800124 Collections.newSetFromMap(new ConcurrentHashMap<InCallEventListener, Boolean>(8, 0.9f, 1));
125
linyuh183cb712017-12-27 17:02:37 -0800126 private StatusBarNotifier statusBarNotifier;
127 private ExternalCallNotifier externalCallNotifier;
128 private ContactInfoCache contactInfoCache;
129 private Context context;
130 private final OnCheckBlockedListener onCheckBlockedListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800131 new OnCheckBlockedListener() {
132 @Override
133 public void onCheckComplete(final Integer id) {
134 if (id != null && id != FilteredNumberAsyncQueryHandler.INVALID_ID) {
135 // Silence the ringer now to prevent ringing and vibration before the call is
136 // terminated when Telecom attempts to add it.
linyuh183cb712017-12-27 17:02:37 -0800137 TelecomUtil.silenceRinger(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800138 }
139 }
140 };
linyuh183cb712017-12-27 17:02:37 -0800141 private CallList callList;
142 private ExternalCallList externalCallList;
143 private InCallActivity inCallActivity;
144 private ManageConferenceActivity manageConferenceActivity;
145 private final android.telecom.Call.Callback callCallback =
Eric Erfanianccca3152017-02-22 16:32:36 -0800146 new android.telecom.Call.Callback() {
147 @Override
148 public void onPostDialWait(
149 android.telecom.Call telecomCall, String remainingPostDialSequence) {
linyuh183cb712017-12-27 17:02:37 -0800150 final DialerCall call = callList.getDialerCallFromTelecomCall(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800151 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700152 LogUtil.w(
153 "InCallPresenter.onPostDialWait",
154 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800155 return;
156 }
157 onPostDialCharWait(call.getId(), remainingPostDialSequence);
158 }
159
160 @Override
161 public void onDetailsChanged(
162 android.telecom.Call telecomCall, android.telecom.Call.Details details) {
linyuh183cb712017-12-27 17:02:37 -0800163 final DialerCall call = callList.getDialerCallFromTelecomCall(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800164 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700165 LogUtil.w(
166 "InCallPresenter.onDetailsChanged",
167 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800168 return;
169 }
170
171 if (details.hasProperty(Details.PROPERTY_IS_EXTERNAL_CALL)
linyuh183cb712017-12-27 17:02:37 -0800172 && !externalCallList.isCallTracked(telecomCall)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800173
174 // A regular call became an external call so swap call lists.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700175 LogUtil.i("InCallPresenter.onDetailsChanged", "Call became external: " + telecomCall);
linyuh183cb712017-12-27 17:02:37 -0800176 callList.onInternalCallMadeExternal(context, telecomCall);
177 externalCallList.onCallAdded(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800178 return;
179 }
180
linyuh183cb712017-12-27 17:02:37 -0800181 for (InCallDetailsListener listener : detailsListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800182 listener.onDetailsChanged(call, details);
183 }
184 }
185
186 @Override
187 public void onConferenceableCallsChanged(
188 android.telecom.Call telecomCall, List<android.telecom.Call> conferenceableCalls) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700189 LogUtil.i(
190 "InCallPresenter.onConferenceableCallsChanged",
191 "onConferenceableCallsChanged: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800192 onDetailsChanged(telecomCall, telecomCall.getDetails());
193 }
194 };
linyuh183cb712017-12-27 17:02:37 -0800195 private InCallState inCallState = InCallState.NO_CALLS;
196 private ProximitySensor proximitySensor;
197 private final PseudoScreenState pseudoScreenState = new PseudoScreenState();
198 private boolean serviceConnected;
199 private InCallCameraManager inCallCameraManager;
200 private FilteredNumberAsyncQueryHandler filteredQueryHandler;
201 private CallList.Listener spamCallListListener;
twyen27c37182018-04-24 11:48:12 -0700202 private CallList.Listener activeCallsListener;
Eric Erfanianccca3152017-02-22 16:32:36 -0800203 /** Whether or not we are currently bound and waiting for Telecom to send us a new call. */
linyuh183cb712017-12-27 17:02:37 -0800204 private boolean boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -0800205 /** Determines if the InCall UI is in fullscreen mode or not. */
linyuh183cb712017-12-27 17:02:37 -0800206 private boolean isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800207
linyuh183cb712017-12-27 17:02:37 -0800208 private boolean screenTimeoutEnabled = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700209
linyuh183cb712017-12-27 17:02:37 -0800210 private PhoneStateListener phoneStateListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800211 new PhoneStateListener() {
212 @Override
213 public void onCallStateChanged(int state, String incomingNumber) {
214 if (state == TelephonyManager.CALL_STATE_RINGING) {
linyuh183cb712017-12-27 17:02:37 -0800215 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800216 return;
217 }
218 // Check if the number is blocked, to silence the ringer.
linyuh183cb712017-12-27 17:02:37 -0800219 String countryIso = GeoUtil.getCurrentCountryIso(context);
220 filteredQueryHandler.isBlockedNumber(
221 onCheckBlockedListener, incomingNumber, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800222 }
223 }
224 };
twyena4745bd2017-12-12 18:40:11 -0800225
Eric Erfanianccca3152017-02-22 16:32:36 -0800226 /** Whether or not InCallService is bound to Telecom. */
linyuh183cb712017-12-27 17:02:37 -0800227 private boolean serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800228
229 /**
230 * When configuration changes Android kills the current activity and starts a new one. The flag is
231 * used to check if full clean up is necessary (activity is stopped and new activity won't be
232 * started), or if a new activity will be started right after the current one is destroyed, and
233 * therefore no need in release all resources.
234 */
linyuh183cb712017-12-27 17:02:37 -0800235 private boolean isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800236
linyuh183cb712017-12-27 17:02:37 -0800237 private boolean awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800238
linyuh183cb712017-12-27 17:02:37 -0800239 private ExternalCallList.ExternalCallListener externalCallListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800240 new ExternalCallList.ExternalCallListener() {
241
242 @Override
243 public void onExternalCallPulled(android.telecom.Call call) {
244 // Note: keep this code in sync with InCallPresenter#onCallAdded
245 LatencyReport latencyReport = new LatencyReport(call);
246 latencyReport.onCallBlockingDone();
247 // Note: External calls do not require spam checking.
linyuh183cb712017-12-27 17:02:37 -0800248 callList.onCallAdded(context, call, latencyReport);
249 call.registerCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800250 }
251
252 @Override
253 public void onExternalCallAdded(android.telecom.Call call) {
254 // No-op
255 }
256
257 @Override
258 public void onExternalCallRemoved(android.telecom.Call call) {
259 // No-op
260 }
261
262 @Override
263 public void onExternalCallUpdated(android.telecom.Call call) {
264 // No-op
265 }
266 };
267
linyuh183cb712017-12-27 17:02:37 -0800268 private ThemeColorManager themeColorManager;
269 private VideoSurfaceTexture localVideoSurfaceTexture;
270 private VideoSurfaceTexture remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -0800271
erfaniand05d8992018-03-20 19:42:26 -0700272 private SpeakEasyCallManager speakEasyCallManager;
273
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,
yueg4613e8f2018-04-23 13:35:33 -0700341 @NonNull SpeakEasyCallManager speakEasyCallManager) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700342 Trace.beginSection("InCallPresenter.setUp");
linyuh183cb712017-12-27 17:02:37 -0800343 if (serviceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700344 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
linyuh183cb712017-12-27 17:02:37 -0800345 if (context != this.context || callList != this.callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800346 throw new IllegalStateException();
347 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700348 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800349 return;
350 }
351
352 Objects.requireNonNull(context);
linyuh183cb712017-12-27 17:02:37 -0800353 this.context = context;
Eric Erfanianccca3152017-02-22 16:32:36 -0800354
linyuh183cb712017-12-27 17:02:37 -0800355 this.contactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800356
linyuh183cb712017-12-27 17:02:37 -0800357 this.statusBarNotifier = statusBarNotifier;
358 this.externalCallNotifier = externalCallNotifier;
359 addListener(this.statusBarNotifier);
360 EnrichedCallComponent.get(this.context)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700361 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -0800362 .registerStateChangedListener(this.statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800363
linyuh183cb712017-12-27 17:02:37 -0800364 this.proximitySensor = proximitySensor;
365 addListener(this.proximitySensor);
Eric Erfanianccca3152017-02-22 16:32:36 -0800366
linyuh183cb712017-12-27 17:02:37 -0800367 if (themeColorManager == null) {
368 themeColorManager =
369 new ThemeColorManager(new InCallUIMaterialColorMapUtils(this.context.getResources()));
wangqi8d662ca2017-10-26 11:27:19 -0700370 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800371
linyuh183cb712017-12-27 17:02:37 -0800372 this.callList = callList;
373 this.externalCallList = externalCallList;
374 externalCallList.addExternalCallListener(this.externalCallNotifier);
375 externalCallList.addExternalCallListener(externalCallListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800376
377 // This only gets called by the service so this is okay.
linyuh183cb712017-12-27 17:02:37 -0800378 serviceConnected = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800379
380 // The final thing we do in this set up is add ourselves as a listener to CallList. This
381 // will kick off an update and the whole process can start.
linyuh183cb712017-12-27 17:02:37 -0800382 this.callList.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800383
384 // Create spam call list listener and add it to the list of listeners
linyuh183cb712017-12-27 17:02:37 -0800385 spamCallListListener =
zachh6a4cebd2017-10-24 17:10:06 -0700386 new SpamCallListListener(
387 context, DialerExecutorComponent.get(context).dialerExecutorFactory());
linyuh183cb712017-12-27 17:02:37 -0800388 this.callList.addListener(spamCallListListener);
twyen27c37182018-04-24 11:48:12 -0700389 activeCallsListener = new ActiveCallsCallListListener(context);
390 this.callList.addListener(activeCallsListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800391
392 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800393
linyuh183cb712017-12-27 17:02:37 -0800394 filteredQueryHandler = filteredNumberQueryHandler;
erfaniand05d8992018-03-20 19:42:26 -0700395 this.speakEasyCallManager = speakEasyCallManager;
linyuh183cb712017-12-27 17:02:37 -0800396 this.context
Eric Erfanianccca3152017-02-22 16:32:36 -0800397 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800398 .listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800399
yueg77cb8e52017-10-27 16:42:51 -0700400 AudioModeProvider.getInstance().addListener(this);
401
roldenburg42099012018-04-09 14:47:15 -0700402 // 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 motorolaInCallUiNotifier = new MotorolaInCallUiNotifier(context);
406 addInCallUiListener(motorolaInCallUiNotifier);
407 addListener(motorolaInCallUiNotifier);
yueg7f5acbe2018-01-10 13:50:29 -0800408
Eric Erfanian2ca43182017-08-31 06:57:16 -0700409 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
410 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800411 }
412
413 /**
yueg32d9ae72018-04-05 17:35:56 -0700414 * Return whether we should start call in bubble mode and not show InCallActivity. The call mode
415 * should be set in CallConfiguration in EXTRA_OUTGOING_CALL_EXTRAS when starting a call intent.
416 */
yueg4613e8f2018-04-23 13:35:33 -0700417 private boolean shouldStartInBubbleMode() {
yueg32d9ae72018-04-05 17:35:56 -0700418 if (!ReturnToCallController.isEnabled(context)) {
419 return false;
420 }
yueg4613e8f2018-04-23 13:35:33 -0700421
422 // We only start in Bubble mode for outgoing call
423 DialerCall dialerCall = callList.getPendingOutgoingCall();
424 if (dialerCall == null) {
425 dialerCall = callList.getOutgoingCall();
426 }
427 if (dialerCall == null) {
yueg32d9ae72018-04-05 17:35:56 -0700428 return false;
429 }
yueg4613e8f2018-04-23 13:35:33 -0700430
431 Bundle extras = dialerCall.getIntentExtras();
432 return shouldStartInBubbleModeWithExtras(extras);
433 }
434
435 private boolean shouldStartInBubbleModeWithExtras(Bundle outgoingExtras) {
436 if (!ReturnToCallController.isEnabled(context)) {
437 return false;
438 }
439
440 if (outgoingExtras == null) {
441 return false;
442 }
443 byte[] callConfigurationByteArray = outgoingExtras.getByteArray(CALL_CONFIGURATION_EXTRA);
yueg32d9ae72018-04-05 17:35:56 -0700444 if (callConfigurationByteArray == null) {
445 return false;
446 }
447 try {
448 CallConfiguration callConfiguration = CallConfiguration.parseFrom(callConfigurationByteArray);
449 LogUtil.i(
450 "InCallPresenter.shouldStartInBubbleMode",
451 "call mode: " + callConfiguration.getCallMode());
452 return callConfiguration.getCallMode() == Mode.BUBBLE;
453 } catch (InvalidProtocolBufferException e) {
454 return false;
455 }
456 }
457
458 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800459 * Called when the telephony service has disconnected from us. This will happen when there are no
460 * more active calls. However, we may still want to continue showing the UI for certain cases like
461 * showing "Call Ended". What we really want is to wait for the activity and the service to both
462 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
463 * secondary method that performs the aforementioned logic.
464 */
465 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700466 LogUtil.d("InCallPresenter.tearDown", "tearDown");
linyuh183cb712017-12-27 17:02:37 -0800467 callList.clearOnDisconnect();
Eric Erfanianccca3152017-02-22 16:32:36 -0800468
linyuh183cb712017-12-27 17:02:37 -0800469 serviceConnected = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800470
linyuh183cb712017-12-27 17:02:37 -0800471 context
Eric Erfanianccca3152017-02-22 16:32:36 -0800472 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800473 .listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800474
475 attemptCleanup();
476 VideoPauseController.getInstance().tearDown();
yueg77cb8e52017-10-27 16:42:51 -0700477 AudioModeProvider.getInstance().removeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800478 }
479
480 private void attemptFinishActivity() {
linyuh183cb712017-12-27 17:02:37 -0800481 screenTimeoutEnabled = true;
482 final boolean doFinish = (inCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700483 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800484 if (doFinish) {
linyuh183cb712017-12-27 17:02:37 -0800485 inCallActivity.setExcludeFromRecents(true);
486 inCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800487 }
488 }
489
490 /**
491 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
492 * for more insight on the tear-down process.
493 */
494 public void unsetActivity(InCallActivity inCallActivity) {
495 if (inCallActivity == null) {
496 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
497 }
linyuh183cb712017-12-27 17:02:37 -0800498 if (this.inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700499 LogUtil.i(
500 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800501 return;
502 }
linyuh183cb712017-12-27 17:02:37 -0800503 if (this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700504 LogUtil.w(
505 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800506 "Second instance of InCallActivity is trying to unregister when another"
507 + " instance is active. Ignoring.");
508 return;
509 }
510 updateActivity(null);
511 }
512
513 /**
514 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
515 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
516 */
517 private void updateActivity(InCallActivity inCallActivity) {
wangqi9982f0d2017-10-11 17:46:07 -0700518 Trace.beginSection("InCallPresenter.updateActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800519 boolean updateListeners = false;
520 boolean doAttemptCleanup = false;
521
522 if (inCallActivity != null) {
linyuh183cb712017-12-27 17:02:37 -0800523 if (this.inCallActivity == null) {
524 context = inCallActivity.getApplicationContext();
Eric Erfanianccca3152017-02-22 16:32:36 -0800525 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700526 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800527 } else {
528 // since setActivity is called onStart(), it can be called multiple times.
529 // This is fine and ignorable, but we do not want to update the world every time
530 // this happens (like going to/from background) so we do not set updateListeners.
531 }
532
linyuh183cb712017-12-27 17:02:37 -0800533 this.inCallActivity = inCallActivity;
534 this.inCallActivity.setExcludeFromRecents(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800535
536 // By the time the UI finally comes up, the call may already be disconnected.
537 // If that's the case, we may need to show an error dialog.
linyuh183cb712017-12-27 17:02:37 -0800538 if (callList != null && callList.getDisconnectedCall() != null) {
539 showDialogOrToastForDisconnectedCall(callList.getDisconnectedCall());
Eric Erfanianccca3152017-02-22 16:32:36 -0800540 }
541
542 // When the UI comes up, we need to first check the in-call state.
543 // If we are showing NO_CALLS, that means that a call probably connected and
544 // then immediately disconnected before the UI was able to come up.
545 // If we dont have any calls, start tearing down the UI instead.
546 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
547 // it has been set.
linyuh183cb712017-12-27 17:02:37 -0800548 if (inCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700549 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800550 attemptFinishActivity();
wangqi9982f0d2017-10-11 17:46:07 -0700551 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800552 return;
553 }
554 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700555 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800556 updateListeners = true;
linyuh183cb712017-12-27 17:02:37 -0800557 this.inCallActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800558
559 // We attempt cleanup for the destroy case but only after we recalculate the state
560 // to see if we need to come back up or stay shut down. This is why we do the
561 // cleanup after the call to onCallListChange() instead of directly here.
562 doAttemptCleanup = true;
563 }
564
565 // Messages can come from the telephony layer while the activity is coming up
566 // and while the activity is going down. So in both cases we need to recalculate what
567 // state we should be in after they complete.
568 // Examples: (1) A new incoming call could come in and then get disconnected before
569 // the activity is created.
570 // (2) All calls could disconnect and then get a new incoming call before the
571 // activity is destroyed.
572 //
Eric Erfanian938468d2017-10-24 14:05:52 -0700573 // a bug - We previously had a check for mServiceConnected here as well, but there are
Eric Erfanianccca3152017-02-22 16:32:36 -0800574 // cases where we need to recalculate the current state even if the service in not
575 // connected. In particular the case where startOrFinish() is called while the app is
576 // already finish()ing. In that case, we skip updating the state with the knowledge that
577 // we will check again once the activity has finished. That means we have to recalculate the
578 // state here even if the service is disconnected since we may not have finished a state
579 // transition while finish()ing.
580 if (updateListeners) {
linyuh183cb712017-12-27 17:02:37 -0800581 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800582 }
583
584 if (doAttemptCleanup) {
585 attemptCleanup();
586 }
wangqi9982f0d2017-10-11 17:46:07 -0700587 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800588 }
589
erfanian12243de2018-04-17 12:17:08 -0700590 public SpeakEasyCallManager getSpeakEasyCallManager() {
591 return this.speakEasyCallManager;
592 }
593
Eric Erfanianccca3152017-02-22 16:32:36 -0800594 public void setManageConferenceActivity(
595 @Nullable ManageConferenceActivity manageConferenceActivity) {
linyuh183cb712017-12-27 17:02:37 -0800596 this.manageConferenceActivity = manageConferenceActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -0800597 }
598
599 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700600 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800601 bringToForeground(showDialpad);
602 }
603
604 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700605 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800606 LatencyReport latencyReport = new LatencyReport(call);
607 if (shouldAttemptBlocking(call)) {
608 maybeBlockCall(call, latencyReport);
609 } else {
610 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800611 externalCallList.onCallAdded(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800612 } else {
613 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800614 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800615 }
616 }
617
618 // Since a call has been added we are no longer waiting for Telecom to send us a call.
619 setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -0800620 call.registerCallback(callCallback);
zachh78e54ac2017-12-05 16:38:35 -0800621 // TODO(maxwelb): Return the future in recordPhoneLookupInfo and propagate.
linyuh183cb712017-12-27 17:02:37 -0800622 PhoneLookupHistoryRecorder.recordPhoneLookupInfo(context.getApplicationContext(), call);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700623 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800624 }
625
626 private boolean shouldAttemptBlocking(android.telecom.Call call) {
627 if (call.getState() != android.telecom.Call.STATE_RINGING) {
628 return false;
629 }
linyuh183cb712017-12-27 17:02:37 -0800630 if (!UserManagerCompat.isUserUnlocked(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700631 LogUtil.i(
632 "InCallPresenter.shouldAttemptBlocking",
633 "not attempting to block incoming call because user is locked");
634 return false;
635 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800636 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700637 LogUtil.i(
638 "InCallPresenter.shouldAttemptBlocking",
639 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800640 return false;
641 }
linyuh183cb712017-12-27 17:02:37 -0800642 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700643 LogUtil.i(
644 "InCallPresenter.shouldAttemptBlocking",
645 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800646 return false;
647 }
648 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
649 return false;
650 }
linyuh183cb712017-12-27 17:02:37 -0800651 if (FilteredNumberCompat.useNewFiltering(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700652 LogUtil.i(
653 "InCallPresenter.shouldAttemptBlocking",
654 "not attempting to block incoming call because framework blocking is in use");
655 return false;
656 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800657 return true;
658 }
659
660 /**
661 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
662 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
663 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
664 * call anyways.
665 */
666 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
linyuh183cb712017-12-27 17:02:37 -0800667 final String countryIso = GeoUtil.getCurrentCountryIso(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800668 final String number = TelecomCallUtil.getNumber(call);
669 final long timeAdded = System.currentTimeMillis();
670
671 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
672 // main UI thread. It is needed so we can change its value within different scopes, since
673 // that cannot be done with a final boolean.
674 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
675
676 final Handler handler = new Handler();
677
678 // Proceed if the query is slow; the call may still be blocked after the query returns.
679 final Runnable runnable =
680 new Runnable() {
681 @Override
682 public void run() {
683 hasTimedOut.set(true);
684 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800685 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800686 }
687 };
688 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
689
690 OnCheckBlockedListener onCheckBlockedListener =
691 new OnCheckBlockedListener() {
692 @Override
693 public void onCheckComplete(final Integer id) {
694 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700695 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800696 return;
697 }
698 if (!hasTimedOut.get()) {
699 handler.removeCallbacks(runnable);
700 }
701 if (id == null) {
702 if (!hasTimedOut.get()) {
703 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800704 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800705 }
706 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700707 LogUtil.d(
708 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800709 if (!hasTimedOut.get()) {
710 handler.removeCallbacks(runnable);
711
712 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800713 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800714 }
715 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700716 LogUtil.i(
717 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800718 call.reject(false, null);
linyuh183cb712017-12-27 17:02:37 -0800719 Logger.get(context).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
Eric Erfanianccca3152017-02-22 16:32:36 -0800720
721 /*
722 * If mContext is null, then the InCallPresenter was torn down before the
723 * block check had a chance to complete. The context is no longer valid, so
724 * don't attempt to remove the call log entry.
725 */
linyuh183cb712017-12-27 17:02:37 -0800726 if (context == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800727 return;
728 }
729 // Register observer to update the call log.
730 // BlockedNumberContentObserver will unregister after successful log or timeout.
731 BlockedNumberContentObserver contentObserver =
linyuh183cb712017-12-27 17:02:37 -0800732 new BlockedNumberContentObserver(context, new Handler(), number, timeAdded);
Eric Erfanianccca3152017-02-22 16:32:36 -0800733 contentObserver.register();
734 }
735 }
736 };
737
linyuh183cb712017-12-27 17:02:37 -0800738 filteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800739 }
740
741 public void onCallRemoved(android.telecom.Call call) {
742 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800743 externalCallList.onCallRemoved(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800744 } else {
linyuh183cb712017-12-27 17:02:37 -0800745 callList.onCallRemoved(context, call);
746 call.unregisterCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800747 }
748 }
749
750 public void onCanAddCallChanged(boolean canAddCall) {
linyuh183cb712017-12-27 17:02:37 -0800751 for (CanAddCallListener listener : canAddCallListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800752 listener.onCanAddCallChanged(canAddCall);
753 }
754 }
755
756 @Override
757 public void onWiFiToLteHandover(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -0700758 if (call.hasShownWiFiToLteHandoverToast()) {
759 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800760 }
yueg32d9ae72018-04-05 17:35:56 -0700761
762 Toast.makeText(context, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG)
763 .show();
764 call.setHasShownWiFiToLteHandoverToast();
Eric Erfanianccca3152017-02-22 16:32:36 -0800765 }
766
767 @Override
768 public void onHandoverToWifiFailed(DialerCall call) {
linyuh183cb712017-12-27 17:02:37 -0800769 if (inCallActivity != null) {
770 inCallActivity.showDialogOrToastForWifiHandoverFailure(call);
yueg32d9ae72018-04-05 17:35:56 -0700771 } else {
772 Toast.makeText(context, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT)
773 .show();
Eric Erfanianccca3152017-02-22 16:32:36 -0800774 }
775 }
776
Eric Erfanianc857f902017-05-15 14:05:33 -0700777 @Override
778 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
779 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
yuegb47528e2018-04-24 12:12:57 -0700780
781 if (!InternationalCallOnWifiDialogFragment.shouldShow(context)) {
782 LogUtil.i(
783 "InCallPresenter.onInternationalCallOnWifi",
784 "InternationalCallOnWifiDialogFragment.shouldShow returned false");
785 return;
786 }
787
linyuh183cb712017-12-27 17:02:37 -0800788 if (inCallActivity != null) {
789 inCallActivity.showDialogForInternationalCallOnWifi(call);
yuegb47528e2018-04-24 12:12:57 -0700790 } else {
791 Intent intent = new Intent(context, InternationalCallOnWifiDialogActivity.class);
792 // Prevent showing MainActivity with InternationalCallOnWifiDialogActivity on above
793 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
794 intent.putExtra(InternationalCallOnWifiDialogActivity.EXTRA_CALL_ID, call.getId());
795 context.startActivity(intent);
Eric Erfanianc857f902017-05-15 14:05:33 -0700796 }
797 }
798
Eric Erfanianccca3152017-02-22 16:32:36 -0800799 /**
800 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
801 * app based on the information it gets from CallList. Dispatches the in-call state to all
802 * listeners. Can trigger the creation or destruction of the UI based on the states that is
803 * calculates.
804 */
805 @Override
806 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700807 Trace.beginSection("InCallPresenter.onCallListChange");
linyuh183cb712017-12-27 17:02:37 -0800808 if (inCallActivity != null && inCallActivity.isInCallScreenAnimating()) {
809 awaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700810 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800811 return;
812 }
813 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700814 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800815 return;
816 }
817
linyuh183cb712017-12-27 17:02:37 -0800818 awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800819
820 InCallState newState = getPotentialStateFromCallList(callList);
linyuh183cb712017-12-27 17:02:37 -0800821 InCallState oldState = inCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700822 LogUtil.d(
823 "InCallPresenter.onCallListChange",
824 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700825
826 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
827 // incall activity for that call will still exist (even if it's not visible). In the case of
828 // an incoming call in that situation, just disconnect that "waiting for account" call and
829 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
Eric Erfanian938468d2017-10-24 14:05:52 -0700830 // a bug for more details.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700831 DialerCall waitingForAccountCall;
832 if (newState == InCallState.INCOMING
833 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
834 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700835 // The InCallActivity might be destroyed or not started yet at this point.
836 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800837 inCallActivity.dismissPendingDialogs();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700838 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700839 }
840
Eric Erfanianccca3152017-02-22 16:32:36 -0800841 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700842 LogUtil.d(
843 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800844
845 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700846 LogUtil.i(
847 "InCallPresenter.onCallListChange",
848 "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800849 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800850
yuegb9103042018-03-30 12:12:25 -0700851 // Foreground call changed
852 DialerCall primary = null;
853 if (newState == InCallState.INCOMING) {
854 primary = callList.getIncomingCall();
855 } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
856 primary = callList.getOutgoingCall();
857 if (primary == null) {
858 primary = callList.getPendingOutgoingCall();
859 }
860 } else if (newState == InCallState.INCALL) {
861 primary = getCallToDisplay(callList, null, false);
862 }
863 if (primary != null) {
864 onForegroundCallChanged(primary);
865 }
866
Eric Erfanianccca3152017-02-22 16:32:36 -0800867 // notify listeners of new state
linyuh183cb712017-12-27 17:02:37 -0800868 for (InCallStateListener listener : listeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700869 LogUtil.d(
870 "InCallPresenter.onCallListChange",
linyuh183cb712017-12-27 17:02:37 -0800871 "Notify " + listener + " of state " + inCallState.toString());
872 listener.onStateChange(oldState, inCallState, callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800873 }
874
875 if (isActivityStarted()) {
876 final boolean hasCall =
877 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
linyuh183cb712017-12-27 17:02:37 -0800878 inCallActivity.dismissKeyguard(hasCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800879 }
twyen27c37182018-04-24 11:48:12 -0700880
Eric Erfanian2ca43182017-08-31 06:57:16 -0700881 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800882 }
883
yuegb9103042018-03-30 12:12:25 -0700884 /**
885 * Get the highest priority call to display. Goes through the calls and chooses which to return
886 * based on priority of which type of call to display to the user. Callers can use the "ignore"
887 * feature to get the second best call by passing a previously found primary call as ignore.
888 *
889 * @param ignore A call to ignore if found.
890 */
891 static DialerCall getCallToDisplay(
892 CallList callList, DialerCall ignore, boolean skipDisconnected) {
893 // Active calls come second. An active call always gets precedent.
894 DialerCall retval = callList.getActiveCall();
895 if (retval != null && retval != ignore) {
896 return retval;
897 }
898
899 // Sometimes there is intemediate state that two calls are in active even one is about
900 // to be on hold.
901 retval = callList.getSecondActiveCall();
902 if (retval != null && retval != ignore) {
903 return retval;
904 }
905
906 // Disconnected calls get primary position if there are no active calls
907 // to let user know quickly what call has disconnected. Disconnected
908 // calls are very short lived.
909 if (!skipDisconnected) {
910 retval = callList.getDisconnectingCall();
911 if (retval != null && retval != ignore) {
912 return retval;
913 }
914 retval = callList.getDisconnectedCall();
915 if (retval != null && retval != ignore) {
916 return retval;
917 }
918 }
919
920 // Then we go to background call (calls on hold)
921 retval = callList.getBackgroundCall();
922 if (retval != null && retval != ignore) {
923 return retval;
924 }
925
926 // Lastly, we go to a second background call.
927 retval = callList.getSecondBackgroundCall();
928
929 return retval;
930 }
931
Eric Erfanianccca3152017-02-22 16:32:36 -0800932 /** Called when there is a new incoming call. */
933 @Override
934 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700935 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800936 InCallState newState = startOrFinishUi(InCallState.INCOMING);
linyuh183cb712017-12-27 17:02:37 -0800937 InCallState oldState = inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800938
Eric Erfanian2ca43182017-08-31 06:57:16 -0700939 LogUtil.i(
940 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800941 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800942
wangqicf61ca02017-08-31 15:32:55 -0700943 Trace.beginSection("listener.onIncomingCall");
linyuh183cb712017-12-27 17:02:37 -0800944 for (IncomingCallListener listener : incomingCallListeners) {
945 listener.onIncomingCall(oldState, inCallState, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800946 }
wangqicf61ca02017-08-31 15:32:55 -0700947 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800948
wangqicf61ca02017-08-31 15:32:55 -0700949 Trace.beginSection("onPrimaryCallStateChanged");
linyuh183cb712017-12-27 17:02:37 -0800950 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800951 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800952 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800953 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700954 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700955 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800956 }
957
958 @Override
959 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700960 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
linyuh183cb712017-12-27 17:02:37 -0800961 && inCallState == InCallPresenter.InCallState.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800962 LogUtil.i(
963 "InCallPresenter.onUpgradeToVideo",
964 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700965 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800966 }
967
linyuh183cb712017-12-27 17:02:37 -0800968 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800969 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800970 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800971 }
972 }
973
974 @Override
wangqibc28ea72018-04-02 16:23:00 -0700975 public void onUpgradeToRtt(DialerCall call, int rttRequestId) {
976 if (inCallActivity != null) {
977 inCallActivity.showDialogForRttRequest(call, rttRequestId);
978 }
979 }
980
981 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700982 public void onSessionModificationStateChange(DialerCall call) {
983 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800984 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
linyuh183cb712017-12-27 17:02:37 -0800985 if (proximitySensor == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800986 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
987 return;
988 }
linyuh183cb712017-12-27 17:02:37 -0800989 proximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700990 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
linyuh183cb712017-12-27 17:02:37 -0800991 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800992 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800993 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800994 }
995 }
996
997 /**
998 * Called when a call becomes disconnected. Called everytime an existing call changes from being
999 * connected (incoming/outgoing/active) to disconnected.
1000 */
1001 @Override
1002 public void onDisconnect(DialerCall call) {
linyuh7b86f562017-11-16 11:24:09 -08001003 showDialogOrToastForDisconnectedCall(call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001004
1005 // We need to do the run the same code as onCallListChange.
linyuh183cb712017-12-27 17:02:37 -08001006 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001007
1008 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001009 inCallActivity.dismissKeyguard(false);
Eric Erfanianccca3152017-02-22 16:32:36 -08001010 }
1011
1012 if (call.isEmergencyCall()) {
linyuh183cb712017-12-27 17:02:37 -08001013 FilteredNumbersUtil.recordLastEmergencyCallTime(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001014 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001015
linyuh183cb712017-12-27 17:02:37 -08001016 if (!callList.hasLiveCall()
Eric Erfanianfc37b022017-03-21 10:11:17 -07001017 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -07001018 && !isSecretCode(call.getNumber())
wangqi9982f0d2017-10-11 17:46:07 -07001019 && !call.isVoiceMailNumber()) {
linyuh183cb712017-12-27 17:02:37 -08001020 PostCall.onCallDisconnected(context, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -08001021 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001022 }
1023
Eric Erfanian10b34a52017-05-04 08:23:17 -07001024 private boolean isSecretCode(@Nullable String number) {
1025 return number != null
1026 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
1027 }
1028
Eric Erfanianccca3152017-02-22 16:32:36 -08001029 /** Given the call list, return the state in which the in-call screen should be. */
1030 public InCallState getPotentialStateFromCallList(CallList callList) {
1031
1032 InCallState newState = InCallState.NO_CALLS;
1033
1034 if (callList == null) {
1035 return newState;
1036 }
1037 if (callList.getIncomingCall() != null) {
1038 newState = InCallState.INCOMING;
1039 } else if (callList.getWaitingForAccountCall() != null) {
1040 newState = InCallState.WAITING_FOR_ACCOUNT;
1041 } else if (callList.getPendingOutgoingCall() != null) {
1042 newState = InCallState.PENDING_OUTGOING;
1043 } else if (callList.getOutgoingCall() != null) {
1044 newState = InCallState.OUTGOING;
1045 } else if (callList.getActiveCall() != null
1046 || callList.getBackgroundCall() != null
1047 || callList.getDisconnectedCall() != null
1048 || callList.getDisconnectingCall() != null) {
1049 newState = InCallState.INCALL;
1050 }
1051
1052 if (newState == InCallState.NO_CALLS) {
linyuh183cb712017-12-27 17:02:37 -08001053 if (boundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001054 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001055 }
1056 }
1057
1058 return newState;
1059 }
1060
1061 public boolean isBoundAndWaitingForOutgoingCall() {
linyuh183cb712017-12-27 17:02:37 -08001062 return boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -08001063 }
1064
1065 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001066 LogUtil.i(
1067 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
1068 "setBoundAndWaitingForOutgoingCall: " + isBound);
linyuh183cb712017-12-27 17:02:37 -08001069 boundAndWaitingForOutgoingCall = isBound;
1070 themeColorManager.setPendingPhoneAccountHandle(handle);
1071 if (isBound && inCallState == InCallState.NO_CALLS) {
1072 inCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001073 }
1074 }
1075
1076 public void onShrinkAnimationComplete() {
linyuh183cb712017-12-27 17:02:37 -08001077 if (awaitingCallListUpdate) {
1078 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001079 }
1080 }
1081
1082 public void addIncomingCallListener(IncomingCallListener listener) {
1083 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001084 incomingCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001085 }
1086
1087 public void removeIncomingCallListener(IncomingCallListener listener) {
1088 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001089 incomingCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001090 }
1091 }
1092
1093 public void addListener(InCallStateListener listener) {
1094 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001095 listeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001096 }
1097
1098 public void removeListener(InCallStateListener listener) {
1099 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001100 listeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001101 }
1102 }
1103
1104 public void addDetailsListener(InCallDetailsListener listener) {
1105 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001106 detailsListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001107 }
1108
1109 public void removeDetailsListener(InCallDetailsListener listener) {
1110 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001111 detailsListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001112 }
1113 }
1114
1115 public void addCanAddCallListener(CanAddCallListener listener) {
1116 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001117 canAddCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001118 }
1119
1120 public void removeCanAddCallListener(CanAddCallListener listener) {
1121 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001122 canAddCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001123 }
1124 }
1125
1126 public void addOrientationListener(InCallOrientationListener listener) {
1127 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001128 orientationListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001129 }
1130
1131 public void removeOrientationListener(InCallOrientationListener listener) {
1132 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001133 orientationListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001134 }
1135 }
1136
1137 public void addInCallEventListener(InCallEventListener listener) {
1138 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001139 inCallEventListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001140 }
1141
1142 public void removeInCallEventListener(InCallEventListener listener) {
1143 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001144 inCallEventListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001145 }
1146 }
1147
1148 public ProximitySensor getProximitySensor() {
linyuh183cb712017-12-27 17:02:37 -08001149 return proximitySensor;
Eric Erfanianccca3152017-02-22 16:32:36 -08001150 }
1151
1152 public PseudoScreenState getPseudoScreenState() {
linyuh183cb712017-12-27 17:02:37 -08001153 return pseudoScreenState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001154 }
1155
1156 /** Returns true if the incall app is the foreground application. */
1157 public boolean isShowingInCallUi() {
1158 if (!isActivityStarted()) {
1159 return false;
1160 }
linyuh183cb712017-12-27 17:02:37 -08001161 if (manageConferenceActivity != null && manageConferenceActivity.isVisible()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001162 return true;
1163 }
linyuh183cb712017-12-27 17:02:37 -08001164 return inCallActivity.isVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001165 }
1166
1167 /**
1168 * Returns true if the activity has been created and is running. Returns true as long as activity
1169 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
1170 * (not in foreground).
1171 */
1172 public boolean isActivityStarted() {
linyuh183cb712017-12-27 17:02:37 -08001173 return (inCallActivity != null
1174 && !inCallActivity.isDestroyed()
1175 && !inCallActivity.isFinishing());
Eric Erfanianccca3152017-02-22 16:32:36 -08001176 }
1177
1178 /**
1179 * Determines if the In-Call app is currently changing configuration.
1180 *
1181 * @return {@code true} if the In-Call app is changing configuration.
1182 */
1183 public boolean isChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001184 return isChangingConfigurations;
Eric Erfanianccca3152017-02-22 16:32:36 -08001185 }
1186
1187 /**
1188 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1189 * screen orientation).
1190 */
1191 /*package*/
1192 void updateIsChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001193 isChangingConfigurations = false;
1194 if (inCallActivity != null) {
1195 isChangingConfigurations = inCallActivity.isChangingConfigurations();
Eric Erfanianccca3152017-02-22 16:32:36 -08001196 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001197 LogUtil.v(
1198 "InCallPresenter.updateIsChangingConfigurations",
linyuh183cb712017-12-27 17:02:37 -08001199 "updateIsChangingConfigurations = " + isChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001200 }
1201
yueg10f6e822018-01-17 15:32:18 -08001202 /** Called when the activity goes in/out of the foreground. */
1203 public void onUiShowing(boolean showing) {
linyuh183cb712017-12-27 17:02:37 -08001204 if (proximitySensor != null) {
1205 proximitySensor.onInCallShowing(showing);
Eric Erfanianccca3152017-02-22 16:32:36 -08001206 }
1207
yueg092b21c2017-11-15 16:20:07 -08001208 if (!showing) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001209 updateIsChangingConfigurations();
1210 }
1211
linyuh183cb712017-12-27 17:02:37 -08001212 for (InCallUiListener listener : inCallUiListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001213 listener.onUiShowing(showing);
1214 }
1215
linyuh183cb712017-12-27 17:02:37 -08001216 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001217 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001218 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001219 }
1220 }
1221
Eric Erfanian2ca43182017-08-31 06:57:16 -07001222 public void refreshUi() {
linyuh183cb712017-12-27 17:02:37 -08001223 if (inCallActivity != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001224 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001225 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001226 }
1227 }
1228
Eric Erfanianccca3152017-02-22 16:32:36 -08001229 public void addInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001230 inCallUiListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001231 }
1232
1233 public boolean removeInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001234 return inCallUiListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001235 }
1236
1237 /*package*/
1238 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001239 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001240 notifyVideoPauseController(true);
Eric Erfanian2ca43182017-08-31 06:57:16 -07001241 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001242 }
1243
1244 /*package*/
1245 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001246 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001247 notifyVideoPauseController(false);
1248 }
1249
1250 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001251 LogUtil.d(
1252 "InCallPresenter.notifyVideoPauseController",
linyuh183cb712017-12-27 17:02:37 -08001253 "mIsChangingConfigurations=" + isChangingConfigurations);
1254 if (!isChangingConfigurations) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001255 VideoPauseController.getInstance().onUiShowing(showing);
1256 }
1257 }
1258
1259 /** Brings the app into the foreground if possible. */
1260 public void bringToForeground(boolean showDialpad) {
1261 // Before we bring the incall UI to the foreground, we check to see if:
1262 // 1. It is not currently in the foreground
1263 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1264 // be displayed)
1265 // If the activity hadn't actually been started previously, yet there are still calls
1266 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1267 // bring it up the UI regardless.
linyuh183cb712017-12-27 17:02:37 -08001268 if (!isShowingInCallUi() && inCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001269 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001270 }
1271 }
1272
1273 public void onPostDialCharWait(String callId, String chars) {
1274 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001275 inCallActivity.showDialogForPostCharWait(callId, chars);
Eric Erfanianccca3152017-02-22 16:32:36 -08001276 }
1277 }
1278
1279 /**
1280 * Handles the green CALL key while in-call.
1281 *
1282 * @return true if we consumed the event.
1283 */
1284 public boolean handleCallKey() {
1285 LogUtil.v("InCallPresenter.handleCallKey", null);
1286
1287 // The green CALL button means either "Answer", "Unhold", or
1288 // "Swap calls", or can be a no-op, depending on the current state
1289 // of the Phone.
1290
1291 /** INCOMING CALL */
linyuh183cb712017-12-27 17:02:37 -08001292 final CallList calls = callList;
Eric Erfanianccca3152017-02-22 16:32:36 -08001293 final DialerCall incomingCall = calls.getIncomingCall();
1294 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1295
1296 // (1) Attempt to answer a call
1297 if (incomingCall != null) {
1298 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1299 return true;
1300 }
1301
1302 /** STATE_ACTIVE CALL */
1303 final DialerCall activeCall = calls.getActiveCall();
1304 if (activeCall != null) {
1305 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1306 // consolidate this logic.
1307 final boolean canMerge =
1308 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1309 final boolean canSwap =
1310 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1311
Eric Erfanian2ca43182017-08-31 06:57:16 -07001312 LogUtil.v(
1313 "InCallPresenter.handleCallKey",
1314 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001315
1316 // (2) Attempt actions on conference calls
1317 if (canMerge) {
1318 TelecomAdapter.getInstance().merge(activeCall.getId());
1319 return true;
1320 } else if (canSwap) {
1321 TelecomAdapter.getInstance().swap(activeCall.getId());
1322 return true;
1323 }
1324 }
1325
1326 /** BACKGROUND CALL */
1327 final DialerCall heldCall = calls.getBackgroundCall();
1328 if (heldCall != null) {
1329 // We have a hold call so presumeable it will always support HOLD...but
1330 // there is no harm in double checking.
1331 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1332
Eric Erfanian2ca43182017-08-31 06:57:16 -07001333 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001334
1335 // (4) unhold call
1336 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1337 heldCall.unhold();
1338 return true;
1339 }
1340 }
1341
1342 // Always consume hard keys
1343 return true;
1344 }
1345
Eric Erfanianccca3152017-02-22 16:32:36 -08001346 /** Clears the previous fullscreen state. */
1347 public void clearFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001348 isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001349 }
1350
1351 /**
1352 * Changes the fullscreen mode of the in-call UI.
1353 *
1354 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1355 * otherwise.
1356 */
1357 public void setFullScreen(boolean isFullScreen) {
1358 setFullScreen(isFullScreen, false /* force */);
1359 }
1360
1361 /**
1362 * Changes the fullscreen mode of the in-call UI.
1363 *
1364 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1365 * otherwise.
1366 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1367 */
1368 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001369 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001370
1371 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1372 if (isDialpadVisible()) {
1373 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001374 LogUtil.v(
1375 "InCallPresenter.setFullScreen",
1376 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001377 }
1378
linyuh183cb712017-12-27 17:02:37 -08001379 if (this.isFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001380 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001381 return;
1382 }
linyuh183cb712017-12-27 17:02:37 -08001383 this.isFullScreen = isFullScreen;
1384 notifyFullscreenModeChange(this.isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001385 }
1386
1387 /**
1388 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1389 * otherwise.
1390 */
1391 public boolean isFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001392 return isFullScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001393 }
1394
1395 /**
1396 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1397 *
1398 * @param isFullscreenMode {@code True} if entering full screen mode.
1399 */
1400 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001401 for (InCallEventListener listener : inCallEventListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001402 listener.onFullscreenModeChanged(isFullscreenMode);
1403 }
1404 }
1405
linyuh7b86f562017-11-16 11:24:09 -08001406 /** Instruct the in-call activity to show an error dialog or toast for a disconnected call. */
1407 private void showDialogOrToastForDisconnectedCall(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -07001408 if (call.getState() != DialerCall.State.DISCONNECTED) {
linyuh7b86f562017-11-16 11:24:09 -08001409 return;
Eric Erfanianccca3152017-02-22 16:32:36 -08001410 }
linyuh7b86f562017-11-16 11:24:09 -08001411
1412 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1413 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1414 setDisconnectCauseForMissingAccounts(call);
1415 }
1416
yueg32d9ae72018-04-05 17:35:56 -07001417 if (isActivityStarted()) {
1418 inCallActivity.showDialogOrToastForDisconnectedCall(
1419 new DisconnectMessage(inCallActivity, call));
1420 } else {
1421 CharSequence message = new DisconnectMessage(context, call).toastMessage;
1422 if (message != null) {
1423 Toast.makeText(context, message, Toast.LENGTH_LONG).show();
1424 }
1425 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001426 }
1427
1428 /**
1429 * When the state of in-call changes, this is the first method to get called. It determines if the
1430 * UI needs to be started or finished depending on the new state and does it.
1431 */
1432 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001433 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001434 LogUtil.d(
linyuh183cb712017-12-27 17:02:37 -08001435 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + inCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001436
1437 // TODO: Consider a proper state machine implementation
1438
1439 // If the state isn't changing we have already done any starting/stopping of activities in
1440 // a previous pass...so lets cut out early
linyuh183cb712017-12-27 17:02:37 -08001441 if (newState == inCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001442 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001443 return newState;
1444 }
1445
1446 // A new Incoming call means that the user needs to be notified of the the call (since
1447 // it wasn't them who initiated it). We do this through full screen notifications and
1448 // happens indirectly through {@link StatusBarNotifier}.
1449 //
1450 // The process for incoming calls is as follows:
1451 //
1452 // 1) CallList - Announces existence of new INCOMING call
1453 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1454 // - should be set to INCOMING.
1455 // 3) InCallPresenter - This method is called to see if we need to start or finish
1456 // the app given the new state.
1457 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1458 // StatusBarNotifier explicitly to issue a FullScreen Notification
1459 // that will either start the InCallActivity or show the user a
1460 // top-level notification dialog if the user is in an immersive app.
1461 // That notification can also start the InCallActivity.
1462 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1463 // call InCallPresenter::setActivity() to let the presenter
1464 // know that start-up is complete.
1465 //
1466 // [ AND NOW YOU'RE IN THE CALL. voila! ]
Eric Erfanianccca3152017-02-22 16:32:36 -08001467
1468 // A dialog to show on top of the InCallUI to select a PhoneAccount
1469 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1470
1471 // A new outgoing call indicates that the user just now dialed a number and when that
1472 // happens we need to display the screen immediately or show an account picker dialog if
1473 // no default is set. However, if the main InCallUI is already visible, we do not want to
1474 // re-initiate the start-up animation, so we do not need to do anything here.
1475 //
1476 // It is also possible to go into an intermediate state where the call has been initiated
1477 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1478 // This pending outgoing state can also launch the call screen.
1479 //
1480 // This is different from the incoming call sequence because we do not need to shock the
1481 // user with a top-level notification. Just show the call UI normally.
1482 boolean callCardFragmentVisible =
linyuh183cb712017-12-27 17:02:37 -08001483 inCallActivity != null && inCallActivity.getCallCardFragmentVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001484 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1485 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1486
1487 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1488 // outgoing call process, so the UI should be brought up to show an error dialog.
1489 showCallUi |=
linyuh183cb712017-12-27 17:02:37 -08001490 (InCallState.PENDING_OUTGOING == inCallState
Eric Erfanianccca3152017-02-22 16:32:36 -08001491 && InCallState.INCALL == newState
1492 && !isShowingInCallUi());
1493
1494 // Another exception - InCallActivity is in charge of disconnecting a call with no
1495 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1496 // call so that:
1497 // 1) The call can be disconnected correctly
1498 // 2) The UI comes up and correctly displays the error dialog.
1499 // TODO: Remove these special case conditions by making InCallPresenter a true state
1500 // machine. Telecom should also be the component responsible for disconnecting a call
1501 // with no valid accounts.
1502 showCallUi |=
1503 InCallState.PENDING_OUTGOING == newState
1504 && mainUiNotVisible
linyuh183cb712017-12-27 17:02:37 -08001505 && isCallWithNoValidAccounts(callList.getPendingOutgoingCall());
Eric Erfanianccca3152017-02-22 16:32:36 -08001506
1507 // The only time that we have an instance of mInCallActivity and it isn't started is
1508 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1509 // the activity. When it is finally destroyed, we double check if we should bring it back
1510 // up so we aren't going to lose anything by avoiding a second startup here.
linyuh183cb712017-12-27 17:02:37 -08001511 boolean activityIsFinishing = inCallActivity != null && !isActivityStarted();
Eric Erfanianccca3152017-02-22 16:32:36 -08001512 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001513 LogUtil.i(
1514 "InCallPresenter.startOrFinishUi",
linyuh183cb712017-12-27 17:02:37 -08001515 "Undo the state change: " + newState + " -> " + inCallState);
wangqicf61ca02017-08-31 15:32:55 -07001516 Trace.endSection();
linyuh183cb712017-12-27 17:02:37 -08001517 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001518 }
1519
1520 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1521 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1522 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1523 // outgoing call after user ignore it by pressing home button.
1524 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1525 && !showCallUi
1526 && isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001527 inCallActivity.dismissPendingDialogs();
Eric Erfanianccca3152017-02-22 16:32:36 -08001528 }
1529
yueg4613e8f2018-04-23 13:35:33 -07001530 if ((showCallUi || showAccountPicker) && !shouldStartInBubbleMode()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001531 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001532 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001533 } else if (newState == InCallState.NO_CALLS) {
1534 // The new state is the no calls state. Tear everything down.
yueg8e544862018-04-06 14:34:59 -07001535 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001536 attemptFinishActivity();
1537 attemptCleanup();
1538 }
1539
wangqicf61ca02017-08-31 15:32:55 -07001540 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001541 return newState;
1542 }
1543
1544 /**
1545 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1546 * or PhoneAccounts to select from.
1547 */
1548 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1549
1550 Bundle extras = call.getIntentExtras();
1551 // Initialize the extras bundle to avoid NPE
1552 if (extras == null) {
1553 extras = new Bundle();
1554 }
1555
1556 final List<PhoneAccountHandle> phoneAccountHandles =
1557 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1558
1559 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1560 String scheme = call.getHandle().getScheme();
1561 final String errorMsg =
1562 PhoneAccount.SCHEME_TEL.equals(scheme)
linyuh183cb712017-12-27 17:02:37 -08001563 ? context.getString(R.string.callFailed_simError)
1564 : context.getString(R.string.incall_error_supp_service_unknown);
Eric Erfanianccca3152017-02-22 16:32:36 -08001565 DisconnectCause disconnectCause =
1566 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1567 call.setDisconnectCause(disconnectCause);
1568 }
1569 }
1570
Eric Erfanianccca3152017-02-22 16:32:36 -08001571 /**
1572 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1573 * Calling classes should use this as an indication whether to interact with the
1574 * InCallPresenter or not.
1575 */
1576 public boolean isReadyForTearDown() {
linyuh183cb712017-12-27 17:02:37 -08001577 return inCallActivity == null && !serviceConnected && inCallState == InCallState.NO_CALLS;
Eric Erfanianccca3152017-02-22 16:32:36 -08001578 }
1579
1580 /**
1581 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1582 */
1583 private void attemptCleanup() {
1584 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001585 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001586
1587 cleanupSurfaces();
1588
linyuh183cb712017-12-27 17:02:37 -08001589 isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001590
yueg8e544862018-04-06 14:34:59 -07001591 audioRouteSetForBubbleMode = false;
yueg32d9ae72018-04-05 17:35:56 -07001592
Eric Erfanianccca3152017-02-22 16:32:36 -08001593 // blow away stale contact info so that we get fresh data on
1594 // the next set of calls
linyuh183cb712017-12-27 17:02:37 -08001595 if (contactInfoCache != null) {
1596 contactInfoCache.clearCache();
Eric Erfanianccca3152017-02-22 16:32:36 -08001597 }
linyuh183cb712017-12-27 17:02:37 -08001598 contactInfoCache = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001599
linyuh183cb712017-12-27 17:02:37 -08001600 if (proximitySensor != null) {
1601 removeListener(proximitySensor);
1602 proximitySensor.tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -08001603 }
linyuh183cb712017-12-27 17:02:37 -08001604 proximitySensor = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001605
linyuh183cb712017-12-27 17:02:37 -08001606 if (statusBarNotifier != null) {
1607 removeListener(statusBarNotifier);
1608 EnrichedCallComponent.get(context)
Eric Erfaniand8046e52017-04-06 09:41:50 -07001609 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -08001610 .unregisterStateChangedListener(statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001611 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001612
linyuh183cb712017-12-27 17:02:37 -08001613 if (externalCallNotifier != null && externalCallList != null) {
1614 externalCallList.removeExternalCallListener(externalCallNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001615 }
linyuh183cb712017-12-27 17:02:37 -08001616 statusBarNotifier = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001617
linyuh183cb712017-12-27 17:02:37 -08001618 if (callList != null) {
1619 callList.removeListener(this);
1620 callList.removeListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001621 }
linyuh183cb712017-12-27 17:02:37 -08001622 callList = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001623
linyuh183cb712017-12-27 17:02:37 -08001624 context = null;
1625 inCallActivity = null;
1626 manageConferenceActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001627
linyuh183cb712017-12-27 17:02:37 -08001628 listeners.clear();
1629 incomingCallListeners.clear();
1630 detailsListeners.clear();
1631 canAddCallListeners.clear();
1632 orientationListeners.clear();
1633 inCallEventListeners.clear();
1634 inCallUiListeners.clear();
1635 if (!inCallUiLocks.isEmpty()) {
1636 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + inCallUiLocks);
1637 inCallUiLocks.clear();
twyen8efb4952017-10-06 16:35:54 -07001638 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001639 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001640 }
1641 }
1642
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001643 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001644 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
linyuh183cb712017-12-27 17:02:37 -08001645 context.startActivity(
1646 InCallActivity.getIntent(context, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001647 }
1648
1649 public void onServiceBind() {
linyuh183cb712017-12-27 17:02:37 -08001650 serviceBound = true;
Eric Erfanianccca3152017-02-22 16:32:36 -08001651 }
1652
1653 public void onServiceUnbind() {
1654 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -08001655 serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001656 }
1657
1658 public boolean isServiceBound() {
linyuh183cb712017-12-27 17:02:37 -08001659 return serviceBound;
Eric Erfanianccca3152017-02-22 16:32:36 -08001660 }
1661
1662 public void maybeStartRevealAnimation(Intent intent) {
linyuh183cb712017-12-27 17:02:37 -08001663 if (intent == null || inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001664 return;
1665 }
1666 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1667 if (extras == null) {
1668 // Incoming call, just show the in-call UI directly.
1669 return;
1670 }
1671
1672 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1673 // Account selection dialog will show up so don't show the animation.
1674 return;
1675 }
1676
1677 final PhoneAccountHandle accountHandle =
1678 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1679 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001680
yueg32d9ae72018-04-05 17:35:56 -07001681 setBoundAndWaitingForOutgoingCall(true, accountHandle);
1682
yueg4613e8f2018-04-23 13:35:33 -07001683 if (shouldStartInBubbleModeWithExtras(extras)) {
yueg32d9ae72018-04-05 17:35:56 -07001684 LogUtil.i("InCallPresenter.maybeStartRevealAnimation", "shouldStartInBubbleMode");
1685 // Show bubble instead of in call UI
1686 return;
1687 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001688
1689 final Intent activityIntent =
linyuh183cb712017-12-27 17:02:37 -08001690 InCallActivity.getIntent(context, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001691 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
linyuh183cb712017-12-27 17:02:37 -08001692 context.startActivity(activityIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001693 }
1694
1695 /**
1696 * Retrieves the current in-call camera manager instance, creating if necessary.
1697 *
1698 * @return The {@link InCallCameraManager}.
1699 */
1700 public InCallCameraManager getInCallCameraManager() {
1701 synchronized (this) {
linyuh183cb712017-12-27 17:02:37 -08001702 if (inCallCameraManager == null) {
1703 inCallCameraManager = new InCallCameraManager(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001704 }
1705
linyuh183cb712017-12-27 17:02:37 -08001706 return inCallCameraManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001707 }
1708 }
1709
1710 /**
1711 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1712 *
1713 * @param orientation The screen orientation of the device (one of: {@link
1714 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1715 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1716 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1717 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1718 */
1719 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001720 LogUtil.d(
1721 "InCallPresenter.onDeviceOrientationChange",
1722 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001723
linyuh183cb712017-12-27 17:02:37 -08001724 if (callList != null) {
1725 callList.notifyCallsOfDeviceRotation(orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001726 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001727 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001728 }
1729
1730 // Notify listeners of device orientation changed.
linyuh183cb712017-12-27 17:02:37 -08001731 for (InCallOrientationListener listener : orientationListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001732 listener.onDeviceOrientationChanged(orientation);
1733 }
1734 }
1735
1736 /**
1737 * Configures the in-call UI activity so it can change orientations or not. Enables the
1738 * orientation event listener if allowOrientationChange is true, disables it if false.
1739 *
1740 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1741 * landscape. {@code false} if the in-call UI should be locked in portrait.
1742 */
1743 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
linyuh183cb712017-12-27 17:02:37 -08001744 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001745 LogUtil.e(
1746 "InCallPresenter.setInCallAllowsOrientationChange",
1747 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001748 return;
1749 }
linyuh183cb712017-12-27 17:02:37 -08001750 inCallActivity.setAllowOrientationChange(allowOrientationChange);
Eric Erfanianccca3152017-02-22 16:32:36 -08001751 }
1752
1753 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001754 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
linyuh183cb712017-12-27 17:02:37 -08001755 screenTimeoutEnabled = enable;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001756 applyScreenTimeout();
1757 }
1758
1759 private void applyScreenTimeout() {
linyuh183cb712017-12-27 17:02:37 -08001760 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001761 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001762 return;
1763 }
1764
linyuh183cb712017-12-27 17:02:37 -08001765 final Window window = inCallActivity.getWindow();
1766 if (screenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001767 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1768 } else {
1769 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1770 }
1771 }
1772
1773 /**
1774 * Hides or shows the conference manager fragment.
1775 *
1776 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1777 * be hidden.
1778 */
1779 public void showConferenceCallManager(boolean show) {
linyuh183cb712017-12-27 17:02:37 -08001780 if (inCallActivity != null) {
1781 inCallActivity.showConferenceFragment(show);
Eric Erfanianccca3152017-02-22 16:32:36 -08001782 }
linyuh183cb712017-12-27 17:02:37 -08001783 if (!show && manageConferenceActivity != null) {
1784 manageConferenceActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -08001785 }
1786 }
1787
1788 /**
1789 * Determines if the dialpad is visible.
1790 *
1791 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1792 */
1793 public boolean isDialpadVisible() {
linyuh183cb712017-12-27 17:02:37 -08001794 if (inCallActivity == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001795 return false;
1796 }
linyuh183cb712017-12-27 17:02:37 -08001797 return inCallActivity.isDialpadVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001798 }
1799
1800 public ThemeColorManager getThemeColorManager() {
linyuh183cb712017-12-27 17:02:37 -08001801 return themeColorManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001802 }
1803
wangqi8d662ca2017-10-26 11:27:19 -07001804 @VisibleForTesting
1805 public void setThemeColorManager(ThemeColorManager themeColorManager) {
linyuh183cb712017-12-27 17:02:37 -08001806 this.themeColorManager = themeColorManager;
wangqi8d662ca2017-10-26 11:27:19 -07001807 }
1808
Eric Erfanianccca3152017-02-22 16:32:36 -08001809 /** Called when the foreground call changes. */
1810 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh183cb712017-12-27 17:02:37 -08001811 themeColorManager.onForegroundCallChanged(context, newForegroundCall);
1812 if (inCallActivity != null) {
1813 inCallActivity.onForegroundCallChanged(newForegroundCall);
Eric Erfanianccca3152017-02-22 16:32:36 -08001814 }
1815 }
1816
1817 public InCallActivity getActivity() {
linyuh183cb712017-12-27 17:02:37 -08001818 return inCallActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -08001819 }
1820
1821 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1822 public void setActivity(InCallActivity inCallActivity) {
1823 if (inCallActivity == null) {
1824 throw new IllegalArgumentException("registerActivity cannot be called with null");
1825 }
linyuh183cb712017-12-27 17:02:37 -08001826 if (this.inCallActivity != null && this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001827 LogUtil.w(
1828 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001829 }
1830 updateActivity(inCallActivity);
1831 }
1832
1833 ExternalCallNotifier getExternalCallNotifier() {
linyuh183cb712017-12-27 17:02:37 -08001834 return externalCallNotifier;
Eric Erfanianccca3152017-02-22 16:32:36 -08001835 }
1836
1837 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001838 if (localVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001839 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001840 if (context != null) {
1841 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001842 }
linyuh183cb712017-12-27 17:02:37 -08001843 localVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001844 }
linyuh183cb712017-12-27 17:02:37 -08001845 return localVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001846 }
1847
1848 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001849 if (remoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001850 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001851 if (context != null) {
1852 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001853 }
linyuh183cb712017-12-27 17:02:37 -08001854 remoteVideoSurfaceTexture = VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001855 }
linyuh183cb712017-12-27 17:02:37 -08001856 return remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001857 }
1858
1859 void cleanupSurfaces() {
linyuh183cb712017-12-27 17:02:37 -08001860 if (remoteVideoSurfaceTexture != null) {
1861 remoteVideoSurfaceTexture.setDoneWithSurface();
1862 remoteVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001863 }
linyuh183cb712017-12-27 17:02:37 -08001864 if (localVideoSurfaceTexture != null) {
1865 localVideoSurfaceTexture.setDoneWithSurface();
1866 localVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001867 }
1868 }
1869
yueg77cb8e52017-10-27 16:42:51 -07001870 @Override
1871 public void onAudioStateChanged(CallAudioState audioState) {
yueg8e544862018-04-06 14:34:59 -07001872 // Set sensible audio route for bubble mode when we get real audio state for the first time
1873 // During the first time this function is called, supportedRouteMask is set to
yueg4613e8f2018-04-23 13:35:33 -07001874 // SUPPORTED_AUDIO_ROUTE_ALL, but it's OK since shouldStartInBubbleMode() is false at that time
1875 // (callList not updated yet).
1876 if (!audioRouteSetForBubbleMode && shouldStartInBubbleMode()) {
yueg8e544862018-04-06 14:34:59 -07001877 setAudioRouteForBubbleMode(audioState);
1878 audioRouteSetForBubbleMode = true;
1879 }
1880
linyuh183cb712017-12-27 17:02:37 -08001881 if (statusBarNotifier != null) {
1882 statusBarNotifier.updateNotification();
yueg77cb8e52017-10-27 16:42:51 -07001883 }
1884 }
1885
yueg8e544862018-04-06 14:34:59 -07001886 /**
1887 * Set audio route to make audio sensible. According to availability, set audio route to Bluetooth
1888 * or wired headset or speaker.
1889 */
1890 private void setAudioRouteForBubbleMode(CallAudioState audioState) {
1891 if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
1892 == CallAudioState.ROUTE_BLUETOOTH) {
1893 // Use Bluetooth if available
1894 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
1895 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "bluetooth");
1896 } else if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_WIRED_HEADSET)
1897 == CallAudioState.ROUTE_WIRED_HEADSET) {
1898 // Use wired headset if available
1899 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_WIRED_HEADSET);
1900 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "wired headset");
1901 } else {
1902 // Use speaker
1903 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_SPEAKER);
1904 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "speaker");
1905 }
1906 }
1907
Eric Erfanianccca3152017-02-22 16:32:36 -08001908 /** All the main states of InCallActivity. */
1909 public enum InCallState {
1910 // InCall Screen is off and there are no calls
1911 NO_CALLS,
1912
1913 // Incoming-call screen is up
1914 INCOMING,
1915
1916 // In-call experience is showing
1917 INCALL,
1918
1919 // Waiting for user input before placing outgoing call
1920 WAITING_FOR_ACCOUNT,
1921
1922 // UI is starting up but no call has been initiated yet.
1923 // The UI is waiting for Telecom to respond.
1924 PENDING_OUTGOING,
1925
1926 // User is dialing out
1927 OUTGOING;
1928
1929 public boolean isIncoming() {
1930 return (this == INCOMING);
1931 }
1932
1933 public boolean isConnectingOrConnected() {
1934 return (this == INCOMING || this == OUTGOING || this == INCALL);
1935 }
1936 }
1937
1938 /** Interface implemented by classes that need to know about the InCall State. */
1939 public interface InCallStateListener {
1940
1941 // TODO: Enhance state to contain the call objects instead of passing CallList
1942 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1943 }
1944
1945 public interface IncomingCallListener {
1946
1947 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1948 }
1949
1950 public interface CanAddCallListener {
1951
1952 void onCanAddCallChanged(boolean canAddCall);
1953 }
1954
1955 public interface InCallDetailsListener {
1956
1957 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1958 }
1959
1960 public interface InCallOrientationListener {
1961
1962 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1963 }
1964
1965 /**
1966 * Interface implemented by classes that need to know about events which occur within the In-Call
1967 * UI. Used as a means of communicating between fragments that make up the UI.
1968 */
1969 public interface InCallEventListener {
1970
1971 void onFullscreenModeChanged(boolean isFullscreenMode);
1972 }
1973
1974 public interface InCallUiListener {
1975
1976 void onUiShowing(boolean showing);
1977 }
twyen8efb4952017-10-06 16:35:54 -07001978
1979 private class InCallUiLockImpl implements InCallUiLock {
1980 private final String tag;
1981
1982 private InCallUiLockImpl(String tag) {
1983 this.tag = tag;
1984 }
1985
1986 @MainThread
1987 @Override
1988 public void release() {
1989 Assert.isMainThread();
1990 releaseInCallUiLock(InCallUiLockImpl.this);
1991 }
1992
1993 @Override
1994 public String toString() {
1995 return "InCallUiLock[" + tag + "]";
1996 }
1997 }
1998
1999 @MainThread
2000 public InCallUiLock acquireInCallUiLock(String tag) {
2001 Assert.isMainThread();
2002 InCallUiLock lock = new InCallUiLockImpl(tag);
linyuh183cb712017-12-27 17:02:37 -08002003 inCallUiLocks.add(lock);
twyen8efb4952017-10-06 16:35:54 -07002004 return lock;
2005 }
2006
2007 @MainThread
2008 private void releaseInCallUiLock(InCallUiLock lock) {
2009 Assert.isMainThread();
2010 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
linyuh183cb712017-12-27 17:02:37 -08002011 inCallUiLocks.remove(lock);
2012 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002013 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
linyuh183cb712017-12-27 17:02:37 -08002014 if (inCallState == InCallState.NO_CALLS) {
twyen8efb4952017-10-06 16:35:54 -07002015 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
2016 attemptFinishActivity();
2017 attemptCleanup();
2018 }
2019 }
2020 }
2021
2022 @MainThread
2023 public boolean isInCallUiLocked() {
2024 Assert.isMainThread();
linyuh183cb712017-12-27 17:02:37 -08002025 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002026 return false;
2027 }
linyuh183cb712017-12-27 17:02:37 -08002028 for (InCallUiLock lock : inCallUiLocks) {
twyen8efb4952017-10-06 16:35:54 -07002029 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
2030 }
2031 return true;
2032 }
2033
linyuh183cb712017-12-27 17:02:37 -08002034 private final Set<InCallUiLock> inCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08002035}