blob: 17af75691e73ec722702db3e5747d8a68fbcb3ce [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;
yuegb493c3f2018-04-27 14:00:12 -070055import com.android.dialer.logging.DialerImpression;
Eric Erfanian8369df02017-05-03 10:27:13 -070056import com.android.dialer.logging.InteractionEvent;
Eric Erfanianccca3152017-02-22 16:32:36 -080057import com.android.dialer.logging.Logger;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070058import com.android.dialer.postcall.PostCall;
twyena4745bd2017-12-12 18:40:11 -080059import com.android.dialer.telecom.TelecomCallUtil;
Eric Erfanianccca3152017-02-22 16:32:36 -080060import com.android.dialer.telecom.TelecomUtil;
61import com.android.dialer.util.TouchPointManager;
62import com.android.incallui.InCallOrientationEventListener.ScreenOrientation;
63import com.android.incallui.answerproximitysensor.PseudoScreenState;
yueg77cb8e52017-10-27 16:42:51 -070064import com.android.incallui.audiomode.AudioModeProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080065import com.android.incallui.call.CallList;
66import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080067import com.android.incallui.call.ExternalCallList;
Eric Erfanianccca3152017-02-22 16:32:36 -080068import com.android.incallui.call.TelecomAdapter;
wangqibb94ca62018-04-27 14:34:04 -070069import com.android.incallui.call.state.DialerCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -070070import com.android.incallui.disconnectdialog.DisconnectMessage;
twyen8efb4952017-10-06 16:35:54 -070071import com.android.incallui.incalluilock.InCallUiLock;
Eric Erfanianccca3152017-02-22 16:32:36 -080072import com.android.incallui.latencyreport.LatencyReport;
73import com.android.incallui.legacyblocking.BlockedNumberContentObserver;
74import com.android.incallui.spam.SpamCallListListener;
erfaniand05d8992018-03-20 19:42:26 -070075import com.android.incallui.speakeasy.SpeakEasyCallManager;
yuegb47528e2018-04-24 12:12:57 -070076import com.android.incallui.telecomeventui.InternationalCallOnWifiDialogActivity;
77import com.android.incallui.telecomeventui.InternationalCallOnWifiDialogFragment;
Eric Erfanianccca3152017-02-22 16:32:36 -080078import com.android.incallui.videosurface.bindings.VideoSurfaceBindings;
79import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
Eric Erfanian90508232017-03-24 09:31:16 -070080import com.android.incallui.videotech.utils.VideoUtils;
yueg32d9ae72018-04-05 17:35:56 -070081import com.google.protobuf.InvalidProtocolBufferException;
Eric Erfanianccca3152017-02-22 16:32:36 -080082import java.util.Collections;
83import java.util.List;
84import java.util.Objects;
85import java.util.Set;
86import java.util.concurrent.ConcurrentHashMap;
87import java.util.concurrent.CopyOnWriteArrayList;
88import java.util.concurrent.atomic.AtomicBoolean;
89
90/**
91 * Takes updates from the CallList and notifies the InCallActivity (UI) of the changes. Responsible
92 * for starting the activity for a new call and finishing the activity when all calls are
93 * disconnected. Creates and manages the in-call state and provides a listener pattern for the
94 * presenters that want to listen in on the in-call state changes. TODO: This class has become more
95 * of a state machine at this point. Consider renaming.
96 */
yueg77cb8e52017-10-27 16:42:51 -070097public class InCallPresenter implements CallList.Listener, AudioModeProvider.AudioModeListener {
Eric Erfanian2ca43182017-08-31 06:57:16 -070098 private static final String PIXEL2017_SYSTEM_FEATURE =
99 "com.google.android.feature.PIXEL_2017_EXPERIENCE";
yueg32d9ae72018-04-05 17:35:56 -0700100 private static final String CALL_CONFIGURATION_EXTRA = "call_configuration";
Eric Erfanianccca3152017-02-22 16:32:36 -0800101
102 private static final long BLOCK_QUERY_TIMEOUT_MS = 1000;
103
104 private static final Bundle EMPTY_EXTRAS = new Bundle();
105
linyuh183cb712017-12-27 17:02:37 -0800106 private static InCallPresenter inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -0800107
108 /**
109 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is load factor before
110 * resizing, 1 means we only expect a single thread to access the map so make only a single shard
111 */
linyuh183cb712017-12-27 17:02:37 -0800112 private final Set<InCallStateListener> listeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800113 Collections.newSetFromMap(new ConcurrentHashMap<InCallStateListener, Boolean>(8, 0.9f, 1));
114
linyuh183cb712017-12-27 17:02:37 -0800115 private final List<IncomingCallListener> incomingCallListeners = new CopyOnWriteArrayList<>();
116 private final Set<InCallDetailsListener> detailsListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800117 Collections.newSetFromMap(new ConcurrentHashMap<InCallDetailsListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800118 private final Set<CanAddCallListener> canAddCallListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800119 Collections.newSetFromMap(new ConcurrentHashMap<CanAddCallListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800120 private final Set<InCallUiListener> inCallUiListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800121 Collections.newSetFromMap(new ConcurrentHashMap<InCallUiListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800122 private final Set<InCallOrientationListener> orientationListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800123 Collections.newSetFromMap(
124 new ConcurrentHashMap<InCallOrientationListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800125 private final Set<InCallEventListener> inCallEventListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800126 Collections.newSetFromMap(new ConcurrentHashMap<InCallEventListener, Boolean>(8, 0.9f, 1));
127
linyuh183cb712017-12-27 17:02:37 -0800128 private StatusBarNotifier statusBarNotifier;
129 private ExternalCallNotifier externalCallNotifier;
130 private ContactInfoCache contactInfoCache;
131 private Context context;
132 private final OnCheckBlockedListener onCheckBlockedListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800133 new OnCheckBlockedListener() {
134 @Override
135 public void onCheckComplete(final Integer id) {
136 if (id != null && id != FilteredNumberAsyncQueryHandler.INVALID_ID) {
137 // Silence the ringer now to prevent ringing and vibration before the call is
138 // terminated when Telecom attempts to add it.
linyuh183cb712017-12-27 17:02:37 -0800139 TelecomUtil.silenceRinger(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800140 }
141 }
142 };
linyuh183cb712017-12-27 17:02:37 -0800143 private CallList callList;
144 private ExternalCallList externalCallList;
145 private InCallActivity inCallActivity;
146 private ManageConferenceActivity manageConferenceActivity;
147 private final android.telecom.Call.Callback callCallback =
Eric Erfanianccca3152017-02-22 16:32:36 -0800148 new android.telecom.Call.Callback() {
149 @Override
150 public void onPostDialWait(
151 android.telecom.Call telecomCall, String remainingPostDialSequence) {
linyuh183cb712017-12-27 17:02:37 -0800152 final DialerCall call = callList.getDialerCallFromTelecomCall(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800153 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700154 LogUtil.w(
155 "InCallPresenter.onPostDialWait",
156 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800157 return;
158 }
159 onPostDialCharWait(call.getId(), remainingPostDialSequence);
160 }
161
162 @Override
163 public void onDetailsChanged(
164 android.telecom.Call telecomCall, android.telecom.Call.Details details) {
linyuh183cb712017-12-27 17:02:37 -0800165 final DialerCall call = callList.getDialerCallFromTelecomCall(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800166 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700167 LogUtil.w(
168 "InCallPresenter.onDetailsChanged",
169 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800170 return;
171 }
172
173 if (details.hasProperty(Details.PROPERTY_IS_EXTERNAL_CALL)
linyuh183cb712017-12-27 17:02:37 -0800174 && !externalCallList.isCallTracked(telecomCall)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800175
176 // A regular call became an external call so swap call lists.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700177 LogUtil.i("InCallPresenter.onDetailsChanged", "Call became external: " + telecomCall);
linyuh183cb712017-12-27 17:02:37 -0800178 callList.onInternalCallMadeExternal(context, telecomCall);
179 externalCallList.onCallAdded(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800180 return;
181 }
182
linyuh183cb712017-12-27 17:02:37 -0800183 for (InCallDetailsListener listener : detailsListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800184 listener.onDetailsChanged(call, details);
185 }
186 }
187
188 @Override
189 public void onConferenceableCallsChanged(
190 android.telecom.Call telecomCall, List<android.telecom.Call> conferenceableCalls) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700191 LogUtil.i(
192 "InCallPresenter.onConferenceableCallsChanged",
193 "onConferenceableCallsChanged: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800194 onDetailsChanged(telecomCall, telecomCall.getDetails());
195 }
196 };
linyuh183cb712017-12-27 17:02:37 -0800197 private InCallState inCallState = InCallState.NO_CALLS;
198 private ProximitySensor proximitySensor;
199 private final PseudoScreenState pseudoScreenState = new PseudoScreenState();
200 private boolean serviceConnected;
201 private InCallCameraManager inCallCameraManager;
202 private FilteredNumberAsyncQueryHandler filteredQueryHandler;
203 private CallList.Listener spamCallListListener;
twyen27c37182018-04-24 11:48:12 -0700204 private CallList.Listener activeCallsListener;
Eric Erfanianccca3152017-02-22 16:32:36 -0800205 /** Whether or not we are currently bound and waiting for Telecom to send us a new call. */
linyuh183cb712017-12-27 17:02:37 -0800206 private boolean boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -0800207 /** Determines if the InCall UI is in fullscreen mode or not. */
linyuh183cb712017-12-27 17:02:37 -0800208 private boolean isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800209
linyuh183cb712017-12-27 17:02:37 -0800210 private boolean screenTimeoutEnabled = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700211
linyuh183cb712017-12-27 17:02:37 -0800212 private PhoneStateListener phoneStateListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800213 new PhoneStateListener() {
214 @Override
215 public void onCallStateChanged(int state, String incomingNumber) {
216 if (state == TelephonyManager.CALL_STATE_RINGING) {
linyuh183cb712017-12-27 17:02:37 -0800217 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800218 return;
219 }
220 // Check if the number is blocked, to silence the ringer.
linyuh183cb712017-12-27 17:02:37 -0800221 String countryIso = GeoUtil.getCurrentCountryIso(context);
222 filteredQueryHandler.isBlockedNumber(
223 onCheckBlockedListener, incomingNumber, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800224 }
225 }
226 };
twyena4745bd2017-12-12 18:40:11 -0800227
Eric Erfanianccca3152017-02-22 16:32:36 -0800228 /** Whether or not InCallService is bound to Telecom. */
linyuh183cb712017-12-27 17:02:37 -0800229 private boolean serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800230
231 /**
232 * When configuration changes Android kills the current activity and starts a new one. The flag is
233 * used to check if full clean up is necessary (activity is stopped and new activity won't be
234 * started), or if a new activity will be started right after the current one is destroyed, and
235 * therefore no need in release all resources.
236 */
linyuh183cb712017-12-27 17:02:37 -0800237 private boolean isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800238
linyuh183cb712017-12-27 17:02:37 -0800239 private boolean awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800240
linyuh183cb712017-12-27 17:02:37 -0800241 private ExternalCallList.ExternalCallListener externalCallListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800242 new ExternalCallList.ExternalCallListener() {
243
244 @Override
245 public void onExternalCallPulled(android.telecom.Call call) {
246 // Note: keep this code in sync with InCallPresenter#onCallAdded
247 LatencyReport latencyReport = new LatencyReport(call);
248 latencyReport.onCallBlockingDone();
249 // Note: External calls do not require spam checking.
linyuh183cb712017-12-27 17:02:37 -0800250 callList.onCallAdded(context, call, latencyReport);
251 call.registerCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800252 }
253
254 @Override
255 public void onExternalCallAdded(android.telecom.Call call) {
256 // No-op
257 }
258
259 @Override
260 public void onExternalCallRemoved(android.telecom.Call call) {
261 // No-op
262 }
263
264 @Override
265 public void onExternalCallUpdated(android.telecom.Call call) {
266 // No-op
267 }
268 };
269
linyuh183cb712017-12-27 17:02:37 -0800270 private ThemeColorManager themeColorManager;
271 private VideoSurfaceTexture localVideoSurfaceTexture;
272 private VideoSurfaceTexture remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -0800273
erfaniand05d8992018-03-20 19:42:26 -0700274 private SpeakEasyCallManager speakEasyCallManager;
275
Sekine Yasuakia7020842018-05-31 17:10:12 +0900276 private boolean addCallClicked = false;
277 private boolean automaticallyMutedByAddCall = false;
278
Eric Erfanian10b34a52017-05-04 08:23:17 -0700279 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800280 @VisibleForTesting
281 InCallPresenter() {}
282
283 public static synchronized InCallPresenter getInstance() {
linyuh183cb712017-12-27 17:02:37 -0800284 if (inCallPresenter == null) {
wangqic8cf79e2017-10-17 09:21:00 -0700285 Trace.beginSection("InCallPresenter.Constructor");
linyuh183cb712017-12-27 17:02:37 -0800286 inCallPresenter = new InCallPresenter();
wangqic8cf79e2017-10-17 09:21:00 -0700287 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800288 }
linyuh183cb712017-12-27 17:02:37 -0800289 return inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -0800290 }
291
Eric Erfanian10b34a52017-05-04 08:23:17 -0700292 @VisibleForTesting
293 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
linyuh183cb712017-12-27 17:02:37 -0800294 InCallPresenter.inCallPresenter = inCallPresenter;
Eric Erfanian10b34a52017-05-04 08:23:17 -0700295 }
296
Eric Erfanianccca3152017-02-22 16:32:36 -0800297 /**
298 * Determines whether or not a call has no valid phone accounts that can be used to make the call
299 * with. Emergency calls do not require a phone account.
300 *
301 * @param call to check accounts for.
302 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
303 * call contains a phone account that could be used to initiate it with, or is an emergency
304 * call.
305 */
306 public static boolean isCallWithNoValidAccounts(DialerCall call) {
307 if (call != null && !call.isEmergencyCall()) {
308 Bundle extras = call.getIntentExtras();
309
310 if (extras == null) {
311 extras = EMPTY_EXTRAS;
312 }
313
314 final List<PhoneAccountHandle> phoneAccountHandles =
315 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
316
317 if ((call.getAccountHandle() == null
318 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700319 LogUtil.i(
320 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800321 return true;
322 }
323 }
324 return false;
325 }
326
327 public InCallState getInCallState() {
linyuh183cb712017-12-27 17:02:37 -0800328 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800329 }
330
331 public CallList getCallList() {
linyuh183cb712017-12-27 17:02:37 -0800332 return callList;
Eric Erfanianccca3152017-02-22 16:32:36 -0800333 }
334
335 public void setUp(
336 @NonNull Context context,
337 CallList callList,
338 ExternalCallList externalCallList,
339 StatusBarNotifier statusBarNotifier,
340 ExternalCallNotifier externalCallNotifier,
341 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700342 ProximitySensor proximitySensor,
erfaniand05d8992018-03-20 19:42:26 -0700343 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler,
yueg4613e8f2018-04-23 13:35:33 -0700344 @NonNull SpeakEasyCallManager speakEasyCallManager) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700345 Trace.beginSection("InCallPresenter.setUp");
linyuh183cb712017-12-27 17:02:37 -0800346 if (serviceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700347 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
linyuh183cb712017-12-27 17:02:37 -0800348 if (context != this.context || callList != this.callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800349 throw new IllegalStateException();
350 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700351 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800352 return;
353 }
354
355 Objects.requireNonNull(context);
linyuh183cb712017-12-27 17:02:37 -0800356 this.context = context;
Eric Erfanianccca3152017-02-22 16:32:36 -0800357
linyuh183cb712017-12-27 17:02:37 -0800358 this.contactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800359
linyuh183cb712017-12-27 17:02:37 -0800360 this.statusBarNotifier = statusBarNotifier;
361 this.externalCallNotifier = externalCallNotifier;
362 addListener(this.statusBarNotifier);
363 EnrichedCallComponent.get(this.context)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700364 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -0800365 .registerStateChangedListener(this.statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800366
linyuh183cb712017-12-27 17:02:37 -0800367 this.proximitySensor = proximitySensor;
368 addListener(this.proximitySensor);
Eric Erfanianccca3152017-02-22 16:32:36 -0800369
linyuh183cb712017-12-27 17:02:37 -0800370 if (themeColorManager == null) {
calderwoodraa93df432018-05-23 12:59:03 -0700371 themeColorManager = new ThemeColorManager(new InCallUIMaterialColorMapUtils(this.context));
wangqi8d662ca2017-10-26 11:27:19 -0700372 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800373
linyuh183cb712017-12-27 17:02:37 -0800374 this.callList = callList;
375 this.externalCallList = externalCallList;
376 externalCallList.addExternalCallListener(this.externalCallNotifier);
377 externalCallList.addExternalCallListener(externalCallListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800378
379 // This only gets called by the service so this is okay.
linyuh183cb712017-12-27 17:02:37 -0800380 serviceConnected = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800381
382 // The final thing we do in this set up is add ourselves as a listener to CallList. This
383 // will kick off an update and the whole process can start.
linyuh183cb712017-12-27 17:02:37 -0800384 this.callList.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800385
386 // Create spam call list listener and add it to the list of listeners
linyuh183cb712017-12-27 17:02:37 -0800387 spamCallListListener =
zachh6a4cebd2017-10-24 17:10:06 -0700388 new SpamCallListListener(
389 context, DialerExecutorComponent.get(context).dialerExecutorFactory());
linyuh183cb712017-12-27 17:02:37 -0800390 this.callList.addListener(spamCallListListener);
twyen27c37182018-04-24 11:48:12 -0700391 activeCallsListener = new ActiveCallsCallListListener(context);
392 this.callList.addListener(activeCallsListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800393
394 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800395
linyuh183cb712017-12-27 17:02:37 -0800396 filteredQueryHandler = filteredNumberQueryHandler;
erfaniand05d8992018-03-20 19:42:26 -0700397 this.speakEasyCallManager = speakEasyCallManager;
linyuh183cb712017-12-27 17:02:37 -0800398 this.context
Eric Erfanianccca3152017-02-22 16:32:36 -0800399 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800400 .listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800401
yueg77cb8e52017-10-27 16:42:51 -0700402 AudioModeProvider.getInstance().addListener(this);
403
roldenburg42099012018-04-09 14:47:15 -0700404 // Add listener to notify Telephony process when the incoming call screen is started or
405 // finished. This is for hiding USSD dialog because the incoming call screen should have
406 // higher precedence over this dialog.
407 MotorolaInCallUiNotifier motorolaInCallUiNotifier = new MotorolaInCallUiNotifier(context);
408 addInCallUiListener(motorolaInCallUiNotifier);
409 addListener(motorolaInCallUiNotifier);
yueg7f5acbe2018-01-10 13:50:29 -0800410
Eric Erfanian2ca43182017-08-31 06:57:16 -0700411 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
412 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800413 }
414
415 /**
yueg32d9ae72018-04-05 17:35:56 -0700416 * Return whether we should start call in bubble mode and not show InCallActivity. The call mode
417 * should be set in CallConfiguration in EXTRA_OUTGOING_CALL_EXTRAS when starting a call intent.
418 */
yueg71bf7e42018-04-30 12:00:48 -0700419 public boolean shouldStartInBubbleMode() {
yueg32d9ae72018-04-05 17:35:56 -0700420 if (!ReturnToCallController.isEnabled(context)) {
421 return false;
422 }
yueg4613e8f2018-04-23 13:35:33 -0700423
424 // We only start in Bubble mode for outgoing call
425 DialerCall dialerCall = callList.getPendingOutgoingCall();
426 if (dialerCall == null) {
427 dialerCall = callList.getOutgoingCall();
428 }
yueg743b5012018-05-30 13:26:14 -0700429 // Outgoing call can be disconnected and reason will be shown in toast
430 if (dialerCall == null) {
431 dialerCall = callList.getDisconnectedCall();
432 }
yueg4613e8f2018-04-23 13:35:33 -0700433 if (dialerCall == null) {
yueg32d9ae72018-04-05 17:35:56 -0700434 return false;
435 }
yueg2e366472018-04-30 18:08:44 -0700436 if (dialerCall.isEmergencyCall()) {
437 return false;
438 }
yueg4613e8f2018-04-23 13:35:33 -0700439
440 Bundle extras = dialerCall.getIntentExtras();
yuegb493c3f2018-04-27 14:00:12 -0700441 boolean result = shouldStartInBubbleModeWithExtras(extras);
442 if (result) {
443 Logger.get(context)
444 .logCallImpression(
445 DialerImpression.Type.START_CALL_IN_BUBBLE_MODE,
446 dialerCall.getUniqueCallId(),
447 dialerCall.getTimeAddedMs());
448 }
449 return result;
yueg4613e8f2018-04-23 13:35:33 -0700450 }
451
452 private boolean shouldStartInBubbleModeWithExtras(Bundle outgoingExtras) {
453 if (!ReturnToCallController.isEnabled(context)) {
454 return false;
455 }
456
457 if (outgoingExtras == null) {
458 return false;
459 }
460 byte[] callConfigurationByteArray = outgoingExtras.getByteArray(CALL_CONFIGURATION_EXTRA);
yueg32d9ae72018-04-05 17:35:56 -0700461 if (callConfigurationByteArray == null) {
462 return false;
463 }
464 try {
465 CallConfiguration callConfiguration = CallConfiguration.parseFrom(callConfigurationByteArray);
466 LogUtil.i(
467 "InCallPresenter.shouldStartInBubbleMode",
468 "call mode: " + callConfiguration.getCallMode());
469 return callConfiguration.getCallMode() == Mode.BUBBLE;
470 } catch (InvalidProtocolBufferException e) {
471 return false;
472 }
473 }
474
475 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800476 * Called when the telephony service has disconnected from us. This will happen when there are no
477 * more active calls. However, we may still want to continue showing the UI for certain cases like
478 * showing "Call Ended". What we really want is to wait for the activity and the service to both
479 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
480 * secondary method that performs the aforementioned logic.
481 */
482 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700483 LogUtil.d("InCallPresenter.tearDown", "tearDown");
linyuh183cb712017-12-27 17:02:37 -0800484 callList.clearOnDisconnect();
Eric Erfanianccca3152017-02-22 16:32:36 -0800485
linyuh183cb712017-12-27 17:02:37 -0800486 serviceConnected = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800487
linyuh183cb712017-12-27 17:02:37 -0800488 context
Eric Erfanianccca3152017-02-22 16:32:36 -0800489 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800490 .listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800491
492 attemptCleanup();
493 VideoPauseController.getInstance().tearDown();
yueg77cb8e52017-10-27 16:42:51 -0700494 AudioModeProvider.getInstance().removeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800495 }
496
497 private void attemptFinishActivity() {
linyuh183cb712017-12-27 17:02:37 -0800498 screenTimeoutEnabled = true;
499 final boolean doFinish = (inCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700500 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800501 if (doFinish) {
linyuh183cb712017-12-27 17:02:37 -0800502 inCallActivity.setExcludeFromRecents(true);
503 inCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800504 }
505 }
506
507 /**
508 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
509 * for more insight on the tear-down process.
510 */
511 public void unsetActivity(InCallActivity inCallActivity) {
512 if (inCallActivity == null) {
513 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
514 }
linyuh183cb712017-12-27 17:02:37 -0800515 if (this.inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700516 LogUtil.i(
517 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800518 return;
519 }
linyuh183cb712017-12-27 17:02:37 -0800520 if (this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700521 LogUtil.w(
522 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800523 "Second instance of InCallActivity is trying to unregister when another"
524 + " instance is active. Ignoring.");
525 return;
526 }
527 updateActivity(null);
528 }
529
530 /**
531 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
532 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
533 */
534 private void updateActivity(InCallActivity inCallActivity) {
wangqi9982f0d2017-10-11 17:46:07 -0700535 Trace.beginSection("InCallPresenter.updateActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800536 boolean updateListeners = false;
537 boolean doAttemptCleanup = false;
538
539 if (inCallActivity != null) {
linyuh183cb712017-12-27 17:02:37 -0800540 if (this.inCallActivity == null) {
541 context = inCallActivity.getApplicationContext();
Eric Erfanianccca3152017-02-22 16:32:36 -0800542 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700543 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800544 } else {
545 // since setActivity is called onStart(), it can be called multiple times.
546 // This is fine and ignorable, but we do not want to update the world every time
547 // this happens (like going to/from background) so we do not set updateListeners.
548 }
549
linyuh183cb712017-12-27 17:02:37 -0800550 this.inCallActivity = inCallActivity;
551 this.inCallActivity.setExcludeFromRecents(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800552
553 // By the time the UI finally comes up, the call may already be disconnected.
554 // If that's the case, we may need to show an error dialog.
linyuh183cb712017-12-27 17:02:37 -0800555 if (callList != null && callList.getDisconnectedCall() != null) {
556 showDialogOrToastForDisconnectedCall(callList.getDisconnectedCall());
Eric Erfanianccca3152017-02-22 16:32:36 -0800557 }
558
559 // When the UI comes up, we need to first check the in-call state.
560 // If we are showing NO_CALLS, that means that a call probably connected and
561 // then immediately disconnected before the UI was able to come up.
562 // If we dont have any calls, start tearing down the UI instead.
563 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
564 // it has been set.
linyuh183cb712017-12-27 17:02:37 -0800565 if (inCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700566 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800567 attemptFinishActivity();
wangqi9982f0d2017-10-11 17:46:07 -0700568 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800569 return;
570 }
571 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700572 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800573 updateListeners = true;
linyuh183cb712017-12-27 17:02:37 -0800574 this.inCallActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800575
576 // We attempt cleanup for the destroy case but only after we recalculate the state
577 // to see if we need to come back up or stay shut down. This is why we do the
578 // cleanup after the call to onCallListChange() instead of directly here.
579 doAttemptCleanup = true;
580 }
581
582 // Messages can come from the telephony layer while the activity is coming up
583 // and while the activity is going down. So in both cases we need to recalculate what
584 // state we should be in after they complete.
585 // Examples: (1) A new incoming call could come in and then get disconnected before
586 // the activity is created.
587 // (2) All calls could disconnect and then get a new incoming call before the
588 // activity is destroyed.
589 //
Eric Erfanian938468d2017-10-24 14:05:52 -0700590 // a bug - We previously had a check for mServiceConnected here as well, but there are
Eric Erfanianccca3152017-02-22 16:32:36 -0800591 // cases where we need to recalculate the current state even if the service in not
592 // connected. In particular the case where startOrFinish() is called while the app is
593 // already finish()ing. In that case, we skip updating the state with the knowledge that
594 // we will check again once the activity has finished. That means we have to recalculate the
595 // state here even if the service is disconnected since we may not have finished a state
596 // transition while finish()ing.
597 if (updateListeners) {
linyuh183cb712017-12-27 17:02:37 -0800598 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800599 }
600
601 if (doAttemptCleanup) {
602 attemptCleanup();
603 }
wangqi9982f0d2017-10-11 17:46:07 -0700604 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800605 }
606
erfanian12243de2018-04-17 12:17:08 -0700607 public SpeakEasyCallManager getSpeakEasyCallManager() {
608 return this.speakEasyCallManager;
609 }
610
Eric Erfanianccca3152017-02-22 16:32:36 -0800611 public void setManageConferenceActivity(
612 @Nullable ManageConferenceActivity manageConferenceActivity) {
linyuh183cb712017-12-27 17:02:37 -0800613 this.manageConferenceActivity = manageConferenceActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -0800614 }
615
616 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700617 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800618 bringToForeground(showDialpad);
619 }
620
621 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700622 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800623 LatencyReport latencyReport = new LatencyReport(call);
624 if (shouldAttemptBlocking(call)) {
625 maybeBlockCall(call, latencyReport);
626 } else {
627 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800628 externalCallList.onCallAdded(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800629 } else {
630 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800631 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800632 }
633 }
634
635 // Since a call has been added we are no longer waiting for Telecom to send us a call.
636 setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -0800637 call.registerCallback(callCallback);
zachh78e54ac2017-12-05 16:38:35 -0800638 // TODO(maxwelb): Return the future in recordPhoneLookupInfo and propagate.
linyuh183cb712017-12-27 17:02:37 -0800639 PhoneLookupHistoryRecorder.recordPhoneLookupInfo(context.getApplicationContext(), call);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700640 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800641 }
642
643 private boolean shouldAttemptBlocking(android.telecom.Call call) {
644 if (call.getState() != android.telecom.Call.STATE_RINGING) {
645 return false;
646 }
linyuh183cb712017-12-27 17:02:37 -0800647 if (!UserManagerCompat.isUserUnlocked(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700648 LogUtil.i(
649 "InCallPresenter.shouldAttemptBlocking",
650 "not attempting to block incoming call because user is locked");
651 return false;
652 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800653 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700654 LogUtil.i(
655 "InCallPresenter.shouldAttemptBlocking",
656 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800657 return false;
658 }
linyuh183cb712017-12-27 17:02:37 -0800659 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700660 LogUtil.i(
661 "InCallPresenter.shouldAttemptBlocking",
662 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800663 return false;
664 }
665 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
666 return false;
667 }
linyuh183cb712017-12-27 17:02:37 -0800668 if (FilteredNumberCompat.useNewFiltering(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700669 LogUtil.i(
670 "InCallPresenter.shouldAttemptBlocking",
671 "not attempting to block incoming call because framework blocking is in use");
672 return false;
673 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800674 return true;
675 }
676
677 /**
678 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
679 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
680 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
681 * call anyways.
682 */
683 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
linyuh183cb712017-12-27 17:02:37 -0800684 final String countryIso = GeoUtil.getCurrentCountryIso(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800685 final String number = TelecomCallUtil.getNumber(call);
686 final long timeAdded = System.currentTimeMillis();
687
688 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
689 // main UI thread. It is needed so we can change its value within different scopes, since
690 // that cannot be done with a final boolean.
691 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
692
693 final Handler handler = new Handler();
694
695 // Proceed if the query is slow; the call may still be blocked after the query returns.
696 final Runnable runnable =
697 new Runnable() {
698 @Override
699 public void run() {
700 hasTimedOut.set(true);
701 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800702 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800703 }
704 };
705 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
706
707 OnCheckBlockedListener onCheckBlockedListener =
708 new OnCheckBlockedListener() {
709 @Override
710 public void onCheckComplete(final Integer id) {
711 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700712 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800713 return;
714 }
715 if (!hasTimedOut.get()) {
716 handler.removeCallbacks(runnable);
717 }
718 if (id == null) {
719 if (!hasTimedOut.get()) {
720 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800721 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800722 }
723 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700724 LogUtil.d(
725 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800726 if (!hasTimedOut.get()) {
727 handler.removeCallbacks(runnable);
728
729 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800730 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800731 }
732 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700733 LogUtil.i(
734 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800735 call.reject(false, null);
linyuh183cb712017-12-27 17:02:37 -0800736 Logger.get(context).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
Eric Erfanianccca3152017-02-22 16:32:36 -0800737
738 /*
739 * If mContext is null, then the InCallPresenter was torn down before the
740 * block check had a chance to complete. The context is no longer valid, so
741 * don't attempt to remove the call log entry.
742 */
linyuh183cb712017-12-27 17:02:37 -0800743 if (context == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800744 return;
745 }
746 // Register observer to update the call log.
747 // BlockedNumberContentObserver will unregister after successful log or timeout.
748 BlockedNumberContentObserver contentObserver =
linyuh183cb712017-12-27 17:02:37 -0800749 new BlockedNumberContentObserver(context, new Handler(), number, timeAdded);
Eric Erfanianccca3152017-02-22 16:32:36 -0800750 contentObserver.register();
751 }
752 }
753 };
754
linyuh183cb712017-12-27 17:02:37 -0800755 filteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800756 }
757
758 public void onCallRemoved(android.telecom.Call call) {
759 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800760 externalCallList.onCallRemoved(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800761 } else {
linyuh183cb712017-12-27 17:02:37 -0800762 callList.onCallRemoved(context, call);
763 call.unregisterCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800764 }
765 }
766
767 public void onCanAddCallChanged(boolean canAddCall) {
linyuh183cb712017-12-27 17:02:37 -0800768 for (CanAddCallListener listener : canAddCallListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800769 listener.onCanAddCallChanged(canAddCall);
770 }
771 }
772
773 @Override
774 public void onWiFiToLteHandover(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -0700775 if (call.hasShownWiFiToLteHandoverToast()) {
776 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800777 }
yueg32d9ae72018-04-05 17:35:56 -0700778
779 Toast.makeText(context, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG)
780 .show();
781 call.setHasShownWiFiToLteHandoverToast();
Eric Erfanianccca3152017-02-22 16:32:36 -0800782 }
783
784 @Override
785 public void onHandoverToWifiFailed(DialerCall call) {
linyuh183cb712017-12-27 17:02:37 -0800786 if (inCallActivity != null) {
787 inCallActivity.showDialogOrToastForWifiHandoverFailure(call);
yueg32d9ae72018-04-05 17:35:56 -0700788 } else {
789 Toast.makeText(context, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT)
790 .show();
Eric Erfanianccca3152017-02-22 16:32:36 -0800791 }
792 }
793
Eric Erfanianc857f902017-05-15 14:05:33 -0700794 @Override
795 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
796 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
yuegb47528e2018-04-24 12:12:57 -0700797
798 if (!InternationalCallOnWifiDialogFragment.shouldShow(context)) {
799 LogUtil.i(
800 "InCallPresenter.onInternationalCallOnWifi",
801 "InternationalCallOnWifiDialogFragment.shouldShow returned false");
802 return;
803 }
804
linyuh183cb712017-12-27 17:02:37 -0800805 if (inCallActivity != null) {
806 inCallActivity.showDialogForInternationalCallOnWifi(call);
yuegb47528e2018-04-24 12:12:57 -0700807 } else {
808 Intent intent = new Intent(context, InternationalCallOnWifiDialogActivity.class);
809 // Prevent showing MainActivity with InternationalCallOnWifiDialogActivity on above
810 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
811 intent.putExtra(InternationalCallOnWifiDialogActivity.EXTRA_CALL_ID, call.getId());
812 context.startActivity(intent);
Eric Erfanianc857f902017-05-15 14:05:33 -0700813 }
814 }
815
Eric Erfanianccca3152017-02-22 16:32:36 -0800816 /**
817 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
818 * app based on the information it gets from CallList. Dispatches the in-call state to all
819 * listeners. Can trigger the creation or destruction of the UI based on the states that is
820 * calculates.
821 */
822 @Override
823 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700824 Trace.beginSection("InCallPresenter.onCallListChange");
linyuh183cb712017-12-27 17:02:37 -0800825 if (inCallActivity != null && inCallActivity.isInCallScreenAnimating()) {
826 awaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700827 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800828 return;
829 }
830 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700831 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800832 return;
833 }
834
linyuh183cb712017-12-27 17:02:37 -0800835 awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800836
837 InCallState newState = getPotentialStateFromCallList(callList);
linyuh183cb712017-12-27 17:02:37 -0800838 InCallState oldState = inCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700839 LogUtil.d(
840 "InCallPresenter.onCallListChange",
841 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700842
843 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
844 // incall activity for that call will still exist (even if it's not visible). In the case of
845 // an incoming call in that situation, just disconnect that "waiting for account" call and
846 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
Eric Erfanian938468d2017-10-24 14:05:52 -0700847 // a bug for more details.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700848 DialerCall waitingForAccountCall;
849 if (newState == InCallState.INCOMING
850 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
851 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700852 // The InCallActivity might be destroyed or not started yet at this point.
853 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800854 inCallActivity.dismissPendingDialogs();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700855 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700856 }
857
Eric Erfanianccca3152017-02-22 16:32:36 -0800858 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700859 LogUtil.d(
860 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800861
862 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700863 LogUtil.i(
864 "InCallPresenter.onCallListChange",
865 "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800866 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800867
yuegb9103042018-03-30 12:12:25 -0700868 // Foreground call changed
869 DialerCall primary = null;
870 if (newState == InCallState.INCOMING) {
871 primary = callList.getIncomingCall();
872 } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
873 primary = callList.getOutgoingCall();
874 if (primary == null) {
875 primary = callList.getPendingOutgoingCall();
876 }
877 } else if (newState == InCallState.INCALL) {
878 primary = getCallToDisplay(callList, null, false);
879 }
880 if (primary != null) {
881 onForegroundCallChanged(primary);
882 }
883
Eric Erfanianccca3152017-02-22 16:32:36 -0800884 // notify listeners of new state
linyuh183cb712017-12-27 17:02:37 -0800885 for (InCallStateListener listener : listeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700886 LogUtil.d(
887 "InCallPresenter.onCallListChange",
linyuh183cb712017-12-27 17:02:37 -0800888 "Notify " + listener + " of state " + inCallState.toString());
889 listener.onStateChange(oldState, inCallState, callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800890 }
891
892 if (isActivityStarted()) {
893 final boolean hasCall =
894 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
linyuh183cb712017-12-27 17:02:37 -0800895 inCallActivity.dismissKeyguard(hasCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800896 }
twyen27c37182018-04-24 11:48:12 -0700897
Eric Erfanian2ca43182017-08-31 06:57:16 -0700898 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800899 }
900
yuegb9103042018-03-30 12:12:25 -0700901 /**
902 * Get the highest priority call to display. Goes through the calls and chooses which to return
903 * based on priority of which type of call to display to the user. Callers can use the "ignore"
904 * feature to get the second best call by passing a previously found primary call as ignore.
905 *
906 * @param ignore A call to ignore if found.
907 */
908 static DialerCall getCallToDisplay(
909 CallList callList, DialerCall ignore, boolean skipDisconnected) {
910 // Active calls come second. An active call always gets precedent.
911 DialerCall retval = callList.getActiveCall();
912 if (retval != null && retval != ignore) {
913 return retval;
914 }
915
916 // Sometimes there is intemediate state that two calls are in active even one is about
917 // to be on hold.
918 retval = callList.getSecondActiveCall();
919 if (retval != null && retval != ignore) {
920 return retval;
921 }
922
923 // Disconnected calls get primary position if there are no active calls
924 // to let user know quickly what call has disconnected. Disconnected
925 // calls are very short lived.
926 if (!skipDisconnected) {
927 retval = callList.getDisconnectingCall();
928 if (retval != null && retval != ignore) {
929 return retval;
930 }
931 retval = callList.getDisconnectedCall();
932 if (retval != null && retval != ignore) {
933 return retval;
934 }
935 }
936
937 // Then we go to background call (calls on hold)
938 retval = callList.getBackgroundCall();
939 if (retval != null && retval != ignore) {
940 return retval;
941 }
942
943 // Lastly, we go to a second background call.
944 retval = callList.getSecondBackgroundCall();
945
946 return retval;
947 }
948
Eric Erfanianccca3152017-02-22 16:32:36 -0800949 /** Called when there is a new incoming call. */
950 @Override
951 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700952 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800953 InCallState newState = startOrFinishUi(InCallState.INCOMING);
linyuh183cb712017-12-27 17:02:37 -0800954 InCallState oldState = inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800955
Eric Erfanian2ca43182017-08-31 06:57:16 -0700956 LogUtil.i(
957 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800958 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800959
wangqicf61ca02017-08-31 15:32:55 -0700960 Trace.beginSection("listener.onIncomingCall");
linyuh183cb712017-12-27 17:02:37 -0800961 for (IncomingCallListener listener : incomingCallListeners) {
962 listener.onIncomingCall(oldState, inCallState, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800963 }
wangqicf61ca02017-08-31 15:32:55 -0700964 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800965
wangqicf61ca02017-08-31 15:32:55 -0700966 Trace.beginSection("onPrimaryCallStateChanged");
linyuh183cb712017-12-27 17:02:37 -0800967 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800968 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800969 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800970 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700971 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700972 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800973 }
974
975 @Override
976 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700977 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
linyuh183cb712017-12-27 17:02:37 -0800978 && inCallState == InCallPresenter.InCallState.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800979 LogUtil.i(
980 "InCallPresenter.onUpgradeToVideo",
981 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700982 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800983 }
984
linyuh183cb712017-12-27 17:02:37 -0800985 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800986 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800987 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800988 }
989 }
990
991 @Override
wangqibc28ea72018-04-02 16:23:00 -0700992 public void onUpgradeToRtt(DialerCall call, int rttRequestId) {
993 if (inCallActivity != null) {
994 inCallActivity.showDialogForRttRequest(call, rttRequestId);
995 }
996 }
997
998 @Override
erfanian3887ab02018-05-11 11:02:53 -0700999 public void onSpeakEasyStateChange() {
1000 if (inCallActivity != null) {
1001 inCallActivity.onPrimaryCallStateChanged();
1002 }
1003 }
1004
1005 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001006 public void onSessionModificationStateChange(DialerCall call) {
1007 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -08001008 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
linyuh183cb712017-12-27 17:02:37 -08001009 if (proximitySensor == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001010 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
1011 return;
1012 }
linyuh183cb712017-12-27 17:02:37 -08001013 proximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001014 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
linyuh183cb712017-12-27 17:02:37 -08001015 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001016 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001017 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001018 }
1019 }
1020
1021 /**
1022 * Called when a call becomes disconnected. Called everytime an existing call changes from being
1023 * connected (incoming/outgoing/active) to disconnected.
1024 */
1025 @Override
1026 public void onDisconnect(DialerCall call) {
linyuh7b86f562017-11-16 11:24:09 -08001027 showDialogOrToastForDisconnectedCall(call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001028
1029 // We need to do the run the same code as onCallListChange.
linyuh183cb712017-12-27 17:02:37 -08001030 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001031
1032 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001033 inCallActivity.dismissKeyguard(false);
Eric Erfanianccca3152017-02-22 16:32:36 -08001034 }
1035
1036 if (call.isEmergencyCall()) {
linyuh183cb712017-12-27 17:02:37 -08001037 FilteredNumbersUtil.recordLastEmergencyCallTime(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001038 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001039
linyuh183cb712017-12-27 17:02:37 -08001040 if (!callList.hasLiveCall()
Eric Erfanianfc37b022017-03-21 10:11:17 -07001041 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -07001042 && !isSecretCode(call.getNumber())
wangqi9982f0d2017-10-11 17:46:07 -07001043 && !call.isVoiceMailNumber()) {
linyuh183cb712017-12-27 17:02:37 -08001044 PostCall.onCallDisconnected(context, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -08001045 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001046 }
1047
Eric Erfanian10b34a52017-05-04 08:23:17 -07001048 private boolean isSecretCode(@Nullable String number) {
1049 return number != null
1050 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
1051 }
1052
Eric Erfanianccca3152017-02-22 16:32:36 -08001053 /** Given the call list, return the state in which the in-call screen should be. */
1054 public InCallState getPotentialStateFromCallList(CallList callList) {
1055
1056 InCallState newState = InCallState.NO_CALLS;
1057
1058 if (callList == null) {
1059 return newState;
1060 }
1061 if (callList.getIncomingCall() != null) {
1062 newState = InCallState.INCOMING;
1063 } else if (callList.getWaitingForAccountCall() != null) {
1064 newState = InCallState.WAITING_FOR_ACCOUNT;
1065 } else if (callList.getPendingOutgoingCall() != null) {
1066 newState = InCallState.PENDING_OUTGOING;
1067 } else if (callList.getOutgoingCall() != null) {
1068 newState = InCallState.OUTGOING;
1069 } else if (callList.getActiveCall() != null
1070 || callList.getBackgroundCall() != null
1071 || callList.getDisconnectedCall() != null
1072 || callList.getDisconnectingCall() != null) {
1073 newState = InCallState.INCALL;
1074 }
1075
1076 if (newState == InCallState.NO_CALLS) {
linyuh183cb712017-12-27 17:02:37 -08001077 if (boundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001078 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001079 }
1080 }
1081
1082 return newState;
1083 }
1084
1085 public boolean isBoundAndWaitingForOutgoingCall() {
linyuh183cb712017-12-27 17:02:37 -08001086 return boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -08001087 }
1088
1089 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001090 LogUtil.i(
1091 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
1092 "setBoundAndWaitingForOutgoingCall: " + isBound);
linyuh183cb712017-12-27 17:02:37 -08001093 boundAndWaitingForOutgoingCall = isBound;
1094 themeColorManager.setPendingPhoneAccountHandle(handle);
1095 if (isBound && inCallState == InCallState.NO_CALLS) {
1096 inCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001097 }
1098 }
1099
1100 public void onShrinkAnimationComplete() {
linyuh183cb712017-12-27 17:02:37 -08001101 if (awaitingCallListUpdate) {
1102 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001103 }
1104 }
1105
1106 public void addIncomingCallListener(IncomingCallListener listener) {
1107 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001108 incomingCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001109 }
1110
1111 public void removeIncomingCallListener(IncomingCallListener listener) {
1112 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001113 incomingCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001114 }
1115 }
1116
1117 public void addListener(InCallStateListener listener) {
1118 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001119 listeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001120 }
1121
1122 public void removeListener(InCallStateListener listener) {
1123 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001124 listeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001125 }
1126 }
1127
1128 public void addDetailsListener(InCallDetailsListener listener) {
1129 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001130 detailsListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001131 }
1132
1133 public void removeDetailsListener(InCallDetailsListener listener) {
1134 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001135 detailsListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001136 }
1137 }
1138
1139 public void addCanAddCallListener(CanAddCallListener listener) {
1140 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001141 canAddCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001142 }
1143
1144 public void removeCanAddCallListener(CanAddCallListener listener) {
1145 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001146 canAddCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001147 }
1148 }
1149
1150 public void addOrientationListener(InCallOrientationListener listener) {
1151 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001152 orientationListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001153 }
1154
1155 public void removeOrientationListener(InCallOrientationListener listener) {
1156 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001157 orientationListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001158 }
1159 }
1160
1161 public void addInCallEventListener(InCallEventListener listener) {
1162 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001163 inCallEventListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001164 }
1165
1166 public void removeInCallEventListener(InCallEventListener listener) {
1167 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001168 inCallEventListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001169 }
1170 }
1171
1172 public ProximitySensor getProximitySensor() {
linyuh183cb712017-12-27 17:02:37 -08001173 return proximitySensor;
Eric Erfanianccca3152017-02-22 16:32:36 -08001174 }
1175
1176 public PseudoScreenState getPseudoScreenState() {
linyuh183cb712017-12-27 17:02:37 -08001177 return pseudoScreenState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001178 }
1179
1180 /** Returns true if the incall app is the foreground application. */
1181 public boolean isShowingInCallUi() {
1182 if (!isActivityStarted()) {
1183 return false;
1184 }
linyuh183cb712017-12-27 17:02:37 -08001185 if (manageConferenceActivity != null && manageConferenceActivity.isVisible()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001186 return true;
1187 }
linyuh183cb712017-12-27 17:02:37 -08001188 return inCallActivity.isVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001189 }
1190
1191 /**
1192 * Returns true if the activity has been created and is running. Returns true as long as activity
1193 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
1194 * (not in foreground).
1195 */
1196 public boolean isActivityStarted() {
linyuh183cb712017-12-27 17:02:37 -08001197 return (inCallActivity != null
1198 && !inCallActivity.isDestroyed()
1199 && !inCallActivity.isFinishing());
Eric Erfanianccca3152017-02-22 16:32:36 -08001200 }
1201
1202 /**
1203 * Determines if the In-Call app is currently changing configuration.
1204 *
1205 * @return {@code true} if the In-Call app is changing configuration.
1206 */
1207 public boolean isChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001208 return isChangingConfigurations;
Eric Erfanianccca3152017-02-22 16:32:36 -08001209 }
1210
1211 /**
1212 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1213 * screen orientation).
1214 */
1215 /*package*/
1216 void updateIsChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001217 isChangingConfigurations = false;
1218 if (inCallActivity != null) {
1219 isChangingConfigurations = inCallActivity.isChangingConfigurations();
Eric Erfanianccca3152017-02-22 16:32:36 -08001220 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001221 LogUtil.v(
1222 "InCallPresenter.updateIsChangingConfigurations",
linyuh183cb712017-12-27 17:02:37 -08001223 "updateIsChangingConfigurations = " + isChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001224 }
1225
yueg10f6e822018-01-17 15:32:18 -08001226 /** Called when the activity goes in/out of the foreground. */
1227 public void onUiShowing(boolean showing) {
linyuh183cb712017-12-27 17:02:37 -08001228 if (proximitySensor != null) {
1229 proximitySensor.onInCallShowing(showing);
Eric Erfanianccca3152017-02-22 16:32:36 -08001230 }
1231
Sekine Yasuakia7020842018-05-31 17:10:12 +09001232 if (showing) {
1233 refreshMuteState();
1234 } else {
Eric Erfanianccca3152017-02-22 16:32:36 -08001235 updateIsChangingConfigurations();
1236 }
1237
linyuh183cb712017-12-27 17:02:37 -08001238 for (InCallUiListener listener : inCallUiListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001239 listener.onUiShowing(showing);
1240 }
1241
linyuh183cb712017-12-27 17:02:37 -08001242 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001243 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001244 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001245 }
1246 }
1247
Eric Erfanian2ca43182017-08-31 06:57:16 -07001248 public void refreshUi() {
linyuh183cb712017-12-27 17:02:37 -08001249 if (inCallActivity != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001250 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001251 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001252 }
1253 }
1254
Eric Erfanianccca3152017-02-22 16:32:36 -08001255 public void addInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001256 inCallUiListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001257 }
1258
1259 public boolean removeInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001260 return inCallUiListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001261 }
1262
1263 /*package*/
1264 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001265 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001266 notifyVideoPauseController(true);
Eric Erfanian2ca43182017-08-31 06:57:16 -07001267 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001268 }
1269
1270 /*package*/
1271 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001272 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001273 notifyVideoPauseController(false);
1274 }
1275
1276 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001277 LogUtil.d(
1278 "InCallPresenter.notifyVideoPauseController",
linyuh183cb712017-12-27 17:02:37 -08001279 "mIsChangingConfigurations=" + isChangingConfigurations);
1280 if (!isChangingConfigurations) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001281 VideoPauseController.getInstance().onUiShowing(showing);
1282 }
1283 }
1284
1285 /** Brings the app into the foreground if possible. */
1286 public void bringToForeground(boolean showDialpad) {
1287 // Before we bring the incall UI to the foreground, we check to see if:
1288 // 1. It is not currently in the foreground
1289 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1290 // be displayed)
1291 // If the activity hadn't actually been started previously, yet there are still calls
1292 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1293 // bring it up the UI regardless.
linyuh183cb712017-12-27 17:02:37 -08001294 if (!isShowingInCallUi() && inCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001295 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001296 }
1297 }
1298
1299 public void onPostDialCharWait(String callId, String chars) {
yuega489f512018-04-25 12:01:09 -07001300 // If not visible, inCallActivity is stopped. Starting from P, calling recreate() will destroy
1301 // the old activity instance and create a new instance immediately. Previously, the old activity
1302 // went through its lifecycle from create to destroy before creating a new instance.
1303 // So this case doesn't work now: make a call with char WAIT, leave in call UI, call gets
1304 // connected, and go back to in call UI to see the dialog.
1305 // So we should show dialog in an empty activity if inCallActivity is not visible. And it also
1306 // helps with background calling.
1307 if (isActivityStarted() && inCallActivity.isVisible()) {
linyuh183cb712017-12-27 17:02:37 -08001308 inCallActivity.showDialogForPostCharWait(callId, chars);
yuega489f512018-04-25 12:01:09 -07001309 } else {
1310 Intent intent = new Intent(context, PostCharDialogActivity.class);
1311 // Prevent showing MainActivity with PostCharDialogActivity on above
1312 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1313 intent.putExtra(PostCharDialogActivity.EXTRA_CALL_ID, callId);
1314 intent.putExtra(PostCharDialogActivity.EXTRA_POST_DIAL_STRING, chars);
1315 context.startActivity(intent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001316 }
1317 }
1318
1319 /**
1320 * Handles the green CALL key while in-call.
1321 *
1322 * @return true if we consumed the event.
1323 */
1324 public boolean handleCallKey() {
1325 LogUtil.v("InCallPresenter.handleCallKey", null);
1326
1327 // The green CALL button means either "Answer", "Unhold", or
1328 // "Swap calls", or can be a no-op, depending on the current state
1329 // of the Phone.
1330
1331 /** INCOMING CALL */
linyuh183cb712017-12-27 17:02:37 -08001332 final CallList calls = callList;
Eric Erfanianccca3152017-02-22 16:32:36 -08001333 final DialerCall incomingCall = calls.getIncomingCall();
1334 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1335
1336 // (1) Attempt to answer a call
1337 if (incomingCall != null) {
1338 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1339 return true;
1340 }
1341
1342 /** STATE_ACTIVE CALL */
1343 final DialerCall activeCall = calls.getActiveCall();
1344 if (activeCall != null) {
1345 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1346 // consolidate this logic.
1347 final boolean canMerge =
1348 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1349 final boolean canSwap =
1350 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1351
Eric Erfanian2ca43182017-08-31 06:57:16 -07001352 LogUtil.v(
1353 "InCallPresenter.handleCallKey",
1354 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001355
1356 // (2) Attempt actions on conference calls
1357 if (canMerge) {
1358 TelecomAdapter.getInstance().merge(activeCall.getId());
1359 return true;
1360 } else if (canSwap) {
1361 TelecomAdapter.getInstance().swap(activeCall.getId());
1362 return true;
1363 }
1364 }
1365
1366 /** BACKGROUND CALL */
1367 final DialerCall heldCall = calls.getBackgroundCall();
1368 if (heldCall != null) {
1369 // We have a hold call so presumeable it will always support HOLD...but
1370 // there is no harm in double checking.
1371 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1372
Eric Erfanian2ca43182017-08-31 06:57:16 -07001373 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001374
1375 // (4) unhold call
wangqibb94ca62018-04-27 14:34:04 -07001376 if (heldCall.getState() == DialerCallState.ONHOLD && canHold) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001377 heldCall.unhold();
1378 return true;
1379 }
1380 }
1381
1382 // Always consume hard keys
1383 return true;
1384 }
1385
Eric Erfanianccca3152017-02-22 16:32:36 -08001386 /** Clears the previous fullscreen state. */
1387 public void clearFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001388 isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001389 }
1390
1391 /**
1392 * Changes the fullscreen mode of the in-call UI.
1393 *
1394 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1395 * otherwise.
1396 */
1397 public void setFullScreen(boolean isFullScreen) {
1398 setFullScreen(isFullScreen, false /* force */);
1399 }
1400
1401 /**
1402 * Changes the fullscreen mode of the in-call UI.
1403 *
1404 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1405 * otherwise.
1406 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1407 */
1408 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001409 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001410
1411 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1412 if (isDialpadVisible()) {
1413 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001414 LogUtil.v(
1415 "InCallPresenter.setFullScreen",
1416 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001417 }
1418
linyuh183cb712017-12-27 17:02:37 -08001419 if (this.isFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001420 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001421 return;
1422 }
linyuh183cb712017-12-27 17:02:37 -08001423 this.isFullScreen = isFullScreen;
1424 notifyFullscreenModeChange(this.isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001425 }
1426
1427 /**
1428 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1429 * otherwise.
1430 */
1431 public boolean isFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001432 return isFullScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001433 }
1434
1435 /**
1436 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1437 *
1438 * @param isFullscreenMode {@code True} if entering full screen mode.
1439 */
1440 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001441 for (InCallEventListener listener : inCallEventListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001442 listener.onFullscreenModeChanged(isFullscreenMode);
1443 }
1444 }
1445
linyuh7b86f562017-11-16 11:24:09 -08001446 /** Instruct the in-call activity to show an error dialog or toast for a disconnected call. */
1447 private void showDialogOrToastForDisconnectedCall(DialerCall call) {
wangqibb94ca62018-04-27 14:34:04 -07001448 if (call.getState() != DialerCallState.DISCONNECTED) {
linyuh7b86f562017-11-16 11:24:09 -08001449 return;
Eric Erfanianccca3152017-02-22 16:32:36 -08001450 }
linyuh7b86f562017-11-16 11:24:09 -08001451
1452 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1453 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1454 setDisconnectCauseForMissingAccounts(call);
1455 }
1456
yueg32d9ae72018-04-05 17:35:56 -07001457 if (isActivityStarted()) {
1458 inCallActivity.showDialogOrToastForDisconnectedCall(
1459 new DisconnectMessage(inCallActivity, call));
1460 } else {
1461 CharSequence message = new DisconnectMessage(context, call).toastMessage;
1462 if (message != null) {
1463 Toast.makeText(context, message, Toast.LENGTH_LONG).show();
1464 }
1465 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001466 }
1467
1468 /**
1469 * When the state of in-call changes, this is the first method to get called. It determines if the
1470 * UI needs to be started or finished depending on the new state and does it.
1471 */
1472 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001473 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001474 LogUtil.d(
linyuh183cb712017-12-27 17:02:37 -08001475 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + inCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001476
1477 // TODO: Consider a proper state machine implementation
1478
1479 // If the state isn't changing we have already done any starting/stopping of activities in
1480 // a previous pass...so lets cut out early
linyuh183cb712017-12-27 17:02:37 -08001481 if (newState == inCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001482 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001483 return newState;
1484 }
1485
1486 // A new Incoming call means that the user needs to be notified of the the call (since
1487 // it wasn't them who initiated it). We do this through full screen notifications and
1488 // happens indirectly through {@link StatusBarNotifier}.
1489 //
1490 // The process for incoming calls is as follows:
1491 //
1492 // 1) CallList - Announces existence of new INCOMING call
1493 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1494 // - should be set to INCOMING.
1495 // 3) InCallPresenter - This method is called to see if we need to start or finish
1496 // the app given the new state.
1497 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1498 // StatusBarNotifier explicitly to issue a FullScreen Notification
1499 // that will either start the InCallActivity or show the user a
1500 // top-level notification dialog if the user is in an immersive app.
1501 // That notification can also start the InCallActivity.
1502 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1503 // call InCallPresenter::setActivity() to let the presenter
1504 // know that start-up is complete.
1505 //
1506 // [ AND NOW YOU'RE IN THE CALL. voila! ]
Eric Erfanianccca3152017-02-22 16:32:36 -08001507
1508 // A dialog to show on top of the InCallUI to select a PhoneAccount
1509 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1510
1511 // A new outgoing call indicates that the user just now dialed a number and when that
1512 // happens we need to display the screen immediately or show an account picker dialog if
1513 // no default is set. However, if the main InCallUI is already visible, we do not want to
1514 // re-initiate the start-up animation, so we do not need to do anything here.
1515 //
1516 // It is also possible to go into an intermediate state where the call has been initiated
1517 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1518 // This pending outgoing state can also launch the call screen.
1519 //
1520 // This is different from the incoming call sequence because we do not need to shock the
1521 // user with a top-level notification. Just show the call UI normally.
1522 boolean callCardFragmentVisible =
linyuh183cb712017-12-27 17:02:37 -08001523 inCallActivity != null && inCallActivity.getCallCardFragmentVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001524 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1525 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1526
1527 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1528 // outgoing call process, so the UI should be brought up to show an error dialog.
1529 showCallUi |=
linyuh183cb712017-12-27 17:02:37 -08001530 (InCallState.PENDING_OUTGOING == inCallState
Eric Erfanianccca3152017-02-22 16:32:36 -08001531 && InCallState.INCALL == newState
1532 && !isShowingInCallUi());
1533
1534 // Another exception - InCallActivity is in charge of disconnecting a call with no
1535 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1536 // call so that:
1537 // 1) The call can be disconnected correctly
1538 // 2) The UI comes up and correctly displays the error dialog.
1539 // TODO: Remove these special case conditions by making InCallPresenter a true state
1540 // machine. Telecom should also be the component responsible for disconnecting a call
1541 // with no valid accounts.
1542 showCallUi |=
1543 InCallState.PENDING_OUTGOING == newState
1544 && mainUiNotVisible
linyuh183cb712017-12-27 17:02:37 -08001545 && isCallWithNoValidAccounts(callList.getPendingOutgoingCall());
Eric Erfanianccca3152017-02-22 16:32:36 -08001546
1547 // The only time that we have an instance of mInCallActivity and it isn't started is
1548 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1549 // the activity. When it is finally destroyed, we double check if we should bring it back
1550 // up so we aren't going to lose anything by avoiding a second startup here.
linyuh183cb712017-12-27 17:02:37 -08001551 boolean activityIsFinishing = inCallActivity != null && !isActivityStarted();
Eric Erfanianccca3152017-02-22 16:32:36 -08001552 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001553 LogUtil.i(
1554 "InCallPresenter.startOrFinishUi",
linyuh183cb712017-12-27 17:02:37 -08001555 "Undo the state change: " + newState + " -> " + inCallState);
wangqicf61ca02017-08-31 15:32:55 -07001556 Trace.endSection();
linyuh183cb712017-12-27 17:02:37 -08001557 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001558 }
1559
1560 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1561 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1562 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1563 // outgoing call after user ignore it by pressing home button.
1564 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1565 && !showCallUi
1566 && isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001567 inCallActivity.dismissPendingDialogs();
Eric Erfanianccca3152017-02-22 16:32:36 -08001568 }
1569
yueg4613e8f2018-04-23 13:35:33 -07001570 if ((showCallUi || showAccountPicker) && !shouldStartInBubbleMode()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001571 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001572 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001573 } else if (newState == InCallState.NO_CALLS) {
1574 // The new state is the no calls state. Tear everything down.
yueg8e544862018-04-06 14:34:59 -07001575 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001576 attemptFinishActivity();
1577 attemptCleanup();
1578 }
1579
wangqicf61ca02017-08-31 15:32:55 -07001580 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001581 return newState;
1582 }
1583
1584 /**
1585 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1586 * or PhoneAccounts to select from.
1587 */
1588 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1589
1590 Bundle extras = call.getIntentExtras();
1591 // Initialize the extras bundle to avoid NPE
1592 if (extras == null) {
1593 extras = new Bundle();
1594 }
1595
1596 final List<PhoneAccountHandle> phoneAccountHandles =
1597 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1598
1599 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1600 String scheme = call.getHandle().getScheme();
1601 final String errorMsg =
1602 PhoneAccount.SCHEME_TEL.equals(scheme)
linyuh183cb712017-12-27 17:02:37 -08001603 ? context.getString(R.string.callFailed_simError)
1604 : context.getString(R.string.incall_error_supp_service_unknown);
Eric Erfanianccca3152017-02-22 16:32:36 -08001605 DisconnectCause disconnectCause =
1606 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1607 call.setDisconnectCause(disconnectCause);
1608 }
1609 }
1610
Eric Erfanianccca3152017-02-22 16:32:36 -08001611 /**
1612 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1613 * Calling classes should use this as an indication whether to interact with the
1614 * InCallPresenter or not.
1615 */
1616 public boolean isReadyForTearDown() {
linyuh183cb712017-12-27 17:02:37 -08001617 return inCallActivity == null && !serviceConnected && inCallState == InCallState.NO_CALLS;
Eric Erfanianccca3152017-02-22 16:32:36 -08001618 }
1619
1620 /**
1621 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1622 */
1623 private void attemptCleanup() {
1624 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001625 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001626
1627 cleanupSurfaces();
1628
linyuh183cb712017-12-27 17:02:37 -08001629 isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001630
1631 // blow away stale contact info so that we get fresh data on
1632 // the next set of calls
linyuh183cb712017-12-27 17:02:37 -08001633 if (contactInfoCache != null) {
1634 contactInfoCache.clearCache();
Eric Erfanianccca3152017-02-22 16:32:36 -08001635 }
linyuh183cb712017-12-27 17:02:37 -08001636 contactInfoCache = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001637
linyuh183cb712017-12-27 17:02:37 -08001638 if (proximitySensor != null) {
1639 removeListener(proximitySensor);
1640 proximitySensor.tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -08001641 }
linyuh183cb712017-12-27 17:02:37 -08001642 proximitySensor = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001643
linyuh183cb712017-12-27 17:02:37 -08001644 if (statusBarNotifier != null) {
1645 removeListener(statusBarNotifier);
1646 EnrichedCallComponent.get(context)
Eric Erfaniand8046e52017-04-06 09:41:50 -07001647 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -08001648 .unregisterStateChangedListener(statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001649 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001650
linyuh183cb712017-12-27 17:02:37 -08001651 if (externalCallNotifier != null && externalCallList != null) {
1652 externalCallList.removeExternalCallListener(externalCallNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001653 }
linyuh183cb712017-12-27 17:02:37 -08001654 statusBarNotifier = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001655
linyuh183cb712017-12-27 17:02:37 -08001656 if (callList != null) {
1657 callList.removeListener(this);
1658 callList.removeListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001659 }
linyuh183cb712017-12-27 17:02:37 -08001660 callList = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001661
linyuh183cb712017-12-27 17:02:37 -08001662 context = null;
1663 inCallActivity = null;
1664 manageConferenceActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001665
linyuh183cb712017-12-27 17:02:37 -08001666 listeners.clear();
1667 incomingCallListeners.clear();
1668 detailsListeners.clear();
1669 canAddCallListeners.clear();
1670 orientationListeners.clear();
1671 inCallEventListeners.clear();
1672 inCallUiListeners.clear();
1673 if (!inCallUiLocks.isEmpty()) {
1674 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + inCallUiLocks);
1675 inCallUiLocks.clear();
twyen8efb4952017-10-06 16:35:54 -07001676 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001677 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001678 }
1679 }
1680
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001681 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001682 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
linyuh183cb712017-12-27 17:02:37 -08001683 context.startActivity(
1684 InCallActivity.getIntent(context, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001685 }
1686
1687 public void onServiceBind() {
linyuh183cb712017-12-27 17:02:37 -08001688 serviceBound = true;
Eric Erfanianccca3152017-02-22 16:32:36 -08001689 }
1690
1691 public void onServiceUnbind() {
1692 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -08001693 serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001694 }
1695
1696 public boolean isServiceBound() {
linyuh183cb712017-12-27 17:02:37 -08001697 return serviceBound;
Eric Erfanianccca3152017-02-22 16:32:36 -08001698 }
1699
1700 public void maybeStartRevealAnimation(Intent intent) {
linyuh183cb712017-12-27 17:02:37 -08001701 if (intent == null || inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001702 return;
1703 }
1704 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1705 if (extras == null) {
1706 // Incoming call, just show the in-call UI directly.
1707 return;
1708 }
1709
1710 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1711 // Account selection dialog will show up so don't show the animation.
1712 return;
1713 }
1714
1715 final PhoneAccountHandle accountHandle =
1716 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1717 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001718
yueg32d9ae72018-04-05 17:35:56 -07001719 setBoundAndWaitingForOutgoingCall(true, accountHandle);
1720
yueg4613e8f2018-04-23 13:35:33 -07001721 if (shouldStartInBubbleModeWithExtras(extras)) {
yueg32d9ae72018-04-05 17:35:56 -07001722 LogUtil.i("InCallPresenter.maybeStartRevealAnimation", "shouldStartInBubbleMode");
1723 // Show bubble instead of in call UI
1724 return;
1725 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001726
1727 final Intent activityIntent =
linyuh183cb712017-12-27 17:02:37 -08001728 InCallActivity.getIntent(context, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001729 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
linyuh183cb712017-12-27 17:02:37 -08001730 context.startActivity(activityIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001731 }
1732
1733 /**
1734 * Retrieves the current in-call camera manager instance, creating if necessary.
1735 *
1736 * @return The {@link InCallCameraManager}.
1737 */
1738 public InCallCameraManager getInCallCameraManager() {
1739 synchronized (this) {
linyuh183cb712017-12-27 17:02:37 -08001740 if (inCallCameraManager == null) {
1741 inCallCameraManager = new InCallCameraManager(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001742 }
1743
linyuh183cb712017-12-27 17:02:37 -08001744 return inCallCameraManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001745 }
1746 }
1747
1748 /**
1749 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1750 *
1751 * @param orientation The screen orientation of the device (one of: {@link
1752 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1753 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1754 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1755 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1756 */
1757 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001758 LogUtil.d(
1759 "InCallPresenter.onDeviceOrientationChange",
1760 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001761
linyuh183cb712017-12-27 17:02:37 -08001762 if (callList != null) {
1763 callList.notifyCallsOfDeviceRotation(orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001764 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001765 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001766 }
1767
1768 // Notify listeners of device orientation changed.
linyuh183cb712017-12-27 17:02:37 -08001769 for (InCallOrientationListener listener : orientationListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001770 listener.onDeviceOrientationChanged(orientation);
1771 }
1772 }
1773
1774 /**
1775 * Configures the in-call UI activity so it can change orientations or not. Enables the
1776 * orientation event listener if allowOrientationChange is true, disables it if false.
1777 *
1778 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1779 * landscape. {@code false} if the in-call UI should be locked in portrait.
1780 */
1781 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
linyuh183cb712017-12-27 17:02:37 -08001782 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001783 LogUtil.e(
1784 "InCallPresenter.setInCallAllowsOrientationChange",
1785 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001786 return;
1787 }
linyuh183cb712017-12-27 17:02:37 -08001788 inCallActivity.setAllowOrientationChange(allowOrientationChange);
Eric Erfanianccca3152017-02-22 16:32:36 -08001789 }
1790
1791 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001792 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
linyuh183cb712017-12-27 17:02:37 -08001793 screenTimeoutEnabled = enable;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001794 applyScreenTimeout();
1795 }
1796
1797 private void applyScreenTimeout() {
linyuh183cb712017-12-27 17:02:37 -08001798 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001799 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001800 return;
1801 }
1802
linyuh183cb712017-12-27 17:02:37 -08001803 final Window window = inCallActivity.getWindow();
1804 if (screenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001805 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1806 } else {
1807 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1808 }
1809 }
1810
1811 /**
1812 * Hides or shows the conference manager fragment.
1813 *
1814 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1815 * be hidden.
1816 */
1817 public void showConferenceCallManager(boolean show) {
linyuh183cb712017-12-27 17:02:37 -08001818 if (inCallActivity != null) {
1819 inCallActivity.showConferenceFragment(show);
Eric Erfanianccca3152017-02-22 16:32:36 -08001820 }
linyuh183cb712017-12-27 17:02:37 -08001821 if (!show && manageConferenceActivity != null) {
1822 manageConferenceActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -08001823 }
1824 }
1825
1826 /**
1827 * Determines if the dialpad is visible.
1828 *
1829 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1830 */
1831 public boolean isDialpadVisible() {
linyuh183cb712017-12-27 17:02:37 -08001832 if (inCallActivity == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001833 return false;
1834 }
linyuh183cb712017-12-27 17:02:37 -08001835 return inCallActivity.isDialpadVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001836 }
1837
1838 public ThemeColorManager getThemeColorManager() {
linyuh183cb712017-12-27 17:02:37 -08001839 return themeColorManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001840 }
1841
wangqi8d662ca2017-10-26 11:27:19 -07001842 @VisibleForTesting
1843 public void setThemeColorManager(ThemeColorManager themeColorManager) {
linyuh183cb712017-12-27 17:02:37 -08001844 this.themeColorManager = themeColorManager;
wangqi8d662ca2017-10-26 11:27:19 -07001845 }
1846
Eric Erfanianccca3152017-02-22 16:32:36 -08001847 /** Called when the foreground call changes. */
1848 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh183cb712017-12-27 17:02:37 -08001849 themeColorManager.onForegroundCallChanged(context, newForegroundCall);
1850 if (inCallActivity != null) {
1851 inCallActivity.onForegroundCallChanged(newForegroundCall);
Eric Erfanianccca3152017-02-22 16:32:36 -08001852 }
1853 }
1854
1855 public InCallActivity getActivity() {
linyuh183cb712017-12-27 17:02:37 -08001856 return inCallActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -08001857 }
1858
1859 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1860 public void setActivity(InCallActivity inCallActivity) {
1861 if (inCallActivity == null) {
1862 throw new IllegalArgumentException("registerActivity cannot be called with null");
1863 }
linyuh183cb712017-12-27 17:02:37 -08001864 if (this.inCallActivity != null && this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001865 LogUtil.w(
1866 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001867 }
1868 updateActivity(inCallActivity);
1869 }
1870
1871 ExternalCallNotifier getExternalCallNotifier() {
linyuh183cb712017-12-27 17:02:37 -08001872 return externalCallNotifier;
Eric Erfanianccca3152017-02-22 16:32:36 -08001873 }
1874
1875 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001876 if (localVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001877 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001878 if (context != null) {
1879 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001880 }
linyuh183cb712017-12-27 17:02:37 -08001881 localVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001882 }
linyuh183cb712017-12-27 17:02:37 -08001883 return localVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001884 }
1885
1886 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001887 if (remoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001888 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001889 if (context != null) {
1890 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001891 }
linyuh183cb712017-12-27 17:02:37 -08001892 remoteVideoSurfaceTexture = VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001893 }
linyuh183cb712017-12-27 17:02:37 -08001894 return remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001895 }
1896
1897 void cleanupSurfaces() {
linyuh183cb712017-12-27 17:02:37 -08001898 if (remoteVideoSurfaceTexture != null) {
1899 remoteVideoSurfaceTexture.setDoneWithSurface();
1900 remoteVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001901 }
linyuh183cb712017-12-27 17:02:37 -08001902 if (localVideoSurfaceTexture != null) {
1903 localVideoSurfaceTexture.setDoneWithSurface();
1904 localVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001905 }
1906 }
1907
yueg77cb8e52017-10-27 16:42:51 -07001908 @Override
1909 public void onAudioStateChanged(CallAudioState audioState) {
linyuh183cb712017-12-27 17:02:37 -08001910 if (statusBarNotifier != null) {
1911 statusBarNotifier.updateNotification();
yueg77cb8e52017-10-27 16:42:51 -07001912 }
1913 }
1914
Eric Erfanianccca3152017-02-22 16:32:36 -08001915 /** All the main states of InCallActivity. */
1916 public enum InCallState {
1917 // InCall Screen is off and there are no calls
1918 NO_CALLS,
1919
1920 // Incoming-call screen is up
1921 INCOMING,
1922
1923 // In-call experience is showing
1924 INCALL,
1925
1926 // Waiting for user input before placing outgoing call
1927 WAITING_FOR_ACCOUNT,
1928
1929 // UI is starting up but no call has been initiated yet.
1930 // The UI is waiting for Telecom to respond.
1931 PENDING_OUTGOING,
1932
1933 // User is dialing out
1934 OUTGOING;
1935
1936 public boolean isIncoming() {
1937 return (this == INCOMING);
1938 }
1939
1940 public boolean isConnectingOrConnected() {
1941 return (this == INCOMING || this == OUTGOING || this == INCALL);
1942 }
1943 }
1944
1945 /** Interface implemented by classes that need to know about the InCall State. */
1946 public interface InCallStateListener {
1947
1948 // TODO: Enhance state to contain the call objects instead of passing CallList
1949 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1950 }
1951
1952 public interface IncomingCallListener {
1953
1954 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1955 }
1956
1957 public interface CanAddCallListener {
1958
1959 void onCanAddCallChanged(boolean canAddCall);
1960 }
1961
1962 public interface InCallDetailsListener {
1963
1964 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1965 }
1966
1967 public interface InCallOrientationListener {
1968
1969 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1970 }
1971
1972 /**
1973 * Interface implemented by classes that need to know about events which occur within the In-Call
1974 * UI. Used as a means of communicating between fragments that make up the UI.
1975 */
1976 public interface InCallEventListener {
1977
1978 void onFullscreenModeChanged(boolean isFullscreenMode);
1979 }
1980
1981 public interface InCallUiListener {
1982
1983 void onUiShowing(boolean showing);
1984 }
twyen8efb4952017-10-06 16:35:54 -07001985
1986 private class InCallUiLockImpl implements InCallUiLock {
1987 private final String tag;
1988
1989 private InCallUiLockImpl(String tag) {
1990 this.tag = tag;
1991 }
1992
1993 @MainThread
1994 @Override
1995 public void release() {
1996 Assert.isMainThread();
1997 releaseInCallUiLock(InCallUiLockImpl.this);
1998 }
1999
2000 @Override
2001 public String toString() {
2002 return "InCallUiLock[" + tag + "]";
2003 }
2004 }
2005
2006 @MainThread
2007 public InCallUiLock acquireInCallUiLock(String tag) {
2008 Assert.isMainThread();
2009 InCallUiLock lock = new InCallUiLockImpl(tag);
linyuh183cb712017-12-27 17:02:37 -08002010 inCallUiLocks.add(lock);
twyen8efb4952017-10-06 16:35:54 -07002011 return lock;
2012 }
2013
2014 @MainThread
2015 private void releaseInCallUiLock(InCallUiLock lock) {
2016 Assert.isMainThread();
2017 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
linyuh183cb712017-12-27 17:02:37 -08002018 inCallUiLocks.remove(lock);
2019 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002020 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
linyuh183cb712017-12-27 17:02:37 -08002021 if (inCallState == InCallState.NO_CALLS) {
twyen8efb4952017-10-06 16:35:54 -07002022 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
2023 attemptFinishActivity();
2024 attemptCleanup();
2025 }
2026 }
2027 }
2028
2029 @MainThread
2030 public boolean isInCallUiLocked() {
2031 Assert.isMainThread();
linyuh183cb712017-12-27 17:02:37 -08002032 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002033 return false;
2034 }
linyuh183cb712017-12-27 17:02:37 -08002035 for (InCallUiLock lock : inCallUiLocks) {
twyen8efb4952017-10-06 16:35:54 -07002036 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
2037 }
2038 return true;
2039 }
2040
Sekine Yasuakia7020842018-05-31 17:10:12 +09002041 public void addCallClicked() {
2042 if (addCallClicked) {
2043 // Since clicking add call button brings user to MainActivity and coming back refreshes mute
2044 // state, add call button should only be clicked once during InCallActivity shows.
2045 return;
2046 }
2047 addCallClicked = true;
2048 if (!AudioModeProvider.getInstance().getAudioState().isMuted()) {
2049 // Automatically mute the current call
2050 TelecomAdapter.getInstance().mute(true);
2051 automaticallyMutedByAddCall = true;
2052 }
2053 TelecomAdapter.getInstance().addCall();
2054 }
2055
2056 /** Refresh mute state after call UI resuming from add call screen. */
2057 public void refreshMuteState() {
2058 LogUtil.i(
2059 "InCallPresenter.refreshMuteState",
2060 "refreshMuteStateAfterAddCall: %b addCallClicked: %b",
2061 automaticallyMutedByAddCall,
2062 addCallClicked);
2063 if (!addCallClicked) {
2064 return;
2065 }
2066 if (automaticallyMutedByAddCall) {
2067 // Restore the previous mute state
2068 TelecomAdapter.getInstance().mute(false);
2069 automaticallyMutedByAddCall = false;
2070 }
2071 addCallClicked = false;
2072 }
2073
linyuh183cb712017-12-27 17:02:37 -08002074 private final Set<InCallUiLock> inCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08002075}