blob: a0069a629f73a6073f464a98d6b4dbfaba84ba06 [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;
Eric Erfanian2ca43182017-08-31 06:57:16 -070048import com.android.dialer.common.concurrent.DefaultDialerExecutorFactory;
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) {
276 sInCallPresenter = new InCallPresenter();
277 }
278 return sInCallPresenter;
279 }
280
Eric Erfanian10b34a52017-05-04 08:23:17 -0700281 @VisibleForTesting
282 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
283 sInCallPresenter = inCallPresenter;
284 }
285
Eric Erfanianccca3152017-02-22 16:32:36 -0800286 /**
287 * Determines whether or not a call has no valid phone accounts that can be used to make the call
288 * with. Emergency calls do not require a phone account.
289 *
290 * @param call to check accounts for.
291 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
292 * call contains a phone account that could be used to initiate it with, or is an emergency
293 * call.
294 */
295 public static boolean isCallWithNoValidAccounts(DialerCall call) {
296 if (call != null && !call.isEmergencyCall()) {
297 Bundle extras = call.getIntentExtras();
298
299 if (extras == null) {
300 extras = EMPTY_EXTRAS;
301 }
302
303 final List<PhoneAccountHandle> phoneAccountHandles =
304 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
305
306 if ((call.getAccountHandle() == null
307 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700308 LogUtil.i(
309 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800310 return true;
311 }
312 }
313 return false;
314 }
315
316 public InCallState getInCallState() {
317 return mInCallState;
318 }
319
320 public CallList getCallList() {
321 return mCallList;
322 }
323
324 public void setUp(
325 @NonNull Context context,
326 CallList callList,
327 ExternalCallList externalCallList,
328 StatusBarNotifier statusBarNotifier,
329 ExternalCallNotifier externalCallNotifier,
330 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700331 ProximitySensor proximitySensor,
332 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700333 Trace.beginSection("InCallPresenter.setUp");
Eric Erfanianccca3152017-02-22 16:32:36 -0800334 if (mServiceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700335 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800336 if (context != mContext || callList != mCallList) {
337 throw new IllegalStateException();
338 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700339 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800340 return;
341 }
342
343 Objects.requireNonNull(context);
344 mContext = context;
345
346 mContactInfoCache = contactInfoCache;
347
348 mStatusBarNotifier = statusBarNotifier;
349 mExternalCallNotifier = externalCallNotifier;
350 addListener(mStatusBarNotifier);
Eric Erfaniand8046e52017-04-06 09:41:50 -0700351 EnrichedCallComponent.get(mContext)
352 .getEnrichedCallManager()
353 .registerStateChangedListener(mStatusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800354
355 mProximitySensor = proximitySensor;
356 addListener(mProximitySensor);
357
358 mThemeColorManager =
359 new ThemeColorManager(new InCallUIMaterialColorMapUtils(mContext.getResources()));
360
361 mCallList = callList;
362 mExternalCallList = externalCallList;
363 externalCallList.addExternalCallListener(mExternalCallNotifier);
364 externalCallList.addExternalCallListener(mExternalCallListener);
365
366 // This only gets called by the service so this is okay.
367 mServiceConnected = true;
368
369 // The final thing we do in this set up is add ourselves as a listener to CallList. This
370 // will kick off an update and the whole process can start.
371 mCallList.addListener(this);
372
373 // Create spam call list listener and add it to the list of listeners
Eric Erfanian2ca43182017-08-31 06:57:16 -0700374 mSpamCallListListener = new SpamCallListListener(context, new DefaultDialerExecutorFactory());
Eric Erfanianccca3152017-02-22 16:32:36 -0800375 mCallList.addListener(mSpamCallListListener);
376
377 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800378
Eric Erfanian83b20212017-05-31 08:53:10 -0700379 mFilteredQueryHandler = filteredNumberQueryHandler;
Eric Erfanianccca3152017-02-22 16:32:36 -0800380 mContext
381 .getSystemService(TelephonyManager.class)
382 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
383
Eric Erfanian2ca43182017-08-31 06:57:16 -0700384 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
385 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800386 }
387
388 /**
389 * Called when the telephony service has disconnected from us. This will happen when there are no
390 * more active calls. However, we may still want to continue showing the UI for certain cases like
391 * showing "Call Ended". What we really want is to wait for the activity and the service to both
392 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
393 * secondary method that performs the aforementioned logic.
394 */
395 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700396 LogUtil.d("InCallPresenter.tearDown", "tearDown");
Eric Erfanianccca3152017-02-22 16:32:36 -0800397 mCallList.clearOnDisconnect();
398
399 mServiceConnected = false;
400
401 mContext
402 .getSystemService(TelephonyManager.class)
403 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
404
405 attemptCleanup();
406 VideoPauseController.getInstance().tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -0800407 }
408
409 private void attemptFinishActivity() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700410 mScreenTimeoutEnabled = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800411 final boolean doFinish = (mInCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700412 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800413 if (doFinish) {
414 mInCallActivity.setExcludeFromRecents(true);
415 mInCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800416 }
417 }
418
419 /**
420 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
421 * for more insight on the tear-down process.
422 */
423 public void unsetActivity(InCallActivity inCallActivity) {
424 if (inCallActivity == null) {
425 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
426 }
427 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700428 LogUtil.i(
429 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800430 return;
431 }
432 if (mInCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700433 LogUtil.w(
434 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800435 "Second instance of InCallActivity is trying to unregister when another"
436 + " instance is active. Ignoring.");
437 return;
438 }
439 updateActivity(null);
440 }
441
442 /**
443 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
444 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
445 */
446 private void updateActivity(InCallActivity inCallActivity) {
447 boolean updateListeners = false;
448 boolean doAttemptCleanup = false;
449
450 if (inCallActivity != null) {
451 if (mInCallActivity == null) {
452 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700453 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800454 } else {
455 // since setActivity is called onStart(), it can be called multiple times.
456 // This is fine and ignorable, but we do not want to update the world every time
457 // this happens (like going to/from background) so we do not set updateListeners.
458 }
459
460 mInCallActivity = inCallActivity;
461 mInCallActivity.setExcludeFromRecents(false);
462
463 // By the time the UI finally comes up, the call may already be disconnected.
464 // If that's the case, we may need to show an error dialog.
465 if (mCallList != null && mCallList.getDisconnectedCall() != null) {
466 maybeShowErrorDialogOnDisconnect(mCallList.getDisconnectedCall());
467 }
468
469 // When the UI comes up, we need to first check the in-call state.
470 // If we are showing NO_CALLS, that means that a call probably connected and
471 // then immediately disconnected before the UI was able to come up.
472 // If we dont have any calls, start tearing down the UI instead.
473 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
474 // it has been set.
475 if (mInCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700476 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800477 attemptFinishActivity();
478 return;
479 }
480 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700481 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800482 updateListeners = true;
483 mInCallActivity = null;
484
485 // We attempt cleanup for the destroy case but only after we recalculate the state
486 // to see if we need to come back up or stay shut down. This is why we do the
487 // cleanup after the call to onCallListChange() instead of directly here.
488 doAttemptCleanup = true;
489 }
490
491 // Messages can come from the telephony layer while the activity is coming up
492 // and while the activity is going down. So in both cases we need to recalculate what
493 // state we should be in after they complete.
494 // Examples: (1) A new incoming call could come in and then get disconnected before
495 // the activity is created.
496 // (2) All calls could disconnect and then get a new incoming call before the
497 // activity is destroyed.
498 //
499 // b/1122139 - We previously had a check for mServiceConnected here as well, but there are
500 // cases where we need to recalculate the current state even if the service in not
501 // connected. In particular the case where startOrFinish() is called while the app is
502 // already finish()ing. In that case, we skip updating the state with the knowledge that
503 // we will check again once the activity has finished. That means we have to recalculate the
504 // state here even if the service is disconnected since we may not have finished a state
505 // transition while finish()ing.
506 if (updateListeners) {
507 onCallListChange(mCallList);
508 }
509
510 if (doAttemptCleanup) {
511 attemptCleanup();
512 }
513 }
514
515 public void setManageConferenceActivity(
516 @Nullable ManageConferenceActivity manageConferenceActivity) {
517 mManageConferenceActivity = manageConferenceActivity;
518 }
519
520 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700521 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800522 bringToForeground(showDialpad);
523 }
524
525 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700526 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800527 LatencyReport latencyReport = new LatencyReport(call);
528 if (shouldAttemptBlocking(call)) {
529 maybeBlockCall(call, latencyReport);
530 } else {
531 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
532 mExternalCallList.onCallAdded(call);
533 } else {
534 latencyReport.onCallBlockingDone();
535 mCallList.onCallAdded(mContext, call, latencyReport);
536 }
537 }
538
539 // Since a call has been added we are no longer waiting for Telecom to send us a call.
540 setBoundAndWaitingForOutgoingCall(false, null);
541 call.registerCallback(mCallCallback);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700542 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800543 }
544
545 private boolean shouldAttemptBlocking(android.telecom.Call call) {
546 if (call.getState() != android.telecom.Call.STATE_RINGING) {
547 return false;
548 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700549 if (!UserManagerCompat.isUserUnlocked(mContext)) {
550 LogUtil.i(
551 "InCallPresenter.shouldAttemptBlocking",
552 "not attempting to block incoming call because user is locked");
553 return false;
554 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800555 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700556 LogUtil.i(
557 "InCallPresenter.shouldAttemptBlocking",
558 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800559 return false;
560 }
561 if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700562 LogUtil.i(
563 "InCallPresenter.shouldAttemptBlocking",
564 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800565 return false;
566 }
567 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
568 return false;
569 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700570 if (FilteredNumberCompat.useNewFiltering(mContext)) {
571 LogUtil.i(
572 "InCallPresenter.shouldAttemptBlocking",
573 "not attempting to block incoming call because framework blocking is in use");
574 return false;
575 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800576 return true;
577 }
578
579 /**
580 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
581 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
582 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
583 * call anyways.
584 */
585 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
586 final String countryIso = GeoUtil.getCurrentCountryIso(mContext);
587 final String number = TelecomCallUtil.getNumber(call);
588 final long timeAdded = System.currentTimeMillis();
589
590 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
591 // main UI thread. It is needed so we can change its value within different scopes, since
592 // that cannot be done with a final boolean.
593 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
594
595 final Handler handler = new Handler();
596
597 // Proceed if the query is slow; the call may still be blocked after the query returns.
598 final Runnable runnable =
599 new Runnable() {
600 @Override
601 public void run() {
602 hasTimedOut.set(true);
603 latencyReport.onCallBlockingDone();
604 mCallList.onCallAdded(mContext, call, latencyReport);
605 }
606 };
607 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
608
609 OnCheckBlockedListener onCheckBlockedListener =
610 new OnCheckBlockedListener() {
611 @Override
612 public void onCheckComplete(final Integer id) {
613 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700614 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800615 return;
616 }
617 if (!hasTimedOut.get()) {
618 handler.removeCallbacks(runnable);
619 }
620 if (id == null) {
621 if (!hasTimedOut.get()) {
622 latencyReport.onCallBlockingDone();
623 mCallList.onCallAdded(mContext, call, latencyReport);
624 }
625 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700626 LogUtil.d(
627 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800628 if (!hasTimedOut.get()) {
629 handler.removeCallbacks(runnable);
630
631 latencyReport.onCallBlockingDone();
632 mCallList.onCallAdded(mContext, call, latencyReport);
633 }
634 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700635 LogUtil.i(
636 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800637 call.reject(false, null);
638 Logger.get(mContext).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
639
640 /*
641 * If mContext is null, then the InCallPresenter was torn down before the
642 * block check had a chance to complete. The context is no longer valid, so
643 * don't attempt to remove the call log entry.
644 */
645 if (mContext == null) {
646 return;
647 }
648 // Register observer to update the call log.
649 // BlockedNumberContentObserver will unregister after successful log or timeout.
650 BlockedNumberContentObserver contentObserver =
651 new BlockedNumberContentObserver(mContext, new Handler(), number, timeAdded);
652 contentObserver.register();
653 }
654 }
655 };
656
657 mFilteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
658 }
659
660 public void onCallRemoved(android.telecom.Call call) {
661 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
662 mExternalCallList.onCallRemoved(call);
663 } else {
664 mCallList.onCallRemoved(mContext, call);
665 call.unregisterCallback(mCallCallback);
666 }
667 }
668
669 public void onCanAddCallChanged(boolean canAddCall) {
670 for (CanAddCallListener listener : mCanAddCallListeners) {
671 listener.onCanAddCallChanged(canAddCall);
672 }
673 }
674
675 @Override
676 public void onWiFiToLteHandover(DialerCall call) {
677 if (mInCallActivity != null) {
678 mInCallActivity.onWiFiToLteHandover(call);
679 }
680 }
681
682 @Override
683 public void onHandoverToWifiFailed(DialerCall call) {
684 if (mInCallActivity != null) {
685 mInCallActivity.onHandoverToWifiFailed(call);
686 }
687 }
688
Eric Erfanianc857f902017-05-15 14:05:33 -0700689 @Override
690 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
691 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
692 if (mInCallActivity != null) {
693 mInCallActivity.onInternationalCallOnWifi(call);
694 }
695 }
696
Eric Erfanianccca3152017-02-22 16:32:36 -0800697 /**
698 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
699 * app based on the information it gets from CallList. Dispatches the in-call state to all
700 * listeners. Can trigger the creation or destruction of the UI based on the states that is
701 * calculates.
702 */
703 @Override
704 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700705 Trace.beginSection("InCallPresenter.onCallListChange");
Eric Erfanianccca3152017-02-22 16:32:36 -0800706 if (mInCallActivity != null && mInCallActivity.isInCallScreenAnimating()) {
707 mAwaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700708 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800709 return;
710 }
711 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700712 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800713 return;
714 }
715
716 mAwaitingCallListUpdate = false;
717
718 InCallState newState = getPotentialStateFromCallList(callList);
719 InCallState oldState = mInCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700720 LogUtil.d(
721 "InCallPresenter.onCallListChange",
722 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700723
724 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
725 // incall activity for that call will still exist (even if it's not visible). In the case of
726 // an incoming call in that situation, just disconnect that "waiting for account" call and
727 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
728 // b/33247755 for more details.
729 DialerCall waitingForAccountCall;
730 if (newState == InCallState.INCOMING
731 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
732 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700733 // The InCallActivity might be destroyed or not started yet at this point.
734 if (isActivityStarted()) {
735 mInCallActivity.dismissPendingDialogs();
736 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700737 }
738
Eric Erfanianccca3152017-02-22 16:32:36 -0800739 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700740 LogUtil.d(
741 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800742
743 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700744 LogUtil.i(
745 "InCallPresenter.onCallListChange",
746 "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800747 mInCallState = newState;
748
749 // notify listeners of new state
750 for (InCallStateListener listener : mListeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700751 LogUtil.d(
752 "InCallPresenter.onCallListChange",
753 "Notify " + listener + " of state " + mInCallState.toString());
Eric Erfanianccca3152017-02-22 16:32:36 -0800754 listener.onStateChange(oldState, mInCallState, callList);
755 }
756
757 if (isActivityStarted()) {
758 final boolean hasCall =
759 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
760 mInCallActivity.dismissKeyguard(hasCall);
761 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700762 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800763 }
764
765 /** Called when there is a new incoming call. */
766 @Override
767 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700768 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800769 InCallState newState = startOrFinishUi(InCallState.INCOMING);
770 InCallState oldState = mInCallState;
771
Eric Erfanian2ca43182017-08-31 06:57:16 -0700772 LogUtil.i(
773 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800774 mInCallState = newState;
775
wangqicf61ca02017-08-31 15:32:55 -0700776 Trace.beginSection("listener.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800777 for (IncomingCallListener listener : mIncomingCallListeners) {
778 listener.onIncomingCall(oldState, mInCallState, call);
779 }
wangqicf61ca02017-08-31 15:32:55 -0700780 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800781
wangqicf61ca02017-08-31 15:32:55 -0700782 Trace.beginSection("onPrimaryCallStateChanged");
Eric Erfanianccca3152017-02-22 16:32:36 -0800783 if (mInCallActivity != null) {
784 // Re-evaluate which fragment is being shown.
785 mInCallActivity.onPrimaryCallStateChanged();
786 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700787 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700788 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800789 }
790
791 @Override
792 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700793 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
Eric Erfanianccca3152017-02-22 16:32:36 -0800794 && mInCallState == InCallPresenter.InCallState.INCOMING) {
795 LogUtil.i(
796 "InCallPresenter.onUpgradeToVideo",
797 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700798 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800799 }
800
801 if (mInCallActivity != null) {
802 // Re-evaluate which fragment is being shown.
803 mInCallActivity.onPrimaryCallStateChanged();
804 }
805 }
806
807 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700808 public void onSessionModificationStateChange(DialerCall call) {
809 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800810 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
811 if (mProximitySensor == null) {
812 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
813 return;
814 }
815 mProximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700816 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
Eric Erfanianccca3152017-02-22 16:32:36 -0800817 if (mInCallActivity != null) {
818 // Re-evaluate which fragment is being shown.
819 mInCallActivity.onPrimaryCallStateChanged();
820 }
821 }
822
823 /**
824 * Called when a call becomes disconnected. Called everytime an existing call changes from being
825 * connected (incoming/outgoing/active) to disconnected.
826 */
827 @Override
828 public void onDisconnect(DialerCall call) {
829 maybeShowErrorDialogOnDisconnect(call);
830
831 // We need to do the run the same code as onCallListChange.
832 onCallListChange(mCallList);
833
834 if (isActivityStarted()) {
835 mInCallActivity.dismissKeyguard(false);
836 }
837
838 if (call.isEmergencyCall()) {
839 FilteredNumbersUtil.recordLastEmergencyCallTime(mContext);
840 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800841
Eric Erfanianfc37b022017-03-21 10:11:17 -0700842 if (!mCallList.hasLiveCall()
843 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700844 && !isSecretCode(call.getNumber())
Eric Erfanianfc37b022017-03-21 10:11:17 -0700845 && !CallerInfoUtils.isVoiceMailNumber(mContext, call)) {
Eric Erfanian10b34a52017-05-04 08:23:17 -0700846 PostCall.onCallDisconnected(mContext, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -0800847 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800848 }
849
Eric Erfanian10b34a52017-05-04 08:23:17 -0700850 private boolean isSecretCode(@Nullable String number) {
851 return number != null
852 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
853 }
854
Eric Erfanianccca3152017-02-22 16:32:36 -0800855 /** Given the call list, return the state in which the in-call screen should be. */
856 public InCallState getPotentialStateFromCallList(CallList callList) {
857
858 InCallState newState = InCallState.NO_CALLS;
859
860 if (callList == null) {
861 return newState;
862 }
863 if (callList.getIncomingCall() != null) {
864 newState = InCallState.INCOMING;
865 } else if (callList.getWaitingForAccountCall() != null) {
866 newState = InCallState.WAITING_FOR_ACCOUNT;
867 } else if (callList.getPendingOutgoingCall() != null) {
868 newState = InCallState.PENDING_OUTGOING;
869 } else if (callList.getOutgoingCall() != null) {
870 newState = InCallState.OUTGOING;
871 } else if (callList.getActiveCall() != null
872 || callList.getBackgroundCall() != null
873 || callList.getDisconnectedCall() != null
874 || callList.getDisconnectingCall() != null) {
875 newState = InCallState.INCALL;
876 }
877
878 if (newState == InCallState.NO_CALLS) {
879 if (mBoundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700880 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800881 }
882 }
883
884 return newState;
885 }
886
887 public boolean isBoundAndWaitingForOutgoingCall() {
888 return mBoundAndWaitingForOutgoingCall;
889 }
890
891 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700892 LogUtil.i(
893 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
894 "setBoundAndWaitingForOutgoingCall: " + isBound);
Eric Erfanianccca3152017-02-22 16:32:36 -0800895 mBoundAndWaitingForOutgoingCall = isBound;
896 mThemeColorManager.setPendingPhoneAccountHandle(handle);
897 if (isBound && mInCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700898 mInCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800899 }
900 }
901
902 public void onShrinkAnimationComplete() {
903 if (mAwaitingCallListUpdate) {
904 onCallListChange(mCallList);
905 }
906 }
907
908 public void addIncomingCallListener(IncomingCallListener listener) {
909 Objects.requireNonNull(listener);
910 mIncomingCallListeners.add(listener);
911 }
912
913 public void removeIncomingCallListener(IncomingCallListener listener) {
914 if (listener != null) {
915 mIncomingCallListeners.remove(listener);
916 }
917 }
918
919 public void addListener(InCallStateListener listener) {
920 Objects.requireNonNull(listener);
921 mListeners.add(listener);
922 }
923
924 public void removeListener(InCallStateListener listener) {
925 if (listener != null) {
926 mListeners.remove(listener);
927 }
928 }
929
930 public void addDetailsListener(InCallDetailsListener listener) {
931 Objects.requireNonNull(listener);
932 mDetailsListeners.add(listener);
933 }
934
935 public void removeDetailsListener(InCallDetailsListener listener) {
936 if (listener != null) {
937 mDetailsListeners.remove(listener);
938 }
939 }
940
941 public void addCanAddCallListener(CanAddCallListener listener) {
942 Objects.requireNonNull(listener);
943 mCanAddCallListeners.add(listener);
944 }
945
946 public void removeCanAddCallListener(CanAddCallListener listener) {
947 if (listener != null) {
948 mCanAddCallListeners.remove(listener);
949 }
950 }
951
952 public void addOrientationListener(InCallOrientationListener listener) {
953 Objects.requireNonNull(listener);
954 mOrientationListeners.add(listener);
955 }
956
957 public void removeOrientationListener(InCallOrientationListener listener) {
958 if (listener != null) {
959 mOrientationListeners.remove(listener);
960 }
961 }
962
963 public void addInCallEventListener(InCallEventListener listener) {
964 Objects.requireNonNull(listener);
965 mInCallEventListeners.add(listener);
966 }
967
968 public void removeInCallEventListener(InCallEventListener listener) {
969 if (listener != null) {
970 mInCallEventListeners.remove(listener);
971 }
972 }
973
974 public ProximitySensor getProximitySensor() {
975 return mProximitySensor;
976 }
977
978 public PseudoScreenState getPseudoScreenState() {
979 return mPseudoScreenState;
980 }
981
982 /** Returns true if the incall app is the foreground application. */
983 public boolean isShowingInCallUi() {
984 if (!isActivityStarted()) {
985 return false;
986 }
987 if (mManageConferenceActivity != null && mManageConferenceActivity.isVisible()) {
988 return true;
989 }
990 return mInCallActivity.isVisible();
991 }
992
993 /**
994 * Returns true if the activity has been created and is running. Returns true as long as activity
995 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
996 * (not in foreground).
997 */
998 public boolean isActivityStarted() {
999 return (mInCallActivity != null
1000 && !mInCallActivity.isDestroyed()
1001 && !mInCallActivity.isFinishing());
1002 }
1003
1004 /**
1005 * Determines if the In-Call app is currently changing configuration.
1006 *
1007 * @return {@code true} if the In-Call app is changing configuration.
1008 */
1009 public boolean isChangingConfigurations() {
1010 return mIsChangingConfigurations;
1011 }
1012
1013 /**
1014 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1015 * screen orientation).
1016 */
1017 /*package*/
1018 void updateIsChangingConfigurations() {
1019 mIsChangingConfigurations = false;
1020 if (mInCallActivity != null) {
1021 mIsChangingConfigurations = mInCallActivity.isChangingConfigurations();
1022 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001023 LogUtil.v(
1024 "InCallPresenter.updateIsChangingConfigurations",
1025 "updateIsChangingConfigurations = " + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001026 }
1027
1028 /** Called when the activity goes in/out of the foreground. */
1029 public void onUiShowing(boolean showing) {
1030 // We need to update the notification bar when we leave the UI because that
1031 // could trigger it to show again.
1032 if (mStatusBarNotifier != null) {
1033 mStatusBarNotifier.updateNotification(mCallList);
1034 }
1035
1036 if (mProximitySensor != null) {
1037 mProximitySensor.onInCallShowing(showing);
1038 }
1039
1040 Intent broadcastIntent = Bindings.get(mContext).getUiReadyBroadcastIntent(mContext);
1041 if (broadcastIntent != null) {
1042 broadcastIntent.putExtra(EXTRA_FIRST_TIME_SHOWN, !mIsActivityPreviouslyStarted);
1043
1044 if (showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001045 LogUtil.d("InCallPresenter.onUiShowing", "Sending sticky broadcast: ", broadcastIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001046 mContext.sendStickyBroadcast(broadcastIntent);
1047 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001048 LogUtil.d("InCallPresenter.onUiShowing", "Removing sticky broadcast: ", broadcastIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001049 mContext.removeStickyBroadcast(broadcastIntent);
1050 }
1051 }
1052
1053 if (showing) {
1054 mIsActivityPreviouslyStarted = true;
1055 } else {
1056 updateIsChangingConfigurations();
1057 }
1058
1059 for (InCallUiListener listener : mInCallUiListeners) {
1060 listener.onUiShowing(showing);
1061 }
1062
1063 if (mInCallActivity != null) {
1064 // Re-evaluate which fragment is being shown.
1065 mInCallActivity.onPrimaryCallStateChanged();
1066 }
1067 }
1068
Eric Erfanian2ca43182017-08-31 06:57:16 -07001069 public void refreshUi() {
1070 if (mInCallActivity != null) {
1071 // Re-evaluate which fragment is being shown.
1072 mInCallActivity.onPrimaryCallStateChanged();
1073 }
1074 }
1075
Eric Erfanianccca3152017-02-22 16:32:36 -08001076 public void addInCallUiListener(InCallUiListener listener) {
1077 mInCallUiListeners.add(listener);
1078 }
1079
1080 public boolean removeInCallUiListener(InCallUiListener listener) {
1081 return mInCallUiListeners.remove(listener);
1082 }
1083
1084 /*package*/
1085 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001086 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001087 notifyVideoPauseController(true);
Eric Erfaniand8046e52017-04-06 09:41:50 -07001088 if (mStatusBarNotifier != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001089 // TODO(maxwelb) - b/36649622: Investigate this redundant call
Eric Erfaniand8046e52017-04-06 09:41:50 -07001090 mStatusBarNotifier.updateNotification(mCallList);
1091 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001092 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001093 }
1094
1095 /*package*/
1096 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001097 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001098 notifyVideoPauseController(false);
1099 }
1100
1101 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001102 LogUtil.d(
1103 "InCallPresenter.notifyVideoPauseController",
1104 "mIsChangingConfigurations=" + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001105 if (!mIsChangingConfigurations) {
1106 VideoPauseController.getInstance().onUiShowing(showing);
1107 }
1108 }
1109
1110 /** Brings the app into the foreground if possible. */
1111 public void bringToForeground(boolean showDialpad) {
1112 // Before we bring the incall UI to the foreground, we check to see if:
1113 // 1. It is not currently in the foreground
1114 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1115 // be displayed)
1116 // If the activity hadn't actually been started previously, yet there are still calls
1117 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1118 // bring it up the UI regardless.
1119 if (!isShowingInCallUi() && mInCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001120 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001121 }
1122 }
1123
1124 public void onPostDialCharWait(String callId, String chars) {
1125 if (isActivityStarted()) {
1126 mInCallActivity.showPostCharWaitDialog(callId, chars);
1127 }
1128 }
1129
1130 /**
1131 * Handles the green CALL key while in-call.
1132 *
1133 * @return true if we consumed the event.
1134 */
1135 public boolean handleCallKey() {
1136 LogUtil.v("InCallPresenter.handleCallKey", null);
1137
1138 // The green CALL button means either "Answer", "Unhold", or
1139 // "Swap calls", or can be a no-op, depending on the current state
1140 // of the Phone.
1141
1142 /** INCOMING CALL */
1143 final CallList calls = mCallList;
1144 final DialerCall incomingCall = calls.getIncomingCall();
1145 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1146
1147 // (1) Attempt to answer a call
1148 if (incomingCall != null) {
1149 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1150 return true;
1151 }
1152
1153 /** STATE_ACTIVE CALL */
1154 final DialerCall activeCall = calls.getActiveCall();
1155 if (activeCall != null) {
1156 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1157 // consolidate this logic.
1158 final boolean canMerge =
1159 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1160 final boolean canSwap =
1161 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1162
Eric Erfanian2ca43182017-08-31 06:57:16 -07001163 LogUtil.v(
1164 "InCallPresenter.handleCallKey",
1165 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001166
1167 // (2) Attempt actions on conference calls
1168 if (canMerge) {
1169 TelecomAdapter.getInstance().merge(activeCall.getId());
1170 return true;
1171 } else if (canSwap) {
1172 TelecomAdapter.getInstance().swap(activeCall.getId());
1173 return true;
1174 }
1175 }
1176
1177 /** BACKGROUND CALL */
1178 final DialerCall heldCall = calls.getBackgroundCall();
1179 if (heldCall != null) {
1180 // We have a hold call so presumeable it will always support HOLD...but
1181 // there is no harm in double checking.
1182 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1183
Eric Erfanian2ca43182017-08-31 06:57:16 -07001184 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001185
1186 // (4) unhold call
1187 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1188 heldCall.unhold();
1189 return true;
1190 }
1191 }
1192
1193 // Always consume hard keys
1194 return true;
1195 }
1196
Eric Erfanianccca3152017-02-22 16:32:36 -08001197 /** Clears the previous fullscreen state. */
1198 public void clearFullscreen() {
1199 mIsFullScreen = false;
1200 }
1201
1202 /**
1203 * Changes the fullscreen mode of the in-call UI.
1204 *
1205 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1206 * otherwise.
1207 */
1208 public void setFullScreen(boolean isFullScreen) {
1209 setFullScreen(isFullScreen, false /* force */);
1210 }
1211
1212 /**
1213 * Changes the fullscreen mode of the in-call UI.
1214 *
1215 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1216 * otherwise.
1217 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1218 */
1219 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001220 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001221
1222 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1223 if (isDialpadVisible()) {
1224 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001225 LogUtil.v(
1226 "InCallPresenter.setFullScreen",
1227 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001228 }
1229
1230 if (mIsFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001231 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001232 return;
1233 }
1234 mIsFullScreen = isFullScreen;
1235 notifyFullscreenModeChange(mIsFullScreen);
1236 }
1237
1238 /**
1239 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1240 * otherwise.
1241 */
1242 public boolean isFullscreen() {
1243 return mIsFullScreen;
1244 }
1245
1246 /**
1247 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1248 *
1249 * @param isFullscreenMode {@code True} if entering full screen mode.
1250 */
1251 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
1252 for (InCallEventListener listener : mInCallEventListeners) {
1253 listener.onFullscreenModeChanged(isFullscreenMode);
1254 }
1255 }
1256
1257 /**
1258 * For some disconnected causes, we show a dialog. This calls into the activity to show the dialog
1259 * if appropriate for the call.
1260 */
1261 private void maybeShowErrorDialogOnDisconnect(DialerCall call) {
1262 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1263 if (isActivityStarted() && call.getState() == DialerCall.State.DISCONNECTED) {
1264 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1265 setDisconnectCauseForMissingAccounts(call);
1266 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001267 mInCallActivity.maybeShowErrorDialogOnDisconnect(
1268 new DisconnectMessage(mInCallActivity, call));
Eric Erfanianccca3152017-02-22 16:32:36 -08001269 }
1270 }
1271
1272 /**
1273 * When the state of in-call changes, this is the first method to get called. It determines if the
1274 * UI needs to be started or finished depending on the new state and does it.
1275 */
1276 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001277 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001278 LogUtil.d(
1279 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + mInCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001280
1281 // TODO: Consider a proper state machine implementation
1282
1283 // If the state isn't changing we have already done any starting/stopping of activities in
1284 // a previous pass...so lets cut out early
1285 if (newState == mInCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001286 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001287 return newState;
1288 }
1289
1290 // A new Incoming call means that the user needs to be notified of the the call (since
1291 // it wasn't them who initiated it). We do this through full screen notifications and
1292 // happens indirectly through {@link StatusBarNotifier}.
1293 //
1294 // The process for incoming calls is as follows:
1295 //
1296 // 1) CallList - Announces existence of new INCOMING call
1297 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1298 // - should be set to INCOMING.
1299 // 3) InCallPresenter - This method is called to see if we need to start or finish
1300 // the app given the new state.
1301 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1302 // StatusBarNotifier explicitly to issue a FullScreen Notification
1303 // that will either start the InCallActivity or show the user a
1304 // top-level notification dialog if the user is in an immersive app.
1305 // That notification can also start the InCallActivity.
1306 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1307 // call InCallPresenter::setActivity() to let the presenter
1308 // know that start-up is complete.
1309 //
1310 // [ AND NOW YOU'RE IN THE CALL. voila! ]
1311 //
1312 // Our app is started using a fullScreen notification. We need to do this whenever
1313 // we get an incoming call. Depending on the current context of the device, either a
1314 // incoming call HUN or the actual InCallActivity will be shown.
1315 final boolean startIncomingCallSequence = (InCallState.INCOMING == newState);
1316
1317 // A dialog to show on top of the InCallUI to select a PhoneAccount
1318 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1319
1320 // A new outgoing call indicates that the user just now dialed a number and when that
1321 // happens we need to display the screen immediately or show an account picker dialog if
1322 // no default is set. However, if the main InCallUI is already visible, we do not want to
1323 // re-initiate the start-up animation, so we do not need to do anything here.
1324 //
1325 // It is also possible to go into an intermediate state where the call has been initiated
1326 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1327 // This pending outgoing state can also launch the call screen.
1328 //
1329 // This is different from the incoming call sequence because we do not need to shock the
1330 // user with a top-level notification. Just show the call UI normally.
1331 boolean callCardFragmentVisible =
1332 mInCallActivity != null && mInCallActivity.getCallCardFragmentVisible();
1333 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1334 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1335
1336 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1337 // outgoing call process, so the UI should be brought up to show an error dialog.
1338 showCallUi |=
1339 (InCallState.PENDING_OUTGOING == mInCallState
1340 && InCallState.INCALL == newState
1341 && !isShowingInCallUi());
1342
1343 // Another exception - InCallActivity is in charge of disconnecting a call with no
1344 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1345 // call so that:
1346 // 1) The call can be disconnected correctly
1347 // 2) The UI comes up and correctly displays the error dialog.
1348 // TODO: Remove these special case conditions by making InCallPresenter a true state
1349 // machine. Telecom should also be the component responsible for disconnecting a call
1350 // with no valid accounts.
1351 showCallUi |=
1352 InCallState.PENDING_OUTGOING == newState
1353 && mainUiNotVisible
1354 && isCallWithNoValidAccounts(mCallList.getPendingOutgoingCall());
1355
1356 // The only time that we have an instance of mInCallActivity and it isn't started is
1357 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1358 // the activity. When it is finally destroyed, we double check if we should bring it back
1359 // up so we aren't going to lose anything by avoiding a second startup here.
1360 boolean activityIsFinishing = mInCallActivity != null && !isActivityStarted();
1361 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001362 LogUtil.i(
1363 "InCallPresenter.startOrFinishUi",
1364 "Undo the state change: " + newState + " -> " + mInCallState);
wangqicf61ca02017-08-31 15:32:55 -07001365 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001366 return mInCallState;
1367 }
1368
1369 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1370 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1371 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1372 // outgoing call after user ignore it by pressing home button.
1373 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1374 && !showCallUi
1375 && isActivityStarted()) {
1376 mInCallActivity.dismissPendingDialogs();
1377 }
1378
1379 if (showCallUi || showAccountPicker) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001380 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001381 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001382 } else if (startIncomingCallSequence) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001383 LogUtil.i("InCallPresenter.startOrFinishUi", "Start Full Screen in call UI");
Eric Erfanianccca3152017-02-22 16:32:36 -08001384
Eric Erfaniand8046e52017-04-06 09:41:50 -07001385 mStatusBarNotifier.updateNotification(mCallList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001386 } else if (newState == InCallState.NO_CALLS) {
1387 // The new state is the no calls state. Tear everything down.
1388 attemptFinishActivity();
1389 attemptCleanup();
1390 }
1391
wangqicf61ca02017-08-31 15:32:55 -07001392 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001393 return newState;
1394 }
1395
1396 /**
1397 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1398 * or PhoneAccounts to select from.
1399 */
1400 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1401
1402 Bundle extras = call.getIntentExtras();
1403 // Initialize the extras bundle to avoid NPE
1404 if (extras == null) {
1405 extras = new Bundle();
1406 }
1407
1408 final List<PhoneAccountHandle> phoneAccountHandles =
1409 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1410
1411 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1412 String scheme = call.getHandle().getScheme();
1413 final String errorMsg =
1414 PhoneAccount.SCHEME_TEL.equals(scheme)
1415 ? mContext.getString(R.string.callFailed_simError)
1416 : mContext.getString(R.string.incall_error_supp_service_unknown);
1417 DisconnectCause disconnectCause =
1418 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1419 call.setDisconnectCause(disconnectCause);
1420 }
1421 }
1422
Eric Erfanianccca3152017-02-22 16:32:36 -08001423 /**
1424 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1425 * Calling classes should use this as an indication whether to interact with the
1426 * InCallPresenter or not.
1427 */
1428 public boolean isReadyForTearDown() {
1429 return mInCallActivity == null && !mServiceConnected && mInCallState == InCallState.NO_CALLS;
1430 }
1431
1432 /**
1433 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1434 */
1435 private void attemptCleanup() {
1436 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001437 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001438
1439 cleanupSurfaces();
1440
1441 mIsActivityPreviouslyStarted = false;
1442 mIsChangingConfigurations = false;
1443
1444 // blow away stale contact info so that we get fresh data on
1445 // the next set of calls
1446 if (mContactInfoCache != null) {
1447 mContactInfoCache.clearCache();
1448 }
1449 mContactInfoCache = null;
1450
1451 if (mProximitySensor != null) {
1452 removeListener(mProximitySensor);
1453 mProximitySensor.tearDown();
1454 }
1455 mProximitySensor = null;
1456
1457 if (mStatusBarNotifier != null) {
1458 removeListener(mStatusBarNotifier);
Eric Erfaniand8046e52017-04-06 09:41:50 -07001459 EnrichedCallComponent.get(mContext)
1460 .getEnrichedCallManager()
1461 .unregisterStateChangedListener(mStatusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001462 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001463
Eric Erfanianccca3152017-02-22 16:32:36 -08001464 if (mExternalCallNotifier != null && mExternalCallList != null) {
1465 mExternalCallList.removeExternalCallListener(mExternalCallNotifier);
1466 }
1467 mStatusBarNotifier = null;
1468
1469 if (mCallList != null) {
1470 mCallList.removeListener(this);
1471 mCallList.removeListener(mSpamCallListListener);
1472 }
1473 mCallList = null;
1474
1475 mContext = null;
1476 mInCallActivity = null;
1477 mManageConferenceActivity = null;
1478
1479 mListeners.clear();
1480 mIncomingCallListeners.clear();
1481 mDetailsListeners.clear();
1482 mCanAddCallListeners.clear();
1483 mOrientationListeners.clear();
1484 mInCallEventListeners.clear();
1485 mInCallUiListeners.clear();
twyen8efb4952017-10-06 16:35:54 -07001486 if (!mInCallUiLocks.isEmpty()) {
1487 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + mInCallUiLocks);
1488 mInCallUiLocks.clear();
1489 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001490 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001491 }
1492 }
1493
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001494 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001495 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -08001496 mContext.startActivity(
1497 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001498 mContext, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001499 }
1500
1501 public void onServiceBind() {
1502 mServiceBound = true;
1503 }
1504
1505 public void onServiceUnbind() {
1506 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
1507 mServiceBound = false;
1508 }
1509
1510 public boolean isServiceBound() {
1511 return mServiceBound;
1512 }
1513
1514 public void maybeStartRevealAnimation(Intent intent) {
1515 if (intent == null || mInCallActivity != null) {
1516 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 =
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001536 InCallActivity.getIntent(mContext, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001537 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
1538 mContext.startActivity(activityIntent);
1539 }
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) {
1548 if (mInCallCameraManager == null) {
1549 mInCallCameraManager = new InCallCameraManager(mContext);
1550 }
1551
1552 return mInCallCameraManager;
1553 }
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
1570 if (mCallList != null) {
1571 mCallList.notifyCallsOfDeviceRotation(orientation);
1572 } 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.
1577 for (InCallOrientationListener listener : mOrientationListeners) {
1578 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) {
1590 if (mInCallActivity == 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 }
1596 mInCallActivity.setAllowOrientationChange(allowOrientationChange);
1597 }
1598
1599 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001600 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
1601 mScreenTimeoutEnabled = enable;
1602 applyScreenTimeout();
1603 }
1604
1605 private void applyScreenTimeout() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001606 if (mInCallActivity == 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
1611 final Window window = mInCallActivity.getWindow();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001612 if (mScreenTimeoutEnabled) {
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) {
1626 if (mInCallActivity != null) {
1627 mInCallActivity.showConferenceFragment(show);
1628 }
1629 if (!show && mManageConferenceActivity != null) {
1630 mManageConferenceActivity.finish();
1631 }
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() {
1640 if (mInCallActivity == null) {
1641 return false;
1642 }
1643 return mInCallActivity.isDialpadVisible();
1644 }
1645
1646 public ThemeColorManager getThemeColorManager() {
1647 return mThemeColorManager;
1648 }
1649
1650 /** Called when the foreground call changes. */
1651 public void onForegroundCallChanged(DialerCall newForegroundCall) {
1652 mThemeColorManager.onForegroundCallChanged(mContext, newForegroundCall);
1653 if (mInCallActivity != null) {
1654 mInCallActivity.onForegroundCallChanged(newForegroundCall);
1655 }
1656 }
1657
1658 public InCallActivity getActivity() {
1659 return mInCallActivity;
1660 }
1661
1662 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1663 public void setActivity(InCallActivity inCallActivity) {
1664 if (inCallActivity == null) {
1665 throw new IllegalArgumentException("registerActivity cannot be called with null");
1666 }
1667 if (mInCallActivity != null && mInCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001668 LogUtil.w(
1669 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001670 }
1671 updateActivity(inCallActivity);
1672 }
1673
1674 ExternalCallNotifier getExternalCallNotifier() {
1675 return mExternalCallNotifier;
1676 }
1677
1678 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
1679 if (mLocalVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001680 boolean isPixel2017 = false;
1681 if (mContext != null) {
1682 isPixel2017 = mContext.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
1683 }
1684 mLocalVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001685 }
1686 return mLocalVideoSurfaceTexture;
1687 }
1688
1689 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
1690 if (mRemoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001691 boolean isPixel2017 = false;
1692 if (mContext != null) {
1693 isPixel2017 = mContext.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
1694 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001695 mRemoteVideoSurfaceTexture =
roldenburg12b50c62017-09-01 14:41:19 -07001696 VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001697 }
1698 return mRemoteVideoSurfaceTexture;
1699 }
1700
1701 void cleanupSurfaces() {
1702 if (mRemoteVideoSurfaceTexture != null) {
1703 mRemoteVideoSurfaceTexture.setDoneWithSurface();
1704 mRemoteVideoSurfaceTexture = null;
1705 }
1706 if (mLocalVideoSurfaceTexture != null) {
1707 mLocalVideoSurfaceTexture.setDoneWithSurface();
1708 mLocalVideoSurfaceTexture = null;
1709 }
1710 }
1711
1712 /** All the main states of InCallActivity. */
1713 public enum InCallState {
1714 // InCall Screen is off and there are no calls
1715 NO_CALLS,
1716
1717 // Incoming-call screen is up
1718 INCOMING,
1719
1720 // In-call experience is showing
1721 INCALL,
1722
1723 // Waiting for user input before placing outgoing call
1724 WAITING_FOR_ACCOUNT,
1725
1726 // UI is starting up but no call has been initiated yet.
1727 // The UI is waiting for Telecom to respond.
1728 PENDING_OUTGOING,
1729
1730 // User is dialing out
1731 OUTGOING;
1732
1733 public boolean isIncoming() {
1734 return (this == INCOMING);
1735 }
1736
1737 public boolean isConnectingOrConnected() {
1738 return (this == INCOMING || this == OUTGOING || this == INCALL);
1739 }
1740 }
1741
1742 /** Interface implemented by classes that need to know about the InCall State. */
1743 public interface InCallStateListener {
1744
1745 // TODO: Enhance state to contain the call objects instead of passing CallList
1746 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1747 }
1748
1749 public interface IncomingCallListener {
1750
1751 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1752 }
1753
1754 public interface CanAddCallListener {
1755
1756 void onCanAddCallChanged(boolean canAddCall);
1757 }
1758
1759 public interface InCallDetailsListener {
1760
1761 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1762 }
1763
1764 public interface InCallOrientationListener {
1765
1766 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1767 }
1768
1769 /**
1770 * Interface implemented by classes that need to know about events which occur within the In-Call
1771 * UI. Used as a means of communicating between fragments that make up the UI.
1772 */
1773 public interface InCallEventListener {
1774
1775 void onFullscreenModeChanged(boolean isFullscreenMode);
1776 }
1777
1778 public interface InCallUiListener {
1779
1780 void onUiShowing(boolean showing);
1781 }
twyen8efb4952017-10-06 16:35:54 -07001782
1783 private class InCallUiLockImpl implements InCallUiLock {
1784 private final String tag;
1785
1786 private InCallUiLockImpl(String tag) {
1787 this.tag = tag;
1788 }
1789
1790 @MainThread
1791 @Override
1792 public void release() {
1793 Assert.isMainThread();
1794 releaseInCallUiLock(InCallUiLockImpl.this);
1795 }
1796
1797 @Override
1798 public String toString() {
1799 return "InCallUiLock[" + tag + "]";
1800 }
1801 }
1802
1803 @MainThread
1804 public InCallUiLock acquireInCallUiLock(String tag) {
1805 Assert.isMainThread();
1806 InCallUiLock lock = new InCallUiLockImpl(tag);
1807 mInCallUiLocks.add(lock);
1808 return lock;
1809 }
1810
1811 @MainThread
1812 private void releaseInCallUiLock(InCallUiLock lock) {
1813 Assert.isMainThread();
1814 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
1815 mInCallUiLocks.remove(lock);
1816 if (mInCallUiLocks.isEmpty()) {
1817 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
1818 if (mInCallState == InCallState.NO_CALLS) {
1819 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
1820 attemptFinishActivity();
1821 attemptCleanup();
1822 }
1823 }
1824 }
1825
1826 @MainThread
1827 public boolean isInCallUiLocked() {
1828 Assert.isMainThread();
1829 if (mInCallUiLocks.isEmpty()) {
1830 return false;
1831 }
1832 for (InCallUiLock lock : mInCallUiLocks) {
1833 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
1834 }
1835 return true;
1836 }
1837
1838 private final Set<InCallUiLock> mInCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08001839}