blob: 1ba3b5d3a408a70530a8f3853b7d5700a3bd6ba5 [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.incallui;
18
19import android.content.Context;
20import android.content.Intent;
21import android.graphics.Point;
22import android.os.Bundle;
23import android.os.Handler;
Eric Erfanian2ca43182017-08-31 06:57:16 -070024import android.os.Trace;
twyen8efb4952017-10-06 16:35:54 -070025import android.support.annotation.MainThread;
Eric Erfanianccca3152017-02-22 16:32:36 -080026import android.support.annotation.NonNull;
27import android.support.annotation.Nullable;
28import android.support.annotation.VisibleForTesting;
Eric Erfanian83b20212017-05-31 08:53:10 -070029import android.support.v4.os.UserManagerCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080030import android.telecom.Call.Details;
31import android.telecom.DisconnectCause;
32import android.telecom.PhoneAccount;
33import android.telecom.PhoneAccountHandle;
34import android.telecom.TelecomManager;
35import android.telecom.VideoProfile;
36import android.telephony.PhoneStateListener;
37import android.telephony.TelephonyManager;
twyen8efb4952017-10-06 16:35:54 -070038import android.util.ArraySet;
Eric Erfanianccca3152017-02-22 16:32:36 -080039import android.view.Window;
40import android.view.WindowManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080041import com.android.contacts.common.compat.CallCompat;
42import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler;
43import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler.OnCheckBlockedListener;
Eric Erfanian83b20212017-05-31 08:53:10 -070044import com.android.dialer.blocking.FilteredNumberCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080045import com.android.dialer.blocking.FilteredNumbersUtil;
twyen8efb4952017-10-06 16:35:54 -070046import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080047import com.android.dialer.common.LogUtil;
Eric Erfanian2ca43182017-08-31 06:57:16 -070048import com.android.dialer.common.concurrent.DefaultDialerExecutorFactory;
Eric Erfaniand8046e52017-04-06 09:41:50 -070049import com.android.dialer.enrichedcall.EnrichedCallComponent;
Eric Erfanian10b34a52017-05-04 08:23:17 -070050import com.android.dialer.location.GeoUtil;
Eric Erfanian8369df02017-05-03 10:27:13 -070051import com.android.dialer.logging.InteractionEvent;
Eric Erfanianccca3152017-02-22 16:32:36 -080052import com.android.dialer.logging.Logger;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070053import com.android.dialer.postcall.PostCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080054import com.android.dialer.telecom.TelecomUtil;
55import com.android.dialer.util.TouchPointManager;
56import com.android.incallui.InCallOrientationEventListener.ScreenOrientation;
57import com.android.incallui.answerproximitysensor.PseudoScreenState;
58import com.android.incallui.call.CallList;
59import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080060import com.android.incallui.call.ExternalCallList;
Eric Erfanianccca3152017-02-22 16:32:36 -080061import com.android.incallui.call.TelecomAdapter;
Eric Erfanian2ca43182017-08-31 06:57:16 -070062import com.android.incallui.disconnectdialog.DisconnectMessage;
twyen8efb4952017-10-06 16:35:54 -070063import com.android.incallui.incalluilock.InCallUiLock;
Eric Erfanianccca3152017-02-22 16:32:36 -080064import com.android.incallui.latencyreport.LatencyReport;
65import com.android.incallui.legacyblocking.BlockedNumberContentObserver;
66import com.android.incallui.spam.SpamCallListListener;
67import com.android.incallui.util.TelecomCallUtil;
68import com.android.incallui.videosurface.bindings.VideoSurfaceBindings;
69import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
Eric Erfanian90508232017-03-24 09:31:16 -070070import com.android.incallui.videotech.utils.VideoUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080071import java.util.Collections;
72import java.util.List;
73import java.util.Objects;
74import java.util.Set;
75import java.util.concurrent.ConcurrentHashMap;
76import java.util.concurrent.CopyOnWriteArrayList;
77import java.util.concurrent.atomic.AtomicBoolean;
78
79/**
80 * Takes updates from the CallList and notifies the InCallActivity (UI) of the changes. Responsible
81 * for starting the activity for a new call and finishing the activity when all calls are
82 * disconnected. Creates and manages the in-call state and provides a listener pattern for the
83 * presenters that want to listen in on the in-call state changes. TODO: This class has become more
84 * of a state machine at this point. Consider renaming.
85 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -070086public class InCallPresenter implements CallList.Listener {
Eric Erfanian2ca43182017-08-31 06:57:16 -070087 private static final String PIXEL2017_SYSTEM_FEATURE =
88 "com.google.android.feature.PIXEL_2017_EXPERIENCE";
Eric Erfanianccca3152017-02-22 16:32:36 -080089 private static final String EXTRA_FIRST_TIME_SHOWN =
90 "com.android.incallui.intent.extra.FIRST_TIME_SHOWN";
91
92 private static final long BLOCK_QUERY_TIMEOUT_MS = 1000;
93
94 private static final Bundle EMPTY_EXTRAS = new Bundle();
95
96 private static InCallPresenter sInCallPresenter;
97
98 /**
99 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is load factor before
100 * resizing, 1 means we only expect a single thread to access the map so make only a single shard
101 */
102 private final Set<InCallStateListener> mListeners =
103 Collections.newSetFromMap(new ConcurrentHashMap<InCallStateListener, Boolean>(8, 0.9f, 1));
104
105 private final List<IncomingCallListener> mIncomingCallListeners = new CopyOnWriteArrayList<>();
106 private final Set<InCallDetailsListener> mDetailsListeners =
107 Collections.newSetFromMap(new ConcurrentHashMap<InCallDetailsListener, Boolean>(8, 0.9f, 1));
108 private final Set<CanAddCallListener> mCanAddCallListeners =
109 Collections.newSetFromMap(new ConcurrentHashMap<CanAddCallListener, Boolean>(8, 0.9f, 1));
110 private final Set<InCallUiListener> mInCallUiListeners =
111 Collections.newSetFromMap(new ConcurrentHashMap<InCallUiListener, Boolean>(8, 0.9f, 1));
112 private final Set<InCallOrientationListener> mOrientationListeners =
113 Collections.newSetFromMap(
114 new ConcurrentHashMap<InCallOrientationListener, Boolean>(8, 0.9f, 1));
115 private final Set<InCallEventListener> mInCallEventListeners =
116 Collections.newSetFromMap(new ConcurrentHashMap<InCallEventListener, Boolean>(8, 0.9f, 1));
117
118 private StatusBarNotifier mStatusBarNotifier;
119 private ExternalCallNotifier mExternalCallNotifier;
120 private ContactInfoCache mContactInfoCache;
121 private Context mContext;
122 private final OnCheckBlockedListener mOnCheckBlockedListener =
123 new OnCheckBlockedListener() {
124 @Override
125 public void onCheckComplete(final Integer id) {
126 if (id != null && id != FilteredNumberAsyncQueryHandler.INVALID_ID) {
127 // Silence the ringer now to prevent ringing and vibration before the call is
128 // terminated when Telecom attempts to add it.
129 TelecomUtil.silenceRinger(mContext);
130 }
131 }
132 };
133 private CallList mCallList;
134 private ExternalCallList mExternalCallList;
135 private InCallActivity mInCallActivity;
136 private ManageConferenceActivity mManageConferenceActivity;
137 private final android.telecom.Call.Callback mCallCallback =
138 new android.telecom.Call.Callback() {
139 @Override
140 public void onPostDialWait(
141 android.telecom.Call telecomCall, String remainingPostDialSequence) {
142 final DialerCall call = mCallList.getDialerCallFromTelecomCall(telecomCall);
143 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700144 LogUtil.w(
145 "InCallPresenter.onPostDialWait",
146 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800147 return;
148 }
149 onPostDialCharWait(call.getId(), remainingPostDialSequence);
150 }
151
152 @Override
153 public void onDetailsChanged(
154 android.telecom.Call telecomCall, android.telecom.Call.Details details) {
155 final DialerCall call = mCallList.getDialerCallFromTelecomCall(telecomCall);
156 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700157 LogUtil.w(
158 "InCallPresenter.onDetailsChanged",
159 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800160 return;
161 }
162
163 if (details.hasProperty(Details.PROPERTY_IS_EXTERNAL_CALL)
164 && !mExternalCallList.isCallTracked(telecomCall)) {
165
166 // A regular call became an external call so swap call lists.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700167 LogUtil.i("InCallPresenter.onDetailsChanged", "Call became external: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800168 mCallList.onInternalCallMadeExternal(mContext, telecomCall);
169 mExternalCallList.onCallAdded(telecomCall);
170 return;
171 }
172
173 for (InCallDetailsListener listener : mDetailsListeners) {
174 listener.onDetailsChanged(call, details);
175 }
176 }
177
178 @Override
179 public void onConferenceableCallsChanged(
180 android.telecom.Call telecomCall, List<android.telecom.Call> conferenceableCalls) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700181 LogUtil.i(
182 "InCallPresenter.onConferenceableCallsChanged",
183 "onConferenceableCallsChanged: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800184 onDetailsChanged(telecomCall, telecomCall.getDetails());
185 }
186 };
187 private InCallState mInCallState = InCallState.NO_CALLS;
188 private ProximitySensor mProximitySensor;
189 private final PseudoScreenState mPseudoScreenState = new PseudoScreenState();
190 private boolean mServiceConnected;
Eric Erfanianccca3152017-02-22 16:32:36 -0800191 private InCallCameraManager mInCallCameraManager;
192 private FilteredNumberAsyncQueryHandler mFilteredQueryHandler;
193 private CallList.Listener mSpamCallListListener;
194 /** Whether or not we are currently bound and waiting for Telecom to send us a new call. */
195 private boolean mBoundAndWaitingForOutgoingCall;
196 /** Determines if the InCall UI is in fullscreen mode or not. */
197 private boolean mIsFullScreen = false;
198
Eric Erfanian2ca43182017-08-31 06:57:16 -0700199 private boolean mScreenTimeoutEnabled = true;
200
Eric Erfanianccca3152017-02-22 16:32:36 -0800201 private PhoneStateListener mPhoneStateListener =
202 new PhoneStateListener() {
203 @Override
204 public void onCallStateChanged(int state, String incomingNumber) {
205 if (state == TelephonyManager.CALL_STATE_RINGING) {
206 if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
207 return;
208 }
209 // Check if the number is blocked, to silence the ringer.
210 String countryIso = GeoUtil.getCurrentCountryIso(mContext);
211 mFilteredQueryHandler.isBlockedNumber(
212 mOnCheckBlockedListener, incomingNumber, countryIso);
213 }
214 }
215 };
216 /**
217 * Is true when the activity has been previously started. Some code needs to know not just if the
218 * activity is currently up, but if it had been previously shown in foreground for this in-call
219 * session (e.g., StatusBarNotifier). This gets reset when the session ends in the tear-down
220 * method.
221 */
222 private boolean mIsActivityPreviouslyStarted = false;
223
224 /** Whether or not InCallService is bound to Telecom. */
225 private boolean mServiceBound = false;
226
227 /**
228 * When configuration changes Android kills the current activity and starts a new one. The flag is
229 * used to check if full clean up is necessary (activity is stopped and new activity won't be
230 * started), or if a new activity will be started right after the current one is destroyed, and
231 * therefore no need in release all resources.
232 */
233 private boolean mIsChangingConfigurations = false;
234
235 private boolean mAwaitingCallListUpdate = false;
236
237 private ExternalCallList.ExternalCallListener mExternalCallListener =
238 new ExternalCallList.ExternalCallListener() {
239
240 @Override
241 public void onExternalCallPulled(android.telecom.Call call) {
242 // Note: keep this code in sync with InCallPresenter#onCallAdded
243 LatencyReport latencyReport = new LatencyReport(call);
244 latencyReport.onCallBlockingDone();
245 // Note: External calls do not require spam checking.
246 mCallList.onCallAdded(mContext, call, latencyReport);
247 call.registerCallback(mCallCallback);
248 }
249
250 @Override
251 public void onExternalCallAdded(android.telecom.Call call) {
252 // No-op
253 }
254
255 @Override
256 public void onExternalCallRemoved(android.telecom.Call call) {
257 // No-op
258 }
259
260 @Override
261 public void onExternalCallUpdated(android.telecom.Call call) {
262 // No-op
263 }
264 };
265
266 private ThemeColorManager mThemeColorManager;
267 private VideoSurfaceTexture mLocalVideoSurfaceTexture;
268 private VideoSurfaceTexture mRemoteVideoSurfaceTexture;
269
Eric Erfanian10b34a52017-05-04 08:23:17 -0700270 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800271 @VisibleForTesting
272 InCallPresenter() {}
273
274 public static synchronized InCallPresenter getInstance() {
275 if (sInCallPresenter == null) {
wangqic8cf79e2017-10-17 09:21:00 -0700276 Trace.beginSection("InCallPresenter.Constructor");
Eric Erfanianccca3152017-02-22 16:32:36 -0800277 sInCallPresenter = new InCallPresenter();
wangqic8cf79e2017-10-17 09:21:00 -0700278 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800279 }
280 return sInCallPresenter;
281 }
282
Eric Erfanian10b34a52017-05-04 08:23:17 -0700283 @VisibleForTesting
284 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
285 sInCallPresenter = inCallPresenter;
286 }
287
Eric Erfanianccca3152017-02-22 16:32:36 -0800288 /**
289 * Determines whether or not a call has no valid phone accounts that can be used to make the call
290 * with. Emergency calls do not require a phone account.
291 *
292 * @param call to check accounts for.
293 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
294 * call contains a phone account that could be used to initiate it with, or is an emergency
295 * call.
296 */
297 public static boolean isCallWithNoValidAccounts(DialerCall call) {
298 if (call != null && !call.isEmergencyCall()) {
299 Bundle extras = call.getIntentExtras();
300
301 if (extras == null) {
302 extras = EMPTY_EXTRAS;
303 }
304
305 final List<PhoneAccountHandle> phoneAccountHandles =
306 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
307
308 if ((call.getAccountHandle() == null
309 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700310 LogUtil.i(
311 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800312 return true;
313 }
314 }
315 return false;
316 }
317
318 public InCallState getInCallState() {
319 return mInCallState;
320 }
321
322 public CallList getCallList() {
323 return mCallList;
324 }
325
326 public void setUp(
327 @NonNull Context context,
328 CallList callList,
329 ExternalCallList externalCallList,
330 StatusBarNotifier statusBarNotifier,
331 ExternalCallNotifier externalCallNotifier,
332 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700333 ProximitySensor proximitySensor,
334 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700335 Trace.beginSection("InCallPresenter.setUp");
Eric Erfanianccca3152017-02-22 16:32:36 -0800336 if (mServiceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700337 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800338 if (context != mContext || callList != mCallList) {
339 throw new IllegalStateException();
340 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700341 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800342 return;
343 }
344
345 Objects.requireNonNull(context);
346 mContext = context;
347
348 mContactInfoCache = contactInfoCache;
349
350 mStatusBarNotifier = statusBarNotifier;
351 mExternalCallNotifier = externalCallNotifier;
352 addListener(mStatusBarNotifier);
Eric Erfaniand8046e52017-04-06 09:41:50 -0700353 EnrichedCallComponent.get(mContext)
354 .getEnrichedCallManager()
355 .registerStateChangedListener(mStatusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800356
357 mProximitySensor = proximitySensor;
358 addListener(mProximitySensor);
359
360 mThemeColorManager =
361 new ThemeColorManager(new InCallUIMaterialColorMapUtils(mContext.getResources()));
362
363 mCallList = callList;
364 mExternalCallList = externalCallList;
365 externalCallList.addExternalCallListener(mExternalCallNotifier);
366 externalCallList.addExternalCallListener(mExternalCallListener);
367
368 // This only gets called by the service so this is okay.
369 mServiceConnected = true;
370
371 // The final thing we do in this set up is add ourselves as a listener to CallList. This
372 // will kick off an update and the whole process can start.
373 mCallList.addListener(this);
374
375 // Create spam call list listener and add it to the list of listeners
Eric Erfanian2ca43182017-08-31 06:57:16 -0700376 mSpamCallListListener = new SpamCallListListener(context, new DefaultDialerExecutorFactory());
Eric Erfanianccca3152017-02-22 16:32:36 -0800377 mCallList.addListener(mSpamCallListListener);
378
379 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800380
Eric Erfanian83b20212017-05-31 08:53:10 -0700381 mFilteredQueryHandler = filteredNumberQueryHandler;
Eric Erfanianccca3152017-02-22 16:32:36 -0800382 mContext
383 .getSystemService(TelephonyManager.class)
384 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
385
Eric Erfanian2ca43182017-08-31 06:57:16 -0700386 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
387 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800388 }
389
390 /**
391 * Called when the telephony service has disconnected from us. This will happen when there are no
392 * more active calls. However, we may still want to continue showing the UI for certain cases like
393 * showing "Call Ended". What we really want is to wait for the activity and the service to both
394 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
395 * secondary method that performs the aforementioned logic.
396 */
397 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700398 LogUtil.d("InCallPresenter.tearDown", "tearDown");
Eric Erfanianccca3152017-02-22 16:32:36 -0800399 mCallList.clearOnDisconnect();
400
401 mServiceConnected = false;
402
403 mContext
404 .getSystemService(TelephonyManager.class)
405 .listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
406
407 attemptCleanup();
408 VideoPauseController.getInstance().tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -0800409 }
410
411 private void attemptFinishActivity() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700412 mScreenTimeoutEnabled = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800413 final boolean doFinish = (mInCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700414 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800415 if (doFinish) {
416 mInCallActivity.setExcludeFromRecents(true);
417 mInCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800418 }
419 }
420
421 /**
422 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
423 * for more insight on the tear-down process.
424 */
425 public void unsetActivity(InCallActivity inCallActivity) {
426 if (inCallActivity == null) {
427 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
428 }
429 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700430 LogUtil.i(
431 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800432 return;
433 }
434 if (mInCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700435 LogUtil.w(
436 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800437 "Second instance of InCallActivity is trying to unregister when another"
438 + " instance is active. Ignoring.");
439 return;
440 }
441 updateActivity(null);
442 }
443
444 /**
445 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
446 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
447 */
448 private void updateActivity(InCallActivity inCallActivity) {
wangqi9982f0d2017-10-11 17:46:07 -0700449 Trace.beginSection("InCallPresenter.updateActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800450 boolean updateListeners = false;
451 boolean doAttemptCleanup = false;
452
453 if (inCallActivity != null) {
454 if (mInCallActivity == null) {
455 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700456 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800457 } else {
458 // since setActivity is called onStart(), it can be called multiple times.
459 // This is fine and ignorable, but we do not want to update the world every time
460 // this happens (like going to/from background) so we do not set updateListeners.
461 }
462
463 mInCallActivity = inCallActivity;
464 mInCallActivity.setExcludeFromRecents(false);
465
466 // By the time the UI finally comes up, the call may already be disconnected.
467 // If that's the case, we may need to show an error dialog.
468 if (mCallList != null && mCallList.getDisconnectedCall() != null) {
469 maybeShowErrorDialogOnDisconnect(mCallList.getDisconnectedCall());
470 }
471
472 // When the UI comes up, we need to first check the in-call state.
473 // If we are showing NO_CALLS, that means that a call probably connected and
474 // then immediately disconnected before the UI was able to come up.
475 // If we dont have any calls, start tearing down the UI instead.
476 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
477 // it has been set.
478 if (mInCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700479 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800480 attemptFinishActivity();
wangqi9982f0d2017-10-11 17:46:07 -0700481 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800482 return;
483 }
484 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700485 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800486 updateListeners = true;
487 mInCallActivity = null;
488
489 // We attempt cleanup for the destroy case but only after we recalculate the state
490 // to see if we need to come back up or stay shut down. This is why we do the
491 // cleanup after the call to onCallListChange() instead of directly here.
492 doAttemptCleanup = true;
493 }
494
495 // Messages can come from the telephony layer while the activity is coming up
496 // and while the activity is going down. So in both cases we need to recalculate what
497 // state we should be in after they complete.
498 // Examples: (1) A new incoming call could come in and then get disconnected before
499 // the activity is created.
500 // (2) All calls could disconnect and then get a new incoming call before the
501 // activity is destroyed.
502 //
503 // b/1122139 - We previously had a check for mServiceConnected here as well, but there are
504 // cases where we need to recalculate the current state even if the service in not
505 // connected. In particular the case where startOrFinish() is called while the app is
506 // already finish()ing. In that case, we skip updating the state with the knowledge that
507 // we will check again once the activity has finished. That means we have to recalculate the
508 // state here even if the service is disconnected since we may not have finished a state
509 // transition while finish()ing.
510 if (updateListeners) {
511 onCallListChange(mCallList);
512 }
513
514 if (doAttemptCleanup) {
515 attemptCleanup();
516 }
wangqi9982f0d2017-10-11 17:46:07 -0700517 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800518 }
519
520 public void setManageConferenceActivity(
521 @Nullable ManageConferenceActivity manageConferenceActivity) {
522 mManageConferenceActivity = manageConferenceActivity;
523 }
524
525 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700526 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800527 bringToForeground(showDialpad);
528 }
529
530 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700531 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800532 LatencyReport latencyReport = new LatencyReport(call);
533 if (shouldAttemptBlocking(call)) {
534 maybeBlockCall(call, latencyReport);
535 } else {
536 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
537 mExternalCallList.onCallAdded(call);
538 } else {
539 latencyReport.onCallBlockingDone();
540 mCallList.onCallAdded(mContext, call, latencyReport);
541 }
542 }
543
544 // Since a call has been added we are no longer waiting for Telecom to send us a call.
545 setBoundAndWaitingForOutgoingCall(false, null);
546 call.registerCallback(mCallCallback);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700547 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800548 }
549
550 private boolean shouldAttemptBlocking(android.telecom.Call call) {
551 if (call.getState() != android.telecom.Call.STATE_RINGING) {
552 return false;
553 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700554 if (!UserManagerCompat.isUserUnlocked(mContext)) {
555 LogUtil.i(
556 "InCallPresenter.shouldAttemptBlocking",
557 "not attempting to block incoming call because user is locked");
558 return false;
559 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800560 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700561 LogUtil.i(
562 "InCallPresenter.shouldAttemptBlocking",
563 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800564 return false;
565 }
566 if (FilteredNumbersUtil.hasRecentEmergencyCall(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700567 LogUtil.i(
568 "InCallPresenter.shouldAttemptBlocking",
569 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800570 return false;
571 }
572 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
573 return false;
574 }
Eric Erfanian83b20212017-05-31 08:53:10 -0700575 if (FilteredNumberCompat.useNewFiltering(mContext)) {
576 LogUtil.i(
577 "InCallPresenter.shouldAttemptBlocking",
578 "not attempting to block incoming call because framework blocking is in use");
579 return false;
580 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800581 return true;
582 }
583
584 /**
585 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
586 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
587 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
588 * call anyways.
589 */
590 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
591 final String countryIso = GeoUtil.getCurrentCountryIso(mContext);
592 final String number = TelecomCallUtil.getNumber(call);
593 final long timeAdded = System.currentTimeMillis();
594
595 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
596 // main UI thread. It is needed so we can change its value within different scopes, since
597 // that cannot be done with a final boolean.
598 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
599
600 final Handler handler = new Handler();
601
602 // Proceed if the query is slow; the call may still be blocked after the query returns.
603 final Runnable runnable =
604 new Runnable() {
605 @Override
606 public void run() {
607 hasTimedOut.set(true);
608 latencyReport.onCallBlockingDone();
609 mCallList.onCallAdded(mContext, call, latencyReport);
610 }
611 };
612 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
613
614 OnCheckBlockedListener onCheckBlockedListener =
615 new OnCheckBlockedListener() {
616 @Override
617 public void onCheckComplete(final Integer id) {
618 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700619 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800620 return;
621 }
622 if (!hasTimedOut.get()) {
623 handler.removeCallbacks(runnable);
624 }
625 if (id == null) {
626 if (!hasTimedOut.get()) {
627 latencyReport.onCallBlockingDone();
628 mCallList.onCallAdded(mContext, call, latencyReport);
629 }
630 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700631 LogUtil.d(
632 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800633 if (!hasTimedOut.get()) {
634 handler.removeCallbacks(runnable);
635
636 latencyReport.onCallBlockingDone();
637 mCallList.onCallAdded(mContext, call, latencyReport);
638 }
639 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700640 LogUtil.i(
641 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800642 call.reject(false, null);
643 Logger.get(mContext).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
644
645 /*
646 * If mContext is null, then the InCallPresenter was torn down before the
647 * block check had a chance to complete. The context is no longer valid, so
648 * don't attempt to remove the call log entry.
649 */
650 if (mContext == null) {
651 return;
652 }
653 // Register observer to update the call log.
654 // BlockedNumberContentObserver will unregister after successful log or timeout.
655 BlockedNumberContentObserver contentObserver =
656 new BlockedNumberContentObserver(mContext, new Handler(), number, timeAdded);
657 contentObserver.register();
658 }
659 }
660 };
661
662 mFilteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
663 }
664
665 public void onCallRemoved(android.telecom.Call call) {
666 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
667 mExternalCallList.onCallRemoved(call);
668 } else {
669 mCallList.onCallRemoved(mContext, call);
670 call.unregisterCallback(mCallCallback);
671 }
672 }
673
674 public void onCanAddCallChanged(boolean canAddCall) {
675 for (CanAddCallListener listener : mCanAddCallListeners) {
676 listener.onCanAddCallChanged(canAddCall);
677 }
678 }
679
680 @Override
681 public void onWiFiToLteHandover(DialerCall call) {
682 if (mInCallActivity != null) {
683 mInCallActivity.onWiFiToLteHandover(call);
684 }
685 }
686
687 @Override
688 public void onHandoverToWifiFailed(DialerCall call) {
689 if (mInCallActivity != null) {
690 mInCallActivity.onHandoverToWifiFailed(call);
691 }
692 }
693
Eric Erfanianc857f902017-05-15 14:05:33 -0700694 @Override
695 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
696 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
697 if (mInCallActivity != null) {
698 mInCallActivity.onInternationalCallOnWifi(call);
699 }
700 }
701
Eric Erfanianccca3152017-02-22 16:32:36 -0800702 /**
703 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
704 * app based on the information it gets from CallList. Dispatches the in-call state to all
705 * listeners. Can trigger the creation or destruction of the UI based on the states that is
706 * calculates.
707 */
708 @Override
709 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700710 Trace.beginSection("InCallPresenter.onCallListChange");
Eric Erfanianccca3152017-02-22 16:32:36 -0800711 if (mInCallActivity != null && mInCallActivity.isInCallScreenAnimating()) {
712 mAwaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700713 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800714 return;
715 }
716 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700717 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800718 return;
719 }
720
721 mAwaitingCallListUpdate = false;
722
723 InCallState newState = getPotentialStateFromCallList(callList);
724 InCallState oldState = mInCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700725 LogUtil.d(
726 "InCallPresenter.onCallListChange",
727 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700728
729 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
730 // incall activity for that call will still exist (even if it's not visible). In the case of
731 // an incoming call in that situation, just disconnect that "waiting for account" call and
732 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
733 // b/33247755 for more details.
734 DialerCall waitingForAccountCall;
735 if (newState == InCallState.INCOMING
736 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
737 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700738 // The InCallActivity might be destroyed or not started yet at this point.
739 if (isActivityStarted()) {
740 mInCallActivity.dismissPendingDialogs();
741 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700742 }
743
Eric Erfanianccca3152017-02-22 16:32:36 -0800744 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700745 LogUtil.d(
746 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800747
748 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700749 LogUtil.i(
750 "InCallPresenter.onCallListChange",
751 "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800752 mInCallState = newState;
753
754 // notify listeners of new state
755 for (InCallStateListener listener : mListeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700756 LogUtil.d(
757 "InCallPresenter.onCallListChange",
758 "Notify " + listener + " of state " + mInCallState.toString());
Eric Erfanianccca3152017-02-22 16:32:36 -0800759 listener.onStateChange(oldState, mInCallState, callList);
760 }
761
762 if (isActivityStarted()) {
763 final boolean hasCall =
764 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
765 mInCallActivity.dismissKeyguard(hasCall);
766 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700767 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800768 }
769
770 /** Called when there is a new incoming call. */
771 @Override
772 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700773 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800774 InCallState newState = startOrFinishUi(InCallState.INCOMING);
775 InCallState oldState = mInCallState;
776
Eric Erfanian2ca43182017-08-31 06:57:16 -0700777 LogUtil.i(
778 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800779 mInCallState = newState;
780
wangqicf61ca02017-08-31 15:32:55 -0700781 Trace.beginSection("listener.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800782 for (IncomingCallListener listener : mIncomingCallListeners) {
783 listener.onIncomingCall(oldState, mInCallState, call);
784 }
wangqicf61ca02017-08-31 15:32:55 -0700785 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800786
wangqicf61ca02017-08-31 15:32:55 -0700787 Trace.beginSection("onPrimaryCallStateChanged");
Eric Erfanianccca3152017-02-22 16:32:36 -0800788 if (mInCallActivity != null) {
789 // Re-evaluate which fragment is being shown.
790 mInCallActivity.onPrimaryCallStateChanged();
791 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700792 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700793 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800794 }
795
796 @Override
797 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700798 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
Eric Erfanianccca3152017-02-22 16:32:36 -0800799 && mInCallState == InCallPresenter.InCallState.INCOMING) {
800 LogUtil.i(
801 "InCallPresenter.onUpgradeToVideo",
802 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700803 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800804 }
805
806 if (mInCallActivity != null) {
807 // Re-evaluate which fragment is being shown.
808 mInCallActivity.onPrimaryCallStateChanged();
809 }
810 }
811
812 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700813 public void onSessionModificationStateChange(DialerCall call) {
814 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800815 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
816 if (mProximitySensor == null) {
817 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
818 return;
819 }
820 mProximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700821 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
Eric Erfanianccca3152017-02-22 16:32:36 -0800822 if (mInCallActivity != null) {
823 // Re-evaluate which fragment is being shown.
824 mInCallActivity.onPrimaryCallStateChanged();
825 }
826 }
827
828 /**
829 * Called when a call becomes disconnected. Called everytime an existing call changes from being
830 * connected (incoming/outgoing/active) to disconnected.
831 */
832 @Override
833 public void onDisconnect(DialerCall call) {
834 maybeShowErrorDialogOnDisconnect(call);
835
836 // We need to do the run the same code as onCallListChange.
837 onCallListChange(mCallList);
838
839 if (isActivityStarted()) {
840 mInCallActivity.dismissKeyguard(false);
841 }
842
843 if (call.isEmergencyCall()) {
844 FilteredNumbersUtil.recordLastEmergencyCallTime(mContext);
845 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800846
Eric Erfanianfc37b022017-03-21 10:11:17 -0700847 if (!mCallList.hasLiveCall()
848 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700849 && !isSecretCode(call.getNumber())
wangqi9982f0d2017-10-11 17:46:07 -0700850 && !call.isVoiceMailNumber()) {
Eric Erfanian10b34a52017-05-04 08:23:17 -0700851 PostCall.onCallDisconnected(mContext, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -0800852 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800853 }
854
Eric Erfanian10b34a52017-05-04 08:23:17 -0700855 private boolean isSecretCode(@Nullable String number) {
856 return number != null
857 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
858 }
859
Eric Erfanianccca3152017-02-22 16:32:36 -0800860 /** Given the call list, return the state in which the in-call screen should be. */
861 public InCallState getPotentialStateFromCallList(CallList callList) {
862
863 InCallState newState = InCallState.NO_CALLS;
864
865 if (callList == null) {
866 return newState;
867 }
868 if (callList.getIncomingCall() != null) {
869 newState = InCallState.INCOMING;
870 } else if (callList.getWaitingForAccountCall() != null) {
871 newState = InCallState.WAITING_FOR_ACCOUNT;
872 } else if (callList.getPendingOutgoingCall() != null) {
873 newState = InCallState.PENDING_OUTGOING;
874 } else if (callList.getOutgoingCall() != null) {
875 newState = InCallState.OUTGOING;
876 } else if (callList.getActiveCall() != null
877 || callList.getBackgroundCall() != null
878 || callList.getDisconnectedCall() != null
879 || callList.getDisconnectingCall() != null) {
880 newState = InCallState.INCALL;
881 }
882
883 if (newState == InCallState.NO_CALLS) {
884 if (mBoundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700885 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800886 }
887 }
888
889 return newState;
890 }
891
892 public boolean isBoundAndWaitingForOutgoingCall() {
893 return mBoundAndWaitingForOutgoingCall;
894 }
895
896 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700897 LogUtil.i(
898 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
899 "setBoundAndWaitingForOutgoingCall: " + isBound);
Eric Erfanianccca3152017-02-22 16:32:36 -0800900 mBoundAndWaitingForOutgoingCall = isBound;
901 mThemeColorManager.setPendingPhoneAccountHandle(handle);
902 if (isBound && mInCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700903 mInCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800904 }
905 }
906
907 public void onShrinkAnimationComplete() {
908 if (mAwaitingCallListUpdate) {
909 onCallListChange(mCallList);
910 }
911 }
912
913 public void addIncomingCallListener(IncomingCallListener listener) {
914 Objects.requireNonNull(listener);
915 mIncomingCallListeners.add(listener);
916 }
917
918 public void removeIncomingCallListener(IncomingCallListener listener) {
919 if (listener != null) {
920 mIncomingCallListeners.remove(listener);
921 }
922 }
923
924 public void addListener(InCallStateListener listener) {
925 Objects.requireNonNull(listener);
926 mListeners.add(listener);
927 }
928
929 public void removeListener(InCallStateListener listener) {
930 if (listener != null) {
931 mListeners.remove(listener);
932 }
933 }
934
935 public void addDetailsListener(InCallDetailsListener listener) {
936 Objects.requireNonNull(listener);
937 mDetailsListeners.add(listener);
938 }
939
940 public void removeDetailsListener(InCallDetailsListener listener) {
941 if (listener != null) {
942 mDetailsListeners.remove(listener);
943 }
944 }
945
946 public void addCanAddCallListener(CanAddCallListener listener) {
947 Objects.requireNonNull(listener);
948 mCanAddCallListeners.add(listener);
949 }
950
951 public void removeCanAddCallListener(CanAddCallListener listener) {
952 if (listener != null) {
953 mCanAddCallListeners.remove(listener);
954 }
955 }
956
957 public void addOrientationListener(InCallOrientationListener listener) {
958 Objects.requireNonNull(listener);
959 mOrientationListeners.add(listener);
960 }
961
962 public void removeOrientationListener(InCallOrientationListener listener) {
963 if (listener != null) {
964 mOrientationListeners.remove(listener);
965 }
966 }
967
968 public void addInCallEventListener(InCallEventListener listener) {
969 Objects.requireNonNull(listener);
970 mInCallEventListeners.add(listener);
971 }
972
973 public void removeInCallEventListener(InCallEventListener listener) {
974 if (listener != null) {
975 mInCallEventListeners.remove(listener);
976 }
977 }
978
979 public ProximitySensor getProximitySensor() {
980 return mProximitySensor;
981 }
982
983 public PseudoScreenState getPseudoScreenState() {
984 return mPseudoScreenState;
985 }
986
987 /** Returns true if the incall app is the foreground application. */
988 public boolean isShowingInCallUi() {
989 if (!isActivityStarted()) {
990 return false;
991 }
992 if (mManageConferenceActivity != null && mManageConferenceActivity.isVisible()) {
993 return true;
994 }
995 return mInCallActivity.isVisible();
996 }
997
998 /**
999 * Returns true if the activity has been created and is running. Returns true as long as activity
1000 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
1001 * (not in foreground).
1002 */
1003 public boolean isActivityStarted() {
1004 return (mInCallActivity != null
1005 && !mInCallActivity.isDestroyed()
1006 && !mInCallActivity.isFinishing());
1007 }
1008
1009 /**
1010 * Determines if the In-Call app is currently changing configuration.
1011 *
1012 * @return {@code true} if the In-Call app is changing configuration.
1013 */
1014 public boolean isChangingConfigurations() {
1015 return mIsChangingConfigurations;
1016 }
1017
1018 /**
1019 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1020 * screen orientation).
1021 */
1022 /*package*/
1023 void updateIsChangingConfigurations() {
1024 mIsChangingConfigurations = false;
1025 if (mInCallActivity != null) {
1026 mIsChangingConfigurations = mInCallActivity.isChangingConfigurations();
1027 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001028 LogUtil.v(
1029 "InCallPresenter.updateIsChangingConfigurations",
1030 "updateIsChangingConfigurations = " + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001031 }
1032
1033 /** Called when the activity goes in/out of the foreground. */
1034 public void onUiShowing(boolean showing) {
1035 // We need to update the notification bar when we leave the UI because that
1036 // could trigger it to show again.
1037 if (mStatusBarNotifier != null) {
wangqic8cf79e2017-10-17 09:21:00 -07001038 mStatusBarNotifier.updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -08001039 }
1040
1041 if (mProximitySensor != null) {
1042 mProximitySensor.onInCallShowing(showing);
1043 }
1044
1045 Intent broadcastIntent = Bindings.get(mContext).getUiReadyBroadcastIntent(mContext);
1046 if (broadcastIntent != null) {
1047 broadcastIntent.putExtra(EXTRA_FIRST_TIME_SHOWN, !mIsActivityPreviouslyStarted);
1048
1049 if (showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001050 LogUtil.d("InCallPresenter.onUiShowing", "Sending sticky broadcast: ", broadcastIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001051 mContext.sendStickyBroadcast(broadcastIntent);
1052 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001053 LogUtil.d("InCallPresenter.onUiShowing", "Removing sticky broadcast: ", broadcastIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001054 mContext.removeStickyBroadcast(broadcastIntent);
1055 }
1056 }
1057
1058 if (showing) {
1059 mIsActivityPreviouslyStarted = true;
1060 } else {
1061 updateIsChangingConfigurations();
1062 }
1063
1064 for (InCallUiListener listener : mInCallUiListeners) {
1065 listener.onUiShowing(showing);
1066 }
1067
1068 if (mInCallActivity != null) {
1069 // Re-evaluate which fragment is being shown.
1070 mInCallActivity.onPrimaryCallStateChanged();
1071 }
1072 }
1073
Eric Erfanian2ca43182017-08-31 06:57:16 -07001074 public void refreshUi() {
1075 if (mInCallActivity != null) {
1076 // Re-evaluate which fragment is being shown.
1077 mInCallActivity.onPrimaryCallStateChanged();
1078 }
1079 }
1080
Eric Erfanianccca3152017-02-22 16:32:36 -08001081 public void addInCallUiListener(InCallUiListener listener) {
1082 mInCallUiListeners.add(listener);
1083 }
1084
1085 public boolean removeInCallUiListener(InCallUiListener listener) {
1086 return mInCallUiListeners.remove(listener);
1087 }
1088
1089 /*package*/
1090 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001091 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001092 notifyVideoPauseController(true);
Eric Erfanian2ca43182017-08-31 06:57:16 -07001093 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001094 }
1095
1096 /*package*/
1097 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001098 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001099 notifyVideoPauseController(false);
1100 }
1101
1102 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001103 LogUtil.d(
1104 "InCallPresenter.notifyVideoPauseController",
1105 "mIsChangingConfigurations=" + mIsChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001106 if (!mIsChangingConfigurations) {
1107 VideoPauseController.getInstance().onUiShowing(showing);
1108 }
1109 }
1110
1111 /** Brings the app into the foreground if possible. */
1112 public void bringToForeground(boolean showDialpad) {
1113 // Before we bring the incall UI to the foreground, we check to see if:
1114 // 1. It is not currently in the foreground
1115 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1116 // be displayed)
1117 // If the activity hadn't actually been started previously, yet there are still calls
1118 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1119 // bring it up the UI regardless.
1120 if (!isShowingInCallUi() && mInCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001121 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001122 }
1123 }
1124
1125 public void onPostDialCharWait(String callId, String chars) {
1126 if (isActivityStarted()) {
1127 mInCallActivity.showPostCharWaitDialog(callId, chars);
1128 }
1129 }
1130
1131 /**
1132 * Handles the green CALL key while in-call.
1133 *
1134 * @return true if we consumed the event.
1135 */
1136 public boolean handleCallKey() {
1137 LogUtil.v("InCallPresenter.handleCallKey", null);
1138
1139 // The green CALL button means either "Answer", "Unhold", or
1140 // "Swap calls", or can be a no-op, depending on the current state
1141 // of the Phone.
1142
1143 /** INCOMING CALL */
1144 final CallList calls = mCallList;
1145 final DialerCall incomingCall = calls.getIncomingCall();
1146 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1147
1148 // (1) Attempt to answer a call
1149 if (incomingCall != null) {
1150 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1151 return true;
1152 }
1153
1154 /** STATE_ACTIVE CALL */
1155 final DialerCall activeCall = calls.getActiveCall();
1156 if (activeCall != null) {
1157 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1158 // consolidate this logic.
1159 final boolean canMerge =
1160 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1161 final boolean canSwap =
1162 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1163
Eric Erfanian2ca43182017-08-31 06:57:16 -07001164 LogUtil.v(
1165 "InCallPresenter.handleCallKey",
1166 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001167
1168 // (2) Attempt actions on conference calls
1169 if (canMerge) {
1170 TelecomAdapter.getInstance().merge(activeCall.getId());
1171 return true;
1172 } else if (canSwap) {
1173 TelecomAdapter.getInstance().swap(activeCall.getId());
1174 return true;
1175 }
1176 }
1177
1178 /** BACKGROUND CALL */
1179 final DialerCall heldCall = calls.getBackgroundCall();
1180 if (heldCall != null) {
1181 // We have a hold call so presumeable it will always support HOLD...but
1182 // there is no harm in double checking.
1183 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1184
Eric Erfanian2ca43182017-08-31 06:57:16 -07001185 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001186
1187 // (4) unhold call
1188 if (heldCall.getState() == DialerCall.State.ONHOLD && canHold) {
1189 heldCall.unhold();
1190 return true;
1191 }
1192 }
1193
1194 // Always consume hard keys
1195 return true;
1196 }
1197
Eric Erfanianccca3152017-02-22 16:32:36 -08001198 /** Clears the previous fullscreen state. */
1199 public void clearFullscreen() {
1200 mIsFullScreen = false;
1201 }
1202
1203 /**
1204 * Changes the fullscreen mode of the in-call UI.
1205 *
1206 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1207 * otherwise.
1208 */
1209 public void setFullScreen(boolean isFullScreen) {
1210 setFullScreen(isFullScreen, false /* force */);
1211 }
1212
1213 /**
1214 * Changes the fullscreen mode of the in-call UI.
1215 *
1216 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1217 * otherwise.
1218 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1219 */
1220 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001221 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001222
1223 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1224 if (isDialpadVisible()) {
1225 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001226 LogUtil.v(
1227 "InCallPresenter.setFullScreen",
1228 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001229 }
1230
1231 if (mIsFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001232 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001233 return;
1234 }
1235 mIsFullScreen = isFullScreen;
1236 notifyFullscreenModeChange(mIsFullScreen);
1237 }
1238
1239 /**
1240 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1241 * otherwise.
1242 */
1243 public boolean isFullscreen() {
1244 return mIsFullScreen;
1245 }
1246
1247 /**
1248 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1249 *
1250 * @param isFullscreenMode {@code True} if entering full screen mode.
1251 */
1252 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
1253 for (InCallEventListener listener : mInCallEventListeners) {
1254 listener.onFullscreenModeChanged(isFullscreenMode);
1255 }
1256 }
1257
1258 /**
1259 * For some disconnected causes, we show a dialog. This calls into the activity to show the dialog
1260 * if appropriate for the call.
1261 */
1262 private void maybeShowErrorDialogOnDisconnect(DialerCall call) {
1263 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1264 if (isActivityStarted() && call.getState() == DialerCall.State.DISCONNECTED) {
1265 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1266 setDisconnectCauseForMissingAccounts(call);
1267 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001268 mInCallActivity.maybeShowErrorDialogOnDisconnect(
1269 new DisconnectMessage(mInCallActivity, call));
Eric Erfanianccca3152017-02-22 16:32:36 -08001270 }
1271 }
1272
1273 /**
1274 * When the state of in-call changes, this is the first method to get called. It determines if the
1275 * UI needs to be started or finished depending on the new state and does it.
1276 */
1277 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001278 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001279 LogUtil.d(
1280 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + mInCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001281
1282 // TODO: Consider a proper state machine implementation
1283
1284 // If the state isn't changing we have already done any starting/stopping of activities in
1285 // a previous pass...so lets cut out early
1286 if (newState == mInCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001287 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001288 return newState;
1289 }
1290
1291 // A new Incoming call means that the user needs to be notified of the the call (since
1292 // it wasn't them who initiated it). We do this through full screen notifications and
1293 // happens indirectly through {@link StatusBarNotifier}.
1294 //
1295 // The process for incoming calls is as follows:
1296 //
1297 // 1) CallList - Announces existence of new INCOMING call
1298 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1299 // - should be set to INCOMING.
1300 // 3) InCallPresenter - This method is called to see if we need to start or finish
1301 // the app given the new state.
1302 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1303 // StatusBarNotifier explicitly to issue a FullScreen Notification
1304 // that will either start the InCallActivity or show the user a
1305 // top-level notification dialog if the user is in an immersive app.
1306 // That notification can also start the InCallActivity.
1307 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1308 // call InCallPresenter::setActivity() to let the presenter
1309 // know that start-up is complete.
1310 //
1311 // [ AND NOW YOU'RE IN THE CALL. voila! ]
1312 //
1313 // Our app is started using a fullScreen notification. We need to do this whenever
1314 // we get an incoming call. Depending on the current context of the device, either a
1315 // incoming call HUN or the actual InCallActivity will be shown.
1316 final boolean startIncomingCallSequence = (InCallState.INCOMING == newState);
1317
1318 // A dialog to show on top of the InCallUI to select a PhoneAccount
1319 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1320
1321 // A new outgoing call indicates that the user just now dialed a number and when that
1322 // happens we need to display the screen immediately or show an account picker dialog if
1323 // no default is set. However, if the main InCallUI is already visible, we do not want to
1324 // re-initiate the start-up animation, so we do not need to do anything here.
1325 //
1326 // It is also possible to go into an intermediate state where the call has been initiated
1327 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1328 // This pending outgoing state can also launch the call screen.
1329 //
1330 // This is different from the incoming call sequence because we do not need to shock the
1331 // user with a top-level notification. Just show the call UI normally.
1332 boolean callCardFragmentVisible =
1333 mInCallActivity != null && mInCallActivity.getCallCardFragmentVisible();
1334 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1335 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1336
1337 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1338 // outgoing call process, so the UI should be brought up to show an error dialog.
1339 showCallUi |=
1340 (InCallState.PENDING_OUTGOING == mInCallState
1341 && InCallState.INCALL == newState
1342 && !isShowingInCallUi());
1343
1344 // Another exception - InCallActivity is in charge of disconnecting a call with no
1345 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1346 // call so that:
1347 // 1) The call can be disconnected correctly
1348 // 2) The UI comes up and correctly displays the error dialog.
1349 // TODO: Remove these special case conditions by making InCallPresenter a true state
1350 // machine. Telecom should also be the component responsible for disconnecting a call
1351 // with no valid accounts.
1352 showCallUi |=
1353 InCallState.PENDING_OUTGOING == newState
1354 && mainUiNotVisible
1355 && isCallWithNoValidAccounts(mCallList.getPendingOutgoingCall());
1356
1357 // The only time that we have an instance of mInCallActivity and it isn't started is
1358 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1359 // the activity. When it is finally destroyed, we double check if we should bring it back
1360 // up so we aren't going to lose anything by avoiding a second startup here.
1361 boolean activityIsFinishing = mInCallActivity != null && !isActivityStarted();
1362 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001363 LogUtil.i(
1364 "InCallPresenter.startOrFinishUi",
1365 "Undo the state change: " + newState + " -> " + mInCallState);
wangqicf61ca02017-08-31 15:32:55 -07001366 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001367 return mInCallState;
1368 }
1369
1370 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1371 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1372 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1373 // outgoing call after user ignore it by pressing home button.
1374 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1375 && !showCallUi
1376 && isActivityStarted()) {
1377 mInCallActivity.dismissPendingDialogs();
1378 }
1379
1380 if (showCallUi || showAccountPicker) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001381 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001382 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001383 } else if (startIncomingCallSequence) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001384 LogUtil.i("InCallPresenter.startOrFinishUi", "Start Full Screen in call UI");
Eric Erfanianccca3152017-02-22 16:32:36 -08001385
wangqic8cf79e2017-10-17 09:21:00 -07001386 mStatusBarNotifier.updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -08001387 } else if (newState == InCallState.NO_CALLS) {
1388 // The new state is the no calls state. Tear everything down.
1389 attemptFinishActivity();
1390 attemptCleanup();
1391 }
1392
wangqicf61ca02017-08-31 15:32:55 -07001393 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001394 return newState;
1395 }
1396
1397 /**
1398 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1399 * or PhoneAccounts to select from.
1400 */
1401 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1402
1403 Bundle extras = call.getIntentExtras();
1404 // Initialize the extras bundle to avoid NPE
1405 if (extras == null) {
1406 extras = new Bundle();
1407 }
1408
1409 final List<PhoneAccountHandle> phoneAccountHandles =
1410 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1411
1412 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1413 String scheme = call.getHandle().getScheme();
1414 final String errorMsg =
1415 PhoneAccount.SCHEME_TEL.equals(scheme)
1416 ? mContext.getString(R.string.callFailed_simError)
1417 : mContext.getString(R.string.incall_error_supp_service_unknown);
1418 DisconnectCause disconnectCause =
1419 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1420 call.setDisconnectCause(disconnectCause);
1421 }
1422 }
1423
Eric Erfanianccca3152017-02-22 16:32:36 -08001424 /**
1425 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1426 * Calling classes should use this as an indication whether to interact with the
1427 * InCallPresenter or not.
1428 */
1429 public boolean isReadyForTearDown() {
1430 return mInCallActivity == null && !mServiceConnected && mInCallState == InCallState.NO_CALLS;
1431 }
1432
1433 /**
1434 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1435 */
1436 private void attemptCleanup() {
1437 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001438 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001439
1440 cleanupSurfaces();
1441
1442 mIsActivityPreviouslyStarted = false;
1443 mIsChangingConfigurations = false;
1444
1445 // blow away stale contact info so that we get fresh data on
1446 // the next set of calls
1447 if (mContactInfoCache != null) {
1448 mContactInfoCache.clearCache();
1449 }
1450 mContactInfoCache = null;
1451
1452 if (mProximitySensor != null) {
1453 removeListener(mProximitySensor);
1454 mProximitySensor.tearDown();
1455 }
1456 mProximitySensor = null;
1457
1458 if (mStatusBarNotifier != null) {
1459 removeListener(mStatusBarNotifier);
Eric Erfaniand8046e52017-04-06 09:41:50 -07001460 EnrichedCallComponent.get(mContext)
1461 .getEnrichedCallManager()
1462 .unregisterStateChangedListener(mStatusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001463 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001464
Eric Erfanianccca3152017-02-22 16:32:36 -08001465 if (mExternalCallNotifier != null && mExternalCallList != null) {
1466 mExternalCallList.removeExternalCallListener(mExternalCallNotifier);
1467 }
1468 mStatusBarNotifier = null;
1469
1470 if (mCallList != null) {
1471 mCallList.removeListener(this);
1472 mCallList.removeListener(mSpamCallListListener);
1473 }
1474 mCallList = null;
1475
1476 mContext = null;
1477 mInCallActivity = null;
1478 mManageConferenceActivity = null;
1479
1480 mListeners.clear();
1481 mIncomingCallListeners.clear();
1482 mDetailsListeners.clear();
1483 mCanAddCallListeners.clear();
1484 mOrientationListeners.clear();
1485 mInCallEventListeners.clear();
1486 mInCallUiListeners.clear();
twyen8efb4952017-10-06 16:35:54 -07001487 if (!mInCallUiLocks.isEmpty()) {
1488 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + mInCallUiLocks);
1489 mInCallUiLocks.clear();
1490 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001491 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001492 }
1493 }
1494
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001495 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001496 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -08001497 mContext.startActivity(
1498 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001499 mContext, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001500 }
1501
1502 public void onServiceBind() {
1503 mServiceBound = true;
1504 }
1505
1506 public void onServiceUnbind() {
1507 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
1508 mServiceBound = false;
1509 }
1510
1511 public boolean isServiceBound() {
1512 return mServiceBound;
1513 }
1514
1515 public void maybeStartRevealAnimation(Intent intent) {
1516 if (intent == null || mInCallActivity != null) {
1517 return;
1518 }
1519 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1520 if (extras == null) {
1521 // Incoming call, just show the in-call UI directly.
1522 return;
1523 }
1524
1525 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1526 // Account selection dialog will show up so don't show the animation.
1527 return;
1528 }
1529
1530 final PhoneAccountHandle accountHandle =
1531 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1532 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001533
1534 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(true, accountHandle);
1535
1536 final Intent activityIntent =
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001537 InCallActivity.getIntent(mContext, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001538 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
1539 mContext.startActivity(activityIntent);
1540 }
1541
1542 /**
1543 * Retrieves the current in-call camera manager instance, creating if necessary.
1544 *
1545 * @return The {@link InCallCameraManager}.
1546 */
1547 public InCallCameraManager getInCallCameraManager() {
1548 synchronized (this) {
1549 if (mInCallCameraManager == null) {
1550 mInCallCameraManager = new InCallCameraManager(mContext);
1551 }
1552
1553 return mInCallCameraManager;
1554 }
1555 }
1556
1557 /**
1558 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1559 *
1560 * @param orientation The screen orientation of the device (one of: {@link
1561 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1562 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1563 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1564 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1565 */
1566 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001567 LogUtil.d(
1568 "InCallPresenter.onDeviceOrientationChange",
1569 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001570
1571 if (mCallList != null) {
1572 mCallList.notifyCallsOfDeviceRotation(orientation);
1573 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001574 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001575 }
1576
1577 // Notify listeners of device orientation changed.
1578 for (InCallOrientationListener listener : mOrientationListeners) {
1579 listener.onDeviceOrientationChanged(orientation);
1580 }
1581 }
1582
1583 /**
1584 * Configures the in-call UI activity so it can change orientations or not. Enables the
1585 * orientation event listener if allowOrientationChange is true, disables it if false.
1586 *
1587 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1588 * landscape. {@code false} if the in-call UI should be locked in portrait.
1589 */
1590 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
1591 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001592 LogUtil.e(
1593 "InCallPresenter.setInCallAllowsOrientationChange",
1594 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001595 return;
1596 }
1597 mInCallActivity.setAllowOrientationChange(allowOrientationChange);
1598 }
1599
1600 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001601 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
1602 mScreenTimeoutEnabled = enable;
1603 applyScreenTimeout();
1604 }
1605
1606 private void applyScreenTimeout() {
Eric Erfanianccca3152017-02-22 16:32:36 -08001607 if (mInCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001608 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001609 return;
1610 }
1611
1612 final Window window = mInCallActivity.getWindow();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001613 if (mScreenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001614 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1615 } else {
1616 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1617 }
1618 }
1619
1620 /**
1621 * Hides or shows the conference manager fragment.
1622 *
1623 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1624 * be hidden.
1625 */
1626 public void showConferenceCallManager(boolean show) {
1627 if (mInCallActivity != null) {
1628 mInCallActivity.showConferenceFragment(show);
1629 }
1630 if (!show && mManageConferenceActivity != null) {
1631 mManageConferenceActivity.finish();
1632 }
1633 }
1634
1635 /**
1636 * Determines if the dialpad is visible.
1637 *
1638 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1639 */
1640 public boolean isDialpadVisible() {
1641 if (mInCallActivity == null) {
1642 return false;
1643 }
1644 return mInCallActivity.isDialpadVisible();
1645 }
1646
1647 public ThemeColorManager getThemeColorManager() {
1648 return mThemeColorManager;
1649 }
1650
1651 /** Called when the foreground call changes. */
1652 public void onForegroundCallChanged(DialerCall newForegroundCall) {
1653 mThemeColorManager.onForegroundCallChanged(mContext, newForegroundCall);
1654 if (mInCallActivity != null) {
1655 mInCallActivity.onForegroundCallChanged(newForegroundCall);
1656 }
1657 }
1658
1659 public InCallActivity getActivity() {
1660 return mInCallActivity;
1661 }
1662
1663 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1664 public void setActivity(InCallActivity inCallActivity) {
1665 if (inCallActivity == null) {
1666 throw new IllegalArgumentException("registerActivity cannot be called with null");
1667 }
1668 if (mInCallActivity != null && mInCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001669 LogUtil.w(
1670 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001671 }
1672 updateActivity(inCallActivity);
1673 }
1674
1675 ExternalCallNotifier getExternalCallNotifier() {
1676 return mExternalCallNotifier;
1677 }
1678
1679 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
1680 if (mLocalVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001681 boolean isPixel2017 = false;
1682 if (mContext != null) {
1683 isPixel2017 = mContext.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
1684 }
1685 mLocalVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001686 }
1687 return mLocalVideoSurfaceTexture;
1688 }
1689
1690 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
1691 if (mRemoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001692 boolean isPixel2017 = false;
1693 if (mContext != null) {
1694 isPixel2017 = mContext.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
1695 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001696 mRemoteVideoSurfaceTexture =
roldenburg12b50c62017-09-01 14:41:19 -07001697 VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001698 }
1699 return mRemoteVideoSurfaceTexture;
1700 }
1701
1702 void cleanupSurfaces() {
1703 if (mRemoteVideoSurfaceTexture != null) {
1704 mRemoteVideoSurfaceTexture.setDoneWithSurface();
1705 mRemoteVideoSurfaceTexture = null;
1706 }
1707 if (mLocalVideoSurfaceTexture != null) {
1708 mLocalVideoSurfaceTexture.setDoneWithSurface();
1709 mLocalVideoSurfaceTexture = null;
1710 }
1711 }
1712
1713 /** All the main states of InCallActivity. */
1714 public enum InCallState {
1715 // InCall Screen is off and there are no calls
1716 NO_CALLS,
1717
1718 // Incoming-call screen is up
1719 INCOMING,
1720
1721 // In-call experience is showing
1722 INCALL,
1723
1724 // Waiting for user input before placing outgoing call
1725 WAITING_FOR_ACCOUNT,
1726
1727 // UI is starting up but no call has been initiated yet.
1728 // The UI is waiting for Telecom to respond.
1729 PENDING_OUTGOING,
1730
1731 // User is dialing out
1732 OUTGOING;
1733
1734 public boolean isIncoming() {
1735 return (this == INCOMING);
1736 }
1737
1738 public boolean isConnectingOrConnected() {
1739 return (this == INCOMING || this == OUTGOING || this == INCALL);
1740 }
1741 }
1742
1743 /** Interface implemented by classes that need to know about the InCall State. */
1744 public interface InCallStateListener {
1745
1746 // TODO: Enhance state to contain the call objects instead of passing CallList
1747 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1748 }
1749
1750 public interface IncomingCallListener {
1751
1752 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1753 }
1754
1755 public interface CanAddCallListener {
1756
1757 void onCanAddCallChanged(boolean canAddCall);
1758 }
1759
1760 public interface InCallDetailsListener {
1761
1762 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1763 }
1764
1765 public interface InCallOrientationListener {
1766
1767 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1768 }
1769
1770 /**
1771 * Interface implemented by classes that need to know about events which occur within the In-Call
1772 * UI. Used as a means of communicating between fragments that make up the UI.
1773 */
1774 public interface InCallEventListener {
1775
1776 void onFullscreenModeChanged(boolean isFullscreenMode);
1777 }
1778
1779 public interface InCallUiListener {
1780
1781 void onUiShowing(boolean showing);
1782 }
twyen8efb4952017-10-06 16:35:54 -07001783
1784 private class InCallUiLockImpl implements InCallUiLock {
1785 private final String tag;
1786
1787 private InCallUiLockImpl(String tag) {
1788 this.tag = tag;
1789 }
1790
1791 @MainThread
1792 @Override
1793 public void release() {
1794 Assert.isMainThread();
1795 releaseInCallUiLock(InCallUiLockImpl.this);
1796 }
1797
1798 @Override
1799 public String toString() {
1800 return "InCallUiLock[" + tag + "]";
1801 }
1802 }
1803
1804 @MainThread
1805 public InCallUiLock acquireInCallUiLock(String tag) {
1806 Assert.isMainThread();
1807 InCallUiLock lock = new InCallUiLockImpl(tag);
1808 mInCallUiLocks.add(lock);
1809 return lock;
1810 }
1811
1812 @MainThread
1813 private void releaseInCallUiLock(InCallUiLock lock) {
1814 Assert.isMainThread();
1815 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
1816 mInCallUiLocks.remove(lock);
1817 if (mInCallUiLocks.isEmpty()) {
1818 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
1819 if (mInCallState == InCallState.NO_CALLS) {
1820 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
1821 attemptFinishActivity();
1822 attemptCleanup();
1823 }
1824 }
1825 }
1826
1827 @MainThread
1828 public boolean isInCallUiLocked() {
1829 Assert.isMainThread();
1830 if (mInCallUiLocks.isEmpty()) {
1831 return false;
1832 }
1833 for (InCallUiLock lock : mInCallUiLocks) {
1834 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
1835 }
1836 return true;
1837 }
1838
1839 private final Set<InCallUiLock> mInCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08001840}