blob: 670af9d5478e32ed7983dea2de6dcca925e51e9b [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
yueg8e544862018-04-06 14:34:59 -0700276 private boolean audioRouteSetForBubbleMode;
yueg32d9ae72018-04-05 17:35:56 -0700277
Eric Erfanian10b34a52017-05-04 08:23:17 -0700278 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800279 @VisibleForTesting
280 InCallPresenter() {}
281
282 public static synchronized InCallPresenter getInstance() {
linyuh183cb712017-12-27 17:02:37 -0800283 if (inCallPresenter == null) {
wangqic8cf79e2017-10-17 09:21:00 -0700284 Trace.beginSection("InCallPresenter.Constructor");
linyuh183cb712017-12-27 17:02:37 -0800285 inCallPresenter = new InCallPresenter();
wangqic8cf79e2017-10-17 09:21:00 -0700286 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800287 }
linyuh183cb712017-12-27 17:02:37 -0800288 return inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -0800289 }
290
Eric Erfanian10b34a52017-05-04 08:23:17 -0700291 @VisibleForTesting
292 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
linyuh183cb712017-12-27 17:02:37 -0800293 InCallPresenter.inCallPresenter = inCallPresenter;
Eric Erfanian10b34a52017-05-04 08:23:17 -0700294 }
295
Eric Erfanianccca3152017-02-22 16:32:36 -0800296 /**
297 * Determines whether or not a call has no valid phone accounts that can be used to make the call
298 * with. Emergency calls do not require a phone account.
299 *
300 * @param call to check accounts for.
301 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
302 * call contains a phone account that could be used to initiate it with, or is an emergency
303 * call.
304 */
305 public static boolean isCallWithNoValidAccounts(DialerCall call) {
306 if (call != null && !call.isEmergencyCall()) {
307 Bundle extras = call.getIntentExtras();
308
309 if (extras == null) {
310 extras = EMPTY_EXTRAS;
311 }
312
313 final List<PhoneAccountHandle> phoneAccountHandles =
314 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
315
316 if ((call.getAccountHandle() == null
317 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700318 LogUtil.i(
319 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800320 return true;
321 }
322 }
323 return false;
324 }
325
326 public InCallState getInCallState() {
linyuh183cb712017-12-27 17:02:37 -0800327 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800328 }
329
330 public CallList getCallList() {
linyuh183cb712017-12-27 17:02:37 -0800331 return callList;
Eric Erfanianccca3152017-02-22 16:32:36 -0800332 }
333
334 public void setUp(
335 @NonNull Context context,
336 CallList callList,
337 ExternalCallList externalCallList,
338 StatusBarNotifier statusBarNotifier,
339 ExternalCallNotifier externalCallNotifier,
340 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700341 ProximitySensor proximitySensor,
erfaniand05d8992018-03-20 19:42:26 -0700342 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler,
yueg4613e8f2018-04-23 13:35:33 -0700343 @NonNull SpeakEasyCallManager speakEasyCallManager) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700344 Trace.beginSection("InCallPresenter.setUp");
linyuh183cb712017-12-27 17:02:37 -0800345 if (serviceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700346 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
linyuh183cb712017-12-27 17:02:37 -0800347 if (context != this.context || callList != this.callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800348 throw new IllegalStateException();
349 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700350 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800351 return;
352 }
353
354 Objects.requireNonNull(context);
linyuh183cb712017-12-27 17:02:37 -0800355 this.context = context;
Eric Erfanianccca3152017-02-22 16:32:36 -0800356
linyuh183cb712017-12-27 17:02:37 -0800357 this.contactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800358
linyuh183cb712017-12-27 17:02:37 -0800359 this.statusBarNotifier = statusBarNotifier;
360 this.externalCallNotifier = externalCallNotifier;
361 addListener(this.statusBarNotifier);
362 EnrichedCallComponent.get(this.context)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700363 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -0800364 .registerStateChangedListener(this.statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800365
linyuh183cb712017-12-27 17:02:37 -0800366 this.proximitySensor = proximitySensor;
367 addListener(this.proximitySensor);
Eric Erfanianccca3152017-02-22 16:32:36 -0800368
linyuh183cb712017-12-27 17:02:37 -0800369 if (themeColorManager == null) {
370 themeColorManager =
371 new ThemeColorManager(new InCallUIMaterialColorMapUtils(this.context.getResources()));
wangqi8d662ca2017-10-26 11:27:19 -0700372 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800373
linyuh183cb712017-12-27 17:02:37 -0800374 this.callList = callList;
375 this.externalCallList = externalCallList;
376 externalCallList.addExternalCallListener(this.externalCallNotifier);
377 externalCallList.addExternalCallListener(externalCallListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800378
379 // This only gets called by the service so this is okay.
linyuh183cb712017-12-27 17:02:37 -0800380 serviceConnected = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800381
382 // The final thing we do in this set up is add ourselves as a listener to CallList. This
383 // will kick off an update and the whole process can start.
linyuh183cb712017-12-27 17:02:37 -0800384 this.callList.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800385
386 // Create spam call list listener and add it to the list of listeners
linyuh183cb712017-12-27 17:02:37 -0800387 spamCallListListener =
zachh6a4cebd2017-10-24 17:10:06 -0700388 new SpamCallListListener(
389 context, DialerExecutorComponent.get(context).dialerExecutorFactory());
linyuh183cb712017-12-27 17:02:37 -0800390 this.callList.addListener(spamCallListListener);
twyen27c37182018-04-24 11:48:12 -0700391 activeCallsListener = new ActiveCallsCallListListener(context);
392 this.callList.addListener(activeCallsListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800393
394 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800395
linyuh183cb712017-12-27 17:02:37 -0800396 filteredQueryHandler = filteredNumberQueryHandler;
erfaniand05d8992018-03-20 19:42:26 -0700397 this.speakEasyCallManager = speakEasyCallManager;
linyuh183cb712017-12-27 17:02:37 -0800398 this.context
Eric Erfanianccca3152017-02-22 16:32:36 -0800399 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800400 .listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800401
yueg77cb8e52017-10-27 16:42:51 -0700402 AudioModeProvider.getInstance().addListener(this);
403
roldenburg42099012018-04-09 14:47:15 -0700404 // Add listener to notify Telephony process when the incoming call screen is started or
405 // finished. This is for hiding USSD dialog because the incoming call screen should have
406 // higher precedence over this dialog.
407 MotorolaInCallUiNotifier motorolaInCallUiNotifier = new MotorolaInCallUiNotifier(context);
408 addInCallUiListener(motorolaInCallUiNotifier);
409 addListener(motorolaInCallUiNotifier);
yueg7f5acbe2018-01-10 13:50:29 -0800410
Eric Erfanian2ca43182017-08-31 06:57:16 -0700411 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
412 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800413 }
414
415 /**
yueg32d9ae72018-04-05 17:35:56 -0700416 * Return whether we should start call in bubble mode and not show InCallActivity. The call mode
417 * should be set in CallConfiguration in EXTRA_OUTGOING_CALL_EXTRAS when starting a call intent.
418 */
yueg4613e8f2018-04-23 13:35:33 -0700419 private boolean shouldStartInBubbleMode() {
yueg32d9ae72018-04-05 17:35:56 -0700420 if (!ReturnToCallController.isEnabled(context)) {
421 return false;
422 }
yueg4613e8f2018-04-23 13:35:33 -0700423
424 // We only start in Bubble mode for outgoing call
425 DialerCall dialerCall = callList.getPendingOutgoingCall();
426 if (dialerCall == null) {
427 dialerCall = callList.getOutgoingCall();
428 }
429 if (dialerCall == null) {
yueg32d9ae72018-04-05 17:35:56 -0700430 return false;
431 }
yueg4613e8f2018-04-23 13:35:33 -0700432
433 Bundle extras = dialerCall.getIntentExtras();
yuegb493c3f2018-04-27 14:00:12 -0700434 boolean result = shouldStartInBubbleModeWithExtras(extras);
435 if (result) {
436 Logger.get(context)
437 .logCallImpression(
438 DialerImpression.Type.START_CALL_IN_BUBBLE_MODE,
439 dialerCall.getUniqueCallId(),
440 dialerCall.getTimeAddedMs());
441 }
442 return result;
yueg4613e8f2018-04-23 13:35:33 -0700443 }
444
445 private boolean shouldStartInBubbleModeWithExtras(Bundle outgoingExtras) {
446 if (!ReturnToCallController.isEnabled(context)) {
447 return false;
448 }
449
450 if (outgoingExtras == null) {
451 return false;
452 }
453 byte[] callConfigurationByteArray = outgoingExtras.getByteArray(CALL_CONFIGURATION_EXTRA);
yueg32d9ae72018-04-05 17:35:56 -0700454 if (callConfigurationByteArray == null) {
455 return false;
456 }
457 try {
458 CallConfiguration callConfiguration = CallConfiguration.parseFrom(callConfigurationByteArray);
459 LogUtil.i(
460 "InCallPresenter.shouldStartInBubbleMode",
461 "call mode: " + callConfiguration.getCallMode());
462 return callConfiguration.getCallMode() == Mode.BUBBLE;
463 } catch (InvalidProtocolBufferException e) {
464 return false;
465 }
466 }
467
468 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800469 * Called when the telephony service has disconnected from us. This will happen when there are no
470 * more active calls. However, we may still want to continue showing the UI for certain cases like
471 * showing "Call Ended". What we really want is to wait for the activity and the service to both
472 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
473 * secondary method that performs the aforementioned logic.
474 */
475 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700476 LogUtil.d("InCallPresenter.tearDown", "tearDown");
linyuh183cb712017-12-27 17:02:37 -0800477 callList.clearOnDisconnect();
Eric Erfanianccca3152017-02-22 16:32:36 -0800478
linyuh183cb712017-12-27 17:02:37 -0800479 serviceConnected = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800480
linyuh183cb712017-12-27 17:02:37 -0800481 context
Eric Erfanianccca3152017-02-22 16:32:36 -0800482 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800483 .listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800484
485 attemptCleanup();
486 VideoPauseController.getInstance().tearDown();
yueg77cb8e52017-10-27 16:42:51 -0700487 AudioModeProvider.getInstance().removeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800488 }
489
490 private void attemptFinishActivity() {
linyuh183cb712017-12-27 17:02:37 -0800491 screenTimeoutEnabled = true;
492 final boolean doFinish = (inCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700493 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800494 if (doFinish) {
linyuh183cb712017-12-27 17:02:37 -0800495 inCallActivity.setExcludeFromRecents(true);
496 inCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800497 }
498 }
499
500 /**
501 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
502 * for more insight on the tear-down process.
503 */
504 public void unsetActivity(InCallActivity inCallActivity) {
505 if (inCallActivity == null) {
506 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
507 }
linyuh183cb712017-12-27 17:02:37 -0800508 if (this.inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700509 LogUtil.i(
510 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800511 return;
512 }
linyuh183cb712017-12-27 17:02:37 -0800513 if (this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700514 LogUtil.w(
515 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800516 "Second instance of InCallActivity is trying to unregister when another"
517 + " instance is active. Ignoring.");
518 return;
519 }
520 updateActivity(null);
521 }
522
523 /**
524 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
525 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
526 */
527 private void updateActivity(InCallActivity inCallActivity) {
wangqi9982f0d2017-10-11 17:46:07 -0700528 Trace.beginSection("InCallPresenter.updateActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800529 boolean updateListeners = false;
530 boolean doAttemptCleanup = false;
531
532 if (inCallActivity != null) {
linyuh183cb712017-12-27 17:02:37 -0800533 if (this.inCallActivity == null) {
534 context = inCallActivity.getApplicationContext();
Eric Erfanianccca3152017-02-22 16:32:36 -0800535 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700536 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800537 } else {
538 // since setActivity is called onStart(), it can be called multiple times.
539 // This is fine and ignorable, but we do not want to update the world every time
540 // this happens (like going to/from background) so we do not set updateListeners.
541 }
542
linyuh183cb712017-12-27 17:02:37 -0800543 this.inCallActivity = inCallActivity;
544 this.inCallActivity.setExcludeFromRecents(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800545
546 // By the time the UI finally comes up, the call may already be disconnected.
547 // If that's the case, we may need to show an error dialog.
linyuh183cb712017-12-27 17:02:37 -0800548 if (callList != null && callList.getDisconnectedCall() != null) {
549 showDialogOrToastForDisconnectedCall(callList.getDisconnectedCall());
Eric Erfanianccca3152017-02-22 16:32:36 -0800550 }
551
552 // When the UI comes up, we need to first check the in-call state.
553 // If we are showing NO_CALLS, that means that a call probably connected and
554 // then immediately disconnected before the UI was able to come up.
555 // If we dont have any calls, start tearing down the UI instead.
556 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
557 // it has been set.
linyuh183cb712017-12-27 17:02:37 -0800558 if (inCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700559 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800560 attemptFinishActivity();
wangqi9982f0d2017-10-11 17:46:07 -0700561 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800562 return;
563 }
564 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700565 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800566 updateListeners = true;
linyuh183cb712017-12-27 17:02:37 -0800567 this.inCallActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800568
569 // We attempt cleanup for the destroy case but only after we recalculate the state
570 // to see if we need to come back up or stay shut down. This is why we do the
571 // cleanup after the call to onCallListChange() instead of directly here.
572 doAttemptCleanup = true;
573 }
574
575 // Messages can come from the telephony layer while the activity is coming up
576 // and while the activity is going down. So in both cases we need to recalculate what
577 // state we should be in after they complete.
578 // Examples: (1) A new incoming call could come in and then get disconnected before
579 // the activity is created.
580 // (2) All calls could disconnect and then get a new incoming call before the
581 // activity is destroyed.
582 //
Eric Erfanian938468d2017-10-24 14:05:52 -0700583 // a bug - We previously had a check for mServiceConnected here as well, but there are
Eric Erfanianccca3152017-02-22 16:32:36 -0800584 // cases where we need to recalculate the current state even if the service in not
585 // connected. In particular the case where startOrFinish() is called while the app is
586 // already finish()ing. In that case, we skip updating the state with the knowledge that
587 // we will check again once the activity has finished. That means we have to recalculate the
588 // state here even if the service is disconnected since we may not have finished a state
589 // transition while finish()ing.
590 if (updateListeners) {
linyuh183cb712017-12-27 17:02:37 -0800591 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800592 }
593
594 if (doAttemptCleanup) {
595 attemptCleanup();
596 }
wangqi9982f0d2017-10-11 17:46:07 -0700597 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800598 }
599
erfanian12243de2018-04-17 12:17:08 -0700600 public SpeakEasyCallManager getSpeakEasyCallManager() {
601 return this.speakEasyCallManager;
602 }
603
Eric Erfanianccca3152017-02-22 16:32:36 -0800604 public void setManageConferenceActivity(
605 @Nullable ManageConferenceActivity manageConferenceActivity) {
linyuh183cb712017-12-27 17:02:37 -0800606 this.manageConferenceActivity = manageConferenceActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -0800607 }
608
609 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700610 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800611 bringToForeground(showDialpad);
612 }
613
614 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700615 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800616 LatencyReport latencyReport = new LatencyReport(call);
617 if (shouldAttemptBlocking(call)) {
618 maybeBlockCall(call, latencyReport);
619 } else {
620 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800621 externalCallList.onCallAdded(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800622 } else {
623 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800624 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800625 }
626 }
627
628 // Since a call has been added we are no longer waiting for Telecom to send us a call.
629 setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -0800630 call.registerCallback(callCallback);
zachh78e54ac2017-12-05 16:38:35 -0800631 // TODO(maxwelb): Return the future in recordPhoneLookupInfo and propagate.
linyuh183cb712017-12-27 17:02:37 -0800632 PhoneLookupHistoryRecorder.recordPhoneLookupInfo(context.getApplicationContext(), call);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700633 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800634 }
635
636 private boolean shouldAttemptBlocking(android.telecom.Call call) {
637 if (call.getState() != android.telecom.Call.STATE_RINGING) {
638 return false;
639 }
linyuh183cb712017-12-27 17:02:37 -0800640 if (!UserManagerCompat.isUserUnlocked(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700641 LogUtil.i(
642 "InCallPresenter.shouldAttemptBlocking",
643 "not attempting to block incoming call because user is locked");
644 return false;
645 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800646 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700647 LogUtil.i(
648 "InCallPresenter.shouldAttemptBlocking",
649 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800650 return false;
651 }
linyuh183cb712017-12-27 17:02:37 -0800652 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700653 LogUtil.i(
654 "InCallPresenter.shouldAttemptBlocking",
655 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800656 return false;
657 }
658 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
659 return false;
660 }
linyuh183cb712017-12-27 17:02:37 -0800661 if (FilteredNumberCompat.useNewFiltering(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700662 LogUtil.i(
663 "InCallPresenter.shouldAttemptBlocking",
664 "not attempting to block incoming call because framework blocking is in use");
665 return false;
666 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800667 return true;
668 }
669
670 /**
671 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
672 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
673 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
674 * call anyways.
675 */
676 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
linyuh183cb712017-12-27 17:02:37 -0800677 final String countryIso = GeoUtil.getCurrentCountryIso(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800678 final String number = TelecomCallUtil.getNumber(call);
679 final long timeAdded = System.currentTimeMillis();
680
681 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
682 // main UI thread. It is needed so we can change its value within different scopes, since
683 // that cannot be done with a final boolean.
684 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
685
686 final Handler handler = new Handler();
687
688 // Proceed if the query is slow; the call may still be blocked after the query returns.
689 final Runnable runnable =
690 new Runnable() {
691 @Override
692 public void run() {
693 hasTimedOut.set(true);
694 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800695 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800696 }
697 };
698 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
699
700 OnCheckBlockedListener onCheckBlockedListener =
701 new OnCheckBlockedListener() {
702 @Override
703 public void onCheckComplete(final Integer id) {
704 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700705 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800706 return;
707 }
708 if (!hasTimedOut.get()) {
709 handler.removeCallbacks(runnable);
710 }
711 if (id == null) {
712 if (!hasTimedOut.get()) {
713 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800714 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800715 }
716 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700717 LogUtil.d(
718 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800719 if (!hasTimedOut.get()) {
720 handler.removeCallbacks(runnable);
721
722 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800723 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800724 }
725 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700726 LogUtil.i(
727 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800728 call.reject(false, null);
linyuh183cb712017-12-27 17:02:37 -0800729 Logger.get(context).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
Eric Erfanianccca3152017-02-22 16:32:36 -0800730
731 /*
732 * If mContext is null, then the InCallPresenter was torn down before the
733 * block check had a chance to complete. The context is no longer valid, so
734 * don't attempt to remove the call log entry.
735 */
linyuh183cb712017-12-27 17:02:37 -0800736 if (context == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800737 return;
738 }
739 // Register observer to update the call log.
740 // BlockedNumberContentObserver will unregister after successful log or timeout.
741 BlockedNumberContentObserver contentObserver =
linyuh183cb712017-12-27 17:02:37 -0800742 new BlockedNumberContentObserver(context, new Handler(), number, timeAdded);
Eric Erfanianccca3152017-02-22 16:32:36 -0800743 contentObserver.register();
744 }
745 }
746 };
747
linyuh183cb712017-12-27 17:02:37 -0800748 filteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800749 }
750
751 public void onCallRemoved(android.telecom.Call call) {
752 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800753 externalCallList.onCallRemoved(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800754 } else {
linyuh183cb712017-12-27 17:02:37 -0800755 callList.onCallRemoved(context, call);
756 call.unregisterCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800757 }
758 }
759
760 public void onCanAddCallChanged(boolean canAddCall) {
linyuh183cb712017-12-27 17:02:37 -0800761 for (CanAddCallListener listener : canAddCallListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800762 listener.onCanAddCallChanged(canAddCall);
763 }
764 }
765
766 @Override
767 public void onWiFiToLteHandover(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -0700768 if (call.hasShownWiFiToLteHandoverToast()) {
769 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800770 }
yueg32d9ae72018-04-05 17:35:56 -0700771
772 Toast.makeText(context, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG)
773 .show();
774 call.setHasShownWiFiToLteHandoverToast();
Eric Erfanianccca3152017-02-22 16:32:36 -0800775 }
776
777 @Override
778 public void onHandoverToWifiFailed(DialerCall call) {
linyuh183cb712017-12-27 17:02:37 -0800779 if (inCallActivity != null) {
780 inCallActivity.showDialogOrToastForWifiHandoverFailure(call);
yueg32d9ae72018-04-05 17:35:56 -0700781 } else {
782 Toast.makeText(context, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT)
783 .show();
Eric Erfanianccca3152017-02-22 16:32:36 -0800784 }
785 }
786
Eric Erfanianc857f902017-05-15 14:05:33 -0700787 @Override
788 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
789 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
yuegb47528e2018-04-24 12:12:57 -0700790
791 if (!InternationalCallOnWifiDialogFragment.shouldShow(context)) {
792 LogUtil.i(
793 "InCallPresenter.onInternationalCallOnWifi",
794 "InternationalCallOnWifiDialogFragment.shouldShow returned false");
795 return;
796 }
797
linyuh183cb712017-12-27 17:02:37 -0800798 if (inCallActivity != null) {
799 inCallActivity.showDialogForInternationalCallOnWifi(call);
yuegb47528e2018-04-24 12:12:57 -0700800 } else {
801 Intent intent = new Intent(context, InternationalCallOnWifiDialogActivity.class);
802 // Prevent showing MainActivity with InternationalCallOnWifiDialogActivity on above
803 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
804 intent.putExtra(InternationalCallOnWifiDialogActivity.EXTRA_CALL_ID, call.getId());
805 context.startActivity(intent);
Eric Erfanianc857f902017-05-15 14:05:33 -0700806 }
807 }
808
Eric Erfanianccca3152017-02-22 16:32:36 -0800809 /**
810 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
811 * app based on the information it gets from CallList. Dispatches the in-call state to all
812 * listeners. Can trigger the creation or destruction of the UI based on the states that is
813 * calculates.
814 */
815 @Override
816 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700817 Trace.beginSection("InCallPresenter.onCallListChange");
linyuh183cb712017-12-27 17:02:37 -0800818 if (inCallActivity != null && inCallActivity.isInCallScreenAnimating()) {
819 awaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700820 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800821 return;
822 }
823 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700824 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800825 return;
826 }
827
linyuh183cb712017-12-27 17:02:37 -0800828 awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800829
830 InCallState newState = getPotentialStateFromCallList(callList);
linyuh183cb712017-12-27 17:02:37 -0800831 InCallState oldState = inCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700832 LogUtil.d(
833 "InCallPresenter.onCallListChange",
834 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700835
836 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
837 // incall activity for that call will still exist (even if it's not visible). In the case of
838 // an incoming call in that situation, just disconnect that "waiting for account" call and
839 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
Eric Erfanian938468d2017-10-24 14:05:52 -0700840 // a bug for more details.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700841 DialerCall waitingForAccountCall;
842 if (newState == InCallState.INCOMING
843 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
844 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700845 // The InCallActivity might be destroyed or not started yet at this point.
846 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800847 inCallActivity.dismissPendingDialogs();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700848 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700849 }
850
Eric Erfanianccca3152017-02-22 16:32:36 -0800851 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700852 LogUtil.d(
853 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800854
855 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700856 LogUtil.i(
857 "InCallPresenter.onCallListChange",
858 "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800859 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800860
yuegb9103042018-03-30 12:12:25 -0700861 // Foreground call changed
862 DialerCall primary = null;
863 if (newState == InCallState.INCOMING) {
864 primary = callList.getIncomingCall();
865 } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
866 primary = callList.getOutgoingCall();
867 if (primary == null) {
868 primary = callList.getPendingOutgoingCall();
869 }
870 } else if (newState == InCallState.INCALL) {
871 primary = getCallToDisplay(callList, null, false);
872 }
873 if (primary != null) {
874 onForegroundCallChanged(primary);
875 }
876
Eric Erfanianccca3152017-02-22 16:32:36 -0800877 // notify listeners of new state
linyuh183cb712017-12-27 17:02:37 -0800878 for (InCallStateListener listener : listeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700879 LogUtil.d(
880 "InCallPresenter.onCallListChange",
linyuh183cb712017-12-27 17:02:37 -0800881 "Notify " + listener + " of state " + inCallState.toString());
882 listener.onStateChange(oldState, inCallState, callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800883 }
884
885 if (isActivityStarted()) {
886 final boolean hasCall =
887 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
linyuh183cb712017-12-27 17:02:37 -0800888 inCallActivity.dismissKeyguard(hasCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800889 }
twyen27c37182018-04-24 11:48:12 -0700890
Eric Erfanian2ca43182017-08-31 06:57:16 -0700891 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800892 }
893
yuegb9103042018-03-30 12:12:25 -0700894 /**
895 * Get the highest priority call to display. Goes through the calls and chooses which to return
896 * based on priority of which type of call to display to the user. Callers can use the "ignore"
897 * feature to get the second best call by passing a previously found primary call as ignore.
898 *
899 * @param ignore A call to ignore if found.
900 */
901 static DialerCall getCallToDisplay(
902 CallList callList, DialerCall ignore, boolean skipDisconnected) {
903 // Active calls come second. An active call always gets precedent.
904 DialerCall retval = callList.getActiveCall();
905 if (retval != null && retval != ignore) {
906 return retval;
907 }
908
909 // Sometimes there is intemediate state that two calls are in active even one is about
910 // to be on hold.
911 retval = callList.getSecondActiveCall();
912 if (retval != null && retval != ignore) {
913 return retval;
914 }
915
916 // Disconnected calls get primary position if there are no active calls
917 // to let user know quickly what call has disconnected. Disconnected
918 // calls are very short lived.
919 if (!skipDisconnected) {
920 retval = callList.getDisconnectingCall();
921 if (retval != null && retval != ignore) {
922 return retval;
923 }
924 retval = callList.getDisconnectedCall();
925 if (retval != null && retval != ignore) {
926 return retval;
927 }
928 }
929
930 // Then we go to background call (calls on hold)
931 retval = callList.getBackgroundCall();
932 if (retval != null && retval != ignore) {
933 return retval;
934 }
935
936 // Lastly, we go to a second background call.
937 retval = callList.getSecondBackgroundCall();
938
939 return retval;
940 }
941
Eric Erfanianccca3152017-02-22 16:32:36 -0800942 /** Called when there is a new incoming call. */
943 @Override
944 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700945 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800946 InCallState newState = startOrFinishUi(InCallState.INCOMING);
linyuh183cb712017-12-27 17:02:37 -0800947 InCallState oldState = inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800948
Eric Erfanian2ca43182017-08-31 06:57:16 -0700949 LogUtil.i(
950 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800951 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800952
wangqicf61ca02017-08-31 15:32:55 -0700953 Trace.beginSection("listener.onIncomingCall");
linyuh183cb712017-12-27 17:02:37 -0800954 for (IncomingCallListener listener : incomingCallListeners) {
955 listener.onIncomingCall(oldState, inCallState, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800956 }
wangqicf61ca02017-08-31 15:32:55 -0700957 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800958
wangqicf61ca02017-08-31 15:32:55 -0700959 Trace.beginSection("onPrimaryCallStateChanged");
linyuh183cb712017-12-27 17:02:37 -0800960 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800961 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800962 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800963 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700964 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700965 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800966 }
967
968 @Override
969 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700970 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
linyuh183cb712017-12-27 17:02:37 -0800971 && inCallState == InCallPresenter.InCallState.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800972 LogUtil.i(
973 "InCallPresenter.onUpgradeToVideo",
974 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700975 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800976 }
977
linyuh183cb712017-12-27 17:02:37 -0800978 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800979 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800980 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800981 }
982 }
983
984 @Override
wangqibc28ea72018-04-02 16:23:00 -0700985 public void onUpgradeToRtt(DialerCall call, int rttRequestId) {
986 if (inCallActivity != null) {
987 inCallActivity.showDialogForRttRequest(call, rttRequestId);
988 }
989 }
990
991 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700992 public void onSessionModificationStateChange(DialerCall call) {
993 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800994 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
linyuh183cb712017-12-27 17:02:37 -0800995 if (proximitySensor == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800996 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
997 return;
998 }
linyuh183cb712017-12-27 17:02:37 -0800999 proximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001000 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
linyuh183cb712017-12-27 17:02:37 -08001001 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001002 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001003 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001004 }
1005 }
1006
1007 /**
1008 * Called when a call becomes disconnected. Called everytime an existing call changes from being
1009 * connected (incoming/outgoing/active) to disconnected.
1010 */
1011 @Override
1012 public void onDisconnect(DialerCall call) {
linyuh7b86f562017-11-16 11:24:09 -08001013 showDialogOrToastForDisconnectedCall(call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001014
1015 // We need to do the run the same code as onCallListChange.
linyuh183cb712017-12-27 17:02:37 -08001016 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001017
1018 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001019 inCallActivity.dismissKeyguard(false);
Eric Erfanianccca3152017-02-22 16:32:36 -08001020 }
1021
1022 if (call.isEmergencyCall()) {
linyuh183cb712017-12-27 17:02:37 -08001023 FilteredNumbersUtil.recordLastEmergencyCallTime(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001024 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001025
linyuh183cb712017-12-27 17:02:37 -08001026 if (!callList.hasLiveCall()
Eric Erfanianfc37b022017-03-21 10:11:17 -07001027 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -07001028 && !isSecretCode(call.getNumber())
wangqi9982f0d2017-10-11 17:46:07 -07001029 && !call.isVoiceMailNumber()) {
linyuh183cb712017-12-27 17:02:37 -08001030 PostCall.onCallDisconnected(context, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -08001031 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001032 }
1033
Eric Erfanian10b34a52017-05-04 08:23:17 -07001034 private boolean isSecretCode(@Nullable String number) {
1035 return number != null
1036 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
1037 }
1038
Eric Erfanianccca3152017-02-22 16:32:36 -08001039 /** Given the call list, return the state in which the in-call screen should be. */
1040 public InCallState getPotentialStateFromCallList(CallList callList) {
1041
1042 InCallState newState = InCallState.NO_CALLS;
1043
1044 if (callList == null) {
1045 return newState;
1046 }
1047 if (callList.getIncomingCall() != null) {
1048 newState = InCallState.INCOMING;
1049 } else if (callList.getWaitingForAccountCall() != null) {
1050 newState = InCallState.WAITING_FOR_ACCOUNT;
1051 } else if (callList.getPendingOutgoingCall() != null) {
1052 newState = InCallState.PENDING_OUTGOING;
1053 } else if (callList.getOutgoingCall() != null) {
1054 newState = InCallState.OUTGOING;
1055 } else if (callList.getActiveCall() != null
1056 || callList.getBackgroundCall() != null
1057 || callList.getDisconnectedCall() != null
1058 || callList.getDisconnectingCall() != null) {
1059 newState = InCallState.INCALL;
1060 }
1061
1062 if (newState == InCallState.NO_CALLS) {
linyuh183cb712017-12-27 17:02:37 -08001063 if (boundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001064 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001065 }
1066 }
1067
1068 return newState;
1069 }
1070
1071 public boolean isBoundAndWaitingForOutgoingCall() {
linyuh183cb712017-12-27 17:02:37 -08001072 return boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -08001073 }
1074
1075 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001076 LogUtil.i(
1077 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
1078 "setBoundAndWaitingForOutgoingCall: " + isBound);
linyuh183cb712017-12-27 17:02:37 -08001079 boundAndWaitingForOutgoingCall = isBound;
1080 themeColorManager.setPendingPhoneAccountHandle(handle);
1081 if (isBound && inCallState == InCallState.NO_CALLS) {
1082 inCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001083 }
1084 }
1085
1086 public void onShrinkAnimationComplete() {
linyuh183cb712017-12-27 17:02:37 -08001087 if (awaitingCallListUpdate) {
1088 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001089 }
1090 }
1091
1092 public void addIncomingCallListener(IncomingCallListener listener) {
1093 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001094 incomingCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001095 }
1096
1097 public void removeIncomingCallListener(IncomingCallListener listener) {
1098 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001099 incomingCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001100 }
1101 }
1102
1103 public void addListener(InCallStateListener listener) {
1104 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001105 listeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001106 }
1107
1108 public void removeListener(InCallStateListener listener) {
1109 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001110 listeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001111 }
1112 }
1113
1114 public void addDetailsListener(InCallDetailsListener listener) {
1115 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001116 detailsListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001117 }
1118
1119 public void removeDetailsListener(InCallDetailsListener listener) {
1120 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001121 detailsListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001122 }
1123 }
1124
1125 public void addCanAddCallListener(CanAddCallListener listener) {
1126 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001127 canAddCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001128 }
1129
1130 public void removeCanAddCallListener(CanAddCallListener listener) {
1131 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001132 canAddCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001133 }
1134 }
1135
1136 public void addOrientationListener(InCallOrientationListener listener) {
1137 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001138 orientationListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001139 }
1140
1141 public void removeOrientationListener(InCallOrientationListener listener) {
1142 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001143 orientationListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001144 }
1145 }
1146
1147 public void addInCallEventListener(InCallEventListener listener) {
1148 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001149 inCallEventListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001150 }
1151
1152 public void removeInCallEventListener(InCallEventListener listener) {
1153 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001154 inCallEventListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001155 }
1156 }
1157
1158 public ProximitySensor getProximitySensor() {
linyuh183cb712017-12-27 17:02:37 -08001159 return proximitySensor;
Eric Erfanianccca3152017-02-22 16:32:36 -08001160 }
1161
1162 public PseudoScreenState getPseudoScreenState() {
linyuh183cb712017-12-27 17:02:37 -08001163 return pseudoScreenState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001164 }
1165
1166 /** Returns true if the incall app is the foreground application. */
1167 public boolean isShowingInCallUi() {
1168 if (!isActivityStarted()) {
1169 return false;
1170 }
linyuh183cb712017-12-27 17:02:37 -08001171 if (manageConferenceActivity != null && manageConferenceActivity.isVisible()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001172 return true;
1173 }
linyuh183cb712017-12-27 17:02:37 -08001174 return inCallActivity.isVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001175 }
1176
1177 /**
1178 * Returns true if the activity has been created and is running. Returns true as long as activity
1179 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
1180 * (not in foreground).
1181 */
1182 public boolean isActivityStarted() {
linyuh183cb712017-12-27 17:02:37 -08001183 return (inCallActivity != null
1184 && !inCallActivity.isDestroyed()
1185 && !inCallActivity.isFinishing());
Eric Erfanianccca3152017-02-22 16:32:36 -08001186 }
1187
1188 /**
1189 * Determines if the In-Call app is currently changing configuration.
1190 *
1191 * @return {@code true} if the In-Call app is changing configuration.
1192 */
1193 public boolean isChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001194 return isChangingConfigurations;
Eric Erfanianccca3152017-02-22 16:32:36 -08001195 }
1196
1197 /**
1198 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1199 * screen orientation).
1200 */
1201 /*package*/
1202 void updateIsChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001203 isChangingConfigurations = false;
1204 if (inCallActivity != null) {
1205 isChangingConfigurations = inCallActivity.isChangingConfigurations();
Eric Erfanianccca3152017-02-22 16:32:36 -08001206 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001207 LogUtil.v(
1208 "InCallPresenter.updateIsChangingConfigurations",
linyuh183cb712017-12-27 17:02:37 -08001209 "updateIsChangingConfigurations = " + isChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001210 }
1211
yueg10f6e822018-01-17 15:32:18 -08001212 /** Called when the activity goes in/out of the foreground. */
1213 public void onUiShowing(boolean showing) {
linyuh183cb712017-12-27 17:02:37 -08001214 if (proximitySensor != null) {
1215 proximitySensor.onInCallShowing(showing);
Eric Erfanianccca3152017-02-22 16:32:36 -08001216 }
1217
yueg092b21c2017-11-15 16:20:07 -08001218 if (!showing) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001219 updateIsChangingConfigurations();
1220 }
1221
linyuh183cb712017-12-27 17:02:37 -08001222 for (InCallUiListener listener : inCallUiListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001223 listener.onUiShowing(showing);
1224 }
1225
linyuh183cb712017-12-27 17:02:37 -08001226 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001227 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001228 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001229 }
1230 }
1231
Eric Erfanian2ca43182017-08-31 06:57:16 -07001232 public void refreshUi() {
linyuh183cb712017-12-27 17:02:37 -08001233 if (inCallActivity != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001234 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001235 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001236 }
1237 }
1238
Eric Erfanianccca3152017-02-22 16:32:36 -08001239 public void addInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001240 inCallUiListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001241 }
1242
1243 public boolean removeInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001244 return inCallUiListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001245 }
1246
1247 /*package*/
1248 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001249 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001250 notifyVideoPauseController(true);
Eric Erfanian2ca43182017-08-31 06:57:16 -07001251 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001252 }
1253
1254 /*package*/
1255 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001256 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001257 notifyVideoPauseController(false);
1258 }
1259
1260 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001261 LogUtil.d(
1262 "InCallPresenter.notifyVideoPauseController",
linyuh183cb712017-12-27 17:02:37 -08001263 "mIsChangingConfigurations=" + isChangingConfigurations);
1264 if (!isChangingConfigurations) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001265 VideoPauseController.getInstance().onUiShowing(showing);
1266 }
1267 }
1268
1269 /** Brings the app into the foreground if possible. */
1270 public void bringToForeground(boolean showDialpad) {
1271 // Before we bring the incall UI to the foreground, we check to see if:
1272 // 1. It is not currently in the foreground
1273 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1274 // be displayed)
1275 // If the activity hadn't actually been started previously, yet there are still calls
1276 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1277 // bring it up the UI regardless.
linyuh183cb712017-12-27 17:02:37 -08001278 if (!isShowingInCallUi() && inCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001279 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001280 }
1281 }
1282
1283 public void onPostDialCharWait(String callId, String chars) {
yuega489f512018-04-25 12:01:09 -07001284 // If not visible, inCallActivity is stopped. Starting from P, calling recreate() will destroy
1285 // the old activity instance and create a new instance immediately. Previously, the old activity
1286 // went through its lifecycle from create to destroy before creating a new instance.
1287 // So this case doesn't work now: make a call with char WAIT, leave in call UI, call gets
1288 // connected, and go back to in call UI to see the dialog.
1289 // So we should show dialog in an empty activity if inCallActivity is not visible. And it also
1290 // helps with background calling.
1291 if (isActivityStarted() && inCallActivity.isVisible()) {
linyuh183cb712017-12-27 17:02:37 -08001292 inCallActivity.showDialogForPostCharWait(callId, chars);
yuega489f512018-04-25 12:01:09 -07001293 } else {
1294 Intent intent = new Intent(context, PostCharDialogActivity.class);
1295 // Prevent showing MainActivity with PostCharDialogActivity on above
1296 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1297 intent.putExtra(PostCharDialogActivity.EXTRA_CALL_ID, callId);
1298 intent.putExtra(PostCharDialogActivity.EXTRA_POST_DIAL_STRING, chars);
1299 context.startActivity(intent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001300 }
1301 }
1302
1303 /**
1304 * Handles the green CALL key while in-call.
1305 *
1306 * @return true if we consumed the event.
1307 */
1308 public boolean handleCallKey() {
1309 LogUtil.v("InCallPresenter.handleCallKey", null);
1310
1311 // The green CALL button means either "Answer", "Unhold", or
1312 // "Swap calls", or can be a no-op, depending on the current state
1313 // of the Phone.
1314
1315 /** INCOMING CALL */
linyuh183cb712017-12-27 17:02:37 -08001316 final CallList calls = callList;
Eric Erfanianccca3152017-02-22 16:32:36 -08001317 final DialerCall incomingCall = calls.getIncomingCall();
1318 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1319
1320 // (1) Attempt to answer a call
1321 if (incomingCall != null) {
1322 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1323 return true;
1324 }
1325
1326 /** STATE_ACTIVE CALL */
1327 final DialerCall activeCall = calls.getActiveCall();
1328 if (activeCall != null) {
1329 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1330 // consolidate this logic.
1331 final boolean canMerge =
1332 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1333 final boolean canSwap =
1334 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1335
Eric Erfanian2ca43182017-08-31 06:57:16 -07001336 LogUtil.v(
1337 "InCallPresenter.handleCallKey",
1338 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001339
1340 // (2) Attempt actions on conference calls
1341 if (canMerge) {
1342 TelecomAdapter.getInstance().merge(activeCall.getId());
1343 return true;
1344 } else if (canSwap) {
1345 TelecomAdapter.getInstance().swap(activeCall.getId());
1346 return true;
1347 }
1348 }
1349
1350 /** BACKGROUND CALL */
1351 final DialerCall heldCall = calls.getBackgroundCall();
1352 if (heldCall != null) {
1353 // We have a hold call so presumeable it will always support HOLD...but
1354 // there is no harm in double checking.
1355 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1356
Eric Erfanian2ca43182017-08-31 06:57:16 -07001357 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001358
1359 // (4) unhold call
wangqibb94ca62018-04-27 14:34:04 -07001360 if (heldCall.getState() == DialerCallState.ONHOLD && canHold) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001361 heldCall.unhold();
1362 return true;
1363 }
1364 }
1365
1366 // Always consume hard keys
1367 return true;
1368 }
1369
Eric Erfanianccca3152017-02-22 16:32:36 -08001370 /** Clears the previous fullscreen state. */
1371 public void clearFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001372 isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001373 }
1374
1375 /**
1376 * Changes the fullscreen mode of the in-call UI.
1377 *
1378 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1379 * otherwise.
1380 */
1381 public void setFullScreen(boolean isFullScreen) {
1382 setFullScreen(isFullScreen, false /* force */);
1383 }
1384
1385 /**
1386 * Changes the fullscreen mode of the in-call UI.
1387 *
1388 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1389 * otherwise.
1390 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1391 */
1392 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001393 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001394
1395 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1396 if (isDialpadVisible()) {
1397 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001398 LogUtil.v(
1399 "InCallPresenter.setFullScreen",
1400 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001401 }
1402
linyuh183cb712017-12-27 17:02:37 -08001403 if (this.isFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001404 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001405 return;
1406 }
linyuh183cb712017-12-27 17:02:37 -08001407 this.isFullScreen = isFullScreen;
1408 notifyFullscreenModeChange(this.isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001409 }
1410
1411 /**
1412 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1413 * otherwise.
1414 */
1415 public boolean isFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001416 return isFullScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001417 }
1418
1419 /**
1420 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1421 *
1422 * @param isFullscreenMode {@code True} if entering full screen mode.
1423 */
1424 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001425 for (InCallEventListener listener : inCallEventListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001426 listener.onFullscreenModeChanged(isFullscreenMode);
1427 }
1428 }
1429
linyuh7b86f562017-11-16 11:24:09 -08001430 /** Instruct the in-call activity to show an error dialog or toast for a disconnected call. */
1431 private void showDialogOrToastForDisconnectedCall(DialerCall call) {
wangqibb94ca62018-04-27 14:34:04 -07001432 if (call.getState() != DialerCallState.DISCONNECTED) {
linyuh7b86f562017-11-16 11:24:09 -08001433 return;
Eric Erfanianccca3152017-02-22 16:32:36 -08001434 }
linyuh7b86f562017-11-16 11:24:09 -08001435
1436 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1437 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1438 setDisconnectCauseForMissingAccounts(call);
1439 }
1440
yueg32d9ae72018-04-05 17:35:56 -07001441 if (isActivityStarted()) {
1442 inCallActivity.showDialogOrToastForDisconnectedCall(
1443 new DisconnectMessage(inCallActivity, call));
1444 } else {
1445 CharSequence message = new DisconnectMessage(context, call).toastMessage;
1446 if (message != null) {
1447 Toast.makeText(context, message, Toast.LENGTH_LONG).show();
1448 }
1449 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001450 }
1451
1452 /**
1453 * When the state of in-call changes, this is the first method to get called. It determines if the
1454 * UI needs to be started or finished depending on the new state and does it.
1455 */
1456 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001457 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001458 LogUtil.d(
linyuh183cb712017-12-27 17:02:37 -08001459 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + inCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001460
1461 // TODO: Consider a proper state machine implementation
1462
1463 // If the state isn't changing we have already done any starting/stopping of activities in
1464 // a previous pass...so lets cut out early
linyuh183cb712017-12-27 17:02:37 -08001465 if (newState == inCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001466 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001467 return newState;
1468 }
1469
1470 // A new Incoming call means that the user needs to be notified of the the call (since
1471 // it wasn't them who initiated it). We do this through full screen notifications and
1472 // happens indirectly through {@link StatusBarNotifier}.
1473 //
1474 // The process for incoming calls is as follows:
1475 //
1476 // 1) CallList - Announces existence of new INCOMING call
1477 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1478 // - should be set to INCOMING.
1479 // 3) InCallPresenter - This method is called to see if we need to start or finish
1480 // the app given the new state.
1481 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1482 // StatusBarNotifier explicitly to issue a FullScreen Notification
1483 // that will either start the InCallActivity or show the user a
1484 // top-level notification dialog if the user is in an immersive app.
1485 // That notification can also start the InCallActivity.
1486 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1487 // call InCallPresenter::setActivity() to let the presenter
1488 // know that start-up is complete.
1489 //
1490 // [ AND NOW YOU'RE IN THE CALL. voila! ]
Eric Erfanianccca3152017-02-22 16:32:36 -08001491
1492 // A dialog to show on top of the InCallUI to select a PhoneAccount
1493 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1494
1495 // A new outgoing call indicates that the user just now dialed a number and when that
1496 // happens we need to display the screen immediately or show an account picker dialog if
1497 // no default is set. However, if the main InCallUI is already visible, we do not want to
1498 // re-initiate the start-up animation, so we do not need to do anything here.
1499 //
1500 // It is also possible to go into an intermediate state where the call has been initiated
1501 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1502 // This pending outgoing state can also launch the call screen.
1503 //
1504 // This is different from the incoming call sequence because we do not need to shock the
1505 // user with a top-level notification. Just show the call UI normally.
1506 boolean callCardFragmentVisible =
linyuh183cb712017-12-27 17:02:37 -08001507 inCallActivity != null && inCallActivity.getCallCardFragmentVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001508 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1509 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1510
1511 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1512 // outgoing call process, so the UI should be brought up to show an error dialog.
1513 showCallUi |=
linyuh183cb712017-12-27 17:02:37 -08001514 (InCallState.PENDING_OUTGOING == inCallState
Eric Erfanianccca3152017-02-22 16:32:36 -08001515 && InCallState.INCALL == newState
1516 && !isShowingInCallUi());
1517
1518 // Another exception - InCallActivity is in charge of disconnecting a call with no
1519 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1520 // call so that:
1521 // 1) The call can be disconnected correctly
1522 // 2) The UI comes up and correctly displays the error dialog.
1523 // TODO: Remove these special case conditions by making InCallPresenter a true state
1524 // machine. Telecom should also be the component responsible for disconnecting a call
1525 // with no valid accounts.
1526 showCallUi |=
1527 InCallState.PENDING_OUTGOING == newState
1528 && mainUiNotVisible
linyuh183cb712017-12-27 17:02:37 -08001529 && isCallWithNoValidAccounts(callList.getPendingOutgoingCall());
Eric Erfanianccca3152017-02-22 16:32:36 -08001530
1531 // The only time that we have an instance of mInCallActivity and it isn't started is
1532 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1533 // the activity. When it is finally destroyed, we double check if we should bring it back
1534 // up so we aren't going to lose anything by avoiding a second startup here.
linyuh183cb712017-12-27 17:02:37 -08001535 boolean activityIsFinishing = inCallActivity != null && !isActivityStarted();
Eric Erfanianccca3152017-02-22 16:32:36 -08001536 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001537 LogUtil.i(
1538 "InCallPresenter.startOrFinishUi",
linyuh183cb712017-12-27 17:02:37 -08001539 "Undo the state change: " + newState + " -> " + inCallState);
wangqicf61ca02017-08-31 15:32:55 -07001540 Trace.endSection();
linyuh183cb712017-12-27 17:02:37 -08001541 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001542 }
1543
1544 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1545 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1546 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1547 // outgoing call after user ignore it by pressing home button.
1548 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1549 && !showCallUi
1550 && isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001551 inCallActivity.dismissPendingDialogs();
Eric Erfanianccca3152017-02-22 16:32:36 -08001552 }
1553
yueg4613e8f2018-04-23 13:35:33 -07001554 if ((showCallUi || showAccountPicker) && !shouldStartInBubbleMode()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001555 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001556 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001557 } else if (newState == InCallState.NO_CALLS) {
1558 // The new state is the no calls state. Tear everything down.
yueg8e544862018-04-06 14:34:59 -07001559 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001560 attemptFinishActivity();
1561 attemptCleanup();
1562 }
1563
wangqicf61ca02017-08-31 15:32:55 -07001564 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001565 return newState;
1566 }
1567
1568 /**
1569 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1570 * or PhoneAccounts to select from.
1571 */
1572 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1573
1574 Bundle extras = call.getIntentExtras();
1575 // Initialize the extras bundle to avoid NPE
1576 if (extras == null) {
1577 extras = new Bundle();
1578 }
1579
1580 final List<PhoneAccountHandle> phoneAccountHandles =
1581 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1582
1583 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1584 String scheme = call.getHandle().getScheme();
1585 final String errorMsg =
1586 PhoneAccount.SCHEME_TEL.equals(scheme)
linyuh183cb712017-12-27 17:02:37 -08001587 ? context.getString(R.string.callFailed_simError)
1588 : context.getString(R.string.incall_error_supp_service_unknown);
Eric Erfanianccca3152017-02-22 16:32:36 -08001589 DisconnectCause disconnectCause =
1590 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1591 call.setDisconnectCause(disconnectCause);
1592 }
1593 }
1594
Eric Erfanianccca3152017-02-22 16:32:36 -08001595 /**
1596 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1597 * Calling classes should use this as an indication whether to interact with the
1598 * InCallPresenter or not.
1599 */
1600 public boolean isReadyForTearDown() {
linyuh183cb712017-12-27 17:02:37 -08001601 return inCallActivity == null && !serviceConnected && inCallState == InCallState.NO_CALLS;
Eric Erfanianccca3152017-02-22 16:32:36 -08001602 }
1603
1604 /**
1605 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1606 */
1607 private void attemptCleanup() {
1608 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001609 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001610
1611 cleanupSurfaces();
1612
linyuh183cb712017-12-27 17:02:37 -08001613 isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001614
yueg8e544862018-04-06 14:34:59 -07001615 audioRouteSetForBubbleMode = false;
yueg32d9ae72018-04-05 17:35:56 -07001616
Eric Erfanianccca3152017-02-22 16:32:36 -08001617 // blow away stale contact info so that we get fresh data on
1618 // the next set of calls
linyuh183cb712017-12-27 17:02:37 -08001619 if (contactInfoCache != null) {
1620 contactInfoCache.clearCache();
Eric Erfanianccca3152017-02-22 16:32:36 -08001621 }
linyuh183cb712017-12-27 17:02:37 -08001622 contactInfoCache = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001623
linyuh183cb712017-12-27 17:02:37 -08001624 if (proximitySensor != null) {
1625 removeListener(proximitySensor);
1626 proximitySensor.tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -08001627 }
linyuh183cb712017-12-27 17:02:37 -08001628 proximitySensor = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001629
linyuh183cb712017-12-27 17:02:37 -08001630 if (statusBarNotifier != null) {
1631 removeListener(statusBarNotifier);
1632 EnrichedCallComponent.get(context)
Eric Erfaniand8046e52017-04-06 09:41:50 -07001633 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -08001634 .unregisterStateChangedListener(statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001635 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001636
linyuh183cb712017-12-27 17:02:37 -08001637 if (externalCallNotifier != null && externalCallList != null) {
1638 externalCallList.removeExternalCallListener(externalCallNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001639 }
linyuh183cb712017-12-27 17:02:37 -08001640 statusBarNotifier = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001641
linyuh183cb712017-12-27 17:02:37 -08001642 if (callList != null) {
1643 callList.removeListener(this);
1644 callList.removeListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001645 }
linyuh183cb712017-12-27 17:02:37 -08001646 callList = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001647
linyuh183cb712017-12-27 17:02:37 -08001648 context = null;
1649 inCallActivity = null;
1650 manageConferenceActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001651
linyuh183cb712017-12-27 17:02:37 -08001652 listeners.clear();
1653 incomingCallListeners.clear();
1654 detailsListeners.clear();
1655 canAddCallListeners.clear();
1656 orientationListeners.clear();
1657 inCallEventListeners.clear();
1658 inCallUiListeners.clear();
1659 if (!inCallUiLocks.isEmpty()) {
1660 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + inCallUiLocks);
1661 inCallUiLocks.clear();
twyen8efb4952017-10-06 16:35:54 -07001662 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001663 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001664 }
1665 }
1666
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001667 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001668 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
linyuh183cb712017-12-27 17:02:37 -08001669 context.startActivity(
1670 InCallActivity.getIntent(context, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001671 }
1672
1673 public void onServiceBind() {
linyuh183cb712017-12-27 17:02:37 -08001674 serviceBound = true;
Eric Erfanianccca3152017-02-22 16:32:36 -08001675 }
1676
1677 public void onServiceUnbind() {
1678 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -08001679 serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001680 }
1681
1682 public boolean isServiceBound() {
linyuh183cb712017-12-27 17:02:37 -08001683 return serviceBound;
Eric Erfanianccca3152017-02-22 16:32:36 -08001684 }
1685
1686 public void maybeStartRevealAnimation(Intent intent) {
linyuh183cb712017-12-27 17:02:37 -08001687 if (intent == null || inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001688 return;
1689 }
1690 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1691 if (extras == null) {
1692 // Incoming call, just show the in-call UI directly.
1693 return;
1694 }
1695
1696 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1697 // Account selection dialog will show up so don't show the animation.
1698 return;
1699 }
1700
1701 final PhoneAccountHandle accountHandle =
1702 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1703 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001704
yueg32d9ae72018-04-05 17:35:56 -07001705 setBoundAndWaitingForOutgoingCall(true, accountHandle);
1706
yueg4613e8f2018-04-23 13:35:33 -07001707 if (shouldStartInBubbleModeWithExtras(extras)) {
yueg32d9ae72018-04-05 17:35:56 -07001708 LogUtil.i("InCallPresenter.maybeStartRevealAnimation", "shouldStartInBubbleMode");
1709 // Show bubble instead of in call UI
1710 return;
1711 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001712
1713 final Intent activityIntent =
linyuh183cb712017-12-27 17:02:37 -08001714 InCallActivity.getIntent(context, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001715 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
linyuh183cb712017-12-27 17:02:37 -08001716 context.startActivity(activityIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001717 }
1718
1719 /**
1720 * Retrieves the current in-call camera manager instance, creating if necessary.
1721 *
1722 * @return The {@link InCallCameraManager}.
1723 */
1724 public InCallCameraManager getInCallCameraManager() {
1725 synchronized (this) {
linyuh183cb712017-12-27 17:02:37 -08001726 if (inCallCameraManager == null) {
1727 inCallCameraManager = new InCallCameraManager(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001728 }
1729
linyuh183cb712017-12-27 17:02:37 -08001730 return inCallCameraManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001731 }
1732 }
1733
1734 /**
1735 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1736 *
1737 * @param orientation The screen orientation of the device (one of: {@link
1738 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1739 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1740 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1741 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1742 */
1743 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001744 LogUtil.d(
1745 "InCallPresenter.onDeviceOrientationChange",
1746 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001747
linyuh183cb712017-12-27 17:02:37 -08001748 if (callList != null) {
1749 callList.notifyCallsOfDeviceRotation(orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001750 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001751 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001752 }
1753
1754 // Notify listeners of device orientation changed.
linyuh183cb712017-12-27 17:02:37 -08001755 for (InCallOrientationListener listener : orientationListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001756 listener.onDeviceOrientationChanged(orientation);
1757 }
1758 }
1759
1760 /**
1761 * Configures the in-call UI activity so it can change orientations or not. Enables the
1762 * orientation event listener if allowOrientationChange is true, disables it if false.
1763 *
1764 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1765 * landscape. {@code false} if the in-call UI should be locked in portrait.
1766 */
1767 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
linyuh183cb712017-12-27 17:02:37 -08001768 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001769 LogUtil.e(
1770 "InCallPresenter.setInCallAllowsOrientationChange",
1771 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001772 return;
1773 }
linyuh183cb712017-12-27 17:02:37 -08001774 inCallActivity.setAllowOrientationChange(allowOrientationChange);
Eric Erfanianccca3152017-02-22 16:32:36 -08001775 }
1776
1777 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001778 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
linyuh183cb712017-12-27 17:02:37 -08001779 screenTimeoutEnabled = enable;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001780 applyScreenTimeout();
1781 }
1782
1783 private void applyScreenTimeout() {
linyuh183cb712017-12-27 17:02:37 -08001784 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001785 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001786 return;
1787 }
1788
linyuh183cb712017-12-27 17:02:37 -08001789 final Window window = inCallActivity.getWindow();
1790 if (screenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001791 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1792 } else {
1793 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1794 }
1795 }
1796
1797 /**
1798 * Hides or shows the conference manager fragment.
1799 *
1800 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1801 * be hidden.
1802 */
1803 public void showConferenceCallManager(boolean show) {
linyuh183cb712017-12-27 17:02:37 -08001804 if (inCallActivity != null) {
1805 inCallActivity.showConferenceFragment(show);
Eric Erfanianccca3152017-02-22 16:32:36 -08001806 }
linyuh183cb712017-12-27 17:02:37 -08001807 if (!show && manageConferenceActivity != null) {
1808 manageConferenceActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -08001809 }
1810 }
1811
1812 /**
1813 * Determines if the dialpad is visible.
1814 *
1815 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1816 */
1817 public boolean isDialpadVisible() {
linyuh183cb712017-12-27 17:02:37 -08001818 if (inCallActivity == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001819 return false;
1820 }
linyuh183cb712017-12-27 17:02:37 -08001821 return inCallActivity.isDialpadVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001822 }
1823
1824 public ThemeColorManager getThemeColorManager() {
linyuh183cb712017-12-27 17:02:37 -08001825 return themeColorManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001826 }
1827
wangqi8d662ca2017-10-26 11:27:19 -07001828 @VisibleForTesting
1829 public void setThemeColorManager(ThemeColorManager themeColorManager) {
linyuh183cb712017-12-27 17:02:37 -08001830 this.themeColorManager = themeColorManager;
wangqi8d662ca2017-10-26 11:27:19 -07001831 }
1832
Eric Erfanianccca3152017-02-22 16:32:36 -08001833 /** Called when the foreground call changes. */
1834 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh183cb712017-12-27 17:02:37 -08001835 themeColorManager.onForegroundCallChanged(context, newForegroundCall);
1836 if (inCallActivity != null) {
1837 inCallActivity.onForegroundCallChanged(newForegroundCall);
Eric Erfanianccca3152017-02-22 16:32:36 -08001838 }
1839 }
1840
1841 public InCallActivity getActivity() {
linyuh183cb712017-12-27 17:02:37 -08001842 return inCallActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -08001843 }
1844
1845 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1846 public void setActivity(InCallActivity inCallActivity) {
1847 if (inCallActivity == null) {
1848 throw new IllegalArgumentException("registerActivity cannot be called with null");
1849 }
linyuh183cb712017-12-27 17:02:37 -08001850 if (this.inCallActivity != null && this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001851 LogUtil.w(
1852 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001853 }
1854 updateActivity(inCallActivity);
1855 }
1856
1857 ExternalCallNotifier getExternalCallNotifier() {
linyuh183cb712017-12-27 17:02:37 -08001858 return externalCallNotifier;
Eric Erfanianccca3152017-02-22 16:32:36 -08001859 }
1860
1861 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001862 if (localVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001863 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001864 if (context != null) {
1865 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001866 }
linyuh183cb712017-12-27 17:02:37 -08001867 localVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001868 }
linyuh183cb712017-12-27 17:02:37 -08001869 return localVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001870 }
1871
1872 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001873 if (remoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001874 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001875 if (context != null) {
1876 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001877 }
linyuh183cb712017-12-27 17:02:37 -08001878 remoteVideoSurfaceTexture = VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001879 }
linyuh183cb712017-12-27 17:02:37 -08001880 return remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001881 }
1882
1883 void cleanupSurfaces() {
linyuh183cb712017-12-27 17:02:37 -08001884 if (remoteVideoSurfaceTexture != null) {
1885 remoteVideoSurfaceTexture.setDoneWithSurface();
1886 remoteVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001887 }
linyuh183cb712017-12-27 17:02:37 -08001888 if (localVideoSurfaceTexture != null) {
1889 localVideoSurfaceTexture.setDoneWithSurface();
1890 localVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001891 }
1892 }
1893
yueg77cb8e52017-10-27 16:42:51 -07001894 @Override
1895 public void onAudioStateChanged(CallAudioState audioState) {
yueg8e544862018-04-06 14:34:59 -07001896 // Set sensible audio route for bubble mode when we get real audio state for the first time
1897 // During the first time this function is called, supportedRouteMask is set to
yueg4613e8f2018-04-23 13:35:33 -07001898 // SUPPORTED_AUDIO_ROUTE_ALL, but it's OK since shouldStartInBubbleMode() is false at that time
1899 // (callList not updated yet).
1900 if (!audioRouteSetForBubbleMode && shouldStartInBubbleMode()) {
yueg8e544862018-04-06 14:34:59 -07001901 setAudioRouteForBubbleMode(audioState);
1902 audioRouteSetForBubbleMode = true;
1903 }
1904
linyuh183cb712017-12-27 17:02:37 -08001905 if (statusBarNotifier != null) {
1906 statusBarNotifier.updateNotification();
yueg77cb8e52017-10-27 16:42:51 -07001907 }
1908 }
1909
yueg8e544862018-04-06 14:34:59 -07001910 /**
1911 * Set audio route to make audio sensible. According to availability, set audio route to Bluetooth
1912 * or wired headset or speaker.
1913 */
1914 private void setAudioRouteForBubbleMode(CallAudioState audioState) {
1915 if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
1916 == CallAudioState.ROUTE_BLUETOOTH) {
1917 // Use Bluetooth if available
1918 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
1919 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "bluetooth");
1920 } else if ((audioState.getSupportedRouteMask() & CallAudioState.ROUTE_WIRED_HEADSET)
1921 == CallAudioState.ROUTE_WIRED_HEADSET) {
1922 // Use wired headset if available
1923 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_WIRED_HEADSET);
1924 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "wired headset");
1925 } else {
1926 // Use speaker
1927 TelecomAdapter.getInstance().setAudioRoute(CallAudioState.ROUTE_SPEAKER);
1928 LogUtil.i("InCallPrenter.setAudioRouteForBubbleMode", "speaker");
1929 }
1930 }
1931
Eric Erfanianccca3152017-02-22 16:32:36 -08001932 /** All the main states of InCallActivity. */
1933 public enum InCallState {
1934 // InCall Screen is off and there are no calls
1935 NO_CALLS,
1936
1937 // Incoming-call screen is up
1938 INCOMING,
1939
1940 // In-call experience is showing
1941 INCALL,
1942
1943 // Waiting for user input before placing outgoing call
1944 WAITING_FOR_ACCOUNT,
1945
1946 // UI is starting up but no call has been initiated yet.
1947 // The UI is waiting for Telecom to respond.
1948 PENDING_OUTGOING,
1949
1950 // User is dialing out
1951 OUTGOING;
1952
1953 public boolean isIncoming() {
1954 return (this == INCOMING);
1955 }
1956
1957 public boolean isConnectingOrConnected() {
1958 return (this == INCOMING || this == OUTGOING || this == INCALL);
1959 }
1960 }
1961
1962 /** Interface implemented by classes that need to know about the InCall State. */
1963 public interface InCallStateListener {
1964
1965 // TODO: Enhance state to contain the call objects instead of passing CallList
1966 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1967 }
1968
1969 public interface IncomingCallListener {
1970
1971 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1972 }
1973
1974 public interface CanAddCallListener {
1975
1976 void onCanAddCallChanged(boolean canAddCall);
1977 }
1978
1979 public interface InCallDetailsListener {
1980
1981 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1982 }
1983
1984 public interface InCallOrientationListener {
1985
1986 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1987 }
1988
1989 /**
1990 * Interface implemented by classes that need to know about events which occur within the In-Call
1991 * UI. Used as a means of communicating between fragments that make up the UI.
1992 */
1993 public interface InCallEventListener {
1994
1995 void onFullscreenModeChanged(boolean isFullscreenMode);
1996 }
1997
1998 public interface InCallUiListener {
1999
2000 void onUiShowing(boolean showing);
2001 }
twyen8efb4952017-10-06 16:35:54 -07002002
2003 private class InCallUiLockImpl implements InCallUiLock {
2004 private final String tag;
2005
2006 private InCallUiLockImpl(String tag) {
2007 this.tag = tag;
2008 }
2009
2010 @MainThread
2011 @Override
2012 public void release() {
2013 Assert.isMainThread();
2014 releaseInCallUiLock(InCallUiLockImpl.this);
2015 }
2016
2017 @Override
2018 public String toString() {
2019 return "InCallUiLock[" + tag + "]";
2020 }
2021 }
2022
2023 @MainThread
2024 public InCallUiLock acquireInCallUiLock(String tag) {
2025 Assert.isMainThread();
2026 InCallUiLock lock = new InCallUiLockImpl(tag);
linyuh183cb712017-12-27 17:02:37 -08002027 inCallUiLocks.add(lock);
twyen8efb4952017-10-06 16:35:54 -07002028 return lock;
2029 }
2030
2031 @MainThread
2032 private void releaseInCallUiLock(InCallUiLock lock) {
2033 Assert.isMainThread();
2034 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
linyuh183cb712017-12-27 17:02:37 -08002035 inCallUiLocks.remove(lock);
2036 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002037 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
linyuh183cb712017-12-27 17:02:37 -08002038 if (inCallState == InCallState.NO_CALLS) {
twyen8efb4952017-10-06 16:35:54 -07002039 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
2040 attemptFinishActivity();
2041 attemptCleanup();
2042 }
2043 }
2044 }
2045
2046 @MainThread
2047 public boolean isInCallUiLocked() {
2048 Assert.isMainThread();
linyuh183cb712017-12-27 17:02:37 -08002049 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002050 return false;
2051 }
linyuh183cb712017-12-27 17:02:37 -08002052 for (InCallUiLock lock : inCallUiLocks) {
twyen8efb4952017-10-06 16:35:54 -07002053 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
2054 }
2055 return true;
2056 }
2057
linyuh183cb712017-12-27 17:02:37 -08002058 private final Set<InCallUiLock> inCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08002059}