blob: 1528e7dcf0f6215fad65f22f9c318caa4d810117 [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;
24import android.support.annotation.NonNull;
25import android.support.annotation.Nullable;
26import android.support.annotation.VisibleForTesting;
Eric Erfanian83b20212017-05-31 08:53:10 -070027import android.support.v4.os.UserManagerCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080028import android.telecom.Call.Details;
29import android.telecom.DisconnectCause;
30import android.telecom.PhoneAccount;
31import android.telecom.PhoneAccountHandle;
32import android.telecom.TelecomManager;
33import android.telecom.VideoProfile;
34import android.telephony.PhoneStateListener;
35import android.telephony.TelephonyManager;
36import android.view.Window;
37import android.view.WindowManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080038import com.android.contacts.common.compat.CallCompat;
39import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler;
40import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler.OnCheckBlockedListener;
Eric Erfanian83b20212017-05-31 08:53:10 -070041import com.android.dialer.blocking.FilteredNumberCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080042import com.android.dialer.blocking.FilteredNumbersUtil;
43import com.android.dialer.common.LogUtil;
Eric Erfaniand8046e52017-04-06 09:41:50 -070044import com.android.dialer.enrichedcall.EnrichedCallComponent;
Eric Erfanian10b34a52017-05-04 08:23:17 -070045import com.android.dialer.location.GeoUtil;
Eric Erfanian8369df02017-05-03 10:27:13 -070046import com.android.dialer.logging.InteractionEvent;
Eric Erfanianccca3152017-02-22 16:32:36 -080047import com.android.dialer.logging.Logger;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070048import com.android.dialer.postcall.PostCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080049import com.android.dialer.telecom.TelecomUtil;
50import com.android.dialer.util.TouchPointManager;
51import com.android.incallui.InCallOrientationEventListener.ScreenOrientation;
52import com.android.incallui.answerproximitysensor.PseudoScreenState;
53import com.android.incallui.call.CallList;
54import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080055import com.android.incallui.call.ExternalCallList;
Eric Erfanianccca3152017-02-22 16:32:36 -080056import com.android.incallui.call.TelecomAdapter;
Eric Erfaniand538e0b2017-06-30 15:26:17 -070057import com.android.incallui.disconnectdialog.DisconnectMessage;
Eric Erfanianccca3152017-02-22 16:32:36 -080058import com.android.incallui.latencyreport.LatencyReport;
59import com.android.incallui.legacyblocking.BlockedNumberContentObserver;
60import com.android.incallui.spam.SpamCallListListener;
61import com.android.incallui.util.TelecomCallUtil;
62import com.android.incallui.videosurface.bindings.VideoSurfaceBindings;
63import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
Eric Erfanian90508232017-03-24 09:31:16 -070064import com.android.incallui.videotech.utils.VideoUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080065import java.util.Collections;
66import java.util.List;
67import java.util.Objects;
68import java.util.Set;
69import java.util.concurrent.ConcurrentHashMap;
70import java.util.concurrent.CopyOnWriteArrayList;
71import java.util.concurrent.atomic.AtomicBoolean;
72
73/**
74 * Takes updates from the CallList and notifies the InCallActivity (UI) of the changes. Responsible
75 * for starting the activity for a new call and finishing the activity when all calls are
76 * disconnected. Creates and manages the in-call state and provides a listener pattern for the
77 * presenters that want to listen in on the in-call state changes. TODO: This class has become more
78 * of a state machine at this point. Consider renaming.
79 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -070080public class InCallPresenter implements CallList.Listener {
Eric Erfanianccca3152017-02-22 16:32:36 -080081
82 private static final String EXTRA_FIRST_TIME_SHOWN =
83 "com.android.incallui.intent.extra.FIRST_TIME_SHOWN";
84
85 private static final long BLOCK_QUERY_TIMEOUT_MS = 1000;
86
87 private static final Bundle EMPTY_EXTRAS = new Bundle();
88
89 private static InCallPresenter sInCallPresenter;
90
91 /**
92 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is load factor before
93 * resizing, 1 means we only expect a single thread to access the map so make only a single shard
94 */
95 private final Set<InCallStateListener> mListeners =
96 Collections.newSetFromMap(new ConcurrentHashMap<InCallStateListener, Boolean>(8, 0.9f, 1));
97
98 private final List<IncomingCallListener> mIncomingCallListeners = new CopyOnWriteArrayList<>();
99 private final Set<InCallDetailsListener> mDetailsListeners =
100 Collections.newSetFromMap(new ConcurrentHashMap<InCallDetailsListener, Boolean>(8, 0.9f, 1));
101 private final Set<CanAddCallListener> mCanAddCallListeners =
102 Collections.newSetFromMap(new ConcurrentHashMap<CanAddCallListener, Boolean>(8, 0.9f, 1));
103 private final Set<InCallUiListener> mInCallUiListeners =
104 Collections.newSetFromMap(new ConcurrentHashMap<InCallUiListener, Boolean>(8, 0.9f, 1));
105 private final Set<InCallOrientationListener> mOrientationListeners =
106 Collections.newSetFromMap(
107 new ConcurrentHashMap<InCallOrientationListener, Boolean>(8, 0.9f, 1));
108 private final Set<InCallEventListener> mInCallEventListeners =
109 Collections.newSetFromMap(new ConcurrentHashMap<InCallEventListener, Boolean>(8, 0.9f, 1));
110
111 private StatusBarNotifier mStatusBarNotifier;
112 private ExternalCallNotifier mExternalCallNotifier;
113 private ContactInfoCache mContactInfoCache;
114 private Context mContext;
115 private final OnCheckBlockedListener mOnCheckBlockedListener =
116 new OnCheckBlockedListener() {
117 @Override
118 public void onCheckComplete(final Integer id) {
119 if (id != null && id != FilteredNumberAsyncQueryHandler.INVALID_ID) {
120 // Silence the ringer now to prevent ringing and vibration before the call is
121 // terminated when Telecom attempts to add it.
122 TelecomUtil.silenceRinger(mContext);
123 }
124 }
125 };
126 private CallList mCallList;
127 private ExternalCallList mExternalCallList;
128 private InCallActivity mInCallActivity;
129 private ManageConferenceActivity mManageConferenceActivity;
130 private final android.telecom.Call.Callback mCallCallback =
131 new android.telecom.Call.Callback() {
132 @Override
133 public void onPostDialWait(
134 android.telecom.Call telecomCall, String remainingPostDialSequence) {
135 final DialerCall call = mCallList.getDialerCallFromTelecomCall(telecomCall);
136 if (call == null) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700137 LogUtil.w(
138 "InCallPresenter.onPostDialWait",
139 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800140 return;
141 }
142 onPostDialCharWait(call.getId(), remainingPostDialSequence);
143 }
144
145 @Override
146 public void onDetailsChanged(
147 android.telecom.Call telecomCall, android.telecom.Call.Details details) {
148 final DialerCall call = mCallList.getDialerCallFromTelecomCall(telecomCall);
149 if (call == null) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700150 LogUtil.w(
151 "InCallPresenter.onDetailsChanged",
152 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800153 return;
154 }
155
156 if (details.hasProperty(Details.PROPERTY_IS_EXTERNAL_CALL)
157 && !mExternalCallList.isCallTracked(telecomCall)) {
158
159 // A regular call became an external call so swap call lists.
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700160 LogUtil.i("InCallPresenter.onDetailsChanged", "Call became external: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800161 mCallList.onInternalCallMadeExternal(mContext, telecomCall);
162 mExternalCallList.onCallAdded(telecomCall);
163 return;
164 }
165
166 for (InCallDetailsListener listener : mDetailsListeners) {
167 listener.onDetailsChanged(call, details);
168 }
169 }
170
171 @Override
172 public void onConferenceableCallsChanged(
173 android.telecom.Call telecomCall, List<android.telecom.Call> conferenceableCalls) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700174 LogUtil.i(
175 "InCallPresenter.onConferenceableCallsChanged",
176 "onConferenceableCallsChanged: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800177 onDetailsChanged(telecomCall, telecomCall.getDetails());
178 }
179 };
180 private InCallState mInCallState = InCallState.NO_CALLS;
181 private ProximitySensor mProximitySensor;
182 private final PseudoScreenState mPseudoScreenState = new PseudoScreenState();
183 private boolean mServiceConnected;
Eric Erfanianccca3152017-02-22 16:32:36 -0800184 private InCallCameraManager mInCallCameraManager;
185 private FilteredNumberAsyncQueryHandler mFilteredQueryHandler;
186 private CallList.Listener mSpamCallListListener;
187 /** Whether or not we are currently bound and waiting for Telecom to send us a new call. */
188 private boolean mBoundAndWaitingForOutgoingCall;
189 /** Determines if the InCall UI is in fullscreen mode or not. */
190 private boolean mIsFullScreen = false;
191
192 private PhoneStateListener mPhoneStateListener =
193 new PhoneStateListener() {
194 @Override
195 public void onCallStateChanged(int state, String incomingNumber) {
196 if (state == TelephonyManager.CALL_STATE_RINGING) {
197 if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
198 return;
199 }
200 // Check if the number is blocked, to silence the ringer.
201 String countryIso = GeoUtil.getCurrentCountryIso(mContext);
202 mFilteredQueryHandler.isBlockedNumber(
203 mOnCheckBlockedListener, incomingNumber, countryIso);
204 }
205 }
206 };
207 /**
208 * Is true when the activity has been previously started. Some code needs to know not just if the
209 * activity is currently up, but if it had been previously shown in foreground for this in-call
210 * session (e.g., StatusBarNotifier). This gets reset when the session ends in the tear-down
211 * method.
212 */
213 private boolean mIsActivityPreviouslyStarted = false;
214
215 /** Whether or not InCallService is bound to Telecom. */
216 private boolean mServiceBound = false;
217
218 /**
219 * When configuration changes Android kills the current activity and starts a new one. The flag is
220 * used to check if full clean up is necessary (activity is stopped and new activity won't be
221 * started), or if a new activity will be started right after the current one is destroyed, and
222 * therefore no need in release all resources.
223 */
224 private boolean mIsChangingConfigurations = false;
225
226 private boolean mAwaitingCallListUpdate = false;
227
228 private ExternalCallList.ExternalCallListener mExternalCallListener =
229 new ExternalCallList.ExternalCallListener() {
230
231 @Override
232 public void onExternalCallPulled(android.telecom.Call call) {
233 // Note: keep this code in sync with InCallPresenter#onCallAdded
234 LatencyReport latencyReport = new LatencyReport(call);
235 latencyReport.onCallBlockingDone();
236 // Note: External calls do not require spam checking.
237 mCallList.onCallAdded(mContext, call, latencyReport);
238 call.registerCallback(mCallCallback);
239 }
240
241 @Override
242 public void onExternalCallAdded(android.telecom.Call call) {
243 // No-op
244 }
245
246 @Override
247 public void onExternalCallRemoved(android.telecom.Call call) {
248 // No-op
249 }
250
251 @Override
252 public void onExternalCallUpdated(android.telecom.Call call) {
253 // No-op
254 }
255 };
256
257 private ThemeColorManager mThemeColorManager;
258 private VideoSurfaceTexture mLocalVideoSurfaceTexture;
259 private VideoSurfaceTexture mRemoteVideoSurfaceTexture;
260
Eric Erfanian10b34a52017-05-04 08:23:17 -0700261 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800262 @VisibleForTesting
263 InCallPresenter() {}
264
265 public static synchronized InCallPresenter getInstance() {
266 if (sInCallPresenter == null) {
267 sInCallPresenter = new InCallPresenter();
268 }
269 return sInCallPresenter;
270 }
271
Eric Erfanian10b34a52017-05-04 08:23:17 -0700272 @VisibleForTesting
273 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
274 sInCallPresenter = inCallPresenter;
275 }
276
Eric Erfanianccca3152017-02-22 16:32:36 -0800277 /**
278 * Determines whether or not a call has no valid phone accounts that can be used to make the call
279 * with. Emergency calls do not require a phone account.
280 *
281 * @param call to check accounts for.
282 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
283 * call contains a phone account that could be used to initiate it with, or is an emergency
284 * call.
285 */
286 public static boolean isCallWithNoValidAccounts(DialerCall call) {
287 if (call != null && !call.isEmergencyCall()) {
288 Bundle extras = call.getIntentExtras();
289
290 if (extras == null) {
291 extras = EMPTY_EXTRAS;
292 }
293
294 final List<PhoneAccountHandle> phoneAccountHandles =
295 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
296
297 if ((call.getAccountHandle() == null
298 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700299 LogUtil.i(
300 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800301 return true;
302 }
303 }
304 return false;
305 }
306
307 public InCallState getInCallState() {
308 return mInCallState;
309 }
310
311 public CallList getCallList() {
312 return mCallList;
313 }
314
315 public void setUp(
316 @NonNull Context context,
317 CallList callList,
318 ExternalCallList externalCallList,
319 StatusBarNotifier statusBarNotifier,
320 ExternalCallNotifier externalCallNotifier,
321 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700322 ProximitySensor proximitySensor,
323 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800324 if (mServiceConnected) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700325 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800326 if (context != mContext || callList != mCallList) {
327 throw new IllegalStateException();
328 }
329 return;
330 }
331
332 Objects.requireNonNull(context);
333 mContext = context;
334
335 mContactInfoCache = contactInfoCache;
336
337 mStatusBarNotifier = statusBarNotifier;
338 mExternalCallNotifier = externalCallNotifier;
339 addListener(mStatusBarNotifier);
Eric Erfaniand8046e52017-04-06 09:41:50 -0700340 EnrichedCallComponent.get(mContext)
341 .getEnrichedCallManager()
342 .registerStateChangedListener(mStatusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800343
344 mProximitySensor = proximitySensor;
345 addListener(mProximitySensor);
346
347 mThemeColorManager =
348 new ThemeColorManager(new InCallUIMaterialColorMapUtils(mContext.getResources()));
349
350 mCallList = callList;
351 mExternalCallList = externalCallList;
352 externalCallList.addExternalCallListener(mExternalCallNotifier);
353 externalCallList.addExternalCallListener(mExternalCallListener);
354
355 // This only gets called by the service so this is okay.
356 mServiceConnected = true;
357
358 // The final thing we do in this set up is add ourselves as a listener to CallList. This
359 // will kick off an update and the whole process can start.
360 mCallList.addListener(this);
361
362 // Create spam call list listener and add it to the list of listeners
363 mSpamCallListListener = new SpamCallListListener(context);
364 mCallList.addListener(mSpamCallListListener);
365
366 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800367
Eric Erfanian83b20212017-05-31 08:53:10 -0700368 mFilteredQueryHandler = filteredNumberQueryHandler;
Eric Erfanianccca3152017-02-22 16:32:36 -0800369 mContext
370 .getSystemService(TelephonyManager.class)
371 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
372
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700373 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
Eric Erfanianccca3152017-02-22 16:32:36 -0800374 }
375
376 /**
377 * Called when the telephony service has disconnected from us. This will happen when there are no
378 * more active calls. However, we may still want to continue showing the UI for certain cases like
379 * showing "Call Ended". What we really want is to wait for the activity and the service to both
380 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
381 * secondary method that performs the aforementioned logic.
382 */
383 public void tearDown() {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700384 LogUtil.d("InCallPresenter.tearDown", "tearDown");
Eric Erfanianccca3152017-02-22 16:32:36 -0800385 mCallList.clearOnDisconnect();
386
387 mServiceConnected = false;
388
389 mContext
390 .getSystemService(TelephonyManager.class)
391 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
392
393 attemptCleanup();
394 VideoPauseController.getInstance().tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -0800395 }
396
397 private void attemptFinishActivity() {
398 final boolean doFinish = (mInCallActivity != null && isActivityStarted());
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700399 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800400 if (doFinish) {
401 mInCallActivity.setExcludeFromRecents(true);
402 mInCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800403 }
404 }
405
406 /**
407 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
408 * for more insight on the tear-down process.
409 */
410 public void unsetActivity(InCallActivity inCallActivity) {
411 if (inCallActivity == null) {
412 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
413 }
414 if (mInCallActivity == null) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700415 LogUtil.i(
416 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800417 return;
418 }
419 if (mInCallActivity != inCallActivity) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700420 LogUtil.w(
421 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800422 "Second instance of InCallActivity is trying to unregister when another"
423 + " instance is active. Ignoring.");
424 return;
425 }
426 updateActivity(null);
427 }
428
429 /**
430 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
431 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
432 */
433 private void updateActivity(InCallActivity inCallActivity) {
434 boolean updateListeners = false;
435 boolean doAttemptCleanup = false;
436
437 if (inCallActivity != null) {
438 if (mInCallActivity == null) {
439 updateListeners = true;
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700440 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800441 } else {
442 // since setActivity is called onStart(), it can be called multiple times.
443 // This is fine and ignorable, but we do not want to update the world every time
444 // this happens (like going to/from background) so we do not set updateListeners.
445 }
446
447 mInCallActivity = inCallActivity;
448 mInCallActivity.setExcludeFromRecents(false);
449
450 // By the time the UI finally comes up, the call may already be disconnected.
451 // If that's the case, we may need to show an error dialog.
452 if (mCallList != null && mCallList.getDisconnectedCall() != null) {
453 maybeShowErrorDialogOnDisconnect(mCallList.getDisconnectedCall());
454 }
455
456 // When the UI comes up, we need to first check the in-call state.
457 // If we are showing NO_CALLS, that means that a call probably connected and
458 // then immediately disconnected before the UI was able to come up.
459 // If we dont have any calls, start tearing down the UI instead.
460 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
461 // it has been set.
462 if (mInCallState == InCallState.NO_CALLS) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700463 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800464 attemptFinishActivity();
465 return;
466 }
467 } else {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700468 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800469 updateListeners = true;
470 mInCallActivity = null;
471
472 // We attempt cleanup for the destroy case but only after we recalculate the state
473 // to see if we need to come back up or stay shut down. This is why we do the
474 // cleanup after the call to onCallListChange() instead of directly here.
475 doAttemptCleanup = true;
476 }
477
478 // Messages can come from the telephony layer while the activity is coming up
479 // and while the activity is going down. So in both cases we need to recalculate what
480 // state we should be in after they complete.
481 // Examples: (1) A new incoming call could come in and then get disconnected before
482 // the activity is created.
483 // (2) All calls could disconnect and then get a new incoming call before the
484 // activity is destroyed.
485 //
486 // b/1122139 - We previously had a check for mServiceConnected here as well, but there are
487 // cases where we need to recalculate the current state even if the service in not
488 // connected. In particular the case where startOrFinish() is called while the app is
489 // already finish()ing. In that case, we skip updating the state with the knowledge that
490 // we will check again once the activity has finished. That means we have to recalculate the
491 // state here even if the service is disconnected since we may not have finished a state
492 // transition while finish()ing.
493 if (updateListeners) {
494 onCallListChange(mCallList);
495 }
496
497 if (doAttemptCleanup) {
498 attemptCleanup();
499 }
500 }
501
502 public void setManageConferenceActivity(
503 @Nullable ManageConferenceActivity manageConferenceActivity) {
504 mManageConferenceActivity = manageConferenceActivity;
505 }
506
507 public void onBringToForeground(boolean showDialpad) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700508 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800509 bringToForeground(showDialpad);
510 }
511
512 public void onCallAdded(final android.telecom.Call call) {
513 LatencyReport latencyReport = new LatencyReport(call);
514 if (shouldAttemptBlocking(call)) {
515 maybeBlockCall(call, latencyReport);
516 } else {
517 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
518 mExternalCallList.onCallAdded(call);
519 } else {
520 latencyReport.onCallBlockingDone();
521 mCallList.onCallAdded(mContext, call, latencyReport);
522 }
523 }
524
525 // Since a call has been added we are no longer waiting for Telecom to send us a call.
526 setBoundAndWaitingForOutgoingCall(false, null);
527 call.registerCallback(mCallCallback);
528 }
529
530 private boolean shouldAttemptBlocking(android.telecom.Call call) {
531 if (call.getState() != android.telecom.Call.STATE_RINGING) {
532 return false;
533 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700534 if (!UserManagerCompat.isUserUnlocked(mContext)) {
535 LogUtil.i(
536 "InCallPresenter.shouldAttemptBlocking",
537 "not attempting to block incoming call because user is locked");
538 return false;
539 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800540 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700541 LogUtil.i(
542 "InCallPresenter.shouldAttemptBlocking",
543 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800544 return false;
545 }
546 if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700547 LogUtil.i(
548 "InCallPresenter.shouldAttemptBlocking",
549 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800550 return false;
551 }
552 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
553 return false;
554 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700555 if (FilteredNumberCompat.useNewFiltering(mContext)) {
556 LogUtil.i(
557 "InCallPresenter.shouldAttemptBlocking",
558 "not attempting to block incoming call because framework blocking is in use");
559 return false;
560 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800561 return true;
562 }
563
564 /**
565 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
566 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
567 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
568 * call anyways.
569 */
570 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
571 final String countryIso = GeoUtil.getCurrentCountryIso(mContext);
572 final String number = TelecomCallUtil.getNumber(call);
573 final long timeAdded = System.currentTimeMillis();
574
575 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
576 // main UI thread. It is needed so we can change its value within different scopes, since
577 // that cannot be done with a final boolean.
578 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
579
580 final Handler handler = new Handler();
581
582 // Proceed if the query is slow; the call may still be blocked after the query returns.
583 final Runnable runnable =
584 new Runnable() {
585 @Override
586 public void run() {
587 hasTimedOut.set(true);
588 latencyReport.onCallBlockingDone();
589 mCallList.onCallAdded(mContext, call, latencyReport);
590 }
591 };
592 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
593
594 OnCheckBlockedListener onCheckBlockedListener =
595 new OnCheckBlockedListener() {
596 @Override
597 public void onCheckComplete(final Integer id) {
598 if (isReadyForTearDown()) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700599 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800600 return;
601 }
602 if (!hasTimedOut.get()) {
603 handler.removeCallbacks(runnable);
604 }
605 if (id == null) {
606 if (!hasTimedOut.get()) {
607 latencyReport.onCallBlockingDone();
608 mCallList.onCallAdded(mContext, call, latencyReport);
609 }
610 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700611 LogUtil.d(
612 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800613 if (!hasTimedOut.get()) {
614 handler.removeCallbacks(runnable);
615
616 latencyReport.onCallBlockingDone();
617 mCallList.onCallAdded(mContext, call, latencyReport);
618 }
619 } else {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700620 LogUtil.i(
621 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800622 call.reject(false, null);
623 Logger.get(mContext).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
624
625 /*
626 * If mContext is null, then the InCallPresenter was torn down before the
627 * block check had a chance to complete. The context is no longer valid, so
628 * don't attempt to remove the call log entry.
629 */
630 if (mContext == null) {
631 return;
632 }
633 // Register observer to update the call log.
634 // BlockedNumberContentObserver will unregister after successful log or timeout.
635 BlockedNumberContentObserver contentObserver =
636 new BlockedNumberContentObserver(mContext, new Handler(), number, timeAdded);
637 contentObserver.register();
638 }
639 }
640 };
641
642 mFilteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
643 }
644
645 public void onCallRemoved(android.telecom.Call call) {
646 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
647 mExternalCallList.onCallRemoved(call);
648 } else {
649 mCallList.onCallRemoved(mContext, call);
650 call.unregisterCallback(mCallCallback);
651 }
652 }
653
654 public void onCanAddCallChanged(boolean canAddCall) {
655 for (CanAddCallListener listener : mCanAddCallListeners) {
656 listener.onCanAddCallChanged(canAddCall);
657 }
658 }
659
660 @Override
661 public void onWiFiToLteHandover(DialerCall call) {
662 if (mInCallActivity != null) {
663 mInCallActivity.onWiFiToLteHandover(call);
664 }
665 }
666
667 @Override
668 public void onHandoverToWifiFailed(DialerCall call) {
669 if (mInCallActivity != null) {
670 mInCallActivity.onHandoverToWifiFailed(call);
671 }
672 }
673
Eric Erfanianc857f902017-05-15 14:05:33 -0700674 @Override
675 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
676 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
677 if (mInCallActivity != null) {
678 mInCallActivity.onInternationalCallOnWifi(call);
679 }
680 }
681
Eric Erfanianccca3152017-02-22 16:32:36 -0800682 /**
683 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
684 * app based on the information it gets from CallList. Dispatches the in-call state to all
685 * listeners. Can trigger the creation or destruction of the UI based on the states that is
686 * calculates.
687 */
688 @Override
689 public void onCallListChange(CallList callList) {
690 if (mInCallActivity != null && mInCallActivity.isInCallScreenAnimating()) {
691 mAwaitingCallListUpdate = true;
692 return;
693 }
694 if (callList == null) {
695 return;
696 }
697
698 mAwaitingCallListUpdate = false;
699
700 InCallState newState = getPotentialStateFromCallList(callList);
701 InCallState oldState = mInCallState;
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700702 LogUtil.d(
703 "InCallPresenter.onCallListChange",
704 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700705
706 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
707 // incall activity for that call will still exist (even if it's not visible). In the case of
708 // an incoming call in that situation, just disconnect that "waiting for account" call and
709 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
710 // b/33247755 for more details.
711 DialerCall waitingForAccountCall;
712 if (newState == InCallState.INCOMING
713 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
714 waitingForAccountCall.disconnect();
Eric Erfanian91ce7d22017-06-05 13:35:02 -0700715 // The InCallActivity might be destroyed or not started yet at this point.
716 if (isActivityStarted()) {
717 mInCallActivity.dismissPendingDialogs();
718 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700719 }
720
Eric Erfanianccca3152017-02-22 16:32:36 -0800721 newState = startOrFinishUi(newState);
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700722 LogUtil.d(
723 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800724
725 // Set the new state before announcing it to the world
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700726 LogUtil.i(
727 "InCallPresenter.onCallListChange",
728 "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800729 mInCallState = newState;
730
731 // notify listeners of new state
732 for (InCallStateListener listener : mListeners) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700733 LogUtil.d(
734 "InCallPresenter.onCallListChange",
735 "Notify " + listener + " of state " + mInCallState.toString());
Eric Erfanianccca3152017-02-22 16:32:36 -0800736 listener.onStateChange(oldState, mInCallState, callList);
737 }
738
739 if (isActivityStarted()) {
740 final boolean hasCall =
741 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
742 mInCallActivity.dismissKeyguard(hasCall);
743 }
744 }
745
746 /** Called when there is a new incoming call. */
747 @Override
748 public void onIncomingCall(DialerCall call) {
749 InCallState newState = startOrFinishUi(InCallState.INCOMING);
750 InCallState oldState = mInCallState;
751
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700752 LogUtil.i(
753 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800754 mInCallState = newState;
755
756 for (IncomingCallListener listener : mIncomingCallListeners) {
757 listener.onIncomingCall(oldState, mInCallState, call);
758 }
759
760 if (mInCallActivity != null) {
761 // Re-evaluate which fragment is being shown.
762 mInCallActivity.onPrimaryCallStateChanged();
763 }
764 }
765
766 @Override
767 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700768 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
Eric Erfanianccca3152017-02-22 16:32:36 -0800769 && mInCallState == InCallPresenter.InCallState.INCOMING) {
770 LogUtil.i(
771 "InCallPresenter.onUpgradeToVideo",
772 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700773 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800774 }
775
776 if (mInCallActivity != null) {
777 // Re-evaluate which fragment is being shown.
778 mInCallActivity.onPrimaryCallStateChanged();
779 }
780 }
781
782 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700783 public void onSessionModificationStateChange(DialerCall call) {
784 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800785 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
786 if (mProximitySensor == null) {
787 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
788 return;
789 }
790 mProximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700791 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
Eric Erfanianccca3152017-02-22 16:32:36 -0800792 if (mInCallActivity != null) {
793 // Re-evaluate which fragment is being shown.
794 mInCallActivity.onPrimaryCallStateChanged();
795 }
796 }
797
798 /**
799 * Called when a call becomes disconnected. Called everytime an existing call changes from being
800 * connected (incoming/outgoing/active) to disconnected.
801 */
802 @Override
803 public void onDisconnect(DialerCall call) {
804 maybeShowErrorDialogOnDisconnect(call);
805
806 // We need to do the run the same code as onCallListChange.
807 onCallListChange(mCallList);
808
809 if (isActivityStarted()) {
810 mInCallActivity.dismissKeyguard(false);
811 }
812
813 if (call.isEmergencyCall()) {
814 FilteredNumbersUtil.recordLastEmergencyCallTime(mContext);
815 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800816
Eric Erfanianfc37b022017-03-21 10:11:17 -0700817 if (!mCallList.hasLiveCall()
818 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700819 && !isSecretCode(call.getNumber())
Eric Erfanianfc37b022017-03-21 10:11:17 -0700820 && !CallerInfoUtils.isVoiceMailNumber(mContext, call)) {
Eric Erfanian10b34a52017-05-04 08:23:17 -0700821 PostCall.onCallDisconnected(mContext, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -0800822 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800823 }
824
Eric Erfanian10b34a52017-05-04 08:23:17 -0700825 private boolean isSecretCode(@Nullable String number) {
826 return number != null
827 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
828 }
829
Eric Erfanianccca3152017-02-22 16:32:36 -0800830 /** Given the call list, return the state in which the in-call screen should be. */
831 public InCallState getPotentialStateFromCallList(CallList callList) {
832
833 InCallState newState = InCallState.NO_CALLS;
834
835 if (callList == null) {
836 return newState;
837 }
838 if (callList.getIncomingCall() != null) {
839 newState = InCallState.INCOMING;
840 } else if (callList.getWaitingForAccountCall() != null) {
841 newState = InCallState.WAITING_FOR_ACCOUNT;
842 } else if (callList.getPendingOutgoingCall() != null) {
843 newState = InCallState.PENDING_OUTGOING;
844 } else if (callList.getOutgoingCall() != null) {
845 newState = InCallState.OUTGOING;
846 } else if (callList.getActiveCall() != null
847 || callList.getBackgroundCall() != null
848 || callList.getDisconnectedCall() != null
849 || callList.getDisconnectingCall() != null) {
850 newState = InCallState.INCALL;
851 }
852
853 if (newState == InCallState.NO_CALLS) {
854 if (mBoundAndWaitingForOutgoingCall) {
855 return InCallState.OUTGOING;
856 }
857 }
858
859 return newState;
860 }
861
862 public boolean isBoundAndWaitingForOutgoingCall() {
863 return mBoundAndWaitingForOutgoingCall;
864 }
865
866 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700867 LogUtil.i(
868 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
869 "setBoundAndWaitingForOutgoingCall: " + isBound);
Eric Erfanianccca3152017-02-22 16:32:36 -0800870 mBoundAndWaitingForOutgoingCall = isBound;
871 mThemeColorManager.setPendingPhoneAccountHandle(handle);
872 if (isBound && mInCallState == InCallState.NO_CALLS) {
873 mInCallState = InCallState.OUTGOING;
874 }
875 }
876
877 public void onShrinkAnimationComplete() {
878 if (mAwaitingCallListUpdate) {
879 onCallListChange(mCallList);
880 }
881 }
882
883 public void addIncomingCallListener(IncomingCallListener listener) {
884 Objects.requireNonNull(listener);
885 mIncomingCallListeners.add(listener);
886 }
887
888 public void removeIncomingCallListener(IncomingCallListener listener) {
889 if (listener != null) {
890 mIncomingCallListeners.remove(listener);
891 }
892 }
893
894 public void addListener(InCallStateListener listener) {
895 Objects.requireNonNull(listener);
896 mListeners.add(listener);
897 }
898
899 public void removeListener(InCallStateListener listener) {
900 if (listener != null) {
901 mListeners.remove(listener);
902 }
903 }
904
905 public void addDetailsListener(InCallDetailsListener listener) {
906 Objects.requireNonNull(listener);
907 mDetailsListeners.add(listener);
908 }
909
910 public void removeDetailsListener(InCallDetailsListener listener) {
911 if (listener != null) {
912 mDetailsListeners.remove(listener);
913 }
914 }
915
916 public void addCanAddCallListener(CanAddCallListener listener) {
917 Objects.requireNonNull(listener);
918 mCanAddCallListeners.add(listener);
919 }
920
921 public void removeCanAddCallListener(CanAddCallListener listener) {
922 if (listener != null) {
923 mCanAddCallListeners.remove(listener);
924 }
925 }
926
927 public void addOrientationListener(InCallOrientationListener listener) {
928 Objects.requireNonNull(listener);
929 mOrientationListeners.add(listener);
930 }
931
932 public void removeOrientationListener(InCallOrientationListener listener) {
933 if (listener != null) {
934 mOrientationListeners.remove(listener);
935 }
936 }
937
938 public void addInCallEventListener(InCallEventListener listener) {
939 Objects.requireNonNull(listener);
940 mInCallEventListeners.add(listener);
941 }
942
943 public void removeInCallEventListener(InCallEventListener listener) {
944 if (listener != null) {
945 mInCallEventListeners.remove(listener);
946 }
947 }
948
949 public ProximitySensor getProximitySensor() {
950 return mProximitySensor;
951 }
952
953 public PseudoScreenState getPseudoScreenState() {
954 return mPseudoScreenState;
955 }
956
957 /** Returns true if the incall app is the foreground application. */
958 public boolean isShowingInCallUi() {
959 if (!isActivityStarted()) {
960 return false;
961 }
962 if (mManageConferenceActivity != null && mManageConferenceActivity.isVisible()) {
963 return true;
964 }
965 return mInCallActivity.isVisible();
966 }
967
968 /**
969 * Returns true if the activity has been created and is running. Returns true as long as activity
970 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
971 * (not in foreground).
972 */
973 public boolean isActivityStarted() {
974 return (mInCallActivity != null
975 && !mInCallActivity.isDestroyed()
976 && !mInCallActivity.isFinishing());
977 }
978
979 /**
980 * Determines if the In-Call app is currently changing configuration.
981 *
982 * @return {@code true} if the In-Call app is changing configuration.
983 */
984 public boolean isChangingConfigurations() {
985 return mIsChangingConfigurations;
986 }
987
988 /**
989 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
990 * screen orientation).
991 */
992 /*package*/
993 void updateIsChangingConfigurations() {
994 mIsChangingConfigurations = false;
995 if (mInCallActivity != null) {
996 mIsChangingConfigurations = mInCallActivity.isChangingConfigurations();
997 }
Eric Erfaniand538e0b2017-06-30 15:26:17 -0700998 LogUtil.v(
999 "InCallPresenter.updateIsChangingConfigurations",
1000 "updateIsChangingConfigurations = " + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001001 }
1002
1003 /** Called when the activity goes in/out of the foreground. */
1004 public void onUiShowing(boolean showing) {
1005 // We need to update the notification bar when we leave the UI because that
1006 // could trigger it to show again.
1007 if (mStatusBarNotifier != null) {
1008 mStatusBarNotifier.updateNotification(mCallList);
1009 }
1010
1011 if (mProximitySensor != null) {
1012 mProximitySensor.onInCallShowing(showing);
1013 }
1014
1015 Intent broadcastIntent = Bindings.get(mContext).getUiReadyBroadcastIntent(mContext);
1016 if (broadcastIntent != null) {
1017 broadcastIntent.putExtra(EXTRA_FIRST_TIME_SHOWN, !mIsActivityPreviouslyStarted);
1018
1019 if (showing) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001020 LogUtil.d("InCallPresenter.onUiShowing", "Sending sticky broadcast: ", broadcastIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001021 mContext.sendStickyBroadcast(broadcastIntent);
1022 } else {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001023 LogUtil.d("InCallPresenter.onUiShowing", "Removing sticky broadcast: ", broadcastIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001024 mContext.removeStickyBroadcast(broadcastIntent);
1025 }
1026 }
1027
1028 if (showing) {
1029 mIsActivityPreviouslyStarted = true;
1030 } else {
1031 updateIsChangingConfigurations();
1032 }
1033
1034 for (InCallUiListener listener : mInCallUiListeners) {
1035 listener.onUiShowing(showing);
1036 }
1037
1038 if (mInCallActivity != null) {
1039 // Re-evaluate which fragment is being shown.
1040 mInCallActivity.onPrimaryCallStateChanged();
1041 }
1042 }
1043
Eric Erfanianea7890c2017-06-19 12:40:59 -07001044 public void refreshUi() {
1045 if (mInCallActivity != null) {
1046 // Re-evaluate which fragment is being shown.
1047 mInCallActivity.onPrimaryCallStateChanged();
1048 }
1049 }
1050
Eric Erfanianccca3152017-02-22 16:32:36 -08001051 public void addInCallUiListener(InCallUiListener listener) {
1052 mInCallUiListeners.add(listener);
1053 }
1054
1055 public boolean removeInCallUiListener(InCallUiListener listener) {
1056 return mInCallUiListeners.remove(listener);
1057 }
1058
1059 /*package*/
1060 void onActivityStarted() {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001061 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001062 notifyVideoPauseController(true);
Eric Erfaniand8046e52017-04-06 09:41:50 -07001063 if (mStatusBarNotifier != null) {
1064 // TODO - b/36649622: Investigate this redundant call
1065 mStatusBarNotifier.updateNotification(mCallList);
1066 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001067 }
1068
1069 /*package*/
1070 void onActivityStopped() {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001071 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001072 notifyVideoPauseController(false);
1073 }
1074
1075 private void notifyVideoPauseController(boolean showing) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001076 LogUtil.d(
1077 "InCallPresenter.notifyVideoPauseController",
1078 "mIsChangingConfigurations=" + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001079 if (!mIsChangingConfigurations) {
1080 VideoPauseController.getInstance().onUiShowing(showing);
1081 }
1082 }
1083
1084 /** Brings the app into the foreground if possible. */
1085 public void bringToForeground(boolean showDialpad) {
1086 // Before we bring the incall UI to the foreground, we check to see if:
1087 // 1. It is not currently in the foreground
1088 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1089 // be displayed)
1090 // If the activity hadn't actually been started previously, yet there are still calls
1091 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1092 // bring it up the UI regardless.
1093 if (!isShowingInCallUi() && mInCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001094 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001095 }
1096 }
1097
1098 public void onPostDialCharWait(String callId, String chars) {
1099 if (isActivityStarted()) {
1100 mInCallActivity.showPostCharWaitDialog(callId, chars);
1101 }
1102 }
1103
1104 /**
1105 * Handles the green CALL key while in-call.
1106 *
1107 * @return true if we consumed the event.
1108 */
1109 public boolean handleCallKey() {
1110 LogUtil.v("InCallPresenter.handleCallKey", null);
1111
1112 // The green CALL button means either "Answer", "Unhold", or
1113 // "Swap calls", or can be a no-op, depending on the current state
1114 // of the Phone.
1115
1116 /** INCOMING CALL */
1117 final CallList calls = mCallList;
1118 final DialerCall incomingCall = calls.getIncomingCall();
1119 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1120
1121 // (1) Attempt to answer a call
1122 if (incomingCall != null) {
1123 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1124 return true;
1125 }
1126
1127 /** STATE_ACTIVE CALL */
1128 final DialerCall activeCall = calls.getActiveCall();
1129 if (activeCall != null) {
1130 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1131 // consolidate this logic.
1132 final boolean canMerge =
1133 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1134 final boolean canSwap =
1135 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1136
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001137 LogUtil.v(
1138 "InCallPresenter.handleCallKey",
1139 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001140
1141 // (2) Attempt actions on conference calls
1142 if (canMerge) {
1143 TelecomAdapter.getInstance().merge(activeCall.getId());
1144 return true;
1145 } else if (canSwap) {
1146 TelecomAdapter.getInstance().swap(activeCall.getId());
1147 return true;
1148 }
1149 }
1150
1151 /** BACKGROUND CALL */
1152 final DialerCall heldCall = calls.getBackgroundCall();
1153 if (heldCall != null) {
1154 // We have a hold call so presumeable it will always support HOLD...but
1155 // there is no harm in double checking.
1156 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1157
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001158 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001159
1160 // (4) unhold call
1161 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1162 heldCall.unhold();
1163 return true;
1164 }
1165 }
1166
1167 // Always consume hard keys
1168 return true;
1169 }
1170
1171 /**
1172 * A dialog could have prevented in-call screen from being previously finished. This function
1173 * checks to see if there should be any UI left and if not attempts to tear down the UI.
1174 */
1175 public void onDismissDialog() {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001176 LogUtil.i("InCallPresenter.onDismissDialog", "Dialog dismissed");
Eric Erfanianccca3152017-02-22 16:32:36 -08001177 if (mInCallState == InCallState.NO_CALLS) {
1178 attemptFinishActivity();
1179 attemptCleanup();
1180 }
1181 }
1182
1183 /** Clears the previous fullscreen state. */
1184 public void clearFullscreen() {
1185 mIsFullScreen = false;
1186 }
1187
1188 /**
1189 * Changes the fullscreen mode of the in-call UI.
1190 *
1191 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1192 * otherwise.
1193 */
1194 public void setFullScreen(boolean isFullScreen) {
1195 setFullScreen(isFullScreen, false /* force */);
1196 }
1197
1198 /**
1199 * Changes the fullscreen mode of the in-call UI.
1200 *
1201 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1202 * otherwise.
1203 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1204 */
1205 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001206 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001207
1208 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1209 if (isDialpadVisible()) {
1210 isFullScreen = false;
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001211 LogUtil.v(
1212 "InCallPresenter.setFullScreen",
1213 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001214 }
1215
1216 if (mIsFullScreen == isFullScreen && !force) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001217 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001218 return;
1219 }
1220 mIsFullScreen = isFullScreen;
1221 notifyFullscreenModeChange(mIsFullScreen);
1222 }
1223
1224 /**
1225 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1226 * otherwise.
1227 */
1228 public boolean isFullscreen() {
1229 return mIsFullScreen;
1230 }
1231
1232 /**
1233 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1234 *
1235 * @param isFullscreenMode {@code True} if entering full screen mode.
1236 */
1237 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
1238 for (InCallEventListener listener : mInCallEventListeners) {
1239 listener.onFullscreenModeChanged(isFullscreenMode);
1240 }
1241 }
1242
1243 /**
1244 * For some disconnected causes, we show a dialog. This calls into the activity to show the dialog
1245 * if appropriate for the call.
1246 */
1247 private void maybeShowErrorDialogOnDisconnect(DialerCall call) {
1248 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1249 if (isActivityStarted() && call.getState() == DialerCall.State.DISCONNECTED) {
1250 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1251 setDisconnectCauseForMissingAccounts(call);
1252 }
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001253 mInCallActivity.maybeShowErrorDialogOnDisconnect(
1254 new DisconnectMessage(mInCallActivity, call));
Eric Erfanianccca3152017-02-22 16:32:36 -08001255 }
1256 }
1257
1258 /**
1259 * When the state of in-call changes, this is the first method to get called. It determines if the
1260 * UI needs to be started or finished depending on the new state and does it.
1261 */
1262 private InCallState startOrFinishUi(InCallState newState) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001263 LogUtil.d(
1264 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + mInCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001265
1266 // TODO: Consider a proper state machine implementation
1267
1268 // If the state isn't changing we have already done any starting/stopping of activities in
1269 // a previous pass...so lets cut out early
1270 if (newState == mInCallState) {
1271 return newState;
1272 }
1273
1274 // A new Incoming call means that the user needs to be notified of the the call (since
1275 // it wasn't them who initiated it). We do this through full screen notifications and
1276 // happens indirectly through {@link StatusBarNotifier}.
1277 //
1278 // The process for incoming calls is as follows:
1279 //
1280 // 1) CallList - Announces existence of new INCOMING call
1281 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1282 // - should be set to INCOMING.
1283 // 3) InCallPresenter - This method is called to see if we need to start or finish
1284 // the app given the new state.
1285 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1286 // StatusBarNotifier explicitly to issue a FullScreen Notification
1287 // that will either start the InCallActivity or show the user a
1288 // top-level notification dialog if the user is in an immersive app.
1289 // That notification can also start the InCallActivity.
1290 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1291 // call InCallPresenter::setActivity() to let the presenter
1292 // know that start-up is complete.
1293 //
1294 // [ AND NOW YOU'RE IN THE CALL. voila! ]
1295 //
1296 // Our app is started using a fullScreen notification. We need to do this whenever
1297 // we get an incoming call. Depending on the current context of the device, either a
1298 // incoming call HUN or the actual InCallActivity will be shown.
1299 final boolean startIncomingCallSequence = (InCallState.INCOMING == newState);
1300
1301 // A dialog to show on top of the InCallUI to select a PhoneAccount
1302 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1303
1304 // A new outgoing call indicates that the user just now dialed a number and when that
1305 // happens we need to display the screen immediately or show an account picker dialog if
1306 // no default is set. However, if the main InCallUI is already visible, we do not want to
1307 // re-initiate the start-up animation, so we do not need to do anything here.
1308 //
1309 // It is also possible to go into an intermediate state where the call has been initiated
1310 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1311 // This pending outgoing state can also launch the call screen.
1312 //
1313 // This is different from the incoming call sequence because we do not need to shock the
1314 // user with a top-level notification. Just show the call UI normally.
1315 boolean callCardFragmentVisible =
1316 mInCallActivity != null && mInCallActivity.getCallCardFragmentVisible();
1317 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1318 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1319
1320 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1321 // outgoing call process, so the UI should be brought up to show an error dialog.
1322 showCallUi |=
1323 (InCallState.PENDING_OUTGOING == mInCallState
1324 && InCallState.INCALL == newState
1325 && !isShowingInCallUi());
1326
1327 // Another exception - InCallActivity is in charge of disconnecting a call with no
1328 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1329 // call so that:
1330 // 1) The call can be disconnected correctly
1331 // 2) The UI comes up and correctly displays the error dialog.
1332 // TODO: Remove these special case conditions by making InCallPresenter a true state
1333 // machine. Telecom should also be the component responsible for disconnecting a call
1334 // with no valid accounts.
1335 showCallUi |=
1336 InCallState.PENDING_OUTGOING == newState
1337 && mainUiNotVisible
1338 && isCallWithNoValidAccounts(mCallList.getPendingOutgoingCall());
1339
1340 // The only time that we have an instance of mInCallActivity and it isn't started is
1341 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1342 // the activity. When it is finally destroyed, we double check if we should bring it back
1343 // up so we aren't going to lose anything by avoiding a second startup here.
1344 boolean activityIsFinishing = mInCallActivity != null && !isActivityStarted();
1345 if (activityIsFinishing) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001346 LogUtil.i(
1347 "InCallPresenter.startOrFinishUi",
1348 "Undo the state change: " + newState + " -> " + mInCallState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001349 return mInCallState;
1350 }
1351
1352 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1353 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1354 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1355 // outgoing call after user ignore it by pressing home button.
1356 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1357 && !showCallUi
1358 && isActivityStarted()) {
1359 mInCallActivity.dismissPendingDialogs();
1360 }
1361
1362 if (showCallUi || showAccountPicker) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001363 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001364 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001365 } else if (startIncomingCallSequence) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001366 LogUtil.i("InCallPresenter.startOrFinishUi", "Start Full Screen in call UI");
Eric Erfanianccca3152017-02-22 16:32:36 -08001367
Eric Erfaniand8046e52017-04-06 09:41:50 -07001368 mStatusBarNotifier.updateNotification(mCallList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001369 } else if (newState == InCallState.NO_CALLS) {
1370 // The new state is the no calls state. Tear everything down.
1371 attemptFinishActivity();
1372 attemptCleanup();
1373 }
1374
1375 return newState;
1376 }
1377
1378 /**
1379 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1380 * or PhoneAccounts to select from.
1381 */
1382 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1383
1384 Bundle extras = call.getIntentExtras();
1385 // Initialize the extras bundle to avoid NPE
1386 if (extras == null) {
1387 extras = new Bundle();
1388 }
1389
1390 final List<PhoneAccountHandle> phoneAccountHandles =
1391 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1392
1393 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1394 String scheme = call.getHandle().getScheme();
1395 final String errorMsg =
1396 PhoneAccount.SCHEME_TEL.equals(scheme)
1397 ? mContext.getString(R.string.callFailed_simError)
1398 : mContext.getString(R.string.incall_error_supp_service_unknown);
1399 DisconnectCause disconnectCause =
1400 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1401 call.setDisconnectCause(disconnectCause);
1402 }
1403 }
1404
Eric Erfanianccca3152017-02-22 16:32:36 -08001405 /**
1406 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1407 * Calling classes should use this as an indication whether to interact with the
1408 * InCallPresenter or not.
1409 */
1410 public boolean isReadyForTearDown() {
1411 return mInCallActivity == null && !mServiceConnected && mInCallState == InCallState.NO_CALLS;
1412 }
1413
1414 /**
1415 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1416 */
1417 private void attemptCleanup() {
1418 if (isReadyForTearDown()) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001419 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001420
1421 cleanupSurfaces();
1422
1423 mIsActivityPreviouslyStarted = false;
1424 mIsChangingConfigurations = false;
1425
1426 // blow away stale contact info so that we get fresh data on
1427 // the next set of calls
1428 if (mContactInfoCache != null) {
1429 mContactInfoCache.clearCache();
1430 }
1431 mContactInfoCache = null;
1432
1433 if (mProximitySensor != null) {
1434 removeListener(mProximitySensor);
1435 mProximitySensor.tearDown();
1436 }
1437 mProximitySensor = null;
1438
1439 if (mStatusBarNotifier != null) {
1440 removeListener(mStatusBarNotifier);
Eric Erfaniand8046e52017-04-06 09:41:50 -07001441 EnrichedCallComponent.get(mContext)
1442 .getEnrichedCallManager()
1443 .unregisterStateChangedListener(mStatusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001444 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001445
Eric Erfanianccca3152017-02-22 16:32:36 -08001446 if (mExternalCallNotifier != null && mExternalCallList != null) {
1447 mExternalCallList.removeExternalCallListener(mExternalCallNotifier);
1448 }
1449 mStatusBarNotifier = null;
1450
1451 if (mCallList != null) {
1452 mCallList.removeListener(this);
1453 mCallList.removeListener(mSpamCallListListener);
1454 }
1455 mCallList = null;
1456
1457 mContext = null;
1458 mInCallActivity = null;
1459 mManageConferenceActivity = null;
1460
1461 mListeners.clear();
1462 mIncomingCallListeners.clear();
1463 mDetailsListeners.clear();
1464 mCanAddCallListeners.clear();
1465 mOrientationListeners.clear();
1466 mInCallEventListeners.clear();
1467 mInCallUiListeners.clear();
1468
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001469 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001470 }
1471 }
1472
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001473 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001474 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -08001475 mContext.startActivity(
1476 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001477 mContext, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001478 }
1479
1480 public void onServiceBind() {
1481 mServiceBound = true;
1482 }
1483
1484 public void onServiceUnbind() {
1485 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
1486 mServiceBound = false;
1487 }
1488
1489 public boolean isServiceBound() {
1490 return mServiceBound;
1491 }
1492
1493 public void maybeStartRevealAnimation(Intent intent) {
1494 if (intent == null || mInCallActivity != null) {
1495 return;
1496 }
1497 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1498 if (extras == null) {
1499 // Incoming call, just show the in-call UI directly.
1500 return;
1501 }
1502
1503 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1504 // Account selection dialog will show up so don't show the animation.
1505 return;
1506 }
1507
1508 final PhoneAccountHandle accountHandle =
1509 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1510 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001511
1512 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(true, accountHandle);
1513
1514 final Intent activityIntent =
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001515 InCallActivity.getIntent(mContext, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001516 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
1517 mContext.startActivity(activityIntent);
1518 }
1519
1520 /**
1521 * Retrieves the current in-call camera manager instance, creating if necessary.
1522 *
1523 * @return The {@link InCallCameraManager}.
1524 */
1525 public InCallCameraManager getInCallCameraManager() {
1526 synchronized (this) {
1527 if (mInCallCameraManager == null) {
1528 mInCallCameraManager = new InCallCameraManager(mContext);
1529 }
1530
1531 return mInCallCameraManager;
1532 }
1533 }
1534
1535 /**
1536 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1537 *
1538 * @param orientation The screen orientation of the device (one of: {@link
1539 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1540 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1541 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1542 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1543 */
1544 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001545 LogUtil.d(
1546 "InCallPresenter.onDeviceOrientationChange",
1547 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001548
1549 if (mCallList != null) {
1550 mCallList.notifyCallsOfDeviceRotation(orientation);
1551 } else {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001552 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001553 }
1554
1555 // Notify listeners of device orientation changed.
1556 for (InCallOrientationListener listener : mOrientationListeners) {
1557 listener.onDeviceOrientationChanged(orientation);
1558 }
1559 }
1560
1561 /**
1562 * Configures the in-call UI activity so it can change orientations or not. Enables the
1563 * orientation event listener if allowOrientationChange is true, disables it if false.
1564 *
1565 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1566 * landscape. {@code false} if the in-call UI should be locked in portrait.
1567 */
1568 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
1569 if (mInCallActivity == null) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001570 LogUtil.e(
1571 "InCallPresenter.setInCallAllowsOrientationChange",
1572 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001573 return;
1574 }
1575 mInCallActivity.setAllowOrientationChange(allowOrientationChange);
1576 }
1577
1578 public void enableScreenTimeout(boolean enable) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001579 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
Eric Erfanianccca3152017-02-22 16:32:36 -08001580 if (mInCallActivity == null) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001581 LogUtil.e("InCallPresenter.enableScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001582 return;
1583 }
1584
1585 final Window window = mInCallActivity.getWindow();
1586 if (enable) {
1587 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1588 } else {
1589 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1590 }
1591 }
1592
1593 /**
1594 * Hides or shows the conference manager fragment.
1595 *
1596 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1597 * be hidden.
1598 */
1599 public void showConferenceCallManager(boolean show) {
1600 if (mInCallActivity != null) {
1601 mInCallActivity.showConferenceFragment(show);
1602 }
1603 if (!show && mManageConferenceActivity != null) {
1604 mManageConferenceActivity.finish();
1605 }
1606 }
1607
1608 /**
1609 * Determines if the dialpad is visible.
1610 *
1611 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1612 */
1613 public boolean isDialpadVisible() {
1614 if (mInCallActivity == null) {
1615 return false;
1616 }
1617 return mInCallActivity.isDialpadVisible();
1618 }
1619
1620 public ThemeColorManager getThemeColorManager() {
1621 return mThemeColorManager;
1622 }
1623
1624 /** Called when the foreground call changes. */
1625 public void onForegroundCallChanged(DialerCall newForegroundCall) {
1626 mThemeColorManager.onForegroundCallChanged(mContext, newForegroundCall);
1627 if (mInCallActivity != null) {
1628 mInCallActivity.onForegroundCallChanged(newForegroundCall);
1629 }
1630 }
1631
1632 public InCallActivity getActivity() {
1633 return mInCallActivity;
1634 }
1635
1636 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1637 public void setActivity(InCallActivity inCallActivity) {
1638 if (inCallActivity == null) {
1639 throw new IllegalArgumentException("registerActivity cannot be called with null");
1640 }
1641 if (mInCallActivity != null && mInCallActivity != inCallActivity) {
Eric Erfaniand538e0b2017-06-30 15:26:17 -07001642 LogUtil.w(
1643 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001644 }
1645 updateActivity(inCallActivity);
1646 }
1647
1648 ExternalCallNotifier getExternalCallNotifier() {
1649 return mExternalCallNotifier;
1650 }
1651
1652 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
1653 if (mLocalVideoSurfaceTexture == null) {
1654 mLocalVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture();
1655 }
1656 return mLocalVideoSurfaceTexture;
1657 }
1658
1659 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
1660 if (mRemoteVideoSurfaceTexture == null) {
1661 mRemoteVideoSurfaceTexture = VideoSurfaceBindings.createRemoteVideoSurfaceTexture();
1662 }
1663 return mRemoteVideoSurfaceTexture;
1664 }
1665
1666 void cleanupSurfaces() {
1667 if (mRemoteVideoSurfaceTexture != null) {
1668 mRemoteVideoSurfaceTexture.setDoneWithSurface();
1669 mRemoteVideoSurfaceTexture = null;
1670 }
1671 if (mLocalVideoSurfaceTexture != null) {
1672 mLocalVideoSurfaceTexture.setDoneWithSurface();
1673 mLocalVideoSurfaceTexture = null;
1674 }
1675 }
1676
1677 /** All the main states of InCallActivity. */
1678 public enum InCallState {
1679 // InCall Screen is off and there are no calls
1680 NO_CALLS,
1681
1682 // Incoming-call screen is up
1683 INCOMING,
1684
1685 // In-call experience is showing
1686 INCALL,
1687
1688 // Waiting for user input before placing outgoing call
1689 WAITING_FOR_ACCOUNT,
1690
1691 // UI is starting up but no call has been initiated yet.
1692 // The UI is waiting for Telecom to respond.
1693 PENDING_OUTGOING,
1694
1695 // User is dialing out
1696 OUTGOING;
1697
1698 public boolean isIncoming() {
1699 return (this == INCOMING);
1700 }
1701
1702 public boolean isConnectingOrConnected() {
1703 return (this == INCOMING || this == OUTGOING || this == INCALL);
1704 }
1705 }
1706
1707 /** Interface implemented by classes that need to know about the InCall State. */
1708 public interface InCallStateListener {
1709
1710 // TODO: Enhance state to contain the call objects instead of passing CallList
1711 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1712 }
1713
1714 public interface IncomingCallListener {
1715
1716 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1717 }
1718
1719 public interface CanAddCallListener {
1720
1721 void onCanAddCallChanged(boolean canAddCall);
1722 }
1723
1724 public interface InCallDetailsListener {
1725
1726 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1727 }
1728
1729 public interface InCallOrientationListener {
1730
1731 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1732 }
1733
1734 /**
1735 * Interface implemented by classes that need to know about events which occur within the In-Call
1736 * UI. Used as a means of communicating between fragments that make up the UI.
1737 */
1738 public interface InCallEventListener {
1739
1740 void onFullscreenModeChanged(boolean isFullscreenMode);
1741 }
1742
1743 public interface InCallUiListener {
1744
1745 void onUiShowing(boolean showing);
1746 }
1747}