blob: 6e7daf551ad3d6978abafe671c76551b1a8a13ee [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) {
yuega489f512018-04-25 12:01:09 -07001274 // If not visible, inCallActivity is stopped. Starting from P, calling recreate() will destroy
1275 // the old activity instance and create a new instance immediately. Previously, the old activity
1276 // went through its lifecycle from create to destroy before creating a new instance.
1277 // So this case doesn't work now: make a call with char WAIT, leave in call UI, call gets
1278 // connected, and go back to in call UI to see the dialog.
1279 // So we should show dialog in an empty activity if inCallActivity is not visible. And it also
1280 // helps with background calling.
1281 if (isActivityStarted() && inCallActivity.isVisible()) {
linyuh183cb712017-12-27 17:02:37 -08001282 inCallActivity.showDialogForPostCharWait(callId, chars);
yuega489f512018-04-25 12:01:09 -07001283 } else {
1284 Intent intent = new Intent(context, PostCharDialogActivity.class);
1285 // Prevent showing MainActivity with PostCharDialogActivity on above
1286 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1287 intent.putExtra(PostCharDialogActivity.EXTRA_CALL_ID, callId);
1288 intent.putExtra(PostCharDialogActivity.EXTRA_POST_DIAL_STRING, chars);
1289 context.startActivity(intent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001290 }
1291 }
1292
1293 /**
1294 * Handles the green CALL key while in-call.
1295 *
1296 * @return true if we consumed the event.
1297 */
1298 public boolean handleCallKey() {
1299 LogUtil.v("InCallPresenter.handleCallKey", null);
1300
1301 // The green CALL button means either "Answer", "Unhold", or
1302 // "Swap calls", or can be a no-op, depending on the current state
1303 // of the Phone.
1304
1305 /** INCOMING CALL */
linyuh183cb712017-12-27 17:02:37 -08001306 final CallList calls = callList;
Eric Erfanianccca3152017-02-22 16:32:36 -08001307 final DialerCall incomingCall = calls.getIncomingCall();
1308 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1309
1310 // (1) Attempt to answer a call
1311 if (incomingCall != null) {
1312 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1313 return true;
1314 }
1315
1316 /** STATE_ACTIVE CALL */
1317 final DialerCall activeCall = calls.getActiveCall();
1318 if (activeCall != null) {
1319 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1320 // consolidate this logic.
1321 final boolean canMerge =
1322 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1323 final boolean canSwap =
1324 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1325
Eric Erfanian2ca43182017-08-31 06:57:16 -07001326 LogUtil.v(
1327 "InCallPresenter.handleCallKey",
1328 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001329
1330 // (2) Attempt actions on conference calls
1331 if (canMerge) {
1332 TelecomAdapter.getInstance().merge(activeCall.getId());
1333 return true;
1334 } else if (canSwap) {
1335 TelecomAdapter.getInstance().swap(activeCall.getId());
1336 return true;
1337 }
1338 }
1339
1340 /** BACKGROUND CALL */
1341 final DialerCall heldCall = calls.getBackgroundCall();
1342 if (heldCall != null) {
1343 // We have a hold call so presumeable it will always support HOLD...but
1344 // there is no harm in double checking.
1345 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1346
Eric Erfanian2ca43182017-08-31 06:57:16 -07001347 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001348
1349 // (4) unhold call
1350 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1351 heldCall.unhold();
1352 return true;
1353 }
1354 }
1355
1356 // Always consume hard keys
1357 return true;
1358 }
1359
Eric Erfanianccca3152017-02-22 16:32:36 -08001360 /** Clears the previous fullscreen state. */
1361 public void clearFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001362 isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001363 }
1364
1365 /**
1366 * Changes the fullscreen mode of the in-call UI.
1367 *
1368 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1369 * otherwise.
1370 */
1371 public void setFullScreen(boolean isFullScreen) {
1372 setFullScreen(isFullScreen, false /* force */);
1373 }
1374
1375 /**
1376 * Changes the fullscreen mode of the in-call UI.
1377 *
1378 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1379 * otherwise.
1380 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1381 */
1382 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001383 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001384
1385 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1386 if (isDialpadVisible()) {
1387 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001388 LogUtil.v(
1389 "InCallPresenter.setFullScreen",
1390 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001391 }
1392
linyuh183cb712017-12-27 17:02:37 -08001393 if (this.isFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001394 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001395 return;
1396 }
linyuh183cb712017-12-27 17:02:37 -08001397 this.isFullScreen = isFullScreen;
1398 notifyFullscreenModeChange(this.isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001399 }
1400
1401 /**
1402 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1403 * otherwise.
1404 */
1405 public boolean isFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001406 return isFullScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001407 }
1408
1409 /**
1410 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1411 *
1412 * @param isFullscreenMode {@code True} if entering full screen mode.
1413 */
1414 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001415 for (InCallEventListener listener : inCallEventListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001416 listener.onFullscreenModeChanged(isFullscreenMode);
1417 }
1418 }
1419
linyuh7b86f562017-11-16 11:24:09 -08001420 /** Instruct the in-call activity to show an error dialog or toast for a disconnected call. */
1421 private void showDialogOrToastForDisconnectedCall(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -07001422 if (call.getState() != DialerCall.State.DISCONNECTED) {
linyuh7b86f562017-11-16 11:24:09 -08001423 return;
Eric Erfanianccca3152017-02-22 16:32:36 -08001424 }
linyuh7b86f562017-11-16 11:24:09 -08001425
1426 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1427 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1428 setDisconnectCauseForMissingAccounts(call);
1429 }
1430
yueg32d9ae72018-04-05 17:35:56 -07001431 if (isActivityStarted()) {
1432 inCallActivity.showDialogOrToastForDisconnectedCall(
1433 new DisconnectMessage(inCallActivity, call));
1434 } else {
1435 CharSequence message = new DisconnectMessage(context, call).toastMessage;
1436 if (message != null) {
1437 Toast.makeText(context, message, Toast.LENGTH_LONG).show();
1438 }
1439 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001440 }
1441
1442 /**
1443 * When the state of in-call changes, this is the first method to get called. It determines if the
1444 * UI needs to be started or finished depending on the new state and does it.
1445 */
1446 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001447 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001448 LogUtil.d(
linyuh183cb712017-12-27 17:02:37 -08001449 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + inCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001450
1451 // TODO: Consider a proper state machine implementation
1452
1453 // If the state isn't changing we have already done any starting/stopping of activities in
1454 // a previous pass...so lets cut out early
linyuh183cb712017-12-27 17:02:37 -08001455 if (newState == inCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001456 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001457 return newState;
1458 }
1459
1460 // A new Incoming call means that the user needs to be notified of the the call (since
1461 // it wasn't them who initiated it). We do this through full screen notifications and
1462 // happens indirectly through {@link StatusBarNotifier}.
1463 //
1464 // The process for incoming calls is as follows:
1465 //
1466 // 1) CallList - Announces existence of new INCOMING call
1467 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1468 // - should be set to INCOMING.
1469 // 3) InCallPresenter - This method is called to see if we need to start or finish
1470 // the app given the new state.
1471 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1472 // StatusBarNotifier explicitly to issue a FullScreen Notification
1473 // that will either start the InCallActivity or show the user a
1474 // top-level notification dialog if the user is in an immersive app.
1475 // That notification can also start the InCallActivity.
1476 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1477 // call InCallPresenter::setActivity() to let the presenter
1478 // know that start-up is complete.
1479 //
1480 // [ AND NOW YOU'RE IN THE CALL. voila! ]
Eric Erfanianccca3152017-02-22 16:32:36 -08001481
1482 // A dialog to show on top of the InCallUI to select a PhoneAccount
1483 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1484
1485 // A new outgoing call indicates that the user just now dialed a number and when that
1486 // happens we need to display the screen immediately or show an account picker dialog if
1487 // no default is set. However, if the main InCallUI is already visible, we do not want to
1488 // re-initiate the start-up animation, so we do not need to do anything here.
1489 //
1490 // It is also possible to go into an intermediate state where the call has been initiated
1491 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1492 // This pending outgoing state can also launch the call screen.
1493 //
1494 // This is different from the incoming call sequence because we do not need to shock the
1495 // user with a top-level notification. Just show the call UI normally.
1496 boolean callCardFragmentVisible =
linyuh183cb712017-12-27 17:02:37 -08001497 inCallActivity != null && inCallActivity.getCallCardFragmentVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001498 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1499 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1500
1501 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1502 // outgoing call process, so the UI should be brought up to show an error dialog.
1503 showCallUi |=
linyuh183cb712017-12-27 17:02:37 -08001504 (InCallState.PENDING_OUTGOING == inCallState
Eric Erfanianccca3152017-02-22 16:32:36 -08001505 && InCallState.INCALL == newState
1506 && !isShowingInCallUi());
1507
1508 // Another exception - InCallActivity is in charge of disconnecting a call with no
1509 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1510 // call so that:
1511 // 1) The call can be disconnected correctly
1512 // 2) The UI comes up and correctly displays the error dialog.
1513 // TODO: Remove these special case conditions by making InCallPresenter a true state
1514 // machine. Telecom should also be the component responsible for disconnecting a call
1515 // with no valid accounts.
1516 showCallUi |=
1517 InCallState.PENDING_OUTGOING == newState
1518 && mainUiNotVisible
linyuh183cb712017-12-27 17:02:37 -08001519 && isCallWithNoValidAccounts(callList.getPendingOutgoingCall());
Eric Erfanianccca3152017-02-22 16:32:36 -08001520
1521 // The only time that we have an instance of mInCallActivity and it isn't started is
1522 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1523 // the activity. When it is finally destroyed, we double check if we should bring it back
1524 // up so we aren't going to lose anything by avoiding a second startup here.
linyuh183cb712017-12-27 17:02:37 -08001525 boolean activityIsFinishing = inCallActivity != null && !isActivityStarted();
Eric Erfanianccca3152017-02-22 16:32:36 -08001526 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001527 LogUtil.i(
1528 "InCallPresenter.startOrFinishUi",
linyuh183cb712017-12-27 17:02:37 -08001529 "Undo the state change: " + newState + " -> " + inCallState);
wangqicf61ca02017-08-31 15:32:55 -07001530 Trace.endSection();
linyuh183cb712017-12-27 17:02:37 -08001531 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001532 }
1533
1534 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1535 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1536 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1537 // outgoing call after user ignore it by pressing home button.
1538 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1539 && !showCallUi
1540 && isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001541 inCallActivity.dismissPendingDialogs();
Eric Erfanianccca3152017-02-22 16:32:36 -08001542 }
1543
yueg4613e8f2018-04-23 13:35:33 -07001544 if ((showCallUi || showAccountPicker) && !shouldStartInBubbleMode()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001545 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001546 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001547 } else if (newState == InCallState.NO_CALLS) {
1548 // The new state is the no calls state. Tear everything down.
yueg8e544862018-04-06 14:34:59 -07001549 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001550 attemptFinishActivity();
1551 attemptCleanup();
1552 }
1553
wangqicf61ca02017-08-31 15:32:55 -07001554 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001555 return newState;
1556 }
1557
1558 /**
1559 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1560 * or PhoneAccounts to select from.
1561 */
1562 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1563
1564 Bundle extras = call.getIntentExtras();
1565 // Initialize the extras bundle to avoid NPE
1566 if (extras == null) {
1567 extras = new Bundle();
1568 }
1569
1570 final List<PhoneAccountHandle> phoneAccountHandles =
1571 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1572
1573 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1574 String scheme = call.getHandle().getScheme();
1575 final String errorMsg =
1576 PhoneAccount.SCHEME_TEL.equals(scheme)
linyuh183cb712017-12-27 17:02:37 -08001577 ? context.getString(R.string.callFailed_simError)
1578 : context.getString(R.string.incall_error_supp_service_unknown);
Eric Erfanianccca3152017-02-22 16:32:36 -08001579 DisconnectCause disconnectCause =
1580 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1581 call.setDisconnectCause(disconnectCause);
1582 }
1583 }
1584
Eric Erfanianccca3152017-02-22 16:32:36 -08001585 /**
1586 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1587 * Calling classes should use this as an indication whether to interact with the
1588 * InCallPresenter or not.
1589 */
1590 public boolean isReadyForTearDown() {
linyuh183cb712017-12-27 17:02:37 -08001591 return inCallActivity == null && !serviceConnected && inCallState == InCallState.NO_CALLS;
Eric Erfanianccca3152017-02-22 16:32:36 -08001592 }
1593
1594 /**
1595 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1596 */
1597 private void attemptCleanup() {
1598 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001599 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001600
1601 cleanupSurfaces();
1602
linyuh183cb712017-12-27 17:02:37 -08001603 isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001604
yueg8e544862018-04-06 14:34:59 -07001605 audioRouteSetForBubbleMode = false;
yueg32d9ae72018-04-05 17:35:56 -07001606
Eric Erfanianccca3152017-02-22 16:32:36 -08001607 // blow away stale contact info so that we get fresh data on
1608 // the next set of calls
linyuh183cb712017-12-27 17:02:37 -08001609 if (contactInfoCache != null) {
1610 contactInfoCache.clearCache();
Eric Erfanianccca3152017-02-22 16:32:36 -08001611 }
linyuh183cb712017-12-27 17:02:37 -08001612 contactInfoCache = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001613
linyuh183cb712017-12-27 17:02:37 -08001614 if (proximitySensor != null) {
1615 removeListener(proximitySensor);
1616 proximitySensor.tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -08001617 }
linyuh183cb712017-12-27 17:02:37 -08001618 proximitySensor = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001619
linyuh183cb712017-12-27 17:02:37 -08001620 if (statusBarNotifier != null) {
1621 removeListener(statusBarNotifier);
1622 EnrichedCallComponent.get(context)
Eric Erfaniand8046e52017-04-06 09:41:50 -07001623 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -08001624 .unregisterStateChangedListener(statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001625 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001626
linyuh183cb712017-12-27 17:02:37 -08001627 if (externalCallNotifier != null && externalCallList != null) {
1628 externalCallList.removeExternalCallListener(externalCallNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001629 }
linyuh183cb712017-12-27 17:02:37 -08001630 statusBarNotifier = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001631
linyuh183cb712017-12-27 17:02:37 -08001632 if (callList != null) {
1633 callList.removeListener(this);
1634 callList.removeListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001635 }
linyuh183cb712017-12-27 17:02:37 -08001636 callList = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001637
linyuh183cb712017-12-27 17:02:37 -08001638 context = null;
1639 inCallActivity = null;
1640 manageConferenceActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001641
linyuh183cb712017-12-27 17:02:37 -08001642 listeners.clear();
1643 incomingCallListeners.clear();
1644 detailsListeners.clear();
1645 canAddCallListeners.clear();
1646 orientationListeners.clear();
1647 inCallEventListeners.clear();
1648 inCallUiListeners.clear();
1649 if (!inCallUiLocks.isEmpty()) {
1650 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + inCallUiLocks);
1651 inCallUiLocks.clear();
twyen8efb4952017-10-06 16:35:54 -07001652 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001653 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001654 }
1655 }
1656
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001657 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001658 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
linyuh183cb712017-12-27 17:02:37 -08001659 context.startActivity(
1660 InCallActivity.getIntent(context, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001661 }
1662
1663 public void onServiceBind() {
linyuh183cb712017-12-27 17:02:37 -08001664 serviceBound = true;
Eric Erfanianccca3152017-02-22 16:32:36 -08001665 }
1666
1667 public void onServiceUnbind() {
1668 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -08001669 serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001670 }
1671
1672 public boolean isServiceBound() {
linyuh183cb712017-12-27 17:02:37 -08001673 return serviceBound;
Eric Erfanianccca3152017-02-22 16:32:36 -08001674 }
1675
1676 public void maybeStartRevealAnimation(Intent intent) {
linyuh183cb712017-12-27 17:02:37 -08001677 if (intent == null || inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001678 return;
1679 }
1680 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1681 if (extras == null) {
1682 // Incoming call, just show the in-call UI directly.
1683 return;
1684 }
1685
1686 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1687 // Account selection dialog will show up so don't show the animation.
1688 return;
1689 }
1690
1691 final PhoneAccountHandle accountHandle =
1692 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1693 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001694
yueg32d9ae72018-04-05 17:35:56 -07001695 setBoundAndWaitingForOutgoingCall(true, accountHandle);
1696
yueg4613e8f2018-04-23 13:35:33 -07001697 if (shouldStartInBubbleModeWithExtras(extras)) {
yueg32d9ae72018-04-05 17:35:56 -07001698 LogUtil.i("InCallPresenter.maybeStartRevealAnimation", "shouldStartInBubbleMode");
1699 // Show bubble instead of in call UI
1700 return;
1701 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001702
1703 final Intent activityIntent =
linyuh183cb712017-12-27 17:02:37 -08001704 InCallActivity.getIntent(context, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001705 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
linyuh183cb712017-12-27 17:02:37 -08001706 context.startActivity(activityIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001707 }
1708
1709 /**
1710 * Retrieves the current in-call camera manager instance, creating if necessary.
1711 *
1712 * @return The {@link InCallCameraManager}.
1713 */
1714 public InCallCameraManager getInCallCameraManager() {
1715 synchronized (this) {
linyuh183cb712017-12-27 17:02:37 -08001716 if (inCallCameraManager == null) {
1717 inCallCameraManager = new InCallCameraManager(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001718 }
1719
linyuh183cb712017-12-27 17:02:37 -08001720 return inCallCameraManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001721 }
1722 }
1723
1724 /**
1725 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1726 *
1727 * @param orientation The screen orientation of the device (one of: {@link
1728 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1729 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1730 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1731 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1732 */
1733 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001734 LogUtil.d(
1735 "InCallPresenter.onDeviceOrientationChange",
1736 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001737
linyuh183cb712017-12-27 17:02:37 -08001738 if (callList != null) {
1739 callList.notifyCallsOfDeviceRotation(orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001740 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001741 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001742 }
1743
1744 // Notify listeners of device orientation changed.
linyuh183cb712017-12-27 17:02:37 -08001745 for (InCallOrientationListener listener : orientationListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001746 listener.onDeviceOrientationChanged(orientation);
1747 }
1748 }
1749
1750 /**
1751 * Configures the in-call UI activity so it can change orientations or not. Enables the
1752 * orientation event listener if allowOrientationChange is true, disables it if false.
1753 *
1754 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1755 * landscape. {@code false} if the in-call UI should be locked in portrait.
1756 */
1757 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
linyuh183cb712017-12-27 17:02:37 -08001758 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001759 LogUtil.e(
1760 "InCallPresenter.setInCallAllowsOrientationChange",
1761 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001762 return;
1763 }
linyuh183cb712017-12-27 17:02:37 -08001764 inCallActivity.setAllowOrientationChange(allowOrientationChange);
Eric Erfanianccca3152017-02-22 16:32:36 -08001765 }
1766
1767 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001768 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
linyuh183cb712017-12-27 17:02:37 -08001769 screenTimeoutEnabled = enable;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001770 applyScreenTimeout();
1771 }
1772
1773 private void applyScreenTimeout() {
linyuh183cb712017-12-27 17:02:37 -08001774 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001775 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001776 return;
1777 }
1778
linyuh183cb712017-12-27 17:02:37 -08001779 final Window window = inCallActivity.getWindow();
1780 if (screenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001781 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1782 } else {
1783 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1784 }
1785 }
1786
1787 /**
1788 * Hides or shows the conference manager fragment.
1789 *
1790 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1791 * be hidden.
1792 */
1793 public void showConferenceCallManager(boolean show) {
linyuh183cb712017-12-27 17:02:37 -08001794 if (inCallActivity != null) {
1795 inCallActivity.showConferenceFragment(show);
Eric Erfanianccca3152017-02-22 16:32:36 -08001796 }
linyuh183cb712017-12-27 17:02:37 -08001797 if (!show && manageConferenceActivity != null) {
1798 manageConferenceActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -08001799 }
1800 }
1801
1802 /**
1803 * Determines if the dialpad is visible.
1804 *
1805 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1806 */
1807 public boolean isDialpadVisible() {
linyuh183cb712017-12-27 17:02:37 -08001808 if (inCallActivity == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001809 return false;
1810 }
linyuh183cb712017-12-27 17:02:37 -08001811 return inCallActivity.isDialpadVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001812 }
1813
1814 public ThemeColorManager getThemeColorManager() {
linyuh183cb712017-12-27 17:02:37 -08001815 return themeColorManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001816 }
1817
wangqi8d662ca2017-10-26 11:27:19 -07001818 @VisibleForTesting
1819 public void setThemeColorManager(ThemeColorManager themeColorManager) {
linyuh183cb712017-12-27 17:02:37 -08001820 this.themeColorManager = themeColorManager;
wangqi8d662ca2017-10-26 11:27:19 -07001821 }
1822
Eric Erfanianccca3152017-02-22 16:32:36 -08001823 /** Called when the foreground call changes. */
1824 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh183cb712017-12-27 17:02:37 -08001825 themeColorManager.onForegroundCallChanged(context, newForegroundCall);
1826 if (inCallActivity != null) {
1827 inCallActivity.onForegroundCallChanged(newForegroundCall);
Eric Erfanianccca3152017-02-22 16:32:36 -08001828 }
1829 }
1830
1831 public InCallActivity getActivity() {
linyuh183cb712017-12-27 17:02:37 -08001832 return inCallActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -08001833 }
1834
1835 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1836 public void setActivity(InCallActivity inCallActivity) {
1837 if (inCallActivity == null) {
1838 throw new IllegalArgumentException("registerActivity cannot be called with null");
1839 }
linyuh183cb712017-12-27 17:02:37 -08001840 if (this.inCallActivity != null && this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001841 LogUtil.w(
1842 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001843 }
1844 updateActivity(inCallActivity);
1845 }
1846
1847 ExternalCallNotifier getExternalCallNotifier() {
linyuh183cb712017-12-27 17:02:37 -08001848 return externalCallNotifier;
Eric Erfanianccca3152017-02-22 16:32:36 -08001849 }
1850
1851 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001852 if (localVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001853 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001854 if (context != null) {
1855 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001856 }
linyuh183cb712017-12-27 17:02:37 -08001857 localVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001858 }
linyuh183cb712017-12-27 17:02:37 -08001859 return localVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001860 }
1861
1862 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001863 if (remoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001864 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001865 if (context != null) {
1866 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001867 }
linyuh183cb712017-12-27 17:02:37 -08001868 remoteVideoSurfaceTexture = VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001869 }
linyuh183cb712017-12-27 17:02:37 -08001870 return remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001871 }
1872
1873 void cleanupSurfaces() {
linyuh183cb712017-12-27 17:02:37 -08001874 if (remoteVideoSurfaceTexture != null) {
1875 remoteVideoSurfaceTexture.setDoneWithSurface();
1876 remoteVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001877 }
linyuh183cb712017-12-27 17:02:37 -08001878 if (localVideoSurfaceTexture != null) {
1879 localVideoSurfaceTexture.setDoneWithSurface();
1880 localVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001881 }
1882 }
1883
yueg77cb8e52017-10-27 16:42:51 -07001884 @Override
1885 public void onAudioStateChanged(CallAudioState audioState) {
yueg8e544862018-04-06 14:34:59 -07001886 // Set sensible audio route for bubble mode when we get real audio state for the first time
1887 // During the first time this function is called, supportedRouteMask is set to
yueg4613e8f2018-04-23 13:35:33 -07001888 // SUPPORTED_AUDIO_ROUTE_ALL, but it's OK since shouldStartInBubbleMode() is false at that time
1889 // (callList not updated yet).
1890 if (!audioRouteSetForBubbleMode && shouldStartInBubbleMode()) {
yueg8e544862018-04-06 14:34:59 -07001891 setAudioRouteForBubbleMode(audioState);
1892 audioRouteSetForBubbleMode = true;
1893 }
1894
linyuh183cb712017-12-27 17:02:37 -08001895 if (statusBarNotifier != null) {
1896 statusBarNotifier.updateNotification();
yueg77cb8e52017-10-27 16:42:51 -07001897 }
1898 }
1899
yueg8e544862018-04-06 14:34:59 -07001900 /**
1901 * Set audio route to make audio sensible. According to availability, set audio route to Bluetooth
1902 * or wired headset or speaker.
1903 */
1904 private void setAudioRouteForBubbleMode(CallAudioState audioState) {
1905 if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
1906 == CallAudioState.ROUTE_BLUETOOTH) {
1907 // Use Bluetooth if available
1908 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
1909 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "bluetooth");
1910 } else if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_WIRED_HEADSET)
1911 == CallAudioState.ROUTE_WIRED_HEADSET) {
1912 // Use wired headset if available
1913 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_WIRED_HEADSET);
1914 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "wired headset");
1915 } else {
1916 // Use speaker
1917 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_SPEAKER);
1918 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "speaker");
1919 }
1920 }
1921
Eric Erfanianccca3152017-02-22 16:32:36 -08001922 /** All the main states of InCallActivity. */
1923 public enum InCallState {
1924 // InCall Screen is off and there are no calls
1925 NO_CALLS,
1926
1927 // Incoming-call screen is up
1928 INCOMING,
1929
1930 // In-call experience is showing
1931 INCALL,
1932
1933 // Waiting for user input before placing outgoing call
1934 WAITING_FOR_ACCOUNT,
1935
1936 // UI is starting up but no call has been initiated yet.
1937 // The UI is waiting for Telecom to respond.
1938 PENDING_OUTGOING,
1939
1940 // User is dialing out
1941 OUTGOING;
1942
1943 public boolean isIncoming() {
1944 return (this == INCOMING);
1945 }
1946
1947 public boolean isConnectingOrConnected() {
1948 return (this == INCOMING || this == OUTGOING || this == INCALL);
1949 }
1950 }
1951
1952 /** Interface implemented by classes that need to know about the InCall State. */
1953 public interface InCallStateListener {
1954
1955 // TODO: Enhance state to contain the call objects instead of passing CallList
1956 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1957 }
1958
1959 public interface IncomingCallListener {
1960
1961 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1962 }
1963
1964 public interface CanAddCallListener {
1965
1966 void onCanAddCallChanged(boolean canAddCall);
1967 }
1968
1969 public interface InCallDetailsListener {
1970
1971 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1972 }
1973
1974 public interface InCallOrientationListener {
1975
1976 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1977 }
1978
1979 /**
1980 * Interface implemented by classes that need to know about events which occur within the In-Call
1981 * UI. Used as a means of communicating between fragments that make up the UI.
1982 */
1983 public interface InCallEventListener {
1984
1985 void onFullscreenModeChanged(boolean isFullscreenMode);
1986 }
1987
1988 public interface InCallUiListener {
1989
1990 void onUiShowing(boolean showing);
1991 }
twyen8efb4952017-10-06 16:35:54 -07001992
1993 private class InCallUiLockImpl implements InCallUiLock {
1994 private final String tag;
1995
1996 private InCallUiLockImpl(String tag) {
1997 this.tag = tag;
1998 }
1999
2000 @MainThread
2001 @Override
2002 public void release() {
2003 Assert.isMainThread();
2004 releaseInCallUiLock(InCallUiLockImpl.this);
2005 }
2006
2007 @Override
2008 public String toString() {
2009 return "InCallUiLock[" + tag + "]";
2010 }
2011 }
2012
2013 @MainThread
2014 public InCallUiLock acquireInCallUiLock(String tag) {
2015 Assert.isMainThread();
2016 InCallUiLock lock = new InCallUiLockImpl(tag);
linyuh183cb712017-12-27 17:02:37 -08002017 inCallUiLocks.add(lock);
twyen8efb4952017-10-06 16:35:54 -07002018 return lock;
2019 }
2020
2021 @MainThread
2022 private void releaseInCallUiLock(InCallUiLock lock) {
2023 Assert.isMainThread();
2024 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
linyuh183cb712017-12-27 17:02:37 -08002025 inCallUiLocks.remove(lock);
2026 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002027 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
linyuh183cb712017-12-27 17:02:37 -08002028 if (inCallState == InCallState.NO_CALLS) {
twyen8efb4952017-10-06 16:35:54 -07002029 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
2030 attemptFinishActivity();
2031 attemptCleanup();
2032 }
2033 }
2034 }
2035
2036 @MainThread
2037 public boolean isInCallUiLocked() {
2038 Assert.isMainThread();
linyuh183cb712017-12-27 17:02:37 -08002039 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002040 return false;
2041 }
linyuh183cb712017-12-27 17:02:37 -08002042 for (InCallUiLock lock : inCallUiLocks) {
twyen8efb4952017-10-06 16:35:54 -07002043 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
2044 }
2045 return true;
2046 }
2047
linyuh183cb712017-12-27 17:02:37 -08002048 private final Set<InCallUiLock> inCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08002049}