blob: da5d20fa39dd976ad01ba7c3e7fdc68ea3f4e95c [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
Eric Erfanian10b34a52017-05-04 08:23:17 -0700276 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800277 @VisibleForTesting
278 InCallPresenter() {}
279
280 public static synchronized InCallPresenter getInstance() {
linyuh183cb712017-12-27 17:02:37 -0800281 if (inCallPresenter == null) {
wangqic8cf79e2017-10-17 09:21:00 -0700282 Trace.beginSection("InCallPresenter.Constructor");
linyuh183cb712017-12-27 17:02:37 -0800283 inCallPresenter = new InCallPresenter();
wangqic8cf79e2017-10-17 09:21:00 -0700284 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800285 }
linyuh183cb712017-12-27 17:02:37 -0800286 return inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -0800287 }
288
Eric Erfanian10b34a52017-05-04 08:23:17 -0700289 @VisibleForTesting
290 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
linyuh183cb712017-12-27 17:02:37 -0800291 InCallPresenter.inCallPresenter = inCallPresenter;
Eric Erfanian10b34a52017-05-04 08:23:17 -0700292 }
293
Eric Erfanianccca3152017-02-22 16:32:36 -0800294 /**
295 * Determines whether or not a call has no valid phone accounts that can be used to make the call
296 * with. Emergency calls do not require a phone account.
297 *
298 * @param call to check accounts for.
299 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
300 * call contains a phone account that could be used to initiate it with, or is an emergency
301 * call.
302 */
303 public static boolean isCallWithNoValidAccounts(DialerCall call) {
304 if (call != null && !call.isEmergencyCall()) {
305 Bundle extras = call.getIntentExtras();
306
307 if (extras == null) {
308 extras = EMPTY_EXTRAS;
309 }
310
311 final List<PhoneAccountHandle> phoneAccountHandles =
312 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
313
314 if ((call.getAccountHandle() == null
315 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700316 LogUtil.i(
317 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800318 return true;
319 }
320 }
321 return false;
322 }
323
324 public InCallState getInCallState() {
linyuh183cb712017-12-27 17:02:37 -0800325 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800326 }
327
328 public CallList getCallList() {
linyuh183cb712017-12-27 17:02:37 -0800329 return callList;
Eric Erfanianccca3152017-02-22 16:32:36 -0800330 }
331
332 public void setUp(
333 @NonNull Context context,
334 CallList callList,
335 ExternalCallList externalCallList,
336 StatusBarNotifier statusBarNotifier,
337 ExternalCallNotifier externalCallNotifier,
338 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700339 ProximitySensor proximitySensor,
erfaniand05d8992018-03-20 19:42:26 -0700340 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler,
yueg4613e8f2018-04-23 13:35:33 -0700341 @NonNull SpeakEasyCallManager speakEasyCallManager) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700342 Trace.beginSection("InCallPresenter.setUp");
linyuh183cb712017-12-27 17:02:37 -0800343 if (serviceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700344 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
linyuh183cb712017-12-27 17:02:37 -0800345 if (context != this.context || callList != this.callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800346 throw new IllegalStateException();
347 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700348 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800349 return;
350 }
351
352 Objects.requireNonNull(context);
linyuh183cb712017-12-27 17:02:37 -0800353 this.context = context;
Eric Erfanianccca3152017-02-22 16:32:36 -0800354
linyuh183cb712017-12-27 17:02:37 -0800355 this.contactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800356
linyuh183cb712017-12-27 17:02:37 -0800357 this.statusBarNotifier = statusBarNotifier;
358 this.externalCallNotifier = externalCallNotifier;
359 addListener(this.statusBarNotifier);
360 EnrichedCallComponent.get(this.context)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700361 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -0800362 .registerStateChangedListener(this.statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800363
linyuh183cb712017-12-27 17:02:37 -0800364 this.proximitySensor = proximitySensor;
365 addListener(this.proximitySensor);
Eric Erfanianccca3152017-02-22 16:32:36 -0800366
linyuh183cb712017-12-27 17:02:37 -0800367 if (themeColorManager == null) {
368 themeColorManager =
369 new ThemeColorManager(new InCallUIMaterialColorMapUtils(this.context.getResources()));
wangqi8d662ca2017-10-26 11:27:19 -0700370 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800371
linyuh183cb712017-12-27 17:02:37 -0800372 this.callList = callList;
373 this.externalCallList = externalCallList;
374 externalCallList.addExternalCallListener(this.externalCallNotifier);
375 externalCallList.addExternalCallListener(externalCallListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800376
377 // This only gets called by the service so this is okay.
linyuh183cb712017-12-27 17:02:37 -0800378 serviceConnected = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800379
380 // The final thing we do in this set up is add ourselves as a listener to CallList. This
381 // will kick off an update and the whole process can start.
linyuh183cb712017-12-27 17:02:37 -0800382 this.callList.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800383
384 // Create spam call list listener and add it to the list of listeners
linyuh183cb712017-12-27 17:02:37 -0800385 spamCallListListener =
zachh6a4cebd2017-10-24 17:10:06 -0700386 new SpamCallListListener(
387 context, DialerExecutorComponent.get(context).dialerExecutorFactory());
linyuh183cb712017-12-27 17:02:37 -0800388 this.callList.addListener(spamCallListListener);
twyen27c37182018-04-24 11:48:12 -0700389 activeCallsListener = new ActiveCallsCallListListener(context);
390 this.callList.addListener(activeCallsListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800391
392 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800393
linyuh183cb712017-12-27 17:02:37 -0800394 filteredQueryHandler = filteredNumberQueryHandler;
erfaniand05d8992018-03-20 19:42:26 -0700395 this.speakEasyCallManager = speakEasyCallManager;
linyuh183cb712017-12-27 17:02:37 -0800396 this.context
Eric Erfanianccca3152017-02-22 16:32:36 -0800397 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800398 .listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800399
yueg77cb8e52017-10-27 16:42:51 -0700400 AudioModeProvider.getInstance().addListener(this);
401
roldenburg42099012018-04-09 14:47:15 -0700402 // Add listener to notify Telephony process when the incoming call screen is started or
403 // finished. This is for hiding USSD dialog because the incoming call screen should have
404 // higher precedence over this dialog.
405 MotorolaInCallUiNotifier motorolaInCallUiNotifier = new MotorolaInCallUiNotifier(context);
406 addInCallUiListener(motorolaInCallUiNotifier);
407 addListener(motorolaInCallUiNotifier);
yueg7f5acbe2018-01-10 13:50:29 -0800408
Eric Erfanian2ca43182017-08-31 06:57:16 -0700409 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
410 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800411 }
412
413 /**
yueg32d9ae72018-04-05 17:35:56 -0700414 * Return whether we should start call in bubble mode and not show InCallActivity. The call mode
415 * should be set in CallConfiguration in EXTRA_OUTGOING_CALL_EXTRAS when starting a call intent.
416 */
yueg71bf7e42018-04-30 12:00:48 -0700417 public boolean shouldStartInBubbleMode() {
yueg32d9ae72018-04-05 17:35:56 -0700418 if (!ReturnToCallController.isEnabled(context)) {
419 return false;
420 }
yueg4613e8f2018-04-23 13:35:33 -0700421
422 // We only start in Bubble mode for outgoing call
423 DialerCall dialerCall = callList.getPendingOutgoingCall();
424 if (dialerCall == null) {
425 dialerCall = callList.getOutgoingCall();
426 }
427 if (dialerCall == null) {
yueg32d9ae72018-04-05 17:35:56 -0700428 return false;
429 }
yueg2e366472018-04-30 18:08:44 -0700430 if (dialerCall.isEmergencyCall()) {
431 return false;
432 }
yueg4613e8f2018-04-23 13:35:33 -0700433
434 Bundle extras = dialerCall.getIntentExtras();
yuegb493c3f2018-04-27 14:00:12 -0700435 boolean result = shouldStartInBubbleModeWithExtras(extras);
436 if (result) {
437 Logger.get(context)
438 .logCallImpression(
439 DialerImpression.Type.START_CALL_IN_BUBBLE_MODE,
440 dialerCall.getUniqueCallId(),
441 dialerCall.getTimeAddedMs());
442 }
443 return result;
yueg4613e8f2018-04-23 13:35:33 -0700444 }
445
446 private boolean shouldStartInBubbleModeWithExtras(Bundle outgoingExtras) {
447 if (!ReturnToCallController.isEnabled(context)) {
448 return false;
449 }
450
451 if (outgoingExtras == null) {
452 return false;
453 }
454 byte[] callConfigurationByteArray = outgoingExtras.getByteArray(CALL_CONFIGURATION_EXTRA);
yueg32d9ae72018-04-05 17:35:56 -0700455 if (callConfigurationByteArray == null) {
456 return false;
457 }
458 try {
459 CallConfiguration callConfiguration = CallConfiguration.parseFrom(callConfigurationByteArray);
460 LogUtil.i(
461 "InCallPresenter.shouldStartInBubbleMode",
462 "call mode: " + callConfiguration.getCallMode());
463 return callConfiguration.getCallMode() == Mode.BUBBLE;
464 } catch (InvalidProtocolBufferException e) {
465 return false;
466 }
467 }
468
469 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800470 * Called when the telephony service has disconnected from us. This will happen when there are no
471 * more active calls. However, we may still want to continue showing the UI for certain cases like
472 * showing "Call Ended". What we really want is to wait for the activity and the service to both
473 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
474 * secondary method that performs the aforementioned logic.
475 */
476 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700477 LogUtil.d("InCallPresenter.tearDown", "tearDown");
linyuh183cb712017-12-27 17:02:37 -0800478 callList.clearOnDisconnect();
Eric Erfanianccca3152017-02-22 16:32:36 -0800479
linyuh183cb712017-12-27 17:02:37 -0800480 serviceConnected = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800481
linyuh183cb712017-12-27 17:02:37 -0800482 context
Eric Erfanianccca3152017-02-22 16:32:36 -0800483 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800484 .listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800485
486 attemptCleanup();
487 VideoPauseController.getInstance().tearDown();
yueg77cb8e52017-10-27 16:42:51 -0700488 AudioModeProvider.getInstance().removeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800489 }
490
491 private void attemptFinishActivity() {
linyuh183cb712017-12-27 17:02:37 -0800492 screenTimeoutEnabled = true;
493 final boolean doFinish = (inCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700494 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800495 if (doFinish) {
linyuh183cb712017-12-27 17:02:37 -0800496 inCallActivity.setExcludeFromRecents(true);
497 inCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800498 }
499 }
500
501 /**
502 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
503 * for more insight on the tear-down process.
504 */
505 public void unsetActivity(InCallActivity inCallActivity) {
506 if (inCallActivity == null) {
507 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
508 }
linyuh183cb712017-12-27 17:02:37 -0800509 if (this.inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700510 LogUtil.i(
511 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800512 return;
513 }
linyuh183cb712017-12-27 17:02:37 -0800514 if (this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700515 LogUtil.w(
516 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800517 "Second instance of InCallActivity is trying to unregister when another"
518 + " instance is active. Ignoring.");
519 return;
520 }
521 updateActivity(null);
522 }
523
524 /**
525 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
526 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
527 */
528 private void updateActivity(InCallActivity inCallActivity) {
wangqi9982f0d2017-10-11 17:46:07 -0700529 Trace.beginSection("InCallPresenter.updateActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800530 boolean updateListeners = false;
531 boolean doAttemptCleanup = false;
532
533 if (inCallActivity != null) {
linyuh183cb712017-12-27 17:02:37 -0800534 if (this.inCallActivity == null) {
535 context = inCallActivity.getApplicationContext();
Eric Erfanianccca3152017-02-22 16:32:36 -0800536 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700537 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800538 } else {
539 // since setActivity is called onStart(), it can be called multiple times.
540 // This is fine and ignorable, but we do not want to update the world every time
541 // this happens (like going to/from background) so we do not set updateListeners.
542 }
543
linyuh183cb712017-12-27 17:02:37 -0800544 this.inCallActivity = inCallActivity;
545 this.inCallActivity.setExcludeFromRecents(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800546
547 // By the time the UI finally comes up, the call may already be disconnected.
548 // If that's the case, we may need to show an error dialog.
linyuh183cb712017-12-27 17:02:37 -0800549 if (callList != null && callList.getDisconnectedCall() != null) {
550 showDialogOrToastForDisconnectedCall(callList.getDisconnectedCall());
Eric Erfanianccca3152017-02-22 16:32:36 -0800551 }
552
553 // When the UI comes up, we need to first check the in-call state.
554 // If we are showing NO_CALLS, that means that a call probably connected and
555 // then immediately disconnected before the UI was able to come up.
556 // If we dont have any calls, start tearing down the UI instead.
557 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
558 // it has been set.
linyuh183cb712017-12-27 17:02:37 -0800559 if (inCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700560 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800561 attemptFinishActivity();
wangqi9982f0d2017-10-11 17:46:07 -0700562 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800563 return;
564 }
565 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700566 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800567 updateListeners = true;
linyuh183cb712017-12-27 17:02:37 -0800568 this.inCallActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800569
570 // We attempt cleanup for the destroy case but only after we recalculate the state
571 // to see if we need to come back up or stay shut down. This is why we do the
572 // cleanup after the call to onCallListChange() instead of directly here.
573 doAttemptCleanup = true;
574 }
575
576 // Messages can come from the telephony layer while the activity is coming up
577 // and while the activity is going down. So in both cases we need to recalculate what
578 // state we should be in after they complete.
579 // Examples: (1) A new incoming call could come in and then get disconnected before
580 // the activity is created.
581 // (2) All calls could disconnect and then get a new incoming call before the
582 // activity is destroyed.
583 //
Eric Erfanian938468d2017-10-24 14:05:52 -0700584 // a bug - We previously had a check for mServiceConnected here as well, but there are
Eric Erfanianccca3152017-02-22 16:32:36 -0800585 // cases where we need to recalculate the current state even if the service in not
586 // connected. In particular the case where startOrFinish() is called while the app is
587 // already finish()ing. In that case, we skip updating the state with the knowledge that
588 // we will check again once the activity has finished. That means we have to recalculate the
589 // state here even if the service is disconnected since we may not have finished a state
590 // transition while finish()ing.
591 if (updateListeners) {
linyuh183cb712017-12-27 17:02:37 -0800592 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800593 }
594
595 if (doAttemptCleanup) {
596 attemptCleanup();
597 }
wangqi9982f0d2017-10-11 17:46:07 -0700598 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800599 }
600
erfanian12243de2018-04-17 12:17:08 -0700601 public SpeakEasyCallManager getSpeakEasyCallManager() {
602 return this.speakEasyCallManager;
603 }
604
Eric Erfanianccca3152017-02-22 16:32:36 -0800605 public void setManageConferenceActivity(
606 @Nullable ManageConferenceActivity manageConferenceActivity) {
linyuh183cb712017-12-27 17:02:37 -0800607 this.manageConferenceActivity = manageConferenceActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -0800608 }
609
610 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700611 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800612 bringToForeground(showDialpad);
613 }
614
615 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700616 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800617 LatencyReport latencyReport = new LatencyReport(call);
618 if (shouldAttemptBlocking(call)) {
619 maybeBlockCall(call, latencyReport);
620 } else {
621 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800622 externalCallList.onCallAdded(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800623 } else {
624 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800625 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800626 }
627 }
628
629 // Since a call has been added we are no longer waiting for Telecom to send us a call.
630 setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -0800631 call.registerCallback(callCallback);
zachh78e54ac2017-12-05 16:38:35 -0800632 // TODO(maxwelb): Return the future in recordPhoneLookupInfo and propagate.
linyuh183cb712017-12-27 17:02:37 -0800633 PhoneLookupHistoryRecorder.recordPhoneLookupInfo(context.getApplicationContext(), call);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700634 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800635 }
636
637 private boolean shouldAttemptBlocking(android.telecom.Call call) {
638 if (call.getState() != android.telecom.Call.STATE_RINGING) {
639 return false;
640 }
linyuh183cb712017-12-27 17:02:37 -0800641 if (!UserManagerCompat.isUserUnlocked(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700642 LogUtil.i(
643 "InCallPresenter.shouldAttemptBlocking",
644 "not attempting to block incoming call because user is locked");
645 return false;
646 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800647 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700648 LogUtil.i(
649 "InCallPresenter.shouldAttemptBlocking",
650 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800651 return false;
652 }
linyuh183cb712017-12-27 17:02:37 -0800653 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700654 LogUtil.i(
655 "InCallPresenter.shouldAttemptBlocking",
656 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800657 return false;
658 }
659 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
660 return false;
661 }
linyuh183cb712017-12-27 17:02:37 -0800662 if (FilteredNumberCompat.useNewFiltering(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700663 LogUtil.i(
664 "InCallPresenter.shouldAttemptBlocking",
665 "not attempting to block incoming call because framework blocking is in use");
666 return false;
667 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800668 return true;
669 }
670
671 /**
672 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
673 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
674 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
675 * call anyways.
676 */
677 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
linyuh183cb712017-12-27 17:02:37 -0800678 final String countryIso = GeoUtil.getCurrentCountryIso(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800679 final String number = TelecomCallUtil.getNumber(call);
680 final long timeAdded = System.currentTimeMillis();
681
682 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
683 // main UI thread. It is needed so we can change its value within different scopes, since
684 // that cannot be done with a final boolean.
685 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
686
687 final Handler handler = new Handler();
688
689 // Proceed if the query is slow; the call may still be blocked after the query returns.
690 final Runnable runnable =
691 new Runnable() {
692 @Override
693 public void run() {
694 hasTimedOut.set(true);
695 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800696 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800697 }
698 };
699 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
700
701 OnCheckBlockedListener onCheckBlockedListener =
702 new OnCheckBlockedListener() {
703 @Override
704 public void onCheckComplete(final Integer id) {
705 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700706 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800707 return;
708 }
709 if (!hasTimedOut.get()) {
710 handler.removeCallbacks(runnable);
711 }
712 if (id == null) {
713 if (!hasTimedOut.get()) {
714 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800715 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800716 }
717 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700718 LogUtil.d(
719 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800720 if (!hasTimedOut.get()) {
721 handler.removeCallbacks(runnable);
722
723 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800724 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800725 }
726 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700727 LogUtil.i(
728 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800729 call.reject(false, null);
linyuh183cb712017-12-27 17:02:37 -0800730 Logger.get(context).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
Eric Erfanianccca3152017-02-22 16:32:36 -0800731
732 /*
733 * If mContext is null, then the InCallPresenter was torn down before the
734 * block check had a chance to complete. The context is no longer valid, so
735 * don't attempt to remove the call log entry.
736 */
linyuh183cb712017-12-27 17:02:37 -0800737 if (context == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800738 return;
739 }
740 // Register observer to update the call log.
741 // BlockedNumberContentObserver will unregister after successful log or timeout.
742 BlockedNumberContentObserver contentObserver =
linyuh183cb712017-12-27 17:02:37 -0800743 new BlockedNumberContentObserver(context, new Handler(), number, timeAdded);
Eric Erfanianccca3152017-02-22 16:32:36 -0800744 contentObserver.register();
745 }
746 }
747 };
748
linyuh183cb712017-12-27 17:02:37 -0800749 filteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800750 }
751
752 public void onCallRemoved(android.telecom.Call call) {
753 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800754 externalCallList.onCallRemoved(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800755 } else {
linyuh183cb712017-12-27 17:02:37 -0800756 callList.onCallRemoved(context, call);
757 call.unregisterCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800758 }
759 }
760
761 public void onCanAddCallChanged(boolean canAddCall) {
linyuh183cb712017-12-27 17:02:37 -0800762 for (CanAddCallListener listener : canAddCallListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800763 listener.onCanAddCallChanged(canAddCall);
764 }
765 }
766
767 @Override
768 public void onWiFiToLteHandover(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -0700769 if (call.hasShownWiFiToLteHandoverToast()) {
770 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800771 }
yueg32d9ae72018-04-05 17:35:56 -0700772
773 Toast.makeText(context, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG)
774 .show();
775 call.setHasShownWiFiToLteHandoverToast();
Eric Erfanianccca3152017-02-22 16:32:36 -0800776 }
777
778 @Override
779 public void onHandoverToWifiFailed(DialerCall call) {
linyuh183cb712017-12-27 17:02:37 -0800780 if (inCallActivity != null) {
781 inCallActivity.showDialogOrToastForWifiHandoverFailure(call);
yueg32d9ae72018-04-05 17:35:56 -0700782 } else {
783 Toast.makeText(context, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT)
784 .show();
Eric Erfanianccca3152017-02-22 16:32:36 -0800785 }
786 }
787
Eric Erfanianc857f902017-05-15 14:05:33 -0700788 @Override
789 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
790 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
yuegb47528e2018-04-24 12:12:57 -0700791
792 if (!InternationalCallOnWifiDialogFragment.shouldShow(context)) {
793 LogUtil.i(
794 "InCallPresenter.onInternationalCallOnWifi",
795 "InternationalCallOnWifiDialogFragment.shouldShow returned false");
796 return;
797 }
798
linyuh183cb712017-12-27 17:02:37 -0800799 if (inCallActivity != null) {
800 inCallActivity.showDialogForInternationalCallOnWifi(call);
yuegb47528e2018-04-24 12:12:57 -0700801 } else {
802 Intent intent = new Intent(context, InternationalCallOnWifiDialogActivity.class);
803 // Prevent showing MainActivity with InternationalCallOnWifiDialogActivity on above
804 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
805 intent.putExtra(InternationalCallOnWifiDialogActivity.EXTRA_CALL_ID, call.getId());
806 context.startActivity(intent);
Eric Erfanianc857f902017-05-15 14:05:33 -0700807 }
808 }
809
Eric Erfanianccca3152017-02-22 16:32:36 -0800810 /**
811 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
812 * app based on the information it gets from CallList. Dispatches the in-call state to all
813 * listeners. Can trigger the creation or destruction of the UI based on the states that is
814 * calculates.
815 */
816 @Override
817 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700818 Trace.beginSection("InCallPresenter.onCallListChange");
linyuh183cb712017-12-27 17:02:37 -0800819 if (inCallActivity != null && inCallActivity.isInCallScreenAnimating()) {
820 awaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700821 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800822 return;
823 }
824 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700825 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800826 return;
827 }
828
linyuh183cb712017-12-27 17:02:37 -0800829 awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800830
831 InCallState newState = getPotentialStateFromCallList(callList);
linyuh183cb712017-12-27 17:02:37 -0800832 InCallState oldState = inCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700833 LogUtil.d(
834 "InCallPresenter.onCallListChange",
835 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700836
837 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
838 // incall activity for that call will still exist (even if it's not visible). In the case of
839 // an incoming call in that situation, just disconnect that "waiting for account" call and
840 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
Eric Erfanian938468d2017-10-24 14:05:52 -0700841 // a bug for more details.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700842 DialerCall waitingForAccountCall;
843 if (newState == InCallState.INCOMING
844 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
845 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700846 // The InCallActivity might be destroyed or not started yet at this point.
847 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800848 inCallActivity.dismissPendingDialogs();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700849 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700850 }
851
Eric Erfanianccca3152017-02-22 16:32:36 -0800852 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700853 LogUtil.d(
854 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800855
856 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700857 LogUtil.i(
858 "InCallPresenter.onCallListChange",
859 "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800860 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800861
yuegb9103042018-03-30 12:12:25 -0700862 // Foreground call changed
863 DialerCall primary = null;
864 if (newState == InCallState.INCOMING) {
865 primary = callList.getIncomingCall();
866 } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
867 primary = callList.getOutgoingCall();
868 if (primary == null) {
869 primary = callList.getPendingOutgoingCall();
870 }
871 } else if (newState == InCallState.INCALL) {
872 primary = getCallToDisplay(callList, null, false);
873 }
874 if (primary != null) {
875 onForegroundCallChanged(primary);
876 }
877
Eric Erfanianccca3152017-02-22 16:32:36 -0800878 // notify listeners of new state
linyuh183cb712017-12-27 17:02:37 -0800879 for (InCallStateListener listener : listeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700880 LogUtil.d(
881 "InCallPresenter.onCallListChange",
linyuh183cb712017-12-27 17:02:37 -0800882 "Notify " + listener + " of state " + inCallState.toString());
883 listener.onStateChange(oldState, inCallState, callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800884 }
885
886 if (isActivityStarted()) {
887 final boolean hasCall =
888 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
linyuh183cb712017-12-27 17:02:37 -0800889 inCallActivity.dismissKeyguard(hasCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800890 }
twyen27c37182018-04-24 11:48:12 -0700891
Eric Erfanian2ca43182017-08-31 06:57:16 -0700892 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800893 }
894
yuegb9103042018-03-30 12:12:25 -0700895 /**
896 * Get the highest priority call to display. Goes through the calls and chooses which to return
897 * based on priority of which type of call to display to the user. Callers can use the "ignore"
898 * feature to get the second best call by passing a previously found primary call as ignore.
899 *
900 * @param ignore A call to ignore if found.
901 */
902 static DialerCall getCallToDisplay(
903 CallList callList, DialerCall ignore, boolean skipDisconnected) {
904 // Active calls come second. An active call always gets precedent.
905 DialerCall retval = callList.getActiveCall();
906 if (retval != null && retval != ignore) {
907 return retval;
908 }
909
910 // Sometimes there is intemediate state that two calls are in active even one is about
911 // to be on hold.
912 retval = callList.getSecondActiveCall();
913 if (retval != null && retval != ignore) {
914 return retval;
915 }
916
917 // Disconnected calls get primary position if there are no active calls
918 // to let user know quickly what call has disconnected. Disconnected
919 // calls are very short lived.
920 if (!skipDisconnected) {
921 retval = callList.getDisconnectingCall();
922 if (retval != null && retval != ignore) {
923 return retval;
924 }
925 retval = callList.getDisconnectedCall();
926 if (retval != null && retval != ignore) {
927 return retval;
928 }
929 }
930
931 // Then we go to background call (calls on hold)
932 retval = callList.getBackgroundCall();
933 if (retval != null && retval != ignore) {
934 return retval;
935 }
936
937 // Lastly, we go to a second background call.
938 retval = callList.getSecondBackgroundCall();
939
940 return retval;
941 }
942
Eric Erfanianccca3152017-02-22 16:32:36 -0800943 /** Called when there is a new incoming call. */
944 @Override
945 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700946 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800947 InCallState newState = startOrFinishUi(InCallState.INCOMING);
linyuh183cb712017-12-27 17:02:37 -0800948 InCallState oldState = inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800949
Eric Erfanian2ca43182017-08-31 06:57:16 -0700950 LogUtil.i(
951 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800952 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800953
wangqicf61ca02017-08-31 15:32:55 -0700954 Trace.beginSection("listener.onIncomingCall");
linyuh183cb712017-12-27 17:02:37 -0800955 for (IncomingCallListener listener : incomingCallListeners) {
956 listener.onIncomingCall(oldState, inCallState, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800957 }
wangqicf61ca02017-08-31 15:32:55 -0700958 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800959
wangqicf61ca02017-08-31 15:32:55 -0700960 Trace.beginSection("onPrimaryCallStateChanged");
linyuh183cb712017-12-27 17:02:37 -0800961 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800962 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800963 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800964 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700965 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700966 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800967 }
968
969 @Override
970 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700971 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
linyuh183cb712017-12-27 17:02:37 -0800972 && inCallState == InCallPresenter.InCallState.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800973 LogUtil.i(
974 "InCallPresenter.onUpgradeToVideo",
975 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700976 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800977 }
978
linyuh183cb712017-12-27 17:02:37 -0800979 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800980 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800981 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800982 }
983 }
984
985 @Override
wangqibc28ea72018-04-02 16:23:00 -0700986 public void onUpgradeToRtt(DialerCall call, int rttRequestId) {
987 if (inCallActivity != null) {
988 inCallActivity.showDialogForRttRequest(call, rttRequestId);
989 }
990 }
991
992 @Override
erfanian3887ab02018-05-11 11:02:53 -0700993 public void onSpeakEasyStateChange() {
994 if (inCallActivity != null) {
995 inCallActivity.onPrimaryCallStateChanged();
996 }
997 }
998
999 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001000 public void onSessionModificationStateChange(DialerCall call) {
1001 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -08001002 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
linyuh183cb712017-12-27 17:02:37 -08001003 if (proximitySensor == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001004 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
1005 return;
1006 }
linyuh183cb712017-12-27 17:02:37 -08001007 proximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001008 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
linyuh183cb712017-12-27 17:02:37 -08001009 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001010 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001011 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001012 }
1013 }
1014
1015 /**
1016 * Called when a call becomes disconnected. Called everytime an existing call changes from being
1017 * connected (incoming/outgoing/active) to disconnected.
1018 */
1019 @Override
1020 public void onDisconnect(DialerCall call) {
linyuh7b86f562017-11-16 11:24:09 -08001021 showDialogOrToastForDisconnectedCall(call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001022
1023 // We need to do the run the same code as onCallListChange.
linyuh183cb712017-12-27 17:02:37 -08001024 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001025
1026 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001027 inCallActivity.dismissKeyguard(false);
Eric Erfanianccca3152017-02-22 16:32:36 -08001028 }
1029
1030 if (call.isEmergencyCall()) {
linyuh183cb712017-12-27 17:02:37 -08001031 FilteredNumbersUtil.recordLastEmergencyCallTime(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001032 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001033
linyuh183cb712017-12-27 17:02:37 -08001034 if (!callList.hasLiveCall()
Eric Erfanianfc37b022017-03-21 10:11:17 -07001035 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -07001036 && !isSecretCode(call.getNumber())
wangqi9982f0d2017-10-11 17:46:07 -07001037 && !call.isVoiceMailNumber()) {
linyuh183cb712017-12-27 17:02:37 -08001038 PostCall.onCallDisconnected(context, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -08001039 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001040 }
1041
Eric Erfanian10b34a52017-05-04 08:23:17 -07001042 private boolean isSecretCode(@Nullable String number) {
1043 return number != null
1044 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
1045 }
1046
Eric Erfanianccca3152017-02-22 16:32:36 -08001047 /** Given the call list, return the state in which the in-call screen should be. */
1048 public InCallState getPotentialStateFromCallList(CallList callList) {
1049
1050 InCallState newState = InCallState.NO_CALLS;
1051
1052 if (callList == null) {
1053 return newState;
1054 }
1055 if (callList.getIncomingCall() != null) {
1056 newState = InCallState.INCOMING;
1057 } else if (callList.getWaitingForAccountCall() != null) {
1058 newState = InCallState.WAITING_FOR_ACCOUNT;
1059 } else if (callList.getPendingOutgoingCall() != null) {
1060 newState = InCallState.PENDING_OUTGOING;
1061 } else if (callList.getOutgoingCall() != null) {
1062 newState = InCallState.OUTGOING;
1063 } else if (callList.getActiveCall() != null
1064 || callList.getBackgroundCall() != null
1065 || callList.getDisconnectedCall() != null
1066 || callList.getDisconnectingCall() != null) {
1067 newState = InCallState.INCALL;
1068 }
1069
1070 if (newState == InCallState.NO_CALLS) {
linyuh183cb712017-12-27 17:02:37 -08001071 if (boundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001072 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001073 }
1074 }
1075
1076 return newState;
1077 }
1078
1079 public boolean isBoundAndWaitingForOutgoingCall() {
linyuh183cb712017-12-27 17:02:37 -08001080 return boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -08001081 }
1082
1083 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001084 LogUtil.i(
1085 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
1086 "setBoundAndWaitingForOutgoingCall: " + isBound);
linyuh183cb712017-12-27 17:02:37 -08001087 boundAndWaitingForOutgoingCall = isBound;
1088 themeColorManager.setPendingPhoneAccountHandle(handle);
1089 if (isBound && inCallState == InCallState.NO_CALLS) {
1090 inCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001091 }
1092 }
1093
1094 public void onShrinkAnimationComplete() {
linyuh183cb712017-12-27 17:02:37 -08001095 if (awaitingCallListUpdate) {
1096 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001097 }
1098 }
1099
1100 public void addIncomingCallListener(IncomingCallListener listener) {
1101 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001102 incomingCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001103 }
1104
1105 public void removeIncomingCallListener(IncomingCallListener listener) {
1106 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001107 incomingCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001108 }
1109 }
1110
1111 public void addListener(InCallStateListener listener) {
1112 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001113 listeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001114 }
1115
1116 public void removeListener(InCallStateListener listener) {
1117 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001118 listeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001119 }
1120 }
1121
1122 public void addDetailsListener(InCallDetailsListener listener) {
1123 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001124 detailsListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001125 }
1126
1127 public void removeDetailsListener(InCallDetailsListener listener) {
1128 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001129 detailsListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001130 }
1131 }
1132
1133 public void addCanAddCallListener(CanAddCallListener listener) {
1134 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001135 canAddCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001136 }
1137
1138 public void removeCanAddCallListener(CanAddCallListener listener) {
1139 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001140 canAddCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001141 }
1142 }
1143
1144 public void addOrientationListener(InCallOrientationListener listener) {
1145 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001146 orientationListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001147 }
1148
1149 public void removeOrientationListener(InCallOrientationListener listener) {
1150 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001151 orientationListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001152 }
1153 }
1154
1155 public void addInCallEventListener(InCallEventListener listener) {
1156 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001157 inCallEventListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001158 }
1159
1160 public void removeInCallEventListener(InCallEventListener listener) {
1161 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001162 inCallEventListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001163 }
1164 }
1165
1166 public ProximitySensor getProximitySensor() {
linyuh183cb712017-12-27 17:02:37 -08001167 return proximitySensor;
Eric Erfanianccca3152017-02-22 16:32:36 -08001168 }
1169
1170 public PseudoScreenState getPseudoScreenState() {
linyuh183cb712017-12-27 17:02:37 -08001171 return pseudoScreenState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001172 }
1173
1174 /** Returns true if the incall app is the foreground application. */
1175 public boolean isShowingInCallUi() {
1176 if (!isActivityStarted()) {
1177 return false;
1178 }
linyuh183cb712017-12-27 17:02:37 -08001179 if (manageConferenceActivity != null && manageConferenceActivity.isVisible()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001180 return true;
1181 }
linyuh183cb712017-12-27 17:02:37 -08001182 return inCallActivity.isVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001183 }
1184
1185 /**
1186 * Returns true if the activity has been created and is running. Returns true as long as activity
1187 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
1188 * (not in foreground).
1189 */
1190 public boolean isActivityStarted() {
linyuh183cb712017-12-27 17:02:37 -08001191 return (inCallActivity != null
1192 && !inCallActivity.isDestroyed()
1193 && !inCallActivity.isFinishing());
Eric Erfanianccca3152017-02-22 16:32:36 -08001194 }
1195
1196 /**
1197 * Determines if the In-Call app is currently changing configuration.
1198 *
1199 * @return {@code true} if the In-Call app is changing configuration.
1200 */
1201 public boolean isChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001202 return isChangingConfigurations;
Eric Erfanianccca3152017-02-22 16:32:36 -08001203 }
1204
1205 /**
1206 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1207 * screen orientation).
1208 */
1209 /*package*/
1210 void updateIsChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001211 isChangingConfigurations = false;
1212 if (inCallActivity != null) {
1213 isChangingConfigurations = inCallActivity.isChangingConfigurations();
Eric Erfanianccca3152017-02-22 16:32:36 -08001214 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001215 LogUtil.v(
1216 "InCallPresenter.updateIsChangingConfigurations",
linyuh183cb712017-12-27 17:02:37 -08001217 "updateIsChangingConfigurations = " + isChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001218 }
1219
yueg10f6e822018-01-17 15:32:18 -08001220 /** Called when the activity goes in/out of the foreground. */
1221 public void onUiShowing(boolean showing) {
linyuh183cb712017-12-27 17:02:37 -08001222 if (proximitySensor != null) {
1223 proximitySensor.onInCallShowing(showing);
Eric Erfanianccca3152017-02-22 16:32:36 -08001224 }
1225
yueg092b21c2017-11-15 16:20:07 -08001226 if (!showing) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001227 updateIsChangingConfigurations();
1228 }
1229
linyuh183cb712017-12-27 17:02:37 -08001230 for (InCallUiListener listener : inCallUiListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001231 listener.onUiShowing(showing);
1232 }
1233
linyuh183cb712017-12-27 17:02:37 -08001234 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001235 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001236 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001237 }
1238 }
1239
Eric Erfanian2ca43182017-08-31 06:57:16 -07001240 public void refreshUi() {
linyuh183cb712017-12-27 17:02:37 -08001241 if (inCallActivity != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001242 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001243 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001244 }
1245 }
1246
Eric Erfanianccca3152017-02-22 16:32:36 -08001247 public void addInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001248 inCallUiListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001249 }
1250
1251 public boolean removeInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001252 return inCallUiListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001253 }
1254
1255 /*package*/
1256 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001257 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001258 notifyVideoPauseController(true);
Eric Erfanian2ca43182017-08-31 06:57:16 -07001259 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001260 }
1261
1262 /*package*/
1263 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001264 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001265 notifyVideoPauseController(false);
1266 }
1267
1268 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001269 LogUtil.d(
1270 "InCallPresenter.notifyVideoPauseController",
linyuh183cb712017-12-27 17:02:37 -08001271 "mIsChangingConfigurations=" + isChangingConfigurations);
1272 if (!isChangingConfigurations) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001273 VideoPauseController.getInstance().onUiShowing(showing);
1274 }
1275 }
1276
1277 /** Brings the app into the foreground if possible. */
1278 public void bringToForeground(boolean showDialpad) {
1279 // Before we bring the incall UI to the foreground, we check to see if:
1280 // 1. It is not currently in the foreground
1281 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1282 // be displayed)
1283 // If the activity hadn't actually been started previously, yet there are still calls
1284 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1285 // bring it up the UI regardless.
linyuh183cb712017-12-27 17:02:37 -08001286 if (!isShowingInCallUi() && inCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001287 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001288 }
1289 }
1290
1291 public void onPostDialCharWait(String callId, String chars) {
yuega489f512018-04-25 12:01:09 -07001292 // If not visible, inCallActivity is stopped. Starting from P, calling recreate() will destroy
1293 // the old activity instance and create a new instance immediately. Previously, the old activity
1294 // went through its lifecycle from create to destroy before creating a new instance.
1295 // So this case doesn't work now: make a call with char WAIT, leave in call UI, call gets
1296 // connected, and go back to in call UI to see the dialog.
1297 // So we should show dialog in an empty activity if inCallActivity is not visible. And it also
1298 // helps with background calling.
1299 if (isActivityStarted() && inCallActivity.isVisible()) {
linyuh183cb712017-12-27 17:02:37 -08001300 inCallActivity.showDialogForPostCharWait(callId, chars);
yuega489f512018-04-25 12:01:09 -07001301 } else {
1302 Intent intent = new Intent(context, PostCharDialogActivity.class);
1303 // Prevent showing MainActivity with PostCharDialogActivity on above
1304 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1305 intent.putExtra(PostCharDialogActivity.EXTRA_CALL_ID, callId);
1306 intent.putExtra(PostCharDialogActivity.EXTRA_POST_DIAL_STRING, chars);
1307 context.startActivity(intent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001308 }
1309 }
1310
1311 /**
1312 * Handles the green CALL key while in-call.
1313 *
1314 * @return true if we consumed the event.
1315 */
1316 public boolean handleCallKey() {
1317 LogUtil.v("InCallPresenter.handleCallKey", null);
1318
1319 // The green CALL button means either "Answer", "Unhold", or
1320 // "Swap calls", or can be a no-op, depending on the current state
1321 // of the Phone.
1322
1323 /** INCOMING CALL */
linyuh183cb712017-12-27 17:02:37 -08001324 final CallList calls = callList;
Eric Erfanianccca3152017-02-22 16:32:36 -08001325 final DialerCall incomingCall = calls.getIncomingCall();
1326 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1327
1328 // (1) Attempt to answer a call
1329 if (incomingCall != null) {
1330 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1331 return true;
1332 }
1333
1334 /** STATE_ACTIVE CALL */
1335 final DialerCall activeCall = calls.getActiveCall();
1336 if (activeCall != null) {
1337 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1338 // consolidate this logic.
1339 final boolean canMerge =
1340 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1341 final boolean canSwap =
1342 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1343
Eric Erfanian2ca43182017-08-31 06:57:16 -07001344 LogUtil.v(
1345 "InCallPresenter.handleCallKey",
1346 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001347
1348 // (2) Attempt actions on conference calls
1349 if (canMerge) {
1350 TelecomAdapter.getInstance().merge(activeCall.getId());
1351 return true;
1352 } else if (canSwap) {
1353 TelecomAdapter.getInstance().swap(activeCall.getId());
1354 return true;
1355 }
1356 }
1357
1358 /** BACKGROUND CALL */
1359 final DialerCall heldCall = calls.getBackgroundCall();
1360 if (heldCall != null) {
1361 // We have a hold call so presumeable it will always support HOLD...but
1362 // there is no harm in double checking.
1363 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1364
Eric Erfanian2ca43182017-08-31 06:57:16 -07001365 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001366
1367 // (4) unhold call
wangqibb94ca62018-04-27 14:34:04 -07001368 if (heldCall.getState() == DialerCallState.ONHOLD && canHold) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001369 heldCall.unhold();
1370 return true;
1371 }
1372 }
1373
1374 // Always consume hard keys
1375 return true;
1376 }
1377
Eric Erfanianccca3152017-02-22 16:32:36 -08001378 /** Clears the previous fullscreen state. */
1379 public void clearFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001380 isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001381 }
1382
1383 /**
1384 * Changes the fullscreen mode of the in-call UI.
1385 *
1386 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1387 * otherwise.
1388 */
1389 public void setFullScreen(boolean isFullScreen) {
1390 setFullScreen(isFullScreen, false /* force */);
1391 }
1392
1393 /**
1394 * Changes the fullscreen mode of the in-call UI.
1395 *
1396 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1397 * otherwise.
1398 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1399 */
1400 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001401 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001402
1403 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1404 if (isDialpadVisible()) {
1405 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001406 LogUtil.v(
1407 "InCallPresenter.setFullScreen",
1408 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001409 }
1410
linyuh183cb712017-12-27 17:02:37 -08001411 if (this.isFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001412 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001413 return;
1414 }
linyuh183cb712017-12-27 17:02:37 -08001415 this.isFullScreen = isFullScreen;
1416 notifyFullscreenModeChange(this.isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001417 }
1418
1419 /**
1420 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1421 * otherwise.
1422 */
1423 public boolean isFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001424 return isFullScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001425 }
1426
1427 /**
1428 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1429 *
1430 * @param isFullscreenMode {@code True} if entering full screen mode.
1431 */
1432 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001433 for (InCallEventListener listener : inCallEventListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001434 listener.onFullscreenModeChanged(isFullscreenMode);
1435 }
1436 }
1437
linyuh7b86f562017-11-16 11:24:09 -08001438 /** Instruct the in-call activity to show an error dialog or toast for a disconnected call. */
1439 private void showDialogOrToastForDisconnectedCall(DialerCall call) {
wangqibb94ca62018-04-27 14:34:04 -07001440 if (call.getState() != DialerCallState.DISCONNECTED) {
linyuh7b86f562017-11-16 11:24:09 -08001441 return;
Eric Erfanianccca3152017-02-22 16:32:36 -08001442 }
linyuh7b86f562017-11-16 11:24:09 -08001443
1444 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1445 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1446 setDisconnectCauseForMissingAccounts(call);
1447 }
1448
yueg32d9ae72018-04-05 17:35:56 -07001449 if (isActivityStarted()) {
1450 inCallActivity.showDialogOrToastForDisconnectedCall(
1451 new DisconnectMessage(inCallActivity, call));
1452 } else {
1453 CharSequence message = new DisconnectMessage(context, call).toastMessage;
1454 if (message != null) {
1455 Toast.makeText(context, message, Toast.LENGTH_LONG).show();
1456 }
1457 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001458 }
1459
1460 /**
1461 * When the state of in-call changes, this is the first method to get called. It determines if the
1462 * UI needs to be started or finished depending on the new state and does it.
1463 */
1464 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001465 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001466 LogUtil.d(
linyuh183cb712017-12-27 17:02:37 -08001467 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + inCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001468
1469 // TODO: Consider a proper state machine implementation
1470
1471 // If the state isn't changing we have already done any starting/stopping of activities in
1472 // a previous pass...so lets cut out early
linyuh183cb712017-12-27 17:02:37 -08001473 if (newState == inCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001474 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001475 return newState;
1476 }
1477
1478 // A new Incoming call means that the user needs to be notified of the the call (since
1479 // it wasn't them who initiated it). We do this through full screen notifications and
1480 // happens indirectly through {@link StatusBarNotifier}.
1481 //
1482 // The process for incoming calls is as follows:
1483 //
1484 // 1) CallList - Announces existence of new INCOMING call
1485 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1486 // - should be set to INCOMING.
1487 // 3) InCallPresenter - This method is called to see if we need to start or finish
1488 // the app given the new state.
1489 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1490 // StatusBarNotifier explicitly to issue a FullScreen Notification
1491 // that will either start the InCallActivity or show the user a
1492 // top-level notification dialog if the user is in an immersive app.
1493 // That notification can also start the InCallActivity.
1494 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1495 // call InCallPresenter::setActivity() to let the presenter
1496 // know that start-up is complete.
1497 //
1498 // [ AND NOW YOU'RE IN THE CALL. voila! ]
Eric Erfanianccca3152017-02-22 16:32:36 -08001499
1500 // A dialog to show on top of the InCallUI to select a PhoneAccount
1501 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1502
1503 // A new outgoing call indicates that the user just now dialed a number and when that
1504 // happens we need to display the screen immediately or show an account picker dialog if
1505 // no default is set. However, if the main InCallUI is already visible, we do not want to
1506 // re-initiate the start-up animation, so we do not need to do anything here.
1507 //
1508 // It is also possible to go into an intermediate state where the call has been initiated
1509 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1510 // This pending outgoing state can also launch the call screen.
1511 //
1512 // This is different from the incoming call sequence because we do not need to shock the
1513 // user with a top-level notification. Just show the call UI normally.
1514 boolean callCardFragmentVisible =
linyuh183cb712017-12-27 17:02:37 -08001515 inCallActivity != null && inCallActivity.getCallCardFragmentVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001516 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1517 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1518
1519 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1520 // outgoing call process, so the UI should be brought up to show an error dialog.
1521 showCallUi |=
linyuh183cb712017-12-27 17:02:37 -08001522 (InCallState.PENDING_OUTGOING == inCallState
Eric Erfanianccca3152017-02-22 16:32:36 -08001523 && InCallState.INCALL == newState
1524 && !isShowingInCallUi());
1525
1526 // Another exception - InCallActivity is in charge of disconnecting a call with no
1527 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1528 // call so that:
1529 // 1) The call can be disconnected correctly
1530 // 2) The UI comes up and correctly displays the error dialog.
1531 // TODO: Remove these special case conditions by making InCallPresenter a true state
1532 // machine. Telecom should also be the component responsible for disconnecting a call
1533 // with no valid accounts.
1534 showCallUi |=
1535 InCallState.PENDING_OUTGOING == newState
1536 && mainUiNotVisible
linyuh183cb712017-12-27 17:02:37 -08001537 && isCallWithNoValidAccounts(callList.getPendingOutgoingCall());
Eric Erfanianccca3152017-02-22 16:32:36 -08001538
1539 // The only time that we have an instance of mInCallActivity and it isn't started is
1540 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1541 // the activity. When it is finally destroyed, we double check if we should bring it back
1542 // up so we aren't going to lose anything by avoiding a second startup here.
linyuh183cb712017-12-27 17:02:37 -08001543 boolean activityIsFinishing = inCallActivity != null && !isActivityStarted();
Eric Erfanianccca3152017-02-22 16:32:36 -08001544 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001545 LogUtil.i(
1546 "InCallPresenter.startOrFinishUi",
linyuh183cb712017-12-27 17:02:37 -08001547 "Undo the state change: " + newState + " -> " + inCallState);
wangqicf61ca02017-08-31 15:32:55 -07001548 Trace.endSection();
linyuh183cb712017-12-27 17:02:37 -08001549 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001550 }
1551
1552 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1553 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1554 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1555 // outgoing call after user ignore it by pressing home button.
1556 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1557 && !showCallUi
1558 && isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001559 inCallActivity.dismissPendingDialogs();
Eric Erfanianccca3152017-02-22 16:32:36 -08001560 }
1561
yueg4613e8f2018-04-23 13:35:33 -07001562 if ((showCallUi || showAccountPicker) && !shouldStartInBubbleMode()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001563 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001564 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001565 } else if (newState == InCallState.NO_CALLS) {
1566 // The new state is the no calls state. Tear everything down.
yueg8e544862018-04-06 14:34:59 -07001567 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001568 attemptFinishActivity();
1569 attemptCleanup();
1570 }
1571
wangqicf61ca02017-08-31 15:32:55 -07001572 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001573 return newState;
1574 }
1575
1576 /**
1577 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1578 * or PhoneAccounts to select from.
1579 */
1580 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1581
1582 Bundle extras = call.getIntentExtras();
1583 // Initialize the extras bundle to avoid NPE
1584 if (extras == null) {
1585 extras = new Bundle();
1586 }
1587
1588 final List<PhoneAccountHandle> phoneAccountHandles =
1589 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1590
1591 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1592 String scheme = call.getHandle().getScheme();
1593 final String errorMsg =
1594 PhoneAccount.SCHEME_TEL.equals(scheme)
linyuh183cb712017-12-27 17:02:37 -08001595 ? context.getString(R.string.callFailed_simError)
1596 : context.getString(R.string.incall_error_supp_service_unknown);
Eric Erfanianccca3152017-02-22 16:32:36 -08001597 DisconnectCause disconnectCause =
1598 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1599 call.setDisconnectCause(disconnectCause);
1600 }
1601 }
1602
Eric Erfanianccca3152017-02-22 16:32:36 -08001603 /**
1604 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1605 * Calling classes should use this as an indication whether to interact with the
1606 * InCallPresenter or not.
1607 */
1608 public boolean isReadyForTearDown() {
linyuh183cb712017-12-27 17:02:37 -08001609 return inCallActivity == null && !serviceConnected && inCallState == InCallState.NO_CALLS;
Eric Erfanianccca3152017-02-22 16:32:36 -08001610 }
1611
1612 /**
1613 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1614 */
1615 private void attemptCleanup() {
1616 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001617 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001618
1619 cleanupSurfaces();
1620
linyuh183cb712017-12-27 17:02:37 -08001621 isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001622
1623 // blow away stale contact info so that we get fresh data on
1624 // the next set of calls
linyuh183cb712017-12-27 17:02:37 -08001625 if (contactInfoCache != null) {
1626 contactInfoCache.clearCache();
Eric Erfanianccca3152017-02-22 16:32:36 -08001627 }
linyuh183cb712017-12-27 17:02:37 -08001628 contactInfoCache = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001629
linyuh183cb712017-12-27 17:02:37 -08001630 if (proximitySensor != null) {
1631 removeListener(proximitySensor);
1632 proximitySensor.tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -08001633 }
linyuh183cb712017-12-27 17:02:37 -08001634 proximitySensor = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001635
linyuh183cb712017-12-27 17:02:37 -08001636 if (statusBarNotifier != null) {
1637 removeListener(statusBarNotifier);
1638 EnrichedCallComponent.get(context)
Eric Erfaniand8046e52017-04-06 09:41:50 -07001639 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -08001640 .unregisterStateChangedListener(statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001641 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001642
linyuh183cb712017-12-27 17:02:37 -08001643 if (externalCallNotifier != null && externalCallList != null) {
1644 externalCallList.removeExternalCallListener(externalCallNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001645 }
linyuh183cb712017-12-27 17:02:37 -08001646 statusBarNotifier = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001647
linyuh183cb712017-12-27 17:02:37 -08001648 if (callList != null) {
1649 callList.removeListener(this);
1650 callList.removeListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001651 }
linyuh183cb712017-12-27 17:02:37 -08001652 callList = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001653
linyuh183cb712017-12-27 17:02:37 -08001654 context = null;
1655 inCallActivity = null;
1656 manageConferenceActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001657
linyuh183cb712017-12-27 17:02:37 -08001658 listeners.clear();
1659 incomingCallListeners.clear();
1660 detailsListeners.clear();
1661 canAddCallListeners.clear();
1662 orientationListeners.clear();
1663 inCallEventListeners.clear();
1664 inCallUiListeners.clear();
1665 if (!inCallUiLocks.isEmpty()) {
1666 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + inCallUiLocks);
1667 inCallUiLocks.clear();
twyen8efb4952017-10-06 16:35:54 -07001668 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001669 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001670 }
1671 }
1672
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001673 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001674 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
linyuh183cb712017-12-27 17:02:37 -08001675 context.startActivity(
1676 InCallActivity.getIntent(context, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001677 }
1678
1679 public void onServiceBind() {
linyuh183cb712017-12-27 17:02:37 -08001680 serviceBound = true;
Eric Erfanianccca3152017-02-22 16:32:36 -08001681 }
1682
1683 public void onServiceUnbind() {
1684 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -08001685 serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001686 }
1687
1688 public boolean isServiceBound() {
linyuh183cb712017-12-27 17:02:37 -08001689 return serviceBound;
Eric Erfanianccca3152017-02-22 16:32:36 -08001690 }
1691
1692 public void maybeStartRevealAnimation(Intent intent) {
linyuh183cb712017-12-27 17:02:37 -08001693 if (intent == null || inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001694 return;
1695 }
1696 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1697 if (extras == null) {
1698 // Incoming call, just show the in-call UI directly.
1699 return;
1700 }
1701
1702 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1703 // Account selection dialog will show up so don't show the animation.
1704 return;
1705 }
1706
1707 final PhoneAccountHandle accountHandle =
1708 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1709 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001710
yueg32d9ae72018-04-05 17:35:56 -07001711 setBoundAndWaitingForOutgoingCall(true, accountHandle);
1712
yueg4613e8f2018-04-23 13:35:33 -07001713 if (shouldStartInBubbleModeWithExtras(extras)) {
yueg32d9ae72018-04-05 17:35:56 -07001714 LogUtil.i("InCallPresenter.maybeStartRevealAnimation", "shouldStartInBubbleMode");
1715 // Show bubble instead of in call UI
1716 return;
1717 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001718
1719 final Intent activityIntent =
linyuh183cb712017-12-27 17:02:37 -08001720 InCallActivity.getIntent(context, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001721 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
linyuh183cb712017-12-27 17:02:37 -08001722 context.startActivity(activityIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001723 }
1724
1725 /**
1726 * Retrieves the current in-call camera manager instance, creating if necessary.
1727 *
1728 * @return The {@link InCallCameraManager}.
1729 */
1730 public InCallCameraManager getInCallCameraManager() {
1731 synchronized (this) {
linyuh183cb712017-12-27 17:02:37 -08001732 if (inCallCameraManager == null) {
1733 inCallCameraManager = new InCallCameraManager(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001734 }
1735
linyuh183cb712017-12-27 17:02:37 -08001736 return inCallCameraManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001737 }
1738 }
1739
1740 /**
1741 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1742 *
1743 * @param orientation The screen orientation of the device (one of: {@link
1744 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1745 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1746 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1747 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1748 */
1749 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001750 LogUtil.d(
1751 "InCallPresenter.onDeviceOrientationChange",
1752 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001753
linyuh183cb712017-12-27 17:02:37 -08001754 if (callList != null) {
1755 callList.notifyCallsOfDeviceRotation(orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001756 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001757 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001758 }
1759
1760 // Notify listeners of device orientation changed.
linyuh183cb712017-12-27 17:02:37 -08001761 for (InCallOrientationListener listener : orientationListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001762 listener.onDeviceOrientationChanged(orientation);
1763 }
1764 }
1765
1766 /**
1767 * Configures the in-call UI activity so it can change orientations or not. Enables the
1768 * orientation event listener if allowOrientationChange is true, disables it if false.
1769 *
1770 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1771 * landscape. {@code false} if the in-call UI should be locked in portrait.
1772 */
1773 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
linyuh183cb712017-12-27 17:02:37 -08001774 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001775 LogUtil.e(
1776 "InCallPresenter.setInCallAllowsOrientationChange",
1777 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001778 return;
1779 }
linyuh183cb712017-12-27 17:02:37 -08001780 inCallActivity.setAllowOrientationChange(allowOrientationChange);
Eric Erfanianccca3152017-02-22 16:32:36 -08001781 }
1782
1783 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001784 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
linyuh183cb712017-12-27 17:02:37 -08001785 screenTimeoutEnabled = enable;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001786 applyScreenTimeout();
1787 }
1788
1789 private void applyScreenTimeout() {
linyuh183cb712017-12-27 17:02:37 -08001790 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001791 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001792 return;
1793 }
1794
linyuh183cb712017-12-27 17:02:37 -08001795 final Window window = inCallActivity.getWindow();
1796 if (screenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001797 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1798 } else {
1799 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1800 }
1801 }
1802
1803 /**
1804 * Hides or shows the conference manager fragment.
1805 *
1806 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1807 * be hidden.
1808 */
1809 public void showConferenceCallManager(boolean show) {
linyuh183cb712017-12-27 17:02:37 -08001810 if (inCallActivity != null) {
1811 inCallActivity.showConferenceFragment(show);
Eric Erfanianccca3152017-02-22 16:32:36 -08001812 }
linyuh183cb712017-12-27 17:02:37 -08001813 if (!show && manageConferenceActivity != null) {
1814 manageConferenceActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -08001815 }
1816 }
1817
1818 /**
1819 * Determines if the dialpad is visible.
1820 *
1821 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1822 */
1823 public boolean isDialpadVisible() {
linyuh183cb712017-12-27 17:02:37 -08001824 if (inCallActivity == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001825 return false;
1826 }
linyuh183cb712017-12-27 17:02:37 -08001827 return inCallActivity.isDialpadVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001828 }
1829
1830 public ThemeColorManager getThemeColorManager() {
linyuh183cb712017-12-27 17:02:37 -08001831 return themeColorManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001832 }
1833
wangqi8d662ca2017-10-26 11:27:19 -07001834 @VisibleForTesting
1835 public void setThemeColorManager(ThemeColorManager themeColorManager) {
linyuh183cb712017-12-27 17:02:37 -08001836 this.themeColorManager = themeColorManager;
wangqi8d662ca2017-10-26 11:27:19 -07001837 }
1838
Eric Erfanianccca3152017-02-22 16:32:36 -08001839 /** Called when the foreground call changes. */
1840 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh183cb712017-12-27 17:02:37 -08001841 themeColorManager.onForegroundCallChanged(context, newForegroundCall);
1842 if (inCallActivity != null) {
1843 inCallActivity.onForegroundCallChanged(newForegroundCall);
Eric Erfanianccca3152017-02-22 16:32:36 -08001844 }
1845 }
1846
1847 public InCallActivity getActivity() {
linyuh183cb712017-12-27 17:02:37 -08001848 return inCallActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -08001849 }
1850
1851 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1852 public void setActivity(InCallActivity inCallActivity) {
1853 if (inCallActivity == null) {
1854 throw new IllegalArgumentException("registerActivity cannot be called with null");
1855 }
linyuh183cb712017-12-27 17:02:37 -08001856 if (this.inCallActivity != null && this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001857 LogUtil.w(
1858 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001859 }
1860 updateActivity(inCallActivity);
1861 }
1862
1863 ExternalCallNotifier getExternalCallNotifier() {
linyuh183cb712017-12-27 17:02:37 -08001864 return externalCallNotifier;
Eric Erfanianccca3152017-02-22 16:32:36 -08001865 }
1866
1867 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001868 if (localVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001869 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001870 if (context != null) {
1871 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001872 }
linyuh183cb712017-12-27 17:02:37 -08001873 localVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001874 }
linyuh183cb712017-12-27 17:02:37 -08001875 return localVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001876 }
1877
1878 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001879 if (remoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001880 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001881 if (context != null) {
1882 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001883 }
linyuh183cb712017-12-27 17:02:37 -08001884 remoteVideoSurfaceTexture = VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001885 }
linyuh183cb712017-12-27 17:02:37 -08001886 return remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001887 }
1888
1889 void cleanupSurfaces() {
linyuh183cb712017-12-27 17:02:37 -08001890 if (remoteVideoSurfaceTexture != null) {
1891 remoteVideoSurfaceTexture.setDoneWithSurface();
1892 remoteVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001893 }
linyuh183cb712017-12-27 17:02:37 -08001894 if (localVideoSurfaceTexture != null) {
1895 localVideoSurfaceTexture.setDoneWithSurface();
1896 localVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001897 }
1898 }
1899
yueg77cb8e52017-10-27 16:42:51 -07001900 @Override
1901 public void onAudioStateChanged(CallAudioState audioState) {
linyuh183cb712017-12-27 17:02:37 -08001902 if (statusBarNotifier != null) {
1903 statusBarNotifier.updateNotification();
yueg77cb8e52017-10-27 16:42:51 -07001904 }
1905 }
1906
Eric Erfanianccca3152017-02-22 16:32:36 -08001907 /** All the main states of InCallActivity. */
1908 public enum InCallState {
1909 // InCall Screen is off and there are no calls
1910 NO_CALLS,
1911
1912 // Incoming-call screen is up
1913 INCOMING,
1914
1915 // In-call experience is showing
1916 INCALL,
1917
1918 // Waiting for user input before placing outgoing call
1919 WAITING_FOR_ACCOUNT,
1920
1921 // UI is starting up but no call has been initiated yet.
1922 // The UI is waiting for Telecom to respond.
1923 PENDING_OUTGOING,
1924
1925 // User is dialing out
1926 OUTGOING;
1927
1928 public boolean isIncoming() {
1929 return (this == INCOMING);
1930 }
1931
1932 public boolean isConnectingOrConnected() {
1933 return (this == INCOMING || this == OUTGOING || this == INCALL);
1934 }
1935 }
1936
1937 /** Interface implemented by classes that need to know about the InCall State. */
1938 public interface InCallStateListener {
1939
1940 // TODO: Enhance state to contain the call objects instead of passing CallList
1941 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1942 }
1943
1944 public interface IncomingCallListener {
1945
1946 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1947 }
1948
1949 public interface CanAddCallListener {
1950
1951 void onCanAddCallChanged(boolean canAddCall);
1952 }
1953
1954 public interface InCallDetailsListener {
1955
1956 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1957 }
1958
1959 public interface InCallOrientationListener {
1960
1961 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1962 }
1963
1964 /**
1965 * Interface implemented by classes that need to know about events which occur within the In-Call
1966 * UI. Used as a means of communicating between fragments that make up the UI.
1967 */
1968 public interface InCallEventListener {
1969
1970 void onFullscreenModeChanged(boolean isFullscreenMode);
1971 }
1972
1973 public interface InCallUiListener {
1974
1975 void onUiShowing(boolean showing);
1976 }
twyen8efb4952017-10-06 16:35:54 -07001977
1978 private class InCallUiLockImpl implements InCallUiLock {
1979 private final String tag;
1980
1981 private InCallUiLockImpl(String tag) {
1982 this.tag = tag;
1983 }
1984
1985 @MainThread
1986 @Override
1987 public void release() {
1988 Assert.isMainThread();
1989 releaseInCallUiLock(InCallUiLockImpl.this);
1990 }
1991
1992 @Override
1993 public String toString() {
1994 return "InCallUiLock[" + tag + "]";
1995 }
1996 }
1997
1998 @MainThread
1999 public InCallUiLock acquireInCallUiLock(String tag) {
2000 Assert.isMainThread();
2001 InCallUiLock lock = new InCallUiLockImpl(tag);
linyuh183cb712017-12-27 17:02:37 -08002002 inCallUiLocks.add(lock);
twyen8efb4952017-10-06 16:35:54 -07002003 return lock;
2004 }
2005
2006 @MainThread
2007 private void releaseInCallUiLock(InCallUiLock lock) {
2008 Assert.isMainThread();
2009 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
linyuh183cb712017-12-27 17:02:37 -08002010 inCallUiLocks.remove(lock);
2011 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002012 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
linyuh183cb712017-12-27 17:02:37 -08002013 if (inCallState == InCallState.NO_CALLS) {
twyen8efb4952017-10-06 16:35:54 -07002014 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
2015 attemptFinishActivity();
2016 attemptCleanup();
2017 }
2018 }
2019 }
2020
2021 @MainThread
2022 public boolean isInCallUiLocked() {
2023 Assert.isMainThread();
linyuh183cb712017-12-27 17:02:37 -08002024 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002025 return false;
2026 }
linyuh183cb712017-12-27 17:02:37 -08002027 for (InCallUiLock lock : inCallUiLocks) {
twyen8efb4952017-10-06 16:35:54 -07002028 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
2029 }
2030 return true;
2031 }
2032
linyuh183cb712017-12-27 17:02:37 -08002033 private final Set<InCallUiLock> inCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08002034}