blob: 8535cbc3ec87784d1c487fdede33e51ed968f7a6 [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;
31import android.telecom.DisconnectCause;
32import android.telecom.PhoneAccount;
33import android.telecom.PhoneAccountHandle;
34import android.telecom.TelecomManager;
35import android.telecom.VideoProfile;
36import android.telephony.PhoneStateListener;
37import android.telephony.TelephonyManager;
twyen8efb4952017-10-06 16:35:54 -070038import android.util.ArraySet;
Eric Erfanianccca3152017-02-22 16:32:36 -080039import android.view.Window;
40import android.view.WindowManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080041import com.android.contacts.common.compat.CallCompat;
42import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler;
43import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler.OnCheckBlockedListener;
Eric Erfanian83b20212017-05-31 08:53:10 -070044import com.android.dialer.blocking.FilteredNumberCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080045import com.android.dialer.blocking.FilteredNumbersUtil;
twyen8efb4952017-10-06 16:35:54 -070046import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080047import com.android.dialer.common.LogUtil;
zachh6a4cebd2017-10-24 17:10:06 -070048import com.android.dialer.common.concurrent.DialerExecutorComponent;
Eric Erfaniand8046e52017-04-06 09:41:50 -070049import com.android.dialer.enrichedcall.EnrichedCallComponent;
Eric Erfanian10b34a52017-05-04 08:23:17 -070050import com.android.dialer.location.GeoUtil;
Eric Erfanian8369df02017-05-03 10:27:13 -070051import com.android.dialer.logging.InteractionEvent;
Eric Erfanianccca3152017-02-22 16:32:36 -080052import com.android.dialer.logging.Logger;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070053import com.android.dialer.postcall.PostCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080054import com.android.dialer.telecom.TelecomUtil;
55import com.android.dialer.util.TouchPointManager;
56import com.android.incallui.InCallOrientationEventListener.ScreenOrientation;
57import com.android.incallui.answerproximitysensor.PseudoScreenState;
58import com.android.incallui.call.CallList;
59import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080060import com.android.incallui.call.ExternalCallList;
Eric Erfanianccca3152017-02-22 16:32:36 -080061import com.android.incallui.call.TelecomAdapter;
Eric Erfanian2ca43182017-08-31 06:57:16 -070062import com.android.incallui.disconnectdialog.DisconnectMessage;
twyen8efb4952017-10-06 16:35:54 -070063import com.android.incallui.incalluilock.InCallUiLock;
Eric Erfanianccca3152017-02-22 16:32:36 -080064import com.android.incallui.latencyreport.LatencyReport;
65import com.android.incallui.legacyblocking.BlockedNumberContentObserver;
66import com.android.incallui.spam.SpamCallListListener;
67import com.android.incallui.util.TelecomCallUtil;
68import com.android.incallui.videosurface.bindings.VideoSurfaceBindings;
69import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
Eric Erfanian90508232017-03-24 09:31:16 -070070import com.android.incallui.videotech.utils.VideoUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080071import java.util.Collections;
72import java.util.List;
73import java.util.Objects;
74import java.util.Set;
75import java.util.concurrent.ConcurrentHashMap;
76import java.util.concurrent.CopyOnWriteArrayList;
77import java.util.concurrent.atomic.AtomicBoolean;
78
79/**
80 * Takes updates from the CallList and notifies the InCallActivity (UI) of the changes. Responsible
81 * for starting the activity for a new call and finishing the activity when all calls are
82 * disconnected. Creates and manages the in-call state and provides a listener pattern for the
83 * presenters that want to listen in on the in-call state changes. TODO: This class has become more
84 * of a state machine at this point. Consider renaming.
85 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -070086public class InCallPresenter implements CallList.Listener {
Eric Erfanian2ca43182017-08-31 06:57:16 -070087 private static final String PIXEL2017_SYSTEM_FEATURE =
88 "com.google.android.feature.PIXEL_2017_EXPERIENCE";
Eric Erfanianccca3152017-02-22 16:32:36 -080089 private static final String EXTRA_FIRST_TIME_SHOWN =
90 "com.android.incallui.intent.extra.FIRST_TIME_SHOWN";
91
92 private static final long BLOCK_QUERY_TIMEOUT_MS = 1000;
93
94 private static final Bundle EMPTY_EXTRAS = new Bundle();
95
96 private static InCallPresenter sInCallPresenter;
97
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 */
102 private final Set<InCallStateListener> mListeners =
103 Collections.newSetFromMap(new ConcurrentHashMap<InCallStateListener, Boolean>(8, 0.9f, 1));
104
105 private final List<IncomingCallListener> mIncomingCallListeners = new CopyOnWriteArrayList<>();
106 private final Set<InCallDetailsListener> mDetailsListeners =
107 Collections.newSetFromMap(new ConcurrentHashMap<InCallDetailsListener, Boolean>(8, 0.9f, 1));
108 private final Set<CanAddCallListener> mCanAddCallListeners =
109 Collections.newSetFromMap(new ConcurrentHashMap<CanAddCallListener, Boolean>(8, 0.9f, 1));
110 private final Set<InCallUiListener> mInCallUiListeners =
111 Collections.newSetFromMap(new ConcurrentHashMap<InCallUiListener, Boolean>(8, 0.9f, 1));
112 private final Set<InCallOrientationListener> mOrientationListeners =
113 Collections.newSetFromMap(
114 new ConcurrentHashMap<InCallOrientationListener, Boolean>(8, 0.9f, 1));
115 private final Set<InCallEventListener> mInCallEventListeners =
116 Collections.newSetFromMap(new ConcurrentHashMap<InCallEventListener, Boolean>(8, 0.9f, 1));
117
118 private StatusBarNotifier mStatusBarNotifier;
119 private ExternalCallNotifier mExternalCallNotifier;
120 private ContactInfoCache mContactInfoCache;
121 private Context mContext;
122 private final OnCheckBlockedListener mOnCheckBlockedListener =
123 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.
129 TelecomUtil.silenceRinger(mContext);
130 }
131 }
132 };
133 private CallList mCallList;
134 private ExternalCallList mExternalCallList;
135 private InCallActivity mInCallActivity;
136 private ManageConferenceActivity mManageConferenceActivity;
137 private final android.telecom.Call.Callback mCallCallback =
138 new android.telecom.Call.Callback() {
139 @Override
140 public void onPostDialWait(
141 android.telecom.Call telecomCall, String remainingPostDialSequence) {
142 final DialerCall call = mCallList.getDialerCallFromTelecomCall(telecomCall);
143 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) {
155 final DialerCall call = mCallList.getDialerCallFromTelecomCall(telecomCall);
156 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)
164 && !mExternalCallList.isCallTracked(telecomCall)) {
165
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);
Eric Erfanianccca3152017-02-22 16:32:36 -0800168 mCallList.onInternalCallMadeExternal(mContext, telecomCall);
169 mExternalCallList.onCallAdded(telecomCall);
170 return;
171 }
172
173 for (InCallDetailsListener listener : mDetailsListeners) {
174 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 };
187 private InCallState mInCallState = InCallState.NO_CALLS;
188 private ProximitySensor mProximitySensor;
189 private final PseudoScreenState mPseudoScreenState = new PseudoScreenState();
190 private boolean mServiceConnected;
Eric Erfanianccca3152017-02-22 16:32:36 -0800191 private InCallCameraManager mInCallCameraManager;
192 private FilteredNumberAsyncQueryHandler mFilteredQueryHandler;
193 private CallList.Listener mSpamCallListListener;
194 /** Whether or not we are currently bound and waiting for Telecom to send us a new call. */
195 private boolean mBoundAndWaitingForOutgoingCall;
196 /** Determines if the InCall UI is in fullscreen mode or not. */
197 private boolean mIsFullScreen = false;
198
Eric Erfanian2ca43182017-08-31 06:57:16 -0700199 private boolean mScreenTimeoutEnabled = true;
200
Eric Erfanianccca3152017-02-22 16:32:36 -0800201 private PhoneStateListener mPhoneStateListener =
202 new PhoneStateListener() {
203 @Override
204 public void onCallStateChanged(int state, String incomingNumber) {
205 if (state == TelephonyManager.CALL_STATE_RINGING) {
206 if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
207 return;
208 }
209 // Check if the number is blocked, to silence the ringer.
210 String countryIso = GeoUtil.getCurrentCountryIso(mContext);
211 mFilteredQueryHandler.isBlockedNumber(
212 mOnCheckBlockedListener, incomingNumber, countryIso);
213 }
214 }
215 };
216 /**
217 * Is true when the activity has been previously started. Some code needs to know not just if the
218 * activity is currently up, but if it had been previously shown in foreground for this in-call
219 * session (e.g., StatusBarNotifier). This gets reset when the session ends in the tear-down
220 * method.
221 */
222 private boolean mIsActivityPreviouslyStarted = false;
223
224 /** Whether or not InCallService is bound to Telecom. */
225 private boolean mServiceBound = false;
226
227 /**
228 * When configuration changes Android kills the current activity and starts a new one. The flag is
229 * used to check if full clean up is necessary (activity is stopped and new activity won't be
230 * started), or if a new activity will be started right after the current one is destroyed, and
231 * therefore no need in release all resources.
232 */
233 private boolean mIsChangingConfigurations = false;
234
235 private boolean mAwaitingCallListUpdate = false;
236
237 private ExternalCallList.ExternalCallListener mExternalCallListener =
238 new ExternalCallList.ExternalCallListener() {
239
240 @Override
241 public void onExternalCallPulled(android.telecom.Call call) {
242 // Note: keep this code in sync with InCallPresenter#onCallAdded
243 LatencyReport latencyReport = new LatencyReport(call);
244 latencyReport.onCallBlockingDone();
245 // Note: External calls do not require spam checking.
246 mCallList.onCallAdded(mContext, call, latencyReport);
247 call.registerCallback(mCallCallback);
248 }
249
250 @Override
251 public void onExternalCallAdded(android.telecom.Call call) {
252 // No-op
253 }
254
255 @Override
256 public void onExternalCallRemoved(android.telecom.Call call) {
257 // No-op
258 }
259
260 @Override
261 public void onExternalCallUpdated(android.telecom.Call call) {
262 // No-op
263 }
264 };
265
266 private ThemeColorManager mThemeColorManager;
267 private VideoSurfaceTexture mLocalVideoSurfaceTexture;
268 private VideoSurfaceTexture mRemoteVideoSurfaceTexture;
269
Eric Erfanian10b34a52017-05-04 08:23:17 -0700270 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800271 @VisibleForTesting
272 InCallPresenter() {}
273
274 public static synchronized InCallPresenter getInstance() {
275 if (sInCallPresenter == null) {
wangqic8cf79e2017-10-17 09:21:00 -0700276 Trace.beginSection("InCallPresenter.Constructor");
Eric Erfanianccca3152017-02-22 16:32:36 -0800277 sInCallPresenter = new InCallPresenter();
wangqic8cf79e2017-10-17 09:21:00 -0700278 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800279 }
280 return sInCallPresenter;
281 }
282
Eric Erfanian10b34a52017-05-04 08:23:17 -0700283 @VisibleForTesting
284 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
285 sInCallPresenter = inCallPresenter;
286 }
287
Eric Erfanianccca3152017-02-22 16:32:36 -0800288 /**
289 * Determines whether or not a call has no valid phone accounts that can be used to make the call
290 * with. Emergency calls do not require a phone account.
291 *
292 * @param call to check accounts for.
293 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
294 * call contains a phone account that could be used to initiate it with, or is an emergency
295 * call.
296 */
297 public static boolean isCallWithNoValidAccounts(DialerCall call) {
298 if (call != null && !call.isEmergencyCall()) {
299 Bundle extras = call.getIntentExtras();
300
301 if (extras == null) {
302 extras = EMPTY_EXTRAS;
303 }
304
305 final List<PhoneAccountHandle> phoneAccountHandles =
306 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
307
308 if ((call.getAccountHandle() == null
309 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700310 LogUtil.i(
311 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800312 return true;
313 }
314 }
315 return false;
316 }
317
318 public InCallState getInCallState() {
319 return mInCallState;
320 }
321
322 public CallList getCallList() {
323 return mCallList;
324 }
325
326 public void setUp(
327 @NonNull Context context,
328 CallList callList,
329 ExternalCallList externalCallList,
330 StatusBarNotifier statusBarNotifier,
331 ExternalCallNotifier externalCallNotifier,
332 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700333 ProximitySensor proximitySensor,
334 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700335 Trace.beginSection("InCallPresenter.setUp");
Eric Erfanianccca3152017-02-22 16:32:36 -0800336 if (mServiceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700337 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800338 if (context != mContext || callList != mCallList) {
339 throw new IllegalStateException();
340 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700341 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800342 return;
343 }
344
345 Objects.requireNonNull(context);
346 mContext = context;
347
348 mContactInfoCache = contactInfoCache;
349
350 mStatusBarNotifier = statusBarNotifier;
351 mExternalCallNotifier = externalCallNotifier;
352 addListener(mStatusBarNotifier);
Eric Erfaniand8046e52017-04-06 09:41:50 -0700353 EnrichedCallComponent.get(mContext)
354 .getEnrichedCallManager()
355 .registerStateChangedListener(mStatusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800356
357 mProximitySensor = proximitySensor;
358 addListener(mProximitySensor);
359
360 mThemeColorManager =
361 new ThemeColorManager(new InCallUIMaterialColorMapUtils(mContext.getResources()));
362
363 mCallList = callList;
364 mExternalCallList = externalCallList;
365 externalCallList.addExternalCallListener(mExternalCallNotifier);
366 externalCallList.addExternalCallListener(mExternalCallListener);
367
368 // This only gets called by the service so this is okay.
369 mServiceConnected = true;
370
371 // The final thing we do in this set up is add ourselves as a listener to CallList. This
372 // will kick off an update and the whole process can start.
373 mCallList.addListener(this);
374
375 // Create spam call list listener and add it to the list of listeners
zachh6a4cebd2017-10-24 17:10:06 -0700376 mSpamCallListListener =
377 new SpamCallListListener(
378 context, DialerExecutorComponent.get(context).dialerExecutorFactory());
Eric Erfanianccca3152017-02-22 16:32:36 -0800379 mCallList.addListener(mSpamCallListListener);
380
381 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800382
Eric Erfanian83b20212017-05-31 08:53:10 -0700383 mFilteredQueryHandler = filteredNumberQueryHandler;
Eric Erfanianccca3152017-02-22 16:32:36 -0800384 mContext
385 .getSystemService(TelephonyManager.class)
386 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
387
Eric Erfanian2ca43182017-08-31 06:57:16 -0700388 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
389 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800390 }
391
392 /**
393 * Called when the telephony service has disconnected from us. This will happen when there are no
394 * more active calls. However, we may still want to continue showing the UI for certain cases like
395 * showing "Call Ended". What we really want is to wait for the activity and the service to both
396 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
397 * secondary method that performs the aforementioned logic.
398 */
399 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700400 LogUtil.d("InCallPresenter.tearDown", "tearDown");
Eric Erfanianccca3152017-02-22 16:32:36 -0800401 mCallList.clearOnDisconnect();
402
403 mServiceConnected = false;
404
405 mContext
406 .getSystemService(TelephonyManager.class)
407 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
408
409 attemptCleanup();
410 VideoPauseController.getInstance().tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -0800411 }
412
413 private void attemptFinishActivity() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700414 mScreenTimeoutEnabled = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800415 final boolean doFinish = (mInCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700416 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800417 if (doFinish) {
418 mInCallActivity.setExcludeFromRecents(true);
419 mInCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800420 }
421 }
422
423 /**
424 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
425 * for more insight on the tear-down process.
426 */
427 public void unsetActivity(InCallActivity inCallActivity) {
428 if (inCallActivity == null) {
429 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
430 }
431 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700432 LogUtil.i(
433 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800434 return;
435 }
436 if (mInCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700437 LogUtil.w(
438 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800439 "Second instance of InCallActivity is trying to unregister when another"
440 + " instance is active. Ignoring.");
441 return;
442 }
443 updateActivity(null);
444 }
445
446 /**
447 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
448 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
449 */
450 private void updateActivity(InCallActivity inCallActivity) {
wangqi9982f0d2017-10-11 17:46:07 -0700451 Trace.beginSection("InCallPresenter.updateActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800452 boolean updateListeners = false;
453 boolean doAttemptCleanup = false;
454
455 if (inCallActivity != null) {
456 if (mInCallActivity == null) {
Eric Erfanian938468d2017-10-24 14:05:52 -0700457 mContext = inCallActivity.getApplicationContext();
Eric Erfanianccca3152017-02-22 16:32:36 -0800458 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700459 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800460 } else {
461 // since setActivity is called onStart(), it can be called multiple times.
462 // This is fine and ignorable, but we do not want to update the world every time
463 // this happens (like going to/from background) so we do not set updateListeners.
464 }
465
466 mInCallActivity = inCallActivity;
467 mInCallActivity.setExcludeFromRecents(false);
468
469 // By the time the UI finally comes up, the call may already be disconnected.
470 // If that's the case, we may need to show an error dialog.
471 if (mCallList != null && mCallList.getDisconnectedCall() != null) {
472 maybeShowErrorDialogOnDisconnect(mCallList.getDisconnectedCall());
473 }
474
475 // When the UI comes up, we need to first check the in-call state.
476 // If we are showing NO_CALLS, that means that a call probably connected and
477 // then immediately disconnected before the UI was able to come up.
478 // If we dont have any calls, start tearing down the UI instead.
479 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
480 // it has been set.
481 if (mInCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700482 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800483 attemptFinishActivity();
wangqi9982f0d2017-10-11 17:46:07 -0700484 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800485 return;
486 }
487 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700488 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800489 updateListeners = true;
490 mInCallActivity = null;
491
492 // We attempt cleanup for the destroy case but only after we recalculate the state
493 // to see if we need to come back up or stay shut down. This is why we do the
494 // cleanup after the call to onCallListChange() instead of directly here.
495 doAttemptCleanup = true;
496 }
497
498 // Messages can come from the telephony layer while the activity is coming up
499 // and while the activity is going down. So in both cases we need to recalculate what
500 // state we should be in after they complete.
501 // Examples: (1) A new incoming call could come in and then get disconnected before
502 // the activity is created.
503 // (2) All calls could disconnect and then get a new incoming call before the
504 // activity is destroyed.
505 //
Eric Erfanian938468d2017-10-24 14:05:52 -0700506 // a bug - We previously had a check for mServiceConnected here as well, but there are
Eric Erfanianccca3152017-02-22 16:32:36 -0800507 // cases where we need to recalculate the current state even if the service in not
508 // connected. In particular the case where startOrFinish() is called while the app is
509 // already finish()ing. In that case, we skip updating the state with the knowledge that
510 // we will check again once the activity has finished. That means we have to recalculate the
511 // state here even if the service is disconnected since we may not have finished a state
512 // transition while finish()ing.
513 if (updateListeners) {
514 onCallListChange(mCallList);
515 }
516
517 if (doAttemptCleanup) {
518 attemptCleanup();
519 }
wangqi9982f0d2017-10-11 17:46:07 -0700520 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800521 }
522
523 public void setManageConferenceActivity(
524 @Nullable ManageConferenceActivity manageConferenceActivity) {
525 mManageConferenceActivity = manageConferenceActivity;
526 }
527
528 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700529 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800530 bringToForeground(showDialpad);
531 }
532
533 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700534 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800535 LatencyReport latencyReport = new LatencyReport(call);
536 if (shouldAttemptBlocking(call)) {
537 maybeBlockCall(call, latencyReport);
538 } else {
539 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
540 mExternalCallList.onCallAdded(call);
541 } else {
542 latencyReport.onCallBlockingDone();
543 mCallList.onCallAdded(mContext, call, latencyReport);
544 }
545 }
546
547 // Since a call has been added we are no longer waiting for Telecom to send us a call.
548 setBoundAndWaitingForOutgoingCall(false, null);
549 call.registerCallback(mCallCallback);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700550 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800551 }
552
553 private boolean shouldAttemptBlocking(android.telecom.Call call) {
554 if (call.getState() != android.telecom.Call.STATE_RINGING) {
555 return false;
556 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700557 if (!UserManagerCompat.isUserUnlocked(mContext)) {
558 LogUtil.i(
559 "InCallPresenter.shouldAttemptBlocking",
560 "not attempting to block incoming call because user is locked");
561 return false;
562 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800563 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700564 LogUtil.i(
565 "InCallPresenter.shouldAttemptBlocking",
566 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800567 return false;
568 }
569 if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700570 LogUtil.i(
571 "InCallPresenter.shouldAttemptBlocking",
572 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800573 return false;
574 }
575 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
576 return false;
577 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700578 if (FilteredNumberCompat.useNewFiltering(mContext)) {
579 LogUtil.i(
580 "InCallPresenter.shouldAttemptBlocking",
581 "not attempting to block incoming call because framework blocking is in use");
582 return false;
583 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800584 return true;
585 }
586
587 /**
588 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
589 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
590 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
591 * call anyways.
592 */
593 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
594 final String countryIso = GeoUtil.getCurrentCountryIso(mContext);
595 final String number = TelecomCallUtil.getNumber(call);
596 final long timeAdded = System.currentTimeMillis();
597
598 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
599 // main UI thread. It is needed so we can change its value within different scopes, since
600 // that cannot be done with a final boolean.
601 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
602
603 final Handler handler = new Handler();
604
605 // Proceed if the query is slow; the call may still be blocked after the query returns.
606 final Runnable runnable =
607 new Runnable() {
608 @Override
609 public void run() {
610 hasTimedOut.set(true);
611 latencyReport.onCallBlockingDone();
612 mCallList.onCallAdded(mContext, call, latencyReport);
613 }
614 };
615 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
616
617 OnCheckBlockedListener onCheckBlockedListener =
618 new OnCheckBlockedListener() {
619 @Override
620 public void onCheckComplete(final Integer id) {
621 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700622 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800623 return;
624 }
625 if (!hasTimedOut.get()) {
626 handler.removeCallbacks(runnable);
627 }
628 if (id == null) {
629 if (!hasTimedOut.get()) {
630 latencyReport.onCallBlockingDone();
631 mCallList.onCallAdded(mContext, call, latencyReport);
632 }
633 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700634 LogUtil.d(
635 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800636 if (!hasTimedOut.get()) {
637 handler.removeCallbacks(runnable);
638
639 latencyReport.onCallBlockingDone();
640 mCallList.onCallAdded(mContext, call, latencyReport);
641 }
642 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700643 LogUtil.i(
644 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800645 call.reject(false, null);
646 Logger.get(mContext).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
647
648 /*
649 * If mContext is null, then the InCallPresenter was torn down before the
650 * block check had a chance to complete. The context is no longer valid, so
651 * don't attempt to remove the call log entry.
652 */
653 if (mContext == null) {
654 return;
655 }
656 // Register observer to update the call log.
657 // BlockedNumberContentObserver will unregister after successful log or timeout.
658 BlockedNumberContentObserver contentObserver =
659 new BlockedNumberContentObserver(mContext, new Handler(), number, timeAdded);
660 contentObserver.register();
661 }
662 }
663 };
664
665 mFilteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
666 }
667
668 public void onCallRemoved(android.telecom.Call call) {
669 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
670 mExternalCallList.onCallRemoved(call);
671 } else {
672 mCallList.onCallRemoved(mContext, call);
673 call.unregisterCallback(mCallCallback);
674 }
675 }
676
677 public void onCanAddCallChanged(boolean canAddCall) {
678 for (CanAddCallListener listener : mCanAddCallListeners) {
679 listener.onCanAddCallChanged(canAddCall);
680 }
681 }
682
683 @Override
684 public void onWiFiToLteHandover(DialerCall call) {
685 if (mInCallActivity != null) {
686 mInCallActivity.onWiFiToLteHandover(call);
687 }
688 }
689
690 @Override
691 public void onHandoverToWifiFailed(DialerCall call) {
692 if (mInCallActivity != null) {
693 mInCallActivity.onHandoverToWifiFailed(call);
694 }
695 }
696
Eric Erfanianc857f902017-05-15 14:05:33 -0700697 @Override
698 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
699 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
700 if (mInCallActivity != null) {
701 mInCallActivity.onInternationalCallOnWifi(call);
702 }
703 }
704
Eric Erfanianccca3152017-02-22 16:32:36 -0800705 /**
706 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
707 * app based on the information it gets from CallList. Dispatches the in-call state to all
708 * listeners. Can trigger the creation or destruction of the UI based on the states that is
709 * calculates.
710 */
711 @Override
712 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700713 Trace.beginSection("InCallPresenter.onCallListChange");
Eric Erfanianccca3152017-02-22 16:32:36 -0800714 if (mInCallActivity != null && mInCallActivity.isInCallScreenAnimating()) {
715 mAwaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700716 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800717 return;
718 }
719 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700720 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800721 return;
722 }
723
724 mAwaitingCallListUpdate = false;
725
726 InCallState newState = getPotentialStateFromCallList(callList);
727 InCallState oldState = mInCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700728 LogUtil.d(
729 "InCallPresenter.onCallListChange",
730 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700731
732 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
733 // incall activity for that call will still exist (even if it's not visible). In the case of
734 // an incoming call in that situation, just disconnect that "waiting for account" call and
735 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
Eric Erfanian938468d2017-10-24 14:05:52 -0700736 // a bug for more details.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700737 DialerCall waitingForAccountCall;
738 if (newState == InCallState.INCOMING
739 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
740 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700741 // The InCallActivity might be destroyed or not started yet at this point.
742 if (isActivityStarted()) {
743 mInCallActivity.dismissPendingDialogs();
744 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700745 }
746
Eric Erfanianccca3152017-02-22 16:32:36 -0800747 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700748 LogUtil.d(
749 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800750
751 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700752 LogUtil.i(
753 "InCallPresenter.onCallListChange",
754 "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800755 mInCallState = newState;
756
757 // notify listeners of new state
758 for (InCallStateListener listener : mListeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700759 LogUtil.d(
760 "InCallPresenter.onCallListChange",
761 "Notify " + listener + " of state " + mInCallState.toString());
Eric Erfanianccca3152017-02-22 16:32:36 -0800762 listener.onStateChange(oldState, mInCallState, callList);
763 }
764
765 if (isActivityStarted()) {
766 final boolean hasCall =
767 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
768 mInCallActivity.dismissKeyguard(hasCall);
769 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700770 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800771 }
772
773 /** Called when there is a new incoming call. */
774 @Override
775 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700776 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800777 InCallState newState = startOrFinishUi(InCallState.INCOMING);
778 InCallState oldState = mInCallState;
779
Eric Erfanian2ca43182017-08-31 06:57:16 -0700780 LogUtil.i(
781 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800782 mInCallState = newState;
783
wangqicf61ca02017-08-31 15:32:55 -0700784 Trace.beginSection("listener.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800785 for (IncomingCallListener listener : mIncomingCallListeners) {
786 listener.onIncomingCall(oldState, mInCallState, call);
787 }
wangqicf61ca02017-08-31 15:32:55 -0700788 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800789
wangqicf61ca02017-08-31 15:32:55 -0700790 Trace.beginSection("onPrimaryCallStateChanged");
Eric Erfanianccca3152017-02-22 16:32:36 -0800791 if (mInCallActivity != null) {
792 // Re-evaluate which fragment is being shown.
793 mInCallActivity.onPrimaryCallStateChanged();
794 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700795 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700796 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800797 }
798
799 @Override
800 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700801 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
Eric Erfanianccca3152017-02-22 16:32:36 -0800802 && mInCallState == InCallPresenter.InCallState.INCOMING) {
803 LogUtil.i(
804 "InCallPresenter.onUpgradeToVideo",
805 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700806 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800807 }
808
809 if (mInCallActivity != null) {
810 // Re-evaluate which fragment is being shown.
811 mInCallActivity.onPrimaryCallStateChanged();
812 }
813 }
814
815 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700816 public void onSessionModificationStateChange(DialerCall call) {
817 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800818 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
819 if (mProximitySensor == null) {
820 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
821 return;
822 }
823 mProximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700824 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
Eric Erfanianccca3152017-02-22 16:32:36 -0800825 if (mInCallActivity != null) {
826 // Re-evaluate which fragment is being shown.
827 mInCallActivity.onPrimaryCallStateChanged();
828 }
829 }
830
831 /**
832 * Called when a call becomes disconnected. Called everytime an existing call changes from being
833 * connected (incoming/outgoing/active) to disconnected.
834 */
835 @Override
836 public void onDisconnect(DialerCall call) {
837 maybeShowErrorDialogOnDisconnect(call);
838
839 // We need to do the run the same code as onCallListChange.
840 onCallListChange(mCallList);
841
842 if (isActivityStarted()) {
843 mInCallActivity.dismissKeyguard(false);
844 }
845
846 if (call.isEmergencyCall()) {
847 FilteredNumbersUtil.recordLastEmergencyCallTime(mContext);
848 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800849
Eric Erfanianfc37b022017-03-21 10:11:17 -0700850 if (!mCallList.hasLiveCall()
851 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700852 && !isSecretCode(call.getNumber())
wangqi9982f0d2017-10-11 17:46:07 -0700853 && !call.isVoiceMailNumber()) {
Eric Erfanian10b34a52017-05-04 08:23:17 -0700854 PostCall.onCallDisconnected(mContext, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -0800855 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800856 }
857
Eric Erfanian10b34a52017-05-04 08:23:17 -0700858 private boolean isSecretCode(@Nullable String number) {
859 return number != null
860 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
861 }
862
Eric Erfanianccca3152017-02-22 16:32:36 -0800863 /** Given the call list, return the state in which the in-call screen should be. */
864 public InCallState getPotentialStateFromCallList(CallList callList) {
865
866 InCallState newState = InCallState.NO_CALLS;
867
868 if (callList == null) {
869 return newState;
870 }
871 if (callList.getIncomingCall() != null) {
872 newState = InCallState.INCOMING;
873 } else if (callList.getWaitingForAccountCall() != null) {
874 newState = InCallState.WAITING_FOR_ACCOUNT;
875 } else if (callList.getPendingOutgoingCall() != null) {
876 newState = InCallState.PENDING_OUTGOING;
877 } else if (callList.getOutgoingCall() != null) {
878 newState = InCallState.OUTGOING;
879 } else if (callList.getActiveCall() != null
880 || callList.getBackgroundCall() != null
881 || callList.getDisconnectedCall() != null
882 || callList.getDisconnectingCall() != null) {
883 newState = InCallState.INCALL;
884 }
885
886 if (newState == InCallState.NO_CALLS) {
887 if (mBoundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700888 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800889 }
890 }
891
892 return newState;
893 }
894
895 public boolean isBoundAndWaitingForOutgoingCall() {
896 return mBoundAndWaitingForOutgoingCall;
897 }
898
899 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700900 LogUtil.i(
901 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
902 "setBoundAndWaitingForOutgoingCall: " + isBound);
Eric Erfanianccca3152017-02-22 16:32:36 -0800903 mBoundAndWaitingForOutgoingCall = isBound;
904 mThemeColorManager.setPendingPhoneAccountHandle(handle);
905 if (isBound && mInCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700906 mInCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800907 }
908 }
909
910 public void onShrinkAnimationComplete() {
911 if (mAwaitingCallListUpdate) {
912 onCallListChange(mCallList);
913 }
914 }
915
916 public void addIncomingCallListener(IncomingCallListener listener) {
917 Objects.requireNonNull(listener);
918 mIncomingCallListeners.add(listener);
919 }
920
921 public void removeIncomingCallListener(IncomingCallListener listener) {
922 if (listener != null) {
923 mIncomingCallListeners.remove(listener);
924 }
925 }
926
927 public void addListener(InCallStateListener listener) {
928 Objects.requireNonNull(listener);
929 mListeners.add(listener);
930 }
931
932 public void removeListener(InCallStateListener listener) {
933 if (listener != null) {
934 mListeners.remove(listener);
935 }
936 }
937
938 public void addDetailsListener(InCallDetailsListener listener) {
939 Objects.requireNonNull(listener);
940 mDetailsListeners.add(listener);
941 }
942
943 public void removeDetailsListener(InCallDetailsListener listener) {
944 if (listener != null) {
945 mDetailsListeners.remove(listener);
946 }
947 }
948
949 public void addCanAddCallListener(CanAddCallListener listener) {
950 Objects.requireNonNull(listener);
951 mCanAddCallListeners.add(listener);
952 }
953
954 public void removeCanAddCallListener(CanAddCallListener listener) {
955 if (listener != null) {
956 mCanAddCallListeners.remove(listener);
957 }
958 }
959
960 public void addOrientationListener(InCallOrientationListener listener) {
961 Objects.requireNonNull(listener);
962 mOrientationListeners.add(listener);
963 }
964
965 public void removeOrientationListener(InCallOrientationListener listener) {
966 if (listener != null) {
967 mOrientationListeners.remove(listener);
968 }
969 }
970
971 public void addInCallEventListener(InCallEventListener listener) {
972 Objects.requireNonNull(listener);
973 mInCallEventListeners.add(listener);
974 }
975
976 public void removeInCallEventListener(InCallEventListener listener) {
977 if (listener != null) {
978 mInCallEventListeners.remove(listener);
979 }
980 }
981
982 public ProximitySensor getProximitySensor() {
983 return mProximitySensor;
984 }
985
986 public PseudoScreenState getPseudoScreenState() {
987 return mPseudoScreenState;
988 }
989
990 /** Returns true if the incall app is the foreground application. */
991 public boolean isShowingInCallUi() {
992 if (!isActivityStarted()) {
993 return false;
994 }
995 if (mManageConferenceActivity != null && mManageConferenceActivity.isVisible()) {
996 return true;
997 }
998 return mInCallActivity.isVisible();
999 }
1000
1001 /**
1002 * Returns true if the activity has been created and is running. Returns true as long as activity
1003 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
1004 * (not in foreground).
1005 */
1006 public boolean isActivityStarted() {
1007 return (mInCallActivity != null
1008 && !mInCallActivity.isDestroyed()
1009 && !mInCallActivity.isFinishing());
1010 }
1011
1012 /**
1013 * Determines if the In-Call app is currently changing configuration.
1014 *
1015 * @return {@code true} if the In-Call app is changing configuration.
1016 */
1017 public boolean isChangingConfigurations() {
1018 return mIsChangingConfigurations;
1019 }
1020
1021 /**
1022 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1023 * screen orientation).
1024 */
1025 /*package*/
1026 void updateIsChangingConfigurations() {
1027 mIsChangingConfigurations = false;
1028 if (mInCallActivity != null) {
1029 mIsChangingConfigurations = mInCallActivity.isChangingConfigurations();
1030 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001031 LogUtil.v(
1032 "InCallPresenter.updateIsChangingConfigurations",
1033 "updateIsChangingConfigurations = " + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001034 }
1035
1036 /** Called when the activity goes in/out of the foreground. */
1037 public void onUiShowing(boolean showing) {
1038 // We need to update the notification bar when we leave the UI because that
1039 // could trigger it to show again.
1040 if (mStatusBarNotifier != null) {
wangqic8cf79e2017-10-17 09:21:00 -07001041 mStatusBarNotifier.updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -08001042 }
1043
1044 if (mProximitySensor != null) {
1045 mProximitySensor.onInCallShowing(showing);
1046 }
1047
1048 Intent broadcastIntent = Bindings.get(mContext).getUiReadyBroadcastIntent(mContext);
1049 if (broadcastIntent != null) {
1050 broadcastIntent.putExtra(EXTRA_FIRST_TIME_SHOWN, !mIsActivityPreviouslyStarted);
1051
1052 if (showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001053 LogUtil.d("InCallPresenter.onUiShowing", "Sending sticky broadcast: ", broadcastIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001054 mContext.sendStickyBroadcast(broadcastIntent);
1055 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001056 LogUtil.d("InCallPresenter.onUiShowing", "Removing sticky broadcast: ", broadcastIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001057 mContext.removeStickyBroadcast(broadcastIntent);
1058 }
1059 }
1060
1061 if (showing) {
1062 mIsActivityPreviouslyStarted = true;
1063 } else {
1064 updateIsChangingConfigurations();
1065 }
1066
1067 for (InCallUiListener listener : mInCallUiListeners) {
1068 listener.onUiShowing(showing);
1069 }
1070
1071 if (mInCallActivity != null) {
1072 // Re-evaluate which fragment is being shown.
1073 mInCallActivity.onPrimaryCallStateChanged();
1074 }
1075 }
1076
Eric Erfanian2ca43182017-08-31 06:57:16 -07001077 public void refreshUi() {
1078 if (mInCallActivity != null) {
1079 // Re-evaluate which fragment is being shown.
1080 mInCallActivity.onPrimaryCallStateChanged();
1081 }
1082 }
1083
Eric Erfanianccca3152017-02-22 16:32:36 -08001084 public void addInCallUiListener(InCallUiListener listener) {
1085 mInCallUiListeners.add(listener);
1086 }
1087
1088 public boolean removeInCallUiListener(InCallUiListener listener) {
1089 return mInCallUiListeners.remove(listener);
1090 }
1091
1092 /*package*/
1093 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001094 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001095 notifyVideoPauseController(true);
Eric Erfanian2ca43182017-08-31 06:57:16 -07001096 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001097 }
1098
1099 /*package*/
1100 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001101 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001102 notifyVideoPauseController(false);
1103 }
1104
1105 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001106 LogUtil.d(
1107 "InCallPresenter.notifyVideoPauseController",
1108 "mIsChangingConfigurations=" + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001109 if (!mIsChangingConfigurations) {
1110 VideoPauseController.getInstance().onUiShowing(showing);
1111 }
1112 }
1113
1114 /** Brings the app into the foreground if possible. */
1115 public void bringToForeground(boolean showDialpad) {
1116 // Before we bring the incall UI to the foreground, we check to see if:
1117 // 1. It is not currently in the foreground
1118 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1119 // be displayed)
1120 // If the activity hadn't actually been started previously, yet there are still calls
1121 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1122 // bring it up the UI regardless.
1123 if (!isShowingInCallUi() && mInCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001124 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001125 }
1126 }
1127
1128 public void onPostDialCharWait(String callId, String chars) {
1129 if (isActivityStarted()) {
1130 mInCallActivity.showPostCharWaitDialog(callId, chars);
1131 }
1132 }
1133
1134 /**
1135 * Handles the green CALL key while in-call.
1136 *
1137 * @return true if we consumed the event.
1138 */
1139 public boolean handleCallKey() {
1140 LogUtil.v("InCallPresenter.handleCallKey", null);
1141
1142 // The green CALL button means either "Answer", "Unhold", or
1143 // "Swap calls", or can be a no-op, depending on the current state
1144 // of the Phone.
1145
1146 /** INCOMING CALL */
1147 final CallList calls = mCallList;
1148 final DialerCall incomingCall = calls.getIncomingCall();
1149 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1150
1151 // (1) Attempt to answer a call
1152 if (incomingCall != null) {
1153 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1154 return true;
1155 }
1156
1157 /** STATE_ACTIVE CALL */
1158 final DialerCall activeCall = calls.getActiveCall();
1159 if (activeCall != null) {
1160 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1161 // consolidate this logic.
1162 final boolean canMerge =
1163 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1164 final boolean canSwap =
1165 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1166
Eric Erfanian2ca43182017-08-31 06:57:16 -07001167 LogUtil.v(
1168 "InCallPresenter.handleCallKey",
1169 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001170
1171 // (2) Attempt actions on conference calls
1172 if (canMerge) {
1173 TelecomAdapter.getInstance().merge(activeCall.getId());
1174 return true;
1175 } else if (canSwap) {
1176 TelecomAdapter.getInstance().swap(activeCall.getId());
1177 return true;
1178 }
1179 }
1180
1181 /** BACKGROUND CALL */
1182 final DialerCall heldCall = calls.getBackgroundCall();
1183 if (heldCall != null) {
1184 // We have a hold call so presumeable it will always support HOLD...but
1185 // there is no harm in double checking.
1186 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1187
Eric Erfanian2ca43182017-08-31 06:57:16 -07001188 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001189
1190 // (4) unhold call
1191 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1192 heldCall.unhold();
1193 return true;
1194 }
1195 }
1196
1197 // Always consume hard keys
1198 return true;
1199 }
1200
Eric Erfanianccca3152017-02-22 16:32:36 -08001201 /** Clears the previous fullscreen state. */
1202 public void clearFullscreen() {
1203 mIsFullScreen = false;
1204 }
1205
1206 /**
1207 * Changes the fullscreen mode of the in-call UI.
1208 *
1209 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1210 * otherwise.
1211 */
1212 public void setFullScreen(boolean isFullScreen) {
1213 setFullScreen(isFullScreen, false /* force */);
1214 }
1215
1216 /**
1217 * Changes the fullscreen mode of the in-call UI.
1218 *
1219 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1220 * otherwise.
1221 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1222 */
1223 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001224 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001225
1226 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1227 if (isDialpadVisible()) {
1228 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001229 LogUtil.v(
1230 "InCallPresenter.setFullScreen",
1231 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001232 }
1233
1234 if (mIsFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001235 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001236 return;
1237 }
1238 mIsFullScreen = isFullScreen;
1239 notifyFullscreenModeChange(mIsFullScreen);
1240 }
1241
1242 /**
1243 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1244 * otherwise.
1245 */
1246 public boolean isFullscreen() {
1247 return mIsFullScreen;
1248 }
1249
1250 /**
1251 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1252 *
1253 * @param isFullscreenMode {@code True} if entering full screen mode.
1254 */
1255 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
1256 for (InCallEventListener listener : mInCallEventListeners) {
1257 listener.onFullscreenModeChanged(isFullscreenMode);
1258 }
1259 }
1260
1261 /**
1262 * For some disconnected causes, we show a dialog. This calls into the activity to show the dialog
1263 * if appropriate for the call.
1264 */
1265 private void maybeShowErrorDialogOnDisconnect(DialerCall call) {
1266 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1267 if (isActivityStarted() && call.getState() == DialerCall.State.DISCONNECTED) {
1268 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1269 setDisconnectCauseForMissingAccounts(call);
1270 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001271 mInCallActivity.maybeShowErrorDialogOnDisconnect(
1272 new DisconnectMessage(mInCallActivity, call));
Eric Erfanianccca3152017-02-22 16:32:36 -08001273 }
1274 }
1275
1276 /**
1277 * When the state of in-call changes, this is the first method to get called. It determines if the
1278 * UI needs to be started or finished depending on the new state and does it.
1279 */
1280 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001281 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001282 LogUtil.d(
1283 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + mInCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001284
1285 // TODO: Consider a proper state machine implementation
1286
1287 // If the state isn't changing we have already done any starting/stopping of activities in
1288 // a previous pass...so lets cut out early
1289 if (newState == mInCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001290 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001291 return newState;
1292 }
1293
1294 // A new Incoming call means that the user needs to be notified of the the call (since
1295 // it wasn't them who initiated it). We do this through full screen notifications and
1296 // happens indirectly through {@link StatusBarNotifier}.
1297 //
1298 // The process for incoming calls is as follows:
1299 //
1300 // 1) CallList - Announces existence of new INCOMING call
1301 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1302 // - should be set to INCOMING.
1303 // 3) InCallPresenter - This method is called to see if we need to start or finish
1304 // the app given the new state.
1305 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1306 // StatusBarNotifier explicitly to issue a FullScreen Notification
1307 // that will either start the InCallActivity or show the user a
1308 // top-level notification dialog if the user is in an immersive app.
1309 // That notification can also start the InCallActivity.
1310 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1311 // call InCallPresenter::setActivity() to let the presenter
1312 // know that start-up is complete.
1313 //
1314 // [ AND NOW YOU'RE IN THE CALL. voila! ]
1315 //
1316 // Our app is started using a fullScreen notification. We need to do this whenever
1317 // we get an incoming call. Depending on the current context of the device, either a
1318 // incoming call HUN or the actual InCallActivity will be shown.
1319 final boolean startIncomingCallSequence = (InCallState.INCOMING == newState);
1320
1321 // A dialog to show on top of the InCallUI to select a PhoneAccount
1322 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1323
1324 // A new outgoing call indicates that the user just now dialed a number and when that
1325 // happens we need to display the screen immediately or show an account picker dialog if
1326 // no default is set. However, if the main InCallUI is already visible, we do not want to
1327 // re-initiate the start-up animation, so we do not need to do anything here.
1328 //
1329 // It is also possible to go into an intermediate state where the call has been initiated
1330 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1331 // This pending outgoing state can also launch the call screen.
1332 //
1333 // This is different from the incoming call sequence because we do not need to shock the
1334 // user with a top-level notification. Just show the call UI normally.
1335 boolean callCardFragmentVisible =
1336 mInCallActivity != null && mInCallActivity.getCallCardFragmentVisible();
1337 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1338 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1339
1340 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1341 // outgoing call process, so the UI should be brought up to show an error dialog.
1342 showCallUi |=
1343 (InCallState.PENDING_OUTGOING == mInCallState
1344 && InCallState.INCALL == newState
1345 && !isShowingInCallUi());
1346
1347 // Another exception - InCallActivity is in charge of disconnecting a call with no
1348 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1349 // call so that:
1350 // 1) The call can be disconnected correctly
1351 // 2) The UI comes up and correctly displays the error dialog.
1352 // TODO: Remove these special case conditions by making InCallPresenter a true state
1353 // machine. Telecom should also be the component responsible for disconnecting a call
1354 // with no valid accounts.
1355 showCallUi |=
1356 InCallState.PENDING_OUTGOING == newState
1357 && mainUiNotVisible
1358 && isCallWithNoValidAccounts(mCallList.getPendingOutgoingCall());
1359
1360 // The only time that we have an instance of mInCallActivity and it isn't started is
1361 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1362 // the activity. When it is finally destroyed, we double check if we should bring it back
1363 // up so we aren't going to lose anything by avoiding a second startup here.
1364 boolean activityIsFinishing = mInCallActivity != null && !isActivityStarted();
1365 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001366 LogUtil.i(
1367 "InCallPresenter.startOrFinishUi",
1368 "Undo the state change: " + newState + " -> " + mInCallState);
wangqicf61ca02017-08-31 15:32:55 -07001369 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001370 return mInCallState;
1371 }
1372
1373 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1374 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1375 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1376 // outgoing call after user ignore it by pressing home button.
1377 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1378 && !showCallUi
1379 && isActivityStarted()) {
1380 mInCallActivity.dismissPendingDialogs();
1381 }
1382
1383 if (showCallUi || showAccountPicker) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001384 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001385 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001386 } else if (startIncomingCallSequence) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001387 LogUtil.i("InCallPresenter.startOrFinishUi", "Start Full Screen in call UI");
Eric Erfanianccca3152017-02-22 16:32:36 -08001388
wangqic8cf79e2017-10-17 09:21:00 -07001389 mStatusBarNotifier.updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -08001390 } else if (newState == InCallState.NO_CALLS) {
1391 // The new state is the no calls state. Tear everything down.
1392 attemptFinishActivity();
1393 attemptCleanup();
1394 }
1395
wangqicf61ca02017-08-31 15:32:55 -07001396 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001397 return newState;
1398 }
1399
1400 /**
1401 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1402 * or PhoneAccounts to select from.
1403 */
1404 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1405
1406 Bundle extras = call.getIntentExtras();
1407 // Initialize the extras bundle to avoid NPE
1408 if (extras == null) {
1409 extras = new Bundle();
1410 }
1411
1412 final List<PhoneAccountHandle> phoneAccountHandles =
1413 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1414
1415 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1416 String scheme = call.getHandle().getScheme();
1417 final String errorMsg =
1418 PhoneAccount.SCHEME_TEL.equals(scheme)
1419 ? mContext.getString(R.string.callFailed_simError)
1420 : mContext.getString(R.string.incall_error_supp_service_unknown);
1421 DisconnectCause disconnectCause =
1422 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1423 call.setDisconnectCause(disconnectCause);
1424 }
1425 }
1426
Eric Erfanianccca3152017-02-22 16:32:36 -08001427 /**
1428 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1429 * Calling classes should use this as an indication whether to interact with the
1430 * InCallPresenter or not.
1431 */
1432 public boolean isReadyForTearDown() {
1433 return mInCallActivity == null && !mServiceConnected && mInCallState == InCallState.NO_CALLS;
1434 }
1435
1436 /**
1437 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1438 */
1439 private void attemptCleanup() {
1440 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001441 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001442
1443 cleanupSurfaces();
1444
1445 mIsActivityPreviouslyStarted = false;
1446 mIsChangingConfigurations = false;
1447
1448 // blow away stale contact info so that we get fresh data on
1449 // the next set of calls
1450 if (mContactInfoCache != null) {
1451 mContactInfoCache.clearCache();
1452 }
1453 mContactInfoCache = null;
1454
1455 if (mProximitySensor != null) {
1456 removeListener(mProximitySensor);
1457 mProximitySensor.tearDown();
1458 }
1459 mProximitySensor = null;
1460
1461 if (mStatusBarNotifier != null) {
1462 removeListener(mStatusBarNotifier);
Eric Erfaniand8046e52017-04-06 09:41:50 -07001463 EnrichedCallComponent.get(mContext)
1464 .getEnrichedCallManager()
1465 .unregisterStateChangedListener(mStatusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001466 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001467
Eric Erfanianccca3152017-02-22 16:32:36 -08001468 if (mExternalCallNotifier != null && mExternalCallList != null) {
1469 mExternalCallList.removeExternalCallListener(mExternalCallNotifier);
1470 }
1471 mStatusBarNotifier = null;
1472
1473 if (mCallList != null) {
1474 mCallList.removeListener(this);
1475 mCallList.removeListener(mSpamCallListListener);
1476 }
1477 mCallList = null;
1478
1479 mContext = null;
1480 mInCallActivity = null;
1481 mManageConferenceActivity = null;
1482
1483 mListeners.clear();
1484 mIncomingCallListeners.clear();
1485 mDetailsListeners.clear();
1486 mCanAddCallListeners.clear();
1487 mOrientationListeners.clear();
1488 mInCallEventListeners.clear();
1489 mInCallUiListeners.clear();
twyen8efb4952017-10-06 16:35:54 -07001490 if (!mInCallUiLocks.isEmpty()) {
1491 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + mInCallUiLocks);
1492 mInCallUiLocks.clear();
1493 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001494 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001495 }
1496 }
1497
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001498 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001499 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -08001500 mContext.startActivity(
1501 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001502 mContext, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001503 }
1504
1505 public void onServiceBind() {
1506 mServiceBound = true;
1507 }
1508
1509 public void onServiceUnbind() {
1510 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
1511 mServiceBound = false;
1512 }
1513
1514 public boolean isServiceBound() {
1515 return mServiceBound;
1516 }
1517
1518 public void maybeStartRevealAnimation(Intent intent) {
1519 if (intent == null || mInCallActivity != null) {
1520 return;
1521 }
1522 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1523 if (extras == null) {
1524 // Incoming call, just show the in-call UI directly.
1525 return;
1526 }
1527
1528 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1529 // Account selection dialog will show up so don't show the animation.
1530 return;
1531 }
1532
1533 final PhoneAccountHandle accountHandle =
1534 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1535 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001536
1537 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(true, accountHandle);
1538
1539 final Intent activityIntent =
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001540 InCallActivity.getIntent(mContext, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001541 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
1542 mContext.startActivity(activityIntent);
1543 }
1544
1545 /**
1546 * Retrieves the current in-call camera manager instance, creating if necessary.
1547 *
1548 * @return The {@link InCallCameraManager}.
1549 */
1550 public InCallCameraManager getInCallCameraManager() {
1551 synchronized (this) {
1552 if (mInCallCameraManager == null) {
1553 mInCallCameraManager = new InCallCameraManager(mContext);
1554 }
1555
1556 return mInCallCameraManager;
1557 }
1558 }
1559
1560 /**
1561 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1562 *
1563 * @param orientation The screen orientation of the device (one of: {@link
1564 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1565 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1566 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1567 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1568 */
1569 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001570 LogUtil.d(
1571 "InCallPresenter.onDeviceOrientationChange",
1572 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001573
1574 if (mCallList != null) {
1575 mCallList.notifyCallsOfDeviceRotation(orientation);
1576 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001577 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001578 }
1579
1580 // Notify listeners of device orientation changed.
1581 for (InCallOrientationListener listener : mOrientationListeners) {
1582 listener.onDeviceOrientationChanged(orientation);
1583 }
1584 }
1585
1586 /**
1587 * Configures the in-call UI activity so it can change orientations or not. Enables the
1588 * orientation event listener if allowOrientationChange is true, disables it if false.
1589 *
1590 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1591 * landscape. {@code false} if the in-call UI should be locked in portrait.
1592 */
1593 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
1594 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001595 LogUtil.e(
1596 "InCallPresenter.setInCallAllowsOrientationChange",
1597 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001598 return;
1599 }
1600 mInCallActivity.setAllowOrientationChange(allowOrientationChange);
1601 }
1602
1603 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001604 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
1605 mScreenTimeoutEnabled = enable;
1606 applyScreenTimeout();
1607 }
1608
1609 private void applyScreenTimeout() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001610 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001611 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001612 return;
1613 }
1614
1615 final Window window = mInCallActivity.getWindow();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001616 if (mScreenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001617 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1618 } else {
1619 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1620 }
1621 }
1622
1623 /**
1624 * Hides or shows the conference manager fragment.
1625 *
1626 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1627 * be hidden.
1628 */
1629 public void showConferenceCallManager(boolean show) {
1630 if (mInCallActivity != null) {
1631 mInCallActivity.showConferenceFragment(show);
1632 }
1633 if (!show && mManageConferenceActivity != null) {
1634 mManageConferenceActivity.finish();
1635 }
1636 }
1637
1638 /**
1639 * Determines if the dialpad is visible.
1640 *
1641 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1642 */
1643 public boolean isDialpadVisible() {
1644 if (mInCallActivity == null) {
1645 return false;
1646 }
1647 return mInCallActivity.isDialpadVisible();
1648 }
1649
1650 public ThemeColorManager getThemeColorManager() {
1651 return mThemeColorManager;
1652 }
1653
1654 /** Called when the foreground call changes. */
1655 public void onForegroundCallChanged(DialerCall newForegroundCall) {
1656 mThemeColorManager.onForegroundCallChanged(mContext, newForegroundCall);
1657 if (mInCallActivity != null) {
1658 mInCallActivity.onForegroundCallChanged(newForegroundCall);
1659 }
1660 }
1661
1662 public InCallActivity getActivity() {
1663 return mInCallActivity;
1664 }
1665
1666 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1667 public void setActivity(InCallActivity inCallActivity) {
1668 if (inCallActivity == null) {
1669 throw new IllegalArgumentException("registerActivity cannot be called with null");
1670 }
1671 if (mInCallActivity != null && mInCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001672 LogUtil.w(
1673 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001674 }
1675 updateActivity(inCallActivity);
1676 }
1677
1678 ExternalCallNotifier getExternalCallNotifier() {
1679 return mExternalCallNotifier;
1680 }
1681
1682 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
1683 if (mLocalVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001684 boolean isPixel2017 = false;
1685 if (mContext != null) {
1686 isPixel2017 = mContext.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
1687 }
1688 mLocalVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001689 }
1690 return mLocalVideoSurfaceTexture;
1691 }
1692
1693 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
1694 if (mRemoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001695 boolean isPixel2017 = false;
1696 if (mContext != null) {
1697 isPixel2017 = mContext.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
1698 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001699 mRemoteVideoSurfaceTexture =
roldenburg12b50c62017-09-01 14:41:19 -07001700 VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001701 }
1702 return mRemoteVideoSurfaceTexture;
1703 }
1704
1705 void cleanupSurfaces() {
1706 if (mRemoteVideoSurfaceTexture != null) {
1707 mRemoteVideoSurfaceTexture.setDoneWithSurface();
1708 mRemoteVideoSurfaceTexture = null;
1709 }
1710 if (mLocalVideoSurfaceTexture != null) {
1711 mLocalVideoSurfaceTexture.setDoneWithSurface();
1712 mLocalVideoSurfaceTexture = null;
1713 }
1714 }
1715
1716 /** All the main states of InCallActivity. */
1717 public enum InCallState {
1718 // InCall Screen is off and there are no calls
1719 NO_CALLS,
1720
1721 // Incoming-call screen is up
1722 INCOMING,
1723
1724 // In-call experience is showing
1725 INCALL,
1726
1727 // Waiting for user input before placing outgoing call
1728 WAITING_FOR_ACCOUNT,
1729
1730 // UI is starting up but no call has been initiated yet.
1731 // The UI is waiting for Telecom to respond.
1732 PENDING_OUTGOING,
1733
1734 // User is dialing out
1735 OUTGOING;
1736
1737 public boolean isIncoming() {
1738 return (this == INCOMING);
1739 }
1740
1741 public boolean isConnectingOrConnected() {
1742 return (this == INCOMING || this == OUTGOING || this == INCALL);
1743 }
1744 }
1745
1746 /** Interface implemented by classes that need to know about the InCall State. */
1747 public interface InCallStateListener {
1748
1749 // TODO: Enhance state to contain the call objects instead of passing CallList
1750 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1751 }
1752
1753 public interface IncomingCallListener {
1754
1755 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1756 }
1757
1758 public interface CanAddCallListener {
1759
1760 void onCanAddCallChanged(boolean canAddCall);
1761 }
1762
1763 public interface InCallDetailsListener {
1764
1765 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1766 }
1767
1768 public interface InCallOrientationListener {
1769
1770 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1771 }
1772
1773 /**
1774 * Interface implemented by classes that need to know about events which occur within the In-Call
1775 * UI. Used as a means of communicating between fragments that make up the UI.
1776 */
1777 public interface InCallEventListener {
1778
1779 void onFullscreenModeChanged(boolean isFullscreenMode);
1780 }
1781
1782 public interface InCallUiListener {
1783
1784 void onUiShowing(boolean showing);
1785 }
twyen8efb4952017-10-06 16:35:54 -07001786
1787 private class InCallUiLockImpl implements InCallUiLock {
1788 private final String tag;
1789
1790 private InCallUiLockImpl(String tag) {
1791 this.tag = tag;
1792 }
1793
1794 @MainThread
1795 @Override
1796 public void release() {
1797 Assert.isMainThread();
1798 releaseInCallUiLock(InCallUiLockImpl.this);
1799 }
1800
1801 @Override
1802 public String toString() {
1803 return "InCallUiLock[" + tag + "]";
1804 }
1805 }
1806
1807 @MainThread
1808 public InCallUiLock acquireInCallUiLock(String tag) {
1809 Assert.isMainThread();
1810 InCallUiLock lock = new InCallUiLockImpl(tag);
1811 mInCallUiLocks.add(lock);
1812 return lock;
1813 }
1814
1815 @MainThread
1816 private void releaseInCallUiLock(InCallUiLock lock) {
1817 Assert.isMainThread();
1818 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
1819 mInCallUiLocks.remove(lock);
1820 if (mInCallUiLocks.isEmpty()) {
1821 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
1822 if (mInCallState == InCallState.NO_CALLS) {
1823 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
1824 attemptFinishActivity();
1825 attemptCleanup();
1826 }
1827 }
1828 }
1829
1830 @MainThread
1831 public boolean isInCallUiLocked() {
1832 Assert.isMainThread();
1833 if (mInCallUiLocks.isEmpty()) {
1834 return false;
1835 }
1836 for (InCallUiLock lock : mInCallUiLocks) {
1837 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
1838 }
1839 return true;
1840 }
1841
1842 private final Set<InCallUiLock> mInCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08001843}