blob: f0d3adc7a5e871b3f371bbcc14fa52215dc3f6e3 [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;
Eric Erfanianccca3152017-02-22 16:32:36 -080042import com.android.contacts.common.compat.CallCompat;
43import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler;
44import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler.OnCheckBlockedListener;
Eric Erfanian83b20212017-05-31 08:53:10 -070045import com.android.dialer.blocking.FilteredNumberCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080046import com.android.dialer.blocking.FilteredNumbersUtil;
twyen8efb4952017-10-06 16:35:54 -070047import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080048import com.android.dialer.common.LogUtil;
zachh6a4cebd2017-10-24 17:10:06 -070049import com.android.dialer.common.concurrent.DialerExecutorComponent;
Eric Erfaniand8046e52017-04-06 09:41:50 -070050import com.android.dialer.enrichedcall.EnrichedCallComponent;
Eric Erfanian10b34a52017-05-04 08:23:17 -070051import com.android.dialer.location.GeoUtil;
Eric Erfanian8369df02017-05-03 10:27:13 -070052import com.android.dialer.logging.InteractionEvent;
Eric Erfanianccca3152017-02-22 16:32:36 -080053import com.android.dialer.logging.Logger;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070054import com.android.dialer.postcall.PostCall;
twyena4745bd2017-12-12 18:40:11 -080055import com.android.dialer.telecom.TelecomCallUtil;
Eric Erfanianccca3152017-02-22 16:32:36 -080056import com.android.dialer.telecom.TelecomUtil;
57import com.android.dialer.util.TouchPointManager;
58import com.android.incallui.InCallOrientationEventListener.ScreenOrientation;
59import com.android.incallui.answerproximitysensor.PseudoScreenState;
yueg77cb8e52017-10-27 16:42:51 -070060import com.android.incallui.audiomode.AudioModeProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080061import com.android.incallui.call.CallList;
62import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080063import com.android.incallui.call.ExternalCallList;
Eric Erfanianccca3152017-02-22 16:32:36 -080064import com.android.incallui.call.TelecomAdapter;
Eric Erfanian2ca43182017-08-31 06:57:16 -070065import com.android.incallui.disconnectdialog.DisconnectMessage;
twyen8efb4952017-10-06 16:35:54 -070066import com.android.incallui.incalluilock.InCallUiLock;
Eric Erfanianccca3152017-02-22 16:32:36 -080067import com.android.incallui.latencyreport.LatencyReport;
68import com.android.incallui.legacyblocking.BlockedNumberContentObserver;
69import com.android.incallui.spam.SpamCallListListener;
Eric Erfanianccca3152017-02-22 16:32:36 -080070import com.android.incallui.videosurface.bindings.VideoSurfaceBindings;
71import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
Eric Erfanian90508232017-03-24 09:31:16 -070072import com.android.incallui.videotech.utils.VideoUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080073import java.util.Collections;
74import java.util.List;
75import java.util.Objects;
76import java.util.Set;
77import java.util.concurrent.ConcurrentHashMap;
78import java.util.concurrent.CopyOnWriteArrayList;
79import java.util.concurrent.atomic.AtomicBoolean;
80
81/**
82 * Takes updates from the CallList and notifies the InCallActivity (UI) of the changes. Responsible
83 * for starting the activity for a new call and finishing the activity when all calls are
84 * disconnected. Creates and manages the in-call state and provides a listener pattern for the
85 * presenters that want to listen in on the in-call state changes. TODO: This class has become more
86 * of a state machine at this point. Consider renaming.
87 */
yueg77cb8e52017-10-27 16:42:51 -070088public class InCallPresenter implements CallList.Listener, AudioModeProvider.AudioModeListener {
Eric Erfanian2ca43182017-08-31 06:57:16 -070089 private static final String PIXEL2017_SYSTEM_FEATURE =
90 "com.google.android.feature.PIXEL_2017_EXPERIENCE";
Eric Erfanianccca3152017-02-22 16:32:36 -080091
92 private static final long BLOCK_QUERY_TIMEOUT_MS = 1000;
93
94 private static final Bundle EMPTY_EXTRAS = new Bundle();
95
linyuh183cb712017-12-27 17:02:37 -080096 private static InCallPresenter inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -080097
98 /**
99 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is load factor before
100 * resizing, 1 means we only expect a single thread to access the map so make only a single shard
101 */
linyuh183cb712017-12-27 17:02:37 -0800102 private final Set<InCallStateListener> listeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800103 Collections.newSetFromMap(new ConcurrentHashMap<InCallStateListener, Boolean>(8, 0.9f, 1));
104
linyuh183cb712017-12-27 17:02:37 -0800105 private final List<IncomingCallListener> incomingCallListeners = new CopyOnWriteArrayList<>();
106 private final Set<InCallDetailsListener> detailsListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800107 Collections.newSetFromMap(new ConcurrentHashMap<InCallDetailsListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800108 private final Set<CanAddCallListener> canAddCallListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800109 Collections.newSetFromMap(new ConcurrentHashMap<CanAddCallListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800110 private final Set<InCallUiListener> inCallUiListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800111 Collections.newSetFromMap(new ConcurrentHashMap<InCallUiListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800112 private final Set<InCallOrientationListener> orientationListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800113 Collections.newSetFromMap(
114 new ConcurrentHashMap<InCallOrientationListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800115 private final Set<InCallEventListener> inCallEventListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800116 Collections.newSetFromMap(new ConcurrentHashMap<InCallEventListener, Boolean>(8, 0.9f, 1));
117
linyuh183cb712017-12-27 17:02:37 -0800118 private StatusBarNotifier statusBarNotifier;
119 private ExternalCallNotifier externalCallNotifier;
120 private ContactInfoCache contactInfoCache;
121 private Context context;
122 private final OnCheckBlockedListener onCheckBlockedListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800123 new OnCheckBlockedListener() {
124 @Override
125 public void onCheckComplete(final Integer id) {
126 if (id != null && id != FilteredNumberAsyncQueryHandler.INVALID_ID) {
127 // Silence the ringer now to prevent ringing and vibration before the call is
128 // terminated when Telecom attempts to add it.
linyuh183cb712017-12-27 17:02:37 -0800129 TelecomUtil.silenceRinger(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800130 }
131 }
132 };
linyuh183cb712017-12-27 17:02:37 -0800133 private CallList callList;
134 private ExternalCallList externalCallList;
135 private InCallActivity inCallActivity;
136 private ManageConferenceActivity manageConferenceActivity;
137 private final android.telecom.Call.Callback callCallback =
Eric Erfanianccca3152017-02-22 16:32:36 -0800138 new android.telecom.Call.Callback() {
139 @Override
140 public void onPostDialWait(
141 android.telecom.Call telecomCall, String remainingPostDialSequence) {
linyuh183cb712017-12-27 17:02:37 -0800142 final DialerCall call = callList.getDialerCallFromTelecomCall(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800143 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700144 LogUtil.w(
145 "InCallPresenter.onPostDialWait",
146 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800147 return;
148 }
149 onPostDialCharWait(call.getId(), remainingPostDialSequence);
150 }
151
152 @Override
153 public void onDetailsChanged(
154 android.telecom.Call telecomCall, android.telecom.Call.Details details) {
linyuh183cb712017-12-27 17:02:37 -0800155 final DialerCall call = callList.getDialerCallFromTelecomCall(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800156 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700157 LogUtil.w(
158 "InCallPresenter.onDetailsChanged",
159 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800160 return;
161 }
162
163 if (details.hasProperty(Details.PROPERTY_IS_EXTERNAL_CALL)
linyuh183cb712017-12-27 17:02:37 -0800164 && !externalCallList.isCallTracked(telecomCall)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800165
166 // A regular call became an external call so swap call lists.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700167 LogUtil.i("InCallPresenter.onDetailsChanged", "Call became external: " + telecomCall);
linyuh183cb712017-12-27 17:02:37 -0800168 callList.onInternalCallMadeExternal(context, telecomCall);
169 externalCallList.onCallAdded(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800170 return;
171 }
172
linyuh183cb712017-12-27 17:02:37 -0800173 for (InCallDetailsListener listener : detailsListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800174 listener.onDetailsChanged(call, details);
175 }
176 }
177
178 @Override
179 public void onConferenceableCallsChanged(
180 android.telecom.Call telecomCall, List<android.telecom.Call> conferenceableCalls) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700181 LogUtil.i(
182 "InCallPresenter.onConferenceableCallsChanged",
183 "onConferenceableCallsChanged: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800184 onDetailsChanged(telecomCall, telecomCall.getDetails());
185 }
186 };
linyuh183cb712017-12-27 17:02:37 -0800187 private InCallState inCallState = InCallState.NO_CALLS;
188 private ProximitySensor proximitySensor;
189 private final PseudoScreenState pseudoScreenState = new PseudoScreenState();
190 private boolean serviceConnected;
191 private InCallCameraManager inCallCameraManager;
192 private FilteredNumberAsyncQueryHandler filteredQueryHandler;
193 private CallList.Listener spamCallListListener;
Eric Erfanianccca3152017-02-22 16:32:36 -0800194 /** Whether or not we are currently bound and waiting for Telecom to send us a new call. */
linyuh183cb712017-12-27 17:02:37 -0800195 private boolean boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -0800196 /** Determines if the InCall UI is in fullscreen mode or not. */
linyuh183cb712017-12-27 17:02:37 -0800197 private boolean isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800198
linyuh183cb712017-12-27 17:02:37 -0800199 private boolean screenTimeoutEnabled = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700200
linyuh183cb712017-12-27 17:02:37 -0800201 private PhoneStateListener phoneStateListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800202 new PhoneStateListener() {
203 @Override
204 public void onCallStateChanged(int state, String incomingNumber) {
205 if (state == TelephonyManager.CALL_STATE_RINGING) {
linyuh183cb712017-12-27 17:02:37 -0800206 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800207 return;
208 }
209 // Check if the number is blocked, to silence the ringer.
linyuh183cb712017-12-27 17:02:37 -0800210 String countryIso = GeoUtil.getCurrentCountryIso(context);
211 filteredQueryHandler.isBlockedNumber(
212 onCheckBlockedListener, incomingNumber, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800213 }
214 }
215 };
twyena4745bd2017-12-12 18:40:11 -0800216
Eric Erfanianccca3152017-02-22 16:32:36 -0800217 /** Whether or not InCallService is bound to Telecom. */
linyuh183cb712017-12-27 17:02:37 -0800218 private boolean serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800219
220 /**
221 * When configuration changes Android kills the current activity and starts a new one. The flag is
222 * used to check if full clean up is necessary (activity is stopped and new activity won't be
223 * started), or if a new activity will be started right after the current one is destroyed, and
224 * therefore no need in release all resources.
225 */
linyuh183cb712017-12-27 17:02:37 -0800226 private boolean isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800227
linyuh183cb712017-12-27 17:02:37 -0800228 private boolean awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800229
linyuh183cb712017-12-27 17:02:37 -0800230 private ExternalCallList.ExternalCallListener externalCallListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800231 new ExternalCallList.ExternalCallListener() {
232
233 @Override
234 public void onExternalCallPulled(android.telecom.Call call) {
235 // Note: keep this code in sync with InCallPresenter#onCallAdded
236 LatencyReport latencyReport = new LatencyReport(call);
237 latencyReport.onCallBlockingDone();
238 // Note: External calls do not require spam checking.
linyuh183cb712017-12-27 17:02:37 -0800239 callList.onCallAdded(context, call, latencyReport);
240 call.registerCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800241 }
242
243 @Override
244 public void onExternalCallAdded(android.telecom.Call call) {
245 // No-op
246 }
247
248 @Override
249 public void onExternalCallRemoved(android.telecom.Call call) {
250 // No-op
251 }
252
253 @Override
254 public void onExternalCallUpdated(android.telecom.Call call) {
255 // No-op
256 }
257 };
258
linyuh183cb712017-12-27 17:02:37 -0800259 private ThemeColorManager themeColorManager;
260 private VideoSurfaceTexture localVideoSurfaceTexture;
261 private VideoSurfaceTexture remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -0800262
yueg7f5acbe2018-01-10 13:50:29 -0800263 private MotorolaInCallUiNotifier motorolaInCallUiNotifier;
264
Eric Erfanian10b34a52017-05-04 08:23:17 -0700265 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800266 @VisibleForTesting
267 InCallPresenter() {}
268
269 public static synchronized InCallPresenter getInstance() {
linyuh183cb712017-12-27 17:02:37 -0800270 if (inCallPresenter == null) {
wangqic8cf79e2017-10-17 09:21:00 -0700271 Trace.beginSection("InCallPresenter.Constructor");
linyuh183cb712017-12-27 17:02:37 -0800272 inCallPresenter = new InCallPresenter();
wangqic8cf79e2017-10-17 09:21:00 -0700273 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800274 }
linyuh183cb712017-12-27 17:02:37 -0800275 return inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -0800276 }
277
Eric Erfanian10b34a52017-05-04 08:23:17 -0700278 @VisibleForTesting
279 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
linyuh183cb712017-12-27 17:02:37 -0800280 InCallPresenter.inCallPresenter = inCallPresenter;
Eric Erfanian10b34a52017-05-04 08:23:17 -0700281 }
282
Eric Erfanianccca3152017-02-22 16:32:36 -0800283 /**
284 * Determines whether or not a call has no valid phone accounts that can be used to make the call
285 * with. Emergency calls do not require a phone account.
286 *
287 * @param call to check accounts for.
288 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
289 * call contains a phone account that could be used to initiate it with, or is an emergency
290 * call.
291 */
292 public static boolean isCallWithNoValidAccounts(DialerCall call) {
293 if (call != null && !call.isEmergencyCall()) {
294 Bundle extras = call.getIntentExtras();
295
296 if (extras == null) {
297 extras = EMPTY_EXTRAS;
298 }
299
300 final List<PhoneAccountHandle> phoneAccountHandles =
301 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
302
303 if ((call.getAccountHandle() == null
304 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700305 LogUtil.i(
306 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800307 return true;
308 }
309 }
310 return false;
311 }
312
313 public InCallState getInCallState() {
linyuh183cb712017-12-27 17:02:37 -0800314 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800315 }
316
317 public CallList getCallList() {
linyuh183cb712017-12-27 17:02:37 -0800318 return callList;
Eric Erfanianccca3152017-02-22 16:32:36 -0800319 }
320
321 public void setUp(
322 @NonNull Context context,
323 CallList callList,
324 ExternalCallList externalCallList,
325 StatusBarNotifier statusBarNotifier,
326 ExternalCallNotifier externalCallNotifier,
327 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700328 ProximitySensor proximitySensor,
329 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700330 Trace.beginSection("InCallPresenter.setUp");
linyuh183cb712017-12-27 17:02:37 -0800331 if (serviceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700332 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
linyuh183cb712017-12-27 17:02:37 -0800333 if (context != this.context || callList != this.callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800334 throw new IllegalStateException();
335 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700336 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800337 return;
338 }
339
340 Objects.requireNonNull(context);
linyuh183cb712017-12-27 17:02:37 -0800341 this.context = context;
Eric Erfanianccca3152017-02-22 16:32:36 -0800342
linyuh183cb712017-12-27 17:02:37 -0800343 this.contactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800344
linyuh183cb712017-12-27 17:02:37 -0800345 this.statusBarNotifier = statusBarNotifier;
346 this.externalCallNotifier = externalCallNotifier;
347 addListener(this.statusBarNotifier);
348 EnrichedCallComponent.get(this.context)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700349 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -0800350 .registerStateChangedListener(this.statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800351
linyuh183cb712017-12-27 17:02:37 -0800352 this.proximitySensor = proximitySensor;
353 addListener(this.proximitySensor);
Eric Erfanianccca3152017-02-22 16:32:36 -0800354
linyuh183cb712017-12-27 17:02:37 -0800355 if (themeColorManager == null) {
356 themeColorManager =
357 new ThemeColorManager(new InCallUIMaterialColorMapUtils(this.context.getResources()));
wangqi8d662ca2017-10-26 11:27:19 -0700358 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800359
linyuh183cb712017-12-27 17:02:37 -0800360 this.callList = callList;
361 this.externalCallList = externalCallList;
362 externalCallList.addExternalCallListener(this.externalCallNotifier);
363 externalCallList.addExternalCallListener(externalCallListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800364
365 // This only gets called by the service so this is okay.
linyuh183cb712017-12-27 17:02:37 -0800366 serviceConnected = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800367
368 // The final thing we do in this set up is add ourselves as a listener to CallList. This
369 // will kick off an update and the whole process can start.
linyuh183cb712017-12-27 17:02:37 -0800370 this.callList.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800371
372 // Create spam call list listener and add it to the list of listeners
linyuh183cb712017-12-27 17:02:37 -0800373 spamCallListListener =
zachh6a4cebd2017-10-24 17:10:06 -0700374 new SpamCallListListener(
375 context, DialerExecutorComponent.get(context).dialerExecutorFactory());
linyuh183cb712017-12-27 17:02:37 -0800376 this.callList.addListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800377
378 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800379
linyuh183cb712017-12-27 17:02:37 -0800380 filteredQueryHandler = filteredNumberQueryHandler;
381 this.context
Eric Erfanianccca3152017-02-22 16:32:36 -0800382 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800383 .listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800384
yueg77cb8e52017-10-27 16:42:51 -0700385 AudioModeProvider.getInstance().addListener(this);
386
yueg7f5acbe2018-01-10 13:50:29 -0800387 if (motorolaInCallUiNotifier == null) {
388 // Add listener to notify Telephony process when the incoming call screen is started or
389 // finished. This is for hiding USSD dialog because the incoming call screen should have
390 // higher precedence over this dialog.
391 motorolaInCallUiNotifier = new MotorolaInCallUiNotifier(context);
392 addInCallUiListener(motorolaInCallUiNotifier);
393 addListener(motorolaInCallUiNotifier);
394 }
395
Eric Erfanian2ca43182017-08-31 06:57:16 -0700396 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
397 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800398 }
399
400 /**
401 * Called when the telephony service has disconnected from us. This will happen when there are no
402 * more active calls. However, we may still want to continue showing the UI for certain cases like
403 * showing "Call Ended". What we really want is to wait for the activity and the service to both
404 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
405 * secondary method that performs the aforementioned logic.
406 */
407 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700408 LogUtil.d("InCallPresenter.tearDown", "tearDown");
linyuh183cb712017-12-27 17:02:37 -0800409 callList.clearOnDisconnect();
Eric Erfanianccca3152017-02-22 16:32:36 -0800410
linyuh183cb712017-12-27 17:02:37 -0800411 serviceConnected = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800412
linyuh183cb712017-12-27 17:02:37 -0800413 context
Eric Erfanianccca3152017-02-22 16:32:36 -0800414 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800415 .listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800416
417 attemptCleanup();
418 VideoPauseController.getInstance().tearDown();
yueg77cb8e52017-10-27 16:42:51 -0700419 AudioModeProvider.getInstance().removeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800420 }
421
422 private void attemptFinishActivity() {
linyuh183cb712017-12-27 17:02:37 -0800423 screenTimeoutEnabled = true;
424 final boolean doFinish = (inCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700425 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800426 if (doFinish) {
linyuh183cb712017-12-27 17:02:37 -0800427 inCallActivity.setExcludeFromRecents(true);
428 inCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800429 }
430 }
431
432 /**
433 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
434 * for more insight on the tear-down process.
435 */
436 public void unsetActivity(InCallActivity inCallActivity) {
437 if (inCallActivity == null) {
438 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
439 }
linyuh183cb712017-12-27 17:02:37 -0800440 if (this.inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700441 LogUtil.i(
442 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800443 return;
444 }
linyuh183cb712017-12-27 17:02:37 -0800445 if (this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700446 LogUtil.w(
447 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800448 "Second instance of InCallActivity is trying to unregister when another"
449 + " instance is active. Ignoring.");
450 return;
451 }
452 updateActivity(null);
453 }
454
455 /**
456 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
457 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
458 */
459 private void updateActivity(InCallActivity inCallActivity) {
wangqi9982f0d2017-10-11 17:46:07 -0700460 Trace.beginSection("InCallPresenter.updateActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800461 boolean updateListeners = false;
462 boolean doAttemptCleanup = false;
463
464 if (inCallActivity != null) {
linyuh183cb712017-12-27 17:02:37 -0800465 if (this.inCallActivity == null) {
466 context = inCallActivity.getApplicationContext();
Eric Erfanianccca3152017-02-22 16:32:36 -0800467 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700468 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800469 } else {
470 // since setActivity is called onStart(), it can be called multiple times.
471 // This is fine and ignorable, but we do not want to update the world every time
472 // this happens (like going to/from background) so we do not set updateListeners.
473 }
474
linyuh183cb712017-12-27 17:02:37 -0800475 this.inCallActivity = inCallActivity;
476 this.inCallActivity.setExcludeFromRecents(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800477
478 // By the time the UI finally comes up, the call may already be disconnected.
479 // If that's the case, we may need to show an error dialog.
linyuh183cb712017-12-27 17:02:37 -0800480 if (callList != null && callList.getDisconnectedCall() != null) {
481 showDialogOrToastForDisconnectedCall(callList.getDisconnectedCall());
Eric Erfanianccca3152017-02-22 16:32:36 -0800482 }
483
484 // When the UI comes up, we need to first check the in-call state.
485 // If we are showing NO_CALLS, that means that a call probably connected and
486 // then immediately disconnected before the UI was able to come up.
487 // If we dont have any calls, start tearing down the UI instead.
488 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
489 // it has been set.
linyuh183cb712017-12-27 17:02:37 -0800490 if (inCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700491 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800492 attemptFinishActivity();
wangqi9982f0d2017-10-11 17:46:07 -0700493 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800494 return;
495 }
496 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700497 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800498 updateListeners = true;
linyuh183cb712017-12-27 17:02:37 -0800499 this.inCallActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800500
501 // We attempt cleanup for the destroy case but only after we recalculate the state
502 // to see if we need to come back up or stay shut down. This is why we do the
503 // cleanup after the call to onCallListChange() instead of directly here.
504 doAttemptCleanup = true;
505 }
506
507 // Messages can come from the telephony layer while the activity is coming up
508 // and while the activity is going down. So in both cases we need to recalculate what
509 // state we should be in after they complete.
510 // Examples: (1) A new incoming call could come in and then get disconnected before
511 // the activity is created.
512 // (2) All calls could disconnect and then get a new incoming call before the
513 // activity is destroyed.
514 //
Eric Erfanian938468d2017-10-24 14:05:52 -0700515 // a bug - We previously had a check for mServiceConnected here as well, but there are
Eric Erfanianccca3152017-02-22 16:32:36 -0800516 // cases where we need to recalculate the current state even if the service in not
517 // connected. In particular the case where startOrFinish() is called while the app is
518 // already finish()ing. In that case, we skip updating the state with the knowledge that
519 // we will check again once the activity has finished. That means we have to recalculate the
520 // state here even if the service is disconnected since we may not have finished a state
521 // transition while finish()ing.
522 if (updateListeners) {
linyuh183cb712017-12-27 17:02:37 -0800523 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800524 }
525
526 if (doAttemptCleanup) {
527 attemptCleanup();
528 }
wangqi9982f0d2017-10-11 17:46:07 -0700529 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800530 }
531
532 public void setManageConferenceActivity(
533 @Nullable ManageConferenceActivity manageConferenceActivity) {
linyuh183cb712017-12-27 17:02:37 -0800534 this.manageConferenceActivity = manageConferenceActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -0800535 }
536
537 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700538 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800539 bringToForeground(showDialpad);
540 }
541
542 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700543 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800544 LatencyReport latencyReport = new LatencyReport(call);
545 if (shouldAttemptBlocking(call)) {
546 maybeBlockCall(call, latencyReport);
547 } else {
548 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800549 externalCallList.onCallAdded(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800550 } else {
551 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800552 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800553 }
554 }
555
556 // Since a call has been added we are no longer waiting for Telecom to send us a call.
557 setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -0800558 call.registerCallback(callCallback);
zachh78e54ac2017-12-05 16:38:35 -0800559 // TODO(maxwelb): Return the future in recordPhoneLookupInfo and propagate.
linyuh183cb712017-12-27 17:02:37 -0800560 PhoneLookupHistoryRecorder.recordPhoneLookupInfo(context.getApplicationContext(), call);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700561 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800562 }
563
564 private boolean shouldAttemptBlocking(android.telecom.Call call) {
565 if (call.getState() != android.telecom.Call.STATE_RINGING) {
566 return false;
567 }
linyuh183cb712017-12-27 17:02:37 -0800568 if (!UserManagerCompat.isUserUnlocked(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700569 LogUtil.i(
570 "InCallPresenter.shouldAttemptBlocking",
571 "not attempting to block incoming call because user is locked");
572 return false;
573 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800574 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700575 LogUtil.i(
576 "InCallPresenter.shouldAttemptBlocking",
577 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800578 return false;
579 }
linyuh183cb712017-12-27 17:02:37 -0800580 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700581 LogUtil.i(
582 "InCallPresenter.shouldAttemptBlocking",
583 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800584 return false;
585 }
586 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
587 return false;
588 }
linyuh183cb712017-12-27 17:02:37 -0800589 if (FilteredNumberCompat.useNewFiltering(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700590 LogUtil.i(
591 "InCallPresenter.shouldAttemptBlocking",
592 "not attempting to block incoming call because framework blocking is in use");
593 return false;
594 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800595 return true;
596 }
597
598 /**
599 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
600 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
601 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
602 * call anyways.
603 */
604 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
linyuh183cb712017-12-27 17:02:37 -0800605 final String countryIso = GeoUtil.getCurrentCountryIso(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800606 final String number = TelecomCallUtil.getNumber(call);
607 final long timeAdded = System.currentTimeMillis();
608
609 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
610 // main UI thread. It is needed so we can change its value within different scopes, since
611 // that cannot be done with a final boolean.
612 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
613
614 final Handler handler = new Handler();
615
616 // Proceed if the query is slow; the call may still be blocked after the query returns.
617 final Runnable runnable =
618 new Runnable() {
619 @Override
620 public void run() {
621 hasTimedOut.set(true);
622 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800623 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800624 }
625 };
626 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
627
628 OnCheckBlockedListener onCheckBlockedListener =
629 new OnCheckBlockedListener() {
630 @Override
631 public void onCheckComplete(final Integer id) {
632 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700633 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800634 return;
635 }
636 if (!hasTimedOut.get()) {
637 handler.removeCallbacks(runnable);
638 }
639 if (id == null) {
640 if (!hasTimedOut.get()) {
641 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800642 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800643 }
644 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700645 LogUtil.d(
646 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800647 if (!hasTimedOut.get()) {
648 handler.removeCallbacks(runnable);
649
650 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800651 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800652 }
653 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700654 LogUtil.i(
655 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800656 call.reject(false, null);
linyuh183cb712017-12-27 17:02:37 -0800657 Logger.get(context).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
Eric Erfanianccca3152017-02-22 16:32:36 -0800658
659 /*
660 * If mContext is null, then the InCallPresenter was torn down before the
661 * block check had a chance to complete. The context is no longer valid, so
662 * don't attempt to remove the call log entry.
663 */
linyuh183cb712017-12-27 17:02:37 -0800664 if (context == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800665 return;
666 }
667 // Register observer to update the call log.
668 // BlockedNumberContentObserver will unregister after successful log or timeout.
669 BlockedNumberContentObserver contentObserver =
linyuh183cb712017-12-27 17:02:37 -0800670 new BlockedNumberContentObserver(context, new Handler(), number, timeAdded);
Eric Erfanianccca3152017-02-22 16:32:36 -0800671 contentObserver.register();
672 }
673 }
674 };
675
linyuh183cb712017-12-27 17:02:37 -0800676 filteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800677 }
678
679 public void onCallRemoved(android.telecom.Call call) {
680 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800681 externalCallList.onCallRemoved(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800682 } else {
linyuh183cb712017-12-27 17:02:37 -0800683 callList.onCallRemoved(context, call);
684 call.unregisterCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800685 }
686 }
687
688 public void onCanAddCallChanged(boolean canAddCall) {
linyuh183cb712017-12-27 17:02:37 -0800689 for (CanAddCallListener listener : canAddCallListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800690 listener.onCanAddCallChanged(canAddCall);
691 }
692 }
693
694 @Override
695 public void onWiFiToLteHandover(DialerCall call) {
linyuh183cb712017-12-27 17:02:37 -0800696 if (inCallActivity != null) {
697 inCallActivity.showToastForWiFiToLteHandover(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800698 }
699 }
700
701 @Override
702 public void onHandoverToWifiFailed(DialerCall call) {
linyuh183cb712017-12-27 17:02:37 -0800703 if (inCallActivity != null) {
704 inCallActivity.showDialogOrToastForWifiHandoverFailure(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800705 }
706 }
707
Eric Erfanianc857f902017-05-15 14:05:33 -0700708 @Override
709 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
710 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
linyuh183cb712017-12-27 17:02:37 -0800711 if (inCallActivity != null) {
712 inCallActivity.showDialogForInternationalCallOnWifi(call);
Eric Erfanianc857f902017-05-15 14:05:33 -0700713 }
714 }
715
Eric Erfanianccca3152017-02-22 16:32:36 -0800716 /**
717 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
718 * app based on the information it gets from CallList. Dispatches the in-call state to all
719 * listeners. Can trigger the creation or destruction of the UI based on the states that is
720 * calculates.
721 */
722 @Override
723 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700724 Trace.beginSection("InCallPresenter.onCallListChange");
linyuh183cb712017-12-27 17:02:37 -0800725 if (inCallActivity != null && inCallActivity.isInCallScreenAnimating()) {
726 awaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700727 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800728 return;
729 }
730 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700731 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800732 return;
733 }
734
linyuh183cb712017-12-27 17:02:37 -0800735 awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800736
737 InCallState newState = getPotentialStateFromCallList(callList);
linyuh183cb712017-12-27 17:02:37 -0800738 InCallState oldState = inCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700739 LogUtil.d(
740 "InCallPresenter.onCallListChange",
741 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700742
743 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
744 // incall activity for that call will still exist (even if it's not visible). In the case of
745 // an incoming call in that situation, just disconnect that "waiting for account" call and
746 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
Eric Erfanian938468d2017-10-24 14:05:52 -0700747 // a bug for more details.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700748 DialerCall waitingForAccountCall;
749 if (newState == InCallState.INCOMING
750 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
751 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700752 // The InCallActivity might be destroyed or not started yet at this point.
753 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800754 inCallActivity.dismissPendingDialogs();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700755 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700756 }
757
Eric Erfanianccca3152017-02-22 16:32:36 -0800758 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700759 LogUtil.d(
760 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800761
762 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700763 LogUtil.i(
764 "InCallPresenter.onCallListChange",
765 "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800766 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800767
768 // notify listeners of new state
linyuh183cb712017-12-27 17:02:37 -0800769 for (InCallStateListener listener : listeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700770 LogUtil.d(
771 "InCallPresenter.onCallListChange",
linyuh183cb712017-12-27 17:02:37 -0800772 "Notify " + listener + " of state " + inCallState.toString());
773 listener.onStateChange(oldState, inCallState, callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800774 }
775
776 if (isActivityStarted()) {
777 final boolean hasCall =
778 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
linyuh183cb712017-12-27 17:02:37 -0800779 inCallActivity.dismissKeyguard(hasCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800780 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700781 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800782 }
783
784 /** Called when there is a new incoming call. */
785 @Override
786 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700787 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800788 InCallState newState = startOrFinishUi(InCallState.INCOMING);
linyuh183cb712017-12-27 17:02:37 -0800789 InCallState oldState = inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800790
Eric Erfanian2ca43182017-08-31 06:57:16 -0700791 LogUtil.i(
792 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800793 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800794
wangqicf61ca02017-08-31 15:32:55 -0700795 Trace.beginSection("listener.onIncomingCall");
linyuh183cb712017-12-27 17:02:37 -0800796 for (IncomingCallListener listener : incomingCallListeners) {
797 listener.onIncomingCall(oldState, inCallState, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800798 }
wangqicf61ca02017-08-31 15:32:55 -0700799 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800800
wangqicf61ca02017-08-31 15:32:55 -0700801 Trace.beginSection("onPrimaryCallStateChanged");
linyuh183cb712017-12-27 17:02:37 -0800802 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800803 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800804 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800805 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700806 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700807 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800808 }
809
810 @Override
811 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700812 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
linyuh183cb712017-12-27 17:02:37 -0800813 && inCallState == InCallPresenter.InCallState.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800814 LogUtil.i(
815 "InCallPresenter.onUpgradeToVideo",
816 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700817 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800818 }
819
linyuh183cb712017-12-27 17:02:37 -0800820 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800821 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800822 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800823 }
824 }
825
826 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700827 public void onSessionModificationStateChange(DialerCall call) {
828 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800829 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
linyuh183cb712017-12-27 17:02:37 -0800830 if (proximitySensor == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800831 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
832 return;
833 }
linyuh183cb712017-12-27 17:02:37 -0800834 proximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700835 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
linyuh183cb712017-12-27 17:02:37 -0800836 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800837 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800838 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800839 }
840 }
841
842 /**
843 * Called when a call becomes disconnected. Called everytime an existing call changes from being
844 * connected (incoming/outgoing/active) to disconnected.
845 */
846 @Override
847 public void onDisconnect(DialerCall call) {
linyuh7b86f562017-11-16 11:24:09 -0800848 showDialogOrToastForDisconnectedCall(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800849
850 // We need to do the run the same code as onCallListChange.
linyuh183cb712017-12-27 17:02:37 -0800851 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800852
853 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800854 inCallActivity.dismissKeyguard(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800855 }
856
857 if (call.isEmergencyCall()) {
linyuh183cb712017-12-27 17:02:37 -0800858 FilteredNumbersUtil.recordLastEmergencyCallTime(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800859 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800860
linyuh183cb712017-12-27 17:02:37 -0800861 if (!callList.hasLiveCall()
Eric Erfanianfc37b022017-03-21 10:11:17 -0700862 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700863 && !isSecretCode(call.getNumber())
wangqi9982f0d2017-10-11 17:46:07 -0700864 && !call.isVoiceMailNumber()) {
linyuh183cb712017-12-27 17:02:37 -0800865 PostCall.onCallDisconnected(context, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -0800866 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800867 }
868
Eric Erfanian10b34a52017-05-04 08:23:17 -0700869 private boolean isSecretCode(@Nullable String number) {
870 return number != null
871 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
872 }
873
Eric Erfanianccca3152017-02-22 16:32:36 -0800874 /** Given the call list, return the state in which the in-call screen should be. */
875 public InCallState getPotentialStateFromCallList(CallList callList) {
876
877 InCallState newState = InCallState.NO_CALLS;
878
879 if (callList == null) {
880 return newState;
881 }
882 if (callList.getIncomingCall() != null) {
883 newState = InCallState.INCOMING;
884 } else if (callList.getWaitingForAccountCall() != null) {
885 newState = InCallState.WAITING_FOR_ACCOUNT;
886 } else if (callList.getPendingOutgoingCall() != null) {
887 newState = InCallState.PENDING_OUTGOING;
888 } else if (callList.getOutgoingCall() != null) {
889 newState = InCallState.OUTGOING;
890 } else if (callList.getActiveCall() != null
891 || callList.getBackgroundCall() != null
892 || callList.getDisconnectedCall() != null
893 || callList.getDisconnectingCall() != null) {
894 newState = InCallState.INCALL;
895 }
896
897 if (newState == InCallState.NO_CALLS) {
linyuh183cb712017-12-27 17:02:37 -0800898 if (boundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700899 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800900 }
901 }
902
903 return newState;
904 }
905
906 public boolean isBoundAndWaitingForOutgoingCall() {
linyuh183cb712017-12-27 17:02:37 -0800907 return boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -0800908 }
909
910 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700911 LogUtil.i(
912 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
913 "setBoundAndWaitingForOutgoingCall: " + isBound);
linyuh183cb712017-12-27 17:02:37 -0800914 boundAndWaitingForOutgoingCall = isBound;
915 themeColorManager.setPendingPhoneAccountHandle(handle);
916 if (isBound && inCallState == InCallState.NO_CALLS) {
917 inCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800918 }
919 }
920
921 public void onShrinkAnimationComplete() {
linyuh183cb712017-12-27 17:02:37 -0800922 if (awaitingCallListUpdate) {
923 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800924 }
925 }
926
927 public void addIncomingCallListener(IncomingCallListener listener) {
928 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -0800929 incomingCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800930 }
931
932 public void removeIncomingCallListener(IncomingCallListener listener) {
933 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -0800934 incomingCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800935 }
936 }
937
938 public void addListener(InCallStateListener listener) {
939 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -0800940 listeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800941 }
942
943 public void removeListener(InCallStateListener listener) {
944 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -0800945 listeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800946 }
947 }
948
949 public void addDetailsListener(InCallDetailsListener listener) {
950 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -0800951 detailsListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800952 }
953
954 public void removeDetailsListener(InCallDetailsListener listener) {
955 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -0800956 detailsListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800957 }
958 }
959
960 public void addCanAddCallListener(CanAddCallListener listener) {
961 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -0800962 canAddCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800963 }
964
965 public void removeCanAddCallListener(CanAddCallListener listener) {
966 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -0800967 canAddCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800968 }
969 }
970
971 public void addOrientationListener(InCallOrientationListener listener) {
972 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -0800973 orientationListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800974 }
975
976 public void removeOrientationListener(InCallOrientationListener listener) {
977 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -0800978 orientationListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800979 }
980 }
981
982 public void addInCallEventListener(InCallEventListener listener) {
983 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -0800984 inCallEventListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800985 }
986
987 public void removeInCallEventListener(InCallEventListener listener) {
988 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -0800989 inCallEventListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800990 }
991 }
992
993 public ProximitySensor getProximitySensor() {
linyuh183cb712017-12-27 17:02:37 -0800994 return proximitySensor;
Eric Erfanianccca3152017-02-22 16:32:36 -0800995 }
996
997 public PseudoScreenState getPseudoScreenState() {
linyuh183cb712017-12-27 17:02:37 -0800998 return pseudoScreenState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800999 }
1000
1001 /** Returns true if the incall app is the foreground application. */
1002 public boolean isShowingInCallUi() {
1003 if (!isActivityStarted()) {
1004 return false;
1005 }
linyuh183cb712017-12-27 17:02:37 -08001006 if (manageConferenceActivity != null && manageConferenceActivity.isVisible()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001007 return true;
1008 }
linyuh183cb712017-12-27 17:02:37 -08001009 return inCallActivity.isVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001010 }
1011
1012 /**
1013 * Returns true if the activity has been created and is running. Returns true as long as activity
1014 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
1015 * (not in foreground).
1016 */
1017 public boolean isActivityStarted() {
linyuh183cb712017-12-27 17:02:37 -08001018 return (inCallActivity != null
1019 && !inCallActivity.isDestroyed()
1020 && !inCallActivity.isFinishing());
Eric Erfanianccca3152017-02-22 16:32:36 -08001021 }
1022
1023 /**
1024 * Determines if the In-Call app is currently changing configuration.
1025 *
1026 * @return {@code true} if the In-Call app is changing configuration.
1027 */
1028 public boolean isChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001029 return isChangingConfigurations;
Eric Erfanianccca3152017-02-22 16:32:36 -08001030 }
1031
1032 /**
1033 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1034 * screen orientation).
1035 */
1036 /*package*/
1037 void updateIsChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001038 isChangingConfigurations = false;
1039 if (inCallActivity != null) {
1040 isChangingConfigurations = inCallActivity.isChangingConfigurations();
Eric Erfanianccca3152017-02-22 16:32:36 -08001041 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001042 LogUtil.v(
1043 "InCallPresenter.updateIsChangingConfigurations",
linyuh183cb712017-12-27 17:02:37 -08001044 "updateIsChangingConfigurations = " + isChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001045 }
1046
1047 /** Called when the activity goes in/out of the foreground. */
1048 public void onUiShowing(boolean showing) {
1049 // We need to update the notification bar when we leave the UI because that
1050 // could trigger it to show again.
linyuh183cb712017-12-27 17:02:37 -08001051 if (statusBarNotifier != null) {
1052 statusBarNotifier.updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -08001053 }
1054
linyuh183cb712017-12-27 17:02:37 -08001055 if (proximitySensor != null) {
1056 proximitySensor.onInCallShowing(showing);
Eric Erfanianccca3152017-02-22 16:32:36 -08001057 }
1058
yueg092b21c2017-11-15 16:20:07 -08001059 if (!showing) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001060 updateIsChangingConfigurations();
1061 }
1062
linyuh183cb712017-12-27 17:02:37 -08001063 for (InCallUiListener listener : inCallUiListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001064 listener.onUiShowing(showing);
1065 }
1066
linyuh183cb712017-12-27 17:02:37 -08001067 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001068 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001069 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001070 }
1071 }
1072
Eric Erfanian2ca43182017-08-31 06:57:16 -07001073 public void refreshUi() {
linyuh183cb712017-12-27 17:02:37 -08001074 if (inCallActivity != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001075 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001076 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001077 }
1078 }
1079
Eric Erfanianccca3152017-02-22 16:32:36 -08001080 public void addInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001081 inCallUiListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001082 }
1083
1084 public boolean removeInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001085 return inCallUiListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001086 }
1087
1088 /*package*/
1089 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001090 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001091 notifyVideoPauseController(true);
Eric Erfanian2ca43182017-08-31 06:57:16 -07001092 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001093 }
1094
1095 /*package*/
1096 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001097 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001098 notifyVideoPauseController(false);
1099 }
1100
1101 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001102 LogUtil.d(
1103 "InCallPresenter.notifyVideoPauseController",
linyuh183cb712017-12-27 17:02:37 -08001104 "mIsChangingConfigurations=" + isChangingConfigurations);
1105 if (!isChangingConfigurations) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001106 VideoPauseController.getInstance().onUiShowing(showing);
1107 }
1108 }
1109
1110 /** Brings the app into the foreground if possible. */
1111 public void bringToForeground(boolean showDialpad) {
1112 // Before we bring the incall UI to the foreground, we check to see if:
1113 // 1. It is not currently in the foreground
1114 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1115 // be displayed)
1116 // If the activity hadn't actually been started previously, yet there are still calls
1117 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1118 // bring it up the UI regardless.
linyuh183cb712017-12-27 17:02:37 -08001119 if (!isShowingInCallUi() && inCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001120 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001121 }
1122 }
1123
1124 public void onPostDialCharWait(String callId, String chars) {
1125 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001126 inCallActivity.showDialogForPostCharWait(callId, chars);
Eric Erfanianccca3152017-02-22 16:32:36 -08001127 }
1128 }
1129
1130 /**
1131 * Handles the green CALL key while in-call.
1132 *
1133 * @return true if we consumed the event.
1134 */
1135 public boolean handleCallKey() {
1136 LogUtil.v("InCallPresenter.handleCallKey", null);
1137
1138 // The green CALL button means either "Answer", "Unhold", or
1139 // "Swap calls", or can be a no-op, depending on the current state
1140 // of the Phone.
1141
1142 /** INCOMING CALL */
linyuh183cb712017-12-27 17:02:37 -08001143 final CallList calls = callList;
Eric Erfanianccca3152017-02-22 16:32:36 -08001144 final DialerCall incomingCall = calls.getIncomingCall();
1145 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1146
1147 // (1) Attempt to answer a call
1148 if (incomingCall != null) {
1149 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1150 return true;
1151 }
1152
1153 /** STATE_ACTIVE CALL */
1154 final DialerCall activeCall = calls.getActiveCall();
1155 if (activeCall != null) {
1156 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1157 // consolidate this logic.
1158 final boolean canMerge =
1159 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1160 final boolean canSwap =
1161 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1162
Eric Erfanian2ca43182017-08-31 06:57:16 -07001163 LogUtil.v(
1164 "InCallPresenter.handleCallKey",
1165 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001166
1167 // (2) Attempt actions on conference calls
1168 if (canMerge) {
1169 TelecomAdapter.getInstance().merge(activeCall.getId());
1170 return true;
1171 } else if (canSwap) {
1172 TelecomAdapter.getInstance().swap(activeCall.getId());
1173 return true;
1174 }
1175 }
1176
1177 /** BACKGROUND CALL */
1178 final DialerCall heldCall = calls.getBackgroundCall();
1179 if (heldCall != null) {
1180 // We have a hold call so presumeable it will always support HOLD...but
1181 // there is no harm in double checking.
1182 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1183
Eric Erfanian2ca43182017-08-31 06:57:16 -07001184 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001185
1186 // (4) unhold call
1187 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1188 heldCall.unhold();
1189 return true;
1190 }
1191 }
1192
1193 // Always consume hard keys
1194 return true;
1195 }
1196
Eric Erfanianccca3152017-02-22 16:32:36 -08001197 /** Clears the previous fullscreen state. */
1198 public void clearFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001199 isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001200 }
1201
1202 /**
1203 * Changes the fullscreen mode of the in-call UI.
1204 *
1205 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1206 * otherwise.
1207 */
1208 public void setFullScreen(boolean isFullScreen) {
1209 setFullScreen(isFullScreen, false /* force */);
1210 }
1211
1212 /**
1213 * Changes the fullscreen mode of the in-call UI.
1214 *
1215 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1216 * otherwise.
1217 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1218 */
1219 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001220 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001221
1222 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1223 if (isDialpadVisible()) {
1224 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001225 LogUtil.v(
1226 "InCallPresenter.setFullScreen",
1227 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001228 }
1229
linyuh183cb712017-12-27 17:02:37 -08001230 if (this.isFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001231 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001232 return;
1233 }
linyuh183cb712017-12-27 17:02:37 -08001234 this.isFullScreen = isFullScreen;
1235 notifyFullscreenModeChange(this.isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001236 }
1237
1238 /**
1239 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1240 * otherwise.
1241 */
1242 public boolean isFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001243 return isFullScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001244 }
1245
1246 /**
1247 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1248 *
1249 * @param isFullscreenMode {@code True} if entering full screen mode.
1250 */
1251 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001252 for (InCallEventListener listener : inCallEventListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001253 listener.onFullscreenModeChanged(isFullscreenMode);
1254 }
1255 }
1256
linyuh7b86f562017-11-16 11:24:09 -08001257 /** Instruct the in-call activity to show an error dialog or toast for a disconnected call. */
1258 private void showDialogOrToastForDisconnectedCall(DialerCall call) {
1259 if (!isActivityStarted() || call.getState() != DialerCall.State.DISCONNECTED) {
1260 return;
Eric Erfanianccca3152017-02-22 16:32:36 -08001261 }
linyuh7b86f562017-11-16 11:24:09 -08001262
1263 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1264 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1265 setDisconnectCauseForMissingAccounts(call);
1266 }
1267
linyuh183cb712017-12-27 17:02:37 -08001268 inCallActivity.showDialogOrToastForDisconnectedCall(
1269 new DisconnectMessage(inCallActivity, call));
Eric Erfanianccca3152017-02-22 16:32:36 -08001270 }
1271
1272 /**
1273 * When the state of in-call changes, this is the first method to get called. It determines if the
1274 * UI needs to be started or finished depending on the new state and does it.
1275 */
1276 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001277 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001278 LogUtil.d(
linyuh183cb712017-12-27 17:02:37 -08001279 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + inCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001280
1281 // TODO: Consider a proper state machine implementation
1282
1283 // If the state isn't changing we have already done any starting/stopping of activities in
1284 // a previous pass...so lets cut out early
linyuh183cb712017-12-27 17:02:37 -08001285 if (newState == inCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001286 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001287 return newState;
1288 }
1289
1290 // A new Incoming call means that the user needs to be notified of the the call (since
1291 // it wasn't them who initiated it). We do this through full screen notifications and
1292 // happens indirectly through {@link StatusBarNotifier}.
1293 //
1294 // The process for incoming calls is as follows:
1295 //
1296 // 1) CallList - Announces existence of new INCOMING call
1297 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1298 // - should be set to INCOMING.
1299 // 3) InCallPresenter - This method is called to see if we need to start or finish
1300 // the app given the new state.
1301 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1302 // StatusBarNotifier explicitly to issue a FullScreen Notification
1303 // that will either start the InCallActivity or show the user a
1304 // top-level notification dialog if the user is in an immersive app.
1305 // That notification can also start the InCallActivity.
1306 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1307 // call InCallPresenter::setActivity() to let the presenter
1308 // know that start-up is complete.
1309 //
1310 // [ AND NOW YOU'RE IN THE CALL. voila! ]
1311 //
1312 // Our app is started using a fullScreen notification. We need to do this whenever
1313 // we get an incoming call. Depending on the current context of the device, either a
1314 // incoming call HUN or the actual InCallActivity will be shown.
1315 final boolean startIncomingCallSequence = (InCallState.INCOMING == newState);
1316
1317 // A dialog to show on top of the InCallUI to select a PhoneAccount
1318 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1319
1320 // A new outgoing call indicates that the user just now dialed a number and when that
1321 // happens we need to display the screen immediately or show an account picker dialog if
1322 // no default is set. However, if the main InCallUI is already visible, we do not want to
1323 // re-initiate the start-up animation, so we do not need to do anything here.
1324 //
1325 // It is also possible to go into an intermediate state where the call has been initiated
1326 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1327 // This pending outgoing state can also launch the call screen.
1328 //
1329 // This is different from the incoming call sequence because we do not need to shock the
1330 // user with a top-level notification. Just show the call UI normally.
1331 boolean callCardFragmentVisible =
linyuh183cb712017-12-27 17:02:37 -08001332 inCallActivity != null && inCallActivity.getCallCardFragmentVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001333 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1334 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1335
1336 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1337 // outgoing call process, so the UI should be brought up to show an error dialog.
1338 showCallUi |=
linyuh183cb712017-12-27 17:02:37 -08001339 (InCallState.PENDING_OUTGOING == inCallState
Eric Erfanianccca3152017-02-22 16:32:36 -08001340 && InCallState.INCALL == newState
1341 && !isShowingInCallUi());
1342
1343 // Another exception - InCallActivity is in charge of disconnecting a call with no
1344 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1345 // call so that:
1346 // 1) The call can be disconnected correctly
1347 // 2) The UI comes up and correctly displays the error dialog.
1348 // TODO: Remove these special case conditions by making InCallPresenter a true state
1349 // machine. Telecom should also be the component responsible for disconnecting a call
1350 // with no valid accounts.
1351 showCallUi |=
1352 InCallState.PENDING_OUTGOING == newState
1353 && mainUiNotVisible
linyuh183cb712017-12-27 17:02:37 -08001354 && isCallWithNoValidAccounts(callList.getPendingOutgoingCall());
Eric Erfanianccca3152017-02-22 16:32:36 -08001355
1356 // The only time that we have an instance of mInCallActivity and it isn't started is
1357 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1358 // the activity. When it is finally destroyed, we double check if we should bring it back
1359 // up so we aren't going to lose anything by avoiding a second startup here.
linyuh183cb712017-12-27 17:02:37 -08001360 boolean activityIsFinishing = inCallActivity != null && !isActivityStarted();
Eric Erfanianccca3152017-02-22 16:32:36 -08001361 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001362 LogUtil.i(
1363 "InCallPresenter.startOrFinishUi",
linyuh183cb712017-12-27 17:02:37 -08001364 "Undo the state change: " + newState + " -> " + inCallState);
wangqicf61ca02017-08-31 15:32:55 -07001365 Trace.endSection();
linyuh183cb712017-12-27 17:02:37 -08001366 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001367 }
1368
1369 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1370 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1371 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1372 // outgoing call after user ignore it by pressing home button.
1373 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1374 && !showCallUi
1375 && isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001376 inCallActivity.dismissPendingDialogs();
Eric Erfanianccca3152017-02-22 16:32:36 -08001377 }
1378
1379 if (showCallUi || showAccountPicker) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001380 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001381 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001382 } else if (startIncomingCallSequence) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001383 LogUtil.i("InCallPresenter.startOrFinishUi", "Start Full Screen in call UI");
Eric Erfanianccca3152017-02-22 16:32:36 -08001384
linyuh183cb712017-12-27 17:02:37 -08001385 statusBarNotifier.updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -08001386 } else if (newState == InCallState.NO_CALLS) {
1387 // The new state is the no calls state. Tear everything down.
1388 attemptFinishActivity();
1389 attemptCleanup();
1390 }
1391
wangqicf61ca02017-08-31 15:32:55 -07001392 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001393 return newState;
1394 }
1395
1396 /**
1397 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1398 * or PhoneAccounts to select from.
1399 */
1400 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1401
1402 Bundle extras = call.getIntentExtras();
1403 // Initialize the extras bundle to avoid NPE
1404 if (extras == null) {
1405 extras = new Bundle();
1406 }
1407
1408 final List<PhoneAccountHandle> phoneAccountHandles =
1409 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1410
1411 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1412 String scheme = call.getHandle().getScheme();
1413 final String errorMsg =
1414 PhoneAccount.SCHEME_TEL.equals(scheme)
linyuh183cb712017-12-27 17:02:37 -08001415 ? context.getString(R.string.callFailed_simError)
1416 : context.getString(R.string.incall_error_supp_service_unknown);
Eric Erfanianccca3152017-02-22 16:32:36 -08001417 DisconnectCause disconnectCause =
1418 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1419 call.setDisconnectCause(disconnectCause);
1420 }
1421 }
1422
Eric Erfanianccca3152017-02-22 16:32:36 -08001423 /**
1424 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1425 * Calling classes should use this as an indication whether to interact with the
1426 * InCallPresenter or not.
1427 */
1428 public boolean isReadyForTearDown() {
linyuh183cb712017-12-27 17:02:37 -08001429 return inCallActivity == null && !serviceConnected && inCallState == InCallState.NO_CALLS;
Eric Erfanianccca3152017-02-22 16:32:36 -08001430 }
1431
1432 /**
1433 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1434 */
1435 private void attemptCleanup() {
1436 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001437 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001438
1439 cleanupSurfaces();
1440
linyuh183cb712017-12-27 17:02:37 -08001441 isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001442
1443 // blow away stale contact info so that we get fresh data on
1444 // the next set of calls
linyuh183cb712017-12-27 17:02:37 -08001445 if (contactInfoCache != null) {
1446 contactInfoCache.clearCache();
Eric Erfanianccca3152017-02-22 16:32:36 -08001447 }
linyuh183cb712017-12-27 17:02:37 -08001448 contactInfoCache = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001449
linyuh183cb712017-12-27 17:02:37 -08001450 if (proximitySensor != null) {
1451 removeListener(proximitySensor);
1452 proximitySensor.tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -08001453 }
linyuh183cb712017-12-27 17:02:37 -08001454 proximitySensor = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001455
linyuh183cb712017-12-27 17:02:37 -08001456 if (statusBarNotifier != null) {
1457 removeListener(statusBarNotifier);
1458 EnrichedCallComponent.get(context)
Eric Erfaniand8046e52017-04-06 09:41:50 -07001459 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -08001460 .unregisterStateChangedListener(statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001461 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001462
linyuh183cb712017-12-27 17:02:37 -08001463 if (externalCallNotifier != null && externalCallList != null) {
1464 externalCallList.removeExternalCallListener(externalCallNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001465 }
linyuh183cb712017-12-27 17:02:37 -08001466 statusBarNotifier = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001467
linyuh183cb712017-12-27 17:02:37 -08001468 if (callList != null) {
1469 callList.removeListener(this);
1470 callList.removeListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001471 }
linyuh183cb712017-12-27 17:02:37 -08001472 callList = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001473
linyuh183cb712017-12-27 17:02:37 -08001474 context = null;
1475 inCallActivity = null;
1476 manageConferenceActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001477
linyuh183cb712017-12-27 17:02:37 -08001478 listeners.clear();
1479 incomingCallListeners.clear();
1480 detailsListeners.clear();
1481 canAddCallListeners.clear();
1482 orientationListeners.clear();
1483 inCallEventListeners.clear();
1484 inCallUiListeners.clear();
1485 if (!inCallUiLocks.isEmpty()) {
1486 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + inCallUiLocks);
1487 inCallUiLocks.clear();
twyen8efb4952017-10-06 16:35:54 -07001488 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001489 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001490 }
1491 }
1492
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001493 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001494 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
linyuh183cb712017-12-27 17:02:37 -08001495 context.startActivity(
1496 InCallActivity.getIntent(context, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001497 }
1498
1499 public void onServiceBind() {
linyuh183cb712017-12-27 17:02:37 -08001500 serviceBound = true;
Eric Erfanianccca3152017-02-22 16:32:36 -08001501 }
1502
1503 public void onServiceUnbind() {
1504 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -08001505 serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001506 }
1507
1508 public boolean isServiceBound() {
linyuh183cb712017-12-27 17:02:37 -08001509 return serviceBound;
Eric Erfanianccca3152017-02-22 16:32:36 -08001510 }
1511
1512 public void maybeStartRevealAnimation(Intent intent) {
linyuh183cb712017-12-27 17:02:37 -08001513 if (intent == null || inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001514 return;
1515 }
1516 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1517 if (extras == null) {
1518 // Incoming call, just show the in-call UI directly.
1519 return;
1520 }
1521
1522 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1523 // Account selection dialog will show up so don't show the animation.
1524 return;
1525 }
1526
1527 final PhoneAccountHandle accountHandle =
1528 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1529 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001530
1531 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(true, accountHandle);
1532
1533 final Intent activityIntent =
linyuh183cb712017-12-27 17:02:37 -08001534 InCallActivity.getIntent(context, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001535 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
linyuh183cb712017-12-27 17:02:37 -08001536 context.startActivity(activityIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001537 }
1538
1539 /**
1540 * Retrieves the current in-call camera manager instance, creating if necessary.
1541 *
1542 * @return The {@link InCallCameraManager}.
1543 */
1544 public InCallCameraManager getInCallCameraManager() {
1545 synchronized (this) {
linyuh183cb712017-12-27 17:02:37 -08001546 if (inCallCameraManager == null) {
1547 inCallCameraManager = new InCallCameraManager(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001548 }
1549
linyuh183cb712017-12-27 17:02:37 -08001550 return inCallCameraManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001551 }
1552 }
1553
1554 /**
1555 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1556 *
1557 * @param orientation The screen orientation of the device (one of: {@link
1558 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1559 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1560 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1561 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1562 */
1563 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001564 LogUtil.d(
1565 "InCallPresenter.onDeviceOrientationChange",
1566 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001567
linyuh183cb712017-12-27 17:02:37 -08001568 if (callList != null) {
1569 callList.notifyCallsOfDeviceRotation(orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001570 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001571 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001572 }
1573
1574 // Notify listeners of device orientation changed.
linyuh183cb712017-12-27 17:02:37 -08001575 for (InCallOrientationListener listener : orientationListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001576 listener.onDeviceOrientationChanged(orientation);
1577 }
1578 }
1579
1580 /**
1581 * Configures the in-call UI activity so it can change orientations or not. Enables the
1582 * orientation event listener if allowOrientationChange is true, disables it if false.
1583 *
1584 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1585 * landscape. {@code false} if the in-call UI should be locked in portrait.
1586 */
1587 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
linyuh183cb712017-12-27 17:02:37 -08001588 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001589 LogUtil.e(
1590 "InCallPresenter.setInCallAllowsOrientationChange",
1591 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001592 return;
1593 }
linyuh183cb712017-12-27 17:02:37 -08001594 inCallActivity.setAllowOrientationChange(allowOrientationChange);
Eric Erfanianccca3152017-02-22 16:32:36 -08001595 }
1596
1597 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001598 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
linyuh183cb712017-12-27 17:02:37 -08001599 screenTimeoutEnabled = enable;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001600 applyScreenTimeout();
1601 }
1602
1603 private void applyScreenTimeout() {
linyuh183cb712017-12-27 17:02:37 -08001604 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001605 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001606 return;
1607 }
1608
linyuh183cb712017-12-27 17:02:37 -08001609 final Window window = inCallActivity.getWindow();
1610 if (screenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001611 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1612 } else {
1613 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1614 }
1615 }
1616
1617 /**
1618 * Hides or shows the conference manager fragment.
1619 *
1620 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1621 * be hidden.
1622 */
1623 public void showConferenceCallManager(boolean show) {
linyuh183cb712017-12-27 17:02:37 -08001624 if (inCallActivity != null) {
1625 inCallActivity.showConferenceFragment(show);
Eric Erfanianccca3152017-02-22 16:32:36 -08001626 }
linyuh183cb712017-12-27 17:02:37 -08001627 if (!show && manageConferenceActivity != null) {
1628 manageConferenceActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -08001629 }
1630 }
1631
1632 /**
1633 * Determines if the dialpad is visible.
1634 *
1635 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1636 */
1637 public boolean isDialpadVisible() {
linyuh183cb712017-12-27 17:02:37 -08001638 if (inCallActivity == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001639 return false;
1640 }
linyuh183cb712017-12-27 17:02:37 -08001641 return inCallActivity.isDialpadVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001642 }
1643
1644 public ThemeColorManager getThemeColorManager() {
linyuh183cb712017-12-27 17:02:37 -08001645 return themeColorManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001646 }
1647
wangqi8d662ca2017-10-26 11:27:19 -07001648 @VisibleForTesting
1649 public void setThemeColorManager(ThemeColorManager themeColorManager) {
linyuh183cb712017-12-27 17:02:37 -08001650 this.themeColorManager = themeColorManager;
wangqi8d662ca2017-10-26 11:27:19 -07001651 }
1652
Eric Erfanianccca3152017-02-22 16:32:36 -08001653 /** Called when the foreground call changes. */
1654 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh183cb712017-12-27 17:02:37 -08001655 themeColorManager.onForegroundCallChanged(context, newForegroundCall);
1656 if (inCallActivity != null) {
1657 inCallActivity.onForegroundCallChanged(newForegroundCall);
Eric Erfanianccca3152017-02-22 16:32:36 -08001658 }
1659 }
1660
1661 public InCallActivity getActivity() {
linyuh183cb712017-12-27 17:02:37 -08001662 return inCallActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -08001663 }
1664
1665 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1666 public void setActivity(InCallActivity inCallActivity) {
1667 if (inCallActivity == null) {
1668 throw new IllegalArgumentException("registerActivity cannot be called with null");
1669 }
linyuh183cb712017-12-27 17:02:37 -08001670 if (this.inCallActivity != null && this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001671 LogUtil.w(
1672 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001673 }
1674 updateActivity(inCallActivity);
1675 }
1676
1677 ExternalCallNotifier getExternalCallNotifier() {
linyuh183cb712017-12-27 17:02:37 -08001678 return externalCallNotifier;
Eric Erfanianccca3152017-02-22 16:32:36 -08001679 }
1680
1681 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001682 if (localVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001683 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001684 if (context != null) {
1685 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001686 }
linyuh183cb712017-12-27 17:02:37 -08001687 localVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001688 }
linyuh183cb712017-12-27 17:02:37 -08001689 return localVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001690 }
1691
1692 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001693 if (remoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001694 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001695 if (context != null) {
1696 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001697 }
linyuh183cb712017-12-27 17:02:37 -08001698 remoteVideoSurfaceTexture = VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001699 }
linyuh183cb712017-12-27 17:02:37 -08001700 return remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001701 }
1702
1703 void cleanupSurfaces() {
linyuh183cb712017-12-27 17:02:37 -08001704 if (remoteVideoSurfaceTexture != null) {
1705 remoteVideoSurfaceTexture.setDoneWithSurface();
1706 remoteVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001707 }
linyuh183cb712017-12-27 17:02:37 -08001708 if (localVideoSurfaceTexture != null) {
1709 localVideoSurfaceTexture.setDoneWithSurface();
1710 localVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001711 }
1712 }
1713
yueg77cb8e52017-10-27 16:42:51 -07001714 @Override
1715 public void onAudioStateChanged(CallAudioState audioState) {
linyuh183cb712017-12-27 17:02:37 -08001716 if (statusBarNotifier != null) {
1717 statusBarNotifier.updateNotification();
yueg77cb8e52017-10-27 16:42:51 -07001718 }
1719 }
1720
Eric Erfanianccca3152017-02-22 16:32:36 -08001721 /** All the main states of InCallActivity. */
1722 public enum InCallState {
1723 // InCall Screen is off and there are no calls
1724 NO_CALLS,
1725
1726 // Incoming-call screen is up
1727 INCOMING,
1728
1729 // In-call experience is showing
1730 INCALL,
1731
1732 // Waiting for user input before placing outgoing call
1733 WAITING_FOR_ACCOUNT,
1734
1735 // UI is starting up but no call has been initiated yet.
1736 // The UI is waiting for Telecom to respond.
1737 PENDING_OUTGOING,
1738
1739 // User is dialing out
1740 OUTGOING;
1741
1742 public boolean isIncoming() {
1743 return (this == INCOMING);
1744 }
1745
1746 public boolean isConnectingOrConnected() {
1747 return (this == INCOMING || this == OUTGOING || this == INCALL);
1748 }
1749 }
1750
1751 /** Interface implemented by classes that need to know about the InCall State. */
1752 public interface InCallStateListener {
1753
1754 // TODO: Enhance state to contain the call objects instead of passing CallList
1755 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1756 }
1757
1758 public interface IncomingCallListener {
1759
1760 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1761 }
1762
1763 public interface CanAddCallListener {
1764
1765 void onCanAddCallChanged(boolean canAddCall);
1766 }
1767
1768 public interface InCallDetailsListener {
1769
1770 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1771 }
1772
1773 public interface InCallOrientationListener {
1774
1775 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1776 }
1777
1778 /**
1779 * Interface implemented by classes that need to know about events which occur within the In-Call
1780 * UI. Used as a means of communicating between fragments that make up the UI.
1781 */
1782 public interface InCallEventListener {
1783
1784 void onFullscreenModeChanged(boolean isFullscreenMode);
1785 }
1786
1787 public interface InCallUiListener {
1788
1789 void onUiShowing(boolean showing);
1790 }
twyen8efb4952017-10-06 16:35:54 -07001791
1792 private class InCallUiLockImpl implements InCallUiLock {
1793 private final String tag;
1794
1795 private InCallUiLockImpl(String tag) {
1796 this.tag = tag;
1797 }
1798
1799 @MainThread
1800 @Override
1801 public void release() {
1802 Assert.isMainThread();
1803 releaseInCallUiLock(InCallUiLockImpl.this);
1804 }
1805
1806 @Override
1807 public String toString() {
1808 return "InCallUiLock[" + tag + "]";
1809 }
1810 }
1811
1812 @MainThread
1813 public InCallUiLock acquireInCallUiLock(String tag) {
1814 Assert.isMainThread();
1815 InCallUiLock lock = new InCallUiLockImpl(tag);
linyuh183cb712017-12-27 17:02:37 -08001816 inCallUiLocks.add(lock);
twyen8efb4952017-10-06 16:35:54 -07001817 return lock;
1818 }
1819
1820 @MainThread
1821 private void releaseInCallUiLock(InCallUiLock lock) {
1822 Assert.isMainThread();
1823 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
linyuh183cb712017-12-27 17:02:37 -08001824 inCallUiLocks.remove(lock);
1825 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07001826 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
linyuh183cb712017-12-27 17:02:37 -08001827 if (inCallState == InCallState.NO_CALLS) {
twyen8efb4952017-10-06 16:35:54 -07001828 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
1829 attemptFinishActivity();
1830 attemptCleanup();
1831 }
1832 }
1833 }
1834
1835 @MainThread
1836 public boolean isInCallUiLocked() {
1837 Assert.isMainThread();
linyuh183cb712017-12-27 17:02:37 -08001838 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07001839 return false;
1840 }
linyuh183cb712017-12-27 17:02:37 -08001841 for (InCallUiLock lock : inCallUiLocks) {
twyen8efb4952017-10-06 16:35:54 -07001842 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
1843 }
1844 return true;
1845 }
1846
linyuh183cb712017-12-27 17:02:37 -08001847 private final Set<InCallUiLock> inCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08001848}