blob: 9b1f674bdb2ab2184757d43e0129472626912aa1 [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;
Eric Erfanianccca3152017-02-22 16:32:36 -080025import android.support.annotation.NonNull;
26import android.support.annotation.Nullable;
27import android.support.annotation.VisibleForTesting;
Eric Erfanian83b20212017-05-31 08:53:10 -070028import android.support.v4.os.UserManagerCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080029import android.telecom.Call.Details;
30import android.telecom.DisconnectCause;
31import android.telecom.PhoneAccount;
32import android.telecom.PhoneAccountHandle;
33import android.telecom.TelecomManager;
34import android.telecom.VideoProfile;
35import android.telephony.PhoneStateListener;
36import android.telephony.TelephonyManager;
37import android.view.Window;
38import android.view.WindowManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080039import com.android.contacts.common.compat.CallCompat;
40import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler;
41import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler.OnCheckBlockedListener;
Eric Erfanian83b20212017-05-31 08:53:10 -070042import com.android.dialer.blocking.FilteredNumberCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080043import com.android.dialer.blocking.FilteredNumbersUtil;
44import com.android.dialer.common.LogUtil;
Eric Erfanian2ca43182017-08-31 06:57:16 -070045import com.android.dialer.common.concurrent.DefaultDialerExecutorFactory;
Eric Erfaniand8046e52017-04-06 09:41:50 -070046import com.android.dialer.enrichedcall.EnrichedCallComponent;
Eric Erfanian10b34a52017-05-04 08:23:17 -070047import com.android.dialer.location.GeoUtil;
Eric Erfanian8369df02017-05-03 10:27:13 -070048import com.android.dialer.logging.InteractionEvent;
Eric Erfanianccca3152017-02-22 16:32:36 -080049import com.android.dialer.logging.Logger;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070050import com.android.dialer.postcall.PostCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080051import com.android.dialer.telecom.TelecomUtil;
52import com.android.dialer.util.TouchPointManager;
53import com.android.incallui.InCallOrientationEventListener.ScreenOrientation;
54import com.android.incallui.answerproximitysensor.PseudoScreenState;
55import com.android.incallui.call.CallList;
56import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080057import com.android.incallui.call.ExternalCallList;
Eric Erfanianccca3152017-02-22 16:32:36 -080058import com.android.incallui.call.TelecomAdapter;
Eric Erfanian2ca43182017-08-31 06:57:16 -070059import com.android.incallui.disconnectdialog.DisconnectMessage;
Eric Erfanianccca3152017-02-22 16:32:36 -080060import com.android.incallui.latencyreport.LatencyReport;
61import com.android.incallui.legacyblocking.BlockedNumberContentObserver;
62import com.android.incallui.spam.SpamCallListListener;
63import com.android.incallui.util.TelecomCallUtil;
64import com.android.incallui.videosurface.bindings.VideoSurfaceBindings;
65import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
Eric Erfanian90508232017-03-24 09:31:16 -070066import com.android.incallui.videotech.utils.VideoUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080067import java.util.Collections;
68import java.util.List;
69import java.util.Objects;
70import java.util.Set;
71import java.util.concurrent.ConcurrentHashMap;
72import java.util.concurrent.CopyOnWriteArrayList;
73import java.util.concurrent.atomic.AtomicBoolean;
74
75/**
76 * Takes updates from the CallList and notifies the InCallActivity (UI) of the changes. Responsible
77 * for starting the activity for a new call and finishing the activity when all calls are
78 * disconnected. Creates and manages the in-call state and provides a listener pattern for the
79 * presenters that want to listen in on the in-call state changes. TODO: This class has become more
80 * of a state machine at this point. Consider renaming.
81 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -070082public class InCallPresenter implements CallList.Listener {
Eric Erfanian2ca43182017-08-31 06:57:16 -070083 private static final String PIXEL2017_SYSTEM_FEATURE =
84 "com.google.android.feature.PIXEL_2017_EXPERIENCE";
Eric Erfanianccca3152017-02-22 16:32:36 -080085 private static final String EXTRA_FIRST_TIME_SHOWN =
86 "com.android.incallui.intent.extra.FIRST_TIME_SHOWN";
87
88 private static final long BLOCK_QUERY_TIMEOUT_MS = 1000;
89
90 private static final Bundle EMPTY_EXTRAS = new Bundle();
91
92 private static InCallPresenter sInCallPresenter;
93
94 /**
95 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is load factor before
96 * resizing, 1 means we only expect a single thread to access the map so make only a single shard
97 */
98 private final Set<InCallStateListener> mListeners =
99 Collections.newSetFromMap(new ConcurrentHashMap<InCallStateListener, Boolean>(8, 0.9f, 1));
100
101 private final List<IncomingCallListener> mIncomingCallListeners = new CopyOnWriteArrayList<>();
102 private final Set<InCallDetailsListener> mDetailsListeners =
103 Collections.newSetFromMap(new ConcurrentHashMap<InCallDetailsListener, Boolean>(8, 0.9f, 1));
104 private final Set<CanAddCallListener> mCanAddCallListeners =
105 Collections.newSetFromMap(new ConcurrentHashMap<CanAddCallListener, Boolean>(8, 0.9f, 1));
106 private final Set<InCallUiListener> mInCallUiListeners =
107 Collections.newSetFromMap(new ConcurrentHashMap<InCallUiListener, Boolean>(8, 0.9f, 1));
108 private final Set<InCallOrientationListener> mOrientationListeners =
109 Collections.newSetFromMap(
110 new ConcurrentHashMap<InCallOrientationListener, Boolean>(8, 0.9f, 1));
111 private final Set<InCallEventListener> mInCallEventListeners =
112 Collections.newSetFromMap(new ConcurrentHashMap<InCallEventListener, Boolean>(8, 0.9f, 1));
113
114 private StatusBarNotifier mStatusBarNotifier;
115 private ExternalCallNotifier mExternalCallNotifier;
116 private ContactInfoCache mContactInfoCache;
117 private Context mContext;
118 private final OnCheckBlockedListener mOnCheckBlockedListener =
119 new OnCheckBlockedListener() {
120 @Override
121 public void onCheckComplete(final Integer id) {
122 if (id != null && id != FilteredNumberAsyncQueryHandler.INVALID_ID) {
123 // Silence the ringer now to prevent ringing and vibration before the call is
124 // terminated when Telecom attempts to add it.
125 TelecomUtil.silenceRinger(mContext);
126 }
127 }
128 };
129 private CallList mCallList;
130 private ExternalCallList mExternalCallList;
131 private InCallActivity mInCallActivity;
132 private ManageConferenceActivity mManageConferenceActivity;
133 private final android.telecom.Call.Callback mCallCallback =
134 new android.telecom.Call.Callback() {
135 @Override
136 public void onPostDialWait(
137 android.telecom.Call telecomCall, String remainingPostDialSequence) {
138 final DialerCall call = mCallList.getDialerCallFromTelecomCall(telecomCall);
139 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700140 LogUtil.w(
141 "InCallPresenter.onPostDialWait",
142 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800143 return;
144 }
145 onPostDialCharWait(call.getId(), remainingPostDialSequence);
146 }
147
148 @Override
149 public void onDetailsChanged(
150 android.telecom.Call telecomCall, android.telecom.Call.Details details) {
151 final DialerCall call = mCallList.getDialerCallFromTelecomCall(telecomCall);
152 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700153 LogUtil.w(
154 "InCallPresenter.onDetailsChanged",
155 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800156 return;
157 }
158
159 if (details.hasProperty(Details.PROPERTY_IS_EXTERNAL_CALL)
160 && !mExternalCallList.isCallTracked(telecomCall)) {
161
162 // A regular call became an external call so swap call lists.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700163 LogUtil.i("InCallPresenter.onDetailsChanged", "Call became external: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800164 mCallList.onInternalCallMadeExternal(mContext, telecomCall);
165 mExternalCallList.onCallAdded(telecomCall);
166 return;
167 }
168
169 for (InCallDetailsListener listener : mDetailsListeners) {
170 listener.onDetailsChanged(call, details);
171 }
172 }
173
174 @Override
175 public void onConferenceableCallsChanged(
176 android.telecom.Call telecomCall, List<android.telecom.Call> conferenceableCalls) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700177 LogUtil.i(
178 "InCallPresenter.onConferenceableCallsChanged",
179 "onConferenceableCallsChanged: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800180 onDetailsChanged(telecomCall, telecomCall.getDetails());
181 }
182 };
183 private InCallState mInCallState = InCallState.NO_CALLS;
184 private ProximitySensor mProximitySensor;
185 private final PseudoScreenState mPseudoScreenState = new PseudoScreenState();
186 private boolean mServiceConnected;
Eric Erfanianccca3152017-02-22 16:32:36 -0800187 private InCallCameraManager mInCallCameraManager;
188 private FilteredNumberAsyncQueryHandler mFilteredQueryHandler;
189 private CallList.Listener mSpamCallListListener;
190 /** Whether or not we are currently bound and waiting for Telecom to send us a new call. */
191 private boolean mBoundAndWaitingForOutgoingCall;
192 /** Determines if the InCall UI is in fullscreen mode or not. */
193 private boolean mIsFullScreen = false;
194
Eric Erfanian2ca43182017-08-31 06:57:16 -0700195 private boolean mScreenTimeoutEnabled = true;
196
Eric Erfanianccca3152017-02-22 16:32:36 -0800197 private PhoneStateListener mPhoneStateListener =
198 new PhoneStateListener() {
199 @Override
200 public void onCallStateChanged(int state, String incomingNumber) {
201 if (state == TelephonyManager.CALL_STATE_RINGING) {
202 if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
203 return;
204 }
205 // Check if the number is blocked, to silence the ringer.
206 String countryIso = GeoUtil.getCurrentCountryIso(mContext);
207 mFilteredQueryHandler.isBlockedNumber(
208 mOnCheckBlockedListener, incomingNumber, countryIso);
209 }
210 }
211 };
212 /**
213 * Is true when the activity has been previously started. Some code needs to know not just if the
214 * activity is currently up, but if it had been previously shown in foreground for this in-call
215 * session (e.g., StatusBarNotifier). This gets reset when the session ends in the tear-down
216 * method.
217 */
218 private boolean mIsActivityPreviouslyStarted = false;
219
220 /** Whether or not InCallService is bound to Telecom. */
221 private boolean mServiceBound = false;
222
223 /**
224 * When configuration changes Android kills the current activity and starts a new one. The flag is
225 * used to check if full clean up is necessary (activity is stopped and new activity won't be
226 * started), or if a new activity will be started right after the current one is destroyed, and
227 * therefore no need in release all resources.
228 */
229 private boolean mIsChangingConfigurations = false;
230
231 private boolean mAwaitingCallListUpdate = false;
232
233 private ExternalCallList.ExternalCallListener mExternalCallListener =
234 new ExternalCallList.ExternalCallListener() {
235
236 @Override
237 public void onExternalCallPulled(android.telecom.Call call) {
238 // Note: keep this code in sync with InCallPresenter#onCallAdded
239 LatencyReport latencyReport = new LatencyReport(call);
240 latencyReport.onCallBlockingDone();
241 // Note: External calls do not require spam checking.
242 mCallList.onCallAdded(mContext, call, latencyReport);
243 call.registerCallback(mCallCallback);
244 }
245
246 @Override
247 public void onExternalCallAdded(android.telecom.Call call) {
248 // No-op
249 }
250
251 @Override
252 public void onExternalCallRemoved(android.telecom.Call call) {
253 // No-op
254 }
255
256 @Override
257 public void onExternalCallUpdated(android.telecom.Call call) {
258 // No-op
259 }
260 };
261
262 private ThemeColorManager mThemeColorManager;
263 private VideoSurfaceTexture mLocalVideoSurfaceTexture;
264 private VideoSurfaceTexture mRemoteVideoSurfaceTexture;
265
Eric Erfanian10b34a52017-05-04 08:23:17 -0700266 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800267 @VisibleForTesting
268 InCallPresenter() {}
269
270 public static synchronized InCallPresenter getInstance() {
271 if (sInCallPresenter == null) {
272 sInCallPresenter = new InCallPresenter();
273 }
274 return sInCallPresenter;
275 }
276
Eric Erfanian10b34a52017-05-04 08:23:17 -0700277 @VisibleForTesting
278 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
279 sInCallPresenter = inCallPresenter;
280 }
281
Eric Erfanianccca3152017-02-22 16:32:36 -0800282 /**
283 * Determines whether or not a call has no valid phone accounts that can be used to make the call
284 * with. Emergency calls do not require a phone account.
285 *
286 * @param call to check accounts for.
287 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
288 * call contains a phone account that could be used to initiate it with, or is an emergency
289 * call.
290 */
291 public static boolean isCallWithNoValidAccounts(DialerCall call) {
292 if (call != null && !call.isEmergencyCall()) {
293 Bundle extras = call.getIntentExtras();
294
295 if (extras == null) {
296 extras = EMPTY_EXTRAS;
297 }
298
299 final List<PhoneAccountHandle> phoneAccountHandles =
300 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
301
302 if ((call.getAccountHandle() == null
303 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700304 LogUtil.i(
305 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800306 return true;
307 }
308 }
309 return false;
310 }
311
312 public InCallState getInCallState() {
313 return mInCallState;
314 }
315
316 public CallList getCallList() {
317 return mCallList;
318 }
319
320 public void setUp(
321 @NonNull Context context,
322 CallList callList,
323 ExternalCallList externalCallList,
324 StatusBarNotifier statusBarNotifier,
325 ExternalCallNotifier externalCallNotifier,
326 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700327 ProximitySensor proximitySensor,
328 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700329 Trace.beginSection("InCallPresenter.setUp");
Eric Erfanianccca3152017-02-22 16:32:36 -0800330 if (mServiceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700331 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800332 if (context != mContext || callList != mCallList) {
333 throw new IllegalStateException();
334 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700335 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800336 return;
337 }
338
339 Objects.requireNonNull(context);
340 mContext = context;
341
342 mContactInfoCache = contactInfoCache;
343
344 mStatusBarNotifier = statusBarNotifier;
345 mExternalCallNotifier = externalCallNotifier;
346 addListener(mStatusBarNotifier);
Eric Erfaniand8046e52017-04-06 09:41:50 -0700347 EnrichedCallComponent.get(mContext)
348 .getEnrichedCallManager()
349 .registerStateChangedListener(mStatusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800350
351 mProximitySensor = proximitySensor;
352 addListener(mProximitySensor);
353
354 mThemeColorManager =
355 new ThemeColorManager(new InCallUIMaterialColorMapUtils(mContext.getResources()));
356
357 mCallList = callList;
358 mExternalCallList = externalCallList;
359 externalCallList.addExternalCallListener(mExternalCallNotifier);
360 externalCallList.addExternalCallListener(mExternalCallListener);
361
362 // This only gets called by the service so this is okay.
363 mServiceConnected = true;
364
365 // The final thing we do in this set up is add ourselves as a listener to CallList. This
366 // will kick off an update and the whole process can start.
367 mCallList.addListener(this);
368
369 // Create spam call list listener and add it to the list of listeners
Eric Erfanian2ca43182017-08-31 06:57:16 -0700370 mSpamCallListListener = new SpamCallListListener(context, new DefaultDialerExecutorFactory());
Eric Erfanianccca3152017-02-22 16:32:36 -0800371 mCallList.addListener(mSpamCallListListener);
372
373 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800374
Eric Erfanian83b20212017-05-31 08:53:10 -0700375 mFilteredQueryHandler = filteredNumberQueryHandler;
Eric Erfanianccca3152017-02-22 16:32:36 -0800376 mContext
377 .getSystemService(TelephonyManager.class)
378 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
379
Eric Erfanian2ca43182017-08-31 06:57:16 -0700380 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
381 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800382 }
383
384 /**
385 * Called when the telephony service has disconnected from us. This will happen when there are no
386 * more active calls. However, we may still want to continue showing the UI for certain cases like
387 * showing "Call Ended". What we really want is to wait for the activity and the service to both
388 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
389 * secondary method that performs the aforementioned logic.
390 */
391 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700392 LogUtil.d("InCallPresenter.tearDown", "tearDown");
Eric Erfanianccca3152017-02-22 16:32:36 -0800393 mCallList.clearOnDisconnect();
394
395 mServiceConnected = false;
396
397 mContext
398 .getSystemService(TelephonyManager.class)
399 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
400
401 attemptCleanup();
402 VideoPauseController.getInstance().tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -0800403 }
404
405 private void attemptFinishActivity() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700406 mScreenTimeoutEnabled = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800407 final boolean doFinish = (mInCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700408 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800409 if (doFinish) {
410 mInCallActivity.setExcludeFromRecents(true);
411 mInCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800412 }
413 }
414
415 /**
416 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
417 * for more insight on the tear-down process.
418 */
419 public void unsetActivity(InCallActivity inCallActivity) {
420 if (inCallActivity == null) {
421 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
422 }
423 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700424 LogUtil.i(
425 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800426 return;
427 }
428 if (mInCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700429 LogUtil.w(
430 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800431 "Second instance of InCallActivity is trying to unregister when another"
432 + " instance is active. Ignoring.");
433 return;
434 }
435 updateActivity(null);
436 }
437
438 /**
439 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
440 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
441 */
442 private void updateActivity(InCallActivity inCallActivity) {
443 boolean updateListeners = false;
444 boolean doAttemptCleanup = false;
445
446 if (inCallActivity != null) {
447 if (mInCallActivity == null) {
448 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700449 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800450 } else {
451 // since setActivity is called onStart(), it can be called multiple times.
452 // This is fine and ignorable, but we do not want to update the world every time
453 // this happens (like going to/from background) so we do not set updateListeners.
454 }
455
456 mInCallActivity = inCallActivity;
457 mInCallActivity.setExcludeFromRecents(false);
458
459 // By the time the UI finally comes up, the call may already be disconnected.
460 // If that's the case, we may need to show an error dialog.
461 if (mCallList != null && mCallList.getDisconnectedCall() != null) {
462 maybeShowErrorDialogOnDisconnect(mCallList.getDisconnectedCall());
463 }
464
465 // When the UI comes up, we need to first check the in-call state.
466 // If we are showing NO_CALLS, that means that a call probably connected and
467 // then immediately disconnected before the UI was able to come up.
468 // If we dont have any calls, start tearing down the UI instead.
469 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
470 // it has been set.
471 if (mInCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700472 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800473 attemptFinishActivity();
474 return;
475 }
476 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700477 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800478 updateListeners = true;
479 mInCallActivity = null;
480
481 // We attempt cleanup for the destroy case but only after we recalculate the state
482 // to see if we need to come back up or stay shut down. This is why we do the
483 // cleanup after the call to onCallListChange() instead of directly here.
484 doAttemptCleanup = true;
485 }
486
487 // Messages can come from the telephony layer while the activity is coming up
488 // and while the activity is going down. So in both cases we need to recalculate what
489 // state we should be in after they complete.
490 // Examples: (1) A new incoming call could come in and then get disconnected before
491 // the activity is created.
492 // (2) All calls could disconnect and then get a new incoming call before the
493 // activity is destroyed.
494 //
495 // b/1122139 - We previously had a check for mServiceConnected here as well, but there are
496 // cases where we need to recalculate the current state even if the service in not
497 // connected. In particular the case where startOrFinish() is called while the app is
498 // already finish()ing. In that case, we skip updating the state with the knowledge that
499 // we will check again once the activity has finished. That means we have to recalculate the
500 // state here even if the service is disconnected since we may not have finished a state
501 // transition while finish()ing.
502 if (updateListeners) {
503 onCallListChange(mCallList);
504 }
505
506 if (doAttemptCleanup) {
507 attemptCleanup();
508 }
509 }
510
511 public void setManageConferenceActivity(
512 @Nullable ManageConferenceActivity manageConferenceActivity) {
513 mManageConferenceActivity = manageConferenceActivity;
514 }
515
516 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700517 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800518 bringToForeground(showDialpad);
519 }
520
521 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700522 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800523 LatencyReport latencyReport = new LatencyReport(call);
524 if (shouldAttemptBlocking(call)) {
525 maybeBlockCall(call, latencyReport);
526 } else {
527 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
528 mExternalCallList.onCallAdded(call);
529 } else {
530 latencyReport.onCallBlockingDone();
531 mCallList.onCallAdded(mContext, call, latencyReport);
532 }
533 }
534
535 // Since a call has been added we are no longer waiting for Telecom to send us a call.
536 setBoundAndWaitingForOutgoingCall(false, null);
537 call.registerCallback(mCallCallback);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700538 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800539 }
540
541 private boolean shouldAttemptBlocking(android.telecom.Call call) {
542 if (call.getState() != android.telecom.Call.STATE_RINGING) {
543 return false;
544 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700545 if (!UserManagerCompat.isUserUnlocked(mContext)) {
546 LogUtil.i(
547 "InCallPresenter.shouldAttemptBlocking",
548 "not attempting to block incoming call because user is locked");
549 return false;
550 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800551 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700552 LogUtil.i(
553 "InCallPresenter.shouldAttemptBlocking",
554 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800555 return false;
556 }
557 if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700558 LogUtil.i(
559 "InCallPresenter.shouldAttemptBlocking",
560 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800561 return false;
562 }
563 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
564 return false;
565 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700566 if (FilteredNumberCompat.useNewFiltering(mContext)) {
567 LogUtil.i(
568 "InCallPresenter.shouldAttemptBlocking",
569 "not attempting to block incoming call because framework blocking is in use");
570 return false;
571 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800572 return true;
573 }
574
575 /**
576 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
577 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
578 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
579 * call anyways.
580 */
581 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
582 final String countryIso = GeoUtil.getCurrentCountryIso(mContext);
583 final String number = TelecomCallUtil.getNumber(call);
584 final long timeAdded = System.currentTimeMillis();
585
586 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
587 // main UI thread. It is needed so we can change its value within different scopes, since
588 // that cannot be done with a final boolean.
589 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
590
591 final Handler handler = new Handler();
592
593 // Proceed if the query is slow; the call may still be blocked after the query returns.
594 final Runnable runnable =
595 new Runnable() {
596 @Override
597 public void run() {
598 hasTimedOut.set(true);
599 latencyReport.onCallBlockingDone();
600 mCallList.onCallAdded(mContext, call, latencyReport);
601 }
602 };
603 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
604
605 OnCheckBlockedListener onCheckBlockedListener =
606 new OnCheckBlockedListener() {
607 @Override
608 public void onCheckComplete(final Integer id) {
609 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700610 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800611 return;
612 }
613 if (!hasTimedOut.get()) {
614 handler.removeCallbacks(runnable);
615 }
616 if (id == null) {
617 if (!hasTimedOut.get()) {
618 latencyReport.onCallBlockingDone();
619 mCallList.onCallAdded(mContext, call, latencyReport);
620 }
621 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700622 LogUtil.d(
623 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800624 if (!hasTimedOut.get()) {
625 handler.removeCallbacks(runnable);
626
627 latencyReport.onCallBlockingDone();
628 mCallList.onCallAdded(mContext, call, latencyReport);
629 }
630 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700631 LogUtil.i(
632 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800633 call.reject(false, null);
634 Logger.get(mContext).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
635
636 /*
637 * If mContext is null, then the InCallPresenter was torn down before the
638 * block check had a chance to complete. The context is no longer valid, so
639 * don't attempt to remove the call log entry.
640 */
641 if (mContext == null) {
642 return;
643 }
644 // Register observer to update the call log.
645 // BlockedNumberContentObserver will unregister after successful log or timeout.
646 BlockedNumberContentObserver contentObserver =
647 new BlockedNumberContentObserver(mContext, new Handler(), number, timeAdded);
648 contentObserver.register();
649 }
650 }
651 };
652
653 mFilteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
654 }
655
656 public void onCallRemoved(android.telecom.Call call) {
657 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
658 mExternalCallList.onCallRemoved(call);
659 } else {
660 mCallList.onCallRemoved(mContext, call);
661 call.unregisterCallback(mCallCallback);
662 }
663 }
664
665 public void onCanAddCallChanged(boolean canAddCall) {
666 for (CanAddCallListener listener : mCanAddCallListeners) {
667 listener.onCanAddCallChanged(canAddCall);
668 }
669 }
670
671 @Override
672 public void onWiFiToLteHandover(DialerCall call) {
673 if (mInCallActivity != null) {
674 mInCallActivity.onWiFiToLteHandover(call);
675 }
676 }
677
678 @Override
679 public void onHandoverToWifiFailed(DialerCall call) {
680 if (mInCallActivity != null) {
681 mInCallActivity.onHandoverToWifiFailed(call);
682 }
683 }
684
Eric Erfanianc857f902017-05-15 14:05:33 -0700685 @Override
686 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
687 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
688 if (mInCallActivity != null) {
689 mInCallActivity.onInternationalCallOnWifi(call);
690 }
691 }
692
Eric Erfanianccca3152017-02-22 16:32:36 -0800693 /**
694 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
695 * app based on the information it gets from CallList. Dispatches the in-call state to all
696 * listeners. Can trigger the creation or destruction of the UI based on the states that is
697 * calculates.
698 */
699 @Override
700 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700701 Trace.beginSection("InCallPresenter.onCallListChange");
Eric Erfanianccca3152017-02-22 16:32:36 -0800702 if (mInCallActivity != null && mInCallActivity.isInCallScreenAnimating()) {
703 mAwaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700704 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800705 return;
706 }
707 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700708 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800709 return;
710 }
711
712 mAwaitingCallListUpdate = false;
713
714 InCallState newState = getPotentialStateFromCallList(callList);
715 InCallState oldState = mInCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700716 LogUtil.d(
717 "InCallPresenter.onCallListChange",
718 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700719
720 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
721 // incall activity for that call will still exist (even if it's not visible). In the case of
722 // an incoming call in that situation, just disconnect that "waiting for account" call and
723 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
724 // b/33247755 for more details.
725 DialerCall waitingForAccountCall;
726 if (newState == InCallState.INCOMING
727 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
728 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700729 // The InCallActivity might be destroyed or not started yet at this point.
730 if (isActivityStarted()) {
731 mInCallActivity.dismissPendingDialogs();
732 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700733 }
734
Eric Erfanianccca3152017-02-22 16:32:36 -0800735 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700736 LogUtil.d(
737 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800738
739 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700740 LogUtil.i(
741 "InCallPresenter.onCallListChange",
742 "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800743 mInCallState = newState;
744
745 // notify listeners of new state
746 for (InCallStateListener listener : mListeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700747 LogUtil.d(
748 "InCallPresenter.onCallListChange",
749 "Notify " + listener + " of state " + mInCallState.toString());
Eric Erfanianccca3152017-02-22 16:32:36 -0800750 listener.onStateChange(oldState, mInCallState, callList);
751 }
752
753 if (isActivityStarted()) {
754 final boolean hasCall =
755 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
756 mInCallActivity.dismissKeyguard(hasCall);
757 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700758 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800759 }
760
761 /** Called when there is a new incoming call. */
762 @Override
763 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700764 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800765 InCallState newState = startOrFinishUi(InCallState.INCOMING);
766 InCallState oldState = mInCallState;
767
Eric Erfanian2ca43182017-08-31 06:57:16 -0700768 LogUtil.i(
769 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800770 mInCallState = newState;
771
772 for (IncomingCallListener listener : mIncomingCallListeners) {
773 listener.onIncomingCall(oldState, mInCallState, call);
774 }
775
776 if (mInCallActivity != null) {
777 // Re-evaluate which fragment is being shown.
778 mInCallActivity.onPrimaryCallStateChanged();
779 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700780 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800781 }
782
783 @Override
784 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700785 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
Eric Erfanianccca3152017-02-22 16:32:36 -0800786 && mInCallState == InCallPresenter.InCallState.INCOMING) {
787 LogUtil.i(
788 "InCallPresenter.onUpgradeToVideo",
789 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700790 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800791 }
792
793 if (mInCallActivity != null) {
794 // Re-evaluate which fragment is being shown.
795 mInCallActivity.onPrimaryCallStateChanged();
796 }
797 }
798
799 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700800 public void onSessionModificationStateChange(DialerCall call) {
801 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800802 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
803 if (mProximitySensor == null) {
804 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
805 return;
806 }
807 mProximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700808 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
Eric Erfanianccca3152017-02-22 16:32:36 -0800809 if (mInCallActivity != null) {
810 // Re-evaluate which fragment is being shown.
811 mInCallActivity.onPrimaryCallStateChanged();
812 }
813 }
814
815 /**
816 * Called when a call becomes disconnected. Called everytime an existing call changes from being
817 * connected (incoming/outgoing/active) to disconnected.
818 */
819 @Override
820 public void onDisconnect(DialerCall call) {
821 maybeShowErrorDialogOnDisconnect(call);
822
823 // We need to do the run the same code as onCallListChange.
824 onCallListChange(mCallList);
825
826 if (isActivityStarted()) {
827 mInCallActivity.dismissKeyguard(false);
828 }
829
830 if (call.isEmergencyCall()) {
831 FilteredNumbersUtil.recordLastEmergencyCallTime(mContext);
832 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800833
Eric Erfanianfc37b022017-03-21 10:11:17 -0700834 if (!mCallList.hasLiveCall()
835 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700836 && !isSecretCode(call.getNumber())
Eric Erfanianfc37b022017-03-21 10:11:17 -0700837 && !CallerInfoUtils.isVoiceMailNumber(mContext, call)) {
Eric Erfanian10b34a52017-05-04 08:23:17 -0700838 PostCall.onCallDisconnected(mContext, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -0800839 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800840 }
841
Eric Erfanian10b34a52017-05-04 08:23:17 -0700842 private boolean isSecretCode(@Nullable String number) {
843 return number != null
844 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
845 }
846
Eric Erfanianccca3152017-02-22 16:32:36 -0800847 /** Given the call list, return the state in which the in-call screen should be. */
848 public InCallState getPotentialStateFromCallList(CallList callList) {
849
850 InCallState newState = InCallState.NO_CALLS;
851
852 if (callList == null) {
853 return newState;
854 }
855 if (callList.getIncomingCall() != null) {
856 newState = InCallState.INCOMING;
857 } else if (callList.getWaitingForAccountCall() != null) {
858 newState = InCallState.WAITING_FOR_ACCOUNT;
859 } else if (callList.getPendingOutgoingCall() != null) {
860 newState = InCallState.PENDING_OUTGOING;
861 } else if (callList.getOutgoingCall() != null) {
862 newState = InCallState.OUTGOING;
863 } else if (callList.getActiveCall() != null
864 || callList.getBackgroundCall() != null
865 || callList.getDisconnectedCall() != null
866 || callList.getDisconnectingCall() != null) {
867 newState = InCallState.INCALL;
868 }
869
870 if (newState == InCallState.NO_CALLS) {
871 if (mBoundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700872 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800873 }
874 }
875
876 return newState;
877 }
878
879 public boolean isBoundAndWaitingForOutgoingCall() {
880 return mBoundAndWaitingForOutgoingCall;
881 }
882
883 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700884 LogUtil.i(
885 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
886 "setBoundAndWaitingForOutgoingCall: " + isBound);
Eric Erfanianccca3152017-02-22 16:32:36 -0800887 mBoundAndWaitingForOutgoingCall = isBound;
888 mThemeColorManager.setPendingPhoneAccountHandle(handle);
889 if (isBound && mInCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700890 mInCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800891 }
892 }
893
894 public void onShrinkAnimationComplete() {
895 if (mAwaitingCallListUpdate) {
896 onCallListChange(mCallList);
897 }
898 }
899
900 public void addIncomingCallListener(IncomingCallListener listener) {
901 Objects.requireNonNull(listener);
902 mIncomingCallListeners.add(listener);
903 }
904
905 public void removeIncomingCallListener(IncomingCallListener listener) {
906 if (listener != null) {
907 mIncomingCallListeners.remove(listener);
908 }
909 }
910
911 public void addListener(InCallStateListener listener) {
912 Objects.requireNonNull(listener);
913 mListeners.add(listener);
914 }
915
916 public void removeListener(InCallStateListener listener) {
917 if (listener != null) {
918 mListeners.remove(listener);
919 }
920 }
921
922 public void addDetailsListener(InCallDetailsListener listener) {
923 Objects.requireNonNull(listener);
924 mDetailsListeners.add(listener);
925 }
926
927 public void removeDetailsListener(InCallDetailsListener listener) {
928 if (listener != null) {
929 mDetailsListeners.remove(listener);
930 }
931 }
932
933 public void addCanAddCallListener(CanAddCallListener listener) {
934 Objects.requireNonNull(listener);
935 mCanAddCallListeners.add(listener);
936 }
937
938 public void removeCanAddCallListener(CanAddCallListener listener) {
939 if (listener != null) {
940 mCanAddCallListeners.remove(listener);
941 }
942 }
943
944 public void addOrientationListener(InCallOrientationListener listener) {
945 Objects.requireNonNull(listener);
946 mOrientationListeners.add(listener);
947 }
948
949 public void removeOrientationListener(InCallOrientationListener listener) {
950 if (listener != null) {
951 mOrientationListeners.remove(listener);
952 }
953 }
954
955 public void addInCallEventListener(InCallEventListener listener) {
956 Objects.requireNonNull(listener);
957 mInCallEventListeners.add(listener);
958 }
959
960 public void removeInCallEventListener(InCallEventListener listener) {
961 if (listener != null) {
962 mInCallEventListeners.remove(listener);
963 }
964 }
965
966 public ProximitySensor getProximitySensor() {
967 return mProximitySensor;
968 }
969
970 public PseudoScreenState getPseudoScreenState() {
971 return mPseudoScreenState;
972 }
973
974 /** Returns true if the incall app is the foreground application. */
975 public boolean isShowingInCallUi() {
976 if (!isActivityStarted()) {
977 return false;
978 }
979 if (mManageConferenceActivity != null && mManageConferenceActivity.isVisible()) {
980 return true;
981 }
982 return mInCallActivity.isVisible();
983 }
984
985 /**
986 * Returns true if the activity has been created and is running. Returns true as long as activity
987 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
988 * (not in foreground).
989 */
990 public boolean isActivityStarted() {
991 return (mInCallActivity != null
992 && !mInCallActivity.isDestroyed()
993 && !mInCallActivity.isFinishing());
994 }
995
996 /**
997 * Determines if the In-Call app is currently changing configuration.
998 *
999 * @return {@code true} if the In-Call app is changing configuration.
1000 */
1001 public boolean isChangingConfigurations() {
1002 return mIsChangingConfigurations;
1003 }
1004
1005 /**
1006 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1007 * screen orientation).
1008 */
1009 /*package*/
1010 void updateIsChangingConfigurations() {
1011 mIsChangingConfigurations = false;
1012 if (mInCallActivity != null) {
1013 mIsChangingConfigurations = mInCallActivity.isChangingConfigurations();
1014 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001015 LogUtil.v(
1016 "InCallPresenter.updateIsChangingConfigurations",
1017 "updateIsChangingConfigurations = " + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001018 }
1019
1020 /** Called when the activity goes in/out of the foreground. */
1021 public void onUiShowing(boolean showing) {
1022 // We need to update the notification bar when we leave the UI because that
1023 // could trigger it to show again.
1024 if (mStatusBarNotifier != null) {
1025 mStatusBarNotifier.updateNotification(mCallList);
1026 }
1027
1028 if (mProximitySensor != null) {
1029 mProximitySensor.onInCallShowing(showing);
1030 }
1031
1032 Intent broadcastIntent = Bindings.get(mContext).getUiReadyBroadcastIntent(mContext);
1033 if (broadcastIntent != null) {
1034 broadcastIntent.putExtra(EXTRA_FIRST_TIME_SHOWN, !mIsActivityPreviouslyStarted);
1035
1036 if (showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001037 LogUtil.d("InCallPresenter.onUiShowing", "Sending sticky broadcast: ", broadcastIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001038 mContext.sendStickyBroadcast(broadcastIntent);
1039 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001040 LogUtil.d("InCallPresenter.onUiShowing", "Removing sticky broadcast: ", broadcastIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001041 mContext.removeStickyBroadcast(broadcastIntent);
1042 }
1043 }
1044
1045 if (showing) {
1046 mIsActivityPreviouslyStarted = true;
1047 } else {
1048 updateIsChangingConfigurations();
1049 }
1050
1051 for (InCallUiListener listener : mInCallUiListeners) {
1052 listener.onUiShowing(showing);
1053 }
1054
1055 if (mInCallActivity != null) {
1056 // Re-evaluate which fragment is being shown.
1057 mInCallActivity.onPrimaryCallStateChanged();
1058 }
1059 }
1060
Eric Erfanian2ca43182017-08-31 06:57:16 -07001061 public void refreshUi() {
1062 if (mInCallActivity != null) {
1063 // Re-evaluate which fragment is being shown.
1064 mInCallActivity.onPrimaryCallStateChanged();
1065 }
1066 }
1067
Eric Erfanianccca3152017-02-22 16:32:36 -08001068 public void addInCallUiListener(InCallUiListener listener) {
1069 mInCallUiListeners.add(listener);
1070 }
1071
1072 public boolean removeInCallUiListener(InCallUiListener listener) {
1073 return mInCallUiListeners.remove(listener);
1074 }
1075
1076 /*package*/
1077 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001078 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001079 notifyVideoPauseController(true);
Eric Erfaniand8046e52017-04-06 09:41:50 -07001080 if (mStatusBarNotifier != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001081 // TODO(maxwelb) - b/36649622: Investigate this redundant call
Eric Erfaniand8046e52017-04-06 09:41:50 -07001082 mStatusBarNotifier.updateNotification(mCallList);
1083 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001084 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001085 }
1086
1087 /*package*/
1088 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001089 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001090 notifyVideoPauseController(false);
1091 }
1092
1093 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001094 LogUtil.d(
1095 "InCallPresenter.notifyVideoPauseController",
1096 "mIsChangingConfigurations=" + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001097 if (!mIsChangingConfigurations) {
1098 VideoPauseController.getInstance().onUiShowing(showing);
1099 }
1100 }
1101
1102 /** Brings the app into the foreground if possible. */
1103 public void bringToForeground(boolean showDialpad) {
1104 // Before we bring the incall UI to the foreground, we check to see if:
1105 // 1. It is not currently in the foreground
1106 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1107 // be displayed)
1108 // If the activity hadn't actually been started previously, yet there are still calls
1109 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1110 // bring it up the UI regardless.
1111 if (!isShowingInCallUi() && mInCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001112 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001113 }
1114 }
1115
1116 public void onPostDialCharWait(String callId, String chars) {
1117 if (isActivityStarted()) {
1118 mInCallActivity.showPostCharWaitDialog(callId, chars);
1119 }
1120 }
1121
1122 /**
1123 * Handles the green CALL key while in-call.
1124 *
1125 * @return true if we consumed the event.
1126 */
1127 public boolean handleCallKey() {
1128 LogUtil.v("InCallPresenter.handleCallKey", null);
1129
1130 // The green CALL button means either "Answer", "Unhold", or
1131 // "Swap calls", or can be a no-op, depending on the current state
1132 // of the Phone.
1133
1134 /** INCOMING CALL */
1135 final CallList calls = mCallList;
1136 final DialerCall incomingCall = calls.getIncomingCall();
1137 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1138
1139 // (1) Attempt to answer a call
1140 if (incomingCall != null) {
1141 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1142 return true;
1143 }
1144
1145 /** STATE_ACTIVE CALL */
1146 final DialerCall activeCall = calls.getActiveCall();
1147 if (activeCall != null) {
1148 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1149 // consolidate this logic.
1150 final boolean canMerge =
1151 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1152 final boolean canSwap =
1153 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1154
Eric Erfanian2ca43182017-08-31 06:57:16 -07001155 LogUtil.v(
1156 "InCallPresenter.handleCallKey",
1157 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001158
1159 // (2) Attempt actions on conference calls
1160 if (canMerge) {
1161 TelecomAdapter.getInstance().merge(activeCall.getId());
1162 return true;
1163 } else if (canSwap) {
1164 TelecomAdapter.getInstance().swap(activeCall.getId());
1165 return true;
1166 }
1167 }
1168
1169 /** BACKGROUND CALL */
1170 final DialerCall heldCall = calls.getBackgroundCall();
1171 if (heldCall != null) {
1172 // We have a hold call so presumeable it will always support HOLD...but
1173 // there is no harm in double checking.
1174 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1175
Eric Erfanian2ca43182017-08-31 06:57:16 -07001176 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001177
1178 // (4) unhold call
1179 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1180 heldCall.unhold();
1181 return true;
1182 }
1183 }
1184
1185 // Always consume hard keys
1186 return true;
1187 }
1188
1189 /**
1190 * A dialog could have prevented in-call screen from being previously finished. This function
1191 * checks to see if there should be any UI left and if not attempts to tear down the UI.
1192 */
1193 public void onDismissDialog() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001194 LogUtil.i("InCallPresenter.onDismissDialog", "Dialog dismissed");
Eric Erfanianccca3152017-02-22 16:32:36 -08001195 if (mInCallState == InCallState.NO_CALLS) {
1196 attemptFinishActivity();
1197 attemptCleanup();
1198 }
1199 }
1200
1201 /** 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) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001281 LogUtil.d(
1282 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + mInCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001283
1284 // TODO: Consider a proper state machine implementation
1285
1286 // If the state isn't changing we have already done any starting/stopping of activities in
1287 // a previous pass...so lets cut out early
1288 if (newState == mInCallState) {
1289 return newState;
1290 }
1291
1292 // A new Incoming call means that the user needs to be notified of the the call (since
1293 // it wasn't them who initiated it). We do this through full screen notifications and
1294 // happens indirectly through {@link StatusBarNotifier}.
1295 //
1296 // The process for incoming calls is as follows:
1297 //
1298 // 1) CallList - Announces existence of new INCOMING call
1299 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1300 // - should be set to INCOMING.
1301 // 3) InCallPresenter - This method is called to see if we need to start or finish
1302 // the app given the new state.
1303 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1304 // StatusBarNotifier explicitly to issue a FullScreen Notification
1305 // that will either start the InCallActivity or show the user a
1306 // top-level notification dialog if the user is in an immersive app.
1307 // That notification can also start the InCallActivity.
1308 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1309 // call InCallPresenter::setActivity() to let the presenter
1310 // know that start-up is complete.
1311 //
1312 // [ AND NOW YOU'RE IN THE CALL. voila! ]
1313 //
1314 // Our app is started using a fullScreen notification. We need to do this whenever
1315 // we get an incoming call. Depending on the current context of the device, either a
1316 // incoming call HUN or the actual InCallActivity will be shown.
1317 final boolean startIncomingCallSequence = (InCallState.INCOMING == newState);
1318
1319 // A dialog to show on top of the InCallUI to select a PhoneAccount
1320 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1321
1322 // A new outgoing call indicates that the user just now dialed a number and when that
1323 // happens we need to display the screen immediately or show an account picker dialog if
1324 // no default is set. However, if the main InCallUI is already visible, we do not want to
1325 // re-initiate the start-up animation, so we do not need to do anything here.
1326 //
1327 // It is also possible to go into an intermediate state where the call has been initiated
1328 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1329 // This pending outgoing state can also launch the call screen.
1330 //
1331 // This is different from the incoming call sequence because we do not need to shock the
1332 // user with a top-level notification. Just show the call UI normally.
1333 boolean callCardFragmentVisible =
1334 mInCallActivity != null && mInCallActivity.getCallCardFragmentVisible();
1335 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1336 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1337
1338 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1339 // outgoing call process, so the UI should be brought up to show an error dialog.
1340 showCallUi |=
1341 (InCallState.PENDING_OUTGOING == mInCallState
1342 && InCallState.INCALL == newState
1343 && !isShowingInCallUi());
1344
1345 // Another exception - InCallActivity is in charge of disconnecting a call with no
1346 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1347 // call so that:
1348 // 1) The call can be disconnected correctly
1349 // 2) The UI comes up and correctly displays the error dialog.
1350 // TODO: Remove these special case conditions by making InCallPresenter a true state
1351 // machine. Telecom should also be the component responsible for disconnecting a call
1352 // with no valid accounts.
1353 showCallUi |=
1354 InCallState.PENDING_OUTGOING == newState
1355 && mainUiNotVisible
1356 && isCallWithNoValidAccounts(mCallList.getPendingOutgoingCall());
1357
1358 // The only time that we have an instance of mInCallActivity and it isn't started is
1359 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1360 // the activity. When it is finally destroyed, we double check if we should bring it back
1361 // up so we aren't going to lose anything by avoiding a second startup here.
1362 boolean activityIsFinishing = mInCallActivity != null && !isActivityStarted();
1363 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001364 LogUtil.i(
1365 "InCallPresenter.startOrFinishUi",
1366 "Undo the state change: " + newState + " -> " + mInCallState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001367 return mInCallState;
1368 }
1369
1370 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1371 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1372 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1373 // outgoing call after user ignore it by pressing home button.
1374 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1375 && !showCallUi
1376 && isActivityStarted()) {
1377 mInCallActivity.dismissPendingDialogs();
1378 }
1379
1380 if (showCallUi || showAccountPicker) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001381 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001382 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001383 } else if (startIncomingCallSequence) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001384 LogUtil.i("InCallPresenter.startOrFinishUi", "Start Full Screen in call UI");
Eric Erfanianccca3152017-02-22 16:32:36 -08001385
Eric Erfaniand8046e52017-04-06 09:41:50 -07001386 mStatusBarNotifier.updateNotification(mCallList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001387 } else if (newState == InCallState.NO_CALLS) {
1388 // The new state is the no calls state. Tear everything down.
1389 attemptFinishActivity();
1390 attemptCleanup();
1391 }
1392
1393 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();
1486
Eric Erfanian2ca43182017-08-31 06:57:16 -07001487 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001488 }
1489 }
1490
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001491 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001492 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -08001493 mContext.startActivity(
1494 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001495 mContext, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001496 }
1497
1498 public void onServiceBind() {
1499 mServiceBound = true;
1500 }
1501
1502 public void onServiceUnbind() {
1503 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
1504 mServiceBound = false;
1505 }
1506
1507 public boolean isServiceBound() {
1508 return mServiceBound;
1509 }
1510
1511 public void maybeStartRevealAnimation(Intent intent) {
1512 if (intent == null || mInCallActivity != null) {
1513 return;
1514 }
1515 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1516 if (extras == null) {
1517 // Incoming call, just show the in-call UI directly.
1518 return;
1519 }
1520
1521 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1522 // Account selection dialog will show up so don't show the animation.
1523 return;
1524 }
1525
1526 final PhoneAccountHandle accountHandle =
1527 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1528 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001529
1530 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(true, accountHandle);
1531
1532 final Intent activityIntent =
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001533 InCallActivity.getIntent(mContext, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001534 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
1535 mContext.startActivity(activityIntent);
1536 }
1537
1538 /**
1539 * Retrieves the current in-call camera manager instance, creating if necessary.
1540 *
1541 * @return The {@link InCallCameraManager}.
1542 */
1543 public InCallCameraManager getInCallCameraManager() {
1544 synchronized (this) {
1545 if (mInCallCameraManager == null) {
1546 mInCallCameraManager = new InCallCameraManager(mContext);
1547 }
1548
1549 return mInCallCameraManager;
1550 }
1551 }
1552
1553 /**
1554 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1555 *
1556 * @param orientation The screen orientation of the device (one of: {@link
1557 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1558 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1559 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1560 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1561 */
1562 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001563 LogUtil.d(
1564 "InCallPresenter.onDeviceOrientationChange",
1565 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001566
1567 if (mCallList != null) {
1568 mCallList.notifyCallsOfDeviceRotation(orientation);
1569 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001570 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001571 }
1572
1573 // Notify listeners of device orientation changed.
1574 for (InCallOrientationListener listener : mOrientationListeners) {
1575 listener.onDeviceOrientationChanged(orientation);
1576 }
1577 }
1578
1579 /**
1580 * Configures the in-call UI activity so it can change orientations or not. Enables the
1581 * orientation event listener if allowOrientationChange is true, disables it if false.
1582 *
1583 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1584 * landscape. {@code false} if the in-call UI should be locked in portrait.
1585 */
1586 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
1587 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001588 LogUtil.e(
1589 "InCallPresenter.setInCallAllowsOrientationChange",
1590 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001591 return;
1592 }
1593 mInCallActivity.setAllowOrientationChange(allowOrientationChange);
1594 }
1595
1596 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001597 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
1598 mScreenTimeoutEnabled = enable;
1599 applyScreenTimeout();
1600 }
1601
1602 private void applyScreenTimeout() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001603 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001604 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001605 return;
1606 }
1607
1608 final Window window = mInCallActivity.getWindow();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001609 if (mScreenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001610 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1611 } else {
1612 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1613 }
1614 }
1615
1616 /**
1617 * Hides or shows the conference manager fragment.
1618 *
1619 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1620 * be hidden.
1621 */
1622 public void showConferenceCallManager(boolean show) {
1623 if (mInCallActivity != null) {
1624 mInCallActivity.showConferenceFragment(show);
1625 }
1626 if (!show && mManageConferenceActivity != null) {
1627 mManageConferenceActivity.finish();
1628 }
1629 }
1630
1631 /**
1632 * Determines if the dialpad is visible.
1633 *
1634 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1635 */
1636 public boolean isDialpadVisible() {
1637 if (mInCallActivity == null) {
1638 return false;
1639 }
1640 return mInCallActivity.isDialpadVisible();
1641 }
1642
1643 public ThemeColorManager getThemeColorManager() {
1644 return mThemeColorManager;
1645 }
1646
1647 /** Called when the foreground call changes. */
1648 public void onForegroundCallChanged(DialerCall newForegroundCall) {
1649 mThemeColorManager.onForegroundCallChanged(mContext, newForegroundCall);
1650 if (mInCallActivity != null) {
1651 mInCallActivity.onForegroundCallChanged(newForegroundCall);
1652 }
1653 }
1654
1655 public InCallActivity getActivity() {
1656 return mInCallActivity;
1657 }
1658
1659 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1660 public void setActivity(InCallActivity inCallActivity) {
1661 if (inCallActivity == null) {
1662 throw new IllegalArgumentException("registerActivity cannot be called with null");
1663 }
1664 if (mInCallActivity != null && mInCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001665 LogUtil.w(
1666 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001667 }
1668 updateActivity(inCallActivity);
1669 }
1670
1671 ExternalCallNotifier getExternalCallNotifier() {
1672 return mExternalCallNotifier;
1673 }
1674
1675 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
1676 if (mLocalVideoSurfaceTexture == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001677 mLocalVideoSurfaceTexture =
1678 VideoSurfaceBindings.createLocalVideoSurfaceTexture(
1679 mContext.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE));
Eric Erfanianccca3152017-02-22 16:32:36 -08001680 }
1681 return mLocalVideoSurfaceTexture;
1682 }
1683
1684 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
1685 if (mRemoteVideoSurfaceTexture == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001686 mRemoteVideoSurfaceTexture =
1687 VideoSurfaceBindings.createRemoteVideoSurfaceTexture(
1688 mContext.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE));
Eric Erfanianccca3152017-02-22 16:32:36 -08001689 }
1690 return mRemoteVideoSurfaceTexture;
1691 }
1692
1693 void cleanupSurfaces() {
1694 if (mRemoteVideoSurfaceTexture != null) {
1695 mRemoteVideoSurfaceTexture.setDoneWithSurface();
1696 mRemoteVideoSurfaceTexture = null;
1697 }
1698 if (mLocalVideoSurfaceTexture != null) {
1699 mLocalVideoSurfaceTexture.setDoneWithSurface();
1700 mLocalVideoSurfaceTexture = null;
1701 }
1702 }
1703
1704 /** All the main states of InCallActivity. */
1705 public enum InCallState {
1706 // InCall Screen is off and there are no calls
1707 NO_CALLS,
1708
1709 // Incoming-call screen is up
1710 INCOMING,
1711
1712 // In-call experience is showing
1713 INCALL,
1714
1715 // Waiting for user input before placing outgoing call
1716 WAITING_FOR_ACCOUNT,
1717
1718 // UI is starting up but no call has been initiated yet.
1719 // The UI is waiting for Telecom to respond.
1720 PENDING_OUTGOING,
1721
1722 // User is dialing out
1723 OUTGOING;
1724
1725 public boolean isIncoming() {
1726 return (this == INCOMING);
1727 }
1728
1729 public boolean isConnectingOrConnected() {
1730 return (this == INCOMING || this == OUTGOING || this == INCALL);
1731 }
1732 }
1733
1734 /** Interface implemented by classes that need to know about the InCall State. */
1735 public interface InCallStateListener {
1736
1737 // TODO: Enhance state to contain the call objects instead of passing CallList
1738 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1739 }
1740
1741 public interface IncomingCallListener {
1742
1743 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1744 }
1745
1746 public interface CanAddCallListener {
1747
1748 void onCanAddCallChanged(boolean canAddCall);
1749 }
1750
1751 public interface InCallDetailsListener {
1752
1753 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1754 }
1755
1756 public interface InCallOrientationListener {
1757
1758 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1759 }
1760
1761 /**
1762 * Interface implemented by classes that need to know about events which occur within the In-Call
1763 * UI. Used as a means of communicating between fragments that make up the UI.
1764 */
1765 public interface InCallEventListener {
1766
1767 void onFullscreenModeChanged(boolean isFullscreenMode);
1768 }
1769
1770 public interface InCallUiListener {
1771
1772 void onUiShowing(boolean showing);
1773 }
1774}