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