blob: 3020bf1fa9009a05712c5e6fd13b4172476a8943 [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;
yueg77cb8e52017-10-27 16:42:51 -070031import android.telecom.CallAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -080032import android.telecom.DisconnectCause;
33import android.telecom.PhoneAccount;
34import android.telecom.PhoneAccountHandle;
35import android.telecom.TelecomManager;
36import android.telecom.VideoProfile;
37import android.telephony.PhoneStateListener;
38import android.telephony.TelephonyManager;
twyen8efb4952017-10-06 16:35:54 -070039import android.util.ArraySet;
Eric Erfanianccca3152017-02-22 16:32:36 -080040import android.view.Window;
41import android.view.WindowManager;
yueg32d9ae72018-04-05 17:35:56 -070042import android.widget.Toast;
Eric Erfanianccca3152017-02-22 16:32:36 -080043import com.android.contacts.common.compat.CallCompat;
yueg32d9ae72018-04-05 17:35:56 -070044import com.android.dialer.CallConfiguration;
45import com.android.dialer.Mode;
Eric Erfanianccca3152017-02-22 16:32:36 -080046import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler;
47import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler.OnCheckBlockedListener;
Eric Erfanian83b20212017-05-31 08:53:10 -070048import com.android.dialer.blocking.FilteredNumberCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080049import com.android.dialer.blocking.FilteredNumbersUtil;
twyen8efb4952017-10-06 16:35:54 -070050import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080051import com.android.dialer.common.LogUtil;
zachh6a4cebd2017-10-24 17:10:06 -070052import com.android.dialer.common.concurrent.DialerExecutorComponent;
Eric Erfaniand8046e52017-04-06 09:41:50 -070053import com.android.dialer.enrichedcall.EnrichedCallComponent;
Eric Erfanian10b34a52017-05-04 08:23:17 -070054import com.android.dialer.location.GeoUtil;
yuegb493c3f2018-04-27 14:00:12 -070055import com.android.dialer.logging.DialerImpression;
Eric Erfanian8369df02017-05-03 10:27:13 -070056import com.android.dialer.logging.InteractionEvent;
Eric Erfanianccca3152017-02-22 16:32:36 -080057import com.android.dialer.logging.Logger;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070058import com.android.dialer.postcall.PostCall;
twyena4745bd2017-12-12 18:40:11 -080059import com.android.dialer.telecom.TelecomCallUtil;
Eric Erfanianccca3152017-02-22 16:32:36 -080060import com.android.dialer.telecom.TelecomUtil;
61import com.android.dialer.util.TouchPointManager;
62import com.android.incallui.InCallOrientationEventListener.ScreenOrientation;
63import com.android.incallui.answerproximitysensor.PseudoScreenState;
yueg77cb8e52017-10-27 16:42:51 -070064import com.android.incallui.audiomode.AudioModeProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080065import com.android.incallui.call.CallList;
66import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080067import com.android.incallui.call.ExternalCallList;
Eric Erfanianccca3152017-02-22 16:32:36 -080068import com.android.incallui.call.TelecomAdapter;
wangqibb94ca62018-04-27 14:34:04 -070069import com.android.incallui.call.state.DialerCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -070070import com.android.incallui.disconnectdialog.DisconnectMessage;
twyen8efb4952017-10-06 16:35:54 -070071import com.android.incallui.incalluilock.InCallUiLock;
Eric Erfanianccca3152017-02-22 16:32:36 -080072import com.android.incallui.latencyreport.LatencyReport;
73import com.android.incallui.legacyblocking.BlockedNumberContentObserver;
74import com.android.incallui.spam.SpamCallListListener;
erfaniand05d8992018-03-20 19:42:26 -070075import com.android.incallui.speakeasy.SpeakEasyCallManager;
yuegb47528e2018-04-24 12:12:57 -070076import com.android.incallui.telecomeventui.InternationalCallOnWifiDialogActivity;
77import com.android.incallui.telecomeventui.InternationalCallOnWifiDialogFragment;
Eric Erfanianccca3152017-02-22 16:32:36 -080078import com.android.incallui.videosurface.bindings.VideoSurfaceBindings;
79import com.android.incallui.videosurface.protocol.VideoSurfaceTexture;
Eric Erfanian90508232017-03-24 09:31:16 -070080import com.android.incallui.videotech.utils.VideoUtils;
yueg32d9ae72018-04-05 17:35:56 -070081import com.google.protobuf.InvalidProtocolBufferException;
Eric Erfanianccca3152017-02-22 16:32:36 -080082import java.util.Collections;
83import java.util.List;
84import java.util.Objects;
85import java.util.Set;
86import java.util.concurrent.ConcurrentHashMap;
87import java.util.concurrent.CopyOnWriteArrayList;
88import java.util.concurrent.atomic.AtomicBoolean;
89
90/**
91 * Takes updates from the CallList and notifies the InCallActivity (UI) of the changes. Responsible
92 * for starting the activity for a new call and finishing the activity when all calls are
93 * disconnected. Creates and manages the in-call state and provides a listener pattern for the
94 * presenters that want to listen in on the in-call state changes. TODO: This class has become more
95 * of a state machine at this point. Consider renaming.
96 */
yueg77cb8e52017-10-27 16:42:51 -070097public class InCallPresenter implements CallList.Listener, AudioModeProvider.AudioModeListener {
Eric Erfanian2ca43182017-08-31 06:57:16 -070098 private static final String PIXEL2017_SYSTEM_FEATURE =
99 "com.google.android.feature.PIXEL_2017_EXPERIENCE";
yueg32d9ae72018-04-05 17:35:56 -0700100 private static final String CALL_CONFIGURATION_EXTRA = "call_configuration";
Eric Erfanianccca3152017-02-22 16:32:36 -0800101
102 private static final long BLOCK_QUERY_TIMEOUT_MS = 1000;
103
104 private static final Bundle EMPTY_EXTRAS = new Bundle();
105
linyuh183cb712017-12-27 17:02:37 -0800106 private static InCallPresenter inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -0800107
108 /**
109 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is load factor before
110 * resizing, 1 means we only expect a single thread to access the map so make only a single shard
111 */
linyuh183cb712017-12-27 17:02:37 -0800112 private final Set<InCallStateListener> listeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800113 Collections.newSetFromMap(new ConcurrentHashMap<InCallStateListener, Boolean>(8, 0.9f, 1));
114
linyuh183cb712017-12-27 17:02:37 -0800115 private final List<IncomingCallListener> incomingCallListeners = new CopyOnWriteArrayList<>();
116 private final Set<InCallDetailsListener> detailsListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800117 Collections.newSetFromMap(new ConcurrentHashMap<InCallDetailsListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800118 private final Set<CanAddCallListener> canAddCallListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800119 Collections.newSetFromMap(new ConcurrentHashMap<CanAddCallListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800120 private final Set<InCallUiListener> inCallUiListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800121 Collections.newSetFromMap(new ConcurrentHashMap<InCallUiListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800122 private final Set<InCallOrientationListener> orientationListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800123 Collections.newSetFromMap(
124 new ConcurrentHashMap<InCallOrientationListener, Boolean>(8, 0.9f, 1));
linyuh183cb712017-12-27 17:02:37 -0800125 private final Set<InCallEventListener> inCallEventListeners =
Eric Erfanianccca3152017-02-22 16:32:36 -0800126 Collections.newSetFromMap(new ConcurrentHashMap<InCallEventListener, Boolean>(8, 0.9f, 1));
127
linyuh183cb712017-12-27 17:02:37 -0800128 private StatusBarNotifier statusBarNotifier;
129 private ExternalCallNotifier externalCallNotifier;
130 private ContactInfoCache contactInfoCache;
131 private Context context;
132 private final OnCheckBlockedListener onCheckBlockedListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800133 new OnCheckBlockedListener() {
134 @Override
135 public void onCheckComplete(final Integer id) {
136 if (id != null && id != FilteredNumberAsyncQueryHandler.INVALID_ID) {
137 // Silence the ringer now to prevent ringing and vibration before the call is
138 // terminated when Telecom attempts to add it.
linyuh183cb712017-12-27 17:02:37 -0800139 TelecomUtil.silenceRinger(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800140 }
141 }
142 };
linyuh183cb712017-12-27 17:02:37 -0800143 private CallList callList;
144 private ExternalCallList externalCallList;
145 private InCallActivity inCallActivity;
146 private ManageConferenceActivity manageConferenceActivity;
147 private final android.telecom.Call.Callback callCallback =
Eric Erfanianccca3152017-02-22 16:32:36 -0800148 new android.telecom.Call.Callback() {
149 @Override
150 public void onPostDialWait(
151 android.telecom.Call telecomCall, String remainingPostDialSequence) {
linyuh183cb712017-12-27 17:02:37 -0800152 final DialerCall call = callList.getDialerCallFromTelecomCall(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800153 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700154 LogUtil.w(
155 "InCallPresenter.onPostDialWait",
156 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800157 return;
158 }
159 onPostDialCharWait(call.getId(), remainingPostDialSequence);
160 }
161
162 @Override
163 public void onDetailsChanged(
164 android.telecom.Call telecomCall, android.telecom.Call.Details details) {
linyuh183cb712017-12-27 17:02:37 -0800165 final DialerCall call = callList.getDialerCallFromTelecomCall(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800166 if (call == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700167 LogUtil.w(
168 "InCallPresenter.onDetailsChanged",
169 "DialerCall not found in call list: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800170 return;
171 }
172
173 if (details.hasProperty(Details.PROPERTY_IS_EXTERNAL_CALL)
linyuh183cb712017-12-27 17:02:37 -0800174 && !externalCallList.isCallTracked(telecomCall)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800175
176 // A regular call became an external call so swap call lists.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700177 LogUtil.i("InCallPresenter.onDetailsChanged", "Call became external: " + telecomCall);
linyuh183cb712017-12-27 17:02:37 -0800178 callList.onInternalCallMadeExternal(context, telecomCall);
179 externalCallList.onCallAdded(telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800180 return;
181 }
182
linyuh183cb712017-12-27 17:02:37 -0800183 for (InCallDetailsListener listener : detailsListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800184 listener.onDetailsChanged(call, details);
185 }
186 }
187
188 @Override
189 public void onConferenceableCallsChanged(
190 android.telecom.Call telecomCall, List<android.telecom.Call> conferenceableCalls) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700191 LogUtil.i(
192 "InCallPresenter.onConferenceableCallsChanged",
193 "onConferenceableCallsChanged: " + telecomCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800194 onDetailsChanged(telecomCall, telecomCall.getDetails());
195 }
196 };
linyuh183cb712017-12-27 17:02:37 -0800197 private InCallState inCallState = InCallState.NO_CALLS;
198 private ProximitySensor proximitySensor;
199 private final PseudoScreenState pseudoScreenState = new PseudoScreenState();
200 private boolean serviceConnected;
201 private InCallCameraManager inCallCameraManager;
202 private FilteredNumberAsyncQueryHandler filteredQueryHandler;
203 private CallList.Listener spamCallListListener;
twyen27c37182018-04-24 11:48:12 -0700204 private CallList.Listener activeCallsListener;
Eric Erfanianccca3152017-02-22 16:32:36 -0800205 /** Whether or not we are currently bound and waiting for Telecom to send us a new call. */
linyuh183cb712017-12-27 17:02:37 -0800206 private boolean boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -0800207 /** Determines if the InCall UI is in fullscreen mode or not. */
linyuh183cb712017-12-27 17:02:37 -0800208 private boolean isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800209
linyuh183cb712017-12-27 17:02:37 -0800210 private boolean screenTimeoutEnabled = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700211
linyuh183cb712017-12-27 17:02:37 -0800212 private PhoneStateListener phoneStateListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800213 new PhoneStateListener() {
214 @Override
215 public void onCallStateChanged(int state, String incomingNumber) {
216 if (state == TelephonyManager.CALL_STATE_RINGING) {
linyuh183cb712017-12-27 17:02:37 -0800217 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800218 return;
219 }
220 // Check if the number is blocked, to silence the ringer.
linyuh183cb712017-12-27 17:02:37 -0800221 String countryIso = GeoUtil.getCurrentCountryIso(context);
222 filteredQueryHandler.isBlockedNumber(
223 onCheckBlockedListener, incomingNumber, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800224 }
225 }
226 };
twyena4745bd2017-12-12 18:40:11 -0800227
Eric Erfanianccca3152017-02-22 16:32:36 -0800228 /** Whether or not InCallService is bound to Telecom. */
linyuh183cb712017-12-27 17:02:37 -0800229 private boolean serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800230
231 /**
232 * When configuration changes Android kills the current activity and starts a new one. The flag is
233 * used to check if full clean up is necessary (activity is stopped and new activity won't be
234 * started), or if a new activity will be started right after the current one is destroyed, and
235 * therefore no need in release all resources.
236 */
linyuh183cb712017-12-27 17:02:37 -0800237 private boolean isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800238
linyuh183cb712017-12-27 17:02:37 -0800239 private boolean awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800240
linyuh183cb712017-12-27 17:02:37 -0800241 private ExternalCallList.ExternalCallListener externalCallListener =
Eric Erfanianccca3152017-02-22 16:32:36 -0800242 new ExternalCallList.ExternalCallListener() {
243
244 @Override
245 public void onExternalCallPulled(android.telecom.Call call) {
246 // Note: keep this code in sync with InCallPresenter#onCallAdded
247 LatencyReport latencyReport = new LatencyReport(call);
248 latencyReport.onCallBlockingDone();
249 // Note: External calls do not require spam checking.
linyuh183cb712017-12-27 17:02:37 -0800250 callList.onCallAdded(context, call, latencyReport);
251 call.registerCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800252 }
253
254 @Override
255 public void onExternalCallAdded(android.telecom.Call call) {
256 // No-op
257 }
258
259 @Override
260 public void onExternalCallRemoved(android.telecom.Call call) {
261 // No-op
262 }
263
264 @Override
265 public void onExternalCallUpdated(android.telecom.Call call) {
266 // No-op
267 }
268 };
269
linyuh183cb712017-12-27 17:02:37 -0800270 private ThemeColorManager themeColorManager;
271 private VideoSurfaceTexture localVideoSurfaceTexture;
272 private VideoSurfaceTexture remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -0800273
erfaniand05d8992018-03-20 19:42:26 -0700274 private SpeakEasyCallManager speakEasyCallManager;
275
Eric Erfanian10b34a52017-05-04 08:23:17 -0700276 /** Inaccessible constructor. Must use getRunningInstance() to get this singleton. */
Eric Erfanianccca3152017-02-22 16:32:36 -0800277 @VisibleForTesting
278 InCallPresenter() {}
279
280 public static synchronized InCallPresenter getInstance() {
linyuh183cb712017-12-27 17:02:37 -0800281 if (inCallPresenter == null) {
wangqic8cf79e2017-10-17 09:21:00 -0700282 Trace.beginSection("InCallPresenter.Constructor");
linyuh183cb712017-12-27 17:02:37 -0800283 inCallPresenter = new InCallPresenter();
wangqic8cf79e2017-10-17 09:21:00 -0700284 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800285 }
linyuh183cb712017-12-27 17:02:37 -0800286 return inCallPresenter;
Eric Erfanianccca3152017-02-22 16:32:36 -0800287 }
288
Eric Erfanian10b34a52017-05-04 08:23:17 -0700289 @VisibleForTesting
290 public static synchronized void setInstanceForTesting(InCallPresenter inCallPresenter) {
linyuh183cb712017-12-27 17:02:37 -0800291 InCallPresenter.inCallPresenter = inCallPresenter;
Eric Erfanian10b34a52017-05-04 08:23:17 -0700292 }
293
Eric Erfanianccca3152017-02-22 16:32:36 -0800294 /**
295 * Determines whether or not a call has no valid phone accounts that can be used to make the call
296 * with. Emergency calls do not require a phone account.
297 *
298 * @param call to check accounts for.
299 * @return {@code true} if the call has no call capable phone accounts set, {@code false} if the
300 * call contains a phone account that could be used to initiate it with, or is an emergency
301 * call.
302 */
303 public static boolean isCallWithNoValidAccounts(DialerCall call) {
304 if (call != null && !call.isEmergencyCall()) {
305 Bundle extras = call.getIntentExtras();
306
307 if (extras == null) {
308 extras = EMPTY_EXTRAS;
309 }
310
311 final List<PhoneAccountHandle> phoneAccountHandles =
312 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
313
314 if ((call.getAccountHandle() == null
315 && (phoneAccountHandles == null || phoneAccountHandles.isEmpty()))) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700316 LogUtil.i(
317 "InCallPresenter.isCallWithNoValidAccounts", "No valid accounts for call " + call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800318 return true;
319 }
320 }
321 return false;
322 }
323
324 public InCallState getInCallState() {
linyuh183cb712017-12-27 17:02:37 -0800325 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800326 }
327
328 public CallList getCallList() {
linyuh183cb712017-12-27 17:02:37 -0800329 return callList;
Eric Erfanianccca3152017-02-22 16:32:36 -0800330 }
331
332 public void setUp(
333 @NonNull Context context,
334 CallList callList,
335 ExternalCallList externalCallList,
336 StatusBarNotifier statusBarNotifier,
337 ExternalCallNotifier externalCallNotifier,
338 ContactInfoCache contactInfoCache,
Eric Erfanian83b20212017-05-31 08:53:10 -0700339 ProximitySensor proximitySensor,
erfaniand05d8992018-03-20 19:42:26 -0700340 FilteredNumberAsyncQueryHandler filteredNumberQueryHandler,
yueg4613e8f2018-04-23 13:35:33 -0700341 @NonNull SpeakEasyCallManager speakEasyCallManager) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700342 Trace.beginSection("InCallPresenter.setUp");
linyuh183cb712017-12-27 17:02:37 -0800343 if (serviceConnected) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700344 LogUtil.i("InCallPresenter.setUp", "New service connection replacing existing one.");
linyuh183cb712017-12-27 17:02:37 -0800345 if (context != this.context || callList != this.callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800346 throw new IllegalStateException();
347 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700348 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800349 return;
350 }
351
352 Objects.requireNonNull(context);
linyuh183cb712017-12-27 17:02:37 -0800353 this.context = context;
Eric Erfanianccca3152017-02-22 16:32:36 -0800354
linyuh183cb712017-12-27 17:02:37 -0800355 this.contactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800356
linyuh183cb712017-12-27 17:02:37 -0800357 this.statusBarNotifier = statusBarNotifier;
358 this.externalCallNotifier = externalCallNotifier;
359 addListener(this.statusBarNotifier);
360 EnrichedCallComponent.get(this.context)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700361 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -0800362 .registerStateChangedListener(this.statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -0800363
linyuh183cb712017-12-27 17:02:37 -0800364 this.proximitySensor = proximitySensor;
365 addListener(this.proximitySensor);
Eric Erfanianccca3152017-02-22 16:32:36 -0800366
linyuh183cb712017-12-27 17:02:37 -0800367 if (themeColorManager == null) {
368 themeColorManager =
369 new ThemeColorManager(new InCallUIMaterialColorMapUtils(this.context.getResources()));
wangqi8d662ca2017-10-26 11:27:19 -0700370 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800371
linyuh183cb712017-12-27 17:02:37 -0800372 this.callList = callList;
373 this.externalCallList = externalCallList;
374 externalCallList.addExternalCallListener(this.externalCallNotifier);
375 externalCallList.addExternalCallListener(externalCallListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800376
377 // This only gets called by the service so this is okay.
linyuh183cb712017-12-27 17:02:37 -0800378 serviceConnected = true;
Eric Erfanianccca3152017-02-22 16:32:36 -0800379
380 // The final thing we do in this set up is add ourselves as a listener to CallList. This
381 // will kick off an update and the whole process can start.
linyuh183cb712017-12-27 17:02:37 -0800382 this.callList.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800383
384 // Create spam call list listener and add it to the list of listeners
linyuh183cb712017-12-27 17:02:37 -0800385 spamCallListListener =
zachh6a4cebd2017-10-24 17:10:06 -0700386 new SpamCallListListener(
387 context, DialerExecutorComponent.get(context).dialerExecutorFactory());
linyuh183cb712017-12-27 17:02:37 -0800388 this.callList.addListener(spamCallListListener);
twyen27c37182018-04-24 11:48:12 -0700389 activeCallsListener = new ActiveCallsCallListListener(context);
390 this.callList.addListener(activeCallsListener);
Eric Erfanianccca3152017-02-22 16:32:36 -0800391
392 VideoPauseController.getInstance().setUp(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800393
linyuh183cb712017-12-27 17:02:37 -0800394 filteredQueryHandler = filteredNumberQueryHandler;
erfaniand05d8992018-03-20 19:42:26 -0700395 this.speakEasyCallManager = speakEasyCallManager;
linyuh183cb712017-12-27 17:02:37 -0800396 this.context
Eric Erfanianccca3152017-02-22 16:32:36 -0800397 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800398 .listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800399
yueg77cb8e52017-10-27 16:42:51 -0700400 AudioModeProvider.getInstance().addListener(this);
401
roldenburg42099012018-04-09 14:47:15 -0700402 // Add listener to notify Telephony process when the incoming call screen is started or
403 // finished. This is for hiding USSD dialog because the incoming call screen should have
404 // higher precedence over this dialog.
405 MotorolaInCallUiNotifier motorolaInCallUiNotifier = new MotorolaInCallUiNotifier(context);
406 addInCallUiListener(motorolaInCallUiNotifier);
407 addListener(motorolaInCallUiNotifier);
yueg7f5acbe2018-01-10 13:50:29 -0800408
Eric Erfanian2ca43182017-08-31 06:57:16 -0700409 LogUtil.d("InCallPresenter.setUp", "Finished InCallPresenter.setUp");
410 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800411 }
412
413 /**
yueg32d9ae72018-04-05 17:35:56 -0700414 * Return whether we should start call in bubble mode and not show InCallActivity. The call mode
415 * should be set in CallConfiguration in EXTRA_OUTGOING_CALL_EXTRAS when starting a call intent.
416 */
yueg71bf7e42018-04-30 12:00:48 -0700417 public boolean shouldStartInBubbleMode() {
yueg32d9ae72018-04-05 17:35:56 -0700418 if (!ReturnToCallController.isEnabled(context)) {
419 return false;
420 }
yueg4613e8f2018-04-23 13:35:33 -0700421
422 // We only start in Bubble mode for outgoing call
423 DialerCall dialerCall = callList.getPendingOutgoingCall();
424 if (dialerCall == null) {
425 dialerCall = callList.getOutgoingCall();
426 }
427 if (dialerCall == null) {
yueg32d9ae72018-04-05 17:35:56 -0700428 return false;
429 }
yueg4613e8f2018-04-23 13:35:33 -0700430
431 Bundle extras = dialerCall.getIntentExtras();
yuegb493c3f2018-04-27 14:00:12 -0700432 boolean result = shouldStartInBubbleModeWithExtras(extras);
433 if (result) {
434 Logger.get(context)
435 .logCallImpression(
436 DialerImpression.Type.START_CALL_IN_BUBBLE_MODE,
437 dialerCall.getUniqueCallId(),
438 dialerCall.getTimeAddedMs());
439 }
440 return result;
yueg4613e8f2018-04-23 13:35:33 -0700441 }
442
443 private boolean shouldStartInBubbleModeWithExtras(Bundle outgoingExtras) {
444 if (!ReturnToCallController.isEnabled(context)) {
445 return false;
446 }
447
448 if (outgoingExtras == null) {
449 return false;
450 }
451 byte[] callConfigurationByteArray = outgoingExtras.getByteArray(CALL_CONFIGURATION_EXTRA);
yueg32d9ae72018-04-05 17:35:56 -0700452 if (callConfigurationByteArray == null) {
453 return false;
454 }
455 try {
456 CallConfiguration callConfiguration = CallConfiguration.parseFrom(callConfigurationByteArray);
457 LogUtil.i(
458 "InCallPresenter.shouldStartInBubbleMode",
459 "call mode: " + callConfiguration.getCallMode());
460 return callConfiguration.getCallMode() == Mode.BUBBLE;
461 } catch (InvalidProtocolBufferException e) {
462 return false;
463 }
464 }
465
466 /**
Eric Erfanianccca3152017-02-22 16:32:36 -0800467 * Called when the telephony service has disconnected from us. This will happen when there are no
468 * more active calls. However, we may still want to continue showing the UI for certain cases like
469 * showing "Call Ended". What we really want is to wait for the activity and the service to both
470 * disconnect before we tear things down. This method sets a serviceConnected boolean and calls a
471 * secondary method that performs the aforementioned logic.
472 */
473 public void tearDown() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700474 LogUtil.d("InCallPresenter.tearDown", "tearDown");
linyuh183cb712017-12-27 17:02:37 -0800475 callList.clearOnDisconnect();
Eric Erfanianccca3152017-02-22 16:32:36 -0800476
linyuh183cb712017-12-27 17:02:37 -0800477 serviceConnected = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800478
linyuh183cb712017-12-27 17:02:37 -0800479 context
Eric Erfanianccca3152017-02-22 16:32:36 -0800480 .getSystemService(TelephonyManager.class)
linyuh183cb712017-12-27 17:02:37 -0800481 .listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
Eric Erfanianccca3152017-02-22 16:32:36 -0800482
483 attemptCleanup();
484 VideoPauseController.getInstance().tearDown();
yueg77cb8e52017-10-27 16:42:51 -0700485 AudioModeProvider.getInstance().removeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800486 }
487
488 private void attemptFinishActivity() {
linyuh183cb712017-12-27 17:02:37 -0800489 screenTimeoutEnabled = true;
490 final boolean doFinish = (inCallActivity != null && isActivityStarted());
Eric Erfanian2ca43182017-08-31 06:57:16 -0700491 LogUtil.i("InCallPresenter.attemptFinishActivity", "Hide in call UI: " + doFinish);
Eric Erfanianccca3152017-02-22 16:32:36 -0800492 if (doFinish) {
linyuh183cb712017-12-27 17:02:37 -0800493 inCallActivity.setExcludeFromRecents(true);
494 inCallActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -0800495 }
496 }
497
498 /**
499 * Called when the UI ends. Attempts to tear down everything if necessary. See {@link #tearDown()}
500 * for more insight on the tear-down process.
501 */
502 public void unsetActivity(InCallActivity inCallActivity) {
503 if (inCallActivity == null) {
504 throw new IllegalArgumentException("unregisterActivity cannot be called with null");
505 }
linyuh183cb712017-12-27 17:02:37 -0800506 if (this.inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700507 LogUtil.i(
508 "InCallPresenter.unsetActivity", "No InCallActivity currently set, no need to unset.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800509 return;
510 }
linyuh183cb712017-12-27 17:02:37 -0800511 if (this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700512 LogUtil.w(
513 "InCallPresenter.unsetActivity",
Eric Erfanianccca3152017-02-22 16:32:36 -0800514 "Second instance of InCallActivity is trying to unregister when another"
515 + " instance is active. Ignoring.");
516 return;
517 }
518 updateActivity(null);
519 }
520
521 /**
522 * Updates the current instance of {@link InCallActivity} with the provided one. If a {@code null}
523 * activity is provided, it means that the activity was finished and we should attempt to cleanup.
524 */
525 private void updateActivity(InCallActivity inCallActivity) {
wangqi9982f0d2017-10-11 17:46:07 -0700526 Trace.beginSection("InCallPresenter.updateActivity");
Eric Erfanianccca3152017-02-22 16:32:36 -0800527 boolean updateListeners = false;
528 boolean doAttemptCleanup = false;
529
530 if (inCallActivity != null) {
linyuh183cb712017-12-27 17:02:37 -0800531 if (this.inCallActivity == null) {
532 context = inCallActivity.getApplicationContext();
Eric Erfanianccca3152017-02-22 16:32:36 -0800533 updateListeners = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700534 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized");
Eric Erfanianccca3152017-02-22 16:32:36 -0800535 } else {
536 // since setActivity is called onStart(), it can be called multiple times.
537 // This is fine and ignorable, but we do not want to update the world every time
538 // this happens (like going to/from background) so we do not set updateListeners.
539 }
540
linyuh183cb712017-12-27 17:02:37 -0800541 this.inCallActivity = inCallActivity;
542 this.inCallActivity.setExcludeFromRecents(false);
Eric Erfanianccca3152017-02-22 16:32:36 -0800543
544 // By the time the UI finally comes up, the call may already be disconnected.
545 // If that's the case, we may need to show an error dialog.
linyuh183cb712017-12-27 17:02:37 -0800546 if (callList != null && callList.getDisconnectedCall() != null) {
547 showDialogOrToastForDisconnectedCall(callList.getDisconnectedCall());
Eric Erfanianccca3152017-02-22 16:32:36 -0800548 }
549
550 // When the UI comes up, we need to first check the in-call state.
551 // If we are showing NO_CALLS, that means that a call probably connected and
552 // then immediately disconnected before the UI was able to come up.
553 // If we dont have any calls, start tearing down the UI instead.
554 // NOTE: This code relies on {@link #mInCallActivity} being set so we run it after
555 // it has been set.
linyuh183cb712017-12-27 17:02:37 -0800556 if (inCallState == InCallState.NO_CALLS) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700557 LogUtil.i("InCallPresenter.updateActivity", "UI Initialized, but no calls left. Shut down");
Eric Erfanianccca3152017-02-22 16:32:36 -0800558 attemptFinishActivity();
wangqi9982f0d2017-10-11 17:46:07 -0700559 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800560 return;
561 }
562 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700563 LogUtil.i("InCallPresenter.updateActivity", "UI Destroyed");
Eric Erfanianccca3152017-02-22 16:32:36 -0800564 updateListeners = true;
linyuh183cb712017-12-27 17:02:37 -0800565 this.inCallActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800566
567 // We attempt cleanup for the destroy case but only after we recalculate the state
568 // to see if we need to come back up or stay shut down. This is why we do the
569 // cleanup after the call to onCallListChange() instead of directly here.
570 doAttemptCleanup = true;
571 }
572
573 // Messages can come from the telephony layer while the activity is coming up
574 // and while the activity is going down. So in both cases we need to recalculate what
575 // state we should be in after they complete.
576 // Examples: (1) A new incoming call could come in and then get disconnected before
577 // the activity is created.
578 // (2) All calls could disconnect and then get a new incoming call before the
579 // activity is destroyed.
580 //
Eric Erfanian938468d2017-10-24 14:05:52 -0700581 // a bug - We previously had a check for mServiceConnected here as well, but there are
Eric Erfanianccca3152017-02-22 16:32:36 -0800582 // cases where we need to recalculate the current state even if the service in not
583 // connected. In particular the case where startOrFinish() is called while the app is
584 // already finish()ing. In that case, we skip updating the state with the knowledge that
585 // we will check again once the activity has finished. That means we have to recalculate the
586 // state here even if the service is disconnected since we may not have finished a state
587 // transition while finish()ing.
588 if (updateListeners) {
linyuh183cb712017-12-27 17:02:37 -0800589 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800590 }
591
592 if (doAttemptCleanup) {
593 attemptCleanup();
594 }
wangqi9982f0d2017-10-11 17:46:07 -0700595 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800596 }
597
erfanian12243de2018-04-17 12:17:08 -0700598 public SpeakEasyCallManager getSpeakEasyCallManager() {
599 return this.speakEasyCallManager;
600 }
601
Eric Erfanianccca3152017-02-22 16:32:36 -0800602 public void setManageConferenceActivity(
603 @Nullable ManageConferenceActivity manageConferenceActivity) {
linyuh183cb712017-12-27 17:02:37 -0800604 this.manageConferenceActivity = manageConferenceActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -0800605 }
606
607 public void onBringToForeground(boolean showDialpad) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700608 LogUtil.i("InCallPresenter.onBringToForeground", "Bringing UI to foreground.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800609 bringToForeground(showDialpad);
610 }
611
612 public void onCallAdded(final android.telecom.Call call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700613 Trace.beginSection("InCallPresenter.onCallAdded");
Eric Erfanianccca3152017-02-22 16:32:36 -0800614 LatencyReport latencyReport = new LatencyReport(call);
615 if (shouldAttemptBlocking(call)) {
616 maybeBlockCall(call, latencyReport);
617 } else {
618 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800619 externalCallList.onCallAdded(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800620 } else {
621 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800622 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800623 }
624 }
625
626 // Since a call has been added we are no longer waiting for Telecom to send us a call.
627 setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -0800628 call.registerCallback(callCallback);
zachh78e54ac2017-12-05 16:38:35 -0800629 // TODO(maxwelb): Return the future in recordPhoneLookupInfo and propagate.
linyuh183cb712017-12-27 17:02:37 -0800630 PhoneLookupHistoryRecorder.recordPhoneLookupInfo(context.getApplicationContext(), call);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700631 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800632 }
633
634 private boolean shouldAttemptBlocking(android.telecom.Call call) {
635 if (call.getState() != android.telecom.Call.STATE_RINGING) {
636 return false;
637 }
linyuh183cb712017-12-27 17:02:37 -0800638 if (!UserManagerCompat.isUserUnlocked(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700639 LogUtil.i(
640 "InCallPresenter.shouldAttemptBlocking",
641 "not attempting to block incoming call because user is locked");
642 return false;
643 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800644 if (TelecomCallUtil.isEmergencyCall(call)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700645 LogUtil.i(
646 "InCallPresenter.shouldAttemptBlocking",
647 "Not attempting to block incoming emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800648 return false;
649 }
linyuh183cb712017-12-27 17:02:37 -0800650 if (FilteredNumbersUtil.hasRecentEmergencyCall(context)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700651 LogUtil.i(
652 "InCallPresenter.shouldAttemptBlocking",
653 "Not attempting to block incoming call due to recent emergency call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800654 return false;
655 }
656 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
657 return false;
658 }
linyuh183cb712017-12-27 17:02:37 -0800659 if (FilteredNumberCompat.useNewFiltering(context)) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700660 LogUtil.i(
661 "InCallPresenter.shouldAttemptBlocking",
662 "not attempting to block incoming call because framework blocking is in use");
663 return false;
664 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800665 return true;
666 }
667
668 /**
669 * Checks whether a call should be blocked, and blocks it if so. Otherwise, it adds the call to
670 * the CallList so it can proceed as normal. There is a timeout, so if the function for checking
671 * whether a function is blocked does not return in a reasonable time, we proceed with adding the
672 * call anyways.
673 */
674 private void maybeBlockCall(final android.telecom.Call call, final LatencyReport latencyReport) {
linyuh183cb712017-12-27 17:02:37 -0800675 final String countryIso = GeoUtil.getCurrentCountryIso(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800676 final String number = TelecomCallUtil.getNumber(call);
677 final long timeAdded = System.currentTimeMillis();
678
679 // Though AtomicBoolean's can be scary, don't fear, as in this case it is only used on the
680 // main UI thread. It is needed so we can change its value within different scopes, since
681 // that cannot be done with a final boolean.
682 final AtomicBoolean hasTimedOut = new AtomicBoolean(false);
683
684 final Handler handler = new Handler();
685
686 // Proceed if the query is slow; the call may still be blocked after the query returns.
687 final Runnable runnable =
688 new Runnable() {
689 @Override
690 public void run() {
691 hasTimedOut.set(true);
692 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800693 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800694 }
695 };
696 handler.postDelayed(runnable, BLOCK_QUERY_TIMEOUT_MS);
697
698 OnCheckBlockedListener onCheckBlockedListener =
699 new OnCheckBlockedListener() {
700 @Override
701 public void onCheckComplete(final Integer id) {
702 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700703 LogUtil.i("InCallPresenter.onCheckComplete", "torn down, not adding call");
Eric Erfanianccca3152017-02-22 16:32:36 -0800704 return;
705 }
706 if (!hasTimedOut.get()) {
707 handler.removeCallbacks(runnable);
708 }
709 if (id == null) {
710 if (!hasTimedOut.get()) {
711 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800712 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800713 }
714 } else if (id == FilteredNumberAsyncQueryHandler.INVALID_ID) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700715 LogUtil.d(
716 "InCallPresenter.onCheckComplete", "invalid number, skipping block checking");
Eric Erfanianccca3152017-02-22 16:32:36 -0800717 if (!hasTimedOut.get()) {
718 handler.removeCallbacks(runnable);
719
720 latencyReport.onCallBlockingDone();
linyuh183cb712017-12-27 17:02:37 -0800721 callList.onCallAdded(context, call, latencyReport);
Eric Erfanianccca3152017-02-22 16:32:36 -0800722 }
723 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700724 LogUtil.i(
725 "InCallPresenter.onCheckComplete", "Rejecting incoming call from blocked number");
Eric Erfanianccca3152017-02-22 16:32:36 -0800726 call.reject(false, null);
linyuh183cb712017-12-27 17:02:37 -0800727 Logger.get(context).logInteraction(InteractionEvent.Type.CALL_BLOCKED);
Eric Erfanianccca3152017-02-22 16:32:36 -0800728
729 /*
730 * If mContext is null, then the InCallPresenter was torn down before the
731 * block check had a chance to complete. The context is no longer valid, so
732 * don't attempt to remove the call log entry.
733 */
linyuh183cb712017-12-27 17:02:37 -0800734 if (context == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800735 return;
736 }
737 // Register observer to update the call log.
738 // BlockedNumberContentObserver will unregister after successful log or timeout.
739 BlockedNumberContentObserver contentObserver =
linyuh183cb712017-12-27 17:02:37 -0800740 new BlockedNumberContentObserver(context, new Handler(), number, timeAdded);
Eric Erfanianccca3152017-02-22 16:32:36 -0800741 contentObserver.register();
742 }
743 }
744 };
745
linyuh183cb712017-12-27 17:02:37 -0800746 filteredQueryHandler.isBlockedNumber(onCheckBlockedListener, number, countryIso);
Eric Erfanianccca3152017-02-22 16:32:36 -0800747 }
748
749 public void onCallRemoved(android.telecom.Call call) {
750 if (call.getDetails().hasProperty(CallCompat.Details.PROPERTY_IS_EXTERNAL_CALL)) {
linyuh183cb712017-12-27 17:02:37 -0800751 externalCallList.onCallRemoved(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800752 } else {
linyuh183cb712017-12-27 17:02:37 -0800753 callList.onCallRemoved(context, call);
754 call.unregisterCallback(callCallback);
Eric Erfanianccca3152017-02-22 16:32:36 -0800755 }
756 }
757
758 public void onCanAddCallChanged(boolean canAddCall) {
linyuh183cb712017-12-27 17:02:37 -0800759 for (CanAddCallListener listener : canAddCallListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800760 listener.onCanAddCallChanged(canAddCall);
761 }
762 }
763
764 @Override
765 public void onWiFiToLteHandover(DialerCall call) {
yueg32d9ae72018-04-05 17:35:56 -0700766 if (call.hasShownWiFiToLteHandoverToast()) {
767 return;
Eric Erfanianccca3152017-02-22 16:32:36 -0800768 }
yueg32d9ae72018-04-05 17:35:56 -0700769
770 Toast.makeText(context, R.string.video_call_wifi_to_lte_handover_toast, Toast.LENGTH_LONG)
771 .show();
772 call.setHasShownWiFiToLteHandoverToast();
Eric Erfanianccca3152017-02-22 16:32:36 -0800773 }
774
775 @Override
776 public void onHandoverToWifiFailed(DialerCall call) {
linyuh183cb712017-12-27 17:02:37 -0800777 if (inCallActivity != null) {
778 inCallActivity.showDialogOrToastForWifiHandoverFailure(call);
yueg32d9ae72018-04-05 17:35:56 -0700779 } else {
780 Toast.makeText(context, R.string.video_call_lte_to_wifi_failed_message, Toast.LENGTH_SHORT)
781 .show();
Eric Erfanianccca3152017-02-22 16:32:36 -0800782 }
783 }
784
Eric Erfanianc857f902017-05-15 14:05:33 -0700785 @Override
786 public void onInternationalCallOnWifi(@NonNull DialerCall call) {
787 LogUtil.enterBlock("InCallPresenter.onInternationalCallOnWifi");
yuegb47528e2018-04-24 12:12:57 -0700788
789 if (!InternationalCallOnWifiDialogFragment.shouldShow(context)) {
790 LogUtil.i(
791 "InCallPresenter.onInternationalCallOnWifi",
792 "InternationalCallOnWifiDialogFragment.shouldShow returned false");
793 return;
794 }
795
linyuh183cb712017-12-27 17:02:37 -0800796 if (inCallActivity != null) {
797 inCallActivity.showDialogForInternationalCallOnWifi(call);
yuegb47528e2018-04-24 12:12:57 -0700798 } else {
799 Intent intent = new Intent(context, InternationalCallOnWifiDialogActivity.class);
800 // Prevent showing MainActivity with InternationalCallOnWifiDialogActivity on above
801 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
802 intent.putExtra(InternationalCallOnWifiDialogActivity.EXTRA_CALL_ID, call.getId());
803 context.startActivity(intent);
Eric Erfanianc857f902017-05-15 14:05:33 -0700804 }
805 }
806
Eric Erfanianccca3152017-02-22 16:32:36 -0800807 /**
808 * Called when there is a change to the call list. Sets the In-Call state for the entire in-call
809 * app based on the information it gets from CallList. Dispatches the in-call state to all
810 * listeners. Can trigger the creation or destruction of the UI based on the states that is
811 * calculates.
812 */
813 @Override
814 public void onCallListChange(CallList callList) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700815 Trace.beginSection("InCallPresenter.onCallListChange");
linyuh183cb712017-12-27 17:02:37 -0800816 if (inCallActivity != null && inCallActivity.isInCallScreenAnimating()) {
817 awaitingCallListUpdate = true;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700818 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800819 return;
820 }
821 if (callList == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700822 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800823 return;
824 }
825
linyuh183cb712017-12-27 17:02:37 -0800826 awaitingCallListUpdate = false;
Eric Erfanianccca3152017-02-22 16:32:36 -0800827
828 InCallState newState = getPotentialStateFromCallList(callList);
linyuh183cb712017-12-27 17:02:37 -0800829 InCallState oldState = inCallState;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700830 LogUtil.d(
831 "InCallPresenter.onCallListChange",
832 "onCallListChange oldState= " + oldState + " newState=" + newState);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700833
834 // If the user placed a call and was asked to choose the account, but then pressed "Home", the
835 // incall activity for that call will still exist (even if it's not visible). In the case of
836 // an incoming call in that situation, just disconnect that "waiting for account" call and
837 // dismiss the dialog. The same activity will be reused to handle the new incoming call. See
Eric Erfanian938468d2017-10-24 14:05:52 -0700838 // a bug for more details.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700839 DialerCall waitingForAccountCall;
840 if (newState == InCallState.INCOMING
841 && (waitingForAccountCall = callList.getWaitingForAccountCall()) != null) {
842 waitingForAccountCall.disconnect();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700843 // The InCallActivity might be destroyed or not started yet at this point.
844 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -0800845 inCallActivity.dismissPendingDialogs();
Eric Erfanian2ca43182017-08-31 06:57:16 -0700846 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700847 }
848
Eric Erfanianccca3152017-02-22 16:32:36 -0800849 newState = startOrFinishUi(newState);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700850 LogUtil.d(
851 "InCallPresenter.onCallListChange", "onCallListChange newState changed to " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800852
853 // Set the new state before announcing it to the world
Eric Erfanian2ca43182017-08-31 06:57:16 -0700854 LogUtil.i(
855 "InCallPresenter.onCallListChange",
856 "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800857 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800858
yuegb9103042018-03-30 12:12:25 -0700859 // Foreground call changed
860 DialerCall primary = null;
861 if (newState == InCallState.INCOMING) {
862 primary = callList.getIncomingCall();
863 } else if (newState == InCallState.PENDING_OUTGOING || newState == InCallState.OUTGOING) {
864 primary = callList.getOutgoingCall();
865 if (primary == null) {
866 primary = callList.getPendingOutgoingCall();
867 }
868 } else if (newState == InCallState.INCALL) {
869 primary = getCallToDisplay(callList, null, false);
870 }
871 if (primary != null) {
872 onForegroundCallChanged(primary);
873 }
874
Eric Erfanianccca3152017-02-22 16:32:36 -0800875 // notify listeners of new state
linyuh183cb712017-12-27 17:02:37 -0800876 for (InCallStateListener listener : listeners) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700877 LogUtil.d(
878 "InCallPresenter.onCallListChange",
linyuh183cb712017-12-27 17:02:37 -0800879 "Notify " + listener + " of state " + inCallState.toString());
880 listener.onStateChange(oldState, inCallState, callList);
Eric Erfanianccca3152017-02-22 16:32:36 -0800881 }
882
883 if (isActivityStarted()) {
884 final boolean hasCall =
885 callList.getActiveOrBackgroundCall() != null || callList.getOutgoingCall() != null;
linyuh183cb712017-12-27 17:02:37 -0800886 inCallActivity.dismissKeyguard(hasCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800887 }
twyen27c37182018-04-24 11:48:12 -0700888
Eric Erfanian2ca43182017-08-31 06:57:16 -0700889 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800890 }
891
yuegb9103042018-03-30 12:12:25 -0700892 /**
893 * Get the highest priority call to display. Goes through the calls and chooses which to return
894 * based on priority of which type of call to display to the user. Callers can use the "ignore"
895 * feature to get the second best call by passing a previously found primary call as ignore.
896 *
897 * @param ignore A call to ignore if found.
898 */
899 static DialerCall getCallToDisplay(
900 CallList callList, DialerCall ignore, boolean skipDisconnected) {
901 // Active calls come second. An active call always gets precedent.
902 DialerCall retval = callList.getActiveCall();
903 if (retval != null && retval != ignore) {
904 return retval;
905 }
906
907 // Sometimes there is intemediate state that two calls are in active even one is about
908 // to be on hold.
909 retval = callList.getSecondActiveCall();
910 if (retval != null && retval != ignore) {
911 return retval;
912 }
913
914 // Disconnected calls get primary position if there are no active calls
915 // to let user know quickly what call has disconnected. Disconnected
916 // calls are very short lived.
917 if (!skipDisconnected) {
918 retval = callList.getDisconnectingCall();
919 if (retval != null && retval != ignore) {
920 return retval;
921 }
922 retval = callList.getDisconnectedCall();
923 if (retval != null && retval != ignore) {
924 return retval;
925 }
926 }
927
928 // Then we go to background call (calls on hold)
929 retval = callList.getBackgroundCall();
930 if (retval != null && retval != ignore) {
931 return retval;
932 }
933
934 // Lastly, we go to a second background call.
935 retval = callList.getSecondBackgroundCall();
936
937 return retval;
938 }
939
Eric Erfanianccca3152017-02-22 16:32:36 -0800940 /** Called when there is a new incoming call. */
941 @Override
942 public void onIncomingCall(DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700943 Trace.beginSection("InCallPresenter.onIncomingCall");
Eric Erfanianccca3152017-02-22 16:32:36 -0800944 InCallState newState = startOrFinishUi(InCallState.INCOMING);
linyuh183cb712017-12-27 17:02:37 -0800945 InCallState oldState = inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800946
Eric Erfanian2ca43182017-08-31 06:57:16 -0700947 LogUtil.i(
948 "InCallPresenter.onIncomingCall", "Phone switching state: " + oldState + " -> " + newState);
linyuh183cb712017-12-27 17:02:37 -0800949 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800950
wangqicf61ca02017-08-31 15:32:55 -0700951 Trace.beginSection("listener.onIncomingCall");
linyuh183cb712017-12-27 17:02:37 -0800952 for (IncomingCallListener listener : incomingCallListeners) {
953 listener.onIncomingCall(oldState, inCallState, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800954 }
wangqicf61ca02017-08-31 15:32:55 -0700955 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800956
wangqicf61ca02017-08-31 15:32:55 -0700957 Trace.beginSection("onPrimaryCallStateChanged");
linyuh183cb712017-12-27 17:02:37 -0800958 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800959 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800960 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800961 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700962 Trace.endSection();
wangqicf61ca02017-08-31 15:32:55 -0700963 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800964 }
965
966 @Override
967 public void onUpgradeToVideo(DialerCall call) {
Eric Erfanian90508232017-03-24 09:31:16 -0700968 if (VideoUtils.hasReceivedVideoUpgradeRequest(call.getVideoTech().getSessionModificationState())
linyuh183cb712017-12-27 17:02:37 -0800969 && inCallState == InCallPresenter.InCallState.INCOMING) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800970 LogUtil.i(
971 "InCallPresenter.onUpgradeToVideo",
972 "rejecting upgrade request due to existing incoming call");
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700973 call.getVideoTech().declineVideoRequest();
Eric Erfanianccca3152017-02-22 16:32:36 -0800974 }
975
linyuh183cb712017-12-27 17:02:37 -0800976 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800977 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -0800978 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -0800979 }
980 }
981
982 @Override
wangqibc28ea72018-04-02 16:23:00 -0700983 public void onUpgradeToRtt(DialerCall call, int rttRequestId) {
984 if (inCallActivity != null) {
985 inCallActivity.showDialogForRttRequest(call, rttRequestId);
986 }
987 }
988
989 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700990 public void onSessionModificationStateChange(DialerCall call) {
991 int newState = call.getVideoTech().getSessionModificationState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800992 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "state: %d", newState);
linyuh183cb712017-12-27 17:02:37 -0800993 if (proximitySensor == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800994 LogUtil.i("InCallPresenter.onSessionModificationStateChange", "proximitySensor is null");
995 return;
996 }
linyuh183cb712017-12-27 17:02:37 -0800997 proximitySensor.setIsAttemptingVideoCall(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700998 call.hasSentVideoUpgradeRequest() || call.hasReceivedVideoUpgradeRequest());
linyuh183cb712017-12-27 17:02:37 -0800999 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001000 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001001 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001002 }
1003 }
1004
1005 /**
1006 * Called when a call becomes disconnected. Called everytime an existing call changes from being
1007 * connected (incoming/outgoing/active) to disconnected.
1008 */
1009 @Override
1010 public void onDisconnect(DialerCall call) {
linyuh7b86f562017-11-16 11:24:09 -08001011 showDialogOrToastForDisconnectedCall(call);
Eric Erfanianccca3152017-02-22 16:32:36 -08001012
1013 // We need to do the run the same code as onCallListChange.
linyuh183cb712017-12-27 17:02:37 -08001014 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001015
1016 if (isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001017 inCallActivity.dismissKeyguard(false);
Eric Erfanianccca3152017-02-22 16:32:36 -08001018 }
1019
1020 if (call.isEmergencyCall()) {
linyuh183cb712017-12-27 17:02:37 -08001021 FilteredNumbersUtil.recordLastEmergencyCallTime(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001022 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001023
linyuh183cb712017-12-27 17:02:37 -08001024 if (!callList.hasLiveCall()
Eric Erfanianfc37b022017-03-21 10:11:17 -07001025 && !call.getLogState().isIncoming
Eric Erfanian10b34a52017-05-04 08:23:17 -07001026 && !isSecretCode(call.getNumber())
wangqi9982f0d2017-10-11 17:46:07 -07001027 && !call.isVoiceMailNumber()) {
linyuh183cb712017-12-27 17:02:37 -08001028 PostCall.onCallDisconnected(context, call.getNumber(), call.getConnectTimeMillis());
Eric Erfanianccca3152017-02-22 16:32:36 -08001029 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001030 }
1031
Eric Erfanian10b34a52017-05-04 08:23:17 -07001032 private boolean isSecretCode(@Nullable String number) {
1033 return number != null
1034 && (number.length() <= 8 || number.startsWith("*#*#") || number.endsWith("#*#*"));
1035 }
1036
Eric Erfanianccca3152017-02-22 16:32:36 -08001037 /** Given the call list, return the state in which the in-call screen should be. */
1038 public InCallState getPotentialStateFromCallList(CallList callList) {
1039
1040 InCallState newState = InCallState.NO_CALLS;
1041
1042 if (callList == null) {
1043 return newState;
1044 }
1045 if (callList.getIncomingCall() != null) {
1046 newState = InCallState.INCOMING;
1047 } else if (callList.getWaitingForAccountCall() != null) {
1048 newState = InCallState.WAITING_FOR_ACCOUNT;
1049 } else if (callList.getPendingOutgoingCall() != null) {
1050 newState = InCallState.PENDING_OUTGOING;
1051 } else if (callList.getOutgoingCall() != null) {
1052 newState = InCallState.OUTGOING;
1053 } else if (callList.getActiveCall() != null
1054 || callList.getBackgroundCall() != null
1055 || callList.getDisconnectedCall() != null
1056 || callList.getDisconnectingCall() != null) {
1057 newState = InCallState.INCALL;
1058 }
1059
1060 if (newState == InCallState.NO_CALLS) {
linyuh183cb712017-12-27 17:02:37 -08001061 if (boundAndWaitingForOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001062 return InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001063 }
1064 }
1065
1066 return newState;
1067 }
1068
1069 public boolean isBoundAndWaitingForOutgoingCall() {
linyuh183cb712017-12-27 17:02:37 -08001070 return boundAndWaitingForOutgoingCall;
Eric Erfanianccca3152017-02-22 16:32:36 -08001071 }
1072
1073 public void setBoundAndWaitingForOutgoingCall(boolean isBound, PhoneAccountHandle handle) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001074 LogUtil.i(
1075 "InCallPresenter.setBoundAndWaitingForOutgoingCall",
1076 "setBoundAndWaitingForOutgoingCall: " + isBound);
linyuh183cb712017-12-27 17:02:37 -08001077 boundAndWaitingForOutgoingCall = isBound;
1078 themeColorManager.setPendingPhoneAccountHandle(handle);
1079 if (isBound && inCallState == InCallState.NO_CALLS) {
1080 inCallState = InCallState.PENDING_OUTGOING;
Eric Erfanianccca3152017-02-22 16:32:36 -08001081 }
1082 }
1083
1084 public void onShrinkAnimationComplete() {
linyuh183cb712017-12-27 17:02:37 -08001085 if (awaitingCallListUpdate) {
1086 onCallListChange(callList);
Eric Erfanianccca3152017-02-22 16:32:36 -08001087 }
1088 }
1089
1090 public void addIncomingCallListener(IncomingCallListener listener) {
1091 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001092 incomingCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001093 }
1094
1095 public void removeIncomingCallListener(IncomingCallListener listener) {
1096 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001097 incomingCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001098 }
1099 }
1100
1101 public void addListener(InCallStateListener listener) {
1102 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001103 listeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001104 }
1105
1106 public void removeListener(InCallStateListener listener) {
1107 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001108 listeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001109 }
1110 }
1111
1112 public void addDetailsListener(InCallDetailsListener listener) {
1113 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001114 detailsListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001115 }
1116
1117 public void removeDetailsListener(InCallDetailsListener listener) {
1118 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001119 detailsListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001120 }
1121 }
1122
1123 public void addCanAddCallListener(CanAddCallListener listener) {
1124 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001125 canAddCallListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001126 }
1127
1128 public void removeCanAddCallListener(CanAddCallListener listener) {
1129 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001130 canAddCallListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001131 }
1132 }
1133
1134 public void addOrientationListener(InCallOrientationListener listener) {
1135 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001136 orientationListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001137 }
1138
1139 public void removeOrientationListener(InCallOrientationListener listener) {
1140 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001141 orientationListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001142 }
1143 }
1144
1145 public void addInCallEventListener(InCallEventListener listener) {
1146 Objects.requireNonNull(listener);
linyuh183cb712017-12-27 17:02:37 -08001147 inCallEventListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001148 }
1149
1150 public void removeInCallEventListener(InCallEventListener listener) {
1151 if (listener != null) {
linyuh183cb712017-12-27 17:02:37 -08001152 inCallEventListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001153 }
1154 }
1155
1156 public ProximitySensor getProximitySensor() {
linyuh183cb712017-12-27 17:02:37 -08001157 return proximitySensor;
Eric Erfanianccca3152017-02-22 16:32:36 -08001158 }
1159
1160 public PseudoScreenState getPseudoScreenState() {
linyuh183cb712017-12-27 17:02:37 -08001161 return pseudoScreenState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001162 }
1163
1164 /** Returns true if the incall app is the foreground application. */
1165 public boolean isShowingInCallUi() {
1166 if (!isActivityStarted()) {
1167 return false;
1168 }
linyuh183cb712017-12-27 17:02:37 -08001169 if (manageConferenceActivity != null && manageConferenceActivity.isVisible()) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001170 return true;
1171 }
linyuh183cb712017-12-27 17:02:37 -08001172 return inCallActivity.isVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001173 }
1174
1175 /**
1176 * Returns true if the activity has been created and is running. Returns true as long as activity
1177 * is not destroyed or finishing. This ensures that we return true even if the activity is paused
1178 * (not in foreground).
1179 */
1180 public boolean isActivityStarted() {
linyuh183cb712017-12-27 17:02:37 -08001181 return (inCallActivity != null
1182 && !inCallActivity.isDestroyed()
1183 && !inCallActivity.isFinishing());
Eric Erfanianccca3152017-02-22 16:32:36 -08001184 }
1185
1186 /**
1187 * Determines if the In-Call app is currently changing configuration.
1188 *
1189 * @return {@code true} if the In-Call app is changing configuration.
1190 */
1191 public boolean isChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001192 return isChangingConfigurations;
Eric Erfanianccca3152017-02-22 16:32:36 -08001193 }
1194
1195 /**
1196 * Tracks whether the In-Call app is currently in the process of changing configuration (i.e.
1197 * screen orientation).
1198 */
1199 /*package*/
1200 void updateIsChangingConfigurations() {
linyuh183cb712017-12-27 17:02:37 -08001201 isChangingConfigurations = false;
1202 if (inCallActivity != null) {
1203 isChangingConfigurations = inCallActivity.isChangingConfigurations();
Eric Erfanianccca3152017-02-22 16:32:36 -08001204 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001205 LogUtil.v(
1206 "InCallPresenter.updateIsChangingConfigurations",
linyuh183cb712017-12-27 17:02:37 -08001207 "updateIsChangingConfigurations = " + isChangingConfigurations);
Eric Erfanianccca3152017-02-22 16:32:36 -08001208 }
1209
yueg10f6e822018-01-17 15:32:18 -08001210 /** Called when the activity goes in/out of the foreground. */
1211 public void onUiShowing(boolean showing) {
linyuh183cb712017-12-27 17:02:37 -08001212 if (proximitySensor != null) {
1213 proximitySensor.onInCallShowing(showing);
Eric Erfanianccca3152017-02-22 16:32:36 -08001214 }
1215
yueg092b21c2017-11-15 16:20:07 -08001216 if (!showing) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001217 updateIsChangingConfigurations();
1218 }
1219
linyuh183cb712017-12-27 17:02:37 -08001220 for (InCallUiListener listener : inCallUiListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001221 listener.onUiShowing(showing);
1222 }
1223
linyuh183cb712017-12-27 17:02:37 -08001224 if (inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001225 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001226 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanianccca3152017-02-22 16:32:36 -08001227 }
1228 }
1229
Eric Erfanian2ca43182017-08-31 06:57:16 -07001230 public void refreshUi() {
linyuh183cb712017-12-27 17:02:37 -08001231 if (inCallActivity != null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001232 // Re-evaluate which fragment is being shown.
linyuh183cb712017-12-27 17:02:37 -08001233 inCallActivity.onPrimaryCallStateChanged();
Eric Erfanian2ca43182017-08-31 06:57:16 -07001234 }
1235 }
1236
Eric Erfanianccca3152017-02-22 16:32:36 -08001237 public void addInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001238 inCallUiListeners.add(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001239 }
1240
1241 public boolean removeInCallUiListener(InCallUiListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001242 return inCallUiListeners.remove(listener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001243 }
1244
1245 /*package*/
1246 void onActivityStarted() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001247 LogUtil.d("InCallPresenter.onActivityStarted", "onActivityStarted");
Eric Erfanianccca3152017-02-22 16:32:36 -08001248 notifyVideoPauseController(true);
Eric Erfanian2ca43182017-08-31 06:57:16 -07001249 applyScreenTimeout();
Eric Erfanianccca3152017-02-22 16:32:36 -08001250 }
1251
1252 /*package*/
1253 void onActivityStopped() {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001254 LogUtil.d("InCallPresenter.onActivityStopped", "onActivityStopped");
Eric Erfanianccca3152017-02-22 16:32:36 -08001255 notifyVideoPauseController(false);
1256 }
1257
1258 private void notifyVideoPauseController(boolean showing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001259 LogUtil.d(
1260 "InCallPresenter.notifyVideoPauseController",
linyuh183cb712017-12-27 17:02:37 -08001261 "mIsChangingConfigurations=" + isChangingConfigurations);
1262 if (!isChangingConfigurations) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001263 VideoPauseController.getInstance().onUiShowing(showing);
1264 }
1265 }
1266
1267 /** Brings the app into the foreground if possible. */
1268 public void bringToForeground(boolean showDialpad) {
1269 // Before we bring the incall UI to the foreground, we check to see if:
1270 // 1. It is not currently in the foreground
1271 // 2. We are in a state where we want to show the incall ui (i.e. there are calls to
1272 // be displayed)
1273 // If the activity hadn't actually been started previously, yet there are still calls
1274 // present (e.g. a call was accepted by a bluetooth or wired headset), we want to
1275 // bring it up the UI regardless.
linyuh183cb712017-12-27 17:02:37 -08001276 if (!isShowingInCallUi() && inCallState != InCallState.NO_CALLS) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001277 showInCall(showDialpad, false /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001278 }
1279 }
1280
1281 public void onPostDialCharWait(String callId, String chars) {
yuega489f512018-04-25 12:01:09 -07001282 // If not visible, inCallActivity is stopped. Starting from P, calling recreate() will destroy
1283 // the old activity instance and create a new instance immediately. Previously, the old activity
1284 // went through its lifecycle from create to destroy before creating a new instance.
1285 // So this case doesn't work now: make a call with char WAIT, leave in call UI, call gets
1286 // connected, and go back to in call UI to see the dialog.
1287 // So we should show dialog in an empty activity if inCallActivity is not visible. And it also
1288 // helps with background calling.
1289 if (isActivityStarted() && inCallActivity.isVisible()) {
linyuh183cb712017-12-27 17:02:37 -08001290 inCallActivity.showDialogForPostCharWait(callId, chars);
yuega489f512018-04-25 12:01:09 -07001291 } else {
1292 Intent intent = new Intent(context, PostCharDialogActivity.class);
1293 // Prevent showing MainActivity with PostCharDialogActivity on above
1294 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
1295 intent.putExtra(PostCharDialogActivity.EXTRA_CALL_ID, callId);
1296 intent.putExtra(PostCharDialogActivity.EXTRA_POST_DIAL_STRING, chars);
1297 context.startActivity(intent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001298 }
1299 }
1300
1301 /**
1302 * Handles the green CALL key while in-call.
1303 *
1304 * @return true if we consumed the event.
1305 */
1306 public boolean handleCallKey() {
1307 LogUtil.v("InCallPresenter.handleCallKey", null);
1308
1309 // The green CALL button means either "Answer", "Unhold", or
1310 // "Swap calls", or can be a no-op, depending on the current state
1311 // of the Phone.
1312
1313 /** INCOMING CALL */
linyuh183cb712017-12-27 17:02:37 -08001314 final CallList calls = callList;
Eric Erfanianccca3152017-02-22 16:32:36 -08001315 final DialerCall incomingCall = calls.getIncomingCall();
1316 LogUtil.v("InCallPresenter.handleCallKey", "incomingCall: " + incomingCall);
1317
1318 // (1) Attempt to answer a call
1319 if (incomingCall != null) {
1320 incomingCall.answer(VideoProfile.STATE_AUDIO_ONLY);
1321 return true;
1322 }
1323
1324 /** STATE_ACTIVE CALL */
1325 final DialerCall activeCall = calls.getActiveCall();
1326 if (activeCall != null) {
1327 // TODO: This logic is repeated from CallButtonPresenter.java. We should
1328 // consolidate this logic.
1329 final boolean canMerge =
1330 activeCall.can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE);
1331 final boolean canSwap =
1332 activeCall.can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE);
1333
Eric Erfanian2ca43182017-08-31 06:57:16 -07001334 LogUtil.v(
1335 "InCallPresenter.handleCallKey",
1336 "activeCall: " + activeCall + ", canMerge: " + canMerge + ", canSwap: " + canSwap);
Eric Erfanianccca3152017-02-22 16:32:36 -08001337
1338 // (2) Attempt actions on conference calls
1339 if (canMerge) {
1340 TelecomAdapter.getInstance().merge(activeCall.getId());
1341 return true;
1342 } else if (canSwap) {
1343 TelecomAdapter.getInstance().swap(activeCall.getId());
1344 return true;
1345 }
1346 }
1347
1348 /** BACKGROUND CALL */
1349 final DialerCall heldCall = calls.getBackgroundCall();
1350 if (heldCall != null) {
1351 // We have a hold call so presumeable it will always support HOLD...but
1352 // there is no harm in double checking.
1353 final boolean canHold = heldCall.can(android.telecom.Call.Details.CAPABILITY_HOLD);
1354
Eric Erfanian2ca43182017-08-31 06:57:16 -07001355 LogUtil.v("InCallPresenter.handleCallKey", "heldCall: " + heldCall + ", canHold: " + canHold);
Eric Erfanianccca3152017-02-22 16:32:36 -08001356
1357 // (4) unhold call
wangqibb94ca62018-04-27 14:34:04 -07001358 if (heldCall.getState() == DialerCallState.ONHOLD && canHold) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001359 heldCall.unhold();
1360 return true;
1361 }
1362 }
1363
1364 // Always consume hard keys
1365 return true;
1366 }
1367
Eric Erfanianccca3152017-02-22 16:32:36 -08001368 /** Clears the previous fullscreen state. */
1369 public void clearFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001370 isFullScreen = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001371 }
1372
1373 /**
1374 * Changes the fullscreen mode of the in-call UI.
1375 *
1376 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1377 * otherwise.
1378 */
1379 public void setFullScreen(boolean isFullScreen) {
1380 setFullScreen(isFullScreen, false /* force */);
1381 }
1382
1383 /**
1384 * Changes the fullscreen mode of the in-call UI.
1385 *
1386 * @param isFullScreen {@code true} if in-call should be in fullscreen mode, {@code false}
1387 * otherwise.
1388 * @param force {@code true} if fullscreen mode should be set regardless of its current state.
1389 */
1390 public void setFullScreen(boolean isFullScreen, boolean force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001391 LogUtil.i("InCallPresenter.setFullScreen", "setFullScreen = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001392
1393 // As a safeguard, ensure we cannot enter fullscreen if the dialpad is shown.
1394 if (isDialpadVisible()) {
1395 isFullScreen = false;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001396 LogUtil.v(
1397 "InCallPresenter.setFullScreen",
1398 "setFullScreen overridden as dialpad is shown = " + isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001399 }
1400
linyuh183cb712017-12-27 17:02:37 -08001401 if (this.isFullScreen == isFullScreen && !force) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001402 LogUtil.v("InCallPresenter.setFullScreen", "setFullScreen ignored as already in that state.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001403 return;
1404 }
linyuh183cb712017-12-27 17:02:37 -08001405 this.isFullScreen = isFullScreen;
1406 notifyFullscreenModeChange(this.isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001407 }
1408
1409 /**
1410 * @return {@code true} if the in-call ui is currently in fullscreen mode, {@code false}
1411 * otherwise.
1412 */
1413 public boolean isFullscreen() {
linyuh183cb712017-12-27 17:02:37 -08001414 return isFullScreen;
Eric Erfanianccca3152017-02-22 16:32:36 -08001415 }
1416
1417 /**
1418 * Called by the {@link VideoCallPresenter} to inform of a change in full screen video status.
1419 *
1420 * @param isFullscreenMode {@code True} if entering full screen mode.
1421 */
1422 public void notifyFullscreenModeChange(boolean isFullscreenMode) {
linyuh183cb712017-12-27 17:02:37 -08001423 for (InCallEventListener listener : inCallEventListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001424 listener.onFullscreenModeChanged(isFullscreenMode);
1425 }
1426 }
1427
linyuh7b86f562017-11-16 11:24:09 -08001428 /** Instruct the in-call activity to show an error dialog or toast for a disconnected call. */
1429 private void showDialogOrToastForDisconnectedCall(DialerCall call) {
wangqibb94ca62018-04-27 14:34:04 -07001430 if (call.getState() != DialerCallState.DISCONNECTED) {
linyuh7b86f562017-11-16 11:24:09 -08001431 return;
Eric Erfanianccca3152017-02-22 16:32:36 -08001432 }
linyuh7b86f562017-11-16 11:24:09 -08001433
1434 // For newly disconnected calls, we may want to show a dialog on specific error conditions
1435 if (call.getAccountHandle() == null && !call.isConferenceCall()) {
1436 setDisconnectCauseForMissingAccounts(call);
1437 }
1438
yueg32d9ae72018-04-05 17:35:56 -07001439 if (isActivityStarted()) {
1440 inCallActivity.showDialogOrToastForDisconnectedCall(
1441 new DisconnectMessage(inCallActivity, call));
1442 } else {
1443 CharSequence message = new DisconnectMessage(context, call).toastMessage;
1444 if (message != null) {
1445 Toast.makeText(context, message, Toast.LENGTH_LONG).show();
1446 }
1447 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001448 }
1449
1450 /**
1451 * When the state of in-call changes, this is the first method to get called. It determines if the
1452 * UI needs to be started or finished depending on the new state and does it.
1453 */
1454 private InCallState startOrFinishUi(InCallState newState) {
wangqicf61ca02017-08-31 15:32:55 -07001455 Trace.beginSection("InCallPresenter.startOrFinishUi");
Eric Erfanian2ca43182017-08-31 06:57:16 -07001456 LogUtil.d(
linyuh183cb712017-12-27 17:02:37 -08001457 "InCallPresenter.startOrFinishUi", "startOrFinishUi: " + inCallState + " -> " + newState);
Eric Erfanianccca3152017-02-22 16:32:36 -08001458
1459 // TODO: Consider a proper state machine implementation
1460
1461 // If the state isn't changing we have already done any starting/stopping of activities in
1462 // a previous pass...so lets cut out early
linyuh183cb712017-12-27 17:02:37 -08001463 if (newState == inCallState) {
wangqicf61ca02017-08-31 15:32:55 -07001464 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001465 return newState;
1466 }
1467
1468 // A new Incoming call means that the user needs to be notified of the the call (since
1469 // it wasn't them who initiated it). We do this through full screen notifications and
1470 // happens indirectly through {@link StatusBarNotifier}.
1471 //
1472 // The process for incoming calls is as follows:
1473 //
1474 // 1) CallList - Announces existence of new INCOMING call
1475 // 2) InCallPresenter - Gets announcement and calculates that the new InCallState
1476 // - should be set to INCOMING.
1477 // 3) InCallPresenter - This method is called to see if we need to start or finish
1478 // the app given the new state.
1479 // 4) StatusBarNotifier - Listens to InCallState changes. InCallPresenter calls
1480 // StatusBarNotifier explicitly to issue a FullScreen Notification
1481 // that will either start the InCallActivity or show the user a
1482 // top-level notification dialog if the user is in an immersive app.
1483 // That notification can also start the InCallActivity.
1484 // 5) InCallActivity - Main activity starts up and at the end of its onCreate will
1485 // call InCallPresenter::setActivity() to let the presenter
1486 // know that start-up is complete.
1487 //
1488 // [ AND NOW YOU'RE IN THE CALL. voila! ]
Eric Erfanianccca3152017-02-22 16:32:36 -08001489
1490 // A dialog to show on top of the InCallUI to select a PhoneAccount
1491 final boolean showAccountPicker = (InCallState.WAITING_FOR_ACCOUNT == newState);
1492
1493 // A new outgoing call indicates that the user just now dialed a number and when that
1494 // happens we need to display the screen immediately or show an account picker dialog if
1495 // no default is set. However, if the main InCallUI is already visible, we do not want to
1496 // re-initiate the start-up animation, so we do not need to do anything here.
1497 //
1498 // It is also possible to go into an intermediate state where the call has been initiated
1499 // but Telecom has not yet returned with the details of the call (handle, gateway, etc.).
1500 // This pending outgoing state can also launch the call screen.
1501 //
1502 // This is different from the incoming call sequence because we do not need to shock the
1503 // user with a top-level notification. Just show the call UI normally.
1504 boolean callCardFragmentVisible =
linyuh183cb712017-12-27 17:02:37 -08001505 inCallActivity != null && inCallActivity.getCallCardFragmentVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001506 final boolean mainUiNotVisible = !isShowingInCallUi() || !callCardFragmentVisible;
1507 boolean showCallUi = InCallState.OUTGOING == newState && mainUiNotVisible;
1508
1509 // Direct transition from PENDING_OUTGOING -> INCALL means that there was an error in the
1510 // outgoing call process, so the UI should be brought up to show an error dialog.
1511 showCallUi |=
linyuh183cb712017-12-27 17:02:37 -08001512 (InCallState.PENDING_OUTGOING == inCallState
Eric Erfanianccca3152017-02-22 16:32:36 -08001513 && InCallState.INCALL == newState
1514 && !isShowingInCallUi());
1515
1516 // Another exception - InCallActivity is in charge of disconnecting a call with no
1517 // valid accounts set. Bring the UI up if this is true for the current pending outgoing
1518 // call so that:
1519 // 1) The call can be disconnected correctly
1520 // 2) The UI comes up and correctly displays the error dialog.
1521 // TODO: Remove these special case conditions by making InCallPresenter a true state
1522 // machine. Telecom should also be the component responsible for disconnecting a call
1523 // with no valid accounts.
1524 showCallUi |=
1525 InCallState.PENDING_OUTGOING == newState
1526 && mainUiNotVisible
linyuh183cb712017-12-27 17:02:37 -08001527 && isCallWithNoValidAccounts(callList.getPendingOutgoingCall());
Eric Erfanianccca3152017-02-22 16:32:36 -08001528
1529 // The only time that we have an instance of mInCallActivity and it isn't started is
1530 // when it is being destroyed. In that case, lets avoid bringing up another instance of
1531 // the activity. When it is finally destroyed, we double check if we should bring it back
1532 // up so we aren't going to lose anything by avoiding a second startup here.
linyuh183cb712017-12-27 17:02:37 -08001533 boolean activityIsFinishing = inCallActivity != null && !isActivityStarted();
Eric Erfanianccca3152017-02-22 16:32:36 -08001534 if (activityIsFinishing) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001535 LogUtil.i(
1536 "InCallPresenter.startOrFinishUi",
linyuh183cb712017-12-27 17:02:37 -08001537 "Undo the state change: " + newState + " -> " + inCallState);
wangqicf61ca02017-08-31 15:32:55 -07001538 Trace.endSection();
linyuh183cb712017-12-27 17:02:37 -08001539 return inCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001540 }
1541
1542 // We're about the bring up the in-call UI for outgoing and incoming call. If we still have
1543 // dialogs up, we need to clear them out before showing in-call screen. This is necessary
1544 // to fix the bug that dialog will show up when data reaches limit even after makeing new
1545 // outgoing call after user ignore it by pressing home button.
1546 if ((newState == InCallState.INCOMING || newState == InCallState.PENDING_OUTGOING)
1547 && !showCallUi
1548 && isActivityStarted()) {
linyuh183cb712017-12-27 17:02:37 -08001549 inCallActivity.dismissPendingDialogs();
Eric Erfanianccca3152017-02-22 16:32:36 -08001550 }
1551
yueg4613e8f2018-04-23 13:35:33 -07001552 if ((showCallUi || showAccountPicker) && !shouldStartInBubbleMode()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001553 LogUtil.i("InCallPresenter.startOrFinishUi", "Start in call UI");
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001554 showInCall(false /* showDialpad */, !showAccountPicker /* newOutgoingCall */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001555 } else if (newState == InCallState.NO_CALLS) {
1556 // The new state is the no calls state. Tear everything down.
yueg8e544862018-04-06 14:34:59 -07001557 inCallState = newState;
Eric Erfanianccca3152017-02-22 16:32:36 -08001558 attemptFinishActivity();
1559 attemptCleanup();
1560 }
1561
wangqicf61ca02017-08-31 15:32:55 -07001562 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -08001563 return newState;
1564 }
1565
1566 /**
1567 * Sets the DisconnectCause for a call that was disconnected because it was missing a PhoneAccount
1568 * or PhoneAccounts to select from.
1569 */
1570 private void setDisconnectCauseForMissingAccounts(DialerCall call) {
1571
1572 Bundle extras = call.getIntentExtras();
1573 // Initialize the extras bundle to avoid NPE
1574 if (extras == null) {
1575 extras = new Bundle();
1576 }
1577
1578 final List<PhoneAccountHandle> phoneAccountHandles =
1579 extras.getParcelableArrayList(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS);
1580
1581 if (phoneAccountHandles == null || phoneAccountHandles.isEmpty()) {
1582 String scheme = call.getHandle().getScheme();
1583 final String errorMsg =
1584 PhoneAccount.SCHEME_TEL.equals(scheme)
linyuh183cb712017-12-27 17:02:37 -08001585 ? context.getString(R.string.callFailed_simError)
1586 : context.getString(R.string.incall_error_supp_service_unknown);
Eric Erfanianccca3152017-02-22 16:32:36 -08001587 DisconnectCause disconnectCause =
1588 new DisconnectCause(DisconnectCause.ERROR, null, errorMsg, errorMsg);
1589 call.setDisconnectCause(disconnectCause);
1590 }
1591 }
1592
Eric Erfanianccca3152017-02-22 16:32:36 -08001593 /**
1594 * @return {@code true} if the InCallPresenter is ready to be torn down, {@code false} otherwise.
1595 * Calling classes should use this as an indication whether to interact with the
1596 * InCallPresenter or not.
1597 */
1598 public boolean isReadyForTearDown() {
linyuh183cb712017-12-27 17:02:37 -08001599 return inCallActivity == null && !serviceConnected && inCallState == InCallState.NO_CALLS;
Eric Erfanianccca3152017-02-22 16:32:36 -08001600 }
1601
1602 /**
1603 * Checks to see if both the UI is gone and the service is disconnected. If so, tear it all down.
1604 */
1605 private void attemptCleanup() {
1606 if (isReadyForTearDown()) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001607 LogUtil.i("InCallPresenter.attemptCleanup", "Cleaning up");
Eric Erfanianccca3152017-02-22 16:32:36 -08001608
1609 cleanupSurfaces();
1610
linyuh183cb712017-12-27 17:02:37 -08001611 isChangingConfigurations = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001612
1613 // blow away stale contact info so that we get fresh data on
1614 // the next set of calls
linyuh183cb712017-12-27 17:02:37 -08001615 if (contactInfoCache != null) {
1616 contactInfoCache.clearCache();
Eric Erfanianccca3152017-02-22 16:32:36 -08001617 }
linyuh183cb712017-12-27 17:02:37 -08001618 contactInfoCache = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001619
linyuh183cb712017-12-27 17:02:37 -08001620 if (proximitySensor != null) {
1621 removeListener(proximitySensor);
1622 proximitySensor.tearDown();
Eric Erfanianccca3152017-02-22 16:32:36 -08001623 }
linyuh183cb712017-12-27 17:02:37 -08001624 proximitySensor = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001625
linyuh183cb712017-12-27 17:02:37 -08001626 if (statusBarNotifier != null) {
1627 removeListener(statusBarNotifier);
1628 EnrichedCallComponent.get(context)
Eric Erfaniand8046e52017-04-06 09:41:50 -07001629 .getEnrichedCallManager()
linyuh183cb712017-12-27 17:02:37 -08001630 .unregisterStateChangedListener(statusBarNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001631 }
Eric Erfaniand8046e52017-04-06 09:41:50 -07001632
linyuh183cb712017-12-27 17:02:37 -08001633 if (externalCallNotifier != null && externalCallList != null) {
1634 externalCallList.removeExternalCallListener(externalCallNotifier);
Eric Erfanianccca3152017-02-22 16:32:36 -08001635 }
linyuh183cb712017-12-27 17:02:37 -08001636 statusBarNotifier = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001637
linyuh183cb712017-12-27 17:02:37 -08001638 if (callList != null) {
1639 callList.removeListener(this);
1640 callList.removeListener(spamCallListListener);
Eric Erfanianccca3152017-02-22 16:32:36 -08001641 }
linyuh183cb712017-12-27 17:02:37 -08001642 callList = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001643
linyuh183cb712017-12-27 17:02:37 -08001644 context = null;
1645 inCallActivity = null;
1646 manageConferenceActivity = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001647
linyuh183cb712017-12-27 17:02:37 -08001648 listeners.clear();
1649 incomingCallListeners.clear();
1650 detailsListeners.clear();
1651 canAddCallListeners.clear();
1652 orientationListeners.clear();
1653 inCallEventListeners.clear();
1654 inCallUiListeners.clear();
1655 if (!inCallUiLocks.isEmpty()) {
1656 LogUtil.e("InCallPresenter.attemptCleanup", "held in call locks: " + inCallUiLocks);
1657 inCallUiLocks.clear();
twyen8efb4952017-10-06 16:35:54 -07001658 }
Eric Erfanian2ca43182017-08-31 06:57:16 -07001659 LogUtil.d("InCallPresenter.attemptCleanup", "finished");
Eric Erfanianccca3152017-02-22 16:32:36 -08001660 }
1661 }
1662
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001663 public void showInCall(boolean showDialpad, boolean newOutgoingCall) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001664 LogUtil.i("InCallPresenter.showInCall", "Showing InCallActivity");
linyuh183cb712017-12-27 17:02:37 -08001665 context.startActivity(
1666 InCallActivity.getIntent(context, showDialpad, newOutgoingCall, false /* forFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -08001667 }
1668
1669 public void onServiceBind() {
linyuh183cb712017-12-27 17:02:37 -08001670 serviceBound = true;
Eric Erfanianccca3152017-02-22 16:32:36 -08001671 }
1672
1673 public void onServiceUnbind() {
1674 InCallPresenter.getInstance().setBoundAndWaitingForOutgoingCall(false, null);
linyuh183cb712017-12-27 17:02:37 -08001675 serviceBound = false;
Eric Erfanianccca3152017-02-22 16:32:36 -08001676 }
1677
1678 public boolean isServiceBound() {
linyuh183cb712017-12-27 17:02:37 -08001679 return serviceBound;
Eric Erfanianccca3152017-02-22 16:32:36 -08001680 }
1681
1682 public void maybeStartRevealAnimation(Intent intent) {
linyuh183cb712017-12-27 17:02:37 -08001683 if (intent == null || inCallActivity != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001684 return;
1685 }
1686 final Bundle extras = intent.getBundleExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS);
1687 if (extras == null) {
1688 // Incoming call, just show the in-call UI directly.
1689 return;
1690 }
1691
1692 if (extras.containsKey(android.telecom.Call.AVAILABLE_PHONE_ACCOUNTS)) {
1693 // Account selection dialog will show up so don't show the animation.
1694 return;
1695 }
1696
1697 final PhoneAccountHandle accountHandle =
1698 intent.getParcelableExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE);
1699 final Point touchPoint = extras.getParcelable(TouchPointManager.TOUCH_POINT);
Eric Erfanianccca3152017-02-22 16:32:36 -08001700
yueg32d9ae72018-04-05 17:35:56 -07001701 setBoundAndWaitingForOutgoingCall(true, accountHandle);
1702
yueg4613e8f2018-04-23 13:35:33 -07001703 if (shouldStartInBubbleModeWithExtras(extras)) {
yueg32d9ae72018-04-05 17:35:56 -07001704 LogUtil.i("InCallPresenter.maybeStartRevealAnimation", "shouldStartInBubbleMode");
1705 // Show bubble instead of in call UI
1706 return;
1707 }
Eric Erfanianccca3152017-02-22 16:32:36 -08001708
1709 final Intent activityIntent =
linyuh183cb712017-12-27 17:02:37 -08001710 InCallActivity.getIntent(context, false, true, false /* forFullScreen */);
Eric Erfanianccca3152017-02-22 16:32:36 -08001711 activityIntent.putExtra(TouchPointManager.TOUCH_POINT, touchPoint);
linyuh183cb712017-12-27 17:02:37 -08001712 context.startActivity(activityIntent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001713 }
1714
1715 /**
1716 * Retrieves the current in-call camera manager instance, creating if necessary.
1717 *
1718 * @return The {@link InCallCameraManager}.
1719 */
1720 public InCallCameraManager getInCallCameraManager() {
1721 synchronized (this) {
linyuh183cb712017-12-27 17:02:37 -08001722 if (inCallCameraManager == null) {
1723 inCallCameraManager = new InCallCameraManager(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001724 }
1725
linyuh183cb712017-12-27 17:02:37 -08001726 return inCallCameraManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001727 }
1728 }
1729
1730 /**
1731 * Notifies listeners of changes in orientation and notify calls of rotation angle change.
1732 *
1733 * @param orientation The screen orientation of the device (one of: {@link
1734 * InCallOrientationEventListener#SCREEN_ORIENTATION_0}, {@link
1735 * InCallOrientationEventListener#SCREEN_ORIENTATION_90}, {@link
1736 * InCallOrientationEventListener#SCREEN_ORIENTATION_180}, {@link
1737 * InCallOrientationEventListener#SCREEN_ORIENTATION_270}).
1738 */
1739 public void onDeviceOrientationChange(@ScreenOrientation int orientation) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001740 LogUtil.d(
1741 "InCallPresenter.onDeviceOrientationChange",
1742 "onDeviceOrientationChange: orientation= " + orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001743
linyuh183cb712017-12-27 17:02:37 -08001744 if (callList != null) {
1745 callList.notifyCallsOfDeviceRotation(orientation);
Eric Erfanianccca3152017-02-22 16:32:36 -08001746 } else {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001747 LogUtil.w("InCallPresenter.onDeviceOrientationChange", "CallList is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001748 }
1749
1750 // Notify listeners of device orientation changed.
linyuh183cb712017-12-27 17:02:37 -08001751 for (InCallOrientationListener listener : orientationListeners) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001752 listener.onDeviceOrientationChanged(orientation);
1753 }
1754 }
1755
1756 /**
1757 * Configures the in-call UI activity so it can change orientations or not. Enables the
1758 * orientation event listener if allowOrientationChange is true, disables it if false.
1759 *
1760 * @param allowOrientationChange {@code true} if the in-call UI can change between portrait and
1761 * landscape. {@code false} if the in-call UI should be locked in portrait.
1762 */
1763 public void setInCallAllowsOrientationChange(boolean allowOrientationChange) {
linyuh183cb712017-12-27 17:02:37 -08001764 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001765 LogUtil.e(
1766 "InCallPresenter.setInCallAllowsOrientationChange",
1767 "InCallActivity is null. Can't set requested orientation.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001768 return;
1769 }
linyuh183cb712017-12-27 17:02:37 -08001770 inCallActivity.setAllowOrientationChange(allowOrientationChange);
Eric Erfanianccca3152017-02-22 16:32:36 -08001771 }
1772
1773 public void enableScreenTimeout(boolean enable) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001774 LogUtil.v("InCallPresenter.enableScreenTimeout", "enableScreenTimeout: value=" + enable);
linyuh183cb712017-12-27 17:02:37 -08001775 screenTimeoutEnabled = enable;
Eric Erfanian2ca43182017-08-31 06:57:16 -07001776 applyScreenTimeout();
1777 }
1778
1779 private void applyScreenTimeout() {
linyuh183cb712017-12-27 17:02:37 -08001780 if (inCallActivity == null) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001781 LogUtil.e("InCallPresenter.applyScreenTimeout", "InCallActivity is null.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001782 return;
1783 }
1784
linyuh183cb712017-12-27 17:02:37 -08001785 final Window window = inCallActivity.getWindow();
1786 if (screenTimeoutEnabled) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001787 window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1788 } else {
1789 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1790 }
1791 }
1792
1793 /**
1794 * Hides or shows the conference manager fragment.
1795 *
1796 * @param show {@code true} if the conference manager should be shown, {@code false} if it should
1797 * be hidden.
1798 */
1799 public void showConferenceCallManager(boolean show) {
linyuh183cb712017-12-27 17:02:37 -08001800 if (inCallActivity != null) {
1801 inCallActivity.showConferenceFragment(show);
Eric Erfanianccca3152017-02-22 16:32:36 -08001802 }
linyuh183cb712017-12-27 17:02:37 -08001803 if (!show && manageConferenceActivity != null) {
1804 manageConferenceActivity.finish();
Eric Erfanianccca3152017-02-22 16:32:36 -08001805 }
1806 }
1807
1808 /**
1809 * Determines if the dialpad is visible.
1810 *
1811 * @return {@code true} if the dialpad is visible, {@code false} otherwise.
1812 */
1813 public boolean isDialpadVisible() {
linyuh183cb712017-12-27 17:02:37 -08001814 if (inCallActivity == null) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001815 return false;
1816 }
linyuh183cb712017-12-27 17:02:37 -08001817 return inCallActivity.isDialpadVisible();
Eric Erfanianccca3152017-02-22 16:32:36 -08001818 }
1819
1820 public ThemeColorManager getThemeColorManager() {
linyuh183cb712017-12-27 17:02:37 -08001821 return themeColorManager;
Eric Erfanianccca3152017-02-22 16:32:36 -08001822 }
1823
wangqi8d662ca2017-10-26 11:27:19 -07001824 @VisibleForTesting
1825 public void setThemeColorManager(ThemeColorManager themeColorManager) {
linyuh183cb712017-12-27 17:02:37 -08001826 this.themeColorManager = themeColorManager;
wangqi8d662ca2017-10-26 11:27:19 -07001827 }
1828
Eric Erfanianccca3152017-02-22 16:32:36 -08001829 /** Called when the foreground call changes. */
1830 public void onForegroundCallChanged(DialerCall newForegroundCall) {
linyuh183cb712017-12-27 17:02:37 -08001831 themeColorManager.onForegroundCallChanged(context, newForegroundCall);
1832 if (inCallActivity != null) {
1833 inCallActivity.onForegroundCallChanged(newForegroundCall);
Eric Erfanianccca3152017-02-22 16:32:36 -08001834 }
1835 }
1836
1837 public InCallActivity getActivity() {
linyuh183cb712017-12-27 17:02:37 -08001838 return inCallActivity;
Eric Erfanianccca3152017-02-22 16:32:36 -08001839 }
1840
1841 /** Called when the UI begins, and starts the callstate callbacks if necessary. */
1842 public void setActivity(InCallActivity inCallActivity) {
1843 if (inCallActivity == null) {
1844 throw new IllegalArgumentException("registerActivity cannot be called with null");
1845 }
linyuh183cb712017-12-27 17:02:37 -08001846 if (this.inCallActivity != null && this.inCallActivity != inCallActivity) {
Eric Erfanian2ca43182017-08-31 06:57:16 -07001847 LogUtil.w(
1848 "InCallPresenter.setActivity", "Setting a second activity before destroying the first.");
Eric Erfanianccca3152017-02-22 16:32:36 -08001849 }
1850 updateActivity(inCallActivity);
1851 }
1852
1853 ExternalCallNotifier getExternalCallNotifier() {
linyuh183cb712017-12-27 17:02:37 -08001854 return externalCallNotifier;
Eric Erfanianccca3152017-02-22 16:32:36 -08001855 }
1856
1857 VideoSurfaceTexture getLocalVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001858 if (localVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001859 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001860 if (context != null) {
1861 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001862 }
linyuh183cb712017-12-27 17:02:37 -08001863 localVideoSurfaceTexture = VideoSurfaceBindings.createLocalVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001864 }
linyuh183cb712017-12-27 17:02:37 -08001865 return localVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001866 }
1867
1868 VideoSurfaceTexture getRemoteVideoSurfaceTexture() {
linyuh183cb712017-12-27 17:02:37 -08001869 if (remoteVideoSurfaceTexture == null) {
roldenburg12b50c62017-09-01 14:41:19 -07001870 boolean isPixel2017 = false;
linyuh183cb712017-12-27 17:02:37 -08001871 if (context != null) {
1872 isPixel2017 = context.getPackageManager().hasSystemFeature(PIXEL2017_SYSTEM_FEATURE);
roldenburg12b50c62017-09-01 14:41:19 -07001873 }
linyuh183cb712017-12-27 17:02:37 -08001874 remoteVideoSurfaceTexture = VideoSurfaceBindings.createRemoteVideoSurfaceTexture(isPixel2017);
Eric Erfanianccca3152017-02-22 16:32:36 -08001875 }
linyuh183cb712017-12-27 17:02:37 -08001876 return remoteVideoSurfaceTexture;
Eric Erfanianccca3152017-02-22 16:32:36 -08001877 }
1878
1879 void cleanupSurfaces() {
linyuh183cb712017-12-27 17:02:37 -08001880 if (remoteVideoSurfaceTexture != null) {
1881 remoteVideoSurfaceTexture.setDoneWithSurface();
1882 remoteVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001883 }
linyuh183cb712017-12-27 17:02:37 -08001884 if (localVideoSurfaceTexture != null) {
1885 localVideoSurfaceTexture.setDoneWithSurface();
1886 localVideoSurfaceTexture = null;
Eric Erfanianccca3152017-02-22 16:32:36 -08001887 }
1888 }
1889
yueg77cb8e52017-10-27 16:42:51 -07001890 @Override
1891 public void onAudioStateChanged(CallAudioState audioState) {
linyuh183cb712017-12-27 17:02:37 -08001892 if (statusBarNotifier != null) {
1893 statusBarNotifier.updateNotification();
yueg77cb8e52017-10-27 16:42:51 -07001894 }
1895 }
1896
Eric Erfanianccca3152017-02-22 16:32:36 -08001897 /** All the main states of InCallActivity. */
1898 public enum InCallState {
1899 // InCall Screen is off and there are no calls
1900 NO_CALLS,
1901
1902 // Incoming-call screen is up
1903 INCOMING,
1904
1905 // In-call experience is showing
1906 INCALL,
1907
1908 // Waiting for user input before placing outgoing call
1909 WAITING_FOR_ACCOUNT,
1910
1911 // UI is starting up but no call has been initiated yet.
1912 // The UI is waiting for Telecom to respond.
1913 PENDING_OUTGOING,
1914
1915 // User is dialing out
1916 OUTGOING;
1917
1918 public boolean isIncoming() {
1919 return (this == INCOMING);
1920 }
1921
1922 public boolean isConnectingOrConnected() {
1923 return (this == INCOMING || this == OUTGOING || this == INCALL);
1924 }
1925 }
1926
1927 /** Interface implemented by classes that need to know about the InCall State. */
1928 public interface InCallStateListener {
1929
1930 // TODO: Enhance state to contain the call objects instead of passing CallList
1931 void onStateChange(InCallState oldState, InCallState newState, CallList callList);
1932 }
1933
1934 public interface IncomingCallListener {
1935
1936 void onIncomingCall(InCallState oldState, InCallState newState, DialerCall call);
1937 }
1938
1939 public interface CanAddCallListener {
1940
1941 void onCanAddCallChanged(boolean canAddCall);
1942 }
1943
1944 public interface InCallDetailsListener {
1945
1946 void onDetailsChanged(DialerCall call, android.telecom.Call.Details details);
1947 }
1948
1949 public interface InCallOrientationListener {
1950
1951 void onDeviceOrientationChanged(@ScreenOrientation int orientation);
1952 }
1953
1954 /**
1955 * Interface implemented by classes that need to know about events which occur within the In-Call
1956 * UI. Used as a means of communicating between fragments that make up the UI.
1957 */
1958 public interface InCallEventListener {
1959
1960 void onFullscreenModeChanged(boolean isFullscreenMode);
1961 }
1962
1963 public interface InCallUiListener {
1964
1965 void onUiShowing(boolean showing);
1966 }
twyen8efb4952017-10-06 16:35:54 -07001967
1968 private class InCallUiLockImpl implements InCallUiLock {
1969 private final String tag;
1970
1971 private InCallUiLockImpl(String tag) {
1972 this.tag = tag;
1973 }
1974
1975 @MainThread
1976 @Override
1977 public void release() {
1978 Assert.isMainThread();
1979 releaseInCallUiLock(InCallUiLockImpl.this);
1980 }
1981
1982 @Override
1983 public String toString() {
1984 return "InCallUiLock[" + tag + "]";
1985 }
1986 }
1987
1988 @MainThread
1989 public InCallUiLock acquireInCallUiLock(String tag) {
1990 Assert.isMainThread();
1991 InCallUiLock lock = new InCallUiLockImpl(tag);
linyuh183cb712017-12-27 17:02:37 -08001992 inCallUiLocks.add(lock);
twyen8efb4952017-10-06 16:35:54 -07001993 return lock;
1994 }
1995
1996 @MainThread
1997 private void releaseInCallUiLock(InCallUiLock lock) {
1998 Assert.isMainThread();
1999 LogUtil.i("InCallPresenter.releaseInCallUiLock", "releasing %s", lock);
linyuh183cb712017-12-27 17:02:37 -08002000 inCallUiLocks.remove(lock);
2001 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002002 LogUtil.i("InCallPresenter.releaseInCallUiLock", "all locks released");
linyuh183cb712017-12-27 17:02:37 -08002003 if (inCallState == InCallState.NO_CALLS) {
twyen8efb4952017-10-06 16:35:54 -07002004 LogUtil.i("InCallPresenter.releaseInCallUiLock", "no more calls, finishing UI");
2005 attemptFinishActivity();
2006 attemptCleanup();
2007 }
2008 }
2009 }
2010
2011 @MainThread
2012 public boolean isInCallUiLocked() {
2013 Assert.isMainThread();
linyuh183cb712017-12-27 17:02:37 -08002014 if (inCallUiLocks.isEmpty()) {
twyen8efb4952017-10-06 16:35:54 -07002015 return false;
2016 }
linyuh183cb712017-12-27 17:02:37 -08002017 for (InCallUiLock lock : inCallUiLocks) {
twyen8efb4952017-10-06 16:35:54 -07002018 LogUtil.i("InCallPresenter.isInCallUiLocked", "still locked by %s", lock);
2019 }
2020 return true;
2021 }
2022
linyuh183cb712017-12-27 17:02:37 -08002023 private final Set<InCallUiLock> inCallUiLocks = new ArraySet<>();
Eric Erfanianccca3152017-02-22 16:32:36 -08002024}