blob: 558ca33fc446c4e8e36d16c36fd5730ca5403c77 [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
yueg10f6e822018-01-17 15:32:18 -08001047 void updateNotification() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001048 // We need to update the notification bar when we leave the UI because that
1049 // could trigger it to show again.
linyuh183cb712017-12-27 17:02:37 -08001050 if (statusBarNotifier != null) {
1051 statusBarNotifier.updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -08001052 }
yueg10f6e822018-01-17 15:32:18 -08001053 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001054
yueg10f6e822018-01-17 15:32:18 -08001055 /** Called when the activity goes in/out of the foreground. */
1056 public void onUiShowing(boolean showing) {
linyuh183cb712017-12-27 17:02:37 -08001057 if (proximitySensor != null) {
1058 proximitySensor.onInCallShowing(showing);
Eric Erfanianccca3152017-02-22 16:32:36 -08001059 }
1060
yueg092b21c2017-11-15 16:20:07 -08001061 if (!showing) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001062 updateIsChangingConfigurations();
1063 }
1064
linyuh183cb712017-12-27 17:02:37 -08001065 for (InCallUiListener listener : inCallUiListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001066 listener.onUiShowing(showing);
1067 }
1068
linyuh183cb712017-12-27 17:02:37 -08001069 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001070 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001071 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001072 }
1073 }
1074
Eric Erfanian2ca43182017-08-31 06:57:16 -07001075 public void refreshUi() {
linyuh183cb712017-12-27 17:02:37 -08001076 if (inCallActivity != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001077 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001078 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001079 }
1080 }
1081
Eric Erfanianccca3152017-02-22 16:32:36 -08001082 public void addInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001083 inCallUiListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001084 }
1085
1086 public boolean removeInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001087 return inCallUiListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001088 }
1089
1090 /*package*/
1091 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001092 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001093 notifyVideoPauseController(true);
Eric Erfanian2ca43182017-08-31 06:57:16 -07001094 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001095 }
1096
1097 /*package*/
1098 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001099 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001100 notifyVideoPauseController(false);
1101 }
1102
1103 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001104 LogUtil.d(
1105 "InCallPresenter.notifyVideoPauseController",
linyuh183cb712017-12-27 17:02:37 -08001106 "mIsChangingConfigurations=" + isChangingConfigurations);
1107 if (!isChangingConfigurations) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001108 VideoPauseController.getInstance().onUiShowing(showing);
1109 }
1110 }
1111
1112 /** Brings the app into the foreground if possible. */
1113 public void bringToForeground(boolean showDialpad) {
1114 // Before we bring the incall UI to the foreground, we check to see if:
1115 // 1. It is not currently in the foreground
1116 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1117 // be displayed)
1118 // If the activity hadn't actually been started previously, yet there are still calls
1119 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1120 // bring it up the UI regardless.
linyuh183cb712017-12-27 17:02:37 -08001121 if (!isShowingInCallUi() && inCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001122 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001123 }
1124 }
1125
1126 public void onPostDialCharWait(String callId, String chars) {
1127 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001128 inCallActivity.showDialogForPostCharWait(callId, chars);
Eric Erfanianccca3152017-02-22 16:32:36 -08001129 }
1130 }
1131
1132 /**
1133 * Handles the green CALL key while in-call.
1134 *
1135 * @return true if we consumed the event.
1136 */
1137 public boolean handleCallKey() {
1138 LogUtil.v("InCallPresenter.handleCallKey", null);
1139
1140 // The green CALL button means either "Answer", "Unhold", or
1141 // "Swap calls", or can be a no-op, depending on the current state
1142 // of the Phone.
1143
1144 /** INCOMING CALL */
linyuh183cb712017-12-27 17:02:37 -08001145 final CallList calls = callList;
Eric Erfanianccca3152017-02-22 16:32:36 -08001146 final DialerCall incomingCall = calls.getIncomingCall();
1147 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1148
1149 // (1) Attempt to answer a call
1150 if (incomingCall != null) {
1151 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1152 return true;
1153 }
1154
1155 /** STATE_ACTIVE CALL */
1156 final DialerCall activeCall = calls.getActiveCall();
1157 if (activeCall != null) {
1158 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1159 // consolidate this logic.
1160 final boolean canMerge =
1161 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1162 final boolean canSwap =
1163 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1164
Eric Erfanian2ca43182017-08-31 06:57:16 -07001165 LogUtil.v(
1166 "InCallPresenter.handleCallKey",
1167 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001168
1169 // (2) Attempt actions on conference calls
1170 if (canMerge) {
1171 TelecomAdapter.getInstance().merge(activeCall.getId());
1172 return true;
1173 } else if (canSwap) {
1174 TelecomAdapter.getInstance().swap(activeCall.getId());
1175 return true;
1176 }
1177 }
1178
1179 /** BACKGROUND CALL */
1180 final DialerCall heldCall = calls.getBackgroundCall();
1181 if (heldCall != null) {
1182 // We have a hold call so presumeable it will always support HOLD...but
1183 // there is no harm in double checking.
1184 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1185
Eric Erfanian2ca43182017-08-31 06:57:16 -07001186 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001187
1188 // (4) unhold call
1189 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1190 heldCall.unhold();
1191 return true;
1192 }
1193 }
1194
1195 // Always consume hard keys
1196 return true;
1197 }
1198
Eric Erfanianccca3152017-02-22 16:32:36 -08001199 /** Clears the previous fullscreen state. */
1200 public void clearFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001201 isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001202 }
1203
1204 /**
1205 * Changes the fullscreen mode of the in-call UI.
1206 *
1207 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1208 * otherwise.
1209 */
1210 public void setFullScreen(boolean isFullScreen) {
1211 setFullScreen(isFullScreen, false /* force */);
1212 }
1213
1214 /**
1215 * Changes the fullscreen mode of the in-call UI.
1216 *
1217 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1218 * otherwise.
1219 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1220 */
1221 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001222 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001223
1224 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1225 if (isDialpadVisible()) {
1226 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001227 LogUtil.v(
1228 "InCallPresenter.setFullScreen",
1229 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001230 }
1231
linyuh183cb712017-12-27 17:02:37 -08001232 if (this.isFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001233 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001234 return;
1235 }
linyuh183cb712017-12-27 17:02:37 -08001236 this.isFullScreen = isFullScreen;
1237 notifyFullscreenModeChange(this.isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001238 }
1239
1240 /**
1241 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1242 * otherwise.
1243 */
1244 public boolean isFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001245 return isFullScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001246 }
1247
1248 /**
1249 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1250 *
1251 * @param isFullscreenMode {@code True} if entering full screen mode.
1252 */
1253 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001254 for (InCallEventListener listener : inCallEventListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001255 listener.onFullscreenModeChanged(isFullscreenMode);
1256 }
1257 }
1258
linyuh7b86f562017-11-16 11:24:09 -08001259 /** Instruct the in-call activity to show an error dialog or toast for a disconnected call. */
1260 private void showDialogOrToastForDisconnectedCall(DialerCall call) {
1261 if (!isActivityStarted() || call.getState() != DialerCall.State.DISCONNECTED) {
1262 return;
Eric Erfanianccca3152017-02-22 16:32:36 -08001263 }
linyuh7b86f562017-11-16 11:24:09 -08001264
1265 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1266 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1267 setDisconnectCauseForMissingAccounts(call);
1268 }
1269
linyuh183cb712017-12-27 17:02:37 -08001270 inCallActivity.showDialogOrToastForDisconnectedCall(
1271 new DisconnectMessage(inCallActivity, call));
Eric Erfanianccca3152017-02-22 16:32:36 -08001272 }
1273
1274 /**
1275 * When the state of in-call changes, this is the first method to get called. It determines if the
1276 * UI needs to be started or finished depending on the new state and does it.
1277 */
1278 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001279 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001280 LogUtil.d(
linyuh183cb712017-12-27 17:02:37 -08001281 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + inCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001282
1283 // TODO: Consider a proper state machine implementation
1284
1285 // If the state isn't changing we have already done any starting/stopping of activities in
1286 // a previous pass...so lets cut out early
linyuh183cb712017-12-27 17:02:37 -08001287 if (newState == inCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001288 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001289 return newState;
1290 }
1291
1292 // A new Incoming call means that the user needs to be notified of the the call (since
1293 // it wasn't them who initiated it). We do this through full screen notifications and
1294 // happens indirectly through {@link StatusBarNotifier}.
1295 //
1296 // The process for incoming calls is as follows:
1297 //
1298 // 1) CallList - Announces existence of new INCOMING call
1299 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1300 // - should be set to INCOMING.
1301 // 3) InCallPresenter - This method is called to see if we need to start or finish
1302 // the app given the new state.
1303 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1304 // StatusBarNotifier explicitly to issue a FullScreen Notification
1305 // that will either start the InCallActivity or show the user a
1306 // top-level notification dialog if the user is in an immersive app.
1307 // That notification can also start the InCallActivity.
1308 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1309 // call InCallPresenter::setActivity() to let the presenter
1310 // know that start-up is complete.
1311 //
1312 // [ AND NOW YOU'RE IN THE CALL. voila! ]
1313 //
1314 // Our app is started using a fullScreen notification. We need to do this whenever
1315 // we get an incoming call. Depending on the current context of the device, either a
1316 // incoming call HUN or the actual InCallActivity will be shown.
1317 final boolean startIncomingCallSequence = (InCallState.INCOMING == newState);
1318
1319 // A dialog to show on top of the InCallUI to select a PhoneAccount
1320 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1321
1322 // A new outgoing call indicates that the user just now dialed a number and when that
1323 // happens we need to display the screen immediately or show an account picker dialog if
1324 // no default is set. However, if the main InCallUI is already visible, we do not want to
1325 // re-initiate the start-up animation, so we do not need to do anything here.
1326 //
1327 // It is also possible to go into an intermediate state where the call has been initiated
1328 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1329 // This pending outgoing state can also launch the call screen.
1330 //
1331 // This is different from the incoming call sequence because we do not need to shock the
1332 // user with a top-level notification. Just show the call UI normally.
1333 boolean callCardFragmentVisible =
linyuh183cb712017-12-27 17:02:37 -08001334 inCallActivity != null && inCallActivity.getCallCardFragmentVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001335 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1336 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1337
1338 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1339 // outgoing call process, so the UI should be brought up to show an error dialog.
1340 showCallUi |=
linyuh183cb712017-12-27 17:02:37 -08001341 (InCallState.PENDING_OUTGOING == inCallState
Eric Erfanianccca3152017-02-22 16:32:36 -08001342 && InCallState.INCALL == newState
1343 && !isShowingInCallUi());
1344
1345 // Another exception - InCallActivity is in charge of disconnecting a call with no
1346 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1347 // call so that:
1348 // 1) The call can be disconnected correctly
1349 // 2) The UI comes up and correctly displays the error dialog.
1350 // TODO: Remove these special case conditions by making InCallPresenter a true state
1351 // machine. Telecom should also be the component responsible for disconnecting a call
1352 // with no valid accounts.
1353 showCallUi |=
1354 InCallState.PENDING_OUTGOING == newState
1355 && mainUiNotVisible
linyuh183cb712017-12-27 17:02:37 -08001356 && isCallWithNoValidAccounts(callList.getPendingOutgoingCall());
Eric Erfanianccca3152017-02-22 16:32:36 -08001357
1358 // The only time that we have an instance of mInCallActivity and it isn't started is
1359 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1360 // the activity. When it is finally destroyed, we double check if we should bring it back
1361 // up so we aren't going to lose anything by avoiding a second startup here.
linyuh183cb712017-12-27 17:02:37 -08001362 boolean activityIsFinishing = inCallActivity != null && !isActivityStarted();
Eric Erfanianccca3152017-02-22 16:32:36 -08001363 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001364 LogUtil.i(
1365 "InCallPresenter.startOrFinishUi",
linyuh183cb712017-12-27 17:02:37 -08001366 "Undo the state change: " + newState + " -> " + inCallState);
wangqicf61ca02017-08-31 15:32:55 -07001367 Trace.endSection();
linyuh183cb712017-12-27 17:02:37 -08001368 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001369 }
1370
1371 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1372 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1373 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1374 // outgoing call after user ignore it by pressing home button.
1375 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1376 && !showCallUi
1377 && isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001378 inCallActivity.dismissPendingDialogs();
Eric Erfanianccca3152017-02-22 16:32:36 -08001379 }
1380
1381 if (showCallUi || showAccountPicker) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001382 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001383 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001384 } else if (startIncomingCallSequence) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001385 LogUtil.i("InCallPresenter.startOrFinishUi", "Start Full Screen in call UI");
Eric Erfanianccca3152017-02-22 16:32:36 -08001386
linyuh183cb712017-12-27 17:02:37 -08001387 statusBarNotifier.updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -08001388 } else if (newState == InCallState.NO_CALLS) {
1389 // The new state is the no calls state. Tear everything down.
1390 attemptFinishActivity();
1391 attemptCleanup();
1392 }
1393
wangqicf61ca02017-08-31 15:32:55 -07001394 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001395 return newState;
1396 }
1397
1398 /**
1399 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1400 * or PhoneAccounts to select from.
1401 */
1402 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1403
1404 Bundle extras = call.getIntentExtras();
1405 // Initialize the extras bundle to avoid NPE
1406 if (extras == null) {
1407 extras = new Bundle();
1408 }
1409
1410 final List<PhoneAccountHandle> phoneAccountHandles =
1411 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1412
1413 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1414 String scheme = call.getHandle().getScheme();
1415 final String errorMsg =
1416 PhoneAccount.SCHEME_TEL.equals(scheme)
linyuh183cb712017-12-27 17:02:37 -08001417 ? context.getString(R.string.callFailed_simError)
1418 : context.getString(R.string.incall_error_supp_service_unknown);
Eric Erfanianccca3152017-02-22 16:32:36 -08001419 DisconnectCause disconnectCause =
1420 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1421 call.setDisconnectCause(disconnectCause);
1422 }
1423 }
1424
Eric Erfanianccca3152017-02-22 16:32:36 -08001425 /**
1426 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1427 * Calling classes should use this as an indication whether to interact with the
1428 * InCallPresenter or not.
1429 */
1430 public boolean isReadyForTearDown() {
linyuh183cb712017-12-27 17:02:37 -08001431 return inCallActivity == null && !serviceConnected && inCallState == InCallState.NO_CALLS;
Eric Erfanianccca3152017-02-22 16:32:36 -08001432 }
1433
1434 /**
1435 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1436 */
1437 private void attemptCleanup() {
1438 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001439 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001440
1441 cleanupSurfaces();
1442
linyuh183cb712017-12-27 17:02:37 -08001443 isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001444
1445 // blow away stale contact info so that we get fresh data on
1446 // the next set of calls
linyuh183cb712017-12-27 17:02:37 -08001447 if (contactInfoCache != null) {
1448 contactInfoCache.clearCache();
Eric Erfanianccca3152017-02-22 16:32:36 -08001449 }
linyuh183cb712017-12-27 17:02:37 -08001450 contactInfoCache = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001451
linyuh183cb712017-12-27 17:02:37 -08001452 if (proximitySensor != null) {
1453 removeListener(proximitySensor);
1454 proximitySensor.tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -08001455 }
linyuh183cb712017-12-27 17:02:37 -08001456 proximitySensor = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001457
linyuh183cb712017-12-27 17:02:37 -08001458 if (statusBarNotifier != null) {
1459 removeListener(statusBarNotifier);
1460 EnrichedCallComponent.get(context)
Eric Erfaniand8046e52017-04-06 09:41:50 -07001461 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -08001462 .unregisterStateChangedListener(statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001463 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001464
linyuh183cb712017-12-27 17:02:37 -08001465 if (externalCallNotifier != null && externalCallList != null) {
1466 externalCallList.removeExternalCallListener(externalCallNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001467 }
linyuh183cb712017-12-27 17:02:37 -08001468 statusBarNotifier = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001469
linyuh183cb712017-12-27 17:02:37 -08001470 if (callList != null) {
1471 callList.removeListener(this);
1472 callList.removeListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001473 }
linyuh183cb712017-12-27 17:02:37 -08001474 callList = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001475
linyuh183cb712017-12-27 17:02:37 -08001476 context = null;
1477 inCallActivity = null;
1478 manageConferenceActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001479
linyuh183cb712017-12-27 17:02:37 -08001480 listeners.clear();
1481 incomingCallListeners.clear();
1482 detailsListeners.clear();
1483 canAddCallListeners.clear();
1484 orientationListeners.clear();
1485 inCallEventListeners.clear();
1486 inCallUiListeners.clear();
1487 if (!inCallUiLocks.isEmpty()) {
1488 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + inCallUiLocks);
1489 inCallUiLocks.clear();
twyen8efb4952017-10-06 16:35:54 -07001490 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001491 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001492 }
1493 }
1494
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001495 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001496 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
linyuh183cb712017-12-27 17:02:37 -08001497 context.startActivity(
1498 InCallActivity.getIntent(context, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001499 }
1500
1501 public void onServiceBind() {
linyuh183cb712017-12-27 17:02:37 -08001502 serviceBound = true;
Eric Erfanianccca3152017-02-22 16:32:36 -08001503 }
1504
1505 public void onServiceUnbind() {
1506 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -08001507 serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001508 }
1509
1510 public boolean isServiceBound() {
linyuh183cb712017-12-27 17:02:37 -08001511 return serviceBound;
Eric Erfanianccca3152017-02-22 16:32:36 -08001512 }
1513
1514 public void maybeStartRevealAnimation(Intent intent) {
linyuh183cb712017-12-27 17:02:37 -08001515 if (intent == null || inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001516 return;
1517 }
1518 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1519 if (extras == null) {
1520 // Incoming call, just show the in-call UI directly.
1521 return;
1522 }
1523
1524 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1525 // Account selection dialog will show up so don't show the animation.
1526 return;
1527 }
1528
1529 final PhoneAccountHandle accountHandle =
1530 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1531 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001532
1533 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(true, accountHandle);
1534
1535 final Intent activityIntent =
linyuh183cb712017-12-27 17:02:37 -08001536 InCallActivity.getIntent(context, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001537 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
linyuh183cb712017-12-27 17:02:37 -08001538 context.startActivity(activityIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001539 }
1540
1541 /**
1542 * Retrieves the current in-call camera manager instance, creating if necessary.
1543 *
1544 * @return The {@link InCallCameraManager}.
1545 */
1546 public InCallCameraManager getInCallCameraManager() {
1547 synchronized (this) {
linyuh183cb712017-12-27 17:02:37 -08001548 if (inCallCameraManager == null) {
1549 inCallCameraManager = new InCallCameraManager(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001550 }
1551
linyuh183cb712017-12-27 17:02:37 -08001552 return inCallCameraManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001553 }
1554 }
1555
1556 /**
1557 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1558 *
1559 * @param orientation The screen orientation of the device (one of: {@link
1560 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1561 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1562 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1563 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1564 */
1565 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001566 LogUtil.d(
1567 "InCallPresenter.onDeviceOrientationChange",
1568 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001569
linyuh183cb712017-12-27 17:02:37 -08001570 if (callList != null) {
1571 callList.notifyCallsOfDeviceRotation(orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001572 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001573 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001574 }
1575
1576 // Notify listeners of device orientation changed.
linyuh183cb712017-12-27 17:02:37 -08001577 for (InCallOrientationListener listener : orientationListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001578 listener.onDeviceOrientationChanged(orientation);
1579 }
1580 }
1581
1582 /**
1583 * Configures the in-call UI activity so it can change orientations or not. Enables the
1584 * orientation event listener if allowOrientationChange is true, disables it if false.
1585 *
1586 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1587 * landscape. {@code false} if the in-call UI should be locked in portrait.
1588 */
1589 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
linyuh183cb712017-12-27 17:02:37 -08001590 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001591 LogUtil.e(
1592 "InCallPresenter.setInCallAllowsOrientationChange",
1593 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001594 return;
1595 }
linyuh183cb712017-12-27 17:02:37 -08001596 inCallActivity.setAllowOrientationChange(allowOrientationChange);
Eric Erfanianccca3152017-02-22 16:32:36 -08001597 }
1598
1599 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001600 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
linyuh183cb712017-12-27 17:02:37 -08001601 screenTimeoutEnabled = enable;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001602 applyScreenTimeout();
1603 }
1604
1605 private void applyScreenTimeout() {
linyuh183cb712017-12-27 17:02:37 -08001606 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001607 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001608 return;
1609 }
1610
linyuh183cb712017-12-27 17:02:37 -08001611 final Window window = inCallActivity.getWindow();
1612 if (screenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001613 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1614 } else {
1615 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1616 }
1617 }
1618
1619 /**
1620 * Hides or shows the conference manager fragment.
1621 *
1622 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1623 * be hidden.
1624 */
1625 public void showConferenceCallManager(boolean show) {
linyuh183cb712017-12-27 17:02:37 -08001626 if (inCallActivity != null) {
1627 inCallActivity.showConferenceFragment(show);
Eric Erfanianccca3152017-02-22 16:32:36 -08001628 }
linyuh183cb712017-12-27 17:02:37 -08001629 if (!show && manageConferenceActivity != null) {
1630 manageConferenceActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -08001631 }
1632 }
1633
1634 /**
1635 * Determines if the dialpad is visible.
1636 *
1637 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1638 */
1639 public boolean isDialpadVisible() {
linyuh183cb712017-12-27 17:02:37 -08001640 if (inCallActivity == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001641 return false;
1642 }
linyuh183cb712017-12-27 17:02:37 -08001643 return inCallActivity.isDialpadVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001644 }
1645
1646 public ThemeColorManager getThemeColorManager() {
linyuh183cb712017-12-27 17:02:37 -08001647 return themeColorManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001648 }
1649
wangqi8d662ca2017-10-26 11:27:19 -07001650 @VisibleForTesting
1651 public void setThemeColorManager(ThemeColorManager themeColorManager) {
linyuh183cb712017-12-27 17:02:37 -08001652 this.themeColorManager = themeColorManager;
wangqi8d662ca2017-10-26 11:27:19 -07001653 }
1654
Eric Erfanianccca3152017-02-22 16:32:36 -08001655 /** Called when the foreground call changes. */
1656 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh183cb712017-12-27 17:02:37 -08001657 themeColorManager.onForegroundCallChanged(context, newForegroundCall);
1658 if (inCallActivity != null) {
1659 inCallActivity.onForegroundCallChanged(newForegroundCall);
Eric Erfanianccca3152017-02-22 16:32:36 -08001660 }
1661 }
1662
1663 public InCallActivity getActivity() {
linyuh183cb712017-12-27 17:02:37 -08001664 return inCallActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -08001665 }
1666
1667 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1668 public void setActivity(InCallActivity inCallActivity) {
1669 if (inCallActivity == null) {
1670 throw new IllegalArgumentException("registerActivity cannot be called with null");
1671 }
linyuh183cb712017-12-27 17:02:37 -08001672 if (this.inCallActivity != null && this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001673 LogUtil.w(
1674 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001675 }
1676 updateActivity(inCallActivity);
1677 }
1678
1679 ExternalCallNotifier getExternalCallNotifier() {
linyuh183cb712017-12-27 17:02:37 -08001680 return externalCallNotifier;
Eric Erfanianccca3152017-02-22 16:32:36 -08001681 }
1682
1683 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001684 if (localVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001685 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001686 if (context != null) {
1687 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001688 }
linyuh183cb712017-12-27 17:02:37 -08001689 localVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001690 }
linyuh183cb712017-12-27 17:02:37 -08001691 return localVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001692 }
1693
1694 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001695 if (remoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001696 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001697 if (context != null) {
1698 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001699 }
linyuh183cb712017-12-27 17:02:37 -08001700 remoteVideoSurfaceTexture = VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001701 }
linyuh183cb712017-12-27 17:02:37 -08001702 return remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001703 }
1704
1705 void cleanupSurfaces() {
linyuh183cb712017-12-27 17:02:37 -08001706 if (remoteVideoSurfaceTexture != null) {
1707 remoteVideoSurfaceTexture.setDoneWithSurface();
1708 remoteVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001709 }
linyuh183cb712017-12-27 17:02:37 -08001710 if (localVideoSurfaceTexture != null) {
1711 localVideoSurfaceTexture.setDoneWithSurface();
1712 localVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001713 }
1714 }
1715
yueg77cb8e52017-10-27 16:42:51 -07001716 @Override
1717 public void onAudioStateChanged(CallAudioState audioState) {
linyuh183cb712017-12-27 17:02:37 -08001718 if (statusBarNotifier != null) {
1719 statusBarNotifier.updateNotification();
yueg77cb8e52017-10-27 16:42:51 -07001720 }
1721 }
1722
Eric Erfanianccca3152017-02-22 16:32:36 -08001723 /** All the main states of InCallActivity. */
1724 public enum InCallState {
1725 // InCall Screen is off and there are no calls
1726 NO_CALLS,
1727
1728 // Incoming-call screen is up
1729 INCOMING,
1730
1731 // In-call experience is showing
1732 INCALL,
1733
1734 // Waiting for user input before placing outgoing call
1735 WAITING_FOR_ACCOUNT,
1736
1737 // UI is starting up but no call has been initiated yet.
1738 // The UI is waiting for Telecom to respond.
1739 PENDING_OUTGOING,
1740
1741 // User is dialing out
1742 OUTGOING;
1743
1744 public boolean isIncoming() {
1745 return (this == INCOMING);
1746 }
1747
1748 public boolean isConnectingOrConnected() {
1749 return (this == INCOMING || this == OUTGOING || this == INCALL);
1750 }
1751 }
1752
1753 /** Interface implemented by classes that need to know about the InCall State. */
1754 public interface InCallStateListener {
1755
1756 // TODO: Enhance state to contain the call objects instead of passing CallList
1757 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1758 }
1759
1760 public interface IncomingCallListener {
1761
1762 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1763 }
1764
1765 public interface CanAddCallListener {
1766
1767 void onCanAddCallChanged(boolean canAddCall);
1768 }
1769
1770 public interface InCallDetailsListener {
1771
1772 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1773 }
1774
1775 public interface InCallOrientationListener {
1776
1777 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1778 }
1779
1780 /**
1781 * Interface implemented by classes that need to know about events which occur within the In-Call
1782 * UI. Used as a means of communicating between fragments that make up the UI.
1783 */
1784 public interface InCallEventListener {
1785
1786 void onFullscreenModeChanged(boolean isFullscreenMode);
1787 }
1788
1789 public interface InCallUiListener {
1790
1791 void onUiShowing(boolean showing);
1792 }
twyen8efb4952017-10-06 16:35:54 -07001793
1794 private class InCallUiLockImpl implements InCallUiLock {
1795 private final String tag;
1796
1797 private InCallUiLockImpl(String tag) {
1798 this.tag = tag;
1799 }
1800
1801 @MainThread
1802 @Override
1803 public void release() {
1804 Assert.isMainThread();
1805 releaseInCallUiLock(InCallUiLockImpl.this);
1806 }
1807
1808 @Override
1809 public String toString() {
1810 return "InCallUiLock[" + tag + "]";
1811 }
1812 }
1813
1814 @MainThread
1815 public InCallUiLock acquireInCallUiLock(String tag) {
1816 Assert.isMainThread();
1817 InCallUiLock lock = new InCallUiLockImpl(tag);
linyuh183cb712017-12-27 17:02:37 -08001818 inCallUiLocks.add(lock);
twyen8efb4952017-10-06 16:35:54 -07001819 return lock;
1820 }
1821
1822 @MainThread
1823 private void releaseInCallUiLock(InCallUiLock lock) {
1824 Assert.isMainThread();
1825 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
linyuh183cb712017-12-27 17:02:37 -08001826 inCallUiLocks.remove(lock);
1827 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07001828 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
linyuh183cb712017-12-27 17:02:37 -08001829 if (inCallState == InCallState.NO_CALLS) {
twyen8efb4952017-10-06 16:35:54 -07001830 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
1831 attemptFinishActivity();
1832 attemptCleanup();
1833 }
1834 }
1835 }
1836
1837 @MainThread
1838 public boolean isInCallUiLocked() {
1839 Assert.isMainThread();
linyuh183cb712017-12-27 17:02:37 -08001840 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07001841 return false;
1842 }
linyuh183cb712017-12-27 17:02:37 -08001843 for (InCallUiLock lock : inCallUiLocks) {
twyen8efb4952017-10-06 16:35:54 -07001844 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
1845 }
1846 return true;
1847 }
1848
linyuh183cb712017-12-27 17:02:37 -08001849 private final Set<InCallUiLock> inCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08001850}